[liklon plays with GD32F350] 2. Serial port interruption experiment[Copy link]
In subsequent development, two serial ports are needed. From the manual, we can see that this chip has two UART modules. The example given in the demo uses UART1 (although there are a few comments saying UART2, don't be misled). 1.demo code analysis
void EvbUart1Config(void) { rcu_periph_clock_enable(RCU_GPIOA); rcu_periph_clock_enable(RCU_USART1); /* connect port to USART1_Tx */ gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_2); /* connect port to USARTx_R1 */ gpio_af_set(GPIOA, GPIO_AF_1, GPIO_PIN_3); /* configure USART1 Tx as alternate function push-pull */ gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_PULLUP,GPIO_PIN_2); gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ,GPIO_PIN_2); /* This section is the initialization of the serial port in the demo. It can be seen that after deinit, only the baud rate is configured, and the rest of the check (none), data length (8 bits) and stop bit (1 bit) are all default. There is no configuration and enable interrupt. What I need is to receive triggered by interrupt, and then sort and modify it based on this. 2. Add receive interrupt[code]void uart_init() { usart_deinit(USART1); usart_baudrate_set(USART1,115200); usart_parity_config(USART1, USART_PM_NONE); usart_word_length_set(USART1, USART_WL_8BIT); usart_stop_bit_set(USART1, USART_STB_1BIT); usart_receive_config(USART1, USART_RECEIVE_ENABLE); usart_transmit_config(USART1, USART_TRANSMIT_ENABLE); usart_interrupt_enable(USART1, USART_INT_RBNEIE); nvic_irq_enable(USART1_IRQn, 0, 2); usart_enable(USART1); }
复制代码
In order to make the configuration look more reasonable, configure the checksum and other items once. The receive interrupt is enabled (the receive interrupt is triggered when it is not empty). It is not over yet. You need to add nvic_irq_enable(USART1_IRQn, 0, 2); to configure the priority and enable this interrupt channel. At the beginning of the main function, nvic_priority_group_set(NVIC_PRIGROUP_PRE0_SUB4) is set;