Getting Started Training Module Using MCC: Step 5

Step 5: 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® IDE and add application code by following these steps.

1

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.

app_code1.png
Click image to enlarge.

2

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();
app_code2.png

Note:

a

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.

b

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.

c

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.

3

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;
    }
}
app_code3.png

4

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));
}
app_code4.png

5

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]);
}
app_code5.png
Click image to enlarge.

You are now ready to build the code!


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