/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2024 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "spi.h" #include "tim.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include "rf433.h" #include "rf433_config.h" #include "rf433_hal.h" /* 应用层模块头文件 */ #include "uart2_print.h" #include "io_monitor.h" #include "cmd_parser.h" #include "relay_control.h" /* 多通信接口统一指令处理系统模块 */ #include "multi_uart_router.h" #include "cmd_router.h" #include "debug_log.h" /* W5500 Ethernet模块头文件 */ #if USE_W5500 #include "user_main.h" #include "wiz_platform.h" #include "wiz_interface.h" #include "wizchip_conf.h" #include "loopback.h" #endif extern void wiz_timer_handler(void); #if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH) #include "rf433_tx_app.h" #endif #if (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH) #include "rf433_rx_app.h" #endif /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */ static uint8_t uart2_rx_byte = 0; static uint8_t uart3_rx_byte = 0; /* === 433 模块的接收缓存 (UART1) === */ static uint8_t u1_rx_buffer[256]; static volatile uint16_t u1_rx_len = 0; static volatile uint32_t u1_last_rx_time = 0; /* === 485 设备的接收缓存 (UART3) 增加这三行!=== */ static uint8_t u3_rx_buffer[512]; static volatile uint16_t u3_rx_len = 0; static volatile uint32_t u3_last_rx_time = 0; /* W5500 variables */ #if USE_W5500 #define SOCKET_ID 0 #define ETHERNET_BUF_MAX_SIZE 2048 static uint8_t ethernet_buf[ETHERNET_BUF_MAX_SIZE] = {0}; static uint16_t local_port = 8000; #endif /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART1_UART_Init(); MX_SPI2_Init(); MX_USART2_UART_Init(); MX_USART3_UART_Init(); MX_TIM2_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_Base_Start_IT(&htim2); /* 初始化应用层模块 */ UART2_Print_Init(); IO_Monitor_Init(); CmdParser_Init(); Relay_Init(); /* 初始化多通信接口统一指令处理系统 */ MultiUART_Init(); CmdRouter_Init(); DebugLog_Init(); /* ============================================================== */ /* 🚀 【核心修复】将全局日志门槛提高到 INFO 级别,屏蔽所有底层 DEBUG 噪音 */ DebugLog_SetLevel(LOG_LEVEL_INFO); /* ============================================================== */ printf("\r\n[DEBUG] 1. 启动 TIM2 中断\r\n"); HAL_TIM_Base_Start_IT(&htim2); /* 启动UART2接收中断 */ HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1); /* 启动UART3接收中断 - RS485接口 */ #if USE_RS485 HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1); #endif /* 初始化RF433模块 - 使用默认配置 */ rf433_init(NULL); /* 启动UART1接收 - 使用rf433_hal中的临时变量 */ HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1); #if USE_W5500 printf("[DEBUG] 2. 进入 wizchip_initialize()\r\n"); wizchip_initialize(); printf("[DEBUG] 3. wizchip_initialize() 成功返回!\r\n"); printf("[DEBUG] 4. 进入 network_init()\r\n"); network_init(ethernet_buf, &default_net_info); printf("[DEBUG] 5. network_init() 成功返回!\r\n"); printf("wizchip UDP example started\r\n"); #endif /* ======================================= */ /* 根据配置模式初始化TX/RX应用层 */ #if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH) /* TX模式初始化 */ rf433_tx_app_init(NULL); rf433_tx_app_start(RF433_DEFAULT_TX_COUNT, RF433_DEFAULT_TX_INTERVAL); #endif #if (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH) /* RX模式初始化 */ rf433_rx_app_init(NULL); rf433_rx_app_start(); #endif /* 打印启动信息 */ printf("\r\n========================================\r\n"); printf("E32-433TBH-SC Application Started\r\n"); printf("System Clock: %d MHz\r\n", SystemCoreClock / 1000000); printf("========================================\r\n"); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ #if (RF433_MODE == RF433_MODE_TX) || (RF433_MODE == RF433_MODE_BOTH) /* TX任务 */ rf433_tx_app_task(); #endif #if (RF433_MODE == RF433_MODE_RX) || (RF433_MODE == RF433_MODE_BOTH) /* RX任务 */ rf433_rx_app_task(); #endif /* W5500 UDP回环任务 */ #if USE_W5500 loopback_udps(SOCKET_ID, ethernet_buf, local_port); #endif // /* 强行拉低 M0 和 M1,叫醒 433 模块处于透传模式! */ // HAL_GPIO_WritePin(M0_GPIO_Port, M0_Pin, GPIO_PIN_RESET); // HAL_GPIO_WritePin(GPIOB, M1_Pin, GPIO_PIN_RESET); // /* ⚠️ 致命修复:网络被注释后,单片机启动太快。 // 433模块从休眠到唤醒至少需要几百毫秒。 // 必须改成 1000 毫秒,给硬件足够的缓冲时间! */ // HAL_Delay(1000); /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* ========================================================== 第一部分:恢复系统的核心驱动引擎 (解决网络和端口不传数据的问题) ========================================================== */ UART2_Print_Task(); // 引擎:负责把串口2的缓存发出去 MultiUART_Task(); // 引擎:负责网络分发、多串口异步发送 (极其重要!) IO_Monitor_Task(); /* 如果你的网络指令需要解析,取消下面两行的注释 */ // CmdRouter_Task(); // CmdParser_Task(); /* ========================================================== 第二部分:网络轮询任务 ========================================================== */ #if USE_W5500 loopback_udps(SOCKET_ID, ethernet_buf, local_port); #endif /* ========================================================== 第三部分:非阻塞无乱码透传 (解决485卡顿漏数据的问题) ========================================================== */ /* 1. 从 433 收到无线数据 -> 发给 485 */ if (u1_rx_len > 0 && (HAL_GetTick() - u1_last_rx_time > 20)) { /* 改用 MultiUART_Send!它会把数据瞬间丢进发送队列,耗时0毫秒,绝不卡顿单片机! */ MultiUART_Send(PORT_UART3, (uint8_t*)u1_rx_buffer, u1_rx_len); /* 顺便发给串口2监控 */ MultiUART_Send(PORT_UART2, (uint8_t*)u1_rx_buffer, u1_rx_len); u1_rx_len = 0; /* 清空缓存 */ } /* 2. 从 485 收到设备数据 -> 通过 433 无线发射 */ if (u3_rx_len > 0 && (HAL_GetTick() - u3_last_rx_time > 20)) { MultiUART_Send(PORT_UART1, (uint8_t*)u3_rx_buffer, u3_rx_len); /* 【可选进阶】:如果你希望网络端(比如电脑上的UDP助手)也能收到485传来的数据, 你可以在这里调用你的网络发送函数,把 u3_rx_buffer 发给网口 */ u3_rx_len = 0; /* 清空缓存 */ } /* USER CODE END WHILE */ } /* USER CODE END 3 */ /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /** * @brief UART接收完成中断回调函数 * @note 处理UART1(RF433)、UART2(调试口)和UART3(RS485)的接收数据 * @param huart: UART句柄指针 * @retval 无 */ /* USER CODE BEGIN 4 */ /* USER CODE BEGIN 4 */ /* USER CODE BEGIN 4 */ /* USER CODE BEGIN 4 */ void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) { if (u1_rx_len < sizeof(u1_rx_buffer)) u1_rx_buffer[u1_rx_len++] = rf433_uart_rx_tmp; u1_last_rx_time = HAL_GetTick(); HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1); } else if (huart->Instance == USART3) { if (u3_rx_len < sizeof(u3_rx_buffer)) u3_rx_buffer[u3_rx_len++] = uart3_rx_byte; u3_last_rx_time = HAL_GetTick(); HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1); } else if (huart->Instance == USART2) { HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1); } } void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { /* 彻底清除所有硬件锁死标志 */ __HAL_UART_CLEAR_OREFLAG(huart); __HAL_UART_CLEAR_NEFLAG(huart); __HAL_UART_CLEAR_FEFLAG(huart); if (huart->Instance == USART1) HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1); else if (huart->Instance == USART3) HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1); else if (huart->Instance == USART2) HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1); } /* USER CODE END 4 */ /* 后面原有的 HAL_UART_TxCpltCallback 保留不动... */ /** * @brief UART发送完成中断回调函数 * @note 处理UART1/UART2/UART3发送完成,触发下一次发送 * @param huart: UART句柄指针 * @retval 无 */ void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) { if (huart->Instance == USART1) { /* 调用多UART路由器的UART1发送完成回调 */ MultiUART_TxCpltCallback(PORT_UART1); } else if (huart->Instance == USART2) { /* 调用UART2打印模块的发送完成回调 */ UART2_Print_TxCpltCallback(); } else if (huart->Instance == USART3) { /* 调用多UART路由器的UART3发送完成回调 */ MultiUART_TxCpltCallback(PORT_UART3); } } /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: usb_printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */