【极海APM32F4xx Tiny】学习笔记07-串口使用框架

该文描述了一个用于MCU的USART串口外设的中断驱动库,支持多个USART接口,最高通信速率达10.5Mbit/s。库中包含接收非空中断+空闲中断的机制,以及用于配置波特率、奇偶校验等的函数。同时,提供了发送和接收数据的函数,并且利用DMA功能提高效率。在main.c示例中,展示了库的初始化和数据传输应用。
摘要由CSDN通过智能技术生成

mcu 串口外设简介

该芯片内置多达 6 个通用同步/异步收发器,USART1/6 接口通信速率可达 10.5Mbit/s,其它USART/UART 的通信速率可达 5.25Mbit/s,所有 USART/UART 可配置波特率、奇偶校验位、停止位、数据位长度,都可以支持 DMA
函数库

本函数库主要是用来后期开发方便使用,将所有的变量封装在一个结构体内部
该函数库使用的是接收非空中断+空闲中断的方式收数据
头文件

    #ifndef __BSP_USART_IT_H
    #include "bsp_delay.h"
    #include "apm32f4xx.h"
    #include "apm32f4xx_gpio.h"
    #include "apm32f4xx_eint.h"
    #include "apm32f4xx_rcm.h"
    #include "apm32f4xx_syscfg.h"
    #include "apm32f4xx_misc.h"
    #include "apm32f4xx_usart.h"
    //缓冲区的大小
    #define RX_BUFF_SIZE 256
    //串口id定义,对外部的操作均使用该id操作
    typedef enum
    {
    UART_1,
    UART_2,
    UART_NUM
    }em_uart_id;
    //数据发送函数
    void bsp_uart_send_data(em_uart_id id,uint8_t *dat, uint32_t count);
    //初始化所有配置的串口
    void bsp_uart_it_init_all(void);
    //接收数据
    void bsp_uart_recv_data(em_uart_id id,uint8_t *dat, uint32_t *count);
    #endif

