Step 4.1: Add code to enable ADC
Atmel START drivers typically initialize the peripherals and you may need to enable it from the application code. Here, you will enable the ADC, USART, DMAC and start the timer to begin using these peripherals.
1
Open the main.c file from the Solution Explorer.
Step 4.2: Add a function to configure and enable DMA
1
Above the main() function, add the configure_dma() function with no arguments and no return value.
2
Set the DMA source address to the ADC RESULT register using the function _dma_set_source_address().
3
Set the DMA destination address to the USART DATA register using the function _dma_set_destination_address().
4
Set the number of bytes to read as '1' using the function _dma_set_data_amount().
5
Register an application callback on the DMA transfer complete event using functions _dma_get_channel_resource() and _dma_set_irq_state(). A DMA interrupt is enabled and callback is registered to run the system in an infinite loop for printing the light sensor values continuously.
6
Enable the DMA channel by calling the _dma_enable_transaction() function.
7
Above the configure_dma() function, add the callback function dma_complete_callback() with no return value.
8
In the main() function, below the adc_async_enable_channel() function, call the function configure_dma().
9
Include #include <hpl_dma.h> in the main.c file.
You can copy the code snippet from the text box below.
-----Preprocessor directive-----
#include <hpl_dma.h>
-----DMA call back function to be added------
static void dma_complete_callback(struct _dma_resource *resource)
{
}
-----DMA configuration function to be added-----
static void configure_dma(void)
{
struct _dma_resource* dma_res;
/*Set DMA source address*/
_dma_set_source_address(0, (uint8_t*)&(((Adc *)(ADC_0.device.hw))->RESULT.reg));
/*Set DMA destination address*/
_dma_set_destination_address(0, (uint8_t*)&(((Sercom *)(USART_0.device.hw))->USART.DATA.reg));
/*Set how many bytes to read*/
_dma_set_data_amount(0, (uint32_t)1);
/*Registering application callback after DMA transfer*/
_dma_get_channel_resource(&dma_res, 0);
dma_res->dma_cb.transfer_done = dma_complete_callback;
_dma_set_irq_state(0, DMA_TRANSFER_COMPLETE_CB, true);
/*Enable DMA channel*/
_dma_enable_transaction(0, false);
}
-----Changes in main function------
/*Configure and enable DMA*/
configure_dma();
Step 4.3: Add code to enable USART and start timer
1
In the main() function, below the configure_dma() function, call the function usart_sync_enable() to enable USART as shown in the below image.
Step 4.4: Add sleep implementation to the project
Sleep implementation is added to move the SAME54 MCU to sleep mode to save power as all operations in the project are carried out without the intervention of CPU (using EVENT SYSTEM).
1
In the main() function, inside while(1), add the sleep() function as shown in the image below.
2
In the dma_complete_callback() function, enable DMA again to run the system in an infinite loop for printing the light sensor values continuously as shown in the following image. Respective interrupt and callback are registered in section 4.2.5.
You can copy the code snippet from the text box below.
-----Changes in dma_complete_callback() function-----
_dma_enable_transaction(0, false);
-----Changes in main() function-----
while(1)
{
/* Enter IDLE Sleep Mode */
sleep(PM_SLEEPCFG_SLEEPMODE_STANDBY);
}