GD32E230串口DMA发送接收
串口使用DMA可以节省MCU运行的时间,节省效率,把单片机内部资源用到最优化。串口DMA就是配置好初始化后,调用串口发送程序时先把要发送的数据保存到DMA缓存,然后使能DMA发送,接下来串口就由硬件自动发送数据,软件可以继续处理其他功能。
串口初始化分一下几个阶段:
·GPIO初始化
·串口初始化
·中断使能初始化
·DMA初始化
void Uart_Init(void){/* initilize the com */ com_gpio_init(); com_usart_init(); /*configure DMA interrupt*/ nvic_config(); TxRx_DMA_Config();}
void com_gpio_init(void) //以串口0为例,gpio为PA9 PA10{rcu_periph_clock_enable(RCU_GPIOA);//使能IO口时钟gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);//配置复用功能gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_10);/* configure USART Tx as alternate function push-pull */ gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_9); gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_9); /* configure USART Rx as alternate function push-pull */ gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP, GPIO_PIN_10); gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, GPIO_PIN_10);}
void com_usart_init(void)//串口初始化{/* enable USART clock */ rcu_periph_clock_enable(RCU_USART0);//使能串口时钟 /* USART configure */ usart_deinit(USART0); usart_baudrate_set(USART0, 115200U); usart_receive_config(USART0, USART_RECEIVE_ENABLE); usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); usart_enable(USART0);}
void nvic_config(void)
{
nvic_irq_enable(DMA_Channel1_2_IRQn, 0);
}
void TxRx_DMA_Config(void)//DMA发送接收初始化{dma_parameter_struct dma_init_struct; /* enable DMA clock */ rcu_periph_clock_enable(RCU_DMA); /* initialize DMA channel1 */ dma_deinit(DMA_CH1); 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 = TX_BUF_LEN; dma_init_struct.periph_addr = USART0_TDATA_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(DMA_CH1, &dma_init_struct); /* initialize DMA channel2 */ dma_deinit(DMA_CH2); 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 = RX_BUF_LEN; dma_init_struct.periph_addr = USART0_RDATA_ADDRESS; dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; dma_init_struct.memory_width = DMA_PERIPHERAL_WIDTH_8BIT; dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH; dma_init(DMA_CH2, &dma_init_struct); /* configure DMA mode */ dma_circulation_disable(DMA_CH1); dma_memory_to_memory_disable(DMA_CH1); dma_circulation_disable(DMA_CH2); dma_memory_to_memory_disable(DMA_CH2); /* USART DMA enable for reception */ usart_dma_receive_config(USART0, USART_DENR_ENABLE); /* enable DMA channel2 transfer complete interrupt */ dma_interrupt_enable(DMA_CH2, DMA_INT_FTF); /* enable DMA channel2 */ dma_channel_enable(DMA_CH2); /* USART DMA enable for transmission */ usart_dma_transmit_config(USART0, USART_DENT_ENABLE); /* enable DMA channel1 transfer complete interrupt */ dma_interrupt_enable(DMA_CH1, DMA_INT_FTF); /* enable DMA channel1 */ dma_channel_enable(DMA_CH1);}```cvoid Use_DMA_SendTX1_Data(uint8_t *buffter, uint8_t writeLen){//串口DMA发送函数,发送数据时调用这个函数,发送完成后会产生一个中断uint8 i;//, Tx_Buf1[64]; dma_parameter_struct dma_init_struct; for (i = 0; i < writeLen; i ++) { txbuffer[i] = buffter[i]; } dma_channel_disable(DMA_CH1); dma_memory_address_config(DMA_CH1, (uint32)txbuffer); dma_transfer_number_config(DMA_CH1, (uint32_t)writeLen); /* enable DMA channel1 */ dma_channel_enable(DMA_CH1); /* USART DMA enable for transmission */ usart_dma_transmit_config(USART0, USART_DENT_ENABLE);}
void DMA_Channel1_2_IRQHandler(void)//发送接收通道中断处理{static uint32 Rx_num; if(RESET != dma_interrupt_flag_get(DMA_CH1, DMA_INT_FLAG_FTF)){ dma_interrupt_flag_clear(DMA_CH1, DMA_INT_FLAG_G); } if(RESET != dma_interrupt_flag_get(DMA_CH2, DMA_INT_FLAG_FTF)){ dma_channel_disable(DMA_CH2); dma_interrupt_flag_clear(DMA_CH2, DMA_INT_FLAG_G); dma_flag_clear(DMA_CH2, DMA_FLAG_G); Rx_num = dma_transfer_number_get(DMA_CH2); dma_transfer_number_config(DMA_CH2,RX_BUF_LEN); dma_channel_enable(DMA_CH2); }}
持续更新编码get到的新知识,新技能,持续分享心得体会。
赞 (0)