源文件


    /* Includes */
    #include "../usart_it/bsp_uart_it.h"
    #include <string.h>
    typedef struct
    {
    GPIO_T *tx_gpio_grp;
    GPIO_PIN_T tx_pin;
    uint32_t tx_rcc;
    GPIO_PIN_SOURCE_T tx_pin_source;
    GPIO_T *rx_gpio_grp;
    GPIO_PIN_T rx_pin;
    uint32_t rx_rcc;
    GPIO_PIN_SOURCE_T rx_pin_source;
    GPIO_AF_T gpio_af;
    USART_T *uart;
    IRQn_Type irq;
    uint32_t uart_rcc;
    uint8_t rx_buff[RX_BUFF_SIZE];
    uint16_t rx_count;
    uint16_t idle;
    } uart_it_t;
    static uart_it_t uarts_it[]=
    {
    { GPIOA,GPIO_PIN_9,RCM_AHB1_PERIPH_GPIOA,GPIO_PIN_SOURCE_9,
    GPIOA,GPIO_PIN_10,RCM_AHB1_PERIPH_GPIOA,GPIO_PIN_SOURCE_10,GPIO_AF_USART1,
    USART1,USART1_IRQn,RCM_APB2_PERIPH_USART1
    },
    { GPIOA,GPIO_PIN_2,RCM_AHB1_PERIPH_GPIOA,GPIO_PIN_SOURCE_2,
    GPIOA,GPIO_PIN_3,RCM_AHB1_PERIPH_GPIOA,GPIO_PIN_SOURCE_3,GPIO_AF_USART2,
    USART2,USART2_IRQn,RCM_APB1_PERIPH_USART2
    },
    };
    static void bsp_uart_it_config(uart_it_t *puart)
    {
    USART_Config_T usartConfigStruct;
    usartConfigStruct.baudRate = 115200;
    usartConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
    usartConfigStruct.mode = USART_MODE_TX_RX;
    usartConfigStruct.parity = USART_PARITY_NONE;
    usartConfigStruct.stopBits = USART_STOP_BIT_1;
    usartConfigStruct.wordLength = USART_WORD_LEN_8B;
    if (puart->uart == USART1 || puart->uart == USART6)
    {
    RCM_EnableAPB2PeriphClock(puart->uart_rcc);
    }
    else
    {
    RCM_EnableAPB1PeriphClock(puart->uart_rcc);
    }
    /** USART configuration */
    USART_Config(puart->uart, &usartConfigStruct);
    /** Enable USART1 RXBNE interrput */
    USART_EnableInterrupt(puart->uart, USART_INT_RXBNE);
    USART_EnableInterrupt(puart->uart,USART_INT_IDLE);
    USART_ClearStatusFlag(puart->uart, USART_FLAG_RXBNE);
    NVIC_EnableIRQRequest(puart->irq,1,0);
    /** Enable USART */
    USART_Enable(puart->uart);
    }
    static void bsp_uart_it_gpio_config(uart_it_t *puart)
    {
    GPIO_Config_T GPIO_configStruct;
    GPIO_ConfigStructInit(&GPIO_configStruct);
    /** 使能gpio的rcc*/
    RCM_EnableAHB1PeriphClock(puart->tx_rcc|puart->rx_rcc );
    /** Connect PXx to USARTx_Tx */
    GPIO_ConfigPinAF(puart->tx_gpio_grp, puart->tx_pin_source, puart->gpio_af);
    /** Connect PXx to USARTx_Rx */
    GPIO_ConfigPinAF(puart->rx_gpio_grp, puart->rx_pin_source, puart->gpio_af);
    /** Configure USART Tx as alternate function push-pull */
    GPIO_configStruct.mode = GPIO_MODE_AF;
    GPIO_configStruct.pin = puart->tx_pin;
    GPIO_configStruct.speed = GPIO_SPEED_50MHz;
    GPIO_Config(puart->tx_gpio_grp, &GPIO_configStruct);
    /** Configure USART Rx as input floating */
    GPIO_configStruct.mode = GPIO_MODE_AF;
    GPIO_configStruct.pin = puart->rx_pin;
    GPIO_Config(puart->rx_gpio_grp, &GPIO_configStruct);
    }
    void bsp_uart_it_init_all(void)
    {
    for(int i=0; i<UART_NUM; i++)
    {
    bsp_uart_it_gpio_config(uarts_it+i);
    bsp_uart_it_config(uarts_it+i);
    }
    }
    void bsp_uart_send_data(em_uart_id id,uint8_t *dat, uint32_t count)
    {
    uart_it_t *puart;
    if(UART_NUM>id)
    puart = uarts_it+id;
    while(puart && count--)
    {
    while(USART_ReadStatusFlag(puart->uart, USART_FLAG_TXBE) == RESET);
    USART_TxData(puart->uart, *dat++);
    }
    }
    #define min(num0,num1) (num0>num1?num1:num0)
    void bsp_uart_recv_data(em_uart_id id,uint8_t *dat, uint32_t *count)
    {
    uart_it_t *puart;
    if(UART_NUM>id)
    puart = uarts_it+id;
    if(puart && dat&&count)
    {
    *count = min(puart->rx_count,*count);
    memcpy(dat, puart->rx_buff,*count);
    puart->rx_count=0;
    }
    else{
    *count=0;
    }
    }
    static void bsp_uart_ira_handler(uart_it_t *puart)
    {
    if(USART_ReadIntFlag(puart->uart, USART_INT_RXBNE) == SET)
    {
    USART_ClearIntFlag(puart->uart, USART_INT_RXBNE);
    puart->rx_buff[puart->rx_count++%RX_BUFF_SIZE] = (uint8_t)USART_RxData(puart->uart);
    }
    else if (USART_ReadIntFlag(puart->uart, USART_INT_IDLE) == SET)
    {
    USART_ClearIntFlag(puart->uart, USART_INT_IDLE);
    puart->idle=1;
    USART_RxData(puart->uart);
    }
    }
    void USART1_IRQHandler(void)
    {
    bsp_uart_ira_handler(uarts_it+UART_1);
    }
    void USART2_IRQHandler(void)
    {
    bsp_uart_ira_handler(uarts_it+UART_2);
    }

## main.c
   uint8_t buff[1024];
 int main(void)
 {
 	bsp_uart_it_init_all();
 	bsp_uart_send_data(UART_1,"HELLO",6);
 	while(1)
 	{
 	bsp_uart_recv_data(UART_1,buff,1024);
 	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Car12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值