/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * Copyright (c) 2026 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 software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "dma.h" #include "i2c.h" #include "spi.h" #include "usart.h" #include "gpio.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ #include #include #include "ch395f.h" #include "gd5f2gq5ue.h" #include "tpafe5160.h" #include "sd2506.h" #include "rs485.h" /* 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 */ /* RS-485 通信实例(UART5: PC12=TX, PD2=RX, PD0=DE) */ rs485_handle_t g_rs485; #define RS485_RX_BUF_SIZE 256 uint8_t g_rs485_rx_buf[RS485_RX_BUF_SIZE]; /* 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_DMA_Init(); MX_USART1_UART_Init(); MX_SPI2_Init(); MX_SPI1_Init(); MX_I2C1_Init(); MX_UART4_Init(); MX_UART5_Init(); MX_USART2_UART_Init(); MX_USART3_UART_Init(); /* USER CODE BEGIN 2 */ HAL_Delay(100); /* CH395F 硬件检测 */ ch395f_status_t ch395f_ret = ch395f_check_exist(); printf("[CH395F] SPI: %s\r\n", (ch395f_ret == CH395F_STATUS_OK) ? "OK" : "FAIL"); printf("[CH395F] version: 0x%02X\r\n", ch395f_get_version()); if (ch395f_ret == CH395F_STATUS_OK) { /* 1. 复位 */ ch395f_reset(); /* 2. 配置网络参数(必须在 INIT 之前,INIT 会锁定协议栈参数) */ uint8_t ip[4] = {192, 168, 1, 100}; uint8_t gw[4] = {192, 168, 1, 1}; uint8_t mask[4] = {255, 255, 255, 0}; ch395f_set_ip_addr(ip); ch395f_set_gwip_addr(gw); ch395f_set_mask_addr(mask); /* 3. 初始化协议栈(锁定 IP/GW/MASK 等参数) */ printf("[CH395F] init: %s\r\n", (ch395f_init() == CH395F_STATUS_OK) ? "OK" : "FAIL"); /* 4. 读取 MAC */ uint8_t mac[6]; ch395f_get_mac_addr(mac); printf("[CH395F] MAC: %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); /* 5. 强制 100M 全双工,建立 PHY 链路 */ ch395f_set_phy(CH395F_PHY_100M_FULL); HAL_Delay(3000); uint8_t phy = ch395f_get_phy_status(); const char *phy_str = "UNKNOWN"; switch (phy) { case CH395F_PHY_DISCONN: phy_str = "DISCONNECT"; break; case CH395F_PHY_10M_FULL: phy_str = "10M FULL"; break; case CH395F_PHY_10M_HALF: phy_str = "10M HALF"; break; case CH395F_PHY_100M_FULL: phy_str = "100M FULL"; break; case CH395F_PHY_100M_HALF: phy_str = "100M HALF"; break; } printf("[CH395F] PHY: %s\r\n", phy_str); } /* GD5F 初始化测试 */ int ret = gd5f2gq5ue_init(); const char *gd5f_str = "UNKNOWN"; switch (ret) { case GD5F_OK: gd5f_str = "OK"; break; case GD5F_ERROR: gd5f_str = "ERROR"; break; case GD5F_BUSY_TIMEOUT: gd5f_str = "BUSY TIMEOUT"; break; case GD5F_ECC_ERROR: gd5f_str = "ECC ERROR"; break; case GD5F_PROGRAM_FAIL: gd5f_str = "PROGRAM FAIL"; break; case GD5F_ERASE_FAIL: gd5f_str = "ERASE FAIL"; break; case GD5F_ID_MISMATCH: gd5f_str = "ID MISMATCH"; break; } printf("[GD5F] init: %s\r\n", gd5f_str); /* TPAFE5160 ADC 测试 */ int tp_ret = tpafe5160_init(); printf("[TPAFE5160] init: %s\r\n", (tp_ret == TPAFE5160_OK) ? "OK" : "TIMEOUT"); /* SD2506 RTC 初始化测试 */ int sd_ret = sd2506_init(); printf("[SD2506] init: %s\r\n", (sd_ret == SD2506_OK) ? "OK" : "I2C ERROR"); if (sd_ret == SD2506_OK) { /* 读取 ID */ uint8_t sd_id[8]; sd2506_get_id(sd_id); printf("[SD2506] ID: %02X%02X%02X%02X%02X%02X%02X%02X\r\n", sd_id[0], sd_id[1], sd_id[2], sd_id[3], sd_id[4], sd_id[5], sd_id[6], sd_id[7]); /* 读取当前时间 */ sd2506_time_t sd_time; sd2506_get_time(&sd_time); printf("[SD2506] time: %04d-%02d-%02d %02d:%02d:%02d (week %d)\r\n", sd_time.year, sd_time.month, sd_time.day, sd_time.hour, sd_time.minute, sd_time.second, sd_time.week); /* 读取温度 */ int8_t sd_temp; sd2506_get_temperature(&sd_temp); printf("[SD2506] temp: %d C\r\n", sd_temp); /* 读取电池电压 */ uint16_t sd_vbat; sd2506_get_battery_voltage(&sd_vbat); printf("[SD2506] battery: %d.%02d V\r\n", sd_vbat / 1000, (sd_vbat % 1000) / 10); } /* RS-485 初始化(UART5 + PD0 方向控制) */ rs485_init(&g_rs485, &huart5, ST_DIR4_GPIO_Port, ST_DIR4_Pin); printf("[RS485] init: OK (UART5, DE=PD0)\r\n"); /* 启动第一次 RS-485 接收 */ rs485_receive_start(&g_rs485, g_rs485_rx_buf, RS485_RX_BUF_SIZE); /* 启动第一次转换(CubeMX 已配置 BUSY EXTI 中断) */ tpafe5160_start_conv_irq(); /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ /* TPAFE5160 中断模式读取 */ if (tpafe5160_data_ready()) { const int16_t *buf = tpafe5160_get_buf(); printf("[TPAFE5160] CH:"); for (int i = 0; i < 8; i++) { printf(" %d", buf[i]); } printf("\r\n"); tpafe5160_clear_ready(); /* 启动下一次转换 */ tpafe5160_start_conv_irq(); } /* 其他任务 */ HAL_GPIO_TogglePin(LED1_GPIO_Port, LED1_Pin); HAL_Delay(500); } /* USER CODE END 3 */ } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_RCC_PWR_CLK_ENABLE(); __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** 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.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLM = 4; RCC_OscInitStruct.PLL.PLLN = 168; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLQ = 4; 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_DIV4; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK) { Error_Handler(); } } /* USER CODE BEGIN 4 */ /* * HAL UART 扩展接收回调(IDLE 空闲帧中断触发) * 当 RS-485 总线空闲超过 1 个字符时间后触发 */ void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t Size) { if (huart->Instance == UART5) { rs485_rx_set_size(&g_rs485, Size); /* TODO: 在此处处理接收到的 RS-485 数据 * g_rs485_rx_buf[0 .. Size-1] 为有效数据 * 例如:rs485_transmit(&g_rs485, g_rs485_rx_buf, Size, 0); */ /* 重新开启接收 */ rs485_receive_start(&g_rs485, g_rs485_rx_buf, RS485_RX_BUF_SIZE); } } /* * HAL UART 错误回调 */ void HAL_UART_ErrorCallback(UART_HandleTypeDef *huart) { if (huart->Instance == UART5) { /* STM32F4 通过读 SR + DR 清除 ORE/NE/FE/PE 错误标志 */ (void)huart->Instance->SR; (void)huart->Instance->DR; /* 重新开启接收 */ rs485_receive_start(&g_rs485, g_rs485_rx_buf, RS485_RX_BUF_SIZE); } } /* 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: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */