Hi kef!
I'm using MC9S12A256, it has banked memory, indeed. I forgot to mention that I don't touch the BKSEL bits, and let them in 0, so Flash 0 is selected. And the data I'm writting is only to pages $3C and $3D.
Besides, when attempting to program. I calculate the start of the sector to be touch, and how many sectors would be modified. I backup all the data (in RAM) add/modify values, erase the whole sector (from it's begginig address) and the re-program it whit the new data. It's possible that more than one sector need to be erased/re-programed.
My project has a buzzer, which I sound every time an access error ocurrs. I have several "points" where FLASH_update routines are called. Sometimes the same "point" works, other it fails with an access error. The strange thing is that sometimes the error is not thrown. That's what mess me up; and that 's why I know that:
- PPAGE is ok (I've an error with a valid PPAGE)
- I'm not writting to a misaligned word
- I'm "inside" page addresses 0x8000 and 0xCBFF (I don't recall correctly the end value) (Also the error is thrown when trying to program address 0x3C8208)
- I've not protection set FPROT = 0xFF
- I don't have a wrong value in BKSEL, it's always cero.
- As you can see in the routine above, I'm not writting to any registrer other than FSTAT before and after FCMD0
As said before, FCLKDIV is correctly set for a 16Mhz oscillator as noted in the reference manual. Also the bit with mask 0x80 is set.
I've check that before entering the erase rotuines, CCIF is set and also CBEIF.
Then I go stepping and (when fails) I can see that ACCERR is set after writting 0x30 to FSTAT. If it wont fail, then writting to FSTAT clear the flags (which where not set before, so practillay it does nothing)
It sounds to me like an incomplete "cycle" of erase or program, so it thinks that I'm still doing something with the flash and when I try to clear a flag, it throws me an error. But still I can't figure it out.
Any help is appreciatted. Thanks!