Getting Started with MPLAB® Harmony v3 Peripheral Libraries on PIC32MK GP MCUs

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

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 and 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, 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 the MHC to create an application that will help you get started in developing applications on PIC32MK GP MCUs using MPLAB Harmony v3 Software Framework.

The application makes use of a PIC32MK General Purpose (GP) Development board and a MikroElectronika Weather click board (sold separately).

The application reads the current room temperature from the temperature sensor on the MikroElectronika Weather click board. The temperature reading 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 S1 on the PIC32MK GP Development Board. Also, an LED (LED1) is toggled every time the temperature is displayed on the serial console.

The application you create will utilize:

  • The Serial Peripheral Interface (SPI) Peripheral Library (PLIB) reads the temperature from a temperature sensor.
  • The TMR2 PLIB periodically samples temperature sensor data.
  • The CORE TIMER PLIB uses a blocking timer delay for initializing the temperature sensor.
  • The Universal Asynchronous Receiver Transmitter (UART) and Direct Memory Access (DMA) PLIBs to print the temperature values on a COM (serial) port terminal application running on a PC.
  • The General Purpose Input/Output (GPIO) PLIB changes the periodicity of temperature sensor data readings using the SWITCH press event and toggles the LED.

In the process, the lab will also demonstrate the use of callback functions.

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 program it to the PIC32MK GP Development Board to observe the expected behavior.

Back to Top

Lab Objectives

  1. Create an MPLAB X IDE Harmony v3 project for a PIC32MK GP microcontroller from scratch.

  2. Use the MHC to configure and generate Harmony v3 PLIBs code for TMR2, SPI, UART, CORE TIMER, DMA, and GPIO peripherals.
  3. Use the Harmony v3 PLIB Application Programming Interfaces (APIs) to implement the application.

Back to Top

Materials

Hardware Tools

​The PIC32MK GP Development Board includes a PICkit On Board (PKOB). No external tools are necessary to program or debug the PIC32MK1024GPE100. For programming and debugging, the PKOB connects to the host PC through the USB micro-B connector on the PIC32MK GP Development Board.

Hardware Setup

Figure 1: Hardware Setup

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.
  • USB Type-A male to micro-B male cable to connect USB-UART serial port.

Hardware Modification

MikroElectronika Weather click board supports both I²C and SPI protocols to communicate with the BME280 temperature sensor. It provides jumpers (resistors) to choose a communication interface between I²C and SPI. By default, I²C is selected as the communication interface. The PIC32MK1024GPE100 device does not have an I²C peripheral module; therefore in this lab, SPI is chosen as the communication interface to communicate with the temperature sensor.

The hardware modification to be done on the MikroElectronika Weather click board is shown in the figure below.

Hardware Modification

Figure 2: Hardware Modification

Software Tools

​This project has been verified to work with the following versions of software tools:MPLAB X IDE v5.30, MPLAB XC32 Compiler v2.30, MPLAB Harmony CSP v3.5.2, MPLAB Harmony DEV_PACKS v3.5.0, MHC v3.3.3, and MHC Plugin v3.4.0

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

For this lab, download the following repositories from GitHub:

  • CSP: The following table shows the summary of contents.
FolderDescription
appsExample applications for CSP library components
archInitialization and starter code templates and data
docsCSP library help documentation
peripheralPLIB templates and configuration data
  • DEV_PACKS: The following table shows the summary of contents.
FolderDescription
MicrochipPeripheral register specific definitions
armCore specific register definitons (CMSIS)
  • MHC: The following table shows the summary of contents.
File/FolderDescription
docHelp documentation and licenses for libraries used
np_templatesNew Project templates for supported toolchains
*JARJava implementations of MHC modules
mhc.jarMain Java executable (run: java -jar mhc.jar -h)
runmhc.batWindows cmd batch file to run standalone MHC GUI

Back to Top

Overview

This lab shows you how to create an MPLAB Harmony v3 project from scratch, configure, and generate Harmony v3 PLIB code for TMR2, SPI, UART, CORE TIMER, DMA, and GPIO peripherals. It demonstrates the reading of temperature sensor values from the temperature sensor available on the MikroElectronika Weather click board periodically and displays it on a serial console. The periodicity of temperature sampling is changed on every switch press event. Every time the temperature is displayed on the serial console, an LED is toggled.

Application Flow Sequence

The application initializes clock, PORTs and other peripherals (configured through the MHC) by calling the function SYS_Initialize.

The application registers callback event handlers for SERCOM (as I²C), DMA, RTC, and External Interrupt Controller (EIC) Peripheral Libraries. The callback event handlers are called back by the peripheral libraries when the transaction completion events occur.

Note:

  1. A callback event handler for SERCOM (as USART) is not registered as the actual USART data transfer, and is accomplished by the DMA. The DMA calls back the callback event handler when the DMA transfer request is completed.
  2. 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 peripheral in an application (particularly low power application).

The application checks whether the configured RTC timer period has expired. On every timer period expiration, the application calls the function SERCOM3_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 RTC callback event handler.

The flow sequence of the application task is displayed

Figure 3: shows the flow sequence of the application task

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 UART interface.

The application also monitors the pressing of the switch S1. 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 cycles the temperature sampling rate on every switch press

Figure 4: Pressing of the switch S1

The application also toggles user LED LED1 every time the latest temperature value is displayed on the serial console.

Back to Top

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

The contents of this ZIP file need to be placed in a folder: <Any directory of your choice>/
(example Directory = C:/microchip/harmony/v3)

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 and header files and libraries (if any) under the project folder.
  3. Both points above contrast with Harmony v2 project location. In Harmony v2, the project was supposed to be created in a location under the Harmony framework.

‍ZIP

Extracting the ZIP file creates the following folders:

  • pic32mk_getting_started contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
    • dev_files contains the subfolder pic32mk_gp_db which contains the 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 programmed on the hardware to observe expected behavior.

Back to Top

Procedure

​All the steps mentioned below must be completed before you build, download, and run the application.

Lab Index

Step 1: Create project and configure the PIC32MKGP

  • 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 SPI, UART, CORE TIMER, and TMR2 Peripheral Libraries

  • Step 2.1 - Configure TMR2 Peripheral Library
  • Step 2.1 - Configure CORE TIMER Peripheral Library
  • Step 2.2 - Configure SPI Peripheral Library and SPI Pins
  • Step 2.3 - Configure UART Peripheral Library and UART Pins
  • Step 2.4 - Configure DMA Peripheral Library

Step 3: Configure Pins for Switch and LED

  • Step 3.1 - Configure Switch Button Pin with GPIO to Generate an Interrupt
  • 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

Back to Top