How to jump to the other image in the internal flash?

cancel
Showing results for 
Search instead for 
Did you mean: 

How to jump to the other image in the internal flash?

Jump to solution
218 Views
Contributor III

Hi,there

I am developing with mcu:MKL17Z128VLH4, I tried KL43 too.

How to jump to another image in the internal flash?

description:

1, burn project A to the mcu, this image locates at 0x00000 address.
2, burn project B to the mcu, this image locates starting at 0x0F000 address for example.
3, when project A run, this project trys to jump to project B image, just like the bootloader in ROM, but failed.

source code:

// Variables
uint32_t runBootloaderAddress;
void (*runBootloader)(void * arg);

// Read the function address from the ROM API tree.
//runBootloaderAddress = **(uint32_t **)(0x1c00001c);
runBootloaderAddress = **(uint32_t **)(0x2001c);
//runBootloaderAddress = **(uint32_t **)(0x1c);
runBootloader = (void (*)(void * arg))runBootloaderAddress;
// Start the bootloader.
runBootloader(NULL);

Maybe I used wrong address? But I put the image B at 0x20000.(0x20000 tried, also failed)
Why failed? or How to jump to another image of the internal Flash?

0 Kudos
1 Solution
79 Views
NXP TechSupport
NXP TechSupport

Hi yilong,

Here is the code copy from mcubootloader. This function jump from bootloader to user application. You can use it directly. Pay attention that the application address is the ResetHandler() address, not the image start address. And you also must give stack pointer address.

static void jump_to_application(uint32_t applicationAddress, uint32_t stackPointer)
{
#if BL_FEATURE_OTFAD_MODULE
quadspi_cache_clear();
oftfad_resume_as_needed();
#endif

shutdown_cleanup(kShutdownType_Shutdown);

// Create the function call to the user application.
// Static variables are needed since changed the stack pointer out from under the compiler
// we need to ensure the values we are using are not stored on the previous stack
static uint32_t s_stackPointer = 0;
s_stackPointer = stackPointer;
static void (*farewellBootloader)(void) = 0;
farewellBootloader = (void (*)(void))applicationAddress;

// Set the VTOR to the application vector table address.
SCB->VTOR = (uint32_t)APP_VECTOR_TABLE;

// Set stack pointers to the application stack pointer.
__set_MSP(s_stackPointer);
__set_PSP(s_stackPointer);

// Jump to the application.
farewellBootloader();
// Dummy fcuntion call, should never go to this fcuntion call
shutdown_cleanup(kShutdownType_Shutdown);
}

 

Regards,

Jing

View solution in original post

5 Replies
79 Views
NXP TechSupport
NXP TechSupport

Hi,

KL17 has ROM bootloader. It can support blhost. Please read chapter 6 in RM to get how to enable ROM bootloader.

Regards,

Jing

0 Kudos
79 Views
Senior Contributor II

The "**" (double stars) indicates a pointer-to-a-pointer.

At the address you have shown, does those locations

point to where the code should really start running?

You probably want something more like this:

typedef void (*functype_t)( void ); /* Function pointer passing void, returning void */

functype_t msg_function = (functype_t) &some_defined_function;

or

functypet msg_function = (functype_t) 0x2000UL; /* May need some other cast(s) */

msg_function(); /* Do the function() */
/* The function may not ever return */

0 Kudos
79 Views
Contributor III

Hi, Bob

Tks for reply!

Your code is perfect and can make the core jump to the destination address, if this address is correct.

But my problem is not how to jump.

I put image A locating at 0x00000000.

I put image B locating at 0x00020000.

When the chip resets, it works from image A. When it received my secret instruction, it is supposed to jump to the image B and work correctly.

But the current status is the core falls into the HardFault_Handler.

So, should I need to call something like deinitialize action before jump?

0 Kudos
80 Views
NXP TechSupport
NXP TechSupport

Hi yilong,

Here is the code copy from mcubootloader. This function jump from bootloader to user application. You can use it directly. Pay attention that the application address is the ResetHandler() address, not the image start address. And you also must give stack pointer address.

static void jump_to_application(uint32_t applicationAddress, uint32_t stackPointer)
{
#if BL_FEATURE_OTFAD_MODULE
quadspi_cache_clear();
oftfad_resume_as_needed();
#endif

shutdown_cleanup(kShutdownType_Shutdown);

// Create the function call to the user application.
// Static variables are needed since changed the stack pointer out from under the compiler
// we need to ensure the values we are using are not stored on the previous stack
static uint32_t s_stackPointer = 0;
s_stackPointer = stackPointer;
static void (*farewellBootloader)(void) = 0;
farewellBootloader = (void (*)(void))applicationAddress;

// Set the VTOR to the application vector table address.
SCB->VTOR = (uint32_t)APP_VECTOR_TABLE;

// Set stack pointers to the application stack pointer.
__set_MSP(s_stackPointer);
__set_PSP(s_stackPointer);

// Jump to the application.
farewellBootloader();
// Dummy fcuntion call, should never go to this fcuntion call
shutdown_cleanup(kShutdownType_Shutdown);
}

 

Regards,

Jing

View solution in original post

79 Views
Contributor III

Hi, Jing

Tks so much for your perfect answer!

I see the similar solution at

Jump to app failing custom bootloader. | NXP Community  https://community.nxp.com/message/1216312#comment-1216426

BTW, why blhost can not support KL17? So we have to solve the OTA feature like this: make our own bootloader( project A), and program the image into the higher address and jump to it.

Do you have a better solution instead of our own loader?

Tks a lot!

0 Kudos