本例的DMA是从源数组到目标地址。各512个字节。
利用上次的工程不用新建,加入DMAC模块。
再打开ASF EXPLORER 打开快速指导。Quick Start Guide for Memory to Memory Data Transfer Using DMAC
按照里边的提示形成工程。
然后编译。在没有仿真之前,在目地数组名点右键加入到观察窗口。
程序运行后,源数组数据考入了目地数组中。见下面截图:
以下是程序清单:
#include <asf.h>
#define DATA_LENGTH (512)
static uint8_t source_memory[DATA_LENGTH];
static uint8_t destination_memory[DATA_LENGTH];
static volatile bool transfer_is_done = false;
COMPILER_ALIGNED(16)
DmacDescriptor example_descriptor;
static void transfer_done(struct dma_resource* const resource )
{
transfer_is_done = true;
}
static void configure_dma_resource(struct dma_resource *resource)
{
struct dma_resource_config config;
dma_get_config_defaults(&config);
dma_allocate(resource, &config);
}
static void setup_transfer_descriptor(DmacDescriptor *descriptor )
{
struct dma_descriptor_config descriptor_config;
dma_descriptor_get_config_defaults(&descriptor_config);
descriptor_config.block_transfer_count = sizeof(source_memory);
descriptor_config.source_address = (uint32_t)source_memory +
sizeof(source_memory);
descriptor_config.destination_address = (uint32_t)destination_memory +
sizeof(source_memory);
dma_descriptor_create(descriptor, &descriptor_config);
}
int main (void)
{
system_init();
struct dma_resource example_resource;
configure_dma_resource(&example_resource);
setup_transfer_descriptor(&example_descriptor);
dma_add_descriptor(&example_resource, &example_descriptor);
dma_register_callback(&example_resource, transfer_done,
DMA_CALLBACK_TRANSFER_DONE);
dma_enable_callback(&example_resource, DMA_CALLBACK_TRANSFER_DONE);
for (uint32_t i = 0; i < DATA_LENGTH; i++) {
source_memory[i] = i;
}
dma_start_transfer_job(&example_resource);
dma_trigger_transfer(&example_resource);
while (!transfer_is_done) {
/* Wait for transfer done */
}
while (true) {
/* Nothing to do */
}
}
|