SAM D21 SERCOM I²C Slave Example Project

 Objective

This page provides a code example that configures SERCOM2 as an I²C slave and, using a RAM register set, enables you to change LED blinking patterns or read the switch state.

This example targets the SAM D21 family of devices. The application is designed to work using the SAM D21 Xplained PRO Evaluation Kit (ATSAMD21-XPRO) which contains the ATSAMD21J18A Arm® Cortex®-M0+ MCU.

samd21-xplained-pro.png


This application will use:

  • SERCOM2 in I²C Slave mode,
  • Pins PA08 (SDA) and PA09 (SCL),
  • Pin PB30 (User LED - LED0), and
  • Pin PA15 (User Switch - SW0).


To run this demo, an I²C host must be present to issue the commands necessary for the communication. In this demo, we use the MCP2221 USB-I2C/UART Breakout Module (ADM00559), which can be conveniently connected to a PC where commands can be issued using the provided I²C terminal application.

mcp2221-breakout-jumpered.png


This code example uses a register-direct C-coding style (i.e., no software framework) and is built using the Arm GCC compiler toolchain which is installed along with the Atmel Studio 7 IDE.

Visit the following page to learn how to configure the SERCOM I²C peripheral registers for this application:

 
SERCOM I²C Slave Configuration
Learn more >

 Materials

Hardware Tools

Tool About Purchase
ATSAMD21-XPRO-50px.png
SAM D21 Xplained Pro
Evaluation Kit
ADM00559-50px.png
ADM00559
MCP2221 Breakout Module

Software Tools

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
swtool-28px.png
Atmel Studio
Integrated Development Environment
swtool-28px.png
MCP2221 I2C/SMBus Terminal
Terminal Program

MCP2221 I²C Terminal Application

This PC application simulates an I²C Master device and interacts with our I²C Slave application using the MCP2221 Breakout Module. Click on the collapsible link below to install this tool on Windows®:

Exercise Files

File Download
Installation
Instructions
Windows Linux Mac OSX
Example Project

We recommend extracting the ZIP file to your C:\ folder.

You should see the folder C:\MTT\32arm\samd21\code-examples-gcc\sercom\i2c-slave-demo containing the solution i2c_slave_demo.atsln.

MCP2221 Commands File

In the target folder you should also find an MCP2221 Commands File MCP2221 - CMDs For I2C Slave Demo.csv, which will be used with the I²C Terminal application:

i2c-slave-demo-commands-file.png

 Connection Diagram

SAM D21 Xplained Pro contains an Embedded Debugger (EDBG) that can be used to program and debug the ATSAMD21J18A using the Serial Wire Debug (SWD) interface. The EDBG also includes a Virtual Com port interface over UART, a Data Gateway Interface (DGI) over SPI and TWI, and it monitors four of the SAM D21 GPIOs. Atmel Studio 7 is used as a front-end for the EDBG.

The LED0 is driven by this application and is connected to port PB30, while SW0 is connected to PA15 and requires the internal pull-up for that pin enabled to function properly. The I²C pins SDA and SCL are connected to pins PA08 and PA09 respectively.

i2c-slave-demo-connection-diagram.png

The IC SDA and SCL signal pull-up resistors are mounted on the MCP2221 breakout board.

The MCP2221 board signals may be connected to the SAM D21 Xplained Pro board header pins as shown here using male-female jumper cables:

i2c-slave-demo-connection-picture.png

 Procedure

Attach the SAM D21 Xplained PRO board to your computer using a USB A-to-MicroB cable. Attach the MCP2221 breakout board to your computer using a USB A-to-MiniB cable. Connect the MCP2221 breakout board pins to the Xplained PRO pins as shown above.

If the SAMD21 Xplained PRO board has been successfully enumerated, you should see the board image come up in Atmel Studio as shown:

samd21-xplained-pro-enum-success.png

The board is identified by the last four digits of its serial number (see the sticker on the bottom of the board). In this example, the last four digits are 1344.

1

Open the Solution

Select File > Open > Project/Solution…

as7-open-systick-solution.png


Navigate to the solution folder and select the i2c_slave_demo.atsln solution file:

