EFM32例程——DMA

硬件:EFM32GG230512
工具:keil5

DMA实验
DMA测试,内存间传输

初始化

/*********************************************Copyright (c)***********************************************
** File name:             	adc.c
** Created by:				Gui              
** Version:               	V1.0.0
** Descriptions:           	DMA测试,内存间传输
*********************************************************************************************************/
#include "dma.h"

/* 
 *  DMA控制块,必须根据通道数进行地址对齐
 */
#if (DMA_CHAN_COUNT > 16) 
#define DMA_CONTROL_BLOCK_ALIGNMENT 1024 
#elif (DMA_CHAN_COUNT > 8) 
#define DMA_CONTROL_BLOCK_ALIGNMENT 512 
#elif (DMA_CHAN_COUNT > 4) 
#define DMA_CONTROL_BLOCK_ALIGNMENT 256 
#elif (DMA_CHAN_COUNT > 2) 
#define DMA_CONTROL_BLOCK_ALIGNMENT 128 
#endif

/* 
 *  根据不同的编译器对齐起始地址
 */
#if defined (__ICCARM__)
#pragma data_alignment=DMA_CONTROL_BLOCK_ALIGNMENT
    DMA_DESCRIPTOR_TypeDef dmaControlBlock[DMA_CHAN_COUNT * 2];
#elif defined (__CC_ARM)
    DMA_DESCRIPTOR_TypeDef dmaControlBlock[DMA_CHAN_COUNT * 2] \
    __attribute__ ((aligned(DMA_CONTROL_BLOCK_ALIGNMENT)));
#elif defined (__GNUC__)
    DMA_DESCRIPTOR_TypeDef dmaControlBlock[DMA_CHAN_COUNT * 2] \
    __attribute__ ((aligned(DMA_CONTROL_BLOCK_ALIGNMENT)));
#else
#error Undefined toolkit, need to define alignment 
#endif

DMA_CB_TypeDef	test_cb;//DMA回调结构体
volatile bool	transfer_flag;//传输标志

//channel:传输通道
//primary:是否使用primary结构体
//user:用户附加处理
void test_callback(unsigned int uiChannel, bool bPrimary, void *vpUser)
{ 
    transfer_flag = false;                                           /* 清除表示传输完成             */
}

void dma_init(void){
	
	DMA_Init_TypeDef        tDmaInit;
	DMA_CfgChannel_TypeDef  tChnlCfg;
	DMA_CfgDescr_TypeDef    tDescrCfg;

	CMU_ClockEnable(cmuClock_DMA, true);  
	
	//初始化DMA 
    tDmaInit.hprot        = 0;
    tDmaInit.controlBlock = dmaControlBlock;
    DMA_Init(&tDmaInit);
	
	//设置回调函数 
    test_cb.cbFunc  = test_callback;
    test_cb.userPtr   = NULL;
	
	//设置通道
    tChnlCfg.highPri   = false;
    tChnlCfg.enableInt = true;
    tChnlCfg.select    = 0;
    tChnlCfg.cb      = &(test_cb);
    DMA_CfgChannel(0, &tChnlCfg);
	
	//设置通道描述符
    tDescrCfg.dstInc  = dmaDataInc4;
    tDescrCfg.srcInc  = dmaDataInc4;
    tDescrCfg.size    = dmaDataSize4;
    tDescrCfg.arbRate = dmaArbitrate4;
    tDescrCfg.hprot   = 0;
    DMA_CfgDescr(0, true, &tDescrCfg);
}


main

/*********************************************Copyright (c)***********************************************
** File name:             	  	main.c
** Created by:					Gui              
** Version:               		V1.0.0        
** Descriptions:            	DMA测试,内存间传输
*********************************************************************************************************/
#include "system.h"
#include "em_device.h"
#include "em_chip.h"
#include "em_cmu.h"
#include "em_int.h"
#include "delay.h"
#include "dma.h"
#include "bsp_led.h"

uint32_t g_chip_tick = 0;

uint32_t transfer_data[10] = {0,1,2,3,4,5,6,7,8,9};//传输数据
uint32_t ram_data[10];//存放数据数组
	
int main(){
	
	transfer_flag = true;
	
	CMU_ClockEnable(cmuClock_HFPER, true); //前置芯片设置
	CMU_ClockEnable(cmuClock_GPIO, true);
	if(SysTick_Config(SystemCoreClockGet()/1000))while(1);//1ms的systick中断
	led_init();//PD4 PD5
	dma_init();
	
    DMA_ActivateAuto(0,true,(void *)&ram_data,(void *)&transfer_data,10 - 1);
	
	g_chip_tick = g_rtc_rtcFreq;
	LED0_ON;LED1_OFF;
	INT_Disable();//关闭中断
	while(transfer_flag);
	INT_Enable();//开启中断
	while(1){
		if(g_rtc_rtcFreq - g_chip_tick > 1000 || g_chip_tick > g_rtc_rtcFreq){
			LED0_OFF;LED1_ON;
			break;
		}
	}
	
	while(1){
	}
}


你可能感兴趣的:(EFM32)