DMA data transfer (source code sharing)

The project at hand also needs to add DMA data transmission to maximize CPU efficiency. So I tested the XMEGA DMA module and transferred the DMA data in one memory to another memory. After the DMA transfer is completed, it displays in the interrupt function. DMA Finished", prompting DMA to successfully complete the data transfer. In addition, the DMA uses more data to the USART, SPI, etc. This article is just a small test.

The effect is as follows,

DMA data transfer (source code sharing)

Source code:

#define DMA_BUFFER_SIZE 1024

#define DMA_CHANNEL 0

Uint8_t source[DMA_BUFFER_SIZE], destination[DMA_BUFFER_SIZE];

Static void fill_pattern(uint8_t *buffer, size_t len)

{

Int i;

For (i = 0; i " len; i++) {

Buffer = 42 ^ (i & 0xff) ^ (i 》 8);

}

}

Static bool verify_pattern(uint8_t *buffer, size_t len)

{

For (size_t i = 0; i " len; i++) {

If (buffer != (42 ^ (i & 0xff) ^ (i 》) 8))) {

Return false;

}

}

Return true;

}

Void dma_test(void)

{

Struct dma_channel_config config;

Fill_pattern(source, DMA_BUFFER_SIZE);

Memset(destination, 0, DMA_BUFFER_SIZE);

Dma_enable();

Memset(&config, 0, sizeof(config));

/*

* This example will configure a DMA channel with the following

* settings:

* - Low interrupt priority

* - 1 byte burst length

* - DMA_BUFFER_SIZE bytes for each transfer

* - Reload source and destination address at end of each transfer

* - Increment source and destination address during transfer

* - Source address is set to ef source

* - Destination address is set to ef destination

*/

Dma_channel_set_interrupt_level(&config, DMA_INT_LVL_LO);

Dma_channel_set_burst_length(&config, DMA_CH_BURSTLEN_1BYTE_gc);

Dma_channel_set_transfer_count(&config, DMA_BUFFER_SIZE);

Dma_channel_set_src_reload_mode(&config,

DMA_CH_SRCRELOAD_TRANSACTION_gc);

Dma_channel_set_dest_reload_mode(&config,

DMA_CH_DESTRELOAD_TRANSACTION_gc);

Dma_channel_set_src_dir_mode(&config, DMA_CH_SRCDIR_INC_gc);

Dma_channel_set_dest_dir_mode(&config, DMA_CH_DESTDIR_INC_gc);

Dma_channel_set_source_address(&config, (uint16_t)(uintptr_t)source);

Dma_channel_set_destination_address(&config,

(uint16_t)(uintptr_t)destination);

Dma_channel_write_config(DMA_CHANNEL, &config);

/* Use the configuration above by enabling the DMA channel in use. */

Dma_channel_enable(DMA_CHANNEL);

/*

* Enable interrupts as the example is now configured to handle them

* properly.

*/

Cpu_irq_enable();

/*

* Trigger a manual start since there is no trigger sources used in

* this example.

*/

Dma_channel_trigger_block_transfer(DMA_CHANNEL);

Pmic_init();

Cpu_irq_enable();

While(1);

}

ISR (DMA_CH0_vect)

{

Gfx_mono_draw_string("DMA Finished",0,0,&sysfont);

}

Int main(void)

{

. . .

Dma_test();

. . .

}

51V Battery pack

51V Battery Pack,Portable Battery Box,Portable Battery Bank,Ac Battery Pack

Zhejiang Casnovo Materials Co., Ltd. , https://www.casnovo-new-energy.com