I wrote:
unsigned char *p = (unsigned char *)0x1f000;
*p = 0;
Can you spot the bug I accidentally put in that?
Read through "Table 4-1. CPU Access Type Allowed by Region", and not it says only 32-bit writes are permitted to FLASH. So to fix that:
unsigned long *p = (unsigned long *)0x1f000;
*p = 0;
There's another likely problem. From the Reference Manual:
4.4 Flash
It is not possible to read from a flash block while any command is executing on
that specific flash block.
That means the CPU can't RUN code from a Flash Block while it is erasing or programming.
So where is your code running from? It can't be running from the Flash. The standard way to handle this (that works on all devices) is to write a small block of code that handles the erasing and programming, and then you (in your code) manually copy that block of code to RAM. These functions are called from your code in Flash to do the erase and programming. This also means that all interrupts have to be disabled or you'd get interrupted back into Flash code. This is murder (or impossible) on embedded devices that must respond to external events.
If you're so new to C that you use ASM to write to an address then you probably don't know the tricks for copying functions to RAM and running them from there. I may be wrong of course and you may be doing this already. Are you?
Another way to handle this is to always run all your code from RAM and to treat the Flash as a file system. This only works if you have a lot of (usually external) RAM on the CPU. You don't. Another way is to manipulate the Cache Controller to "preload and lock" the flash programming functions into the Cache. The V1 Coldfire core doesn't have a cache.
Some chips have two Flash controllers so you can be reading code from one block of Flash while changing the other one. This chip isn't one of those. Some Flash controllers can "dead stall" the CPU while they're busy. There's nothing in the manual to say this is one of those, so it probably isn't..
> The code causes a "Protection Violation" flag to appear in the FSTAT register (observed in DeBug)
Possibly because you violated the command timing by READING from the Flash (with the instruction fetch) after telling it to erase. I'd expect a FACCERR in this case, but you're doing something so illegal that anything could happen.
> and the program goes into lala land.
Because it is reading garbage from the locked-out Flash controller and trying to execute it. Then it tries to trap, tries to read the vector from the locked-out flash, gets garbage, jumps to "random" and you get the idea.
> In the .lcf file I have changed the permissions
Does that have any effect on the FPROT register? If it does that's something the development environment might have to handle for you, and I'm guessing it doesn't. I'd guess the LCF permissions only affect what memory has what permissions to the compiler, and what it will let you access in C code.
If you search this forum for the right combination of keywords you'll probably find where this question has been addressed before. Yes, search for "eeprom emulation" (include the quotes). That's what you need.
Another thing to look for. Flash wears out. If your code updates variable too often you may wear it out. The limit is given at 100,000 erases, but if your device is meant to last for 10 years that's only one update per hour! What you really want is mentioned in the manual as "EEPROM emulation applications". That's where you emulate a word-writable EEPROM by writing "update blocks" into a Flash sector and only erase it when it is full of updates. See if you can find anything like that. It would make the part a lot easier to get running if Freescale provided an App Note with working code showing how to do this, but I haven't been able to find one.
Tom