IAP學習, 主要想了解實際上程式碼放在不同的Flash位置如何轉跳且執行。

我的應用程序只做了Pin12, Pin13 LED閃爍來分辨我的 App1 跟 App2的程式碼

App1 程式碼

 int main(void)
{
Delay_Init();
LED_Config();
EXIT_GPIO_Config();
printf("\n--------------------------------------------------\n");
printf("\n STM32F4 IAP APP1 \n");
printf("\n--------------------------------------------------\n"); /* Flash unlock */
FLASH_If_FlashUnlock(); /* Test if User button on the Discovery kit is pressed */
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
/* Check Vector Table: Test if user code is programmed starting from address
"APPLICATION_ADDRESS" */
if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
{
/* Jump to user application */
JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + );
Jump_To_Application = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
Jump_To_Application();
}
} while()
{
GPIOD->ODR |= GPIO_Pin_13;
DelayMs();
GPIOD->ODR &= ~GPIO_Pin_13;
DelayMs();
} }

App2 程式碼

 int main(void)
{
Delay_Init();
LED_Config();
EXIT_GPIO_Config();
printf("\n--------------------------------------------------\n");
printf("\n STM32F4 IAP APP2 \n");
printf("\n--------------------------------------------------\n"); while()
{
GPIOD->ODR |= GPIO_Pin_12;
DelayMs();
GPIOD->ODR &= ~GPIO_Pin_12;
DelayMs();
} }

App1 轉跳 App2 經過一個IO的判斷去決定是否轉跳, 在App2的程式碼內也要指定程式碼放的位置。

分別是 system_stm32f4xx.c 理頭的 VECT_TAB_OFFSET 必須修改成App2的位置

(STM32F4) IAP程式碼實現-LMLPHP

Flash 位置也要改成從0x8010000開始

(STM32F4) IAP程式碼實現-LMLPHP

轉跳代碼

     /* Flash unlock */
FLASH_If_FlashUnlock(); /* Test if User button on the Discovery kit is pressed */
if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_RESET)
{
/* Check Vector Table: Test if user code is programmed starting from address
"APPLICATION_ADDRESS" */
if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)
{
/* Jump to user application */
JumpAddress = *(__IO uint32_t*) (APPLICATION_ADDRESS + );
Jump_To_Application = (pFunction) JumpAddress;
/* Initialize user application's Stack Pointer */
__set_MSP(*(__IO uint32_t*) APPLICATION_ADDRESS);
Jump_To_Application();
}
}

第9行 APPLICATION_ADDRESS必須定義成APP2 Flash 存放的Address, 這段代碼在STM官方網站理頭都下載得到

#define APPLICATION_ADDRESS        (uint32_t)0x08010000
if (((*(__IO uint32_t*)APPLICATION_ADDRESS) & 0x2FFE0000 ) == 0x20000000)

在我的代碼裡頭, 在開機時只需要把 PA0 接到 GND, LED 閃爍的就是P12的LED。

App1 Code 位置從 0x8000000 開使堆疊(Stack)

App2 Code 位置從 0x8010000 開始堆疊(Stack)

05-11 21:53