6
\$\begingroup\$

As I was going through the differences (basically they are same, but they still have some differences) between flash memory and EEPROM here, I figured out that the flash memory can handle lesser amount of write cycles than the EEPROM.

But, the program I upload to my ATmega328 is written in flash memory. This means I write the flash memory every time I upload a program into the microcontroller. But I think I have never written on the EEPROM.

So I wonder why flash instead of EEPROM?

Going through the datasheet, I figured out that the flash memory has the storage of 32 KB while EEPROM has 1 KB. If this was the reason why my programs are uploaded to flash, why didn't the designers make 32 KB of EEPROM and 1 KB of flash?

The only reason I found to program on flash was the speed. Is this the only reason?

\$\endgroup\$
4
  • 2
    \$\begingroup\$ EEPROM is typically NOR flash, while "Flash" memory is NAND flash. Really the difference is speed/density. EEPROM has an advantage that you don't need to rewrite it in blocks, whereas NAND flash is bit-writable but needs to be block-erased. See electronics.stackexchange.com/questions/69234/… \$\endgroup\$
    – Ron Beyer
    Commented Feb 3, 2020 at 16:02
  • 2
    \$\begingroup\$ Different type of memories imply different tradeoffs (volatility, endurance, speed, sizes, cost, ...), and will be more appropriate for different kind of data (code, configuration data, working data, ...). This answer I wrote a long time ago explains some of the relevant choices, when it comes to choosing where to store various kind of things. This is aimed at solutions for storage extenral to a MCU, but could also apply for the internal storage. \$\endgroup\$
    – dim
    Commented Feb 3, 2020 at 16:15
  • 1
    \$\begingroup\$ Because the ATmega328 can only run code from the flash memory. Instead of asking why the software does it, you should ask why the chip was designed that way. \$\endgroup\$ Commented Feb 4, 2020 at 17:43
  • \$\begingroup\$ electronics.stackexchange.com/questions/479284/… \$\endgroup\$
    – G-aura-V
    Commented Jun 5, 2020 at 15:44

4 Answers 4

7
\$\begingroup\$

EEPROM is normally used to save data between power downs/ups or for backup (in case power goes down).

So if you save some data (e.g. acquired by your sketch) to EEPROM, and the Arduino is powered on again (or not, that does not matter), the data is still in EEPROM and your sketch can continue with the data without losing anything.

And it's a good thing that EEPROM can be written more often than Flash, because once your sketch is finished and you build it in your 'product', you don't need to change it anymore. Only during development you will write to Flash. Maybe it happens a lot, but I doubt you do that many thousands of times (if you do, you need to change your workflow).

But the data on EEPROM might be written daily, hourly or even more (depending on the requirements).

