Getting Started Training Module Using MCC: 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.
View SYS_Initialize() function
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).
Register callback event handlers
In the main_sam_e51_cnano.c function, below SYS_Initialize(), add the following code to register callback event handlers.
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.
RTC_Timer32Start();
Implement the registered callback event handlers
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.
{
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;
}
}
Toggle LED and submit a DMA channel transfer
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.
{
isRTCExpired = false;
isUSARTTxComplete = false;
LED0_Toggle();
DMAC_ChannelTransfer(DMAC_CHANNEL_0, uartTxBuffer, \
(const void *)&(SERCOM5_REGS->USART_INT.SERCOM_DATA), \
strlen((const char*)uartTxBuffer));
}
Implement the change of sampling rate
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.
{
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]);
}
You are now ready to build the code!