AnsweredAssumed Answered

Problem with writing to P-Flash on MC9S12XS256MAL in code

Question asked by STEVE LEWIS on Aug 14, 2014
Latest reply on Aug 14, 2014 by STEVE LEWIS

I am looking for some help please on an application that I am transferring from an old S12 processor to S12X.

I am using Code Warrior for S12(X) version 5.1 build 10221

I am using the P&E Micro Multilink Universal FX for debugging through BDM.


For my product, I need to write and store data in P-Flash. This data file will be updated from time to time in the field.


I read the manual and took a very close look at the sample Bootloader Demo Code that came with Application Note AN4258.

I wrote 2 routines - one to erase a sector and one to program 4 words. This are almost direct copies of the code from AN4258.

I have placed a prgma before these routines specifying that they are to be compiled as position independent code - #pragma CODE_SEG __NEAR_SEG __PIC_SEG BOOT_CODE.

Interestingly the code in AN4258 is not specified as position independent code but when I disassemble it the assembler code is effectively position independent anyway.

My two routines are copied into RAM and run in RAM.

But when I run the code, the processor crashes.


So I attached the debugger to see what was happening, and now it gets weird.

1. As I step through the code, I checked that the compiled code is definitely copied into RAM from FLASH. The program counter definitely points to RAM when the first function is called.

2. So I stepped through the code one instruction at a time and it works! The sector is erased.

3. When I stepped through the Flash program function one instruction at a time, it also works! If I read back the memory afterwards then it is correct.

4. But if I try to run the function in one go, everything crashes and I end up with something like Program Counter = 1 and Stack Pointer = 10C, or PC = 1541, SP = 395B on the next attempt.

5. I then tried setting a breakpoint at different stages in the function to see where it crashes. It seems that I can load the command into the FCCOB register fine. I can start the Flash command by setting the CCIF bit in the FSTAT register (bit 7). But the moment my code gets to the next stage, where it reads back the FSTAT register to determine if the command is complete, everything crashes.


Finally, I made copies of these routines and modified them to use the commands for erasing and programming D-FLASH. I load these into RAM and they work fine?


Has anyone got any suggestions? It is probably something very obvious, but I cannot see it!

Thanks for your help