main.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * @attention
  8. *
  9. * Copyright (c) 2025 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 "can.h"
  22. #include "dma.h"
  23. #include "tim.h"
  24. #include "usart.h"
  25. #include "gpio.h"
  26. #include "soft_uart.h"
  27. #include "soft_flash.h"
  28. /* Private includes ----------------------------------------------------------*/
  29. /* USER CODE BEGIN Includes */
  30. /* USER CODE END Includes */
  31. /* Private typedef -----------------------------------------------------------*/
  32. /* USER CODE BEGIN PTD */
  33. /* USER CODE END PTD */
  34. /* Private define ------------------------------------------------------------*/
  35. /* USER CODE BEGIN PD */
  36. /* USER CODE END PD */
  37. /* Private macro -------------------------------------------------------------*/
  38. /* USER CODE BEGIN PM */
  39. /* USER CODE END PM */
  40. /* Private variables ---------------------------------------------------------*/
  41. /* USER CODE BEGIN PV */
  42. /* USER CODE END PV */
  43. /* Private function prototypes -----------------------------------------------*/
  44. void SystemClock_Config(void);
  45. /* USER CODE BEGIN PFP */
  46. /* USER CODE END PFP */
  47. /* Private user code ---------------------------------------------------------*/
  48. /* USER CODE BEGIN 0 */
  49. uint16_t update_flag = 0;
  50. uint32_t led_time = 0;
  51. /* USER CODE END 0 */
  52. /**
  53. * @brief The application entry point.
  54. * @retval int
  55. */
  56. int main(void)
  57. {
  58. /* USER CODE BEGIN 1 */
  59. /* USER CODE END 1 */
  60. /* MCU Configuration--------------------------------------------------------*/
  61. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  62. HAL_Init();
  63. /* USER CODE BEGIN Init */
  64. /* USER CODE END Init */
  65. /* Configure the system clock */
  66. SystemClock_Config();
  67. /* USER CODE BEGIN SysInit */
  68. /* USER CODE END SysInit */
  69. /* Initialize all configured peripherals */
  70. MX_GPIO_Init();
  71. MX_DMA_Init();
  72. //MX_CAN_Init();
  73. MX_USART2_UART_Init();
  74. MX_USART3_UART_Init();
  75. MX_TIM2_Init();
  76. MX_TIM3_Init();
  77. /* USER CODE BEGIN 2 */
  78. SCB->VTOR = FLASH_BASE | 0x0000;
  79. typedef void (*pFunction)(void);
  80. pFunction Jump_To_Application;
  81. uint32_t JumpAddress;
  82. init_usart();
  83. update_flag = flash_read_updata_flag();
  84. flash_ready_SN_and_VERSION();
  85. /* USER CODE END 2 */
  86. /* Infinite loop */
  87. /* USER CODE BEGIN WHILE */
  88. while (1)
  89. {
  90. /* USER CODE END WHILE */
  91. if(update_flag == IAP_FLAG)
  92. {
  93. update_function();
  94. }
  95. if(update_complete == true || update_flag != IAP_FLAG)
  96. {
  97. if ((*(__IO uint32_t *)FLASH_APP_ADDR) <= 0x2000A000 &&
  98. (*(__IO uint32_t *)FLASH_APP_ADDR) >= 0x20000000)
  99. {
  100. JumpAddress = *(__IO uint32_t *)(FLASH_APP_ADDR + 4);
  101. Jump_To_Application = (pFunction)JumpAddress;
  102. HAL_NVIC_DisableIRQ(USART2_IRQn);
  103. __set_MSP(*(__IO uint32_t *)FLASH_APP_ADDR);
  104. Jump_To_Application();
  105. }
  106. /* USER CODE BEGIN 3 */
  107. }
  108. //发送版本SN
  109. send_pmu_version();
  110. //亮红灯
  111. if(HAL_GetTick() - led_time > 1000)
  112. {
  113. HAL_GPIO_TogglePin(GPIOA,GPIO_PIN_5);
  114. led_time = HAL_GetTick();
  115. }
  116. /* USER CODE BEGIN 3 */
  117. }
  118. /* USER CODE END 3 */
  119. }
  120. /**
  121. * @brief System Clock Configuration
  122. * @retval None
  123. */
  124. void SystemClock_Config(void)
  125. {
  126. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  127. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  128. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  129. /** Initializes the RCC Oscillators according to the specified parameters
  130. * in the RCC_OscInitTypeDef structure.
  131. */
  132. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  133. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  134. RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  135. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  136. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  137. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  138. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  139. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  140. {
  141. Error_Handler();
  142. }
  143. /** Initializes the CPU, AHB and APB buses clocks
  144. */
  145. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  146. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  147. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  148. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  149. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  150. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  151. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  152. {
  153. Error_Handler();
  154. }
  155. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2|RCC_PERIPHCLK_USART3;
  156. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  157. PeriphClkInit.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  158. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  159. {
  160. Error_Handler();
  161. }
  162. }
  163. /* USER CODE BEGIN 4 */
  164. /* USER CODE END 4 */
  165. /**
  166. * @brief Period elapsed callback in non blocking mode
  167. * @note This function is called when TIM1 interrupt took place, inside
  168. * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  169. * a global variable "uwTick" used as application time base.
  170. * @param htim : TIM handle
  171. * @retval None
  172. */
  173. void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
  174. {
  175. /* USER CODE BEGIN Callback 0 */
  176. /* USER CODE END Callback 0 */
  177. if (htim->Instance == TIM1)
  178. {
  179. HAL_IncTick();
  180. }
  181. /* USER CODE BEGIN Callback 1 */
  182. /* USER CODE END Callback 1 */
  183. }
  184. /**
  185. * @brief This function is executed in case of error occurrence.
  186. * @retval None
  187. */
  188. void Error_Handler(void)
  189. {
  190. /* USER CODE BEGIN Error_Handler_Debug */
  191. /* User can add his own implementation to report the HAL error return state */
  192. __disable_irq();
  193. while (1)
  194. {
  195. }
  196. /* USER CODE END Error_Handler_Debug */
  197. }
  198. #ifdef USE_FULL_ASSERT
  199. /**
  200. * @brief Reports the name of the source file and the source line number
  201. * where the assert_param error has occurred.
  202. * @param file: pointer to the source file name
  203. * @param line: assert_param error line source number
  204. * @retval None
  205. */
  206. void assert_failed(uint8_t *file, uint32_t line)
  207. {
  208. /* USER CODE BEGIN 6 */
  209. /* User can add his own implementation to report the file name and line number,
  210. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  211. /* USER CODE END 6 */
  212. }
  213. #endif /* USE_FULL_ASSERT */