Getting Started with Harmony v3 Peripheral Libraries on SAM D21 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 Peripheral Libraries (PLIBs), drivers, system services, middleware, 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 MHC to create an application that gets you started in developing applications on SAM D21 MCUs using the MPLAB Harmony v3 software framework.
The application makes use of the SAM D21 Xplained Pro Evaluation Kit and I/O1 Xplained Pro Extension Kit (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 one second, two seconds, four seconds, and back to 500 milliseconds every time you press the switch SW0 on the SAM D21 Xplained Pro Evaluation Kit. Also, an LED0 is toggled every time the temperature is displayed on the serial console.
The application you create will utilize:
- SERCOM (as I²C) PLIB to read the temperature from a temperature sensor
- Real-Time Clock (RTC) PLIB to periodically sample temperature sensor data
- SERCOM (as Universal Synchronous Asynchronous Receiver Transmitter (USART)) and Direct Memory Access (DMA) PLIBs to print the temperature values on a COM (serial) port terminal application running on a PC
- PORT PLIB to toggle the LED
- External Interrupt Controller (EIC) PLIB to change the periodicity of temperature sensor data read using SWITCH press event
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 program it to the SAM D21 Xplained Pro Evaluation Kit Board to observe the expected behavior
Lab Objectives
- Create an MPLAB X Integrated Development Environment (IDE) Harmony v3 project for a SAM D21 microcontroller from scratch.
- Use MHC to configure and generate Harmony v3 PLIBs code for RTC, I²C, USART, DMA, EIC, and PORT peripherals.
- Use the Harmony v3 PLIB Application Programming Interfaces (APIs) to implement the application.
Materials
Reference Materials
About
Purchase
- Part Number: ATSAMD21-XPRO - SAM D21 Xplained Pro evaluation kit
- Part Number: ATIO1-XPRO - I/O1 Xplained Pro extension kit
Tool
The Xplained Pro series evaluation kits include an on-board Embedded Debugger (EDBG). No external tools are necessary to program or debug the ATSAMD21J18A. For programming and debugging, the EDBG connects to the host PC through the USB Micro-B connector on the SAM D21 Xplained Pro Evaluation Kit.
Hardware Connection Setup
Apart from the hardware tools listed above, the following items are required:
- USB Type-A male to Micro-B male cable for programming and debugging.
Software Tools
This project has been verified to work with the following versions of software tools:
- MPLAB X IDE v5.25
- MPLAB XC32 Compiler v2.20
- MPLAB Harmony CSP v3.5.2
- MPLAB Harmony DEV_PACKS v3.5.0
- MHC v3.3.3
- MPLAB Harmony Configurator Plugin v3.3.1
Because we regularly update our tools, occasionally you may discover some issues while using the newer versions. If you suspect that to be the case, we recommend that you use the same versions that the project was tested with.
For this lab, download the following repositories from GitHub:
- CSP: The following table shows the summary of contents
Folder | Description |
---|---|
apps | Example applications for CSP library components |
arch | Initialization and starter code templates and data |
docs | CSP library help documentation |
peripheral | PLIB templates and configuration data |
- DEV_PACKS: The following table shows the summary of contents
Folder | Description |
---|---|
Microchip | Peripheral register specific definitions |
arm | Core Specific Register Definitions (CMSIS) |
- MHC: The following table shows the summary of contents
File/Folder | Description |
---|---|
doc | Help documentation and licenses for libraries used |
np_templates | New Project templates for supported toolchains |
*.jar | Java implementations of MHC modules |
mhc.jar | Main Java executable (run: java -jar mhc.jar -h) |
runmhc.bat | Windows cmd batch file to run standalone MHC Graphical User Interface (GUI) |
- Use the MPLAB Harmony 3 Framework Downloader to download the repositories
Overview
This lab shows you how to create an MPLAB Harmony v3 project from scratch, configure, and generate Harmony v3 PLIBs code for RTC, I²C, USART, DMA, and PORT peripherals. It periodically demonstrates the reading of temperature sensor values from the temperature sensor available on the I/O1 Xplained Pro Extension kit 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.
The application initializes clock, PORT, and other peripherals (configured through MHC) by calling the function SYS_Initialize.
The application registers callback event handlers for SERCOM (as I²C), DMA, RTC, and EIC PLIBs. The callback event handlers are called back by the PLIBs when the transaction completion events occur.
The application checks whether the configured RTC timer period has expired. On every timer period expiration, the application calls the function SERCOM2_I2C_WriteRead to submit a temperature sensor read request to the I²C PLIB. The I²C 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 I²C callback event handler.
The application checks the temperature read complete flag to submit a written request to the 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 one second. On subsequent switch press, the application changes the temperature sampling rate to two seconds, four seconds, and back to 500 milliseconds. The application cycles the temperature sampling rate on every switch press, as shown in the figure below.
The application also toggles user LED0 every time the latest temperature value is displayed on the serial console.
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:
- The samd21_getting_started folder contains the lab solution (in the firmware folder) and source files (in the dev_files folder)
- The dev_files folder contains subfolder sam_d21_xpro containing application source files and other support files (if any) required to perform the lab (see "Procedure" section below)
- The firmware folder contains the completed lab solution project. It can be directly built and programmed on the hardware to observe expected behavior
Procedure
Lab Index
Step 1: Create project and configure the SAM D21
- Install the MPLAB Harmony Configurator (MHC) Plugin in MPLAB X IDE
- Create MPLAB Harmony v3 Project Using MPLAB X IDE
- Verify Clock Settings
Step 2: Configure I²C, USART and RTC Peripheral Libraries
- Configure RTC Peripheral Library
- Configure I²C Peripheral Library and I²C Pins
- Configure USART Peripheral Library and USART Pins
- Configure DMA Peripheral Library
Step 3: Configure Pins for Switch and LED
- Configure Switch Button Pin with EIC to Generate an Interrupt
- Configure LED Pin
- 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