Low Power Application on SAM D21 (Arm® Cortex® M0+) MCUs Using MPLAB® Harmony v3 Peripheral Libraries

MPLAB® Harmony v3 is now configurable through MPLAB Code Configurator (MCC). Although the instructions in this tutorial are for MPLAB Harmony Configurator (MHC), the flow and experience of creating a project, configuring peripherals, and generating code using MCC are similar. Refer to the following links for specific instructions to use MPLAB Harmony v3 with MCC.

 Objective

This tutorial shows you how to create a low power application on a SAM D21 using MPLAB® Harmony v3 software framework.

The SAM D21 is a 32-bit Arm® Cortex® - M0+ based microcontroller that provides features to reduce power consumption through different Sleep modes, such as Idle and Standby.

  • In Idle mode, the CPU is stopped and optionally, some synchronous clock domains are stopped, depending on the IDLE argument. The regulator operates in Normal mode. This mode allows power optimization with a fast wake-up time.
  • In Standby mode, all clock sources are stopped, except those which are kept running if requested by a running module or have the ONDEMAND bit set to zero. The regulator operates in Low Power mode. Before entering Standby mode the user must make sure that a significant amount of clocks and peripherals are disabled so that the voltage regulator is not overloaded. This mode allows the device to consume the lowest power with little overhead on wake-up time.

The Power Manager (PM) module is responsible for controlling the Low Power modes. Additionally, the SAM D21 provides SleepWalking. SleepWalking is the capability for a device to temporarily wake up clocks for the peripheral to perform a task without waking up the CPU in STANDBY sleep mode.

The application makes use of the SAM D21 Xplained Pro Evaluation Kit, Power Debugger and the I/O1 Xplained Pro Extension Kit (sold separately).

The application is developed on the MPLAB Harmony v3 software framework. MPLAB Harmony v3 is a modular framework that provides interoperable firmware libraries for application development on 32-bit MCUs and MPUs. It includes an easy-to-use Graphical User Interface (GUI) (MHC) for selecting, configuring, and generating starter codes, peripheral libraries, and middleware (USB, TCP/IP, Graphics and so on).

On power-up, the application prints a menu on a PC serial terminal. The menu provides options to measure the device power consumption in Sleep modes or to measure wake-up time when the device is wake-up from one of the Sleep modes. Once the option is selected, the application sets up EIC peripheral clock source and goes to Standby Sleep mode to enable measurement modes.

In this training module,

a

The power consumption of the SAM D21 Xplained Pro board is measured using two tools

  • External Power Debugger - A hardware tool
  • Data Visualizer - A software tool

The steps to connect and measure the power consumption using these tools are provided in the following sections.

b

Device wake-up time is measured using the Logic Analyzer. The steps to connect and measure the wake-up time are provided in the following sections.

Power Measurement Mode:
By default, the application is running with the device in Standby Sleep mode. While the device is in Standby Sleep mode, the user can measure the device's power consumption by following the instructions provided in the following sections.

The device wakes up from Standby mode and enters Active mode when you cover the light sensor (by placing a hand over it, for example) on the I/O1 Xplained Pro Extension kit. Here, you can observe the transition of the device's power consumption from Standby Sleep mode to Active mode on the Data Visualizer tool.

The application prints the room temperature on a serial terminal, and the device goes back to Standby Sleep mode. The LED0 on the SAM D21 Xplained Pro Evaluation Kit is toggled every time the serial console displays the temperature.

When you press the switch button SW0, the application switches from Standby Sleep mode to Idle Sleep mode. Here, you can observe the transition of the device's power consumption from Standby Sleep mode to Active mode and from Active mode to Idle Sleep mode. At this point, you can measure the device's power consumption in Idle Sleep mode by following the instructions provided in the following sections.

The device wakes up from Idle mode and enters Active mode when you cover the light sensor (by placing a hand over it, for example) on the I/O1 Xplained Pro extension kit. Here, you can observe the transition of the device's power consumption from Idle Sleep mode to Active mode on the Data Visualizer tool.

