123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582 |
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2025 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "usb_device.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "usbd_cdc_if.h"
- #include "drv_chipFlash.h"
- #include "drv_IAP.h"
- #include "usbd_cdc.h"
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* USER CODE END PTD */
- /* 函数指针 */
- typedef void (*pFunction)(void);
- pFunction Jump_To_Application;
- /* 用户函数跳转地址 */
- uint32_t JumpAddress;
- uint32_t AppAddress = APPLICATION_ADDRESS;
- uint32_t cur_address = 0;
- uint64_t IF_IAP_FLAG = 0;
- uint32_t recv_finish_time = 0;
- uint32_t program_succeed = 0, program_err = 0, num_repeat = 0, num_err = 0, rec_err = 0, crc_err = 0;
- int16_t RCC_FLAG = 0;
- //uint8_t last_buf[256] = {0};
- uint8_t last_buf[8] = {255}; // 补0;
- /* Private define ------------------------------------------------------------*/
- /* USER CODE BEGIN PD */
- /* USER CODE END PD */
- /* Private macro -------------------------------------------------------------*/
- /* USER CODE BEGIN PM */
- /* USER CODE END PM */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- uint32_t sysclk;
- uint32_t hclk;
- uint32_t pclk1;
- uint32_t pclk2;
-
-
- uint32_t _tick;
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- /* USER CODE END 1 */
- /* MCU Configuration--------------------------------------------------------*/
- //__set_PRIMASK(0);
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* USER CODE BEGIN Init */
- /* USER CODE END Init */
-
- /* Configure the system clock */
- SystemClock_Config();
- _tick = HAL_GetTick();
- if(_tick == 0){
- HAL_InitTick(TICK_INT_PRIORITY);
- }
-
- /* USER CODE BEGIN SysInit */
- /* USER CODE END SysInit */
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
-
- HAL_Delay(1000);
- MX_USB_DEVICE_Init();
- /* USER CODE BEGIN 2 */
-
- sysclk = HAL_RCC_GetSysClockFreq();
- hclk = HAL_RCC_GetHCLKFreq();
- pclk1 = HAL_RCC_GetPCLK1Freq();
- pclk2 = HAL_RCC_GetPCLK2Freq();
- /*--------------------------------处理急救模式----------------------------------*/
- if(recv_aid_flag == true)
- {
- //USBD_Stop(&hUsbDeviceFS);
- //USBD_CDC_Stop(&hUsbDeviceFS);
-
- while(FLASH_ErasePage(APPLICATION_ADDRESS - SECTOR_SIZE, 1)!= 0){
- HAL_Delay(100);
- }
-
- //写下需要升级标志
- unsigned short iap_flag = 0x1001;
- // while(FLASH_WriteMoreData(SET_ADDRESS + 2,(uint64_t*)&iap_flag,2) != 0){
- // HAL_Delay(100);
- // }
-
- //USBD_Start(&hUsbDeviceFS);
- //USBD_CDC_Start(&hUsbDeviceFS);
-
- HAL_Delay(100);
- }
-
- /* 读升级标志 */
- FLASH_ReadMoreData(APPLICATION_ADDRESS - SECTOR_SIZE, (uint64_t *)&IF_IAP_FLAG, 8);
-
- /*--------------------------------处理升级模式----------------------------------*/
- if(IF_IAP_FLAG == (int16_t)0x1001)
- {
- send_mavlink_bootloader_status(BOOTLOADER_STATUS_NEEDTRANSMIT);
- while(1)
- {
- if(recv_iap_flag == INIT)
- {
- if(recv_init_flag == true){
-
- //固件升级初始化阶段
- recv_init_flag = false; //清除初始化标志位
-
- //重新初始化写入地址,不初始化的话会再次点击升级或者电台延时错位写入地址会出错
- AppAddress = APPLICATION_ADDRESS;
-
- pre_iap_bin_num = 0; //前序包计数器
- iap_bin_num = 0; //当前包计数器
-
- send_what = MAVLINK_ACK_ID;
- ack_id = 200;
- id_cont = 0;
-
- program_succeed = 0;
- program_err = 0;
- num_repeat = 0;
- num_err = 0;
- rec_err = 0;
- }
-
- if(recv_erasure_flag == true){
- // __disable_irq();
- //USBD_Stop(&hUsbDeviceFS);
- //USBD_CDC_Stop(&hUsbDeviceFS);
-
- pack_send_flag = false;
-
- erasure_bin_flash();
-
- pack_send_flag = true;
-
- // __enable_irq();
- HAL_Delay(100);
-
-
- //USBD_Start(&hUsbDeviceFS);
- //USBD_CDC_Start(&hUsbDeviceFS);
-
- recv_iap_flag = TRANSMIT;
- recv_erasure_flag = false;
- }
-
- }
- else if(recv_iap_flag == TRANSMIT){
- if(recv_onepack_flag == true){
- if(iap_bin_num == (pre_iap_bin_num + 1)){
-
- if((uint16_t) one_pack_size % 8 != 0){
- // memcpy(last_buf, &usb_rx_buf[8], (uint16_t) one_pack_size);
- // int resultsize = one_pack_size / 8 + 1;
- // if(FLASH_Write_DoubleWord(AppAddress, (uint64_t *)&last_buf[0], (uint16_t)resultsize) == 0)
- // {
- // //编程成功
- // recv_onepack_flag = false;
- // //USBD_Stop(&hUsbDeviceFS);
- // //USBD_CDC_Stop(&hUsbDeviceFS);
- // pack_send_flag = true; //打开接收
- // pre_iap_bin_num++;
- // AppAddress = cur_address;
- // send_what = MAVLINK_ACK_ID;
-
- // ack_id = 201;
- // id_cont = iap_bin_num;
-
- // //记录
- // program_succeed++;
- // }
- ///////////////////////////////////////////////////////////////////////////////////////////
- uint16_t remain_bytes = (uint16_t)one_pack_size % 8;
- uint16_t full_doublewords = (uint16_t)one_pack_size / 8;
-
-
- if (full_doublewords > 0) {
- // 先写满8字节的部分
- if (FLASH_Write_DoubleWord(AppAddress, (uint64_t *)&usb_rx_buf[8], full_doublewords) == 0) {
- AppAddress = cur_address;
- }
- }
- if (remain_bytes > 0) {
- memcpy(last_buf, &usb_rx_buf[8 + full_doublewords * 8], remain_bytes);
- if (FLASH_Write_DoubleWord(AppAddress, (uint64_t *)last_buf, 1) == 0) {
- //编程成功
- recv_onepack_flag = false;
- //USBD_Stop(&hUsbDeviceFS);
- //USBD_CDC_Stop(&hUsbDeviceFS);
- pack_send_flag = true; //打开接收
- pre_iap_bin_num++;
- AppAddress = cur_address;
- send_what = MAVLINK_ACK_ID;
-
- ack_id = 201;
- id_cont = iap_bin_num;
-
- //记录
- program_succeed++;
-
- }
- }
- }
- //if(FLASH_Write(AppAddress, (uint32_t *)&usb_rx_buf[8], (uint32_t)one_pack_size / 4) == 0)
- //if(FLASH_Write_HalfWord(AppAddress, (uint16_t *)&usb_rx_buf[8], (uint16_t)one_pack_size / 2) == 0)
- else if(FLASH_Write_DoubleWord(AppAddress, (uint64_t *)&usb_rx_buf[8], (uint16_t)one_pack_size / 8) == 0)
- {
- //编程成功
- recv_onepack_flag = false;
- //USBD_Stop(&hUsbDeviceFS);
- //USBD_CDC_Stop(&hUsbDeviceFS);
- pack_send_flag = true; //打开接收
- pre_iap_bin_num++;
- AppAddress = cur_address;
- send_what = MAVLINK_ACK_ID;
-
- ack_id = 201;
- id_cont = iap_bin_num;
-
- //记录
- program_succeed++;
- }
- else{
- //编程失败
- recv_onepack_flag = false;
- //USBD_Start(&hUsbDeviceFS);
- //USBD_CDC_Start(&hUsbDeviceFS);
- pack_send_flag = true; //打开接收
- program_err++;
- }
-
- }
- else if(iap_bin_num == pre_iap_bin_num){
-
- recv_onepack_flag = false;
- pack_send_flag = true; //打开接收
- //USBD_Start(&hUsbDeviceFS);
- //USBD_CDC_Start(&hUsbDeviceFS);
- send_what = MAVLINK_ACK_ID;
- ack_id = 201;
- id_cont = iap_bin_num;
-
- //记录
- num_repeat++;
- }
- else{
- recv_onepack_flag = false;
- //USBD_Start(&hUsbDeviceFS);
- //USBD_CDC_Start(&hUsbDeviceFS);
- send_what = MAVLINK_ACK_ID;
- ack_id = 201;
- id_cont = iap_bin_num;
-
- num_err++;
- }
- }
- }
- else if(recv_iap_flag == FINISH){
- if(recv_finish_flag == true){
- recv_finish_flag = false;
- send_what = MAVLINK_ACK_ID;
- ack_id = 202;
- id_cont = iap_bin_num;
-
- recv_finish_time = HAL_GetTick();
- }
-
- //等待1.5s 若无数据认为交互完成
- if(HAL_GetTick() - recv_finish_time > 1500){
-
- send_mavlink_bootloader_status(BOOTLOADER_STATUS_OK);
-
- HAL_FLASH_Lock();
-
- //关闭接收中断
- //USBD_Stop(&hUsbDeviceFS);
- //USBD_CDC_Stop(&hUsbDeviceFS);
-
- HAL_Delay(100);
-
- if((*(volatile uint32_t *)APPLICATION_ADDRESS) <= 0x20020000 && (*(volatile uint32_t *)APPLICATION_ADDRESS) >= 0x20000000)
- {
- Cleanup_Before_Jump();
-
- //APPLICATION_ADDRESS + 4对应的是app中断向量表的第二项,复位地址
- JumpAddress = *(volatile uint32_t *) (APPLICATION_ADDRESS + 4);
- //把地址强转为函数指针
- Jump_To_Application = (pFunction) JumpAddress;
- //设置主函数栈指针
- __set_MSP(*(volatile uint32_t *)APPLICATION_ADDRESS);
-
- /*在RTOS工程,这条语句很重要,设置为特权模式,使用MSP指针 */
- __set_CONTROL(0);
- //重新开启USB
- // USBD_CDC_Start(&hUsbDeviceFS);
- //调用函数,实际失去app复位地址去执行复位操作
- Jump_To_Application();
- }
-
- HAL_Delay(100);
-
- recv_iap_flag = FAIL;
-
- //打开接收中断
- //USBD_Start(&hUsbDeviceFS);
- //USBD_CDC_Start(&hUsbDeviceFS);
- //MX_USB_DEVICE_DeInit();
- }
- }
- else if(recv_iap_flag == FAIL){
- HAL_Delay(600);
- }
-
- switch(send_what){
- case 0:
-
- HAL_Delay(600);
- HAL_Delay(600);
- send_mavlink_bootloader_status(BOOTLOADER_STATUS_NEEDTRANSMIT);
- break;
- case MAVLINK_IAP_HEART:
- break;
-
- case MAVLINK_ACK_ID:
- send_mavlink_ack();
- send_what = MAVLINK_IAP_HEART;
- break;
-
- default:
- break;
- }
- }
- }
- else
- {
- //usb_log_debug("APP start!\r\n");
- //HAL_Delay(500);
- if(((*(volatile uint32_t *)APPLICATION_ADDRESS) & 0x2FFE0000) == 0x20000000)
- {
-
- Cleanup_Before_Jump();
-
- uint32_t a = (*(volatile uint32_t *)APPLICATION_ADDRESS);
- //APPLICATION_ADDRESS + 4对应的是app中断向量表的第二项,复位地址
- JumpAddress = *(volatile uint32_t *)(APPLICATION_ADDRESS + 4);
- //把地址强转为函数指针
- Jump_To_Application = (pFunction) JumpAddress;
- //设置主函数栈指针
- __set_MSP(*(volatile uint32_t *)APPLICATION_ADDRESS);
- /*在RTOS工程,这条语句很重要,设置为特权模式,使用MSP指针 */
- __set_CONTROL(0);
- //调用函数,实际是去app复位函数去执行复位操作
- Jump_To_Application();
-
- }
- HAL_Delay(600);
- }
-
- for(;;)
- {
- static char error_counts = 0;
-
- HAL_Delay(600);
-
- error_counts++;
-
- if(error_counts > 10){
- while(FLASH_ErasePage(APPLICATION_ADDRESS - SECTOR_SIZE, 1)!= 0){
- HAL_Delay(100);
- }
-
- //写下需要升级标志
- unsigned short iap_flag = 0x1001;
- // while(FLASH_WriteMoreData(SET_ADDRESS + 2,(uint64_t*)&iap_flag,2) != 0){
- // HAL_Delay(100);
- // }
-
- HAL_FLASH_Lock();
- HAL_Delay(200);
- HAL_NVIC_SystemReset();
- }
- }
-
- /* USER CODE END 2 */
-
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
-
- /* USER CODE END 3 */
- }
- /**
- * @brief System Clock Configuration
- * @retval None
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- /** Configure the main internal regulator output voltage
- */
- if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
- RCC_OscInitStruct.HSEState = RCC_HSE_ON;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
- RCC_OscInitStruct.PLL.PLLM = 1;
- RCC_OscInitStruct.PLL.PLLN = 20;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV7;
- RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
- RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses clocks
- */
- RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
- |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
- RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /**
- * @brief This function is executed in case of error occurrence.
- * @retval None
- */
- void Error_Handler(void)
- {
- /* USER CODE BEGIN Error_Handler_Debug */
- /* User can add his own implementation to report the HAL error return state */
- __disable_irq();
- while (1)
- {
- }
- /* USER CODE END Error_Handler_Debug */
- }
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t *file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */
- /* 开关全局中断的宏 */
- #define ENABLE_INT() __set_PRIMASK(0) /* 使能全局中断 */
- #define DISABLE_INT() __set_PRIMASK(1) /* 禁止全局中断 */
- void Cleanup_Before_Jump(void){
- uint32_t i = 0;
- //关闭外设
- USBD_DeInit(&hUsbDeviceFS);
- //关闭中断
- DISABLE_INT();
- //重置RCC
- HAL_RCC_DeInit();
- // //重置SysTick
- SysTick->CTRL = 0;
- SysTick->LOAD = 0;
- SysTick->VAL = 0;
- for(i = 0;i < 8; i++){
- NVIC->ICER[i] = 0xFFFFFFFF;
- NVIC->ICPR[i] = 0xFFFFFFFF;
- }
- //重启中断
- ENABLE_INT();
- }
|