手頭項(xiàng)目也需要加入DMA數(shù)據(jù)傳輸,以最大限度地提升CPU效率,于是測(cè)試了一下XMEGA的DMA模塊,把一塊內(nèi)存中的數(shù)據(jù)DMA傳輸?shù)搅硗庖粔K內(nèi)存,DMA傳輸完成后,在中斷函數(shù)中顯示“DMAFinished”,提示DMA成功完成數(shù)據(jù)傳輸,另外DMA使用更多的情況是大量數(shù)據(jù)到USART、SPI等,本文只是小試牛刀。
效果如下,
源代碼:
#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();
。。。
}
-
cpu
+關(guān)注
關(guān)注
68文章
10889瀏覽量
212383 -
數(shù)據(jù)傳輸
+關(guān)注
關(guān)注
9文章
1928瀏覽量
64717 -
dma
+關(guān)注
關(guān)注
3文章
566瀏覽量
100750
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論