static void USART_config() {
uint32_t usartx_tx_rcu = RCU_GPIOA;
uint32_t usartx_tx_port = GPIOA;
uint32_t usartx_tx_pin = GPIO_PIN_9;
uint32_t usartx_tx_af = GPIO_AF_7;
uint32_t usartx_rx_rcu = RCU_GPIOA;
uint32_t usartx_rx_port = GPIOA;
uint32_t usartx_rx_pin = GPIO_PIN_10;
uint32_t usartx_rx_af = GPIO_AF_7;
uint32_t usartx = USART0;
uint32_t usartx_rcu = RCU_USART0;
uint32_t usartx_irqn = USART0_IRQn;
uint32_t usartx_p_baudrate = 115200;
uint32_t usartx_p_parity = USART_PM_NONE;
uint32_t usartx_p_wl = USART_WL_8BIT;
uint32_t usartx_p_stop_bit = USART_STB_1BIT;
uint32_t usartx_p_data_first = USART_MSBF_LSB;
rcu_periph_clock_enable(usartx_tx_rcu);
gpio_mode_set(usartx_tx_port, GPIO_MODE_AF, GPIO_PUPD_NONE, usartx_tx_pin);
gpio_af_set(usartx_tx_port, usartx_tx_af, usartx_tx_pin);
gpio_output_options_set(usartx_tx_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, usartx_tx_pin);
rcu_periph_clock_enable(usartx_rx_rcu);
gpio_mode_set(usartx_rx_port, GPIO_MODE_AF, GPIO_PUPD_NONE, usartx_rx_pin);
gpio_af_set(usartx_rx_port, usartx_rx_af, usartx_rx_pin);
gpio_output_options_set(usartx_rx_port, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, usartx_rx_pin);
rcu_periph_clock_enable(RCU_USART0);
usart_deinit(usartx);
usart_baudrate_set(usartx, usartx_p_baudrate);
usart_parity_config(usartx, usartx_p_parity);
usart_word_length_set(usartx, usartx_p_wl);
usart_stop_bit_set(usartx, usartx_p_stop_bit);
usart_data_first_config(usartx, usartx_p_data_first);
usart_transmit_config(usartx, USART_TRANSMIT_ENABLE);
usart_receive_config(usartx, USART_RECEIVE_ENABLE);
nvic_irq_enable(usartx_irqn, 2, 2);
usart_interrupt_enable(usartx, USART_INT_RBNE);
usart_interrupt_enable(usartx, USART_INT_IDLE);
usart_enable(usartx);
}
#define USART_RECEIVE_LENGTH 1024
uint8_t g_recv_buff[USART_RECEIVE_LENGTH];
int g_recv_length = 0;
void USART0_IRQHandler(void) {
if ((usart_interrupt_flag_get(USART0, USART_INT_FLAG_RBNE)) == SET) {
usart_interrupt_flag_clear(USART0, USART_INT_FLAG_RBNE);
uint16_t value = usart_data_receive(USART0);
g_recv_buff[g_recv_length] = value;
g_recv_length++;
}
if (usart_interrupt_flag_get(USART0, USART_INT_FLAG_IDLE) == SET) {
usart_data_receive(USART0);
g_recv_buff[g_recv_length] = '\0';
g_recv_length = 0;
}
}