如圖所示,GD32F350有兩個串口,分別是USART0和USART1,分別是哪幾個腳複用成串口,具體可以看手冊。
我的GPIO複用配置如下
串口初始化函數爲
- void gd_eval_com_init(uint32_t com)
- {
- uint32_t COM_ID;
- if(EVAL_COM1 == com){
- COM_ID = 0U;
- }else{
- COM_ID = 1U;
- }
- /* enable COM GPIO clock */
- rcu_periph_clock_enable(EVAL_COM_GPIO_CLK);
- /* enable USART clock */
- rcu_periph_clock_enable(COM_CLK[COM_ID]);
- /* connect port to USARTx_Tx */
- gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_TX_PIN[COM_ID]);
- /* connect port to USARTx_Rx */
- gpio_af_set(EVAL_COM_GPIO_PORT, EVAL_COM_AF, COM_RX_PIN[COM_ID]);
- /* configure USART Tx as alternate function push-pull */
- gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_TX_PIN[COM_ID]);
- gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_TX_PIN[COM_ID]);
- /* configure USART Rx as alternate function push-pull */
- gpio_mode_set(EVAL_COM_GPIO_PORT, GPIO_MODE_AF, GPIO_PUPD_PULLUP, COM_RX_PIN[COM_ID]);
- gpio_output_options_set(EVAL_COM_GPIO_PORT, GPIO_OTYPE_PP, GPIO_OSPEED_10MHZ, COM_RX_PIN[COM_ID]);
- /* USART configure */
- usart_deinit(com);
- usart_baudrate_set(com, 115200U);
- usart_receive_config(com, USART_RECEIVE_ENABLE);
- usart_transmit_config(com, USART_TRANSMIT_ENABLE);
- usart_enable(com);
- }
複製代碼
串口發送函數,串口0爲例
- void USART_SendBuffer(uint8_t *buf, uint16_t Size)
- {
- uint16_t len=0;
- for (len=0;len<Size;len++)
- {
- usart_data_transmit(EVAL_COM0, buf[len]);
- while(RESET == usart_flag_get(EVAL_COM0, USART_FLAG_TBE));
- }
- }
複製代碼
串口接收中斷函數
- void USART0_IRQHandler(void)
- {
- if(RESET!=usart_interrupt_flag_get(EVAL_COM0,USART_INT_FLAG_RBNE))
- {
- }
- }
複製代碼
當然別忘了開啓串口接收中斷
- // nvic_irq_enable(USART0_IRQn, 0, 0);
- usart_interrupt_enable(EVAL_COM0,USART_INT_RBNE);
複製代碼
大家可以嘗試一下,本人已使用該方法