The application prints the room temperature on a serial terminal, and the device goes back to Standby Sleep mode. The LED0 on the SAM D21 Xplained Pro Evaluation Kit is toggled every time the serial console displays the temperature.

Wake-up Time Measurement Mode:
By default, the application is running with the device in Standby Sleep mode, and the device wakes up from Standby Sleep mode when you press a switch on the development board.

The application toggles the Wake-up GPIO pin on the device wake-up and then goes to the Idle Sleep mode. You can measure the wake-up time by monitoring the switch SW0 pin and Wake-up GPIO pin on the Logic Analyzer tool.

In the next switch press, you can measure the wake-up time from Idle Sleep mode by monitoring the same switch SW0 pin and Wake-up GPIO pin on the Logic Analyzer tool.

The application switches the device Sleep mode between Standby Sleep mode and Idle Sleep mode whenever you press the SW0 switch. Therefore, you can measure the wake-up time from these Sleep modes by monitoring the switch SW0 on the Logic Analyzer tool.

Also, the device wakes up from Standby mode or Idle mode when you cover the light sensor by placing your hand over it, prints the wake-up reason message, and goes back to Sleep mode.

The application you create will utilize the following peripherals:

  • SERCOM2 (as I²C) peripheral library to read the temperature from the temperature sensor.
  • Real-Time Clock (RTC) peripheral library to periodically generate an event for the AC to sample the light sensor.
  • SERCOM3 (as Universal Synchronous Asynchronous Receiver Transmitter (USART)) peripheral library to print messages on a serial console running on a computer.
  • PORT peripheral library to toggle the LED0.
  • Analog Comparator (AC) peripheral library to sample the light sensor analog input and detect whether the light sensor is covered or not.
  • PM peripheral library to configure Low Power modes.
  • External Interrupt Controller (EIC) peripheral library to control the user button SW0.
  • EVSYS peripheral library to trigger the start of the AC conversion on every RTC compare match event. The EVSYS allows for peripheral-to-peripheral communication without CPU intervention. This reduces the burden on the CPU and other resources when compared to conventional interrupt-based systems.

There are two approaches for 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 D21 Xplained Pro evaluation kit to observe the expected behavior.

Lab Objectives

  1. Create an MPLAB X IDE Harmony v3 project for a SAM D21 MCU from scratch.
  2. Use MHC to configure and generate Harmony v3 Peripheral Library code for the RTC, I²C, USART, AC, PM, EIC, EVSYS, and PORT peripherals.
  3. Use the Harmony v3 Peripheral Library Application Programming Interfaces (APIs) to implement and demonstrate a low-power application.

 Materials

Hardware Tools

Tool About Purchase
board-50px.png
SAM D21 Xplained Pro
Evaluation Kit
ATIO1-XPRO-50px.png
I/O1 Xplained Pro
Extension Kit
board-50px.png
Power Debugger
Daughter board

You will also need a logic analyzer or a Cathode-Ray Oscilloscope (CRO) to measure the device wake-up time.

The Xplained Pro series evaluation kits include an onboard Embedded Debugger (EDBG). No external tools are necessary to program or debug the ATSAMD21J18A. For programming or debugging, the EDBG connects to the host PC through the USB micro-B connector on the SAM D21 Xplained Pro evaluation kit.

You will need an external hardware Power Debugger to measure the current consumption by MCU and peripherals.

Software Tools

This project has been verified to work with the following versions of software tools:
MPLAB X IDE v5.50
MPLAB XC32 Compiler v3.00
MPLAB Harmony CSP v3.9.1
DEV_PACKS v3.9.0
MHC v3.7.2
Data Visualizer

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.

Tool About Installers
Installation
Instructions
Windows Linux Mac OSX
MPLAB® X
Integrated Development Environment
MPLAB® XC32
C/C++ Compiler

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 Peripheral library 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)

Overview:

This lab shows you how to create an MPLAB Harmony v3 project from scratch. You will configure and generate Harmony v3 peripheral library code for the RTC, I²C, USART, AC, PM, EIC, EVSYS, and PORT peripherals. It also demonstrates Low Power mode features of the SAMD21 MCU using light and temperature sensors on the I/O1 Xplained Pro extension kit.

