We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
in main() I have two pieces of code - the first one needs to start at the beginning of main and it is very small the second one will be placed just after at the beginnig adddress + 200h can you tell me the directive(s) to use to set this second piece of code to be placed at this specific address after the beginning of main()
thanks for the quick reply
Im building a bootloader and want to make it jump to application after power up always
but...I need to have a predefined jump address where the application code will force a jump to and start the actual bootload operation that resides in the bootloader: get data over usart and write to flash until done when a sw reset will re-start the micro
Im thinking this jump address needs to be in main() (after the jump to application always) but does not need necessarily the case
your help is much appreciated
You are talking about two separate programs that have a separate load region (check your linker user manual!) - a bootloader and an application. What you describe can be achived by a simple function call! You need to separate the programs - each program can be placed then at will - either using a scatter loading file or directly via the IDE (with limited capabilities). There are some samples on the NXP site.
Bad plan. You can't jump to the application and back reliably, because if there's a power failure at just the right point, there may not be any application to jump to --- and thus no way to safely return to the bootloader, either.
Boot loaders pretty much have to work the other way round: on reset, first you check if the conditions triggering the re-flashing are present. If they are, you stay in the bootloader and initiate the flashing session. Second you check if the main program is present with the correct checksum. If not, you stay in the bootloader and wait.
Only if neither of these conditions apply, you run the main application.
When you have a boot loader, you normally (after checking, as already mentioned, that you have a valid application) jump to a well-known start position in the application. But that is not inside any main().
The boot loader will not know what to do to set up the target correctly for the application. So the boot loader must jump to the startup code of the application. The application startup code can potentially be lightened down a bit, if you know what the boot loader have already initialized.
Most people build the application as normal, but linked for another start address. And then let the boot loader check the reset vector at start of application memory region, to figure out where to jump.
In the end, you did ask the wrong question, going directly into details after already having made a couple of incorrect assumptions. Always, always give good background information when asking questions.
Here is the way I start the main application from the bootloader: 1) I write a magic cookie into a variable. 2) I trigger a software reset. 3) Early in the bootloader, I check if the magic cookie is present. If so, I jump to the main application starting address.
This way the main application starts up alomost from the reset state. Very convenient.
But who writes the magic cookie? Can't be the application because it hasn't been started until the boot loader runs.
Would be meaningless for the boot loader to write a magic cookie and then test it.
Magic cookies are good for having an application perform a forced reboot, and with the cookie tell the boot loader to _not_ boot into the application again, but to instead perform a transfer.
For normal operation, it's better to have the application area covered by a CRC-32 or similar, and have the boot loader scan the area to verify if the computed CRC-32 is correct.