main.c 15 KB

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