Atmel START: Using the SAM E54 Event System with RTC, ADC, USART and DMA - Step 4

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.

2

In the main() function, below atmel_start_init(), call the function adc_async_enable_channel() with the ADC descriptor and channel number parameters as shown in the code snippet below.

codesnippet1.png

You can copy the code snippet from text box below.

/*Enable ADC*/ 
adc_async_enable_channel(&ADC_0, 0);

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.

codesnippet2.png
Step 4.2: Add a function configure_dma to configure and enable DMA steps 1-9.

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.

2

Below the usart_sync_enable() function, call the timer_start() function to start the timer as shown in the image below.

codesnippet3.png

You can copy the code snippet from below text box.

/*USART enable*/
usart_sync_enable(&USART_0);

/* Start The timer */
timer_start(&TIMER_0);

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.

codesnippet4.png

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);
}



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.