main.c 8.1 KB

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