天天看點

GD32F1系列序列槽DMA不定長接收

關于GD32F1系列序列槽DMA不定長接收資料詳解

1.使用序列槽:USART0,使用DMA0,CH4

2.序列槽配置

void usart_config(void)
{
    /* enable GPIO clock */
    rcu_periph_clock_enable(RCU_GPIOA);

    /* enable USART clock */
    rcu_periph_clock_enable(RCU_USART0);
    
    /* connect port to USARTx_Tx */
    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);

    /* connect port to USARTx_Rx */
    gpio_init(GPIOA, GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, GPIO_PIN_10);

    /* 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_dma_receive_config(USART0, USART_DENR_ENABLE);
    usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
    usart_enable(USART0);
}
           

3.DMA配置

uint8_t rxbuffer[256];

void dma_config(void)
{
    dma_parameter_struct dma_init_struct;
    
    rcu_periph_clock_enable(RCU_DMA0);
    
    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 = 256;
    dma_init_struct.periph_addr = ((uint32_t)&USART_DATA(USART0));
    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);
    
    dma_circulation_disable(DMA0, DMA_CH4);
    
    dma_channel_enable(DMA0, DMA_CH4);
}
           

4.中斷配置

void nvic_config(void)
{   
    nvic_priority_group_set(NVIC_PRIGROUP_PRE2_SUB2);
    nvic_irq_enable(USART0_IRQn, 0, 0);
	  nvic_irq_enable(TIMER1_IRQn, 1, 1);
}
           

5.序列槽中斷函數

void USART0_IRQHandler(void)
{
   if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE))
	 {
        usart_interrupt_flag_clear(USART0, USART_FLAG_IDLEF);
        usart_data_receive(USART0);
			   receive_flag = 1;
   }
		 /* number of data received */
        rx_count = 256 - (dma_transfer_number_get(DMA0, DMA_CH4));
       
        
        /* disable DMA and reconfigure */
        dma_channel_disable(DMA0, DMA_CH4);
        dma_transfer_number_config(DMA0, DMA_CH4, 256);
        dma_channel_enable(DMA0, DMA_CH4);
}
           

6.序列槽發送函數

uint8_t uart_send(uint32_t usart_periph, uint8_t * data,uint8_t len)
{ 
	    uint8_t tx_count = 0;
	    for(tx_count = 0; tx_count < len; tx_count++){
                while(RESET == usart_flag_get(usart_periph, USART_FLAG_TBE));
                usart_data_transmit(usart_periph, data[tx_count]);
      }
	 
}
           

7.主函數

int main(void)
{ 
    dma_config(); 
    usart_config();
    nvic_config();
    
    usart_flag_clear(USART0, USART_FLAG_IDLEF);
    usart_interrupt_enable(USART0, USART_INT_IDLE);
    .............
    .............
}
           

繼續閱讀