Files
433_STM32/Core/Src/main.c

485 lines
15 KiB
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* 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;
static volatile uint32_t u3_ignore_until = 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
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* === 1. 恢复系统的核心驱动引擎 === */
UART2_Print_Task();
MultiUART_Task();
IO_Monitor_Task();
/* === 2. 网络轮询任务 === */
#if USE_W5500
loopback_udps(SOCKET_ID, ethernet_buf, local_port);
#endif
/* === 3. 极速无乱码透传 === */
/* === 方案 A从 433 收到无线数据 -> 给上位机解析 === */
if (u1_rx_len > 0 && (HAL_GetTick() - u1_last_rx_time > 20))
{
static uint8_t temp_buf1[256];
__disable_irq();
uint16_t len = u1_rx_len;
memcpy(temp_buf1, (uint8_t*)u1_rx_buffer, len);
u1_rx_len = 0;
__enable_irq();
/* 🚀 终极上位机协议:[0xAA帧头] + [设备ID] + [来源接口] + [数据长度] + [纯净数据] */
if (len >= 2 && temp_buf1[0] == 0xAA)
{
uint8_t sender_id = temp_buf1[1];
uint16_t payload_len = len - 2;
uint8_t* payload_data = &temp_buf1[2];
/* 默认 0x03 为 RS485 透传数据 */
uint8_t source_type = 0x03;
/* 判断是不是 $DI 标签 */
if (payload_len >= 3 && payload_data[0] == '$' && payload_data[1] == 'D' && payload_data[2] == 'I')
{
source_type = 0x01; /* 也是 DI 口数据,但不砍标签了,直接全发过去 */
}
/* 判断是不是网口 [NET] 标签 */
else if (payload_len >= 5 && payload_data[0] == '[' && payload_data[1] == 'N' && payload_data[2] == 'E' && payload_data[3] == 'T' && payload_data[4] == ']')
{
source_type = 0x02; /* 0x02 代表是 网络口 收到的数据 */
payload_data += 5; /* 砍掉 "[NET]" 标签 */
payload_len -= 5;
}
/* ========================================================== */
if (payload_len > 0) {
/* 重新组装终极协议帧 */
uint8_t upper_buf[260];
upper_buf[0] = 0xAA; // Byte 0: 魔法帧头
upper_buf[1] = sender_id; // Byte 1: 发送设备 ID
upper_buf[2] = source_type; // Byte 2: 数据来源 (1=DI, 2=NET, 3=485)
upper_buf[3] = (uint8_t)payload_len; // Byte 3: 真实数据长度
memcpy(&upper_buf[4], payload_data, payload_len); // Byte 4~末尾: 绝对纯净的数据
/* 发送给 485 和电脑上位机 */
MultiUART_Send(PORT_UART3, upper_buf, payload_len + 4);
MultiUART_Send(PORT_UART2, upper_buf, payload_len + 4);
}
}
else
{
/* 普通无帧头干扰数据,按原样透传 */
MultiUART_Send(PORT_UART3, temp_buf1, len);
MultiUART_Send(PORT_UART2, temp_buf1, len);
}
/* === 方案 B从 485 收到设备数据 -> 穿上包装(附加ID) -> 通过 433 无线发射 === */
if (u3_rx_len > 0 && (HAL_GetTick() - u3_last_rx_time > 20))
{
static uint8_t temp_buf3[512];
__disable_irq();
uint16_t len = u3_rx_len;
memcpy(temp_buf3, (uint8_t*)u3_rx_buffer, len);
u3_rx_len = 0;
__enable_irq();
/* 🚀 制作“快递包”:在最前面加上 [0xAA] 和 [本机ID] */
static uint8_t rf_tx_buf[515];
rf_tx_buf[0] = 0xAA; // 贴上魔法帧头
rf_tx_buf[1] = MY_DEVICE_ID; // 贴上本机的身份证号
memcpy(&rf_tx_buf[2], temp_buf3, len); // 把 485 收到的真实数据塞进后面
/* 把带有身份证的完整包裹,通过 433 发射到空气中 */
MultiUART_Send(PORT_UART1, rf_tx_buf, len + 2);
}
/* USER CODE END WHILE */
}
}
}
/* 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 */
/* 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 (HAL_GetTick() >= u3_ignore_until) {
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 */