I'm working on a bootloader that loads using CAN messages with the BAM. Once the bootloader starts running, it listens for other messages on CAN that it uses to program the flash memory.
I'm trying to add support that will allow parts of flash to be erased in case the user wants to power cycle and start over, particularly I want to be able to erase the first 8 bytes in flash that can cause a boot failure if the image isn't ready.
I'm using the driver code from:
But any calls I make to the FlashInit_C and the FlashErase_C hang and never return.
I'm using SetLock_C prior to trying to erase, but I still get hangs when I try to erase.
I see similar behavior if I try to just overwrite an address with zeros. From the manual, it sounds like when you program you're changing 1's to 0's and cannot do the reverse. So it seems like I should be able to write 0xA5A5A5A5 for example, then write again with 0x00000000. But on the second write, the FlashProgram_C call just hangs.
Why does it hang? How can I erase?
Thanks for all of the information. All of my code is executing from RAM.
I'm pretty sure I don't have any reads or writes going on when I'm trying to erase block L0 again. Is there something I can check to see if a read/write is going on that is causing the erase to hang?
this will require some debugging. Do you handle all exceptions? You can add just endless loop to all handlers to see if an exception is triggered and to be able to get more details from core registers. Was the MCU reset? Could you check that by oscilloscope? If the MCU was reset, what is the content of SIU_RSR register?
Do you have some interrupts enabled? If yes, did you try to disable them?
Sorry to get back so late. My bootloader code was initializing an MMU after erasing the flash. Something in that initialization was causing a subsequent flash erase to hang. Still not sure exactly what, but I removed the MMU initialization code and no longer had the problem.
Note that I am able to use the FlashErase_C to erase all of flash at the beginning. It works correctly and everything gets set to 1's. But after that, if I try to erase even just the first block using the same routine, it hangs.
How do I erase a second time??
first important point is that Read-While-Write is supported only between partitions. We are not allowed to access partition which is being programmed or erased. The code must be executed from another partition or from RAM.
For more details, see section "22.214.171.124 Read While Write (RWW)" and "Table 13-4. Flash Partitions" in the reference manual. Maybe this is the problem why it "hangs" after second execution of the function.
Regarding overwriting of data in flash: be aware that when a double word is programmed to flash, also ECC syndrome is stored to hidden flash (there are additional 8 bits for each double word). So, overprogramming usually results in double bit ECC error - even if you program the data to all '0'. There's only special case when we can do that. It's not mentioned in all reference manuals but you can find it here, for example:
See section 126.96.36.199 EEPROM emulation.
So, if you overprogram value 0xA5A5A5A5 to 0x0, you will definitely get an ECC error.
This application note could be useful for you: