Embedded

View Original

Embedded Wednesdays. Thanks for the Memories.


Last week we discussed the various languages that are used to program our processors. This week we are going to look at the various types of memory that are used in computer systems, the tradeoffs, and where each is used.

Computers need two types of memory; one to hold the instructions that it needs to execute (a program), and another to hold the data that are being used in the calculations.

On embedded systems, the program gets stored in memory within the microcontroller chip. This memory does not forget the program when the power gets turned off. This leaves the instructions ready to execute as soon as power is restored.

The program memory is non-volatile. This type of memory is quite easy to make very large and very cheap. The drawback is that it is quite slow and difficult to change. Large sections of the program memory must be erased before it can be reprogrammed with any different values. Plus you can only erase any particular chunk a limited number of times before it fails.

The processor does its calculations using memory that forgets your data when the power is turned off. Why would anybody use a design like that? There are various reasons, but mostly speed and cost.

The data memory is volatile, meaning that it forgets when the power goes away. This type of memory is fast, uses more power, and is more complex so it cannot be made very large cheaply. Each bit of this memory can be changed as many times as you wish, quickly, without affecting any other piece.

Without getting too technical, you’ve seen this type of memory before; Flash thumb drives are similar to our program memory, and those RAM SIMMs that you put into your laptop are similar to our data memory. The big difference is that embedded microcontrollers have the flash and RAM inside of the processor chip.


Some explanation of these names is necessary:

RAM - Random Access Memory. This just means that any piece of memory can be accessed at the same speed as any other. You don’t have to start at the beginning and hang around waiting for your data to come by, you can go directly to the piece you want. The alternative is Sequential Access Memory, like tapes, or disk drives where you have to wait for the disk to rotate around to get at your data.

On a desktop box you might have 16 billion[1] bytes of this stuff. A large microcontroller might have 256 kilobytes, like a 1981 IBM PC.

All of the types of memory that we are going to talk about are technically RAM, but this name is now generally agreed to mean the high speed data memory that forgets when it is turned off.

ROM - Read Only Memory. This just means that the piece of memory can only be read, not written. This is memory that is programmed at a factory and can never be altered. Each piece is very cheap since it has no features to make it writable but you have to order huge amounts of it for it to be cost effective. Just don’t make any mistakes that need to be fixed.

ROM chips are non-volatile, they don’t forget when turned off.

PROM - Programmable ROM. Just like ROM, but the customer can program it - once. You can’t buy these chips anymore, I’m just mentioning it for historical reference.

PROM can be rewritten, so is it actually ROM? Convention now says yes, PROM and all of the types that we are going to talk about are still ROM, because that is how they are being used. Erasing and rewriting is expected to be rare, if ever done, so they are often referred to as ROM.

EPROM - Erasable PROM. Somebody figured out that if you expose the PROM chip to high intensity ultra-violet light, it will reset and you can program it again. These chips had to be removed from the circuit board to be erased and reprogrammed. The sockets always got unreliable, but the ability to be erased was a huge advance.

These chips were expensive because they were encased in a ceramic package with a quartz glass window, then you had to put a sticker over top to keep them from accidentally getting erased.

If you have an old, discarded PC motherboard or printer laying around, look for big ceramic chips with stickers on them and peel off the stickers, then you can see the memory array.

EEPROM - Electrically Erasable PROM. Somebody figured out how to erase a PROM without using the UV light. The chips can now be put into plastic packages, since there is no window, and there is no need for stickers. These chips are pretty complex, and pretty expensive, but you can change their contents while they are soldered into a circuit board.

One of the other benefits of EEPROM is that you don’t have to erase all of it before making a change, you can just erase one byte.

Flash - This is the chip that obsoleted ROM, PROM, EPROM, and EEPROM.

Finally came Flash memory that made a huge amount of non-volatile memory available, cheaply, but you have erase it in big blocks (64K-128K) before you can rewrite it, and the erase cycle takes about 1 second per block[2]. That is a huge amount of time for a computer, but since it rarely gets erased, this is a minor annoyance.

