STM32 USART DMA发送 中断接收
扫描二维码
随时随地手机看文章
文件(usart.h):
#ifndef _USART__H
#define _USART__H
#define EMPTY 0xFFFF
extern vu32 uart2_transfer_complete;
typedef enum {
BSP_UART_STOPBITS_1=1,
BSP_UART_STOPBITS_2=2
}BSP_UART_StopBits;
typedef enum {
BSP_UART_PARITY_NO=0,
BSP_UART_PARITY_ODD=1,
BSP_UART_PARITY_EVEN=2
}BSP_UART_Parity;
typedef enum {
DATA_SIZE_BYTE=1,
DATA_SIZE_HALF_WORD=2
}BSP_UART_DataSize;
typedef void (*usart_hook_func_t)(u16 recv_data);
void bsp_ISR_uart1_setRecvHook(usart_hook_func_t func);
void bsp_ISR_uart2_setRecvHook(usart_hook_func_t func);
void bsp_ISR_uart3_setRecvHook(usart_hook_func_t func);
void bsp_ISR_uart4_setRecvHook(usart_hook_func_t func);
void bsp_ISR_uart5_setRecvHook(usart_hook_func_t func);
void bsp_uart1_config(u32 bps, BSP_UART_StopBits stopbits, BSP_UART_Parity parity, u8 databits, BSP_UART_DataSize ds);
void bsp_uart2_config(u32 bps, BSP_UART_StopBits stopbits, BSP_UART_Parity parity, u8 databits, BSP_UART_DataSize ds);
void bsp_uart3_config(u32 bps, BSP_UART_StopBits stopbits, BSP_UART_Parity parity, u8 databits, BSP_UART_DataSize ds);
void bsp_uart4_config(u32 bps, BSP_UART_StopBits stopbits, BSP_UART_Parity parity, u8 databits, BSP_UART_DataSize ds);
void bsp_uart5_config(u32 bps, BSP_UART_StopBits stopbits, BSP_UART_Parity parity, u8 databits, BSP_UART_DataSize ds);
void bsp_uart1_send_u8(u8* buf, u16 size);
void bsp_uart1_send_u16(u16* buf, u16 size);
void bsp_uart2_send_u8(u8* buf, u16 size);
void bsp_uart2_send_u16(u16* buf, u16 size);
void bsp_uart3_send_u8(u8* buf, u16 size);
void bsp_uart3_send_u16(u16* buf, u16 size);
void bsp_uart4_send_u8(u8* buf, u16 size);
void bsp_uart4_send_u16(u16* buf, u16 size);
void bsp_uart5_send_u8(u8* buf, u16 size);
void bsp_uart5_send_u16(u16* buf, u16 size);
/****************************************************************************************/
void bsp_ISR_uart1_putRecvData(u8 data); /* macro_ISR_uart_putRecvData */
void bsp_ISR_uart1_putSendData(u8 data); /* macro_ISR_uart_putSendData */
u16 bsp_uart1_getRecvData(void); /* macro_uart_getRecvData */
u16 bsp_uart1_getSendData(void); /* macro_uart_getSendData */
u8 bsp_uart1_recvBufIsFull(void); /* macro_uart_recvBufIsFull */
u8 bsp_uart1_sendBufIsFull(void); /* macro_uart_sendBufIsFull */
u8 bsp_uart1_sendBufIsEmpty(void); /* macro_uart_sendBufIsEmpty */
void bsp_uart1_cleanAllBuffer(void); /* macro_uart_cleanAllBuffer */
u16 bsp_uart1_recvBufRemain(void); /* macro_uart_recvBufRemain */
u16 bsp_uart1_sendbufRemain(void); /* macro_uart_sendbufRemain */
/******************************************************************************************/
void bsp_ISR_uart2_putRecvData(u8 data); /* macro_ISR_uart_putRecvData */
void bsp_ISR_uart2_putSendData(u8 data); /* macro_ISR_uart_putSendData */
u16 bsp_uart2_getRecvData(void); /* macro_uart_getRecvData */
u16 bsp_uart2_getSendData(void); /* macro_uart_getSendData */
u8 bsp_uart2_recvBufIsFull(void); /* macro_uart_recvBufIsFull */
u8 bsp_uart2_sendBufIsFull(void); /* macro_uart_sendBufIsFull */
u8 bsp_uart2_sendBufIsEmpty(void); /* macro_uart_sendBufIsEmpty */
void bsp_uart2_cleanAllBuffer(void); /* macro_uart_cleanAllBuffer */
u16 bsp_uart2_recvBufRemain(void); /* macro_uart_recvBufRemain */
u16 bsp_uart2_sendbufRemain(void); /* macro_uart_sendbufRemain */
/******************************************************************************************/
void bsp_ISR_uart3_putRecvData(u8 data); /* macro_ISR_uart_putRecvData */
void bsp_ISR_uart3_putSendData(u8 data); /* macro_ISR_uart_putSendData */
u16 bsp_uart3_getRecvData(void); /* macro_uart_getRecvData */
u16 bsp_uart3_getSendData(void); /* macro_uart_getSendData */
u8 bsp_uart3_recvBufIsFull(void); /* macro_uart_recvBufIsFull */
u8 bsp_uart3_sendBufIsFull(void); /* macro_uart_sendBufIsFull */
u8 bsp_uart3_sendBufIsEmpty(void); /* macro_uart_sendBufIsEmpty */
void bsp_uart3_cleanAllBuffer(void); /* macro_uart_cleanAllBuffer */
u16 bsp_uart3_recvBufRemain(void); /* macro_uart_recvBufRemain */
u16 bsp_uart3_sendbufRemain(void); /* macro_uart_sendbufRemain */
/******************************************************************************************/
void bsp_ISR_uart4_putRecvData(u8 data); /* macro_ISR_uart_putRecvData */
void bsp_ISR_uart4_putSendData(u8 data); /* macro_ISR_uart_putSendData */
u16 bsp_uart4_getRecvData(void); /* macro_uart_getRecvData */
u16 bsp_uart4_getSendData(void); /* macro_uart_getSendData */
u8 bsp_uart4_recvBufIsFull(void); /* macro_uart_recvBufIsFull */
u8 bsp_uart4_sendBufIsFull(void); /* macro_uart_sendBufIsFull */
u8 bsp_uart4_sendBufIsEmpty(void); /* macro_uart_sendBufIsEmpty */
void bsp_uart4_cleanAllBuffer(void); /* macro_uart_cleanAllBuffer */
u16 bsp_uart4_recvBufRemain(void); /* macro_uart_recvBufRemain */
u16 bsp_uart4_sendbufRemain(void); /* macro_uart_sendbufRemain */
/******************************************************************************************/
void bsp_ISR_uart5_putRecvData(u8 data); /* macro_ISR_uart_putRecvData */
void bsp_ISR_uart5_putSendData(u8 data); /* macro_ISR_uart_putSendData */
u16 bsp_uart5_getRecvData(void); /* macro_uart_getRecvData */
u16 bsp_uart5_getSendData(void); /* macro_uart_getSendData */
u8 bsp_uart5_recvBufIsFull(void); /* macro_uart_recvBufIsFull */
u8 bsp_uart5_sendBufIsFull(void); /* macro_uart_sendBufIsFull */
u8 bsp_uart5_sendBufIsEmpty(void); /* macro_uart_sendBufIsEmpty */
void bsp_uart5_cleanAllBuffer(void); /* macro_uart_cleanAllBuffer */
u16 bsp_uart5_recvBufRemain(void); /* macro_uart_recvBufRemain */
u16 bsp_uart5_sendbufRemain(void); /* macro_uart_sendbufRemain */
#endif
文件(usart.c):
/*
************************************************************************
** brief : 串口驱动
** note : 接收和发送BUFFER,需要根据实际情况修改大小
************************************************************************
*/
#include "usart.h"
#define USART1_SEND_BUF_SIZE 280
#define USART1_RECV_BUF_SIZE 256*2
#define USART2_SEND_BUF_SIZE 256*1
#define USART2_RECV_BUF_SIZE 256*2
#define USART3_SEND_BUF_SIZE 256*1
#define USART3_RECV_BUF_SIZE 256*1
#define USART4_SEND_BUF_SIZE 256*1
#define USART4_RECV_BUF_SIZE 256*2
#define USART5_SEND_BUF_SIZE 256*1
#define USART5_RECV_BUF_SIZE 256*1
/*
****************************************************************
** brief : 定义USART1-USART5的数据
** USART1_SEND_BUF - USART5_SEND_BUF
** USART1_RECV_BUF - USART5_RECV_BUF
** uart1SendHead - uart5SendHead
** uart1SendTail - uart5SendTail
** uart1RecvHead - uart5RecvHead
** uart5RecvTail - uart5RecvTail
****************************************************************
*/
#define UART_BUF_DEFINE(uartNo)
static u8 USART##uartNo##_SEND_BUF[USART##uartNo##_SEND_BUF_SIZE];
static vu16 uart##uartNo##SendHead=0;
static vu16 uart##uartNo##SendTail=0;
static vu8 UART##uartNo##_RECV_BUF[USART##uartNo##_RECV_BUF_SIZE];
static vu16 uart##uartNo##RecvHead=0;
static vu16 uart##uartNo##RecvTail=0;
UART_BUF_DEFINE(1);
UART_BUF_DEFINE(2);
UART_BUF_DEFINE(3);
UART_BUF_DEFINE(4);
UART_BUF_DEFINE(5);
/*
**************************************************************************************
** brief : 串口1配置初始化
** bsp : 波特率1200 2400 4800 9600 ... 115200,不能小于1200
** stopb : 停止位
** p : 奇偶校验位
** databits : 数据位7 or 8 or 9
** ds : DMA数据宽度
**************************************************************************************
*/
void bsp_uart1_config(u32 bps, BSP_UART_StopBits stopb, BSP_UART_Parity p, u8 databits, BSP_UART_DataSize ds)
{
u16 stopbits;
u16 parity;
if (stopb == BSP_UART_STOPBITS_1) stopbits = USART_StopBits_1;
else if(stopb == BSP_UART_STOPBITS_2)stopbits = USART_StopBits_2;
else stopbits = USART_StopBits_1;
if(p == BSP_UART_PARITY_NO) parity = USART_Parity_No;
else if(p == BSP_UART_PARITY_EVEN) parity = USART_Parity_Even;
else if(p == BSP_UART_PARITY_ODD) parity = USART_Parity_Odd;
else parity = USART_Parity_No;
u32 dataSize = DMA_PeripheralDataSize_Byte;
if(ds == DATA_SIZE_HALF_WORD) {
dataSize = DMA_PeripheralDataSize_HalfWord;
} else {
dataSize = DMA_Periphe