STM32|STM32 USART DMA发送 中断接收

文件(usart.h):

#ifndef _USART__H #define _USART__H#define EMPTY 0xFFFFextern 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 */u16bsp_uart1_getRecvData(void); /* macro_uart_getRecvData */ u16bsp_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 */u16bsp_uart2_getRecvData(void); /* macro_uart_getRecvData */ u16bsp_uart2_getSendData(void); /* macro_uart_getSendData */u8 bsp_uart2_recvBufIsFull(void); /* macro_uart_recvBufIsFull */ u8 bsp_uart2_sendBufIsFull(void); /* macro_uart_sendBufIsFull */u8bsp_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 */u16bsp_uart3_getRecvData(void); /* macro_uart_getRecvData */ u16bsp_uart3_getSendData(void); /* macro_uart_getSendData */u8 bsp_uart3_recvBufIsFull(void); /* macro_uart_recvBufIsFull */ u8 bsp_uart3_sendBufIsFull(void); /* macro_uart_sendBufIsFull */u8bsp_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 */u16bsp_uart4_getRecvData(void); /* macro_uart_getRecvData */ u16bsp_uart4_getSendData(void); /* macro_uart_getSendData */u8 bsp_uart4_recvBufIsFull(void); /* macro_uart_recvBufIsFull */ u8 bsp_uart4_sendBufIsFull(void); /* macro_uart_sendBufIsFull */u8bsp_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 */u16bsp_uart5_getRecvData(void); /* macro_uart_getRecvData */ u16bsp_uart5_getSendData(void); /* macro_uart_getSendData */u8 bsp_uart5_recvBufIsFull(void); /* macro_uart_recvBufIsFull */ u8 bsp_uart5_sendBufIsFull(void); /* macro_uart_sendBufIsFull */u8bsp_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_PeripheralDataSize_Byte; }do { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } while(0); do { USART_InitTypeDef USART_InitStructure; u16 USART_WordLength = USART_WordLength_8b; USART_DeInit(USART1); /* 复位USART1 */bsp_uart1_cleanAllBuffer(); /* 提示: STM32 将奇偶位也算作数据位*/ if(parity!=USART_Parity_No && 7 != databits) { USART_WordLength = USART_WordLength_9b; }if(9==databits) { USART_WordLength = USART_WordLength_9b; }USART_InitStructure.USART_BaudRate = bps; USART_InitStructure.USART_WordLength = USART_WordLength; USART_InitStructure.USART_StopBits = stopbits; USART_InitStructure.USART_Parity = parity; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART1, &USART_InitStructure); /* 接收缓冲区非空中断使能*/ USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); USART_ITConfig(USART1,USART_IT_TC,DISABLE); USART_ITConfig(USART1,USART_IT_TXE,DISABLE); USART_Cmd(USART1, ENABLE); /* **CPU的小缺陷:串口配置好,如果直接Send,则第1个字节发 **送不出去如下语句解决第1个字节无法正确发送出去的问题 */ USART_ClearFlag(USART1, USART_FLAG_TC); } while(0); do { DMA_InitTypeDef DMA_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA1_Channel4); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART1->DR); /* 外设地址*/ DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART1_SEND_BUF; /* 存储器地址*/ DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; /* 从存储器读*/ if(DMA_PeripheralDataSize_HalfWord == dataSize) {/* 传输数据量*/ DMA_InitStructure.DMA_BufferSize = USART1_SEND_BUF_SIZE/2; } else { DMA_InitStructure.DMA_BufferSize = USART1_SEND_BUF_SIZE; } DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; /* 外设地址非增量模式*/ DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; /* 存储器增量模式*/ DMA_InitStructure.DMA_PeripheralDataSize = dataSize; /* 外设数据宽度*/ if(DMA_PeripheralDataSize_HalfWord == dataSize)/* 存储器数据宽度*/ { DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; /* 16bit */ } else { DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; /* 8bit */ } DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; /* 普通模式*/ DMA_InitStructure.DMA_Priority = DMA_Priority_High; /* 高优先级*/ DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; /* 非存储器到存储器模式*/ DMA_Init(DMA1_Channel4, &DMA_InitStructure); /* DMA 初始化*/ DMA_ITConfig(DMA1_Channel4, DMA_IT_TC, ENABLE); /* 使能发送完成中断*/USART_DMACmd(USART1, USART_DMAReq_Tx, ENABLE); /* 使能发送时的DMA 模式*/ DMA_Cmd(DMA1_Channel4, DISABLE); } while(0); do { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } while(0); }/* ************************************************************************************** **brief: 串口2配置初始化 **bsp: 波特率1200 2400 4800 9600 ... 115200,不能小于1200 **stopb: 停止位 **p: 奇偶校验位 **databits : 数据位7 or 8 or 9 **ds: DMA数据宽度 ************************************************************************************** */void bsp_uart2_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_PeripheralDataSize_Byte; }do { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); } while(0); do { USART_InitTypeDef USART_InitStructure; u16 USART_WordLength = USART_WordLength_8b; USART_DeInit(USART2); bsp_uart2_cleanAllBuffer(); if(parity!=USART_Parity_No && 7 != databits) { USART_WordLength = USART_WordLength_9b; } if(9==databits) { USART_WordLength = USART_WordLength_9b; }USART_InitStructure.USART_BaudRate = bps; USART_InitStructure.USART_WordLength = USART_WordLength; USART_InitStructure.USART_StopBits = stopbits; USART_InitStructure.USART_Parity = parity; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); USART_ITConfig(USART2,USART_IT_TC,DISABLE); USART_ITConfig(USART2,USART_IT_TXE,DISABLE); USART_Cmd(USART2, ENABLE); USART_ClearFlag(USART2, USART_FLAG_TC); } while(0); do { DMA_InitTypeDef DMA_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA1_Channel7); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART2->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART2_SEND_BUF; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; if(DMA_PeripheralDataSize_HalfWord == dataSize) { DMA_InitStructure.DMA_BufferSize = USART2_SEND_BUF_SIZE/2; } else { DMA_InitStructure.DMA_BufferSize = USART2_SEND_BUF_SIZE; } DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = dataSize; if(DMA_PeripheralDataSize_HalfWord == dataSize) { DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; } else { DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; } DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel7, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel7, DMA_IT_TC, ENABLE); USART_DMACmd(USART2, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA1_Channel7, DISABLE); } while(0); do { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel7_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } while(0); }/* ************************************************************************************** **brief: 串口3配置初始化 **bsp: 波特率1200 2400 4800 9600 ... 115200,不能小于1200 **stopb: 停止位 **p: 奇偶校验位 **databits : 数据位7 or 8 or 9 **ds: DMA数据宽度 ************************************************************************************** */void bsp_uart3_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_PeripheralDataSize_Byte; }do { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); } while(0); do { USART_InitTypeDef USART_InitStructure; u16 USART_WordLength = USART_WordLength_8b; USART_DeInit(USART3); bsp_uart3_cleanAllBuffer(); if(parity!=USART_Parity_No && 7 != databits) { USART_WordLength = USART_WordLength_9b; }if(9==databits) { USART_WordLength = USART_WordLength_9b; }USART_InitStructure.USART_BaudRate = bps; USART_InitStructure.USART_WordLength = USART_WordLength; USART_InitStructure.USART_StopBits = stopbits; USART_InitStructure.USART_Parity = parity; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx |USART_Mode_Tx; USART_Init(USART3, &USART_InitStructure); USART_ITConfig(USART3,USART_IT_RXNE, ENABLE); USART_ITConfig(USART3,USART_IT_TC,DISABLE); USART_ITConfig(USART3,USART_IT_TXE,DISABLE); USART_Cmd(USART3, ENABLE); USART_ClearFlag(USART3, USART_FLAG_TC); } while(0); do { DMA_InitTypeDef DMA_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); DMA_DeInit(DMA1_Channel2); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&USART3->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART3_SEND_BUF; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; if(DMA_PeripheralDataSize_HalfWord == dataSize) { DMA_InitStructure.DMA_BufferSize = USART3_SEND_BUF_SIZE/2; } else { DMA_InitStructure.DMA_BufferSize = USART3_SEND_BUF_SIZE; } DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = dataSize; if(DMA_PeripheralDataSize_HalfWord == dataSize) { DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; } else { DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; } DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel2, &DMA_InitStructure); DMA_ITConfig(DMA1_Channel2, DMA_IT_TC, ENABLE); USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA1_Channel2, DISABLE); } while(0); do { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = DMA1_Channel2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } while(0); }/* ************************************************************************************** **brief: 串口4配置初始化 **bsp: 波特率1200 2400 4800 9600 ... 115200,不能小于1200 **stopb: 停止位 **p: 奇偶校验位 **databits : 数据位7 or 8 or 9 **ds: DMA数据宽度 ************************************************************************************** */void bsp_uart4_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_PeripheralDataSize_Byte; }do { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC, &GPIO_InitStructure); } while(0); do { USART_InitTypeDef USART_InitStructure; u16 USART_WordLength = USART_WordLength_8b; USART_DeInit(UART4); bsp_uart4_cleanAllBuffer(); if(parity!=USART_Parity_No && 7 != databits) { USART_WordLength = USART_WordLength_9b; }if(9==databits) { USART_WordLength = USART_WordLength_9b; }USART_InitStructure.USART_BaudRate = bps; USART_InitStructure.USART_WordLength = USART_WordLength; USART_InitStructure.USART_StopBits = stopbits; USART_InitStructure.USART_Parity = parity; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(UART4, &USART_InitStructure); USART_ITConfig(UART4,USART_IT_RXNE,ENABLE); USART_ITConfig(UART4,USART_IT_TC,DISABLE); USART_ITConfig(UART4,USART_IT_TXE,DISABLE); USART_Cmd(UART4, ENABLE); USART_ClearFlag(UART4, USART_FLAG_TC); } while(0); do { DMA_InitTypeDef DMA_InitStructure; RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); DMA_DeInit(DMA2_Channel5); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)(&UART4->DR); DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART4_SEND_BUF; if(DMA_PeripheralDataSize_HalfWord == dataSize) { DMA_InitStructure.DMA_BufferSize = USART4_SEND_BUF_SIZE/2; } else { DMA_InitStructure.DMA_BufferSize = USART4_SEND_BUF_SIZE; } DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = dataSize; if(DMA_PeripheralDataSize_HalfWord == dataSize) { DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; } else { DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; } DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA2_Channel5, &DMA_InitStructure); DMA_ITConfig(DMA2_Channel5, DMA_IT_TC, ENABLE); USART_DMACmd(UART4, USART_DMAReq_Tx, ENABLE); DMA_Cmd(DMA2_Channel5, DISABLE); } while(0); do { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = DMA2_Channel4_5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } while(0); }/* ************************************************************************************** **brief: 串口5配置初始化 **bsp: 波特率1200 2400 4800 9600 ... 115200,不能小于1200 **stopb: 停止位 **p: 奇偶校验位 **databits : 数据位7 or 8 or 9 **ds: DMA数据宽度 **note: USART5没有使用DMA,故ds参数无意义 ************************************************************************************** */void bsp_uart5_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; do { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); } while(0); do { USART_InitTypeDef USART_InitStructure; u16 USART_WordLength = USART_WordLength_8b; USART_DeInit(UART5); bsp_uart5_cleanAllBuffer(); if(parity!=USART_Parity_No && 7 != databits) { USART_WordLength = USART_WordLength_9b; }if(9==databits) { USART_WordLength = USART_WordLength_9b; }USART_InitStructure.USART_BaudRate = bps; USART_InitStructure.USART_WordLength = USART_WordLength; USART_InitStructure.USART_StopBits = stopbits; USART_InitStructure.USART_Parity = parity; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx|USART_Mode_Tx; USART_Init(UART5, &USART_InitStructure); /* 使能发送完成中断*/ USART_ITConfig(UART5,USART_IT_RXNE,ENABLE); USART_ITConfig(UART5,USART_IT_TC, ENABLE); USART_ITConfig(UART5,USART_IT_TXE, DISABLE); USART_Cmd(UART5, ENABLE); USART_ClearFlag(UART5, USART_FLAG_TC); } while(0); do { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } while(0); }static vu32 uart1_transfer_complete = 0; /* USART1发送完成*/ vu32 uart2_transfer_complete = 0; /* USART2发送完成*/ static vu32 uart3_transfer_complete = 0; /* USART3发送完成*/ static vu32 uart4_transfer_complete = 0; /* USART4发送完成*/ static vu32 uart5_transfer_complete = 0; /* USART5发送完成*//* ****************************************************************************** **Ret :1= 传输完成 **other = 启动传输的时刻 **note : systemTickMs() - Ret(other) = 传输时间 ****************************************************************************** */ u32 bsp_uart1_transferComplete(void) { return uart1_transfer_complete; } u32 bsp_uart2_transferComplete(void) { return uart2_transfer_complete; } u32 bsp_uart3_transferComplete(void) { return uart3_transfer_complete; } u32 bsp_uart4_transferComplete(void) { return uart4_transfer_complete; } u32 bsp_uart5_transferComplete(void) { return uart5_transfer_complete; } /* *************************************************************** **brief : usart1 发送数据 **buf: 待发送数据缓冲指针 **size: 待发送数据长度 *************************************************************** */ void bsp_uart1_send_u8(u8* buf, u16 size) { if(size > USART1_SEND_BUF_SIZE) { return; } memcpy(USART1_SEND_BUF, buf, size); DMA_Cmd(DMA1_Channel4, DISABLE); /* 关闭DMA传输*/ DMA1_Channel4->CNDTR=size; /* 改变数据传输量*/ DMA_Cmd(DMA1_Channel4, ENABLE); /* 开启DMA传输*/ uart1_transfer_complete = systemMs(); } /* *************************************************************** **brief : usart1 发送数据 **buf: 待发送数据缓冲指针 **size: 待发送数据长度 *************************************************************** */ void bsp_uart1_send_u16(u16* buf, u16 size) { if(size*2 > USART1_SEND_BUF_SIZE) { return; }memcpy(USART1_SEND_BUF, buf, size*2); DMA_Cmd(DMA1_Channel4, DISABLE); DMA1_Channel4->CNDTR=size; DMA_Cmd(DMA1_Channel4, ENABLE); uart1_transfer_complete = systemMs(); } /* *************************************************************** **brief : usart2 发送数据 **buf: 待发送数据缓冲指针 **size: 待发送数据长度 *************************************************************** */void bsp_uart2_send_u8(u8* buf, u16 size) { if(size > USART2_SEND_BUF_SIZE) { return; } memcpy(USART2_SEND_BUF, buf, size); DMA_Cmd(DMA1_Channel7, DISABLE); DMA1_Channel7->CNDTR=size; DMA_Cmd(DMA1_Channel7, ENABLE); uart2_transfer_complete = systemMs(); } /* *************************************************************** **brief : usart2 发送数据 **buf: 待发送数据缓冲指针 **size: 待发送数据长度 *************************************************************** */void bsp_uart2_send_u16(u16* buf, u16 size) { if(size*2 > USART2_SEND_BUF_SIZE) { return; }memcpy(USART2_SEND_BUF, buf, size*2); DMA_Cmd(DMA1_Channel7, DISABLE); DMA1_Channel7->CNDTR=size; DMA_Cmd(DMA1_Channel7, ENABLE); uart2_transfer_complete = systemMs(); } /* *************************************************************** **brief : usart3 发送数据 **buf: 待发送数据缓冲指针 **size: 待发送数据长度 *************************************************************** */void bsp_uart3_send_u8(u8* buf, u16 size) { memcpy(USART3_SEND_BUF, buf, size); DMA_Cmd(DMA1_Channel2, DISABLE); DMA1_Channel2->CNDTR=size; DMA_Cmd(DMA1_Channel2, ENABLE); uart3_transfer_complete = systemMs(); } /* *************************************************************** **brief : usart3 发送数据 **buf: 待发送数据缓冲指针 **size: 待发送数据长度 *************************************************************** */void bsp_uart3_send_u16(u16* buf, u16 size) { if(size*2 > USART3_SEND_BUF_SIZE) { return; } memcpy(USART3_SEND_BUF, buf, size*2); DMA_Cmd(DMA1_Channel2, DISABLE); DMA1_Channel2->CNDTR=size; DMA_Cmd(DMA1_Channel2, ENABLE); uart3_transfer_complete = systemMs(); } /* *************************************************************** **brief : usart4发送数据 **buf: 待发送数据缓冲指针 **size: 待发送数据长度 *************************************************************** */void bsp_uart4_send_u8(u8* buf, u16 size) { memcpy(USART4_SEND_BUF, buf, size); DMA_Cmd(DMA2_Channel5, DISABLE); DMA2_Channel5->CNDTR=size; DMA_Cmd(DMA2_Channel5, ENABLE); uart4_transfer_complete = systemMs(); } /* *************************************************************** **brief : usart4 发送数据 **buf: 待发送数据缓冲指针 **size: 待发送数据长度 *************************************************************** */ void bsp_uart4_send_u16(u16* buf, u16 size) { if(size*2 > USART4_SEND_BUF_SIZE) { return; } memcpy(USART4_SEND_BUF, buf, size*2); DMA_Cmd(DMA2_Channel5, DISABLE); DMA2_Channel5->CNDTR=size; DMA_Cmd(DMA2_Channel5, ENABLE); uart4_transfer_complete = systemMs(); }static vu16 usart5_send_size = 0; static vu16 usart5_send_index = 0; void bsp_uart5_send_u8(u8* buf, u16 size) { if(size > USART5_SEND_BUF_SIZE) { return; }memcpy(USART5_SEND_BUF, buf, size); /* 已是能发送完成终端*/ usart5_send_size = size; usart5_send_index = 0; USART_SendData(UART5, (u16)USART5_SEND_BUF[usart5_send_index]); ++usart5_send_index; uart5_transfer_complete = systemMs(); }static usart_hook_func_t _g_uart1_recv_hook = NULL; static usart_hook_func_t _g_uart2_recv_hook = NULL; static usart_hook_func_t _g_uart3_recv_hook = NULL; static usart_hook_func_t _g_uart4_recv_hook = NULL; static usart_hook_func_t _g_uart5_recv_hook = NULL; /* 串口接收中断钩子函数*/ void bsp_ISR_uart1_setRecvHook(usart_hook_func_t func) { _g_uart1_recv_hook = func; } void bsp_ISR_uart2_setRecvHook(usart_hook_func_t func) { _g_uart2_recv_hook = func; } void bsp_ISR_uart3_setRecvHook(usart_hook_func_t func) { _g_uart3_recv_hook = func; } void bsp_ISR_uart4_setRecvHook(usart_hook_func_t func) { _g_uart4_recv_hook = func; } void bsp_ISR_uart5_setRecvHook(usart_hook_func_t func) { _g_uart5_recv_hook = func; } /* **************************************************************** **breif : usart1 中断服务函数 **note : 接收缓冲区非空中断使能 **************************************************************** */ void USART1_IRQHandler(void) { if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) {/* 读数据寄存器非空*/ u16 recv = USART_ReceiveData(USART1); USART_ClearITPendingBit(USART1,USART_IT_RXNE); bsp_ISR_uart1_putRecvData(recv&0xff); if( NULL != _g_uart1_recv_hook) { (*_g_uart1_recv_hook)(recv); } }if(USART_GetITStatus(USART1, USART_IT_TXE) == SET) {/* 发送数据寄存器空*/ USART_ClearITPendingBit(USART1, USART_IT_TXE); }/* 提示: USART_IT_TC 需要在 USART_IT_ORE 之前处理*/ if(USART_GetITStatus(USART1, USART_IT_TC)==SET) {/* 发送完成*/ USART_ClearITPendingBit(USART1, USART_IT_TC); }/* ** 因 某些原因没处理过来,引起了串口的数据还没被读出的 ** 时候就又收到了新的数据CPU就会出现不停进入中断的问 ** 题.必须读 SR,再读DR,以解决溢出错误 ** 注意:不能使用USART_GetITStatus()来读取USART_IT_ORE */ if(USART_GetFlagStatus(USART1, USART_IT_ORE) == SET) {/* 过载错误*/ USART_ClearFlag(USART1, USART_IT_ORE); //读 SR USART_ReceiveData(USART1); //读 DR } } /* **************************************************************** **breif : usart2 中断服务函数 **note : 接收缓冲区非空中断使能 **************************************************************** */void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { u16 recv = USART_ReceiveData(USART2); USART_ClearITPendingBit(USART2, USART_IT_RXNE); bsp_ISR_uart2_putRecvData(recv&0xff); if( NULL != _g_uart2_recv_hook ) { (*_g_uart2_recv_hook)(recv); } }if(USART_GetITStatus(USART2, USART_IT_TXE) == SET) { USART_ClearITPendingBit(USART2, USART_IT_TXE); }if(USART_GetITStatus(USART2, USART_IT_TC)==SET) { USART_ClearITPendingBit(USART2, USART_IT_TC); }if(USART_GetFlagStatus(USART2, USART_IT_ORE) == SET) { USART_ClearFlag(USART2, USART_IT_ORE); USART_ReceiveData(USART2); } } /* **************************************************************** **breif : usart3 中断服务函数 **note : 接收缓冲区非空中断使能 **************************************************************** */void USART3_IRQHandler(void) { if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) { u16 recv = USART_ReceiveData(USART3); USART_ClearITPendingBit(USART3, USART_IT_RXNE); bsp_ISR_uart3_putRecvData(recv&0xff); if( NULL != _g_uart3_recv_hook) { (*_g_uart3_recv_hook)(recv); } }if(USART_GetITStatus(USART3, USART_IT_TXE) == SET){ USART_ClearITPendingBit(USART3, USART_IT_TXE); }if(USART_GetITStatus(USART3, USART_IT_TC)==SET) { USART_ClearITPendingBit(USART3, USART_IT_TC); }if(USART_GetFlagStatus(USART3, USART_IT_ORE) == SET) { USART_ClearFlag(USART3, USART_IT_ORE); USART_ReceiveData(USART3); } } /* **************************************************************** **breif : usart4 中断服务函数 **note : 接收缓冲区非空中断使能 **************************************************************** */void UART4_IRQHandler(void) { if( USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) { u16 recv = USART_ReceiveData(UART4); USART_ClearITPendingBit(UART4, USART_IT_RXNE); bsp_ISR_uart4_putRecvData(recv&0xff); if( NULL != _g_uart4_recv_hook) { (*_g_uart4_recv_hook)(recv); } }if(USART_GetITStatus(UART4, USART_IT_TXE) == SET) { USART_ClearITPendingBit(UART4, USART_IT_TXE); }if(USART_GetITStatus(UART4, USART_IT_TC)==SET) { USART_ClearITPendingBit(UART4, USART_IT_TC); }if(USART_GetFlagStatus(UART4, USART_IT_ORE) == SET) { USART_ClearFlag(UART4, USART_IT_ORE); USART_ReceiveData(UART4); } } /* **************************************************************** **breif : usart1 中断服务函数 **note : 接收缓冲区非空中断使能 **发送完成中断使能 **************************************************************** */void UART5_IRQHandler(void) { if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET) { u16 recv = USART_ReceiveData(UART5); USART_ClearITPendingBit(UART5, USART_IT_RXNE); bsp_ISR_uart5_putRecvData(recv&0xff); if( NULL != _g_uart5_recv_hook) { (*_g_uart5_recv_hook)(recv); } }if(USART_GetITStatus(UART5, USART_IT_TXE) == SET){ USART_ClearITPendingBit(UART5, USART_IT_TXE); }if(USART_GetITStatus(UART5, USART_IT_TC)==SET) { USART_ClearITPendingBit(UART5, USART_IT_TC); if(usart5_send_index < usart5_send_size) { USART_SendData(UART5, (u16)USART5_SEND_BUF[usart5_send_index]); ++usart5_send_index; } else { uart5_transfer_complete = 0; } }if(USART_GetFlagStatus(UART5, USART_IT_ORE) == SET) { USART_ClearFlag(UART5, USART_IT_ORE); USART_ReceiveData(UART5); } }/* ******************************************************************* **breif : DMA1 Channel4 发送完成中断 **note : usart1 ******************************************************************* */ void DMA1_Channel4_IRQHandler(void) { if(DMA_GetITStatus(DMA1_IT_TC4)) { DMA_ClearITPendingBit(DMA1_IT_TC4); } DMA_Cmd(DMA1_Channel4,DISABLE); uart1_transfer_complete = 1; }/* ******************************************************************* **breif : DMA1 Channel2 发送完成中断 **note : usart3 ******************************************************************* */void DMA1_Channel2_IRQHandler(void) { if(DMA_GetITStatus(DMA1_IT_TC2)) { DMA_ClearITPendingBit(DMA1_IT_TC2); } DMA_Cmd(DMA1_Channel2,DISABLE); uart3_transfer_complete = 1; }/* ******************************************************************* **breif : DMA1 Channel7 发送完成中断 **note : usart2 ******************************************************************* */ void DMA1_Channel7_IRQHandler(void) { if(DMA_GetITStatus(DMA1_IT_TC7)) { DMA_ClearITPendingBit(DMA1_IT_TC7); } DMA_Cmd(DMA1_Channel7,DISABLE); uart2_transfer_complete = 1; }/* ******************************************************************* **breif : DMA2 Channel5 发送完成中断 **note : usart4 ,程序有Bug ******************************************************************* */void DMA2_Channel4_5_IRQHandler(void) { if(DMA_GetITStatus(DMA2_IT_TC4)) { DMA_ClearITPendingBit(DMA2_IT_TC4); DMA_Cmd(DMA2_Channel4,DISABLE); uart4_transfer_complete = 1; }if(DMA_GetITStatus(DMA2_IT_TC5)) { DMA_ClearITPendingBit(DMA2_IT_TC5); DMA_Cmd(DMA2_Channel5,DISABLE); } }/* ********************************************************** **breif : 将数据载入接收缓冲区 **note : 数据载入Head方向 ********************************************************** */ #define macro_ISR_uart_putRecvData(uartNo) \ void bsp_ISR_uart##uartNo##_putRecvData(u8 data) {\ bsp_disableIrq(); \ u16 rxd_head; \ rxd_head = uart##uartNo##RecvHead + 1 ; \ if( rxd_head >= USART##uartNo##_RECV_BUF_SIZE ) \ rxd_head = 0; \ UART##uartNo##_RECV_BUF[uart##uartNo##RecvHead] = data; \ uart##uartNo##RecvHead = rxd_head; \ bsp_enableIrq(); \ }/* **************************************************************************** **breif : 将数据放置在发送缓冲区 **note : 数据载入Head方向 **这个方法平时并不使用,只在特殊应用下用到 **************************************************************************** */ #define macro_ISR_uart_putSendData(uartNo) \ void bsp_ISR_uart##uartNo##_putSendData(u8 data) {\ bsp_disableIrq(); \ u16 rxd_head; \ rxd_head = uart##uartNo##SendHead + 1 ; \ if( rxd_head >= USART##uartNo##_SEND_BUF_SIZE ) \ rxd_head = 0; \ USART##uartNo##_SEND_BUF[uart##uartNo##SendHead] = data; \ uart##uartNo##SendHead = rxd_head; \ bsp_enableIrq(); \ } /* **************************************************************************** **breif : 读接收缓冲区中的数据 **Ret: 1. EMPTY 数据缓冲区为空 **2. 数据 **note : 从Tail方向读取 **************************************************************************** */ #define macro_uart_getRecvData(uartNo) \ u16 bsp_uart##uartNo##_getRecvData(void)\ {\ bsp_disableIrq(); \ if(uart##uartNo##RecvHead==uart##uartNo##RecvTail) {\ bsp_enableIrq(); \ return EMPTY; \ }\ u16 ch=UART##uartNo##_RECV_BUF[uart##uartNo##RecvTail]; \ UART##uartNo##_RECV_BUF[uart##uartNo##RecvTail] = 0; \ uart##uartNo##RecvTail++; \ if(uart##uartNo##RecvTail>=USART##uartNo##_RECV_BUF_SIZE)\ uart##uartNo##RecvTail = 0; \ bsp_enableIrq(); \ return ch; \ } /* **************************************************************************** **breif : 读发送缓冲区中的数据 **Ret: 1. EMPTY 数据缓冲区为空 **2. 数据 **note : 从Tail方向读取 **************************************************************************** */ #define macro_uart_getSendData(uartNo) \ u16 bsp_uart##uartNo##_getSendData(void)\ {\ bsp_disableIrq(); \ if(uart##uartNo##SendHead==uart##uartNo##SendTail) {\ bsp_enableIrq(); \ return EMPTY; \ }\ u16 ch=USART##uartNo##_SEND_BUF[uart##uartNo##SendTail]; \ USART##uartNo##_SEND_BUF[uart##uartNo##SendTail] = 0; \ uart##uartNo##SendTail++; \ if(uart##uartNo##SendTail>=USART##uartNo##_SEND_BUF_SIZE)\ uart##uartNo##SendTail = 0; \ bsp_enableIrq(); \ return ch; \ } /* **************************************************************************** **breif : 接收缓冲区是否满 **Ret: 1. 满 **0. 未满 **************************************************************************** */ #define macro_uart_recvBufIsFull(uartNo) \ u8 bsp_uart##uartNo##_recvBufIsFull(void) \ {\ bsp_disableIrq(); \ u16 head = uart##uartNo##RecvHead+1; \ if(head >= USART##uartNo##_RECV_BUF_SIZE) {\ head = 0; \ }\ if(head == uart##uartNo##RecvTail) {\ bsp_enableIrq(); \ return 1; \ } else {\ bsp_enableIrq(); \ return 0; \ }\ }/* **************************************************************************** **breif : 发送缓冲区是否满 **Ret: 1. 满 **0. 未满 **************************************************************************** */ #define macro_uart_sendBufIsFull(uartNo) \ u8 bsp_uart##uartNo##_sendBufIsFull(void)\ {\ bsp_disableIrq(); \ u16 head = uart##uartNo##SendHead+1; \ if(head >= USART##uartNo##_SEND_BUF_SIZE) {\ head = 0; \ }\ if(head == uart##uartNo##SendTail) {\ bsp_enableIrq(); \ return 1; \ } else {\ bsp_enableIrq(); \ return 0; \ }\ }/* **************************************************************************** **breif : 接收缓冲区是否空 **Ret: 1. 空 **0. 非空 **************************************************************************** */ #define macro_uart_sendBufIsEmpty(uartNo) \ u8 bsp_uart##uartNo##_sendBufIsEmpty(void)\ {\ bsp_disableIrq(); \ if(uart##uartNo##SendHead==uart##uartNo##SendTail){ \ bsp_enableIrq(); \ return 1; \ }\ else {\ bsp_enableIrq(); \ return 0; \ }\ } /* **************************************************************************** **breif : 清空接收和发送缓冲区 **************************************************************************** */#define macro_uart_cleanAllBuffer(uartNo) \ void bsp_uart##uartNo##_cleanAllBuffer(void) \ {\ bsp_disableIrq(); \ uart##uartNo##RecvHead = uart##uartNo##RecvTail; \ uart##uartNo##SendHead = uart##uartNo##SendTail; \ bsp_enableIrq(); \ } /* **************************************************************************** **breif : 计算接收缓冲区剩余空间 **Ret: 剩余字节数 **************************************************************************** */#define macro_uart_recvBufRemain(uartNo) \ u16 bsp_uart##uartNo##_recvBufRemain(void)\ {\ bsp_disableIrq(); \ if(uart##uartNo##RecvHead >= uart##uartNo##RecvTail) {\ u16 v = (USART##uartNo##_RECV_BUF_SIZE-uart##uartNo##RecvHead)+uart##uartNo##RecvTail; \ bsp_enableIrq(); \ return v; \ } else {\ u16 v = (uart##uartNo##RecvTail - uart##uartNo##RecvHead) - 1; \ bsp_enableIrq(); \ return v; \ }\ }/* **************************************************************************** **breif : 发送缓冲区剩余空间 **Ret: 剩余字节数 **************************************************************************** */#define macro_uart_sendbufRemain(uartNo) \ u16 bsp_uart##uartNo##_sendbufRemain(void)\ {\ bsp_disableIrq(); \ if(uart##uartNo##SendHead >= uart##uartNo##SendTail) {\ u16 v = (USART##uartNo##_SEND_BUF_SIZE-uart##uartNo##SendHead)+uart##uartNo##SendTail; \ bsp_enableIrq(); \ return v; \ } else {\ u16 v = (uart##uartNo##SendTail - uart##uartNo##SendHead) - 1; \ bsp_enableIrq(); \ return v; \ }\ }macro_ISR_uart_putRecvData(1); macro_ISR_uart_putRecvData(2); macro_ISR_uart_putRecvData(3); macro_ISR_uart_putRecvData(4); macro_ISR_uart_putRecvData(5); macro_ISR_uart_putSendData(1); macro_ISR_uart_putSendData(2); macro_ISR_uart_putSendData(3); macro_ISR_uart_putSendData(4); macro_ISR_uart_putSendData(5); macro_uart_getRecvData(1); macro_uart_getRecvData(2); macro_uart_getRecvData(3); macro_uart_getRecvData(4); macro_uart_getRecvData(5); macro_uart_getSendData(1); macro_uart_getSendData(2); macro_uart_getSendData(3); macro_uart_getSendData(4); macro_uart_getSendData(5); macro_uart_sendBufIsEmpty(1); macro_uart_sendBufIsEmpty(2); macro_uart_sendBufIsEmpty(3); macro_uart_sendBufIsEmpty(4); macro_uart_sendBufIsEmpty(5); macro_uart_cleanAllBuffer(1); macro_uart_cleanAllBuffer(2); macro_uart_cleanAllBuffer(3); macro_uart_cleanAllBuffer(4); macro_uart_cleanAllBuffer(5); macro_uart_recvBufIsFull(1); macro_uart_recvBufIsFull(2); macro_uart_recvBufIsFull(3); macro_uart_recvBufIsFull(4); macro_uart_recvBufIsFull(5); macro_uart_sendBufIsFull(1); macro_uart_sendBufIsFull(2); macro_uart_sendBufIsFull(3); macro_uart_sendBufIsFull(4); macro_uart_sendBufIsFull(5); macro_uart_recvBufRemain(1); macro_uart_recvBufRemain(2); macro_uart_recvBufRemain(3); macro_uart_recvBufRemain(4); macro_uart_recvBufRemain(5); macro_uart_sendbufRemain(1); macro_uart_sendbufRemain(2); macro_uart_sendbufRemain(3); macro_uart_sendbufRemain(4); macro_uart_sendbufRemain(5);

    推荐阅读