Getting Started with Harmony v3 Drivers on SAM E54 MCUs Using FreeRTOS™

Last modified by Microchip on 2024/06/24 06:31

Warning: This tutorial uses the old Harmony configuration tool (MPLAB® Harmony Configurator (MHC)). New Harmony projects should use the MPLAB Code Configurator (MCC) instead.

 

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 MHC tool, a set of modular devices, and middleware libraries. Additionally, there are numerous example applications, all of which are designed to help the developers 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 E54 MCUs using MPLAB Harmony v3 software framework with FreeRTOS™.

Information

In this tutorial, you will use Arm® Cortex® Microcontroller Software Interface Standard - Real-Time Operating System (CMSIS-RTOS) adoption of FreeRTOS version 10.3.1. FreeRTOS is a market-leading real-time operating system for microcontrollers having ports for Microchip’s microcontrollers. The FreeRTOS kernel is released under the MIT open-source license.

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 RTOS environment
  • Interrupt and thread-safe

Information

Note:

  • Blocking API: A blocking API hangs up execution flow until it has performed its function and returns a result.
  • Non-blocking API: Non-blocking API receives the application request and returns immediately without providing the result. The result of the non-blocking API call is provided separately through an asynchronous event. The application verifies the event to take further action.
  • Interrupt-safe: A sequence of code is said to be “interrupt-safe” when the occurrence of an interrupt(s) doesn't alter the output or functional behavior of the code. To be "interrupt-safe", the sequence of code in consideration must be atomic (indivisible and uninterruptible), and the relevant interrupts must be disabled before entering the sequence.
  • Thread-safe: In an RTOS-based environment, it is possible that a driver and its clients may each run in its own RTOS thread. If the RTOS is preemptive, it is possible that the scheduler may interrupt any of these threads at any time and switch to another. If the other thread happens to also non-atomically access the same shared resource or executes the same critical section of code, that section of code must be guarded and made atomic. The sequence of code with such guards is known as “thread-safe” code.

In this tutorial, you will use Harmony drivers in the Synchronous mode of operation.

The application makes use of the SAM E54 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 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 (LED0) is toggled every time the temperature is displayed on the serial console.

The application you create will utilize:

  • 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.

Back to Top

Ways to Use This Tutorial

There are two ways to use this tutorial:

  1. Create the project from scratch:
    • Use the provided source files and step-by-step instructions below.
  2. Use the solution project as an example:
    • Build the solution project and download it to the SAM E54 Xplained Pro Evaluation Kit Board to observe the expected behavior.

Lab Objectives

  1. Create an MPLAB X IDE Harmony v3 project for a SAM E54 microcontroller from scratch.
  2. Use MHC to configure and generate Harmony Synchronous Driver code for I²C and USART peripherals.
  3. 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 peripherals and configure the corresponding PORT pins.
  4. Use MHC to configure application threads using FreeRTOS.
  5. Use the Harmony v3 Driver, Peripheral Library APIs, and FreeRTOS APIs to implement the application.

Materials

Reference Materials

About

Purchase

Tool

Information

The Xplained Pro series evaluation kits include an on-board Embedded Debugger (EDBG). No external tools are necessary to program or debug the ATSAME54P20A. For programming/debugging, the EDBG connects to the host PC through the USB Micro-B connector on the SAM E54 Xplained Pro Evaluation Kit.

Hardware Connection Setup

Apart from the hardware tools listed above, a USB Type-A male to Micro-B male cable for programming/debugging is required.

Hardware setup

Figure: Hardware Setup

Connection Diagram

The application has the temperature sensor and EEPROM connected to the SAM E54 over the I²C interface and the console (serial terminal) on a PC connected over the USART interface (through USB to USART converter).

Connection diagram

Figure: Connection Diagram

Information

Use the default address of 0x4F and 0x57 to access the temperature sensor (0x4F) and EEPROM (0x57) on the I/O1 Xplained Pro board.

Software Tools

Information

This project has been verified to work with the following versions of software tools:

  • MPLAB X IDE v5.45
  • MPLAB XC32 Compiler v2.50
  • MPLAB Harmony CSP v3.8.3
  • DEV_PACKS v3.8.0
  • MPLAB Harmony 3 Launcher v3.6.2
  • CORE v3.8.1
  • CMSIS_FreeRTOS v10.3.1

Because we regularly update our tools, occasionally you may discover an issue while using the newer versions. If you suspect that to be the case, we recommend that you double-check and use the same versions that the project was tested with.

Back to Top

Steps to Install the Repositories

Go to Tools > Embedded > MPLAB Harmony 3 Content Manager.

Launch content manager​​


Click Browse first to choose any Framework Path and then click Next.

Path content manager​​


Click Download on the required repositories (CSP, DEV_PACKS, MHC, CORE, CMSIS_FreeRTOS).

Download packages​​


Check the Accept License box and then click Close.

Accept license​​


Please complete the same steps to download CSP, DEV_PACKS, and MHC. Once you downloaded all files, you are good to proceed with the MPLAB Harmony project.

For this lab, also you could download the following repositories from GitHub:


Back to Top

Overview

This lab shows you how to create an MPLAB Harmony v3 project from scratch, to configure, and generate:

  • Harmony v3 Peripheral Libraries code for I²C, USART, and corresponding PORT pins 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.

  1. Sensor Thread – To read and display temperature periodically.
  2. EEPROM Thread – To write the temperature values to EEPROM and display it on the COM (serial) port terminal when requested by the user.
  3. 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 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. Developers can use MHC to generate the template files for the three application threads.

Thread state machine

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 that needs to be stored in the EEPROM. Once notified, the sensor threads block again for temperature sampling period duration.

By default, the EEPROM thread is in the waiting state 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 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.

Threads operation

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).

Information

The contents of this ZIP file need to be placed in the following folder:

<Any directory of your choice>/training/
(example Directory = C:/microchip/harmony/v3_5_2)

Note:

  1. The project location of a Harmony v3 project is independent of the location of the Harmony Framework path (i.e., you do not need to create or place a Harmony v3 project in a relative path under the Harmony v3 framework folder). The project can be created or placed in any directory of your choice.
  2. The point above is true because when created, a Harmony v3 project generates all the referred source/header files and libraries (if any) under the project folder.
  3. Both points above contrast with the Harmony v2 project location. In Harmony v2, the project was supposed to be created in a location under the Harmony framework.

Back to Top

ZIP

Extracting the ZIP file creates the following folders:

  • same54_getting_started_freertos contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
    • dev_files contains subfolder sam_e54_xpro containing application source files and other support files (if any) required to perform the lab (see the "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

Information

You must complete all of these steps before you can build, download, and run the application.

Back to Top