Atmel SAM C21
SimplePublish for SAM C21 Xplained Pro
The QSL includes a SimplePublish example for the Atmel SAM C21 Xplained Pro evaluation kit. This is given as an Atmel Studio 7 project based on the Atmel Software Framework (ASF).
The main.c code itself is similar to the one for the Raspberry Pi, with the addition of a watchdog timer and initialization of a second serial port to enable debug prints (the watchdog is for demonstration purposes only, and can be removed as the user see fit). A yellow LED on the Xplained Pro board is also toggled to indicate whether the mote has connected to a network or not.
The example was developed on Atmel Studio Version 7.0.1006 with ASF version 3.32.0.
Download Atmel Studio 7
Download and run the Atmel Studio web installer from the official download page, and follow the instructions. This will also install the necessary drivers for the embedded debugger on the Xplained Pro board.
Windows is the only supported OS for the latest Atmel Studio 7.
ASF is included in Atmel Studio 7 and does not require a specific download, but you can use the Extension Manager (Tools->Extension Manager) to update it.
Prepare the SAM C21 Xplained Pro
Set Supply Voltage to 3.3 V
Make sure that the jumper on the 3-pin header labeled VCC_SEL is set to 3.3 V, as seen in Figure 5. This will select 3.3 V as the supply voltage for the ATSAMC21J18A, peripherals and extension headers, which is important as we will power the mote from the latter.
The supply voltage is by default set to 5.0 V, which will fry your mote instantly!
Figure 5: Xplained Pro VCC selection header.
Connect to Mote
Figure 6: Mote connected to the SAM C21 Xplained Pro.
The Xplained Pro has three extension headers (EXT1, EXT2, EXT3) with pins as listed in Table 7: Connect the mote to one of the headers, as per the mapping in Table 8. In Figure 6 you can see the mote connected to EXT1, but it does not matter which one you use: The pins for UART RX/TX on all three headers are connected to the same pins on the SAM C21.
Long cables between the mote and Xplained Pro may corrupt the API bus communication.
Name | Pin # | Name | |
---|---|---|---|
ID | 1 | 2 | GND |
ADC+ | 3 | 4 | ADC- |
GPIO0 | 5 | 6 | GPIO1 |
PWM+ | 7 | 8 | PWM- |
IRQ | 9 | 10 | SPI_SS_B |
TWI_SDA | 11 | 12 | TWI_SCL |
UART_RX | 13 | 14 | UART_TX |
SPI_SS_A | 15 | 16 | SPI_MOSI |
SPI_MISO | 17 | 18 | SPI_SCK |
GND | 19 | 20 | VCC |
Table 7: Xplained Pro Standard Extention Header.
Xplained Pro | Mote |
---|---|
VCC | VBAT |
GND | GND |
UART_TX | RX |
UART_RX | TX |
GND | RX RTSn |
GND | TX CTSn |
Table 8: Xplained Pro to mote pin mapping.
Connect Debug USB to Computer
Connect a USB cable from your computer to the USB-micro port labeled DEBUG USB: This is the port on the top of the Xplained Pro (visible in both Figure 5 and Figure 6), and is used to program, debug and power the board.
The debug USB also spawns a virtual serial port named EDBG Virtual COM Port. Make sure to note the name, e.g. COM4 in the example pictured below, as we will open a terminal on said port to see debug prints.
Open Debug Terminal
The SimplePublish example code will send all standard input/output (e.g. debug prints) through the serial port you identified in the previous section. Open your favourite serial terminal (e.g. Putty) and connect to said serial port with the following configuration:
- Baudrate: 115200
- Data Bits: 8
- Parity: None
- Stop Bits: 1
You probably have to restart the terminal whenever you disconnect/reconnect the debug USB to the Xplained Pro.
You can (de)activate debug and log messages with the NDEBUG and NLOG defines in dn_debug.h (located in sm_qsl/).
Compile and Run
Navigate to the SimplePublish example directory at examples/samc21_xplained_pro/SimplePublish/ and open the SimplePublish.atsln solution file in Atmel Studio 7. This should open the project with everything set up for you. Since the Xplained Pro has an embedded debugger, you do not have to select any tool: Atmel Studio should detect that the board is connected via USB.
The Atmel Studio project expects to find the sm_clib and sm_qsl as they appear in the Directory Structure. If you chose the zip download or subversion checkout, the sm_clib/ directory from the separate unzipped folder/svn checkout need to be copied into the QSL repository to achieve the same structure.
Now, simply press the symbol (Ctrl+Alt+F5) to compile and run the program, and you should see prints appearing on the debug terminal you opened.
Port to Similar Platforms
Although the provided code is written specifically for the ATSAMC21J18A on the SAM C21 Xplained Pro evaluation kit, it should be pretty straightforward to port it to similar architectures that support the same ASF modules. Correctly configuring the available serial ports (SERCOM USART) in serial.c/.h should be the only thing that might require some adaptation. We therefore give a quick guide on how to set up a new ASF project.
Create New Project
- File > New > Project... (Ctrl+Shift+N)
- Under C/C++, select GCC C ASF Board Project.
- Select project name and location of your choice
- Solution: Create new solution.
- (Optional) Check the Create directory for solution box if you want to place the project folder within a solution directory that you also have to provide a name for.
- Press Ok
- Select the correct device and board, and press Ok.
Add ASF Modules
- Open the ASF Wizard: ASF > ASF Wizard (Ctrl+W)
- Select the correct project in the drop down menu (if not already selected).
You should now see two windows with Available vs Selected Modules. Select and Add >> all the modules listed in Table 9.
Make sure to select the correct mode when adding certain modules (you can change the mode of already selected modules too).
Press Apply.
Name | Type | Mode | Comment |
---|---|---|---|
Generic board support | Driver | Selected by default | |
Delay routines | Service | cycle | Used to implement dn_sleep_ms |
PORT - GPIO Pin Control | Driver | Selected by default | |
RTC - Real Time Counter Driver | Driver | count_polled | Used to implement dn_time_ms |
SYSTEM - Core System Driver | Driver | Selected by default | |
WDT - Watchdog Timer | Driver | callback | Used to implement watchdog functionality (optional) |
Table 9: ASF modules used in the SimplePublish example code.
Add Platform Specific Files (as Copy)
- In the Soution Explorer window (right side), right click the folder src > Add > Existing Item...
- Navigate to the SimplePublish example source directory: examples/samc21_xplained_pro/SimplePublish/src/
- Select all the .c files and press Add (you do not need to add asf.h).
- Press Ok to overwrite the default main.c file.
Add QSL and C Library Files (as Link)
- In the Soution Explorer window (right side), right click the folder src > Add > New Folder
- Name it sm_clib
- Right click the folder you just created > Add > Existing Item...
- Navigate to the C Library directory in the repository: sm_clib/sm_clib/
- Select all the .c and .h files, except those for the manager/wireless heart:
dn_ipmg.c/.h
dn_serial_mg.c/.h
dn_whmt.c/.h - Click the down-arrow next to Add and select Add As Link
- Repeat the previous steps with an sm_qsl folder for the QSL (but this time, add everything in sm_qsl/)
Restart Atmel Studio and reopen your solution/project.
The two directories with the linked files should now have been moved to a subdirectory with the same name as your project.If you don't restart, you will later see that building the project fails: Inspecting the autogenerated makefile unveils that Atmel Studio attempts to build object files for the linked files in the non-existing subdirectory. Attempting to correct the makefile is futile, as Atmel Studio overwrites any manual changes. This seems to be a bug that only happens with files that are added "... As Link", but does not seem to have any other effects than the need for a restart and tolerating the unnecessary sub-directory in the Solution Explorer.
Add QSL and C Library Directories to Toolchain
- In the Soution Explorer window (right side), right click your project > Properties (Alt+F7)
- Toolchain > ARM/GNU C Compiler > Directories
- Press the icon and then the (...) button
Again browse to the C Library in the repository: sm_clib/sm_clib/ and press Select Folder
Make sure that you add a relative path to sm_clib/sm_clib/ and not just the top sm_clib/ directory.
Press OK.
Repeat the previous steps for the QSL in the repository: sm_qsl/
Press Ctrl+S to save your changes
Make necessary adaptations
Finally, you need to dive into the serial port configuration in serial.h and serial.c and make any necessary adaptations. Consult the datasheet of your platform for details (look for SERCOM USART).