main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 "Int_sdram.h"
  36. #include "Int_mpu.h"
  37. #include "Int_lcd.h"
  38. #include "FreeRTOS.h"
  39. #include "task.h"
  40. #include "App_main.h"
  41. #include "Com_cpld_usart.h"
  42. #include "Com_cpld_protocol.h"
  43. #include "Com_pc_usart.h"
  44. #include "Com_usb_cdc.h"
  45. #include "Com_parameter.h"
  46. #include "Int_iwdg.h"
  47. /* USER CODE END Includes */
  48. /* Private typedef -----------------------------------------------------------*/
  49. /* USER CODE BEGIN PTD */
  50. /* USER CODE END PTD */
  51. /* Private define ------------------------------------------------------------*/
  52. /* USER CODE BEGIN PD */
  53. /* USER CODE END PD */
  54. /* Private macro -------------------------------------------------------------*/
  55. /* USER CODE BEGIN PM */
  56. /* USER CODE END PM */
  57. /* Private variables ---------------------------------------------------------*/
  58. /* USER CODE BEGIN PV */
  59. /* USER CODE END PV */
  60. /* Private function prototypes -----------------------------------------------*/
  61. void SystemClock_Config(void);
  62. static void MPU_Config(void);
  63. /* USER CODE BEGIN PFP */
  64. /* USER CODE END PFP */
  65. /* Private user code ---------------------------------------------------------*/
  66. /* USER CODE BEGIN 0 */
  67. void vApplicationStackOverflowHook(TaskHandle_t xTask, char *pcTaskName)
  68. {
  69. LOG_PRINTF("[ERR] Task stack overflow");
  70. while (1) {
  71. }
  72. }
  73. /* USER CODE END 0 */
  74. /**
  75. * @brief The application entry point.
  76. * @retval int
  77. */
  78. int main(void)
  79. {
  80. /* USER CODE BEGIN 1 */
  81. Int_mpu_init();
  82. /* USER CODE END 1 */
  83. /* MPU Configuration--------------------------------------------------------*/
  84. MPU_Config();
  85. /* Enable the CPU Cache */
  86. /* Enable I-Cache---------------------------------------------------------*/
  87. SCB_EnableICache();
  88. /* Enable D-Cache---------------------------------------------------------*/
  89. SCB_EnableDCache();
  90. /* MCU Configuration--------------------------------------------------------*/
  91. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  92. HAL_Init();
  93. /* USER CODE BEGIN Init */
  94. /* USER CODE END Init */
  95. /* Configure the system clock */
  96. SystemClock_Config();
  97. /* USER CODE BEGIN SysInit */
  98. /* USER CODE END SysInit */
  99. /* Initialize all configured peripherals */
  100. MX_GPIO_Init();
  101. MX_DMA_Init();
  102. MX_FMC_Init();
  103. MX_QUADSPI_Init();
  104. MX_USART1_UART_Init();
  105. MX_I2C2_Init();
  106. MX_LTDC_Init();
  107. MX_DMA2D_Init();
  108. MX_USB_DEVICE_Init();
  109. MX_USART3_UART_Init();
  110. MX_IWDG1_Init();
  111. /* USER CODE BEGIN 2 */
  112. Int_sdram_init();
  113. Int_lcd_init();
  114. Com_parameter_init();
  115. Com_usb_cdc_init();
  116. Com_cpld_usart_init();
  117. Com_pc_usart_init();
  118. /* 协议层 golden 自检: 失败仅告警不停机 */
  119. if (Com_cpld_self_test() != 0)
  120. {
  121. LOG_PRINTF("[ERR] CPLD protocol self test failed\r\n");
  122. }
  123. Int_iwdg_init(); /* 重装载计数器, 缩短上电到 guardian 首喂的窗口 */
  124. Int_iwdg_feed();
  125. LOG_PRINTF("[OK] System Init\r\n");
  126. /* USER CODE END 2 */
  127. /* Infinite loop */
  128. /* USER CODE BEGIN WHILE */
  129. /* 创建应用任务 */
  130. App_Task_Create();
  131. vTaskStartScheduler();
  132. while (1)
  133. {
  134. LOG_PRINTF("[ERR] RTOS scheduler exited\r\n");
  135. /* USER CODE END WHILE */
  136. /* USER CODE BEGIN 3 */
  137. }
  138. /* USER CODE END 3 */
  139. }
  140. /**
  141. * @brief System Clock Configuration
  142. * @retval None
  143. */
  144. void SystemClock_Config(void)
  145. {
  146. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  147. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  148. RCC_CRSInitTypeDef RCC_CRSInitStruct = {0};
  149. /** Supply configuration update enable
  150. */
  151. HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  152. /** Configure the main internal regulator output voltage
  153. */
  154. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  155. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  156. /** Initializes the RCC Oscillators according to the specified parameters
  157. * in the RCC_OscInitTypeDef structure.
  158. */
  159. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI48|RCC_OSCILLATORTYPE_HSI
  160. |RCC_OSCILLATORTYPE_LSI;
  161. RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  162. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  163. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  164. RCC_OscInitStruct.HSI48State = RCC_HSI48_ON;
  165. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  166. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  167. RCC_OscInitStruct.PLL.PLLM = 4;
  168. RCC_OscInitStruct.PLL.PLLN = 60;
  169. RCC_OscInitStruct.PLL.PLLP = 2;
  170. RCC_OscInitStruct.PLL.PLLQ = 2;
  171. RCC_OscInitStruct.PLL.PLLR = 2;
  172. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  173. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  174. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  175. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  176. {
  177. Error_Handler();
  178. }
  179. /** Initializes the CPU, AHB and APB buses clocks
  180. */
  181. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  182. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  183. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  184. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  185. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  186. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  187. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  188. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  189. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  190. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  191. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. /** Enable the SYSCFG APB clock
  196. */
  197. __HAL_RCC_CRS_CLK_ENABLE();
  198. /** Configures CRS
  199. */
  200. RCC_CRSInitStruct.Prescaler = RCC_CRS_SYNC_DIV1;
  201. RCC_CRSInitStruct.Source = RCC_CRS_SYNC_SOURCE_USB2;
  202. RCC_CRSInitStruct.Polarity = RCC_CRS_SYNC_POLARITY_RISING;
  203. RCC_CRSInitStruct.ReloadValue = __HAL_RCC_CRS_RELOADVALUE_CALCULATE(48000000,1000);
  204. RCC_CRSInitStruct.ErrorLimitValue = 34;
  205. RCC_CRSInitStruct.HSI48CalibrationValue = 32;
  206. HAL_RCCEx_CRSConfig(&RCC_CRSInitStruct);
  207. }
  208. /* USER CODE BEGIN 4 */
  209. /* USER CODE END 4 */
  210. /* MPU Configuration */
  211. void MPU_Config(void)
  212. {
  213. /* Disables the MPU */
  214. HAL_MPU_Disable();
  215. /* Enables the MPU */
  216. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  217. }
  218. /**
  219. * @brief Period elapsed callback in non blocking mode
  220. * @note This function is called when TIM2 interrupt took place, inside
  221. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  222. * a global variable "uwTick" used as application time base.
  223. * @param htim : TIM handle
  224. * @retval None
  225. */
  226. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  227. {
  228. /* USER CODE BEGIN Callback 0 */
  229. /* USER CODE END Callback 0 */
  230. if (htim->Instance == TIM2)
  231. {
  232. HAL_IncTick();
  233. }
  234. /* USER CODE BEGIN Callback 1 */
  235. /* USER CODE END Callback 1 */
  236. }
  237. /**
  238. * @brief This function is executed in case of error occurrence.
  239. * @retval None
  240. */
  241. void Error_Handler(void)
  242. {
  243. /* USER CODE BEGIN Error_Handler_Debug */
  244. /* User can add his own implementation to report the HAL error return state */
  245. __disable_irq();
  246. while (1)
  247. {
  248. }
  249. /* USER CODE END Error_Handler_Debug */
  250. }
  251. #ifdef USE_FULL_ASSERT
  252. /**
  253. * @brief Reports the name of the source file and the source line number
  254. * where the assert_param error has occurred.
  255. * @param file: pointer to the source file name
  256. * @param line: assert_param error line source number
  257. * @retval None
  258. */
  259. void assert_failed(uint8_t *file, uint32_t line)
  260. {
  261. /* USER CODE BEGIN 6 */
  262. /* User can add his own implementation to report the file name and line number,
  263. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  264. /* USER CODE END 6 */
  265. }
  266. #endif /* USE_FULL_ASSERT */