|
void Uart_Init(UART_Type* UARTx, UartBaudRateIndex baud, UartParityIndex parity)
{
LL_GPIO_InitTypeDef GPIO_InitStruct = {0};
LL_UART_InitTypeDef UART_InitStruct = {0};
LL_UART_DeInit(UARTx);
switch((uint32_t)UARTx)
{
case UART0_BASE:
//PA13:UART0-RX PA14:UART0-TX
GPIO_InitStruct.Pin = LL_GPIO_PIN_13|LL_GPIO_PIN_14;
GPIO_InitStruct.Mode = LL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = DISABLE;
GPIO_InitStruct.RemapPin = DISABLE;
LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
//PA2:UART0-RX PA3:UART0-TX
// GPIO_InitStruct.Pin = LL_GPIO_PIN_2|LL_GPIO_PIN_3;
// GPIO_InitStruct.Mode = LL_GPIO_MODE_DIGITAL;
// GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
// GPIO_InitStruct.Pull = DISABLE;
// GPIO_InitStruct.RemapPin = DISABLE;
// LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
UART_InitStruct.ClockSrc = LL_RCC_UART_OPERATION_CLOCK_SOURCE_APBCLK1;
LL_UART_EnableIT_ReceiveTimeOut(UART0);
LL_UART_SetRxTimeOut(UART0,10);
/*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 = LL_GPIO_PIN_13|LL_GPIO_PIN_14;
GPIO_InitStruct.Mode = LL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = DISABLE;
GPIO_InitStruct.RemapPin = DISABLE;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//PC2:UART1-RX PC3:UART1-TX
// GPIO_InitStruct.Pin = LL_GPIO_PIN_2|LL_GPIO_PIN_3;
// GPIO_InitStruct.Mode = LL_GPIO_MODE_DIGITAL;
// GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
// GPIO_InitStruct.Pull = DISABLE;
// GPIO_InitStruct.RemapPin = DISABLE;
// LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
UART_InitStruct.ClockSrc = LL_RCC_UART_OPERATION_CLOCK_SOURCE_APBCLK1;
/*NVIC中断配置*/
NVIC_DisableIRQ(UART1_IRQn);
NVIC_SetPriority(UART1_IRQn,2);//中断优先级配置
NVIC_EnableIRQ(UART1_IRQn);
break;
case UART4_BASE:
//PB2:UART4-RX PB3:UART4-TX
GPIO_InitStruct.Pin = LL_GPIO_PIN_2|LL_GPIO_PIN_3;
GPIO_InitStruct.Mode = LL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = DISABLE;
GPIO_InitStruct.RemapPin = DISABLE;
LL_GPIO_Init(GPIOB, &GPIO_InitStruct);
//PA0:UART4-RX PA1:UART4-TX
// GPIO_InitStruct.Pin = LL_GPIO_PIN_0|LL_GPIO_PIN_1;
// GPIO_InitStruct.Mode = LL_GPIO_MODE_DIGITAL;
// GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
// GPIO_InitStruct.Pull = DISABLE;
// GPIO_InitStruct.RemapPin = DISABLE;
// LL_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 = LL_GPIO_PIN_0|LL_GPIO_PIN_1;
// GPIO_InitStruct.Mode = LL_GPIO_MODE_DIGITAL;
// GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
// GPIO_InitStruct.Pull = DISABLE;
// GPIO_InitStruct.RemapPin = DISABLE;
// LL_GPIO_Init(GPIOD, &GPIO_InitStruct);
//PC4:UART5-RX PC5:UART5-TX
GPIO_InitStruct.Pin = LL_GPIO_PIN_4|LL_GPIO_PIN_5;
GPIO_InitStruct.Mode = LL_GPIO_MODE_DIGITAL;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = DISABLE;
GPIO_InitStruct.RemapPin = DISABLE;
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*NVIC中断配置*/
NVIC_DisableIRQ(UART5_IRQn);
NVIC_SetPriority(UART5_IRQn,2);//中断优先级配置
NVIC_EnableIRQ(UART5_IRQn);
break;
default:
break;
}
UART_InitStruct.BaudRate = uartBaudRate[baud]; //波特率
UART_InitStruct.DataWidth = LL_UART_DATAWIDTH_8B; //数据位数
UART_InitStruct.StopBits = LL_UART_STOPBITS_1; //停止位
UART_InitStruct.Parity = uartParity[parity]; //奇偶校验
UART_InitStruct.TransferDirection = LL_UART_DIRECTION_TX_RX; //接收-发送使能
UART_InitStruct.InfraredModulation = DISABLE;
LL_UART_Init(UARTx, &UART_InitStruct);
LL_UART_EnableIT_ReceiveBuffFull(UARTx); //打开接收中断
//**********************************************************
if((UARTx==UART0) || (UARTx==UART1))
{
LL_UART_EnableIT_ReceiveTimeOut(UARTx);
LL_UART_SetRxTimeOut(UARTx,100);
}
//**********************************************************
}
void UART1_IRQHandler(void)
{
//接收中断处理
if(ENABLE == LL_UART_IsEnbaledIT_ReceiveBuffFull(UART1))
{
if(SET == LL_UART_IsActiveFlag_RXBF(UART1))
{
//中断转发接收到的数据
buf[len++] = LL_UART_ReceiveData(UART1);//此处省略了len长度检验等程序
}
if(SET == LL_UART_IsActiveFlag_RXTO(UART1))
{
revFinished = 1;//程序无法运行到这里
}
}
}
|
|