flowchart.png
Figure 1. Application Flow sequence

The application flow is as follows:

  • On power-up, the application prints the measurement menu and will read the user measurement mode selection.
  • If the user selects Power Measurement Mode, the application flow is as mentioned below
    • The application keeps the device Standby Sleep mode. Here, the user can measure the device power consumption in Standby sleep mode on the Data Visualizer tool.
    • The RTC is configured as an event generator to generate a periodic (every 500 ms) event to trigger the AC to start conversion.
    • The AC acknowledges the event from the RTC and starts the conversion of the light sensor analog signal.
    • If the AC comparator output rises above the detect level, then AC produces an interrupt to wake up the CPU.
    • In Active mode, the CPU submits an I²C read transfer request to read the temperature value from the temperature sensor on the I/O1 Xplained Pro evaluation kit.
    • The application prints the latest temperature value (in a formatted message) on a serial console/terminal using USART.
    • The application also monitors the pressing of switch SW0. If a switch press is detected, the application moves the device to Idle Sleep mode. Here the user can measure the device power consumption in Idle sleep mode on the Data Visualizer tool.
  • If the user selects Wake-up Measurement Mode, the application flow is as mentioned below
    • The application runs in Standby Sleep mode.
    • If a switch press is detected, the device will wake up, and the application runs in Idle Sleep Mode. On the next switch press, the application will go into the Standby Sleep Mode. Here, the user can measure the device wake-up time from Standby sleep mode and Idle sleep mode using the Logic Analyzer.
    • On every switch press, the application will switch from Standby Sleep mode to Idle Sleep mode and vice-versa.
    • In Sleep mode (Standby or Idle), if the user covers the light sensor by placing their hand over it, the device wakes up from the Sleep mode, prints the wake-up reason on the serial terminal, and goes back to the previous Sleep mode.
block1.png
Figure 2. CPU in Idle or Standby mode

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 by following the step-by-step instructions (see the "Procedure" section on this page).

The contents of this ZIP file need to be placed in a folder of your choice.
Note:

  • The project location of a Harmony v3 project is independent of the location of the Harmony Framework path (i.e., you need not 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. This 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.
  • The point above contrasts with the Harmony v2 project location. In Harmony v2, the project was supposed to be created in a location under the Harmony framework.


Extracting the ZIP file creates the following folders:

  • samd21_low_power contains the lab solution (in the firmware folder) and source files (in the dev_files folder).
    • dev_files contains subfolder sam_d21_xpro which contains 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

All steps must be completed before you are ready to build, download, and run the application.

Lab Index

Step 1: Create Project and Configure the SAM D21

  • Step 1.1 - Verify Whether MHC Plug-in is Installed in MPLAB X IDE
  • Step 1.2 - Create MPLAB Harmony v3 Project Using MPLAB X IDE
  • Step 1.3 - Configure Clock Settings

Step 2: Configure I²C, USART, and RTC Peripheral Libraries

  • Step 2.1 - Configure RTC Peripheral Library
  • Step 2.2 - Configure I²C Peripheral Library, I²C Pins, and Verify I²C Clock
  • Step 2.3 - Configure USART Peripheral Library, USART Pins, and Verify USART Clock

Step 3: Configure AC, Event System, and EIC Peripheral Libraries

  • Step 3.1 - Configure Analog Comparator (AC) Peripheral Library (PLIB)
  • Step 3.2 - Configure External Interrupt Controller (EIC) PLIB for Switch Button, and Configure EIC Clock
  • Step 3.3 - Configure Event System (EVSYS) PLIB

Step 4: Configure PM and NVMCTRL Peripheral libraries, LED and Wake-up Test Pins

  • Step 4.1 - Configure Power Manager (PM) Peripheral Library
  • Step 4.2 - Configure NVMCTRL Peripheral Library
  • Step 4.3 - Configure LED and Wake-up Test Pins
  • Step 4.4 - Rename the Default Main File

Step 5: Generate Code
Step 6: Add Application Code to the Project
Step 7: Build, Program, and Observe the Outputs


First Step >

© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.