串口
- 该函数库的目的就是在统一的地方配置,将配置的不同项放置在一个结构体内部
- 使用一个枚举来定义一个的别名
bsp_uart.h
#ifndef _BSP_UART_H_
#define _BSP_UART_H_
#include
#include
#include "n32l40x.h"
#define BUFF_SIZE 48
#define DEBUG_UART USART1
typedef enum
{
UART_1,
UART_2,
UART_3,
UART_NUM,
}em_uart_t;
typedef struct
{
GPIO_Module* tx_gpiox;
uint16_t tx_pin;
uint32_t tx_gpio_rcc;
uint8_t tx_afx;
GPIO_Module* rx_gpiox;
uint16_t rx_pin;
uint32_t rx_gpio_rcc;
uint8_t rx_afx;
USART_Module* uartx;
uint32_t uart_rcc;
uint32_t bate;
IRQn_Type irqx;
uint8_t it_rx:1;
uint8_t it_idle:1;
uint8_t rx_idle;
uint16_t rx_count;
uint8_t txbuffer[BUFF_SIZE];
uint8_t rxbuffer[BUFF_SIZE];
} uart_t;
void bsp_uarts_init(void);
void bsp_uart_rs232_send(uint8_t * data,int len);
void bsp_uart_rs485_1_send(uint8_t * data,int len);
void bsp_uart_rs485_2_send(uint8_t * data,int len);
void bsp_get_uart_data(em_uart_t id,uint8_t * data,uint16_t *len);
#endif
bsp_uart.c
#include
#include
#include
#include "bsp_include.h"
#include "uart/bsp_uart.h"
static uart_t s_uarts[UART_NUM]= {
{ GPIOA,GPIO_PIN_9,RCC_APB2_PERIPH_GPIOA,GPIO_AF4_USART1,
GPIOA,GPIO_PIN_10,RCC_APB2_PERIPH_GPIOA,GPIO_AF4_USART1,
USART1,RCC_APB2_PERIPH_USART1,115200,USART1_IRQn,
1,1,0,0
},
{ GPIOB,GPIO_PIN_4,RCC_APB2_PERIPH_GPIOB,GPIO_AF4_USART2,
GPIOB,GPIO_PIN_5,RCC_APB2_PERIPH_GPIOB,GPIO_AF6_USART2,
USART2,RCC_APB1_PERIPH_USART2,115200,USART2_IRQn,
1,1,0,0
},
{ GPIOB,GPIO_PIN_10,RCC_APB2_PERIPH_GPIOB,GPIO_AF0_USART3,
GPIOB,GPIO_PIN_11,RCC_APB2_PERIPH_GPIOB,GPIO_AF5_USART3,
USART3,RCC_APB1_PERIPH_USART3,115200,USART3_IRQn,
1,1,0,0
},
};
void bsp_uart_rcc_config(uart_t *puartx )
{
RCC_EnableAPB2PeriphClk(puartx->uart_rcc, ENABLE);
if(USART3 == puartx->uartx || USART2 == puartx->uartx)
{
RCC_EnableAPB1PeriphClk(puartx->uart_rcc,ENABLE);
}
else
{
RCC_EnableAPB2PeriphClk(puartx->uart_rcc,ENABLE);
}
}
void bsp_uart_gpio_config(uart_t *puartx)
{
GPIO_InitType GPIO_InitStructure;
RCC_EnableAPB2PeriphClk(puartx->rx_gpio_rcc,ENABLE);
RCC_EnableAPB2PeriphClk(puartx->tx_gpio_rcc,ENABLE);
GPIO_InitStruct(&GPIO_InitStructure);
GPIO_InitStructure.Pin = puartx->tx_pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Alternate = puartx->tx_afx;
GPIO_InitPeripheral(puartx->tx_gpiox, &GPIO_InitStructure);
GPIO_InitStructure.Pin = puartx->rx_pin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
GPIO_InitStructure.GPIO_Alternate = puartx->rx_afx;
GPIO_InitPeripheral(puartx->rx_gpiox, &GPIO_InitStructure);
}
void bsp_uart_nvic_config(uart_t *puartx)
{
NVIC_InitType NVIC_InitStructure;
if(puartx->it_idle || puartx->it_rx)
{
NVIC_InitStructure.NVIC_IRQChannel = puartx->irqx;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 7;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_SetPriority(puartx->irqx,7);
}
if(puartx->it_rx)
{
USART_ConfigInt(puartx->uartx, USART_INT_RXDNE, ENABLE);
}
if(puartx->it_idle)
{
USART_ConfigInt(puartx->uartx, USART_INT_IDLEF, ENABLE);
}
}
int bsp_uart_get_id(USART_Module* uartx)
{
for(int i=0; i<UART_NUM; i++)
{
if(uartx == s_uarts[i].uartx)
{
return i;
}
}
return -1;
}
int bsp_uart_iaq_handler(uart_t *puartx)
{
int result=0;
if (USART_GetIntStatus(puartx->uartx, USART_INT_RXDNE) != RESET)
{
puartx->rxbuffer[puartx->rx_count%BUFF_SIZE] = USART_ReceiveData(puartx->uartx);
puartx->rx_count++;
}
else if(USART_GetIntStatus(puartx->uartx, USART_INT_IDLEF) != RESET)
{
USART_ReceiveData(puartx->uartx);
puartx->rx_idle = 1;
result=1;
}
return result;
}
void bsp_get_uart_data(em_uart_t id,uint8_t * data,uint16_t *len)
{
if(UART_NUM<id || !data||!len)
{
*len=0;
return;
}
uart_t *puart = s_uarts+id;
*len=0;
if(puart->rx_idle)
{
puart->rx_idle=0;
*len = puart->rx_count;
memcpy(data,puart->rxbuffer,*len);
puart->rx_count=0;
}
}
void bsp_uart_init(uart_t *puartx)
{
USART_InitType USART_InitStructure;
bsp_uart_rcc_config(puartx);
bsp_uart_nvic_config(puartx);
bsp_uart_gpio_config(puartx);
USART_StructInit(&USART_InitStructure);
USART_InitStructure.BaudRate = puartx->bate;
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;
USART_Init(puartx->uartx, &USART_InitStructure);
USART_Enable(puartx->uartx, ENABLE);
}
void bsp_uarts_init(void)
{
for(int i=0; i<UART_NUM; i++)
{
bsp_uart_init(s_uarts+i);
}
}
void USART1_IRQHandler(void)
{
int id = bsp_uart_get_id(USART1);
if(id!=-1) {
bsp_uart_iaq_handler(s_uarts+id);
}
}
void USART2_IRQHandler(void)
{
int id = bsp_uart_get_id(USART2);
if(id!=-1) {
bsp_uart_iaq_handler(s_uarts+id);
}
}
void USART3_IRQHandler(void)
{
int id = bsp_uart_get_id(USART3);
if(id!=-1) {
if( bsp_uart_iaq_handler(s_uarts+id))
{
}
}
}
void UART4_IRQHandler(void)
{
int id = bsp_uart_get_id(UART4);
if(id!=-1) {
bsp_uart_iaq_handler(s_uarts+id);
}
}
void UART5_IRQHandler(void)
{
int id = bsp_uart_get_id(UART5);
if(id!=-1) {
bsp_uart_iaq_handler(s_uarts+id);
}
}
int fputc(int ch, FILE* f)
{
USART_SendData(DEBUG_UART, (uint8_t)ch);
while (USART_GetFlagStatus(DEBUG_UART, USART_FLAG_TXDE) == RESET);
return (ch);
}
void bsp_uart_rs232_send(uint8_t * data,int len)
{
for(int i=0;i<len;i++)
{
USART_SendData(USART3, (uint8_t)data[i]);
while (USART_GetFlagStatus(USART3, USART_FLAG_TXDE) == RESET);
}
}
void bsp_uart_rs485_1_send(uint8_t * data,int len)
{
for(int i=0;i<len;i++)
{
USART_SendData(USART1, (uint8_t)data[i]);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXDE) == RESET);
}
}
void bsp_uart_rs485_2_send(uint8_t * data,int len)
{
led_on_off(RS485_DE,Bit_SET);
for(int i=0;i<len;i++)
{
USART_SendData(USART2, (uint8_t)data[i]);
while (USART_GetFlagStatus(USART2, USART_FLAG_TXDE) == RESET);
}
led_on_off(RS485_DE,Bit_RESET);
}