GD32F103使用DMA方式实现串口数据发送

  1. 选择合适的DMA通道
    在这里插入图片描述
  2. 代码
uint8_t txbuffer[] = "\n\rUSART DMA receive and transmit example, please input 10 bytes:\n\r";
#define ARRAYNUM(arr_name)     (uint32_t)(sizeof(arr_name) / sizeof(*(arr_name)))

#define USART0_DATA_ADDRESS    ((uint32_t)&USART_DATA(USART0))
void uart_dma_Init()
{
    
    
  dma_parameter_struct dma_init_struct;
  
  /* enable DMA0 clock */
  rcu_periph_clock_enable(RCU_DMA0);  
  
 /* initialize DMA channel0 */
  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);
  
  /* configure DMA mode */
  dma_circulation_disable(DMA0, DMA_CH3);
  dma_memory_to_memory_disable(DMA0, DMA_CH3);

  /* USART DMA enable for transmission */
  usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
  
  /* enable DMA transfer complete interrupt */
  dma_interrupt_enable(DMA0, DMA_CH3, DMA_INT_FTF);
  
  nvic_irq_enable(DMA0_Channel3_IRQn, 5, 0);
  
  /* enable DMA channel3 */
//  dma_channel_enable(DMA0, DMA_CH3);
}

void uart_dma_start()
{
    
    
  /* enable DMA channel3 */
  dma_channel_enable(DMA0, DMA_CH3);  
  
//  /* USART DMA enable for transmission */
//  usart_dma_transmit_config(USART0, USART_DENT_ENABLE);
}

/*!
    \brief      this function handles DMA_Channel3_IRQHandler interrupt
    \param[in]  none
    \param[out] none
    \retval     none
*/
void DMA0_Channel6_IRQHandler(void)
{
    
    
    if(dma_interrupt_flag_get(DMA0, DMA_CH6, DMA_INT_FLAG_FTF))
    {
    
         
        dma_interrupt_flag_clear(DMA0, DMA_CH6, DMA_INT_FLAG_G);
        dma_interrupt_flag_clear(DMA0, DMA_CH6, DMA_INT_FLAG_FTF);
        /* enable DMA channel3 */
      dma_channel_disable(DMA0, DMA_CH3);  
//      
//      /* USART DMA enable for transmission */
//      usart_dma_transmit_config(USART0, USART_DENT_DISABLE);
    }
}

void nvic_config(void)
{
    
    
    nvic_priority_group_set(NVIC_PRIGROUP_PRE3_SUB1);   //8级强占优先级,2级响应优先级
}

猜你喜欢

转载自blog.csdn.net/lljss1980/article/details/119833199