RISC-V MCU中文社区

【分享】 USART1、USART2、UART3的串口使用

发表于 GD32VF103 MCU 2021-04-05 15:01:56
0
5582
8

本人用蓝牙测试通过了三个串口模块,其中USART1需要重映射配置。USART0我暂时没有启用!

下图是USART1的使用,相关接口映射可以查找“用户指南”,代码在文末(重映射需要开启AF时钟——感谢胡灿老师!)

另外我重写了delay_1ms函数,改为了delay_1us,后续会用到。


//参考文件
// ~/Nuclei/SoC/gd32vf103/Board/gd32vf103v_eval/Include/gd32vf103v_eval.h
//USART2 UART3可以直接正常使用,USART1需要重映射
//由于用户手册上没找到USART0的接收端口,所以目前没有测试使用过
#include 
#include "nuclei_sdk_hal.h"
#include "gd32vf103_usart.h"
#include "gd32vf103_gpio.h"
#include "gd32vf103_exti.h"
#include "nuclei_sdk_soc.h"

#define COMn                             (4U)

#define GD32_COM0                        USART0
#define GD32_COM0_CLK                    RCU_USART0
#define GD32_COM0_TX_PIN                 GPIO_PIN_9
#define GD32_COM0_RX_PIN                 GPIO_PIN_10
#define GD32_COM0_GPIO_PORT              GPIOA
#define GD32_COM0_GPIO_CLK               RCU_GPIOA

#define GD32_COM1                        USART1
#define GD32_COM1_CLK                    RCU_USART1
#define GD32_COM1_TX_PIN                 GPIO_PIN_5
#define GD32_COM1_RX_PIN                 GPIO_PIN_6
#define GD32_COM1_GPIO_PORT              GPIOD
#define GD32_COM1_GPIO_CLK               RCU_GPIOD

#define GD32_COM2                        USART2
#define GD32_COM2_CLK                    RCU_USART2
#define GD32_COM2_TX_PIN                 GPIO_PIN_10
#define GD32_COM2_RX_PIN                 GPIO_PIN_11
#define GD32_COM2_GPIO_PORT              GPIOB
#define GD32_COM2_GPIO_CLK               RCU_GPIOB

#define GD32_COM3                        UART3
#define GD32_COM3_CLK                    RCU_UART3
#define GD32_COM3_TX_PIN                 GPIO_PIN_10
#define GD32_COM3_RX_PIN                 GPIO_PIN_11
#define GD32_COM3_GPIO_PORT              GPIOC
#define GD32_COM3_GPIO_CLK               RCU_GPIOC
/* private variables */
static rcu_periph_enum COM_CLK[COMn] = {GD32_COM0_CLK, GD32_COM1_CLK,GD32_COM2_CLK,GD32_COM3_CLK};
static uint32_t COM_TX_PIN[COMn] = {GD32_COM0_TX_PIN, GD32_COM1_TX_PIN,GD32_COM2_TX_PIN,GD32_COM3_TX_PIN};
static uint32_t COM_RX_PIN[COMn] = {GD32_COM0_RX_PIN, GD32_COM1_RX_PIN,GD32_COM2_RX_PIN,GD32_COM3_RX_PIN};
static uint32_t COM_GPIO_PORT[COMn] = {GD32_COM0_GPIO_PORT, GD32_COM1_GPIO_PORT,GD32_COM2_GPIO_PORT,GD32_COM3_GPIO_PORT};
static rcu_periph_enum COM_GPIO_CLK[COMn] = {GD32_COM0_GPIO_CLK, GD32_COM1_GPIO_CLK,GD32_COM2_GPIO_CLK,GD32_COM3_GPIO_CLK};
void gd_com_init(uint32_t com);
int USART_IRQHandler(uint32_t com);
void delay_1us(uint32_t count);
int main(void)
{
//select
   IRQn_Type IRQn=USART1_IRQn;
    uint32_t COM=GD32_COM1;
    //select end
//initial
    gd_com_init(COM);
    usart_interrupt_enable(COM,USART_INT_RBNE); //使能接收中断
    ECLIC_Register_IRQ(IRQn,ECLIC_NON_VECTOR_INTERRUPT,ECLIC_LEVEL_TRIGGER,1,0,USART_IRQHandler(COM)); // 配置中断函数
    __enable_irq();
    usart_enable(COM);
    //initial end
//transmit
   for (uint32_t i = 1; i <= 10; i ++) {
	usart_data_transmit(COM,i);
    while ( usart_flag_get(COM, USART_FLAG_TBE)== RESET){}; // 等待发送完成
    delay_1us(1000000);
    }
    //transmit end  
    usart_interrupt_disable(COM,USART_INT_RBNE);
    return 0;
}
void gd_com_init(uint32_t com)
{
    uint32_t com_id = 0U;
    if(GD32_COM0 == com){
        com_id = 0U;
    }else if(GD32_COM1 == com){
        com_id = 1U;
         /* enable AF clock */
        rcu_periph_clock_enable(RCU_AF);
    }else if(GD32_COM2 == com){
        com_id = 2U;
    }else if(GD32_COM3 == com){
        com_id = 3U;
    }
    /* enable GPIO clock */
    rcu_periph_clock_enable(COM_GPIO_CLK[com_id]);

    /* enable USART clock */
    rcu_periph_clock_enable(COM_CLK[com_id]);

    /* connect port to USARTx_Tx */
    gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, COM_TX_PIN[com_id]);

    /* connect port to USARTx_Rx */
    gpio_init(COM_GPIO_PORT[com_id], GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, COM_RX_PIN[com_id]);

    /* USART configure */
/////////////////////////////////////////////////////////////////
//reconfig
    if(GD32_COM1 == com){
        gpio_pin_remap_config(GPIO_USART1_REMAP, ENABLE);
    }
//reconfig end
//////////////////////////////////////////////////////////////////
    usart_deinit(com);
    usart_baudrate_set(com, 9600U);
    usart_word_length_set(com, USART_WL_8BIT);
    usart_parity_config(com, USART_PM_NONE);
    usart_stop_bit_set(com, USART_STB_1BIT);
    
    usart_hardware_flow_rts_config(com, USART_RTS_DISABLE);
    usart_hardware_flow_cts_config(com, USART_CTS_DISABLE);
    usart_receive_config(com, USART_RECEIVE_ENABLE);
    usart_transmit_config(com, USART_TRANSMIT_ENABLE);
    //usart_enable(com);
}
int USART_IRQHandler(uint32_t com){
	if(usart_interrupt_flag_get(com, USART_INT_FLAG_RBNE) != RESET){
		uint8_t serial_recv = usart_data_receive(com);
		printf("%d",serial_recv);
	}
    return 0;
}
void delay_1us(uint32_t count)
{
    uint64_t start_mtime, delta_mtime;
    //uint64_t delay_ticks = (SOC_TIMER_FREQ * (uint64_t)count) / 1000;
    uint64_t delay_ticks = (SOC_TIMER_FREQ * (uint64_t)count)/1000000;
    start_mtime = SysTimer_GetLoadValue();

    do {
        delta_mtime = SysTimer_GetLoadValue() - start_mtime;
    } while (delta_mtime < delay_ticks);
}


喜欢8
用户评论
宇宙无敌

宇宙无敌 实名认证

懒的都不写签名

积分
问答
粉丝
关注
  • RV-STAR 开发板
  • RISC-V处理器设计系列课程
  • 培养RISC-V大学土壤 共建RISC-V教育生态
RV-STAR 开发板