本文主要是介绍【GD32】GD32F303串口设置DMA发生中断无法进入中断函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在GD32F303官方提供的串口例程中,有一个DMA发生和接收中断例程,在模仿着写的过程中,能够正常发送数据,但是无法进入中断函数。
DMA0_Channel3_IRQHandler函数时官方定义的弱函数,需要自己重新实现。如果开启了DMA0通道3相关的中断,在发生中断的时候就会进入该函数。DMA的中断主要有以下3个,每一个通道都有自己的3个中断。
例程的代码主要:
int main(void)
{dma_parameter_struct dma_init_struct;/* enable DMA0 */rcu_periph_clock_enable(RCU_DMA0);/* initialize USART */gd_eval_com_init(EVAL_COM0);/*configure DMA0 interrupt*/nvic_config();/* deinitialize DMA channel3(USART0 tx) */dma_deinit(DMA0, DMA_CH3);dma_struct_para_init(&dma_init_struct);dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL;dma_init_struct.memory_addr = (uint32_t)txbuffer;dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;dma_init_struct.number = ARRAYNUM(txbuffer);dma_init_struct.periph_addr = USART0_DATA_ADDRESS;dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;dma_init(DMA0, DMA_CH3, &dma_init_struct);/* deinitialize DMA channel4 (USART0 rx) */dma_deinit(DMA0, DMA_CH4);dma_struct_para_init(&dma_init_struct);dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY;dma_init_struct.memory_addr = (uint32_t)rxbuffer;dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE;dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT;dma_init_struct.number = 10;dma_init_struct.periph_addr = USART0_DATA_ADDRESS;dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE;dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT;dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH;dma_init(DMA0, DMA_CH4, &dma_init_struct);/* configure DMA mode */dma_circulation_disable(DMA0, DMA_CH3);dma_memory_to_memory_disable(DMA0, DMA_CH3);dma_circulation_disable(DMA0, DMA_CH4);dma_memory_to_memory_disable(DMA0, DMA_CH4);/* enable USART DMA for reception */usart_dma_receive_config(USART0, USART_RECEIVE_DMA_ENABLE);/* enable DMA0 channel4 transfer complete interrupt */dma_interrupt_enable(DMA0, DMA_CH4, DMA_INT_FTF);/* enable DMA0 channel4 */dma_channel_enable(DMA0, DMA_CH4);/* enable USART DMA for transmission */usart_dma_transmit_config(USART0, USART_TRANSMIT_DMA_ENABLE);/* enable DMA0 channel3 transfer complete interrupt */dma_interrupt_enable(DMA0, DMA_CH3, DMA_INT_FTF);/* enable DMA0 channel3 */dma_channel_enable(DMA0, DMA_CH3);/* waiting for the transfer to complete*/while(RESET == g_transfer_complete){}g_transfer_complete = RESET;/* waiting for the transfer to complete*/while(RESET == g_transfer_complete){}printf("\n\r%s\n\r", rxbuffer);while(1){}
}/*!\brief configure DMA interrupt\param[in] none\param[out] none\retval none
*/
void nvic_config(void)
{nvic_irq_enable(DMA0_Channel3_IRQn, 0, 0);nvic_irq_enable(DMA0_Channel4_IRQn, 0, 1);
}
这里要特别注意是dma_interrupt_enable(DMA0, DMA_CH3, DMA_INT_FTF);
,必须要在dma_deinit(DMA0, DMA_CH3);
这个函数的后面。如果他们两个顺序反了,就会无法成功开启中断。
dma_deinit(DMA0, DMA_CH3);
该函数主要是将DMA0的通道3去初始化,后面会接着dma_init
函数。该函数主要将各种寄存器数值重置为0,有点类似于计算器的归0。
在函数内部会将0赋值给CHCTL寄存器,这个寄存器很重要。看数据手册,CHCTL是通道控制寄存器,bit0是CHEN通道使能位,bit1时FTFIE通道传输完成中断使能为,bit2是HTFIE通道半传输完成中断使能,bit3是ERRIE通道错误中断使能位。所以中断使能函数操作的就是该寄存器的某个位。
dma_interrupt_enable(DMA0, DMA_CH3, DMA_INT_FTF);
该函数主要是开启DMA通道的中断,DMA_INT_FTF开启的是通道传输完成中断。如下所示,主要是CHCTL寄存器赋值,也就是将CHCTL的bit1置1。
到这里,我就看出来了。我在配置DMA通道中断的过程中,将dma_interrupt_enable(DMA0, DMA_CH3, DMA_INT_FTF);
提前了,想着和nvic_irq_enable
函数凑在一起,都是使能中断,看起来顺眼,没想到不能提前开启中断。
与上述函数类似不能放在前面的还有dma_channel_enable
函数。
仅此记录。
这篇关于【GD32】GD32F303串口设置DMA发生中断无法进入中断函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!