lugl4313820 发表于 2022-6-4 16:17

【GD32F310G-START】USART 不定长接收

<p>【前言】前面有网友说进不了中断IDLE,这里附上工程,希望对他有帮助:</p>

<p>1、配置usart0:</p>

<pre>
<code>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 USART0 tx */
    gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_9);

    /* connect port to USART0 rx */
    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);

    /* configure USART */
    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);
}
</code></pre>

<p>2、开启中断:</p>

<pre>
<code>/*!
    \brief      configure NVIC
    \paramnone
    \param none
    \retval   none
*/
void nvic_config(void)
{
    nvic_irq_enable(USART0_IRQn, 0, 0);
}
</code></pre>

<p>3、配置DMA</p>

<pre>
<code>void dma_config(void)
{
    dma_parameter_struct dma_init_struct;

    rcu_periph_clock_enable(RCU_DMA);

    /* deinitialize DMA channel2 (USART0 rx) */
    dma_deinit(DMA_CH2);
    dma_struct_para_init(&amp;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)&amp;USART_RDATA(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(DMA_CH2, &amp;dma_init_struct);
    /* configure DMA mode */
    dma_circulation_disable(DMA_CH2);
    /* enable DMA channel2 */
    dma_channel_enable(DMA_CH2);
}</code></pre>

<p>4、书写中断接收函数:</p>

<pre>
<code>/*!
    \brief      this function handles USART RBNE interrupt request and TBE interrupt request
    \paramnone
    \param none
    \retval   none
*/
void USART0_IRQHandler(void)
{
    if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE)) {
      /* clear IDLE flag */
      usart_interrupt_flag_clear(USART0, USART_INT_FLAG_IDLE);
      /* toggle the LED2 */
      gd_eval_led_toggle();

      /* number of data received */
      rx_count = 256 - (dma_transfer_number_get(DMA_CH2));
      receive_flag = 1;

      /* disable DMA and reconfigure */
      dma_channel_disable(DMA_CH2);
      dma_transfer_number_config(DMA_CH2, 256);
      dma_channel_enable(DMA_CH2);
    }
}
</code></pre>

<p>5、main函数:</p>

<pre>
<code>int main(void)
{
    gd_eval_led_init();
    gd_eval_led_on();
    nvic_config();

    /* initialize DMA */
    dma_config();

    /* initialize USART */
    usart_config();

    /* wait IDLEF set and clear it */
    while(RESET == usart_flag_get(USART0, USART_FLAG_IDLE));
    usart_flag_clear(USART0, USART_FLAG_IDLE);
    usart_interrupt_enable(USART0, USART_INT_IDLE);
    /* wait the data is received and send to the hyperterminal */
    while(1) {
      if(1 == receive_flag) {
            for(tx_count = 0; tx_count &lt; rx_count; tx_count++) {
                while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
                usart_data_transmit(USART0, rxbuffer);
            }
            receive_flag = 0;
      }
    }
}</code></pre>

<p>6、实验效果:</p>

<p> &nbsp;</p>

okhxyyo 发表于 2022-6-16 22:37

<p><img height="52" src="https://bbs.eeworld.com.cn/static/editor/plugins/hkemoji/sticker/facebook/pleased.gif" width="48" />谢谢分享,很用心呀</p>

lugl4313820 发表于 2022-6-17 07:13

okhxyyo 发表于 2022-6-16 22:37
谢谢分享,很用心呀

<p>感谢呀,非常感谢检查!</p>

还是叫我超人吧 发表于 2024-6-18 14:53

<p>串口配置里面 IDIE没使能,这条语句能实现吗?if(RESET != usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE))</p>
页: [1]
查看完整版本: 【GD32F310G-START】USART 不定长接收