\$\endgroup\$
7
  • \$\begingroup\$ i don't think its difficult to reach the limit of write cycles when programming Arduino. Every time I debug a program and upload it, don't I rewrite the memory spaces again? I used to upload a same program every time I perform debugging. A long program would take around 25 re uploads. Just in case I reach the limit, what would be the solution? \$\endgroup\$
    – G-aura-V
    Commented Feb 3, 2020 at 16:24
  • \$\begingroup\$ What do you mean with 25 re ? 25 times? or 25,000 ? Note that the limit is afaik 100,000 guaranteed but in practice mostly more. (I will add another comment what you mean with 25 re). \$\endgroup\$ Commented Feb 3, 2020 at 16:32
  • \$\begingroup\$ 25 re-uploads. Sorry for the mistake. \$\endgroup\$
    – G-aura-V
    Commented Feb 3, 2020 at 16:36
  • 1
    \$\begingroup\$ No problem, so 25 is not much at all, considering you have at least 100,000. You might run into problems when you do it 100 times per day, and develop for 100 days, or reuse the same Arduino over and over again for 'testing' … But I guess eventually you will build it in some project and leave it there, or buy a new one (100,000 uploads is an Arduino well spent I would say). Or (to save time), program your non-Arduino software on a PC (much easier to debug), and when it's ready (maybe even unit-tested), copy it to your Arduino sketch. \$\endgroup\$ Commented Feb 3, 2020 at 16:40
  • \$\begingroup\$ It is hard to hit the limit, but what if I do(Let's not discuss how)? \$\endgroup\$
    – G-aura-V
    Commented Feb 3, 2020 at 16:44
6
\$\begingroup\$

EEPROM is more expensive per bit than flash, so it's only used where it is needed. Sometimes the EEPROM is left out entirely, and applications that require EEPROM have to use an external chip or use some trickery to make flash pretend to be EEPROM. It doesn't make much sense to compromise 2048 Kbytes of flash for a few bytes of non-volatile memory.

If I recall correctly, there were a few early MCUs that had a small amount of EEPROM for program memory (and no flash), but they could not compete.

In most cases flash will only be programmed once or twice, and having to erase it in large blocks rather than byte-by-byte is not an issue, so the designers will use the least expensive memory that meets their high-volume end-user requirements.

The datasheet states 10,000 times minimum endurance for the flash on the ATmega328p, so that's enough for most practical situations, even development where it may be reprogrammed many times.

● Write/erase cycles: 10,000 flash/100,000 EEPROM

However, note that the data retention time may be reduced by many erase/write cycles, which is a reason for not shipping development units into the field. From the ATSAME70Q21 datasheet:

enter image description here

Earlier MCUs often used OTP (one-time programmable) EPROM (but with no physical window for the 'E' on production chips) for the program memory (and they may have had a bit of EEPROM).

There are a few MCUs that use a different technology for the program memory and can be reprogrammed more times- for example TI's Ferroelectric FRAM technology MCUs.

Market disadvantages of FeRAM are much lower storage densities than flash devices, storage capacity limitations and higher cost. Like DRAM, FeRAM's read process is destructive, necessitating a write-after-read architecture.

FRAM memory has a limited number of lifetime read cycles so even if the higher cost is okay, it's not a panacea.

There certainly disadvantages of flash- it's relatively slow compared to RAM, it's erased in large blocks, so a lot of information has to be re-written to change a single bit, limited number of write cycles, requirement for an on-chip charge pump etc. but the high density and low cost is what makes it the dominant technology.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Lots of things worth learning. Thank you @Spehro Pefhany. \$\endgroup\$
    – G-aura-V
    Commented Feb 3, 2020 at 16:54
4
\$\begingroup\$

The EEPROM is used for things that can change a lot, like storing some user configuration settings so they survive during reset or powerdown. It is not directly sitting on memory bus, but it is accessed via address and data register. Therefore code cannot be executed from it. It can be erased, read and written one byte at a time, and because it is EEPROM it can handle the many erase and write cycles. The page size is only 4 bytes and the hardware handles a single byte write operation as read-modify-write of 4 bytes for you. It is merely a peripheral.

The Flash on the other hand, sits on the address bus so code can be executed from it. Erase can happen on 128 byte blocks. If you want to change one byte, you must read 128 bytes to temporary buffer, change the byte you want, erase the 128 byte page, and write 128 byte page back. And it can only work for less erase/program cycles. So while it can be used for configuration storage and updates, it juat has some downsides. But code execution is fast from Flash.

\$\endgroup\$
3
  • \$\begingroup\$ What does it mean "sitting on memory bus"? \$\endgroup\$
    – G-aura-V
    Commented Feb 3, 2020 at 16:56
  • \$\begingroup\$ It is not directly accessible on any bus, like Flash or SRAM are. It is accessed via few addresses in the peripheral area. Look up the datasheet examples how to access the EEPROM. \$\endgroup\$
    – Justme
    Commented Feb 3, 2020 at 17:30
  • \$\begingroup\$ I figured out that there are three registers dedicated for EEPROM: address register, data register and control register. So my understanding is: in order to access a memory location in EEPROM, I have to specify its address in the address register and provide the data to be written in data register (or accept the data in data register if I am reading from that location). But in case of Flash memory, the address is sent via address bus and data is communicated via data bus. No any registers in the middle. Is my understanding correct? \$\endgroup\$
    – G-aura-V
    Commented Feb 4, 2020 at 15:29
2
\$\begingroup\$

Flash and EEPROM technologies use different physics (Wikipedia explains this in some detail). That explains why flash is denser (and thus cheaper) but has less write cycles.

You can on some chips use Flash from your programs to store information that fits this requirement (like implementing a music player). (And on some ARM-based microcontrollers, there is no EEPROM so you have to do this).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.