实验现象及操作说明:
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;
int cnt;
unsigned int counter;
unsigned long int ncounter = ;
unsigned char buffer[];
FIL fil;
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(); /* USER CODE BEGIN 2 */
if(ARM_KEY_STATE == KEY_UP){
goto start;
}
LED_BLUE_ON;
//SD¿¨³õʼ»¯
if(BSP_SD_Init() != MSD_OK){
led_trade();
}
//½âËøÄÚ´æ,²Á³ýÉÈÇø
HAL_FLASH_Unlock();
for(cnt = FLASH_SECTOR_2;cnt < FLASH_SECTOR_11;cnt ++){
FLASH_Erase_Sector(cnt,OB_BOR_LEVEL3);
}
LED_BLUE_OFF;
LED_RED_ON; //f_mount
res = f_mount(&fatfs,"0:",); //¸ø´ÅÅÌ·ÖÅäÅÌ·û
if(res != RES_OK){
LED_RED_OFF;
led_trade();
} //f_open
res = f_open(&fil,"0:/app.bin",FA_READ); //´ò¿ªapp.binÎļþ
if(res != RES_OK){
LED_RED_OFF;
led_trade();
} //f_lseek
res = f_lseek(&fil,); //½«Ö¸ÕëÒƶ¯µ½µÚÒ»¸öλÖÃ
if(res != RES_OK){
LED_RED_OFF;
led_trade();
} //дFlash
while(ncounter < fil.fsize)
{
res = f_read(&fil,buffer,,&counter); //¶ÁÎļþ
if(res != RES_OK){
LED_RED_OFF;
led_trade();
}
p = (unsigned long int *)buffer;
for(k = ; k < counter / ; k++)
{
HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD,APPLICATION_ADDRESS + ncounter,*(p + k));
ncounter += ;
}
}
HAL_FLASH_Lock(); //ARM¸üÐÂÉý¼¶³É¹¦£¬ÂÌÉ«LEDµÆµãÁÁ
LED_BLUE_OFF;
LED_RED_OFF;
LED_GREEN_ON;
/* USER CODE END 2 */ /* Infinite loop */
/* USER CODE BEGIN WHILE */
while ()
{
/* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ }
start:
//²âÊÔÓû§´úÂëÊÇ·ñ´ÓAPPLICATION_ADDRESS±»±à³Ì
if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000){ //ÌøתÖÁÓû§³ÌÐò
JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + );
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/1hr5G4AS 密码:1k74
iCore4链接: