Electronic – Is it necessary to write all MSP430 FLASH writing code should be run from RAM

flash-memorieslinker-scriptmsp430ram

I am writing some device parameters to non volatile flash memory of msp430f2619 controller.
Sample code shows that function writing to flash should be copied to RAM in initialization section.

But I am using this function to write into dedicated few of the Flash segment. ( Actually I am writing to Information memory segment B )

So is it really necessary to copy this code in RAM.

I am running low on RAM and by freeing this memory for storing flash write function I can use it for my application.

Best Answer

No it's not necessary.

You can write on the information memory while the code with the writing instructions is executed from flash. Not only the information memory, but the whole flash (except the code section which actually writes the flash) can be written from flash.

Actually the flash controller of the MSP430 handles it quite intelligently, while it states in big scary sentences that:

Reading from or writing to flash memory while it is being programmed or erased is prohibited. If CPU execution is required during the write or erase, the code to be executed must be in RAM.

This sounds a bit intimidating at first, but if you read through the whole chapter (highly recommended) you will stumble upon information which tells you, that while the flash controller is busy, a read from the CPU will return 0x3FFF which turns out to be the opcode for JUMP PC (jump to the program counter) which will just stall the CPU until the flash controller is finished doing it's thing. (2)

The block write mode is not supported from flash, so you won't be able to get the fastest write times if you execute your write from flash. I'd say if you just want to write some calibration values or serial numbers, it won't matter much. If you try to implement your own bootloader to flash the whole device, you better run it from RAM and use the block write mode to gain speed.

Just be careful with the erase instruction first to actually delete only the stuff you need to delete, I accidentally lost all calibration information once because I wasn't.