#include "hal_uart4.h" #include "queue.h" #include "stm32f4xx.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_rcc.h" #include "stm32f4xx_usart.h" uart_type uart_msg = { .rx_count_u8 = 0, .tx_count_u8 = 0, .rx_finished_flg = 0, .disconnect_flg = 0, .rx_over_time = 5, .rx_over_time_count = 0xFF, .disconnect_count = 0, }; void hal_uart4_init(void) { GPIO_InitTypeDef GPIO_StructInit; NVIC_InitTypeDef NVIC_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF; GPIO_StructInit.GPIO_OType = GPIO_OType_PP; GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz; GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_UP; GPIO_StructInit.GPIO_Pin = GPIO_Pin_10; GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_UART4); GPIO_Init(GPIOC, &GPIO_StructInit); GPIO_StructInit.GPIO_Pin = GPIO_Pin_11; // GPIO_StructInit.GPIO_Mode = GPIO_Mode_AF; // GPIO_StructInit.GPIO_OType = GPIO_OType_PP; // GPIO_StructInit.GPIO_Speed = GPIO_Speed_100MHz; // GPIO_StructInit.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); GPIO_Init(GPIOC, &GPIO_StructInit); NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn; // 串口1中断通道 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; // 抢占优先级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 子优先级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // IRQ通道使能 NVIC_Init(&NVIC_InitStructure); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; 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_Rx | USART_Mode_Tx; USART_Init(UART4, &USART_InitStructure); // 初始化串口 USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); USART_Cmd(UART4, ENABLE); // 使能串口 } void UART4_IRQHandler(void) { // 接收中断处理 if (RESET != USART_GetITStatus(UART4, USART_IT_RXNE)) { USART_ClearITPendingBit(UART4, USART_IT_RXNE); uart_msg.rx_over_time_count = 0; uart_msg.disconnect_count = 0; uart_msg.disconnect_flg = 0; if (uart_msg.rx_count_u8 >= RX_TX_BUF_LEN) { uart_msg.rx_count_u8 = 0; } uart_msg.rx[uart_msg.rx_count_u8++] = USART_ReceiveData(UART4); } // 发送中断处理 if (RESET != USART_GetITStatus(UART4, USART_IT_TXE)) { USART_ClearITPendingBit(UART4, USART_IT_TXE); USART_SendData(UART4, uart_msg.tx[uart_msg.tx_count_u8++]); if (uart_msg.tx_count_u8 >= uart_msg.tx_len) { USART_ITConfig(UART4, USART_IT_TXE, DISABLE); USART_ITConfig(UART4, USART_IT_TC, ENABLE); } } // 发送完成中断 if (RESET != USART_GetITStatus(UART4, USART_IT_TC)) { USART_ClearITPendingBit(UART4, USART_IT_TC); USART_ITConfig(UART4, USART_IT_TC, DISABLE); USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); } // 发送错误 if (RESET != USART_GetITStatus(UART4, USART_IT_ORE)) USART_ClearITPendingBit(UART4, USART_IT_ORE); } void uart4_rx_ticks(void) { if (uart_msg.rx_over_time_count != 0xFF) { if (++uart_msg.rx_over_time_count >= uart_msg.rx_over_time) { uart_msg.rx_over_time_count = 0xFF; uart_msg.rx_finished_flg = 1; } } if (uart_msg.disconnect_count != 0xFFFF) { if (++uart_msg.disconnect_count >= 2500) // 5000ms { uart_msg.disconnect_count = 0xFFFF; uart_msg.disconnect_flg = 1; } } } void uart4_start_send(uart_type *p_msg) { p_msg->tx_count_u8 = 0; USART_ITConfig(UART4, USART_IT_RXNE, DISABLE); USART_SendData(UART4, p_msg->tx[p_msg->tx_count_u8++]); USART_ITConfig(UART4, USART_IT_TXE, ENABLE); }