Low Power Application on SAM L10 Using Harmony v3 Peripheral Libraries: Step 6

Step 6: Add Application Code to the Project

The application is already partially developed and is available in the main_l10.c file under <your unzip folder>/saml10_low_power/dev_files/sam_l10_xpro. The main_l10.c file contains the application logic. It also contains placeholders that you will populate with necessary code in the next step.

  • Go to the saml10_low_power/dev_files/sam_l10_xpro folder and copy the pre-developed main_l10.c file.
  • Replace (over-write) the main_l10.c file of your project available at <Your project folder>/saml10_low_power/firmware/src with the copied file.
  • Open main_l10.c in MPLAB® X IDE and add the application code by following the steps below:


Under the main_l10.c file, in the main() function, notice the call to the SYS_Initialize function. The generated SYS_Initialize function initializes all the peripheral modules used in the application, configured through MPLAB Harmony Configurator (MHC).

Tip: Press the CTRL key and left click on the SYS_Initialize function. The click will open the implementation for the SYS_Initialize function as shown in the following image.


Note: The NVMCTRL_Initialize, PM_Initialize, and EVSYS_Initialize are system-specific initialization functions necessary to run the device. MHC adds these modules by default to the project graph and generates code. These modules will be initialized to user configurations if the user configures them explicitly.


In the int main (void) function, below the SYS_Initialize() function call, add the following code to register callback event handlers, enable the Analog-to-Digital Converter (ADC), start the Real-Time Clock (RTC) timer, display the message, and enter Standby mode.

EIC_CallbackRegister (EIC_PIN_5, EIC_User_Handler, 0);

SERCOM1_I2C_CallbackRegister (i2cEventHandler, 0);

DMAC_ChannelCallbackRegister (DMAC_CHANNEL_0, dmaChannel0Handler, 0);

ADC_CallbackRegister (adcEventHandler, 0);

ADC_Enable ();

RTC_Timer32Start ();

printf ("\n\n\r---------------------------------------------------------");
printf ("\n\r           Low-power Application on SAM L10 Xpro           ");
printf ("\n\r---------------------------------------------------------\n\n\r");

printf ("Enter Standby sleep mode\n\n\r");
PM_StandbyModeEnter ();



The EIC_CallbackRegister function call registers an External Interrupt Controller (EIC) callback interrupt handler with the EIC Peripheral Library (PLIB). The interrupt handler is called by EIC PLIB when switch button SW0 is pressed to enter into Idle mode.


The SERCOM1_I2C_CallbackRegister function call registers a callback interrupt handler with the I²C PLIB. The interrupt handler is called by the I²C PLIB when the I²C reads the temperature value from the temperature sensor.


The DMAC_ChannelCallbackRegister function call registers a callback interrupt handler with the Direct Memory Access (DMA) PLIB. The callback interrupt handler is called by the DMA PLIB when the DMA transfer (of temperature sensor data to the serial terminal) is complete.


The ADC_CallbackRegister function call registers an ADC callback interrupt handler with the ADC PLIB. The interrupt handler is called by the ADC PLIB when the ADC Window Monitor event occurs. The ADC Window Monitor event occurs when the ADC result exceeds the lower threshold value of 3000, set when configuring the ADC PLIB in MHC.


The ADC_Enable function call enables the ADC module. The ADC is used to sample the light sensor when a trigger is received from the Event System Channel 0, every 500 milliseconds.


The RTC_Timer32Start function call starts the RTC timer. The event output from RTC is fed as input to the Event System and is used to trigger the ADC conversion.


The PM_StandbyModeEnter function call moves the device to Standby mode. After a call to the PM_IdleModeEnter Application Programming Interface (API), the CPU will enter Idle Sleep mode. The ADC Window Monitor interrupt will bring the CPU out of the Sleep mode.


Implement the registered callback interrupt handlers for DMA, I²C, ADC, and EIC PLIBs by adding the following code before the int main (void) function in main_l10.c.

/* Handler for DMA channel transfer interrupt */
static void dmaChannel0Handler (DMAC_TRANSFER_EVENT event, uintptr_t contextHandle)
        isDMATransferComplete = true;

/* Handler for I²C interrupt */
static void i2cEventHandler (uintptr_t contextHandle)
    if(SERCOM1_I2C_ErrorGet() == SERCOM_I2C_ERROR_NONE)
        isTemperatureRead = true;

        printf ("There were an error during I2C Transmit. Please ensure that the I/O1 Xplained Pro is connected to the board.\n\n\r");

/* Handler for ADC Window Monitor Interrupt */
static void adcEventHandler (ADC_STATUS status, uintptr_t context)
    isADCWinMonitorEvent = true;

/* Handler for button switch interrupt using EIC peripheral */
static void EIC_User_Handler (uintptr_t context)
    sleepMode = true;


Inside the while loop, add the following code to unset the isADCWinMonitorEvent flag, turn on the LED0, print the message, and submit an I²C transfer to read the temperature sensor value. When the submitted request is completed, the i2cEventHandler callback function declared in step 2 is called.

if (isADCWinMonitorEvent == true)
    isADCWinMonitorEvent = false;
    LED0_Clear ();
    printf ("ADC_WINMON Interrupt Detected. Wake-up from sleep mode.\n\r");
    SERCOM1_I2C_WriteRead (TEMP_SENSOR_SLAVE_ADDR, &i2cWrData, 1, i2cRdData, 2);


Inside the while loop, add the following code to unset the isTemperatureRead flag, store the temperature read into the temperatureVal variable, fill uartTxBuffer with the converted temperature value in degrees Fahrenheit and transmit this value to the terminal through the SERCOM0 USART peripheral by using the DMA.

if (isTemperatureRead == true)
    isTemperatureRead = false;
    temperatureVal = getTemperature (i2cRdData);
    sprintf ((char*)uartTxBuffer, "Temperature = %02d F \r\n", temperatureVal);
    while (!SERCOM0_USART_TransmitterIsReady());
    DMAC_ChannelTransfer (DMAC_CHANNEL_0, uartTxBuffer, (const void*)&(SERCOM0_REGS->USART_INT.SERCOM_DATA), sizeof(uartTxBuffer));


Inside the while loop, add the following code to unset the isDMATransferComplete flag, print the message, turn off the LED0, and put the device in Standby mode by using the PM_StandbyModeEnter () function.

if (isDMATransferComplete == true)
    isDMATransferComplete = false;
    printf("DMA transfer completed. Enter Standby sleep mode.\n\n\r");
    LED0_Set ();
    PM_StandbyModeEnter ();


Inside the while loop, add the following code to unset the sleepMode flag, toggle pin PA06, print the message, turn off the LED0, and put the device in Idle mode by using the PM_IdleModeEnter () function.

if (sleepMode == true)
    sleepMode = false;
    printf ("SW0 button pressed. Enter Idle sleep mode.\n\n\r");
    LED0_Set ();
    PM_IdleModeEnter ();


In the plib_eic.c file, add #include "definitions.h" at the top and scroll down to the EIC_OTHER_InterruptHandler function and add the following code at the top of it. This GPIO is toggled in the Interrupt Service Routine (ISR) of the SW0 press interrupt to measure wake-up time. The time between the switch press and the GPIO toggle in the ISR is the wake-up time.


You are now ready to build the code and observe the results!

DMA, I²C, and USART transfers can also be configured to run in Sleep mode using the Event System and Run in Standby options available in the peripheral.

Next Step >

© 2020 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.