main.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. /* Private includes ----------------------------------------------------------*/
  22. /* USER CODE BEGIN Includes */
  23. #include "bootloader.h"
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN PTD */
  27. /* USER CODE END PTD */
  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN PD */
  30. /* USER CODE END PD */
  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN PM */
  33. /* USER CODE END PM */
  34. /* Private variables ---------------------------------------------------------*/
  35. TIM_HandleTypeDef htim6;
  36. UART_HandleTypeDef huart1;
  37. /* USER CODE BEGIN PV */
  38. /* USER CODE END PV */
  39. /* Private function prototypes -----------------------------------------------*/
  40. void SystemClock_Config(void);
  41. static void MX_GPIO_Init(void);
  42. static void MX_USART1_UART_Init(void);
  43. static void MX_TIM6_Init(void);
  44. /* USER CODE BEGIN PFP */
  45. /* USER CODE END PFP */
  46. /* Private user code ---------------------------------------------------------*/
  47. /* USER CODE BEGIN 0 */
  48. /* USER CODE END 0 */
  49. /**
  50. * @brief The application entry point.
  51. * @retval int
  52. */
  53. int main(void)
  54. {
  55. /* USER CODE BEGIN 1 */
  56. /* USER CODE END 1 */
  57. /* MCU Configuration--------------------------------------------------------*/
  58. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  59. HAL_Init();
  60. /* USER CODE BEGIN Init */
  61. /* USER CODE END Init */
  62. /* Configure the system clock */
  63. SystemClock_Config();
  64. /* USER CODE BEGIN SysInit */
  65. /* USER CODE END SysInit */
  66. /* Initialize all configured peripherals */
  67. MX_GPIO_Init();
  68. MX_USART1_UART_Init();
  69. MX_TIM6_Init();
  70. /* USER CODE BEGIN 2 */
  71. HAL_TIM_Base_Start_IT(&htim6);
  72. // Check and Enable memory protection
  73. // Rebooting after enabling
  74. //Bootloader_check_protection();
  75. if (Bootloader_flag() == 1) {
  76. Bootloader_flag_reset();
  77. Bootloader_cmd();
  78. }
  79. // Deinit ALL
  80. Bootloader_Deinit_All();
  81. // GoTo APP
  82. Bootloader_jump_to_app();
  83. /* USER CODE END 2 */
  84. /* Infinite loop */
  85. /* USER CODE BEGIN WHILE */
  86. while (1)
  87. {
  88. /* USER CODE END WHILE */
  89. /* USER CODE BEGIN 3 */
  90. }
  91. /* USER CODE END 3 */
  92. }
  93. /**
  94. * @brief System Clock Configuration
  95. * @retval None
  96. */
  97. void SystemClock_Config(void)
  98. {
  99. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  100. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  101. /** Configure the main internal regulator output voltage
  102. */
  103. HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1);
  104. /** Initializes the RCC Oscillators according to the specified parameters
  105. * in the RCC_OscInitTypeDef structure.
  106. */
  107. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  108. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  109. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  110. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  111. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  112. {
  113. Error_Handler();
  114. }
  115. /** Initializes the CPU, AHB and APB buses clocks
  116. */
  117. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  118. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  119. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  120. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  121. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  122. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  123. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  124. {
  125. Error_Handler();
  126. }
  127. }
  128. /**
  129. * @brief TIM6 Initialization Function
  130. * @param None
  131. * @retval None
  132. */
  133. static void MX_TIM6_Init(void)
  134. {
  135. /* USER CODE BEGIN TIM6_Init 0 */
  136. /* USER CODE END TIM6_Init 0 */
  137. TIM_MasterConfigTypeDef sMasterConfig = {0};
  138. /* USER CODE BEGIN TIM6_Init 1 */
  139. /* USER CODE END TIM6_Init 1 */
  140. htim6.Instance = TIM6;
  141. htim6.Init.Prescaler = 160-1;
  142. htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  143. htim6.Init.Period = 10000;
  144. htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  145. if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  146. {
  147. Error_Handler();
  148. }
  149. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  150. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  151. if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  152. {
  153. Error_Handler();
  154. }
  155. /* USER CODE BEGIN TIM6_Init 2 */
  156. /* USER CODE END TIM6_Init 2 */
  157. }
  158. /**
  159. * @brief USART1 Initialization Function
  160. * @param None
  161. * @retval None
  162. */
  163. static void MX_USART1_UART_Init(void)
  164. {
  165. /* USER CODE BEGIN USART1_Init 0 */
  166. /* USER CODE END USART1_Init 0 */
  167. /* USER CODE BEGIN USART1_Init 1 */
  168. /* USER CODE END USART1_Init 1 */
  169. huart1.Instance = USART1;
  170. huart1.Init.BaudRate = 115200;
  171. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  172. huart1.Init.StopBits = UART_STOPBITS_1;
  173. huart1.Init.Parity = UART_PARITY_NONE;
  174. huart1.Init.Mode = UART_MODE_TX_RX;
  175. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  176. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  177. huart1.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
  178. huart1.Init.ClockPrescaler = UART_PRESCALER_DIV1;
  179. huart1.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  180. if (HAL_UART_Init(&huart1) != HAL_OK)
  181. {
  182. Error_Handler();
  183. }
  184. if (HAL_UARTEx_SetTxFifoThreshold(&huart1, UART_TXFIFO_THRESHOLD_1_8) != HAL_OK)
  185. {
  186. Error_Handler();
  187. }
  188. if (HAL_UARTEx_SetRxFifoThreshold(&huart1, UART_RXFIFO_THRESHOLD_1_8) != HAL_OK)
  189. {
  190. Error_Handler();
  191. }
  192. if (HAL_UARTEx_DisableFifoMode(&huart1) != HAL_OK)
  193. {
  194. Error_Handler();
  195. }
  196. /* USER CODE BEGIN USART1_Init 2 */
  197. /* USER CODE END USART1_Init 2 */
  198. }
  199. /**
  200. * @brief GPIO Initialization Function
  201. * @param None
  202. * @retval None
  203. */
  204. static void MX_GPIO_Init(void)
  205. {
  206. GPIO_InitTypeDef GPIO_InitStruct = {0};
  207. /* USER CODE BEGIN MX_GPIO_Init_1 */
  208. /* USER CODE END MX_GPIO_Init_1 */
  209. /* GPIO Ports Clock Enable */
  210. __HAL_RCC_GPIOC_CLK_ENABLE();
  211. __HAL_RCC_GPIOD_CLK_ENABLE();
  212. __HAL_RCC_GPIOB_CLK_ENABLE();
  213. /*Configure GPIO pin Output Level */
  214. HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);
  215. /*Configure GPIO pin : LED_Pin */
  216. GPIO_InitStruct.Pin = LED_Pin;
  217. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  218. GPIO_InitStruct.Pull = GPIO_NOPULL;
  219. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  220. HAL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
  221. /*Configure GPIO pin : KEY_Pin */
  222. GPIO_InitStruct.Pin = KEY_Pin;
  223. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  224. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  225. HAL_GPIO_Init(KEY_GPIO_Port, &GPIO_InitStruct);
  226. /* USER CODE BEGIN MX_GPIO_Init_2 */
  227. /* USER CODE END MX_GPIO_Init_2 */
  228. }
  229. /* USER CODE BEGIN 4 */
  230. /* USER CODE END 4 */
  231. /**
  232. * @brief This function is executed in case of error occurrence.
  233. * @retval None
  234. */
  235. void Error_Handler(void)
  236. {
  237. /* USER CODE BEGIN Error_Handler_Debug */
  238. /* User can add his own implementation to report the HAL error return state */
  239. __disable_irq();
  240. while (1)
  241. {
  242. }
  243. /* USER CODE END Error_Handler_Debug */
  244. }
  245. #ifdef USE_FULL_ASSERT
  246. /**
  247. * @brief Reports the name of the source file and the source line number
  248. * where the assert_param error has occurred.
  249. * @param file: pointer to the source file name
  250. * @param line: assert_param error line source number
  251. * @retval None
  252. */
  253. void assert_failed(uint8_t *file, uint32_t line)
  254. {
  255. /* USER CODE BEGIN 6 */
  256. /* User can add his own implementation to report the file name and line number,
  257. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  258. /* USER CODE END 6 */
  259. }
  260. #endif /* USE_FULL_ASSERT */