【iCore1S 雙核心板_ARM】例程十九:SD_IAP_ARM實驗——更新升級STM32

XiaomaGee發表於2017-10-26

實驗現象及操作說明:

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

相關文章