main.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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 "tim.h"
  23. #include "gpio.h"
  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "flash.h"
  27. #include "UDS.h"
  28. /* USER CODE END Includes */
  29. /* Private typedef -----------------------------------------------------------*/
  30. /* USER CODE BEGIN PTD */
  31. /* USER CODE END PTD */
  32. /* Private define ------------------------------------------------------------*/
  33. /* USER CODE BEGIN PD */
  34. /* USER CODE END PD */
  35. /* Private macro -------------------------------------------------------------*/
  36. /* USER CODE BEGIN PM */
  37. /* USER CODE END PM */
  38. /* Private variables ---------------------------------------------------------*/
  39. /* USER CODE BEGIN PV */
  40. extern uint8_t TxBuf[8];
  41. extern uint8_t ota_start_flag;
  42. extern uint32_t system_time_ms; // 系统运行时间,用于判断上电等待接收升级指令
  43. extern uint8_t app_error_flag;
  44. /* USER CODE END PV */
  45. /* Private function prototypes -----------------------------------------------*/
  46. void SystemClock_Config(void);
  47. /* USER CODE BEGIN PFP */
  48. /* USER CODE END PFP */
  49. /* Private user code ---------------------------------------------------------*/
  50. /* USER CODE BEGIN 0 */
  51. // 定义版本信息并强制放在指定的Flash地址
  52. // 使用section属性将变量放入特定段
  53. typedef struct {
  54. uint16_t hardware_version; // 硬件版本
  55. uint16_t software_version; // boot软件版本
  56. } VersionInfo;
  57. __attribute__((section(".version_info"), used))
  58. const VersionInfo version_info = {
  59. .hardware_version = 0x0100, // 示例:V1.0
  60. .software_version = 0x0100, // 示例:V1.01
  61. };
  62. // 获取版本信息的函数
  63. uint32_t get_app_version(uint8_t soft)
  64. {
  65. uint32_t app1_version = *(volatile uint32_t*)0x0800C188;
  66. uint32_t app2_version = *(volatile uint32_t*)0x08060188;
  67. if(app1_version == 0xFFFFFFFF)
  68. {
  69. app1_version = 0;
  70. }
  71. if(app2_version == 0xFFFFFFFF)
  72. {
  73. app2_version = 0;
  74. }
  75. if(soft == 1)//app1
  76. {
  77. return app1_version;
  78. }
  79. else if(soft == 2)//app2
  80. {
  81. return app2_version;
  82. }
  83. else
  84. {
  85. return 0;
  86. }
  87. }
  88. /* USER CODE END 0 */
  89. /**
  90. * @brief The application entry point.
  91. * @retval int
  92. */
  93. int main(void)
  94. {
  95. /* USER CODE BEGIN 1 */
  96. /* USER CODE END 1 */
  97. /* MCU Configuration--------------------------------------------------------*/
  98. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  99. HAL_Init();
  100. /* USER CODE BEGIN Init */
  101. /* USER CODE END Init */
  102. /* Configure the system clock */
  103. SystemClock_Config();
  104. /* USER CODE BEGIN SysInit */
  105. /* USER CODE END SysInit */
  106. /* Initialize all configured peripherals */
  107. MX_GPIO_Init();
  108. MX_CAN1_Init();
  109. MX_TIM2_Init();
  110. /* USER CODE BEGIN 2 */
  111. HAL_TIM_Base_Start_IT(&htim2); // 启动定时器中断
  112. CAN_Filter_config();
  113. i15765_init();
  114. i15765app_init();
  115. #if 0 //测试抱闸功能
  116. uint8_t brake_r_state = 0;
  117. uint8_t brake_l_state = 0;
  118. brake_r_state = BRAKE_R;
  119. brake_l_state = BRAKE_L;
  120. BRAKE_R_SET_0;
  121. BRAKE_L_SET_0;
  122. brake_r_state = BRAKE_R;
  123. brake_l_state = BRAKE_L;
  124. BRAKE_R_SET_1;
  125. BRAKE_L_SET_1;
  126. brake_r_state = BRAKE_R;
  127. brake_l_state = BRAKE_L;
  128. #endif
  129. // TxBuf[0] = 0x1;
  130. // TxBuf[1] = 0x22;
  131. // TxBuf[2] = 0x33;
  132. // TxBuf[3] = 0x44;
  133. // TxBuf[4] = 0x55;
  134. // TxBuf[5] = 0x66;
  135. // TxBuf[6] = 0x77;
  136. // TxBuf[7] = 0x88;
  137. // CAN_SendData(1, 2, 0x709, TxBuf,8);
  138. #if 1
  139. //只第一次刷程序时执行1次,再屏蔽掉刷一次
  140. if(1 != *(volatile uint8_t*)(CONFIG_IAP_INFO_ADDR))
  141. {
  142. app_status_set(1,true);
  143. }
  144. // if(1 != *(volatile uint8_t*)(CONFIG_IAP_INFO_ADDR+1))//app2无固件
  145. // {
  146. // app1_copy_to_app2();
  147. // app_status_set(2,true);
  148. // }
  149. #endif
  150. uint32_t app1_version = get_app_version(1);
  151. uint32_t app2_version = get_app_version(2);
  152. if(1 == *(volatile uint8_t*)(CONFIG_IAP_INFO_ADDR+1))//APP_B is ok
  153. {
  154. if(app1_version < app2_version)
  155. {
  156. app2_copy_to_app1();
  157. //flash_erase_app(1);
  158. app_status_set(1,true);
  159. }
  160. }
  161. // app1_copy_to_app2();
  162. // app_status_set(1,true);
  163. // app_status_set(2,true);
  164. //VECT_TAB_OFFSET
  165. /* USER CODE END 2 */
  166. /* Infinite loop */
  167. /* USER CODE BEGIN WHILE */
  168. while (1)
  169. {
  170. /* USER CODE END WHILE */
  171. /* USER CODE BEGIN 3 */
  172. check_SID_run();
  173. //if((system_time_ms == WAIT_OTA_TIME_MS)&&(1))//复位后判断是否从APP跳转过来,不是的话直接跳到APP
  174. if(system_time_ms == WAIT_OTA_TIME_MS)
  175. {
  176. //APP_A is ok
  177. if(1 == *(volatile uint8_t*)CONFIG_IAP_INFO_ADDR)
  178. //if(0)
  179. {
  180. system_time_ms = WAIT_OTA_TIME_MS;
  181. jump_to_app(APP1_ADDRESS);
  182. }
  183. else
  184. {
  185. app_error_flag = 1;
  186. }
  187. }
  188. //CAN_SendData(1, 2, 0x709, TxBuf,8);
  189. //HAL_Delay(1000);
  190. }
  191. /* USER CODE END 3 */
  192. }
  193. /**
  194. * @brief System Clock Configuration
  195. * @retval None
  196. */
  197. void SystemClock_Config(void)
  198. {
  199. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  200. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  201. /** Configure the main internal regulator output voltage
  202. */
  203. __HAL_RCC_PWR_CLK_ENABLE();
  204. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  205. /** Initializes the RCC Oscillators according to the specified parameters
  206. * in the RCC_OscInitTypeDef structure.
  207. */
  208. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  209. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  210. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  211. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  212. RCC_OscInitStruct.PLL.PLLM = 4;
  213. RCC_OscInitStruct.PLL.PLLN = 168;
  214. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  215. RCC_OscInitStruct.PLL.PLLQ = 4;
  216. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  217. {
  218. Error_Handler();
  219. }
  220. /** Initializes the CPU, AHB and APB buses clocks
  221. */
  222. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  223. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  224. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  225. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  226. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  227. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  228. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  229. {
  230. Error_Handler();
  231. }
  232. }
  233. /* USER CODE BEGIN 4 */
  234. /* USER CODE END 4 */
  235. /**
  236. * @brief This function is executed in case of error occurrence.
  237. * @retval None
  238. */
  239. void Error_Handler(void)
  240. {
  241. /* USER CODE BEGIN Error_Handler_Debug */
  242. /* User can add his own implementation to report the HAL error return state */
  243. __disable_irq();
  244. while (1)
  245. {
  246. }
  247. /* USER CODE END Error_Handler_Debug */
  248. }
  249. #ifdef USE_FULL_ASSERT
  250. /**
  251. * @brief Reports the name of the source file and the source line number
  252. * where the assert_param error has occurred.
  253. * @param file: pointer to the source file name
  254. * @param line: assert_param error line source number
  255. * @retval None
  256. */
  257. void assert_failed(uint8_t *file, uint32_t line)
  258. {
  259. /* USER CODE BEGIN 6 */
  260. /* User can add his own implementation to report the file name and line number,
  261. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  262. /* USER CODE END 6 */
  263. }
  264. #endif /* USE_FULL_ASSERT */