实验现象及操作说明:
1、本例程共有两个代码包,APP和IAP,IAP程序功能实现将APP程序升级至STM32中。
2、直接上电或烧写程序将执行升级的APP应用程序。
3、按下按键上电或写程序将进行升级,升级文件.bin文件存入在SD卡中
BIN升级文件产生方法:
1、编译APP工程,产生.hex文件。
2、将.hex文件拖至HEX2BIN.EXE即可产生.bin文件。
int main(void) { /* USER CODE BEGIN 1 */ int k; unsigned int counter; unsigned long int ncounter = 0; unsigned char buffer[1024]; FIL fil; FATFS fatfs; static FRESULT res; unsigned long int *p; /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* 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(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_DMA_Init(); MX_SDIO_SD_Init(); MX_FATFS_Init(); /* USER CODE BEGIN 2 */ LED_BLUE_ON; if(KEY_INPUT){ goto start; } if(State != HAL_SD_STATE_RESET){ led_trade(); } HAL_FLASH_Unlock(); __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR); EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.PageAddress = FLASH_USER_START_ADDR; EraseInitStruct.Banks = FLASH_BANK_1; EraseInitStruct.NbPages = 3; if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK) { led_trade(); } LED_BLUE_OFF; res = f_mount(&fatfs,"0:",1); if(res != RES_OK){ led_trade(); } res = f_open(&fil,"0:/APP.bin",FA_READ); if(res != RES_OK){ led_trade(); } res = f_lseek(&fil,0); if(res != RES_OK){ led_trade(); } while(ncounter < fil.fsize) { red_led_flash(); res = f_read(&fil,buffer,1024,&counter); if(res != RES_OK){ led_trade(); } p = (unsigned long int *)buffer; for(k = 0; k < counter / 4; k++) { HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,APPLICATION_ADDRESS + ncounter,*(p + k)); ncounter += 4; } } HAL_FLASH_Lock(); LED_BLUE_OFF; LED_RED_OFF; LED_GREEN_ON; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } start: if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000){ JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + 4); Jump_To_Application = (pFunction) JumpAddress; __set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS); Jump_To_Application(); }else{ led_trade(); } /* USER CODE END 3 */ }
实验方法及指导书:
链接:http://pan.baidu.com/s/1jIBtFjo 密码:5fl1