main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2026 STMicroelectronics.
  10. * All rights reserved.
  11. *
  12. * This software is licensed under terms that can be found in the LICENSE file
  13. * in the root directory of this software component.
  14. * If no LICENSE file comes with this software, it is provided AS-IS.
  15. *
  16. ******************************************************************************
  17. */
  18. /* USER CODE END Header */
  19. /* Includes ------------------------------------------------------------------*/
  20. #include "main.h"
  21. #include "dma.h"
  22. #include "dma2d.h"
  23. #include "i2c.h"
  24. #include "iwdg.h"
  25. #include "ltdc.h"
  26. #include "quadspi.h"
  27. #include "usart.h"
  28. #include "usb_device.h"
  29. #include "gpio.h"
  30. #include "fmc.h"
  31. /* Private includes ----------------------------------------------------------*/
  32. /* USER CODE BEGIN Includes */
  33. #include "stdio.h"
  34. #include "string.h"
  35. #include "boot_layout.h"
  36. #include "Int_sdram.h"
  37. #include "Int_mpu.h"
  38. #include "Int_lcd.h"
  39. #include "FreeRTOS.h"
  40. #include "task.h"
  41. #include "App_main.h"
  42. #include "Mid_cpld_usart.h"
  43. #include "Com_cpld_protocol.h"
  44. #include "Mid_pc_usart.h"
  45. #include "Com_log.h"
  46. #include "Com_usb_cdc.h"
  47. #include "Com_parameter.h"
  48. #include "Int_iwdg.h"
  49. #include "stm32h7xx_it.h" /* it_usart1_send_str */
  50. /* USER CODE END Includes */
  51. /* Private typedef -----------------------------------------------------------*/
  52. /* USER CODE BEGIN PTD */
  53. /* USER CODE END PTD */
  54. /* Private define ------------------------------------------------------------*/
  55. /* USER CODE BEGIN PD */
  56. /* USER CODE END PD */
  57. /* Private macro -------------------------------------------------------------*/
  58. /* USER CODE BEGIN PM */
  59. /* USER CODE END PM */
  60. /* Private variables ---------------------------------------------------------*/
  61. /* USER CODE BEGIN PV */
  62. /* USER CODE END PV */
  63. /* Private function prototypes -----------------------------------------------*/
  64. void SystemClock_Config(void);
  65. static void MPU_Config(void);
  66. /* USER CODE BEGIN PFP */
  67. /* USER CODE END PFP */
  68. /* Private user code ---------------------------------------------------------*/
  69. /* USER CODE BEGIN 0 */
  70. void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
  71. {
  72. SELFTEST_PRINTF("[ERR] Task stack overflow");
  73. while (1) {
  74. }
  75. }
  76. /* BL 遗留运行态清理
  77. * 根因:BL 的 HAL_RCC_DeInit() 内部会调 HAL_InitTick() 把 TIM2 重新打开,且执行在
  78. * BL 停 TIM2 之后 ⇒ 跳转时 TIM2 仍处于 CEN=1 / UIE=1 / UIF 每 ~1ms 置位。
  79. * 引爆点:App 的 HAL_InitTick() 是"先 EnableIRQ(TIM2_IRQn)、后赋 htim2.Instance",
  80. * NVIC 一放开就被已挂起的中断打断, 此时 Instance==NULL ⇒ 读不到标志 ⇒ 永远清不掉
  81. * 真正的 UIF ⇒ NVIC pending 反复置位 ⇒ 无限中断风暴,main 静默停在 HAL_InitTick。
  82. * 故须在 __enable_irq() 之前把 TIM2 按回未启动态。 */
  83. static void App_clean_bl_leftover(void)
  84. {
  85. __HAL_RCC_TIM2_CLK_ENABLE(); /* 先给时钟,否则寄存器读写无效 */
  86. TIM2->CR1 = 0U; /* CEN=0:停止计数 */
  87. TIM2->DIER = 0U; /* UIE=0:关更新中断 */
  88. TIM2->SR = 0U; /* 清全部标志 */
  89. NVIC_ClearPendingIRQ(TIM2_IRQn);
  90. NVIC_DisableIRQ(TIM2_IRQn);
  91. }
  92. /* USER CODE END 0 */
  93. /**
  94. * @brief The application entry point.
  95. * @retval int
  96. */
  97. int main(void)
  98. {
  99. /* USER CODE BEGIN 1 */
  100. __HAL_RCC_RTC_CLK_ENABLE(); /* BKP1R 需 APB4 的 RTCAPB 时钟 */
  101. App_clean_bl_leftover();
  102. __enable_irq(); /* BL 关中断跳入的兜底,冷启动无副作用 */
  103. Int_mpu_init();
  104. /* USER CODE END 1 */
  105. /* MPU Configuration--------------------------------------------------------*/
  106. MPU_Config();
  107. /* Enable the CPU Cache */
  108. /* Enable I-Cache---------------------------------------------------------*/
  109. SCB_EnableICache();
  110. /* Enable D-Cache---------------------------------------------------------*/
  111. SCB_EnableDCache();
  112. /* MCU Configuration--------------------------------------------------------*/
  113. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  114. HAL_Init();
  115. /* USER CODE BEGIN Init */
  116. /* USER CODE END Init */
  117. /* Configure the system clock */
  118. SystemClock_Config();
  119. /* USER CODE BEGIN SysInit */
  120. /* USER CODE END SysInit */
  121. /* Initialize all configured peripherals */
  122. MX_GPIO_Init();
  123. MX_DMA_Init();
  124. MX_FMC_Init();
  125. MX_QUADSPI_Init();
  126. MX_USART1_UART_Init();
  127. MX_I2C2_Init();
  128. MX_LTDC_Init();
  129. MX_DMA2D_Init();
  130. MX_USB_DEVICE_Init();
  131. MX_USART3_UART_Init();
  132. MX_IWDG1_Init();
  133. /* USER CODE BEGIN 2 */
  134. /* 固件版本: 无条件打印, 且放在 main 的第一条,上位机 OTA 一收到它就能与包内
  135. * 版本对照, 不必等确认任务起来。取 g_app_version (唯一真源, 见 App_main.c),
  136. * 走 SELFTEST_PRINTF 以绕开 COM_LOG_ENABLE; 此刻日志环未建, 自动退化为阻塞直发。
  137. */
  138. SELFTEST_PRINTF("[INFO] app_ver=%u.%u.%u.%u\r\n",
  139. (unsigned)(g_app_version >> 24), (unsigned)((g_app_version >> 16) & 0xFFU),
  140. (unsigned)((g_app_version >> 8) & 0xFFU), (unsigned)(g_app_version & 0xFFU));
  141. Int_sdram_init();
  142. Int_lcd_init();
  143. Com_parameter_init();
  144. Com_usb_cdc_init();
  145. Mid_cpld_usart_init();
  146. Mid_pc_usart_init();
  147. /* 协议层 golden 自检: 失败仅告警不停机 */
  148. if (Com_cpld_self_test() != 0)
  149. {
  150. SELFTEST_PRINTF("[ERR] CPLD protocol self test failed\r\n");
  151. }
  152. Int_iwdg_init(); /* 重装载计数器, 缩短上电到 guardian 首喂的窗口 */
  153. Int_iwdg_feed();
  154. SELFTEST_PRINTF("[OK] System Init\r\n");
  155. /* USER CODE END 2 */
  156. /* Infinite loop */
  157. /* USER CODE BEGIN WHILE */
  158. /* 创建应用任务 */
  159. App_Task_Create();
  160. vTaskStartScheduler();
  161. while (1)
  162. {
  163. SELFTEST_PRINTF("[ERR] RTOS scheduler exited\r\n");
  164. /* USER CODE END WHILE */
  165. /* USER CODE BEGIN 3 */
  166. }
  167. /* USER CODE END 3 */
  168. }
  169. /**
  170. * @brief System Clock Configuration
  171. * @retval None
  172. */
  173. void SystemClock_Config(void)
  174. {
  175. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  176. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  177. RCC_CRSInitTypeDef RCC_CRSInitStruct = {0};
  178. /** Supply configuration update enable
  179. */
  180. HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  181. /** Configure the main internal regulator output voltage
  182. */
  183. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  184. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  185. /** Initializes the RCC Oscillators according to the specified parameters
  186. * in the RCC_OscInitTypeDef structure.
  187. */
  188. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI
  189. |RCC_OSCILLATORTYPE_LSI;
  190. RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  191. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  192. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  193. RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  194. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  195. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  196. RCC_OscInitStruct.PLL.PLLM = 4;
  197. RCC_OscInitStruct.PLL.PLLN = 60;
  198. RCC_OscInitStruct.PLL.PLLP = 2;
  199. RCC_OscInitStruct.PLL.PLLQ = 2;
  200. RCC_OscInitStruct.PLL.PLLR = 2;
  201. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  202. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  203. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  204. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  205. {
  206. Error_Handler();
  207. }
  208. /** Initializes the CPU, AHB and APB buses clocks
  209. */
  210. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  211. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  212. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  213. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  214. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  215. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  216. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  217. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  218. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  219. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  220. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  221. {
  222. Error_Handler();
  223. }
  224. /** Enable the SYSCFG APB clock
  225. */
  226. __HAL_RCC_CRS_CLK_ENABLE();
  227. /** Configures CRS
  228. */
  229. RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1;
  230. RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB2;
  231. RCC_CRSInitStruct.Polarity = RCC_CRS_SYNC_POLARITY_RISING;
  232. RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000,1000);
  233. RCC_CRSInitStruct.ErrorLimitValue = 34;
  234. RCC_CRSInitStruct.HSI48CalibrationValue = 32;
  235. HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct);
  236. }
  237. /* USER CODE BEGIN 4 */
  238. /* USER CODE END 4 */
  239. /* MPU Configuration */
  240. void MPU_Config(void)
  241. {
  242. /* Disables the MPU */
  243. HAL_MPU_Disable();
  244. /* Enables the MPU */
  245. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  246. }
  247. /**
  248. * @brief Period elapsed callback in non blocking mode
  249. * @note This function is called when TIM2 interrupt took place, inside
  250. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  251. * a global variable "uwTick" used as application time base.
  252. * @param htim : TIM handle
  253. * @retval None
  254. */
  255. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  256. {
  257. /* USER CODE BEGIN Callback 0 */
  258. /* USER CODE END Callback 0 */
  259. if (htim->Instance == TIM2)
  260. {
  261. HAL_IncTick();
  262. }
  263. /* USER CODE BEGIN Callback 1 */
  264. /* USER CODE END Callback 1 */
  265. }
  266. /**
  267. * @brief This function is executed in case of error occurrence.
  268. * @retval None
  269. */
  270. void Error_Handler(void)
  271. {
  272. /* USER CODE BEGIN Error_Handler_Debug */
  273. it_usart1_send_str("[ERR] Error_Handler\r\n"); /* 现场区分 Error_Handler / HardFault */
  274. __disable_irq();
  275. while (1)
  276. {
  277. volatile uint32_t dly = 0;
  278. HAL_GPIO_TogglePin(LED0_GPIO_Port, LED0_Pin);
  279. for (dly = 0; dly < 800000U; dly++)
  280. {
  281. __NOP();
  282. }
  283. }
  284. /* USER CODE END Error_Handler_Debug */
  285. }
  286. #ifdef USE_FULL_ASSERT
  287. /**
  288. * @brief Reports the name of the source file and the source line number
  289. * where the assert_param error has occurred.
  290. * @param file: pointer to the source file name
  291. * @param line: assert_param error line source number
  292. * @retval None
  293. */
  294. void assert_failed(uint8_t *file, uint32_t line)
  295. {
  296. /* USER CODE BEGIN 6 */
  297. /* User can add his own implementation to report the file name and line number,
  298. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  299. /* USER CODE END 6 */
  300. }
  301. #endif /* USE_FULL_ASSERT */