Objective
This project shows you how to:
- Use Atmel START to add and configure a USART driver for your project.
- Export your project for the Atmel Studio 7 IDE.
- Use USART example code to write a message to a serial terminal application.
- Example code provided by the Advanced Software Framework 4 (ASF4).
- Program the SAM D21 Xplained Pro Evaluation Kit to verify that your code is working.
Materials
Hardware Tools
Tool | About | Purchase |
---|---|---|
SAM D21 Xplained Pro
Evaluation Kit |
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
Atmel® Studio
Integrated Development Environment |
| | | | |
Atmel® START (ASF4)
Integrated Software Framework |
| Web Based |
Optional Lab Manual
A hardcopy of this tutorial is available here:
Using Atmel START with the SAM D21 MCU >
Procedure:
Step 1: Create a new Atmel START project
Step 2: Add and configure a USART driver
Step 3: Save and export the application for the Studio 7 IDE
Step 4: Import the Atmel START project into the Studio 7 IDE
Step 5: Add USART example code to write a message to a serial terminal application
Step 6: Program the board
Step 7: Verify USART communication
Step 1: Create a new Atmel START project
1
Open a web browser, go to http://start.atmel.com, and select CREATE NEW PROJECT.
The project is created in Atmel START and you now have access to the DASHBOARD view. Scroll to the bottom of this window to verify the ATSAMD21J18A is the selected device and explore its capabilities.
Step 2: Add and configure a USART driver
You will use the Virtual COM Port of the SAM D21 Xplained Pro Embedded Debugger (EDBG) as a USART communication channel.
The Embedded Debugger interface is a composite USB device with three interfaces:
- Programmer/Debugger
- Virtual COM Port
- Data Gateway Interface (DGI) which handles events and data
The Virtual COM Port is connected to a UART on the ATSAMD21J18A and provides an easy way to communicate with the target application through terminal software. It offers variable baud rate, parity, and stop bit settings. Note that the settings on the ATSAMD21J18A must match the settings given in the terminal software.
1
Determine which SAM D21 pins are used for the Embedded Debugger’s virtual COM port. The SAM D21 Xplained Pro Users Guide specifies which SAM D21 pins are used for this port.
- Note Pins PA22 and PA23 are connected to one of the Serial Communications Peripherals (SERCOM3). Next, you will configure this peripheral as a UART.
Step 3: Save and export the project for the Studio 7 IDE
You have now created and configured your Atmel START based project. It’s a good idea to save this configuration so you can make changes to it sometime in the future. Atmel START allows the restoring of any project using its configuration file (*.atstart file format).
Your Atmel START project has been exported for the Studio 7 IDE in a *.atzip file format (standard zip format automatically recognized by Studio 7).
Step 4: Import the Atmel START project into the Studio 7 IDE
Note a default project name and location have been selected for you. Feel free to change these.
Atmel START projects come with some useful examples to help you get started on the different peripherals you’re looking to use. You’ll find these in the driver_examples.c file in the examples folder.
Open the main.c file. You will see the only function called in the current project is the atmel_start_init() function:
The atmel_start_init() function calls the system_init() function.
The system_init() function calls the init_mcu() function (initialize the MCU oscillators, clocks, flash wait states…), and the USART_0_init() function.
Want to see what these functions do?
Right-click on the function name and select 'Goto Implementation' to see the function definition (C file) and description (header file).
For more detail see the ASF4 API Reference Manual.
Open the driver_init.c file to see these and other initialization functions generated in response to the selections you made in Atmel START. You may also want to check out the configuration header files (found in the 'Config' folder).
Step 5: Add USART example code to write a message to a serial terminal application
As mentioned above, the examples in the driver_examples.c file will be used to help you get started.
You will use the io_write() function to send debug messages to the serial terminal. The io function parameter is a structure that describes the peripheral you want to write or read (ASF refers to this as an I/O descriptor). In this case, you've configured the USART_0 driver to use the SERCOM3 peripheral. Executing this function will send 12 bytes (number defined by the third parameter) of the buffer defined by the second parameter ("Hello World!") to the SERCOM3 peripheral.
2
Make the io structure global and rename the USART example function.
- The io structure defined in the USART_0_example() function can be used with other functions (e.g., io_read()), so move this structure declaration from inside the USART_0_example() function to outside of it (make it a global variable).
- Give the USART_0_example() function a more descriptive name … rename it UART_EDBG_init().
Your main.c file should look like this:
3
Refactor the io structure name to uart_edbg_io. Refactoring a variable renames it everywhere it’s found in the project (i.e., across all files of the project).
Right click on io and select:
- Refactor (VA) > Rename…
- Deselect ALL the lines which do not correspond to the main.c file.
- Rename it from io to uart_edbg_io.
- Click Rename.
Warning:
Make sure that only the instances in main.c are renamed or you may modify the usart driver (hal_usart_sync.c) itself or other components.
Step 6: Program the board
3
Compile the project and program the board.
- Compile the project by clicking on the 'Build Solution' icon or by typing ‘F7’, and verify it builds successfully.
- Program the application by clicking on the 'Start Without Debugging' icon.
Note: If the firmware on the evaluation board is out of date, a Firmware Upgrade window will appear asking you if you want to upgrade the firmware. Select Upgrade and allow the process to complete.
Your application is now running on the evaluation board.
Step 7: Verify USART communication
1
Open and configure a serial terminal in the Studio 7 IDE.
- Open the Data Visualizer:
- Tools > Data Visualizer
- Open the Serial Port Control Panel:
- Configuration > External Connection > Serial Port
- Use the drop-down window to select the Virtual COM Port connected to the board (you can find it using Windows® OS Device Manager).
- EDBG Virtual COM Port (COMx)
- Configure the serial port:
- Baud rate: 9600
- Parity: None
- Stop bits: 1
- Click Connect.
Congratulations!
The SAM D21 USART is communicating with your PC as expected.
Table of Contents
|