main.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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 "usb_device.h"
  22. #include "gpio.h"
  23. /* Private includes ----------------------------------------------------------*/
  24. /* USER CODE BEGIN Includes */
  25. #include "usbd_cdc_if.h"
  26. #include "drv_chipFlash.h"
  27. #include "drv_IAP.h"
  28. #include "usbd_cdc.h"
  29. /* USER CODE END Includes */
  30. /* Private typedef -----------------------------------------------------------*/
  31. /* USER CODE BEGIN PTD */
  32. /* USER CODE END PTD */
  33. /* 函数指针 */
  34. typedef void (*pFunction)(void);
  35. pFunction Jump_To_Application;
  36. /* 用户函数跳转地址 */
  37. uint32_t JumpAddress;
  38. uint32_t AppAddress = APPLICATION_ADDRESS;
  39. uint32_t cur_address = 0;
  40. uint64_t IF_IAP_FLAG = 0;
  41. uint32_t recv_finish_time = 0;
  42. uint32_t program_succeed = 0, program_err = 0, num_repeat = 0, num_err = 0, rec_err = 0, crc_err = 0;
  43. int16_t RCC_FLAG = 0;
  44. //uint8_t last_buf[256] = {0};
  45. uint8_t last_buf[8] = {255}; // 补0;
  46. /* Private define ------------------------------------------------------------*/
  47. /* USER CODE BEGIN PD */
  48. /* USER CODE END PD */
  49. /* Private macro -------------------------------------------------------------*/
  50. /* USER CODE BEGIN PM */
  51. /* USER CODE END PM */
  52. /* Private variables ---------------------------------------------------------*/
  53. /* USER CODE BEGIN PV */
  54. /* USER CODE END PV */
  55. /* Private function prototypes -----------------------------------------------*/
  56. void SystemClock_Config(void);
  57. /* USER CODE BEGIN PFP */
  58. /* USER CODE END PFP */
  59. /* Private user code ---------------------------------------------------------*/
  60. /* USER CODE BEGIN 0 */
  61. /* USER CODE END 0 */
  62. /**
  63. * @brief The application entry point.
  64. * @retval int
  65. */
  66. uint32_t sysclk;
  67. uint32_t hclk;
  68. uint32_t pclk1;
  69. uint32_t pclk2;
  70. uint32_t _tick;
  71. int main(void)
  72. {
  73. /* USER CODE BEGIN 1 */
  74. /* USER CODE END 1 */
  75. /* MCU Configuration--------------------------------------------------------*/
  76. //__set_PRIMASK(0);
  77. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  78. HAL_Init();
  79. /* USER CODE BEGIN Init */
  80. /* USER CODE END Init */
  81. /* Configure the system clock */
  82. SystemClock_Config();
  83. _tick = HAL_GetTick();
  84. if(_tick == 0){
  85. HAL_InitTick(TICK_INT_PRIORITY);
  86. }
  87. /* USER CODE BEGIN SysInit */
  88. /* USER CODE END SysInit */
  89. /* Initialize all configured peripherals */
  90. MX_GPIO_Init();
  91. HAL_Delay(1000);
  92. MX_USB_DEVICE_Init();
  93. /* USER CODE BEGIN 2 */
  94. sysclk = HAL_RCC_GetSysClockFreq();
  95. hclk = HAL_RCC_GetHCLKFreq();
  96. pclk1 = HAL_RCC_GetPCLK1Freq();
  97. pclk2 = HAL_RCC_GetPCLK2Freq();
  98. /*--------------------------------处理急救模式----------------------------------*/
  99. if(recv_aid_flag == true)
  100. {
  101. //USBD_Stop(&hUsbDeviceFS);
  102. //USBD_CDC_Stop(&hUsbDeviceFS);
  103. while(FLASH_ErasePage(APPLICATION_ADDRESS - SECTOR_SIZE, 1)!= 0){
  104. HAL_Delay(100);
  105. }
  106. //写下需要升级标志
  107. unsigned short iap_flag = 0x1001;
  108. // while(FLASH_WriteMoreData(SET_ADDRESS + 2,(uint64_t*)&iap_flag,2) != 0){
  109. // HAL_Delay(100);
  110. // }
  111. //USBD_Start(&hUsbDeviceFS);
  112. //USBD_CDC_Start(&hUsbDeviceFS);
  113. HAL_Delay(100);
  114. }
  115. /* 读升级标志 */
  116. FLASH_ReadMoreData(APPLICATION_ADDRESS - SECTOR_SIZE, (uint64_t *)&IF_IAP_FLAG, 8);
  117. /*--------------------------------处理升级模式----------------------------------*/
  118. if(IF_IAP_FLAG == (int16_t)0x1001)
  119. {
  120. send_mavlink_bootloader_status(BOOTLOADER_STATUS_NEEDTRANSMIT);
  121. while(1)
  122. {
  123. if(recv_iap_flag == INIT)
  124. {
  125. if(recv_init_flag == true){
  126. //固件升级初始化阶段
  127. recv_init_flag = false; //清除初始化标志位
  128. //重新初始化写入地址,不初始化的话会再次点击升级或者电台延时错位写入地址会出错
  129. AppAddress = APPLICATION_ADDRESS;
  130. pre_iap_bin_num = 0; //前序包计数器
  131. iap_bin_num = 0; //当前包计数器
  132. send_what = MAVLINK_ACK_ID;
  133. ack_id = 200;
  134. id_cont = 0;
  135. program_succeed = 0;
  136. program_err = 0;
  137. num_repeat = 0;
  138. num_err = 0;
  139. rec_err = 0;
  140. }
  141. if(recv_erasure_flag == true){
  142. // __disable_irq();
  143. //USBD_Stop(&hUsbDeviceFS);
  144. //USBD_CDC_Stop(&hUsbDeviceFS);
  145. pack_send_flag = false;
  146. erasure_bin_flash();
  147. pack_send_flag = true;
  148. // __enable_irq();
  149. HAL_Delay(100);
  150. //USBD_Start(&hUsbDeviceFS);
  151. //USBD_CDC_Start(&hUsbDeviceFS);
  152. recv_iap_flag = TRANSMIT;
  153. recv_erasure_flag = false;
  154. }
  155. }
  156. else if(recv_iap_flag == TRANSMIT){
  157. if(recv_onepack_flag == true){
  158. if(iap_bin_num == (pre_iap_bin_num + 1)){
  159. if((uint16_t) one_pack_size % 8 != 0){
  160. // memcpy(last_buf, &usb_rx_buf[8], (uint16_t) one_pack_size);
  161. // int resultsize = one_pack_size / 8 + 1;
  162. // if(FLASH_Write_DoubleWord(AppAddress, (uint64_t *)&last_buf[0], (uint16_t)resultsize) == 0)
  163. // {
  164. // //编程成功
  165. // recv_onepack_flag = false;
  166. // //USBD_Stop(&hUsbDeviceFS);
  167. // //USBD_CDC_Stop(&hUsbDeviceFS);
  168. // pack_send_flag = true; //打开接收
  169. // pre_iap_bin_num++;
  170. // AppAddress = cur_address;
  171. // send_what = MAVLINK_ACK_ID;
  172. // ack_id = 201;
  173. // id_cont = iap_bin_num;
  174. // //记录
  175. // program_succeed++;
  176. // }
  177. ///////////////////////////////////////////////////////////////////////////////////////////
  178. uint16_t remain_bytes = (uint16_t)one_pack_size % 8;
  179. uint16_t full_doublewords = (uint16_t)one_pack_size / 8;
  180. if (full_doublewords > 0) {
  181. // 先写满8字节的部分
  182. if (FLASH_Write_DoubleWord(AppAddress, (uint64_t *)&usb_rx_buf[8], full_doublewords) == 0) {
  183. AppAddress = cur_address;
  184. }
  185. }
  186. if (remain_bytes > 0) {
  187. memcpy(last_buf, &usb_rx_buf[8 + full_doublewords * 8], remain_bytes);
  188. if (FLASH_Write_DoubleWord(AppAddress, (uint64_t *)last_buf, 1) == 0) {
  189. //编程成功
  190. recv_onepack_flag = false;
  191. //USBD_Stop(&hUsbDeviceFS);
  192. //USBD_CDC_Stop(&hUsbDeviceFS);
  193. pack_send_flag = true; //打开接收
  194. pre_iap_bin_num++;
  195. AppAddress = cur_address;
  196. send_what = MAVLINK_ACK_ID;
  197. ack_id = 201;
  198. id_cont = iap_bin_num;
  199. //记录
  200. program_succeed++;
  201. }
  202. }
  203. }
  204. //if(FLASH_Write(AppAddress, (uint32_t *)&usb_rx_buf[8], (uint32_t)one_pack_size / 4) == 0)
  205. //if(FLASH_Write_HalfWord(AppAddress, (uint16_t *)&usb_rx_buf[8], (uint16_t)one_pack_size / 2) == 0)
  206. else if(FLASH_Write_DoubleWord(AppAddress, (uint64_t *)&usb_rx_buf[8], (uint16_t)one_pack_size / 8) == 0)
  207. {
  208. //编程成功
  209. recv_onepack_flag = false;
  210. //USBD_Stop(&hUsbDeviceFS);
  211. //USBD_CDC_Stop(&hUsbDeviceFS);
  212. pack_send_flag = true; //打开接收
  213. pre_iap_bin_num++;
  214. AppAddress = cur_address;
  215. send_what = MAVLINK_ACK_ID;
  216. ack_id = 201;
  217. id_cont = iap_bin_num;
  218. //记录
  219. program_succeed++;
  220. }
  221. else{
  222. //编程失败
  223. recv_onepack_flag = false;
  224. //USBD_Start(&hUsbDeviceFS);
  225. //USBD_CDC_Start(&hUsbDeviceFS);
  226. pack_send_flag = true; //打开接收
  227. program_err++;
  228. }
  229. }
  230. else if(iap_bin_num == pre_iap_bin_num){
  231. recv_onepack_flag = false;
  232. pack_send_flag = true; //打开接收
  233. //USBD_Start(&hUsbDeviceFS);
  234. //USBD_CDC_Start(&hUsbDeviceFS);
  235. send_what = MAVLINK_ACK_ID;
  236. ack_id = 201;
  237. id_cont = iap_bin_num;
  238. //记录
  239. num_repeat++;
  240. }
  241. else{
  242. recv_onepack_flag = false;
  243. //USBD_Start(&hUsbDeviceFS);
  244. //USBD_CDC_Start(&hUsbDeviceFS);
  245. send_what = MAVLINK_ACK_ID;
  246. ack_id = 201;
  247. id_cont = iap_bin_num;
  248. num_err++;
  249. }
  250. }
  251. }
  252. else if(recv_iap_flag == FINISH){
  253. if(recv_finish_flag == true){
  254. recv_finish_flag = false;
  255. send_what = MAVLINK_ACK_ID;
  256. ack_id = 202;
  257. id_cont = iap_bin_num;
  258. recv_finish_time = HAL_GetTick();
  259. }
  260. //等待1.5s 若无数据认为交互完成
  261. if(HAL_GetTick() - recv_finish_time > 1500){
  262. send_mavlink_bootloader_status(BOOTLOADER_STATUS_OK);
  263. HAL_FLASH_Lock();
  264. //关闭接收中断
  265. //USBD_Stop(&hUsbDeviceFS);
  266. //USBD_CDC_Stop(&hUsbDeviceFS);
  267. HAL_Delay(100);
  268. if((*(volatile uint32_t *)APPLICATION_ADDRESS) <= 0x20020000 && (*(volatile uint32_t *)APPLICATION_ADDRESS) >= 0x20000000)
  269. {
  270. Cleanup_Before_Jump();
  271. //APPLICATION_ADDRESS + 4对应的是app中断向量表的第二项,复位地址
  272. JumpAddress = *(volatile uint32_t *) (APPLICATION_ADDRESS + 4);
  273. //把地址强转为函数指针
  274. Jump_To_Application = (pFunction) JumpAddress;
  275. //设置主函数栈指针
  276. __set_MSP(*(volatile uint32_t *)APPLICATION_ADDRESS);
  277. /*在RTOS工程,这条语句很重要,设置为特权模式,使用MSP指针 */
  278. __set_CONTROL(0);
  279. //重新开启USB
  280. // USBD_CDC_Start(&hUsbDeviceFS);
  281. //调用函数,实际失去app复位地址去执行复位操作
  282. Jump_To_Application();
  283. }
  284. HAL_Delay(100);
  285. recv_iap_flag = FAIL;
  286. //打开接收中断
  287. //USBD_Start(&hUsbDeviceFS);
  288. //USBD_CDC_Start(&hUsbDeviceFS);
  289. //MX_USB_DEVICE_DeInit();
  290. }
  291. }
  292. else if(recv_iap_flag == FAIL){
  293. HAL_Delay(600);
  294. }
  295. switch(send_what){
  296. case 0:
  297. HAL_Delay(600);
  298. HAL_Delay(600);
  299. send_mavlink_bootloader_status(BOOTLOADER_STATUS_NEEDTRANSMIT);
  300. break;
  301. case MAVLINK_IAP_HEART:
  302. break;
  303. case MAVLINK_ACK_ID:
  304. send_mavlink_ack();
  305. send_what = MAVLINK_IAP_HEART;
  306. break;
  307. default:
  308. break;
  309. }
  310. }
  311. }
  312. else
  313. {
  314. //usb_log_debug("APP start!\r\n");
  315. //HAL_Delay(500);
  316. if(((*(volatile uint32_t *)APPLICATION_ADDRESS) & 0x2FFE0000) == 0x20000000)
  317. {
  318. Cleanup_Before_Jump();
  319. uint32_t a = (*(volatile uint32_t *)APPLICATION_ADDRESS);
  320. //APPLICATION_ADDRESS + 4对应的是app中断向量表的第二项,复位地址
  321. JumpAddress = *(volatile uint32_t *)(APPLICATION_ADDRESS + 4);
  322. //把地址强转为函数指针
  323. Jump_To_Application = (pFunction) JumpAddress;
  324. //设置主函数栈指针
  325. __set_MSP(*(volatile uint32_t *)APPLICATION_ADDRESS);
  326. /*在RTOS工程,这条语句很重要,设置为特权模式,使用MSP指针 */
  327. __set_CONTROL(0);
  328. //调用函数,实际是去app复位函数去执行复位操作
  329. Jump_To_Application();
  330. }
  331. HAL_Delay(600);
  332. }
  333. for(;;)
  334. {
  335. static char error_counts = 0;
  336. HAL_Delay(600);
  337. error_counts++;
  338. if(error_counts > 10){
  339. while(FLASH_ErasePage(APPLICATION_ADDRESS - SECTOR_SIZE, 1)!= 0){
  340. HAL_Delay(100);
  341. }
  342. //写下需要升级标志
  343. unsigned short iap_flag = 0x1001;
  344. // while(FLASH_WriteMoreData(SET_ADDRESS + 2,(uint64_t*)&iap_flag,2) != 0){
  345. // HAL_Delay(100);
  346. // }
  347. HAL_FLASH_Lock();
  348. HAL_Delay(200);
  349. HAL_NVIC_SystemReset();
  350. }
  351. }
  352. /* USER CODE END 2 */
  353. /* Infinite loop */
  354. /* USER CODE BEGIN WHILE */
  355. /* USER CODE END 3 */
  356. }
  357. /**
  358. * @brief System Clock Configuration
  359. * @retval None
  360. */
  361. void SystemClock_Config(void)
  362. {
  363. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  364. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  365. /** Configure the main internal regulator output voltage
  366. */
  367. if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
  368. {
  369. Error_Handler();
  370. }
  371. /** Initializes the RCC Oscillators according to the specified parameters
  372. * in the RCC_OscInitTypeDef structure.
  373. */
  374. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  375. RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  376. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  377. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  378. RCC_OscInitStruct.PLL.PLLM = 1;
  379. RCC_OscInitStruct.PLL.PLLN = 20;
  380. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
  381. RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
  382. RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
  383. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  384. {
  385. Error_Handler();
  386. }
  387. /** Initializes the CPU, AHB and APB buses clocks
  388. */
  389. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  390. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  391. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  392. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  393. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  394. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  395. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
  396. {
  397. Error_Handler();
  398. }
  399. }
  400. /* USER CODE BEGIN 4 */
  401. /* USER CODE END 4 */
  402. /**
  403. * @brief This function is executed in case of error occurrence.
  404. * @retval None
  405. */
  406. void Error_Handler(void)
  407. {
  408. /* USER CODE BEGIN Error_Handler_Debug */
  409. /* User can add his own implementation to report the HAL error return state */
  410. __disable_irq();
  411. while (1)
  412. {
  413. }
  414. /* USER CODE END Error_Handler_Debug */
  415. }
  416. #ifdef USE_FULL_ASSERT
  417. /**
  418. * @brief Reports the name of the source file and the source line number
  419. * where the assert_param error has occurred.
  420. * @param file: pointer to the source file name
  421. * @param line: assert_param error line source number
  422. * @retval None
  423. */
  424. void assert_failed(uint8_t *file, uint32_t line)
  425. {
  426. /* USER CODE BEGIN 6 */
  427. /* User can add his own implementation to report the file name and line number,
  428. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  429. /* USER CODE END 6 */
  430. }
  431. #endif /* USE_FULL_ASSERT */
  432. /* 开关全局中断的宏 */
  433. #define ENABLE_INT() __set_PRIMASK(0) /* 使能全局中断 */
  434. #define DISABLE_INT() __set_PRIMASK(1) /* 禁止全局中断 */
  435. void Cleanup_Before_Jump(void){
  436. uint32_t i = 0;
  437. //关闭外设
  438. USBD_DeInit(&hUsbDeviceFS);
  439. //关闭中断
  440. DISABLE_INT();
  441. //重置RCC
  442. HAL_RCC_DeInit();
  443. // //重置SysTick
  444. SysTick->CTRL = 0;
  445. SysTick->LOAD = 0;
  446. SysTick->VAL = 0;
  447. for(i = 0;i < 8; i++){
  448. NVIC->ICER[i] = 0xFFFFFFFF;
  449. NVIC->ICPR[i] = 0xFFFFFFFF;
  450. }
  451. //重启中断
  452. ENABLE_INT();
  453. }