c
复制代码
#include <stdio.h>
#include <math.h>
#include <string.h>
#include "bsp_include.h"
#include "uart/bsp_uart.h"
static uart_t s_uarts[UART_NUM]= {
//UART1
{ GPIOA,GPIO_PIN_9,RCC_APB2_PERIPH_GPIOA,GPIO_AF4_USART1,//gpio tx
GPIOA,GPIO_PIN_10,RCC_APB2_PERIPH_GPIOA,GPIO_AF4_USART1,//gpio rx
USART1,RCC_APB2_PERIPH_USART1,115200,USART1_IRQn,//uart
// DMA_CH1,DMA_CH2,DMA_REMAP_USART1_RX,DMA_REMAP_USART1_TX,DMA_Channel1_IRQn,DMA_Channel2_IRQn,1,1,//dma
1,1,0,0//switch
},
//UART2
{ GPIOB,GPIO_PIN_4,RCC_APB2_PERIPH_GPIOB,GPIO_AF4_USART2,//gpio tx
GPIOB,GPIO_PIN_5,RCC_APB2_PERIPH_GPIOB,GPIO_AF6_USART2,//gpio rx
USART2,RCC_APB1_PERIPH_USART2,115200,USART2_IRQn,//uart
// DMA_CH1,DMA_CH2,DMA_REMAP_USART1_RX,DMA_REMAP_USART1_TX,DMA_Channel1_IRQn,DMA_Channel2_IRQn,1,1,//dma
1,1,0,0//switch
},
//UART3
{ GPIOB,GPIO_PIN_10,RCC_APB2_PERIPH_GPIOB,GPIO_AF0_USART3,//gpio tx
GPIOB,GPIO_PIN_11,RCC_APB2_PERIPH_GPIOB,GPIO_AF5_USART3,//gpio rx
USART3,RCC_APB1_PERIPH_USART3,115200,USART3_IRQn,//uart
// DMA_CH1,DMA_CH2,DMA_REMAP_USART1_RX,DMA_REMAP_USART1_TX,DMA_Channel1_IRQn,DMA_Channel2_IRQn,1,1,//dma
1,1,0,0//switch
},
};
/*
*/
void bsp_uart_rcc_config(uart_t *puartx )
{
// Enable GPIO clock
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);
}
}
/**
* @brief Configures the different GPIO ports.
*/
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);
}
/**
* @brief Configures the nested vectored interrupt controller.
*/
void bsp_uart_nvic_config(uart_t *puartx)
{
NVIC_InitType NVIC_InitStructure;
/* Enable the USARTz Interrupt */
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);
//volatile uint32_t pri = NVIC_GetPriority(puartx->irqx);
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;
/* 1.时钟配置 */
bsp_uart_rcc_config(puartx);
/* 2.中断配置 */
bsp_uart_nvic_config(puartx);
/* 3.串口涉及的gpio配置 */
bsp_uart_gpio_config(puartx);
/* Configure the DMA */
//bsp_uart_dma_config(puartx);
/*4.串口的相关参数设置 */
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);
/* 5.使能串口*/
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)
{
// BaseType_t TaskWoken;
int id = bsp_uart_get_id(USART3);
if(id!=-1) {
if( bsp_uart_iaq_handler(s_uarts+id))
{
//通知gui线程收数据
// if(task_hd_gui_recv)
//
// xTaskNotifyFromISR(task_hd_gui_recv, 1, eSetValueWithOverwrite, &TaskWoken);
}
}
}
/**
* @brief This function handles UART4 global interrupt request.
*/
void UART4_IRQHandler(void)
{
int id = bsp_uart_get_id(UART4);
if(id!=-1) {
bsp_uart_iaq_handler(s_uarts+id);
}
}
/**
* @brief This function handles UART7 global interrupt request.
*/
void UART5_IRQHandler(void)
{
int id = bsp_uart_get_id(UART5);
if(id!=-1) {
bsp_uart_iaq_handler(s_uarts+id);
}
}
/* retarget the C library printf function to the USART */
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);
}