#include "HD_UART.h" void USART2_IRQHandler(void) __attribute__((interrupt())); void USART3_IRQHandler(void) __attribute__((interrupt())); void UART4_IRQHandler(void) __attribute__((interrupt())); void UART5_IRQHandler(void) __attribute__((interrupt())); void UART6_IRQHandler(void) __attribute__((interrupt())); void UART7_IRQHandler(void) __attribute__((interrupt())); void UART8_IRQHandler(void) __attribute__((interrupt())); /** * @brief 初始化串口2 * @param baud 波特率 * @retval */ void uart2Init(int baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* USART2 TX --> PA2 RX --> PA3 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PA2为复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PA3为浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART2, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级为1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //中断优先级初始化 USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_Cmd(USART2,ENABLE); } /** * @brief 初始化串口3 * @param baud 波特率 * @retval */ void uart3Init(int baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* USART3 TX --> PB10 RX --> PB11 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PB10为复用推挽输出 GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PB11为浮空输入 GPIO_Init(GPIOB, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART3, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢占优先级为1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //中断优先级初始化 USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); USART_Cmd(USART3,ENABLE); } /** * @brief 初始化串口4 * @param baud 波特率 * @retval */ void uart4Init(int baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* UART4 TX --> PC10 RX --> PC11 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC10为复用推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PC11为浮空输入 GPIO_Init(GPIOC, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(UART4, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢占优先级为3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //中断优先级初始化 USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); USART_Cmd(UART4,ENABLE); } /** * @brief 初始化串口5 * @param baud 波特率 * @retval */ void uart5Init(int baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* UART5 TX --> PC12 RX --> PD2 */ 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_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC12为复用推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PD2为浮空输入 GPIO_Init(GPIOD, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(UART5, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢占优先级为3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //中断优先级初始化 USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); USART_Cmd(UART5,ENABLE); } /** * @brief 初始化串口6 * @param baud 波特率 * @retval */ void uart6Init(int baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* UART6 TX --> PC0 RX --> PC1 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART6, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC0为复用推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PC1为浮空输入 GPIO_Init(GPIOC, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(UART6, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = UART6_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢占优先级为3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //中断优先级初始化 USART_ITConfig(UART6, USART_IT_RXNE, ENABLE); USART_Cmd(UART6,ENABLE); } /** * @brief 初始化串口7 * @param baud 波特率 * @retval */ void uart7Init(int baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* UART7 TX --> PC2 RX --> PC3 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART7, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC2为复用推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PC3为浮空输入 GPIO_Init(GPIOC, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(UART7, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = UART7_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //抢占优先级为3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //中断优先级初始化 USART_ITConfig(UART7, USART_IT_RXNE, ENABLE); USART_Cmd(UART7,ENABLE); } /** * @brief 初始化串口8 * @param baud 波特率 * @retval */ void uart8Init(int baud) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; /* UART8 TX --> PC4 RX --> PC5 */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART8, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置PC4为复用推挽输出 GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设置PC5为浮空输入 GPIO_Init(GPIOC, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = baud; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(UART8, &USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = UART8_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3; //抢占优先级为3 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级为3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //中断优先级初始化 USART_ITConfig(UART8, USART_IT_RXNE, ENABLE); USART_Cmd(UART8,ENABLE); } /** * @brief 设置空闲中断状态 * @param USARTx 波特率 state 状态 * @retval */ void setIDLE(USART_TypeDef *USARTx, FunctionalState state) { USART_ITConfig(USARTx, USART_IT_IDLE, state); } /** * @brief 设置中断发送状态 * @param USARTx 波特率 state 状态 * @retval */ void setTXE(USART_TypeDef *USARTx, FunctionalState state) { USART_ITConfig(USARTx, USART_IT_TXE, state); } void USART2_IRQHandler(void) { Mcu_Interrupt(); } void USART3_IRQHandler(void) { J0_Interrupt(); J1_Interrupt(); } void UART4_IRQHandler(void) { J2_Interrupt(); J3_Interrupt(); } void UART5_IRQHandler(void) { J4_Interrupt(); J5_Interrupt(); } void UART6_IRQHandler(void) { J6_Interrupt(); J7_Interrupt(); } void UART7_IRQHandler(void) { J9_Interrupt(); Lora_Interrupt(); } void UART8_IRQHandler(void) { J8_Interrupt(); Su806_Interrupt(); }