GD32F405VG 调试过程关于DAC/DMA/TIMER等问题的解决
GD32F405与STM32F405外设完全兼容,外设的地址映射也是完全兼容。ST的标准库中的名字与GD32的外设名字不一致,比如STM32中的TIMER1的地址映射,对应的是GD32的TIMER0,STM32的DAC1对应GD32的DAC0,等等诸如此类。
STM32F405的程序直接烧写到GD32F405中,程序能够运行但部分功能异常,在STM32F405中使用TIMER6触发DAC1转换,使用DAC1的数字量通过DMA传输,程序一切正常,但是GD32运行此程序,DAC无波形产生,DAC的DMA全传输完成中断无法进入,最后发现问题所在:
STM32中配置的TIMER6对应GD32的TIMER5(暂时还是称作TIMER6),但是TIMER6始终无法触发DAC产生波形,开启TIMER6的更新中断,DEBUG模式,程序可以进入TIMER6的更新中断,DAC依然没有模拟量输出,最后抱着试一试的心态使用TIMER5触发DAC,DAC波形出现,但是DMA依然无法正常运行,刚开始认为是硬件配置有问题,最后发现是初始化顺序有问题,DAC_OUT_INIT();后面增添了一段是DAC缓慢输出到所需电压的一段代码,没有此代码时,DMA运行正常,加上此代码段后,DMA不运行,原来是DAC外设初始化后紧接着初始化了DAC的DMA功能,最后使能了DAC输出以及DAC_DMACmd(DAC_Channel_1, ENABLE)就是这一句DAC_DMACmd(DAC_Channel_1, ENABLE)使能早了,如果没有Init_Laser()这一句,使能后没有问题,有了Init_Laser()这一句,DAC的DMA功能使能了但是DMA相应的通道没有使能,使程序异常。
运行不正常的代码如下:
//DAC初始化配置
void DAC_OUT_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*DAC1 is used to generate sweep signal */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T5_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
// /*DAC2 is used to set laser temperature*/
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* Time base configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 2000-1; // 1ms*500点
TIM_TimeBaseStructure.TIM_Prescaler = 42-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
/* TIM6 TRGO selection */
TIM_SelectOutputTrigger(TIM5, TIM_TRGOSource_Update);
// /*TIM6 IT_Update */
// TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
/* TIM6 enable counter */
TIM_Cmd(TIM5, ENABLE);
/* DMA1_Stream5 channel7 configuration DAC1 DMA request*/
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_7;
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DAC_DataTable;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = DAC_DATA_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
/* Enable the DMA Stream5 IRQ Channel */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA_ITConfig(DMA1_Stream5, DMA_IT_TC|DMA_IT_HT, ENABLE);//传输完成和半传输完成中断
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);//传输完成和半传输完成中断
DAC_Cmd(DAC_Channel_1,ENABLE);
DAC_Cmd(DAC_Channel_2,ENABLE);
DAC_DMACmd(DAC_Channel_1, ENABLE);
}
//主函数中调用
DAC_OUT_Init();//**************************DAC初始化配置
Init_Laser();//********************************初始化激光器,使电流缓慢上升
DMA_Cmd(DMA1_Stream5, ENABLE);//***********DMA通道使能,输出扫描信号波形:
当更换了TIMER以后,DAC工作正常,DMA工作不正常,最后根据实验现象发现是初始化顺序出现了问题,将AC_DMACmd(DAC_Channel_1, ENABLE); 从DAC_OUT_Init();中拿出来,放在Init_Laser();之后,程序运行正常,也就是,DAC初始化完之后没有立马使用DMA传输的话,不要使能DMA传输功能,放在最后使能。
能正常运行的代码如下:
//DAC初始化配置函数
void DAC_OUT_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
DAC_InitTypeDef DAC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*DAC1 is used to generate sweep signal */
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T5_TRGO;
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Disable;
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
// /*DAC2 is used to set laser temperature*/
DAC_InitStructure.DAC_Trigger = DAC_Trigger_None;
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
/* Time base configuration */
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
TIM_TimeBaseStructure.TIM_Period = 2000-1; // 1ms*500点
TIM_TimeBaseStructure.TIM_Prescaler = 42-1;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure);
/* TIM6 TRGO selection */
TIM_SelectOutputTrigger(TIM5, TIM_TRGOSource_Update);
// /*TIM6 IT_Update */
// TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);
/* TIM6 enable counter */
TIM_Cmd(TIM5, ENABLE);
/* DMA1_Stream5 channel7 configuration DAC1 DMA request*/
DMA_DeInit(DMA1_Stream5);
DMA_InitStructure.DMA_Channel = DMA_Channel_7;
DMA_InitStructure.DMA_PeripheralBaseAddr = DAC_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&DAC_DataTable;
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
DMA_InitStructure.DMA_BufferSize = DAC_DATA_SIZE;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA1_Stream5, &DMA_InitStructure);
/* Enable the DMA Stream5 IRQ Channel */
NVIC_InitStructure.NVIC_IRQChannel = DMA1_Stream5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//DMA_ITConfig(DMA1_Stream5, DMA_IT_TC|DMA_IT_HT, ENABLE);//传输完成和半传输完成中断
DMA_ITConfig(DMA1_Stream5, DMA_IT_TC, ENABLE);//传输完成和半传输完成中断
DAC_Cmd(DAC_Channel_1,ENABLE);
DAC_Cmd(DAC_Channel_2,ENABLE);
// DAC_DMACmd(DAC_Channel_1, ENABLE);
}
//主函数中调用
DAC_OUT_Init();//**************************DAC初始化配置
Init_Laser();//****************************初始化激光器,使电流缓慢上升
DAC_DMACmd(DAC_Channel_1, ENABLE); //2021-6-5从 DAC_OUT_Init() 函数中提取出来,放在此处,程序运行正常
DMA_Cmd(DMA1_Stream5, ENABLE);//***********DMA通道使能,输出扫描信号波形