SAM D21 SERCOM I²C Slave Example Project
Contents
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.
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.
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
SAM D21 Xplained Pro
Evaluation Kit
ADM00559
MCP2221 Breakout Module
Software Tools
Atmel Studio
Integrated Development Environment
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®:
Download and extract the MCP2221 Windows Driver Package
Run McphCdcDriverInstallation.exe to install the USB drivers for the board. It is located in the \MCP2221 Windows Driver 2014-10-09\Driver Installation Tool\x86 sub-folder.
Plug in the MCP2221 Breakout Board. It should enumerate as a composite class device (HID + CDC) as shown:
Download and Extract the MCP2221 I2C Terminal Installer Package
Run the MCP2221 I²C Terminal Installer application MCP2221Terminal-v2.0.0-windows-installer.exe. The terminal application will be installed in C:\Program Files (x86)\Microchip
Exercise Files
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:
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.
The MCP2221 board signals may be connected to the SAM D21 Xplained Pro board header pins as shown here using male-female jumper cables:
Procedure
If the SAMD21 Xplained PRO board has been successfully enumerated, you should see the board image come up in Atmel Studio as shown:
Open the Solution
Select File > Open > Project/Solution…
Navigate to the solution folder and select the i2c_slave_demo.atsln solution file:
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:
Next, under the project's 'Tool' settings, select your EDBG target from the pull-down. Select 'SWD' as the interface:
Save the tool setting by clicking on the Save All button:
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.
If prompted, upgrade the EDBG firmware on the board:
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:
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:
If there is no device ID on that field, please refer to the MCP2221 terminal installation instructions to install the board.
Import Commands
Now, select File > Import Commands. Navigate to the firmware solution folder and select the file MCP2221 - CMDs For I2C Slave Demo.csv:
You should now have the following commands listed under the Command list:
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:
The Received/Send Data sub-window on the bottom should show the following result:
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.
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:
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:
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:
- To enable LED0 toggling at a 1 second period, run the following command:
- To enable LED0 toggling at a 100 ms period, run the following command:
Always be sure that each command issued has a success output on the Received/Sent Data window as shown:
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.