Getting Started with MPLAB® Harmony v3 Peripheral Libraries on SAMC2x MCUs
Contents
Objective
MPLAB® Harmony v3 is a flexible, fully integrated embedded software development framework for 32-bit microcontrollers (MCUs) and microprocessors (MPUs).
MPLAB Harmony v3 includes the MPLAB Harmony Configurator (MHC) tool, a set of modular device and middleware libraries; and numerous example applications, all of which are designed to help you quickly and easily develop powerful and efficient embedded software for Microchip’s 32-bit PIC® and SAM devices.
This tutorial shows you how to use the MPLAB Harmony Configurator (MHC) to create an application that gets you started in developing applications on SAM C21x MCUs using MPLAB Harmony v3 software framework.
The application makes use of the SAMC21N Xplained Pro Evaluation Kit (ATSAMC21N-XPRO) and I/O1 Xplained Pro Extension Kit (ATIO1-XPRO) (sold separately).
The application reads the current room temperature from the temperature sensor on the I/O1 Xplained Pro Extension. The temperature read is displayed on a serial console periodically every 500 milliseconds. The periodicity of the temperature values displayed on the serial console is changed to 1 second, 2 seconds, 4 seconds, and back to 500 milliseconds every time you press the switch SW0 on the SAMC21N Xplained Pro Evaluation Kit. Also, an LED LED0 is toggled every time temperature is displayed on the serial console.
The application you create will utilize:
- SERCOM (as I2C) Peripheral Library (PLIB) to read the temperature from a temperature sensor.
- Real-Time Clock (RTC) PLIB to periodically sample temperature sensor data.
- SERCOM (as USART), Direct Memory Access (DMA) PLIB to print the temperature values on a COM (serial) port terminal application running on a PC.
- PORTS PLIB to toggle the LED.
In the process, the lab will also demonstrate the use of callback functions.
Two Ways to Use This Tutorial
- Create the project from scratch:
- Use the provided source files and step-by-step instructions below.
- Use the solution project as an example:
- Build the solution project and download it to the SAMC21N Xplained Pro Evaluation Kit Board to observe the expected behavior.
Lab Objectives
- Create an MPLAB X IDE Harmony v3 project for a SAMC21N microcontroller from scratch.
- Use the MHC to configure and generate Harmony v3 Peripheral Libraries code for RTC, I2C, USART, DMA, and PORTS peripherals.
- Use the Harmony v3 PLIB Application Programming Interfaces (APIs) to implement the application.
Materials
Hardware Tools
The Xplained Pro series evaluation kits include an on-board Embedded Debugger (EDBG). No external tools are necessary to program or debug the ATSAMC21N18A. For programming/debugging, the EDBG connects to the host PC through the USB micro-B connector on the SAMC21N Xplained Pro Evaluation Kit.
Software Tools
For this lab, download the following repositories from GitHub:
Overview
This lab shows you how to create an MPLAB Harmony v3 project from scratch, and configure, and generate Harmony v3 peripheral library code for RTC, I2C, USART, DMA, and PORT peripherals. It demonstrates the reading of temperature sensor values from the temperature sensor available on I/O1 Xplained Pro Extension periodically and displays it on a serial console. The periodicity of temperature sampling is changed for every switch press event. Every time the temperature is displayed on the serial console, an LED is toggled.
Figure 1.1 Application Flow Sequence Figure 1.1 shows the flow sequence of the application task. |
The application initializes the clock, PORTs, and other peripherals (configured through the MHC) by calling the function SYS_Initialize.
The application registers callback event handlers for SERCOM (as I2C), DMA, RTC, and External Interrupt Controller (EIC) PLIB. The callback event handlers are called back by the PLIBs when the transaction completion events occur.
Note:
- A callback event handler for SERCOM (as USART) is not registered as the actual USART data transfer is accomplished by the DMA. The DMA calls back the callback event handler when the DMA transfer request is completed.
- RTC peripheral is used for implementing the time period instead of the timer peripheral. This is done to demonstrate how to configure and use RTC peripherals in an application (particularly low power applications).
The application checks whether the configured RTC timer period has expired. On every timer period expiration, the application calls the function SERCOM5_I2C_WriteRead to submit a temperature sensor read request to the I2C PLIB. The I2C PLIB calls back the registered callback event handler when the latest temperature value is read from the sensor. The application sets a flag in the RTC callback event handler.
The application checks the temperature read complete flag to submit a write request to DMA to print the latest temperature value (in a formatted message) onto the serial console over the USART interface.
The application also monitors the pressing of the switch SW0; if a switch press is detected, the application changes the temperature sampling rate from the default 500 milliseconds to 1 second. On subsequent switch presses, the application changes the temperature sampling rate to 2 seconds, 4 seconds, and back to 500 milliseconds. The application cycles the temperature sampling rate on every switch press, as shown in the following figure.
Lab Source Files and Solutions
This ZIP file contains the completed solution project for this lab. It also contains the source files needed to perform the lab as per the following step-by-step instructions (see the "Procedure" section on this page).
ZIP
Extracting the .zip file creates the following folders:
- samc21n_getting_started contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
- dev_files contains subfolder sam_c21n_xpro containing application source files and other support files (if any) required to perform the lab (see "Procedure" section below).
- firmware contains the completed lab solution project. It can be directly built and downloaded on the hardware to observe expected behavior.
Procedure
Lab Index
Step 1: Create project and configure the SAMC21x
- Step 1.1 - Install the MPLAB® Harmony Configurator (MHC) Plug-in in MPLAB® X IDE
- Step 1.2 - Create MPLAB Harmony v3 Project using MPLAB X IDE
- Step 1.3 - Verify Clock Settings
Step 2: Configure I2C, USART and RTC Peripheral Libraries
- Step 2.1 - Configure RTC Peripheral Library (PLIB)
- Step 2.2 - Configure I2C Peripheral Library and I2C pins
- Step 2.3 - Configure USART Peripheral Library and USART pins
- Step 2.4 - Configure DMA Peripheral Library
Step 3: Configure Pins for Switch and LED
- Step 3.1 - Configure switch button pin with EIC
- Step 3.2 - Configure LED Pin
- Step 3.3 - Rename the default main file
Step 4: Generate Code
Step 5: Add application code to the project
Step 6: Build, program and observe the outputs