MPLAB® Harmony v3 Drivers on SAM E70/S70/V70/V71 MCUs Using FreeRTOS™: Getting Started
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 devices, and middleware libraries. Additionally, there are 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 E70/S70/V70/V71 MCUs using MPLAB Harmony v3 software framework with FreeRTOS™.
Harmony v3 drivers support Asynchronous and Synchronous modes of operation.
Asynchronous Mode
- Non-blocking Application Program Interfaces (APIs)
- Works seamlessly in bare-metal and RTOS environments
- Interrupt and thread-safe
Synchronous Mode
- Blocking APIs
- Suitable for use in an RTOS environment
- Interrupt and thread-safe
In this tutorial, you will use Harmony drivers in the Synchronous mode of operation.
The application makes use of the SAM E70 Xplained Ultra 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 Kit. The temperature reading is displayed on a serial console periodically every second. Further, the application writes the temperature readings to EEPROM. When a character is entered on the console, the last five written temperature values are read from the EEPROM and displayed on the console. Also, an LED (LED3) is toggled every time the temperature is displayed on the serial console.
The application you create will utilize:
- Two-Wire Interface High Speed (TWIHS) (I²C) Synchronous Driver to read the temperature from a temperature sensor and store/retrieve to/from EEPROM.
- Universal Synchronous Asynchronous Receiver Transmitter (USART) Synchronous Driver to print the temperature values on a COM (serial) port terminal application running on a PC.
- PORTS Peripheral Library to toggle an LED.
- FreeRTOS library to create application threads and intercommunicate between application threads.
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 SAM E70 Xplained Ultra Evaluation Kit Board to observe the expected behavior.
Lab Objectives
- Create an MPLAB X IDE Harmony v3 project for a SAM E70 microcontroller from scratch.
- Use MHC to configure and generate Harmony Synchronous Driver code for I²C and USART peripherals.
- Along with the configuration of drivers for I²C and USART peripherals, use MHC to configure and generate Harmony v3 peripheral libraries for the I²C, USART, and PORTS peripherals.
- Use MHC to configure application threads using FreeRTOS.
- Use the Harmony v3 Driver, Peripheral Library APIs, and FreeRTOS APIs to implement the application.
Reference Materials
Hardware Tools
Hardware Connection Setup
![hardware setup](/xwiki/bin/download/software-tools/harmony/archive/same70-getting-started-tm-drivers-freertos/WebHome/hardware%20setup.jpg?width=600&height=517&rev=1.1)
Figure: Hardware Setup
Connection Diagram
The application has the temperature sensor and EEPROM connected to the SAM E70 over the I²C interface and the console (serial terminal) on a PC connected over the USART interface (through USB to USART converter).
Hardware Modification
![Hardware Modification](/xwiki/bin/download/software-tools/harmony/archive/same70-getting-started-tm-drivers-freertos/WebHome/hardware_modification.png?width=450&height=303&rev=1.1)
Figure: Hardware Modification
Software Tools
Installers
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) |
- CORE: The following table shows the summary of contents.
Folder | Description |
---|---|
apps | Example applications for core library components |
config | Core module configuration scripts |
docs | Core module library help documentation |
driver | Core module peripheral device drivers |
osal | MPLAB Harmony Operating System Abstraction Layer |
system | MPLAB Harmony system services |
templates | Application and system file templates |
- CMSIS_FreeRTOS: The following table shows the summary of contents.
Folder | Description |
---|---|
CMSIS | CMSIS-FreeRTOS related files |
CMSIS/RTOS2/FreeRTOS/Config | CMSIS-FreeRTOS configuration file |
CMSIS/RTOS2/FreeRTOS/Examples | CMSIS-FreeRTOS example projects |
CMSIS/RTOS2/FreeRTOS/Source | CMSIS-FreeRTOS source code |
Config | FreeRTOS Kernel configuration file |
Demo | FreeRTOS demo projects |
DoxyGen | Source of the documentation |
License | FreeRTOS Kernel license text file |
Source | FreeRTOS Kernel source code |
Utilities | Utility programs |
- Use the MPLAB Harmony 3 Content Manager (MHCM) to download the repositories.
Overview
This lab shows you how to create an MPLAB Harmony v3 project from scratch, configure, and generate:
- Harmony v3 Peripheral Libraries code for TC, I²C, USART, and PORTS peripherals
- Harmony v3 Synchronous Driver code for I²C and USART peripherals
- Harmony v3 application threads using FreeRTOS
It demonstrates the reading of temperature sensor values from the temperature sensor available on the I/O1 Xplained Pro Extension Kit periodically and displays it on a serial console. It further writes the temperature readings to EEPROM. When a character is entered on the console, the last five written temperature values are read from the EEPROM and displayed on the console. Also, an LED is toggled every time the temperature is displayed on the serial console.
The application functionality is divided into three threads.
- Sensor Thread – To read and display temperature periodically.
- EEPROM Thread – To write the temperature values to EEPROM and display it on the COM (serial) port terminal when requested by the user.
- User Input Thread – To read the character entered on the COM (serial) port terminal.
Sensor thread, EEPROM thread, and the User Input thread run the corresponding application function in an infinite loop. These threads are created from the SYS_Tasks routine. Following the creation of these threads, the FreeRTOS scheduler is invoked. The scheduler performs the preemptive scheduling of these threads based on the waiting, ready, and running state of each of them. You will use MHC to generate the template files for the three application threads.
![Diagram showing the three application threads](/xwiki/bin/download/software-tools/harmony/archive/same70-getting-started-tm-drivers-freertos/WebHome/2.png?width=700&height=390&rev=1.1)
Figure: The Application Threads.
Once the scheduler is invoked the threads start to run based on the default scheduling method (preemptive).
By default, the sensor thread is blocked and wakes up every time the temperature sampling period expires. Once active, the sensor thread reads the latest room temperature value from the temperature sensor and prints the same on the serial terminal. It also notifies the EEPROM thread through the RTOS queue of the availability of the latest temperature value which needs to be stored in the EEPROM. Once notified, the sensor threads block again for the temperature sampling period duration.
By default, the EEPROM thread is waiting for an event to occur. It wakes up when there is an event in the RTOS queue to either write the latest temperature value or the read last five temperature values. Based on the event, the EEPROM thread performs writing or reading and goes back to the waiting state.
By default, the user input thread is blocked, waiting to receive a character on the USART receive line. Once a character is received the user input thread becomes active and submits an EEPROM read request to the EEPROM thread through the RTOS queue to read the last five temperature values stored in EEPROM. After serving the user input request, the thread goes back to the blocking state to receive another character on the USART receive line.
![The application thread functioning](/xwiki/bin/download/software-tools/harmony/archive/same70-getting-started-tm-drivers-freertos/WebHome/3.png?width=700&height=332&rev=1.1)
Figure: The Application Thread Functioning
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:
- getting_started_freertos contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
- dev_files contains subfolder sam_e70_xult 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 a Project and Configure the SAM E70
- Step 1.1 - Install the MPLAB Harmony Configurator (MHC) Plugin in MPLAB X IDE
- Step 1.2 - Setup MHC Plugin Window Open in Native mode
- Step 1.4 - Create MPLAB Harmony v3 Project using MPLAB X IDE
- Step 1.4 - Verify Clock Settings
Step 2: Configure I²C and USART Drivers in Synchronous mode
- Step 2.1 - Configure I²C Driver and I²C Pins
- Step 2.2 - Configure USART Driver and USART Pins
- Step 3.1 - Configure LED Pin
- Step 3.2 - Rename the Default Main File
- Step 3.3 - Configure Application Threads
Step 4: Generate Code
Step 5: Add Application Code to the Project
Step 6: Build, Program, and Observe the Outputs