main.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 "i2c.h"
  22. #include "iwdg.h"
  23. #include "quadspi.h"
  24. #include "usart.h"
  25. #include "gpio.h"
  26. /* Private includes ----------------------------------------------------------*/
  27. /* USER CODE BEGIN Includes */
  28. #include "Int_mpu.h"
  29. #include "Int_iwdg.h"
  30. #include "boot_manager.h"
  31. /* USER CODE END Includes */
  32. /* Private typedef -----------------------------------------------------------*/
  33. /* USER CODE BEGIN PTD */
  34. /* USER CODE END PTD */
  35. /* Private define ------------------------------------------------------------*/
  36. /* USER CODE BEGIN PD */
  37. /* USER CODE END PD */
  38. /* Private macro -------------------------------------------------------------*/
  39. /* USER CODE BEGIN PM */
  40. /* USER CODE END PM */
  41. /* Private variables ---------------------------------------------------------*/
  42. /* USER CODE BEGIN PV */
  43. /* USER CODE END PV */
  44. /* Private function prototypes -----------------------------------------------*/
  45. void SystemClock_Config(void);
  46. static void MPU_Config(void);
  47. /* USER CODE BEGIN PFP */
  48. /* USER CODE END PFP */
  49. /* Private user code ---------------------------------------------------------*/
  50. /* USER CODE BEGIN 0 */
  51. /* Bootloader 裸机运行,无 RTOS */
  52. /* USER CODE END 0 */
  53. /**
  54. * @brief The application entry point.
  55. * @retval int
  56. */
  57. int main(void)
  58. {
  59. /* USER CODE BEGIN 1 */
  60. Int_mpu_init();
  61. /* USER CODE END 1 */
  62. /* MPU Configuration--------------------------------------------------------*/
  63. MPU_Config();
  64. /* Enable the CPU Cache */
  65. /* Enable I-Cache---------------------------------------------------------*/
  66. SCB_EnableICache();
  67. /* Enable D-Cache---------------------------------------------------------*/
  68. SCB_EnableDCache();
  69. /* MCU Configuration--------------------------------------------------------*/
  70. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  71. HAL_Init();
  72. /* USER CODE BEGIN Init */
  73. /* USER CODE END Init */
  74. /* Configure the system clock */
  75. SystemClock_Config();
  76. /* USER CODE BEGIN SysInit */
  77. /* USER CODE END SysInit */
  78. /* Initialize all configured peripherals */
  79. MX_GPIO_Init();
  80. MX_QUADSPI_Init();
  81. MX_USART1_UART_Init();
  82. MX_I2C2_Init();
  83. MX_IWDG1_Init();
  84. /* USER CODE BEGIN 2 */
  85. /* Bootloader 不启动 IWDG(擦写长操作会被狗咬)。App 请求软复位进入升级时
  86. IWDG 已在计数, 由 TIM2 时基中断持续刷新保命 */
  87. boot_manager_init(&huart1);
  88. boot_manager_run(); /* 决策/升级/跳转, 正常不返回 */
  89. /* USER CODE END 2 */
  90. /* Infinite loop */
  91. /* USER CODE BEGIN WHILE */
  92. /* Bootloader 主循环: 后续在此接入 EEPROM 状态机 / YMODEM / NOR→APP 烧写 / Jump APP */
  93. while (1)
  94. {
  95. /* boot_manager_run 正常不返回 */
  96. /* USER CODE END WHILE */
  97. /* USER CODE BEGIN 3 */
  98. }
  99. /* USER CODE END 3 */
  100. }
  101. /**
  102. * @brief System Clock Configuration
  103. * @retval None
  104. */
  105. void SystemClock_Config(void)
  106. {
  107. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  108. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  109. /** Supply configuration update enable
  110. */
  111. HAL_PWREx_ConfigSupply(PWR_LDO_SUPPLY);
  112. /** Configure the main internal regulator output voltage
  113. */
  114. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE0);
  115. while(!__HAL_PWR_GET_FLAG(PWR_FLAG_VOSRDY)) {}
  116. /** Initializes the RCC Oscillators according to the specified parameters
  117. * in the RCC_OscInitTypeDef structure.
  118. */
  119. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_LSI;
  120. RCC_OscInitStruct.HSIState = RCC_HSI_DIV1;
  121. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  122. RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  123. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  124. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  125. RCC_OscInitStruct.PLL.PLLM = 4;
  126. RCC_OscInitStruct.PLL.PLLN = 60;
  127. RCC_OscInitStruct.PLL.PLLP = 2;
  128. RCC_OscInitStruct.PLL.PLLQ = 2;
  129. RCC_OscInitStruct.PLL.PLLR = 2;
  130. RCC_OscInitStruct.PLL.PLLRGE = RCC_PLL1VCIRANGE_3;
  131. RCC_OscInitStruct.PLL.PLLVCOSEL = RCC_PLL1VCOWIDE;
  132. RCC_OscInitStruct.PLL.PLLFRACN = 0;
  133. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  134. {
  135. Error_Handler();
  136. }
  137. /** Initializes the CPU, AHB and APB buses clocks
  138. */
  139. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  140. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2
  141. |RCC_CLOCKTYPE_D3PCLK1|RCC_CLOCKTYPE_D1PCLK1;
  142. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  143. RCC_ClkInitStruct.SYSCLKDivider = RCC_SYSCLK_DIV1;
  144. RCC_ClkInitStruct.AHBCLKDivider = RCC_HCLK_DIV2;
  145. RCC_ClkInitStruct.APB3CLKDivider = RCC_APB3_DIV2;
  146. RCC_ClkInitStruct.APB1CLKDivider = RCC_APB1_DIV2;
  147. RCC_ClkInitStruct.APB2CLKDivider = RCC_APB2_DIV2;
  148. RCC_ClkInitStruct.APB4CLKDivider = RCC_APB4_DIV2;
  149. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  150. {
  151. Error_Handler();
  152. }
  153. }
  154. /* USER CODE BEGIN 4 */
  155. /* USER CODE END 4 */
  156. /* MPU Configuration */
  157. void MPU_Config(void)
  158. {
  159. /* Disables the MPU */
  160. HAL_MPU_Disable();
  161. /* Enables the MPU */
  162. HAL_MPU_Enable(MPU_PRIVILEGED_DEFAULT);
  163. }
  164. /**
  165. * @brief Period elapsed callback in non blocking mode
  166. * @note This function is called when TIM2 interrupt took place, inside
  167. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  168. * a global variable "uwTick" used as application time base.
  169. * @param htim : TIM handle
  170. * @retval None
  171. */
  172. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  173. {
  174. /* USER CODE BEGIN Callback 0 */
  175. /* USER CODE END Callback 0 */
  176. if (htim->Instance == TIM2)
  177. {
  178. HAL_IncTick();
  179. }
  180. /* USER CODE BEGIN Callback 1 */
  181. /* USER CODE END Callback 1 */
  182. }
  183. /**
  184. * @brief This function is executed in case of error occurrence.
  185. * @retval None
  186. */
  187. void Error_Handler(void)
  188. {
  189. /* USER CODE BEGIN Error_Handler_Debug */
  190. /* User can add his own implementation to report the HAL error return state */
  191. __disable_irq();
  192. while (1)
  193. {
  194. }
  195. /* USER CODE END Error_Handler_Debug */
  196. }
  197. #ifdef USE_FULL_ASSERT
  198. /**
  199. * @brief Reports the name of the source file and the source line number
  200. * where the assert_param error has occurred.
  201. * @param file: pointer to the source file name
  202. * @param line: assert_param error line source number
  203. * @retval None
  204. */
  205. void assert_failed(uint8_t *file, uint32_t line)
  206. {
  207. /* USER CODE BEGIN 6 */
  208. /* User can add his own implementation to report the file name and line number,
  209. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  210. /* USER CODE END 6 */
  211. }
  212. #endif /* USE_FULL_ASSERT */