flash write in case of microcontrollers

Thread Starter

sharanbr

Joined Apr 13, 2009
82
Hi,

This question is a generic questions on flash write in microcontrollers.
Can anyone answer the following questions please:

1) in case of write to a specific address in the flash, does the SW/FW needs to first check if the data that is being written is same as the current content in that address?
1a) if the new content is such that there is only change from 1 to 0 then it can overwrite
1b) if the new content is such that there is change from 0 to 1 then other steps follow
2) before it can erase the block, does it need to copy the entire block in a temporary location first so that existing content is not lost?
3) once erase is completed then it copies the entire block back to the flash with the modified data. Is this correct?
4) does the SW/FW maintain a map of address to page/block/sector so that it knows what address maps to what page/block/sector?

Thanks in advance
 

BobTPH

Joined Jun 5, 2013
8,664
To write to flash memory you follow the procedure documented in the datasheet. The issues you are asking about are of no concern to the end user.
 

Papabravo

Joined Feb 24, 2006
21,003
Your questions CANNOT be answered in general because there is no standard FLASH implementation. You should assume that each manufacturer's implementation will exhibit differences. That is why the procedures are documented in the datasheet.
 

dl324

Joined Mar 30, 2015
16,688
1) in case of write to a specific address in the flash, does the SW/FW needs to first check if the data that is being written is same as the current content in that address?
There two types of FLASH: NOR and NAND.

This is for discrete FLASH devices, but the implementation in microcontrollers will be similar. Microcontrollers aren't likely to use NAND FLASH because that type is used for high capacity memory.

NOR FLASH have addresses for each byte. All have the ability to program byte-by-byte, but erase is device specific. Some can only erase the entire array, others can erase by sector.

NAND FLASH are blocked devices. They are read and written like hard disk drives.
 

geekoftheweek

Joined Oct 6, 2013
1,183
1) in case of write to a specific address in the flash, does the SW/FW needs to first check if the data that is being written is same as the current content in that address?
1a) if the new content is such that there is only change from 1 to 0 then it can overwrite
1b) if the new content is such that there is change from 0 to 1 then other steps follow
2) before it can erase the block, does it need to copy the entire block in a temporary location first so that existing content is not lost?
3) once erase is completed then it copies the entire block back to the flash with the modified data. Is this correct?
4) does the SW/FW maintain a map of address to page/block/sector so that it knows what address maps to what page/block/sector?
As others have mentioned different microcontrollers are going to be different, but in terms of PIC microcontrollers you are fairly close.

If you were asking about the program flash of PIC family of microcontrollers: (EEPROM memory is a different process)

1 -> Although it could be done to determine if you really need to perform a write to begin with, but it is not exactly necessary to check that the data is different. You could just reprogram it with the same data that is already there.
1a + 1b -> either way you would need to erase it first. Some datasheets specify that you can overwrite existing data a time or two without erasing it first, but it's not exactly recommended as over time it will make the memory unreliable... or unusable in which case you'll either have to flag it as not usable in your linker scripts or get a new part.
2 -> It depends on what you are doing. In my programming I send the new data block from PC to the PIC so the existing data doesn't matter. If you are making changes at run time like storing settings data, or chaning something else in program then yes you would want to copy the block of memory you will need to erase.
3 -> Yes
4 -> With PICs you always erase a block of memory ranging anywhere from 8 bytes all the way up to 256 bytes (that I have worked with so far) and you just use the regular memory address with no mapping needed. Usually you set your table pointer registers to some byte within the block and when you execute a write command it masks the address bits it doesn't need to find the block it's supposed to work with.

If you are working with EEPROM memory on a PIC it is a different story...
You only need to work with one byte at a time.
1 -> Set your table pointer to point to the memory
2 -> Erase the byte
3 -> Write the new value
 

MrChips

Joined Oct 2, 2009
30,488
The implementation depends on the type of memory and how it is organized, as others have already stated.
On some systems, you are able to write one byte at a time. On others, it is done on a page by page basis.

On some memory systems, you can simply overwrite the memory byte or block. On others, all memory cells must first be erased, usually erased to an all ones state. Writing puts zeros in the desired cells.

Having to read and compare each byte before writing takes time. It is more efficient to erase the entire block and then write the new data.
 
Top