如图所示,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
- {
- 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);
复制代码
大家可以尝试一下,本人已使用该方法
本文来自论坛,点击查看完整帖子内容。