Built a different way, flash can also be erased in really small chunks (1 byte), but then the chip size gets much smaller. This is the replacement for EEPROM.

Another tradeoff with flash is the price versus the number of erase cycles you can perform. Some really inexpensive flash chips have a rated life of fewer than 50 erase/write cycles, you can find these in PC motherboards where the BIOS is updated almost never. More expensive flash chips can withstand 1 million erase/write cycles, these would be used in camera memory cards, and USB sticks. The flash memory that is built into a microcontroller chip has an erase/write endurance somewhere in the middle, more like 10 thousand cycles.

This memory is called flash because the erasing process reminded the makers of a camera flash.

A large microcontroller might have 1 million bytes of flash memory to store your program. The smallest USB thumb drive that my favorite computer shop sells these days contains 8 billion bytes of flash memory, has a small microcontroller inside, and costs less that $8.

FRAM - Ferroelectric RAM. This is a useful oddball. It acts like non-volatile RAM. You can write and erase it as many times as you wish, like RAM, but it doesn’t forget its contents when the power is removed, like ROM. It does have drawbacks though, it is very expensive and very small, like 1,000 bytes.


The various types of memory and some vague metrics.

In embedded systems we have RAM for calculations on our data, but not very much. We have flash for storing our programs.

Some microcontrollers still have small amounts of EEPROM for storing configuration information. Each system gets the same program loaded into flash, but settings and calibration information that is unique to each device will be stored in EEPROM. Each setting can be changed without erasing all of the settings, so they may be changed while the system is running without affecting the system.

FRAM can be used for settings and calibration, like EEPROM, but if you need to keep a count of things, or keep track of accumulated time, that is where FRAM shines. Since FRAM can be erased and written basically infinitely, you can keep track of how many seconds the system has been running. With EEPROM or flash, you would soon wear out the piece of memory that holds the counter.

Flash being updated once per second would start to fail in about 2 weeks. At that same rate, FRAM would start to fail in 31 thousand years, theoretically.

Many embedded systems will have a flash memory chip elsewhere on the circuit board, wired to the processor. These can be used for storing large amounts of data, like pictures that will be put on a display, or log files that track the history of the device. This is very much like a small disk drive, but with no moving parts.


When dealing with microcontrollers, the amount of memory available will influence your choice of languages that you can use, and how you write your programs.

The amount of memory on a typical microcontroller is still measured in kilobytes or small megabytes. If you want to use Java, you have some problems. Java is a language that is run by a Java Virtual Machine or JVM on your processor. You will need enough RAM and ROM to run the JVM as well as your program. You will have to find a processor that has a JVM available, or you can work with Oracle to get one running on your processor.  A better choice would be C, C++, or Ada, at least until the chip manufacturers give us processors with more memory and a JVM.

In embedded systems where every resistor and hole in the circuit board count, a large microcontroller would have about 1MB for program storage and 128KB of RAM. If you can get the program to fit in 256K of code and 64K of RAM, you might be able to switch to the next smaller, cheaper, processor. Every penny not spent on a product is a penny in the owner’s pocket.

Processors are produced with varying amounts of memory (some say that all of the processors in a family are identical and it is only the amount of correctly working memory that varies). The more memory you need, the more you pay for the processor, and the larger the package that the manufacturer puts the chip into.

So your program is not infinitely thin. If your program gets too big and you need more flash or RAM, you may have to pay for the larger package, jettison features from your program, and/or spend a lot of time making everything fit in the existing processor.

If you can, opt for a larger processor for version 1 of a product or any hobby project; you do not want the hassle of making everything fit in something too small.

 

 

 

[1] I’m not going to bother with the gigabyte versus gibibyte issue. If this is a big technical mistake for you, you really don’t need to be reading this article. This is for beginners.

[2] Block size and erase times vary with device specifications. Please check your manufacturer’s data sheet for specifics.


This post is part of a series. Please see the other posts here.