Getting Started Training Module Using MPLAB® Code Configurator (MCC): Step 5

Last modified by Microchip on 2023/11/15 13:00

Add Application Code to the Project

The application is already developed and is available in the main_sam_e51_cnano.c file under the <Your unzip folder>/same51n_getting_started/dev_files/sam_e51_cnano folder. The main_sam_e51_cnano.c file contains the application logic. It also contains placeholders that you will populate with necessary code in the next step.

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

Under main_sam_e51_cnano.c file, in function main, notice the call to function SYS_Initialize. The generated SYS_Initialize function initializes all the peripheral modules used in the application (configured through MCC).

Tip: Press CTRL key and left-click on SYS_Initialize function. The click will open the implementation for SYS_Initialize function.

SYS_Initialize function

Back to top


In the main_sam_e51_cnano.c function, below SYS_Initialize(), add the following code to register callback event handlers.

DMAC_ChannelCallbackRegister(DMAC_CHANNEL_0, usartDmaChannelHandler, 0);
EIC_CallbackRegister(EIC_PIN_15, EIC_User_Handler, 0);
RTC_Timer32CallbackRegister(rtcEventHandler, 0);

Following the addition of the above code add the function call and load reset message to USART Transmit Buffer.

sprintf((char*)uartTxBuffer, "Toggling LED at 500 milliseconds rate \r\n");
RTC_Timer32Start();

Initialize modules code

Note:

Function call DMAC_ChannelCallbackRegister registers a callback event handler with the DMA PLIB. The callback event handler is called by the DMA PLIB when the DMA transfer (of temperature sensor data to serial terminal) is complete.

Function call EIC_CallbackRegister registers an EIC callback event handler with the EIC PLIB. The callback event handler is called by EIC PLIB when the user presses the switch SW0.

Function call RTC_Timer32CallbackRegister registers an RTC callback event handler with the RTC PLIB. The callback event handler is called by the RTC PLIB when the configured time period has elapsed.

Back to top


Implement the registered callback event handlers for RTC, USART, and EIC PLIBs by adding the following code before the main() function in main_sam_e51_cnano.c.

static void EIC_User_Handler(uintptr_t context)
{
    changeTempSamplingRate = true;
}
static void rtcEventHandler (RTC_TIMER32_INT_MASK intCause, uintptr_t context)
{
   if (intCause & RTC_MODE0_INTENSET_CMP0_Msk)
    {            
        isRTCExpired    = true;
    }
}
static void usartDmaChannelHandler(DMAC_TRANSFER_EVENT event, uintptr_t contextHandle)
{
   if (event == DMAC_TRANSFER_EVENT_COMPLETE)
    {
        isUSARTTxComplete = true;
    }
}

Callback handlers

Back to top


Add the following code to submit a DMA channel transfer to transmit the LED toggling rate message when the configured time period (default 500 milliseconds) has elapsed. It also toggles the LED (LED0) by calling the LED0_Toggle() function.

if ((isRTCExpired == true) && (true == isUSARTTxComplete))
{
    isRTCExpired = false;
    isUSARTTxComplete = false;
    LED0_Toggle();
    DMAC_ChannelTransfer(DMAC_CHANNEL_0, uartTxBuffer, \
            (const void *)&(SERCOM5_REGS->USART_INT.SERCOM_DATA), \
            strlen((const char*)uartTxBuffer));
}

Toggle LED and submit a DMA channel transfer

Back to top


Following the addition of the above code, add the following code to implement the change of sampling rate and prepare the message for the same on the serial terminal when the user presses the SW0 switch.

Also, add the code to transfer the buffer containing the message mentioning the change of sampling rate over USART using DMA.

if(changeTempSamplingRate == true)
{
    changeTempSamplingRate = false;
   if(tempSampleRate == TEMP_SAMPLING_RATE_500MS)
    {
        tempSampleRate = TEMP_SAMPLING_RATE_1S;
        RTC_Timer32Compare0Set(PERIOD_1S);
    }
   else if(tempSampleRate == TEMP_SAMPLING_RATE_1S)
    {
        tempSampleRate = TEMP_SAMPLING_RATE_2S;
        RTC_Timer32Compare0Set(PERIOD_2S);                        
    }
   else if(tempSampleRate == TEMP_SAMPLING_RATE_2S)
    {
        tempSampleRate = TEMP_SAMPLING_RATE_4S;
        RTC_Timer32Compare0Set(PERIOD_4S);                                        
    }    
   else if(tempSampleRate == TEMP_SAMPLING_RATE_4S)
    {
       tempSampleRate = TEMP_SAMPLING_RATE_500MS;
       RTC_Timer32Compare0Set(PERIOD_500MS);
    }
   else
    {
        ;
    }
    RTC_Timer32CounterSet(0);
    sprintf((char*)uartLocalTxBuffer, "LED Toggling rate is changed to %s\r\n", &timeouts[(uint8_t)tempSampleRate][0]);
    DMAC_ChannelTransfer(DMAC_CHANNEL_0, uartLocalTxBuffer, \
            (const void *)&(SERCOM5_REGS->USART_INT.SERCOM_DATA), \
            strlen((const char*)uartLocalTxBuffer));
    sprintf((char*)uartTxBuffer, "Toggling LED at %s rate \r\n", &timeouts[(uint8_t)tempSampleRate][0]);
}

Implement the change of sampling rate

You are now ready to build the code!

Back to top