main.c 14 KB

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