169 lines
7.0 KiB
C
169 lines
7.0 KiB
C
#include "main.h"
|
|
#include "uart.h"
|
|
#include "lin.h"
|
|
#include "user_init.h"
|
|
|
|
struct UARTOpStruct UARTxOp;
|
|
|
|
/**
|
|
* @brief UART初始化
|
|
* @param UARTx: 串口选择
|
|
* @retval void
|
|
*/
|
|
void Uartx_Init(UART_Type *UARTx)
|
|
{
|
|
FL_GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
FL_UART_InitTypeDef UART_InitStruct = {0};
|
|
|
|
switch ((uint32_t)UARTx)
|
|
{
|
|
|
|
case UART0_BASE:
|
|
/* PA13:UART0-RX PA14:UART0-TX */
|
|
GPIO_InitStruct.pin = FL_GPIO_PIN_13 | FL_GPIO_PIN_14;
|
|
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_OPENDRAIN; /* 推挽输出 */
|
|
GPIO_InitStruct.pull = FL_ENABLE;
|
|
GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* PA2:UART0-RX PA3:UART0-TX */
|
|
// GPIO_InitStruct.pin = FL_GPIO_PIN_2|FL_GPIO_PIN_3;
|
|
// GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
// GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
|
|
// GPIO_InitStruct.pull = FL_DISABLE;
|
|
// GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
// GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
// FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
UART_InitStruct.clockSrc = FL_CMU_UART0_CLK_SOURCE_APBCLK;
|
|
/* NVIC中断配置 */
|
|
NVIC_DisableIRQ(UART0_IRQn);
|
|
NVIC_SetPriority(UART0_IRQn, 2); /* 中断优先级配置 */
|
|
NVIC_EnableIRQ(UART0_IRQn);
|
|
break;
|
|
|
|
case UART1_BASE:
|
|
/* PB13:UART1-RX PB14:UART1-TX */
|
|
// GPIO_InitStruct.pin = FL_GPIO_PIN_13|FL_GPIO_PIN_14;
|
|
// GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
// GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
|
|
// GPIO_InitStruct.pull = FL_ENABLE;
|
|
// GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
// GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
// FL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* PC2:UART1-RX PC3:UART1-TX */
|
|
GPIO_InitStruct.pin = FL_GPIO_PIN_2 | FL_GPIO_PIN_3;
|
|
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
|
|
GPIO_InitStruct.pull = FL_ENABLE;
|
|
GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
FL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
UART_InitStruct.clockSrc = FL_CMU_UART1_CLK_SOURCE_APBCLK;
|
|
/* NVIC中断配置 */
|
|
NVIC_DisableIRQ(UART1_IRQn);
|
|
NVIC_SetPriority(UART1_IRQn, 2); /* 中断优先级配置 */
|
|
NVIC_EnableIRQ(UART1_IRQn);
|
|
break;
|
|
|
|
case UART3_BASE:
|
|
/* PB0:UART3-RX PB1:UART3-TX */
|
|
GPIO_InitStruct.pin = FL_GPIO_PIN_0 | FL_GPIO_PIN_1;
|
|
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
|
|
GPIO_InitStruct.pull = FL_ENABLE;
|
|
GPIO_InitStruct.remapPin = FL_ENABLE;
|
|
GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
FL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* PD7:UART3-RX PD8:UART3-TX */
|
|
// GPIO_InitStruct.pin = FL_GPIO_PIN_7|FL_GPIO_PIN_8;
|
|
// GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
// GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
|
|
// GPIO_InitStruct.pull = FL_DISABLE;
|
|
// GPIO_InitStruct.remapPin = FL_ENABLE;
|
|
// GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
// FL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
/* NVIC中断配置 */
|
|
NVIC_DisableIRQ(UART3_IRQn);
|
|
NVIC_SetPriority(UART3_IRQn, 2); /* 中断优先级配置 */
|
|
NVIC_EnableIRQ(UART3_IRQn);
|
|
break;
|
|
|
|
|
|
case UART4_BASE:
|
|
/* PB2:UART4-RX PB3:UART4-TX */
|
|
GPIO_InitStruct.pin = FL_GPIO_PIN_2 | FL_GPIO_PIN_3;
|
|
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_OPENDRAIN;
|
|
GPIO_InitStruct.pull = FL_ENABLE;
|
|
GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
FL_GPIO_Init(GPIOB, &GPIO_InitStruct);
|
|
|
|
/* PA0:UART4-RX PA1:UART4-TX */
|
|
// GPIO_InitStruct.pin = FL_GPIO_PIN_0|FL_GPIO_PIN_1;
|
|
// GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
// GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
|
|
// GPIO_InitStruct.pull = FL_DISABLE;
|
|
// GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
// GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
// FL_GPIO_Init(GPIOA, &GPIO_InitStruct);
|
|
|
|
/* NVIC中断配置 */
|
|
NVIC_DisableIRQ(UART4_IRQn);
|
|
NVIC_SetPriority(UART4_IRQn, 2); /* 中断优先级配置 */
|
|
NVIC_EnableIRQ(UART4_IRQn);
|
|
break;
|
|
|
|
case UART5_BASE:
|
|
/* PD0:UART5-RX PD1:UART5-TX */
|
|
GPIO_InitStruct.pin = FL_GPIO_PIN_0;
|
|
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.pull = FL_ENABLE;
|
|
GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
FL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
GPIO_InitStruct.pin = FL_GPIO_PIN_1;
|
|
GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
|
|
GPIO_InitStruct.pull = FL_DISABLE;
|
|
GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
FL_GPIO_Init(GPIOD, &GPIO_InitStruct);
|
|
|
|
/* PC4:UART5-RX PC5:UART5-TX */
|
|
// GPIO_InitStruct.pin = FL_GPIO_PIN_4|FL_GPIO_PIN_5;
|
|
// GPIO_InitStruct.mode = FL_GPIO_MODE_DIGITAL;
|
|
// GPIO_InitStruct.outputType = FL_GPIO_OUTPUT_PUSHPULL;
|
|
// GPIO_InitStruct.pull = FL_DISABLE;
|
|
// GPIO_InitStruct.remapPin = FL_DISABLE;
|
|
// GPIO_InitStruct.analogSwitch = FL_DISABLE;
|
|
// FL_GPIO_Init(GPIOC, &GPIO_InitStruct);
|
|
|
|
/* NVIC中断配置 */
|
|
NVIC_DisableIRQ(UART5_IRQn);
|
|
NVIC_SetPriority(UART5_IRQn, LIN_UART_IRQ_PRI); /* 中断优先级配置 */
|
|
NVIC_EnableIRQ(UART5_IRQn);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
|
|
UART_InitStruct.baudRate = LIN_BAUDRATE; /* 波特率 */
|
|
UART_InitStruct.dataWidth = FL_UART_DATA_WIDTH_8B; /* 数据位数 */
|
|
UART_InitStruct.stopBits = FL_UART_STOP_BIT_WIDTH_1B; /* 停止位 */
|
|
UART_InitStruct.parity = FL_UART_PARITY_NONE; /* 奇偶校验 */
|
|
UART_InitStruct.transferDirection = FL_UART_DIRECTION_TX; /* FL_UART_DIRECTION_TX_RX //接收-发送使能 */
|
|
FL_UART_Init(UARTx, &UART_InitStruct);
|
|
}
|
|
|