国民技术N32G430C8开发笔记二-UART驱动开发

参考demo

E:\tfs\data\smartsafe\N32G430\Nations.N32G430_Library.1.0.0\projects\n32g430_EVAL\examples\USART\Interrupt开发uart1和uart3驱动。Uart1用于通信,uart3用于调试。

参考用户手册5.2.5复用功能。Uart1_tx引脚使用PA9,重映射使用AF5,Uart1_rx引脚使用PA10,重映射使用AF5,Uart3_tx引脚使用PB10,重映射使用AF10,Uart3_Rx引脚使用PB11,重映射使用AF10。

串口使用ringbuffer,中断方式接收和发送。

1、配置时钟

包括GPIO时钟和uart时钟。

c 复制代码
void RCC_Configuration(void)
{
    /* Enable GPIO clock */
	RCC_AHB_Peripheral_Clock_Enable(USARTy_GPIO_CLK | USARTz_GPIO_CLK);
    /* Enable USARTy and USARTz Clock */
    USARTy_APBxClkCmd(USARTy_CLK);
    USARTz_APBxClkCmd(USARTz_CLK);
}

2、配置IO

配置IO的复用功能为UART。

c 复制代码
void GPIO_Configuration(void)
{
    GPIO_InitType GPIO_InitStructure;

    /* Initialize GPIO_InitStructure */
    GPIO_Structure_Initialize(&GPIO_InitStructure);
   
    /* Configure USARTy Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTy_TxPin;    
    GPIO_InitStructure.GPIO_Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStructure.GPIO_Alternate = USARTy_Tx_GPIO_AF;
    GPIO_Peripheral_Initialize(USARTy_GPIO, &GPIO_InitStructure);

    /* Configure USARTz Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTz_TxPin;
    GPIO_InitStructure.GPIO_Alternate = USARTz_Tx_GPIO_AF;
    GPIO_Peripheral_Initialize(USARTz_GPIO, &GPIO_InitStructure); 

    /* Configure USARTx Rx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTy_RxPin;
    GPIO_InitStructure.GPIO_Alternate = USARTy_Rx_GPIO_AF;
    GPIO_Peripheral_Initialize(USARTy_GPIO, &GPIO_InitStructure);    

    /* Configure USARTz Rx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USARTz_RxPin;
    GPIO_InitStructure.GPIO_Alternate = USARTz_Rx_GPIO_AF;
    GPIO_Peripheral_Initialize(USARTz_GPIO, &GPIO_InitStructure);        
}

3、配置UART

包括初始化uart,使能uart中断,使能uart外设等。

c 复制代码
    /* USARTy and USARTz configuration ------------------------------------------------------*/
    USART_InitStructure.BaudRate            = 115200;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USARTy and USARTz */
    USART_Initializes(USARTy, &USART_InitStructure);
    USART_Initializes(USARTz, &USART_InitStructure);

    /* Enable USARTy Receive and Transmit interrupts */
    USART_Interrput_Enable(USARTy, USART_INT_RXDNE);
    USART_Interrput_Enable(USARTy, USART_INT_TXDE);

    /* Enable USARTz Receive and Transmit interrupts */
    USART_Interrput_Enable(USARTz, USART_INT_RXDNE);
    USART_Interrput_Enable(USARTz, USART_INT_TXDE);

    /* Enable the USARTy and USARTz */
    USART_Enable(USARTy);
    USART_Enable(USARTz);

4、uart中断响应函数

c 复制代码
void COM_UART_IRQHandler(void)
{
    uint8_t data;
    if (USART_Interrupt_Status_Get(COM_UART, USART_INT_RXDNE) != RESET)
    {
        data = USART_Data_Receive(COM_UART);
        ringBuffer_write(pcomUartRxRingBuffer,data);
    }

    if (USART_Interrupt_Status_Get(COM_UART, USART_INT_TXDE) != RESET)
    {
        if (ringBuffer_is_empty(p_comUartTxRingBuffer))
        {
            /*没有发送数据时要关闭串口发送中断*/
            /* Disable the COM_UART Transmit interrupt */
            USART_Interrput_Disable(COM_UART, USART_INT_TXDE);
        }
        else
        {
            ringBuffer_read(p_comUartTxRingBuffer,&data);
            USART_Data_Send(COM_UART, data);
        }    
    }
}
相关推荐
晶振厂家-晶发电子15 小时前
晶振在5G时代的角色:高精度时钟的核心支撑
单片机·嵌入式硬件·5g·晶振·电子元器件·晶振知识
F1372980155715 小时前
WD5030A 芯片,12V降5V,输出电流12A,电路设计
stm32·单片机·嵌入式硬件·汽车·51单片机
小莞尔16 小时前
【51单片机】【protues仿真】基于51单片机的篮球计时计分器系统
c语言·stm32·单片机·嵌入式硬件·51单片机
三佛科技-1873661339716 小时前
分享机械键盘MCU解决方案
单片机·嵌入式硬件·计算机外设
李永奉16 小时前
51单片机-使用IIC通信协议实现EEPROM模块教程
单片机·嵌入式硬件·51单片机
小莞尔16 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
bing_feilong16 小时前
STM32精准控制水流
单片机·嵌入式硬件
Hello_Embed1 天前
STM32HAL 快速入门(二十):UART 中断改进 —— 环形缓冲区解决数据丢失
笔记·stm32·单片机·学习·嵌入式软件
矢志不移7921 天前
裸机开发 时钟配置,EPIT
单片机·嵌入式硬件
清风6666661 天前
基于STM32的APP遥控视频水泵小车设计
stm32·单片机·mongodb·毕业设计·音视频·课程设计