as7-open-i2c-slave-solution-detail.png

2

Configure the Debugger

Next, you need to configure the debugger in Atmel Studio to discover and connect to the target EDBG IC on your Xplained Pro board.

First, navigate to Project > Properties as shown:

as7-config-debugger-i2c-slave-example-1.png

Next, under the project's 'Tool' settings, select your EDBG target from the pull-down. Select 'SWD' as the interface:

as7-config-debugger-i2c-slave-example-2.png

Save the tool setting by clicking on the Save All button:

as7-config-debugger-i2c-slave-example-3.png

3

Rebuild/Program the Target

Finally, click on the Start Without Debugging icon in Atmel Studio which re-builds the HEX file from the project source code, downloads/programs the HEX file onto the target MCU, and releases the target MCU Reset pin, allowing the program to execute.

as7-start-without-debugging-i2c-slave-example.png

If prompted, upgrade the EDBG firmware on the board:

as7-edbg-firmware-upgrade.png

You need to click on Start Without Debugging again after an EDBG firmware upgrade in order to rebuild/program the target.

After the programming is complete, you should see the Amber LED LED0 on the SAM D21 Xplained PRO board toggle at a 1 second rate:

systick-initialization-example-results.png

4

Open MCP2221 I²C/SMBus Terminal Application

Open the MCP2221 I²C/SMBus Terminal Application. If the MCP2221 board is recognized, you should see the 'Select Device' field populated with the component ID on the far upper-right corner of the window:

mcp2221-terminal-select-device-view.png

If there is no device ID on that field, please refer to the MCP2221 terminal installation instructions to install the board.

5

Import Commands

Now, select File > Import Commands. Navigate to the firmware solution folder and select the file MCP2221 - CMDs For I2C Slave Demo.csv:

i2c-slave-demo-commands-file.png

You should now have the following commands listed under the Command list:

mcp2221-terminal-commands-list.png

6

Issue Read Regs Command to the I²C Slave - Step 1

After the Terminal is configured, issue the first command on the list by pressing the button Send on the right side. This command accesses the I²C device address 0x20, which is the Slave address selected for this example, and write 0x00 to the register offset byte, initializing the register pointer for the memory map into 0x00. This is the first step required before issuing the Read Regs command:

mcp2221-terminal-first-step-for-read-command.png

The Received/Send Data sub-window on the bottom should show the following result:

mcp2221-terminal-first-step-for-read-command-response-good.png

If you get the following error message, it is likely that your I²C connections between the MCP2221 Breakout Board and XplainedPRO board are incorrect. Go back to the "Connection Diagram" section and review your connection.

mcp2221-terminal-first-step-for-read-command-response-error.png

7

Issue Read Regs Command to the I²C Slave - Step 2

After successfully issuing the command in step 6, run the command Read Regs, which is the next command below. This reads two bytes from the I²C Slave, the result should be the following:

mcp2221-terminal-second-step-for-read-command-response-good.png

The result shows that the first byte equals 0x01, which is the current LED blink pattern configuration. The second byte is 0x00, meaning the switch on the board is not pressed. If you press (and hold) the switch, the second byte should be 0x01 and this is the expected output on the terminal:

mcp2221-terminal-second-step-for-read-command-response-detail.png

Always run the first command (step 6) before reading the two bytes (step 7).

8

Issue LED Pattern Behavior Commands

In order to control the LED0 blink pattern behavior, you can choose between three options:

  • To disable the LED0 blink pattern and have it solid OFF, run the following command:
mcp2221-terminal-led-blink-off-command.png
  • To enable LED0 toggling at a 1 second period, run the following command:
mcp2221-terminal-led-toggle-1s-command.png
  • To enable LED0 toggling at a 100 ms period, run the following command:
mcp2221-terminal-led-toggle-100ms-command.png

Always be sure that each command issued has a success output on the Received/Sent Data window as shown:

mcp2221-terminal-led-toggle-command-success-window.png

 Conclusions

This example project demonstrated how to initialize the SAM D21 SERCOM l2C peripheral in Slave mode, as well as how to handle the interrupts generated by the different sources.

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.