GD32 DMA+IDLE
1 void USART0_IRQHandler(void) 2 { 3 uint32_t buff_length = 0; 4 if (RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) 5 { 6 usart_data_receive(USART0); 7 dma_channel_disable(DMA0, DMA_CH4); 8 buff_length = 128 - dma_transfer_number_get(DMA0,DMA_CH4); 9 memcpy(usart0_txbuffer,usart0_rxbuffer,buff_length); 10 UART0_Receive_DMA(usart0_rxbuffer,128); 11 } 12 }
1 void UART0_INIT(int baudrate) 2 { 3 rcu_periph_clock_enable(RCU_GPIOA); 4 rcu_periph_clock_enable(RCU_USART0); 5 //UART_TX 6 gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, USART0_TX); 7 8 //UART_RX 9 gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, USART0_RX); 10 11 usart_deinit(USART0); 12 13 usart_baudrate_set(USART0, baudrate); 14 usart_word_length_set(USART0, USART_WL_8BIT); 15 usart_stop_bit_set(USART0, USART_STB_1BIT); 16 usart_parity_config(USART0, USART_PM_NONE); 17 usart_hardware_flow_rts_config(USART0, USART_RTS_DISABLE); 18 usart_hardware_flow_cts_config(USART0, USART_CTS_DISABLE); 19 usart_receive_config(USART0, USART_RECEIVE_ENABLE); 20 usart_transmit_config(USART0, USART_TRANSMIT_ENABLE); 21 22 //设置串口中断使能 23 usart_interrupt_enable(USART0,USART_INT_IDLE); 24 25 usart_enable(USART0); 26 }
1 void UART0_Transmit_DMA(uint8_t *txbuffer, uint16_t Size) 2 { 3 /***************** DMA_TX_CONFIG ****************/ 4 5 dma_parameter_struct dma_init_struct; 6 /* enable DMA0 */ 7 rcu_periph_clock_enable(RCU_DMA0); 8 /* deinitialize DMA channel3(USART0 tx) */ 9 dma_deinit(DMA0, DMA_CH3); 10 dma_init_struct.direction = DMA_MEMORY_TO_PERIPHERAL; 11 dma_init_struct.memory_addr = (uint32_t)txbuffer; 12 dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; 13 dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT; 14 dma_init_struct.number = Size; 15 dma_init_struct.periph_addr = USART0_DATA_ADDRESS; 16 dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; 17 // dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT; 18 dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH; 19 dma_init(DMA0, DMA_CH3, &dma_init_struct); 20 /* configure DMA mode */ 21 dma_circulation_disable(DMA0, DMA_CH3); 22 dma_memory_to_memory_disable(DMA0, DMA_CH3); 23 24 // dma_deinit(DMA0, DMA_CH4); 25 // dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY; 26 // dma_init_struct.memory_addr = (uint32_t)rx_buffer; 27 // dma_init(DMA0, DMA_CH4, &dma_init_struct); 28 // /* configure DMA mode */ 29 // dma_circulation_disable(DMA0, DMA_CH4); 30 // dma_memory_to_memory_disable(DMA0, DMA_CH4); 31 dma_channel_enable(DMA0, DMA_CH3); 32 usart_dma_transmit_config(USART0, USART_DENT_ENABLE); 33 // while(RESET == dma_flag_get(DMA0, DMA_CH3, DMA_FLAG_FTF)){ 34 // } 35 }
1 void UART0_Receive_DMA(uint8_t *txbuffer, uint16_t Size) 2 { 3 /***************** DMA_TX_CONFIG ****************/ 4 dma_parameter_struct dma_init_struct; 5 /* enable DMA1 */ 6 rcu_periph_clock_enable(RCU_DMA0); 7 dma_deinit(DMA0, DMA_CH4); 8 dma_init_struct.direction = DMA_PERIPHERAL_TO_MEMORY; 9 dma_init_struct.memory_addr = (uint32_t)txbuffer; 10 dma_init_struct.memory_inc = DMA_MEMORY_INCREASE_ENABLE; 11 dma_init_struct.memory_width = DMA_MEMORY_WIDTH_8BIT; 12 dma_init_struct.number = Size; 13 dma_init_struct.periph_addr = USART0_DATA_ADDRESS; 14 dma_init_struct.periph_inc = DMA_PERIPH_INCREASE_DISABLE; 15 dma_init_struct.periph_width = DMA_PERIPHERAL_WIDTH_8BIT; 16 dma_init_struct.priority = DMA_PRIORITY_ULTRA_HIGH; 17 // dma_single_data_mode_init(DMA0, DMA_CH5, dma_init_struct); 18 //查看手册 19 dma_init(DMA0, DMA_CH4, &dma_init_struct); // 20 // 21 /* configure DMA mode */ 22 dma_circulation_disable(DMA0, DMA_CH4); 23 dma_memory_to_memory_disable(DMA0, DMA_CH4); 24 // dma_channel_subperipheral_select(DMA0, DMA_CH5, DMA_SUBPERI4); 25 /* enable DMA channel2 */ 26 dma_channel_enable(DMA0, DMA_CH4); 27 28 usart_dma_receive_config(USART0, USART_DENR_ENABLE); 29 /* wait DMA channel transfer complete */ 30 // while(RESET == dma_flag_get(DMA0, DMA_CH5, DMA_INTF_FTFIF)); 31 32 }