Electronic – Is flash memory in MCU’s similar to a hard disk

embeddedflash-memoriesmicrocontrollerram

I have taken a Computer Architecture course in the past that focuses on desktop CPU's and I currently work in embedded systems and I am kind of confused as to which memory corresponds to what.

I am trying to create in my head an analogous image of the one of the desktop CPU's and I can't find something that I am really confident in.

For example both microprocessors and MCU's have RAM memory which is fine.
From my understanding this is the immediate memory that the CPU can access
You could use virtual addresses of course and have access to all the data but in effect you would have to move it to cache and memory first in order for it to be accessible by the CPU.

Does this mean then that flash memory inside an MCU is similar to the hard disk of a computer?

And if so then what is the role of an EEPROM?

Thank you very much for your help.

Best Answer

There are two kinds of flash memory: NOR flash and NAND flash. NOR flash has a parallel interface for address and data, just like normal RAM, and is typically used as part of the memory space of the microcontroller.

So you can typically execute code out of NOR flash. NOR flash is typically contained with the microcontroller chip, and in fact that is how virtually all of the microcontrollers store their code; or it can be external, extending the memory space of the microcontroller. However this require an external address and data bus, which takes a lot of extra pins on the microcontroller -- often replacing pins that are used for I/O ports.

NAND flash, on the other hand, is almost always external to the microcontroller, and is more like a flash drive that you would plug into a USB port on a PC or laptop (although if NAND flash were interfaced directly to the microcontroller, there would be no need for the USB interface).

The most important thing about both types of flash memory, is that reading is much much faster than writing. So you cannot use it as an extension of RAM. Reading is done just by accessing the memory directly. Data can be read one word at a time, where a word is defined as the data width of the microcontroller, typically 8, 16, or 32-bits.

However programming gets a little more complicated. The default state for NOR flash and other non-volatile memories like NAND flash, EEPROMs (Electrically Erasable Programmable ROM)and even EPROMs (Erasable Programmable ROM) is a logic 1. You cannot program 1's into these devices, you can only program 0's. So for example if you have a byte containing 0x0123 and you want to change it to 0x3210, you can't do so directly like writing over a byte in RAM.

Instead, bits in the memory must be erased, which puts them into the default 1 state already mentioned. This can only be done in blocks, not words. On the Microchip PIC32, which I have worked with the most lately, the minimum block size that can be erased is 4096 bytes. So if you wanted to change just one word (32-bits), you would have to read the 4K of memory, erase the block, then write the 4K of memory back to flash but including the new 32-bit value as needed.

This erasing takes a lot of time -- tens or even hundreds of milliseconds. So it is not usable for read/write memory like RAM. The erasing writes 1's to all bits in the block. After the block has been block eraeed, it is then possible to write to it. Writing can only write 0's to the flash, 1's are ignored since the block has already been erased to 1's. One a bit has been changed to a 0, you cannot change it back to a 1 without doing the erase operation.

Most microcontrollers use what is called a programmer to initially program the code into the chip. Once it is programmed, it is ready to execute code. Most programs do not changes the contents of the flash memory used for code, although it is usually possible to do so.

This block erasing of the device goes back to the first EPROMs, which proceeded EEPROMs The code was programmed into chips (like the 16KB 27128) and placed in sockets. These chips had a little window on top which allowed light to shine on the die. When the program had to be changed, the chips were put into a UV eraser for 20 minutes or so, which would erase the entire chip. Then the chip would be programmed with the new program.

Some EEPROMs require erasing of blocks of memory before programming; others allow writing a byte at a time (the EEPROM controller actually erases the byte first and the programs it).

The useable life of non-volatile memory is measured in terms of erase cycles. The disadvantage of NOR is that the number of erase cycles is about 1/10 that of NAND memory. Many versions of the PIC32 only allow the flash memory to be updated 1000 times, way less then the typical 100,000 erase cycles for EEPROMs or NAND.