| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- #include "uart2.h"
- #include <rtthread.h>
- #include <stm32f4xx_hal.h>
- /* ===================== 全局变量定义 ===================== */
- volatile uint8_t u2_rx_flg = 0; /* 接收完成标志 */
- volatile uint16_t u2_rx_cnt = 0; /* 接收数据长度 */
- volatile uint8_t u2_rx_buf[U2_BUF_MAX]; /* 接收缓冲区 */
- /* UART句柄 */
- static UART_HandleTypeDef huart2;
- /* ===================== 私有函数声明 ===================== */
- static void u2_gpio_init(void);
- static void u2_nvic_init(void);
- static int u2_hw_init(void);
- /* ===================== GPIO初始化 ===================== */
- static void u2_gpio_init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
-
- /* 使能GPIO时钟 */
- __HAL_RCC_GPIOA_CLK_ENABLE();
-
- /* PA2 - TX, PA3 - RX */
- GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
- GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
- GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- }
- /* ===================== NVIC初始化 ===================== */
- static void u2_nvic_init(void)
- {
- HAL_NVIC_SetPriority(USART2_IRQn, 2, 0);
- HAL_NVIC_EnableIRQ(USART2_IRQn);
- }
- /* ===================== UART硬件初始化 ===================== */
- static int u2_hw_init(void)
- {
- /* 使能USART2时钟 */
- __HAL_RCC_USART2_CLK_ENABLE();
-
- /* 配置UART参数 */
- huart2.Instance = USART2;
- huart2.Init.BaudRate = 115200;
- huart2.Init.WordLength = UART_WORDLENGTH_8B;
- huart2.Init.StopBits = UART_STOPBITS_1;
- huart2.Init.Parity = UART_PARITY_NONE;
- huart2.Init.Mode = UART_MODE_TX_RX;
- huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
- huart2.Init.OverSampling = UART_OVERSAMPLING_16;
-
- if (HAL_UART_Init(&huart2) != HAL_OK)
- {
- return -1;
- }
-
- /* 使能空闲中断 */
- __HAL_UART_ENABLE_IT(&huart2, UART_IT_IDLE);
- /* 使能接收中断 */
- __HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
-
- return 0;
- }
- /* ===================== 自动初始化 ===================== */
- static int u2_init(void)
- {
- u2_gpio_init();
- u2_nvic_init();
- if (u2_hw_init() != 0)
- {
- return -1;
- }
- return 0;
- }
- INIT_BOARD_EXPORT(u2_init);
- /* ===================== 发送接口 ===================== */
- int u2_send_buf(uint8_t *buf , int size)
- {
- if (buf == NULL || size <= 0)
- {
- return -1;
- }
-
- if (HAL_UART_Transmit(&huart2, buf, size, 1000) != HAL_OK)
- {
- return -1;
- }
-
- return size;
- }
- /* ===================== 中断服务函数 ===================== */
- void USART2_IRQHandler(void)
- {
- uint32_t isrflags = READ_REG(huart2.Instance->SR);
- uint32_t cr1its = READ_REG(huart2.Instance->CR1);
- uint32_t cr3its = READ_REG(huart2.Instance->CR3);
-
- /* 检查空闲中断标志 */
- if ((isrflags & USART_SR_IDLE) != RESET)
- {
- /* 清除空闲标志:先读SR再读DR */
- (void)huart2.Instance->SR;
- (void)huart2.Instance->DR;
-
- /* 设置接收完成标志 */
- u2_rx_flg = 1;
- }
-
- /* 检查接收中断标志 */
- if (((isrflags & USART_SR_RXNE) != RESET) && ((cr1its & USART_CR1_RXNEIE) != RESET))
- {
- /* 读取接收数据 */
- uint8_t data = (uint8_t)(huart2.Instance->DR & 0xFF);
-
- /* 存入缓冲区 */
- if (u2_rx_cnt < sizeof(u2_rx_buf))
- {
- u2_rx_buf[u2_rx_cnt++] = data;
- }
- }
-
- /* 处理其他中断(错误标志等) */
- if (((isrflags & USART_SR_ORE) != RESET) && (((cr1its & USART_CR1_RXNEIE) != RESET) || ((cr3its & USART_CR3_EIE) != RESET)))
- {
- /* 清除溢出错误标志 */
- (void)huart2.Instance->DR;
- (void)huart2.Instance->SR;
- }
-
- if (((isrflags & USART_SR_FE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
- {
- /* 清除帧错误标志 */
- (void)huart2.Instance->DR;
- (void)huart2.Instance->SR;
- }
-
- if (((isrflags & USART_SR_NE) != RESET) && ((cr3its & USART_CR3_EIE) != RESET))
- {
- /* 清除噪声错误标志 */
- (void)huart2.Instance->DR;
- (void)huart2.Instance->SR;
- }
-
- if (((isrflags & USART_SR_PE) != RESET) && ((cr1its & USART_CR1_PEIE) != RESET))
- {
- /* 清除校验错误标志 */
- (void)huart2.Instance->DR;
- (void)huart2.Instance->SR;
- }
- }
- void uart2_clear_rx_buffer(void)
- {
- u2_rx_flg = 0;
- u2_rx_cnt = 0;
- memset(u2_rx_buf , 0 ,U2_BUF_MAX);
- }
- /* ===================== 测试线程 ===================== */
- //static void u2_test_thread_entry(void *parameter)
- //{
- // (void)parameter;
- //
- // rt_kprintf("UART2 test thread started\r\n");
- //
- // while (1)
- // {
- // /* 检查接收完成标志 */
- // if (u2_rx_flg)
- // {
- // /* 临时保存接收数据 */
- // uint16_t rx_len = u2_rx_cnt;
- // uint8_t tx_buf[256];
- //
- // if (rx_len > 0 && rx_len <= sizeof(u2_rx_buf))
- // {
- // /* 复制数据到发送缓冲区 */
- // rt_memcpy(tx_buf, (void *)u2_rx_buf, rx_len);
- //
- // /* 打印接收信息 */
- // rt_kprintf("UART2 RX: len=%d, data=", rx_len);
- // for (uint16_t i = 0; i < rx_len; i++)
- // {
- // rt_kprintf("%02X ", u2_rx_buf[i]);
- // }
- // rt_kprintf("\r\n");
- //
- // /* 延迟100ms */
- // rt_thread_mdelay(100);
- //
- // /* 原路发送 */
- // u2_send_buf(tx_buf,rx_len);
- // rt_kprintf("UART2 TX: echo back %d bytes\r\n", rx_len);
- // }
- //
- // /* 清除标志和计数,准备下次接收 */
- // u2_rx_cnt = 0;
- // u2_rx_flg = 0;
- // }
- //
- // rt_thread_mdelay(10);
- // }
- //}
- //
- ///* 创建测试线程 */
- //static void u2_test_thread_init(void)
- //{
- // rt_thread_t tid;
- //
- // tid = rt_thread_create("u2_test",
- // u2_test_thread_entry,
- // RT_NULL,
- // 512,
- // RT_THREAD_PRIORITY_MAX / 2,
- // 10);
- // if (tid != RT_NULL)
- // {
- // rt_thread_startup(tid);
- // }
- //}
- //INIT_APP_EXPORT(u2_test_thread_init);
|