Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Microcontrollers - FLASH vs SRAM

Status
Not open for further replies.

shaiko

Advanced Member level 5
Joined
Aug 20, 2011
Messages
2,644
Helped
303
Reputation
608
Reaction score
297
Trophy points
1,363
Activity points
18,302
It maybe that my impression is erroneousness - but it seems like almost all MCU's have more FLASH than SRAM (ignoring those that use an external FLASH). If I'm correct, please explain the motivation behind this practice. Detailed answers are very welcome.
 

I'm not sure why you find it strange.
The point of the flash is to store the program of the microcontroller so that it is maintained after the mcu is powered down.
What would the alternative be, to load the program from an external eeprom?
 

I don't find it stange...just thinking.
Isn't the program copied from the program memory (FLASH) to the data memory (SRAM) in order to run faster and be able to manipulate the variables?
 

It depends on the architecture and the programmer choices.
In some models the RAM stores only variables and in others the user can select function to run from RAM for higher speed but the majority of the code runs from flash.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
the majority of the code runs from flash
I assume that only the data that doesn't have to be changed runs from FLASH. Correct ?
Can you give an example for a piece of code that runs 100% from FLASH and uses no RAM resources ?
 

Flash is cheaper than RAM in a micro-controller EDIT: , but the opposite may be true for a computer, such as a PC.

Flash retains information after power failure, which is a handy way to not have to reload your code every time you power on.

Most micro-controller code (not all) runs 100% from flash. I cannot think of any useful program that would not require at least a little RAM, but perhaps it is theoretically possible (if pointless).

What point are you trying to make?


As for your post #3, I think you are describing a typical microprocessor, not a typical micro-controller.
 
Last edited:
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
What point are you trying to make?
None. I'm just asking.
I don't really understand the concept of "runs from FLASH".
As far as I understand, data it copied from FLASH to RAM in order to be able to change...
 

Flash is usually read-only at run time (writing can be done, but is very slow), but for code, this is not usually a problem. Self-modifying code is rare in the embedded world.

Running code from RAM is rare for micro-controllers, but the norm for microprocessors.

You may want to run code from RAM if RAM is faster (which is not always the case), or to self-modify, or to load from external sources.

You can copy information from ROM to RAM to make make it possible to change it, or load values straight into RAM (maybe serial input would go straight to RAM) without getting the data from ROM first.

All of the above statements are qualified by "As Far As I Know" ..... I could easily be mistaken.
 

The program code (firmware) doesn't change during execution, you can only reprogram the firmware of parts of it using a bootloader or some other special mode.
What changes during execution are just the variable values (excluding constants) and that is why they are copied to ram.
 

But any data that is to be manipulated has to be copied from ROM to RAM, from there to the CPU's internal registers and back to RAM when the manipulation is complete - is this correct ?
 

But any data that is to be manipulated has to be copied from ROM to RAM
No, this is one way to use RAM, but there are other ways to use RAM.

Data can come straight from serial port to RAM, from external ports to RAM, can be initialised to zeros without copying from ROM/Flash, can be received from ADC to RAM. RAM can be (and usually is) used for a stack.

RAM is just memory that can be written or read. It does not always have to get its contents from ROM/Flash, though that is one option.

RAM may hold a graphic image (.bmp picture maybe) that could be copied from ROM, such as a still photograph, or could be calculated, like in a video game. ....or a mix of both.
 
Last edited:
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Can you give an example for a piece of code that runs 100% from FLASH and uses no RAM resources ?
If you are using C, there is no way to run a program without the use of RAM space. C allocates RAM space for stack and heap. To be honest, I never tried to zero this space, but it is completely pointless in a real world program.

I don't really understand the concept of "runs from FLASH".
As far as I understand, data it copied from FLASH to RAM in order to be able to change...
Maybe you need to understand a little better the concept of RAM and FLASH. Traditionally, flash is a memory that you can read data from, but not write data during runtime. You must erase the whole memory and rewrite it from the beggining. Actually you can write data during runtime, but this would be accomplished via bootloader, a complex and slow process. RAM doesn't meet that restriction, you can simply write data during runtime. There are other differences as well, but having this in mind, it is more easy to understand why flash is used to store code. Furthermore, data in flash are not erased after power down, allowing code to restart after power on.

But any data that is to be manipulated has to be copied from ROM to RAM
If data to be manipulated resides in flash, then those bytes are not to be changed during runtime. This is a technique to save RAM space.

In the middle of those, there is EEPROM memory that can be written during runtime but needs several ms for each byte. Data in eeprom are not lost after power down.

Hope it is more clear for you now.

https://en.wikipedia.org/wiki/Random-access_memory
https://en.wikipedia.org/wiki/Flash_memory
https://en.wikipedia.org/wiki/EEPROM
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
Flash is usually read-only at run time (writing can be done, but is very slow), but for code, this is not usually a problem. Self-modifying code is rare in the embedded world.

I would hesitate in referring to In Application Programming (IAP) of Flash "rare," as Alex pointed out most bootloaders use the technique, there are of course other examples of self modifying, self programming or IAP to which it is commonly referred.


@shaiko

The major difference between programming Flash and EEPROM storage is EEPROM storage typically allows the programming/writing of one byte, where as Flash storage typically requires programming/writing an entire sector/block of storage at one time.

Also rewritable nonvolatile storage technologies typically offer a limited number of erases/writes to storage, commonly referred to as endurance, the approximate number depends largely on the technology in question.

For example the PIC18F4550 offers Enhanced Flash storage with a typically endurance of only 100,000, while the devices EEPROM has an endurance rating of 1,000,000.

Memory Endurance: The Enhanced Flash cells
for both program memory and data EEPROM are
rated to last for many thousands of erase/write
cycles – up to 100,000 for program memory and
1,000,000 for EEPROM. Data retention without
refresh is conservatively estimated to be greater
than 40 years.

An endurance of 100,000 may appear to be a large number, however it doesn't long to exceed this rating if the devices program is written to modify the contents of Flash several times a second.

Of course combined with the fact, a write of 32 consecutive bytes and erasure of 64 consecutive bytes are required when programming Flash, careful consideration should be taken when utilizing IAP within your app.

The required steps to program nonvolatile storage like Flash, can be quite involved requiring a specific procedure which can be time/cycle consuming.

Reference: PIC18F2455/2550/4455/4550 Datasheet, Section: 6.0 FLASH PROGRAM MEMORY, Page: 81-90

A similar, although not quite as involved procedure is required to program EEPROM.



Another consideration when designing the devices themselves is the density these different technologies offer and the square footage (area) they require to implement in silicon.

Technologies such as Flash are typically higher densities, thus the many devices offer large capacities of Flash, while SRAM is typically lower densities, thus small capacities are offered.


BigDog
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top