【GD32F310G-START】USART 不定长接收
[复制链接]
【前言】前面有网友说进不了中断IDLE,这里附上工程,希望对他有帮助:
1、配置usart0:
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);
}
2、开启中断:
/*!
\brief configure NVIC
\param[in] none
\param[out] none
\retval none
*/
void nvic_config(void)
{
nvic_irq_enable(USART0_IRQn, 0, 0);
}
3、配置DMA
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(&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_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, &dma_init_struct);
/* configure DMA mode */
dma_circulation_disable(DMA_CH2);
/* enable DMA channel2 */
dma_channel_enable(DMA_CH2);
}
4、书写中断接收函数:
/*!
\brief this function handles USART RBNE interrupt request and TBE interrupt request
\param[in] none
\param[out] 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);
}
}
5、main函数:
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 < rx_count; tx_count++) {
while(RESET == usart_flag_get(USART0, USART_FLAG_TBE));
usart_data_transmit(USART0, rxbuffer[tx_count]);
}
receive_flag = 0;
}
}
}
6、实验效果:
|