Files
433_STM32/Core/Src/main.c

326 lines
8.9 KiB
C
Raw 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 "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"
#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 ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
static uint8_t uart2_rx_byte = 0;
static uint8_t uart3_rx_byte = 0;
/* 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();
/* USER CODE BEGIN 2 */
/* 初始化应用层模块 */
UART2_Print_Init();
IO_Monitor_Init();
CmdParser_Init();
Relay_Init();
/* 初始化多通信接口统一指令处理系统 */
MultiUART_Init();
CmdRouter_Init();
DebugLog_Init();
/* 启动UART2接收中断 */
HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1);
/* 启动UART3接收中断 - RS485接口 */
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
/* 初始化RF433模块 - 使用默认配置 */
rf433_init(NULL);
/* 启动UART1接收 - 使用rf433_hal中的临时变量 */
HAL_UART_Receive_IT(&huart1, &rf433_uart_rx_tmp, 1);
/* 根据配置模式初始化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 */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
/* 应用层任务处理 */
UART2_Print_Task();
IO_Monitor_Task();
CmdParser_Task();
/* 多通信接口统一指令处理系统任务 */
MultiUART_Task();
CmdRouter_Task();
#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
}
/* 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 无
*/
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
/* 先保存接收到的字节避免被rf433_hal回调覆盖 */
uint8_t rx_byte = rf433_uart_rx_tmp;
/* 调用RF433模块的UART接收回调内部会重启接收 */
rf433_hal_uart_rxcplt_callback();
/* 喂入多UART路由器支持RF433指令接收 */
MultiUART_FeedByte(PORT_UART1, rx_byte);
}
else if (huart->Instance == USART2)
{
/* 喂入指令解析器 - UART2保持原有处理方式 */
CmdParser_FeedByte(uart2_rx_byte, HAL_GetTick());
/* 重新启动接收 */
HAL_UART_Receive_IT(&huart2, &uart2_rx_byte, 1);
}
else if (huart->Instance == USART3)
{
/* 喂入多UART路由器 - RS485接口 */
MultiUART_FeedByte(PORT_UART3, uart3_rx_byte);
/* 重新启动接收 */
HAL_UART_Receive_IT(&huart3, &uart3_rx_byte, 1);
}
}
/**
* @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 */