bsp_uart_fifo.h
/*
*********************************************************************************************************
*
* 模块名称 : 串口中断+FIFO驱动模块
* 文件名称 : bsp_uart_fifo.h
* 说 明 : 头文件
*
*********************************************************************************************************
*/
#ifndef _BSP_USART_FIFO_H_
#define _BSP_USART_FIFO_H_
/*
如果需要更改串口对应的管脚,请自行修改 bsp_uart_fifo.c文件中的 static void InitHardUart(void)函数
*/
/* 定义使能的串口, 0 表示不使能(不增加代码大小), 1表示使能 */
/*
串口分配:
【串口1】
PA9/USART1_TX --- 打印调试口
PA10/USART1_RX
*/
#define UART1_FIFO_EN 1
#define UART2_FIFO_EN 0
#define UART3_FIFO_EN 0
#define UART4_FIFO_EN 0
#define UART5_FIFO_EN 0
#define UART6_FIFO_EN 0
/* 定义端口号 */
typedef enum
{
COM1 = 0, /* USART1 PA9, PA10 或 PB6, PB7*/
COM2 = 1, /* USART2, PD5,PD6 或 PA2, PA3 */
COM3 = 2, /* USART3, PB10, PB11 */
COM4 = 3, /* UART4, PC10, PC11 */
COM5 = 4, /* UART5, PC12, PD2 */
COM6 = 5 /* USART6, PC6, PC7 */
}COM_PORT_E;
/* 定义串口波特率和FIFO缓冲区大小,分为发送缓冲区和接收缓冲区, 支持全双工 */
#if UART1_FIFO_EN == 1
#define UART1_BAUD 115200
#define UART1_TX_BUF_SIZE 1*1024
#define UART1_RX_BUF_SIZE 1*1024
#endif
#if UART2_FIFO_EN == 1
#define UART2_BAUD 9600
#define UART2_TX_BUF_SIZE 1
#define UART2_RX_BUF_SIZE 2*1024
#endif
#if UART3_FIFO_EN == 1
#define UART3_BAUD 9600
#define UART3_TX_BUF_SIZE 1*1024
#define UART3_RX_BUF_SIZE 1*1024
#endif
#if UART4_FIFO_EN == 1
#define UART4_BAUD 115200
#define UART4_TX_BUF_SIZE 1*1024
#define UART4_RX_BUF_SIZE 1*1024
#endif
#if UART5_FIFO_EN == 1
#define UART5_BAUD 115200
#define UART5_TX_BUF_SIZE 1*1024
#define UART5_RX_BUF_SIZE 1*1024
#endif
#if UART6_FIFO_EN == 1
#define UART6_BAUD 115200
#define UART6_TX_BUF_SIZE 1*1024
#define UART6_RX_BUF_SIZE 1*1024
#endif
/* 串口设备结构体 */
typedef struct
{
USART_TypeDef *uart; /* STM32内部串口设备指针 */
uint8_t *pTxBuf; /* 发送缓冲区 */
uint8_t *pRxBuf; /* 接收缓冲区 */
uint16_t usTxBufSize; /* 发送缓冲区大小 */
uint16_t usRxBufSize; /* 接收缓冲区大小 */
__IO uint16_t usTxWrite; /* 发送缓冲区写指针 */
__IO uint16_t usTxRead; /* 发送缓冲区读指针 */
__IO uint16_t usTxCount; /* 等待发送的数据个数 */
__IO uint16_t usRxWrite; /* 接收缓冲区写指针 */
__IO uint16_t usRxRead; /* 接收缓冲区读指针 */
__IO uint16_t usRxCount; /* 还未读取的新数据个数 */
void (*SendBefor)(void); /* 开始发送之前的回调函数指针(主要用于RS485切换到发送模式) */
void (*SendOver)(void); /* 发送完毕的回调函数指针(主要用于RS485将发送模式切换为接收模式) */
void (*ReciveNew)(uint8_t _byte); /* 串口收到数据的回调函数指针 */
}UART_T;
typedef enum
{
UART_OK = 0,
UART_TIMEOUT = 1,
UART_BUSY = 2,
UART_ERROR = 3
}UART_StatusTypeDef;
void bsp_InitUart(void);
void comSendBuf(COM_PORT_E _ucPort, uint8_t *_ucaBuf, uint16_t _usLen);
void comSendChar(COM_PORT_E _ucPort, uint8_t _ucByte);
uint8_t comGetChar(COM_PORT_E _ucPort, uint8_t *_pByte);
uint16_t comGetCharNum(COM_PORT_E _ucPort);
void comClearTxFifo(COM_PORT_E _ucPort);
void comClearRxFifo(COM_PORT_E _ucPort);
void comSetBaud(COM_PORT_E _ucPort, uint32_t _BaudRate);
void USART_SetBaudRate(USART_TypeDef* USARTx, uint32_t BaudRate);
UART_StatusTypeDef uart_receive_byte(uint8_t *byCh, uint32_t dwTimeout);
UART_StatusTypeDef uart_receive_data(uint8_t *byString, uint32_t dwLength, uint32_t dwTimeout);
#endif
bsp_uart_fifo.c
/*
*********************************************************************************************************
*
* 模块名称 : 串口中断+FIFO驱动模块
* 文件名称 : bsp_uart_fifo.c
* 版 本 : V1.0
* 说 明 : 采用串口中断+FIFO模式实现多个串口的同时访问
*
*********************************************************************************************************
*/
#include "bsp.h"
/* 串口1的GPIO */
#define UART1_TX_PORT GPIOA
#define UART1_TX_PIN GPIO_Pin_9
#define UART1_TX_CLK RCC_AHB1Periph_GPIOA
#define UART1_TX_SOURCE GPIO_PinSource9
#define UART1_RX_PORT GPIOA
#define UART1_RX_PIN GPIO_Pin_10
#define UART1_RX_CLK RCC_AHB1Periph_GPIOA
#define UART1_RX_SOURCE GPIO_PinSource10
/* 串口2的GPIO */
#define UART2_TX_PORT GPIOA
#define UART2_TX_PIN GPIO_Pin_2
#define UART2_TX_CLK RCC_AHB1Periph_GPIOA
#define UART2_TX_SOURCE GPIO_PinSource2
#define UART2_RX_PORT GPIOA
#define UART2_RX_PIN GPIO_Pin_3
#define UART2_RX_CLK RCC_AHB1Periph_GPIOA
#define UART2_RX_SOURCE GPIO_PinSource3
/* 串口3的GPIO */
#define UART3_TX_PORT GPIOB
#define UART3_TX_PIN GPIO_Pin_10
#define UART3_TX_CLK RCC_AHB1Periph_GPIOB
#define UART3_TX_SOURCE GPIO_PinSource10
#define UART3_RX_PORT GPIOB
#define UART3_RX_PIN GPIO_Pin_11
#define UART3_RX_CLK RCC_AHB1Periph_GPIOB
#define UART3_RX_SOURCE GPIO_PinSource11
/* 串口4的GPIO */
#define UART4_TX_PORT GPIOC
#define UART4_TX_PIN GPIO_Pin_10
#define UART4_TX_CLK RCC_AHB1Periph_GPIOC
#define UART4_TX_SOURCE GPIO_PinSource10
#define UART4_RX_PORT GPIOC
#define UART4_RX_PIN GPIO_Pin_11
#define UART4_RX_CLK RCC_AHB1Periph_GPIOC
#define UART4_RX_SOURCE GPIO_PinSource11
/* 串口5的GPIO */
#define UART5_TX_PORT GPIOC
#define UART5_TX_PIN GPIO_Pin_12
#define UART5_TX_CLK RCC_AHB1Periph_GPIOC
#define UART5_TX_SOURCE GPIO_PinSource12
#define UART5_RX_PORT GPIOD
#define UART5_RX_PIN GPIO_Pin_2
#define UART5_RX_CLK RCC_AHB1Periph_GPIOD
#define UART5_RX_SOURCE GPIO_PinSource2
/* 串口6的GPIO */
#define UART6_TX_PORT GPIOG
#define UART6_TX_PIN GPIO_Pin_14
#define UART6_TX_CLK RCC_AHB1Periph_GPIOG
#define UART6_TX_SOURCE GPIO_PinSource14
#define UART6_RX_PORT GPIOC
#define UART6_RX_PIN GPIO_Pin_7
#define UART6_RX_CLK RCC_AHB1Periph_GPIOC
#define UART6_RX_SOURCE GPIO_PinSource7
/* 定义每个串口结构体变量 */
#if UART1_FIFO_EN == 1
static UART_T g_tUart1;
static uint8_t g_TxBuf1[UART1_TX_BUF_SIZE]; /* 发送缓冲区 */
static uint8_t g_RxBuf1[UART1_RX_BUF_SIZE]; /* 接收缓冲区 */
#endif
#if UART2_FIFO_EN == 1
static UART_T g_tUart2;
static uint8_t g_TxBuf2[UART2_TX_BUF_SIZE]; /* 发送缓冲区 */
static uint8_t g_RxBuf2[UART2_RX_BUF_SIZE]; /* 接收缓冲区 */
#endif
#if UART3_FIFO_EN == 1
static UART_T g_tUart3;
static uint8_t g_TxBuf3[UART3_TX_BUF_SIZE]; /* 发送缓冲区 */
static uint8_t g_RxBuf3[UART3_RX_BUF_SIZE]; /* 接收缓冲区 */
#endif
#if UART4_FIFO_EN == 1
static UART_T g_tUart4;
static uint8_t g_TxBuf4[UART4_TX_BUF_SIZE]; /* 发送缓冲区 */
static uint8_t g_RxBuf4[UART4_RX_BUF_SIZE]; /* 接收缓冲区 */
#endif
#if UART5_FIFO_EN == 1
static UART_T g_tUart5;
static uint8_t g_TxBuf5[UART5_TX_BUF_SIZE]; /* 发送缓冲区 */
static uint8_t g_RxBuf5[UART5_RX_BUF_SIZE]; /* 接收缓冲区 */
#endif
#if UART6_FIFO_EN == 1
static UART_T g_tUart6;
static uint8_t g_TxBuf6[UART6_TX_BUF_SIZE]; /* 发送缓冲区 */
static uint8_t g_RxBuf6[UART6_RX_BUF_SIZE]; /* 接收缓冲区 */
#endif
static void UartVarInit(void);
static void InitHardUart(void);
static void UartSend(UART_T *_pUart, uint8_t *_ucaBuf, uint16_t _usLen);
static uint8_t UartGetChar(UART_T *_pUart, uint8_t *_pByte);
static void UartIRQ(UART_T *_pUart);
static void ConfigUartNVIC(void);
void RS485_InitTXE(void);
/*
*********************************************************************************************************
* 函 数 名: bsp_InitUart
* 功能说明: 初始化串口硬件,并对全局变量赋初值.
* 形 参: 无
* 返 回 值: 无
*********************************************************************************************************
*/
void bsp_InitUart(void)
{
UartVarInit(); /* 必须先初始化全局变量,再配置硬件 */
InitHardUart(); /* 配置串口的硬件参数(波特率等) */
// RS485_InitTXE(); /* 配置RS485芯片的发送使能硬件,配置为推挽输出 */
ConfigUartNVIC(); /* 配置串口中断 */
}
/*
*********************************************************************************************************
* 函 数 名: ComToUart
* 功能说明: 将COM端口号转换为UART指针
* 形 参: _ucPort: 端口号(COM1 - COM6)
* 返 回 值: uart指针
*********************************************************************************************************
*/
UART_T *ComToUart(COM_PORT_E _ucPort)
{
if (_ucPort == COM1)
{
#if UART1_FIFO_EN == 1
return &g_tUart1;
#else
return 0;
#endif
}
else if (_ucPort == COM2)
{
#if UART2_FIFO_EN == 1
return &g_tUart2;
#else
return 0;
#endif
}
else if (_ucPort == COM3)
{
#if UART3_FIFO_EN == 1
return &g_tUart3;
#else
return 0;
#endif
}
else if (_ucPort == COM4)
{
#if UART4_FIFO_EN == 1
return &g_tUart4;
#else
return 0;
#endif
}
else if (_ucPort == COM5)
{
#if UART5_FIFO_EN == 1
return &g_tUart5;
#else
return 0;
#endif
}
else if (_ucPort == COM6)
{
#if UART6_FIFO_EN == 1
return &g_tUart6;
#else
return 0;
#endif
}
else
{
/* 不做任何处理 */
return 0;
}
}
/*
*********************************************************************************************************
* 函 数 名: ComToUart
* 功能说明: 将COM端口号转换为 USART_TypeDef* USARTx
* 形 参: _ucPort: 端口号(COM1 - COM6)
* 返 回 值: USART_TypeDef*, USART1, USART2, USART3, UART4, UART5
*******