4
\$\begingroup\$

On PC, program executable is read from hard disk, and loaded into RAM to execute it. On microcontroller, program is stored on flash.

  1. Is it loaded into SRAM when microcontroller starts up?
  2. If yes, then STM32F103 document says, flash is 64KiB, SRAM is 20KiB. Program doesn't fit into SRAM. (Same for other microcontrollers as well).
  3. If you will say that not all flash content is loaded into SRAM, what purpose does the remaining data on flash serve to? All operations are done on SRAM already in program codes.
\$\endgroup\$
1
  • \$\begingroup\$ Depends on the program. If it copies itself to SRAM (e.g. as part of its startup code), then yes. As you note, sometimes it wouldn't make sense to do so... \$\endgroup\$ – user_1818839 Jan 29 '16 at 20:11
9
\$\begingroup\$

Most 8, 16 and 32 bit microcontrollers execute the program directly from flash. This is true of the STM32F103 range. Most microcontrollers are capable of executing the program from RAM, but only relatively specialised programs actually do this. For this reason, most microcontrollers have far more flash than RAM.

There are some microcontrollers that have lots of RAM and little or even no flash. It may even be possible to get an STM32 like this. These parts rely on storing the program off-chip in a physically small and cheap serial flash chip such as a Micron MT25Q.

True microprocessors (where the RAM and flash is in separate IC's, such as the ARM 7 and similar parts) can and often do copy the program from flash into RAM and execute it from RAM. The main reason for this is that RAM is usually much faster to access compared to flash, so the program will run faster. Fast processors often run the Linux operating system which usually works this way. It also gives Linux the ability to store the program in other types of storage memory, such as SD cards and serial interface flash (serial flash).

Linux can be made to execute the program directly from flash (it is termed Execute-in-place) but this usually suffers a performance penalty.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ All the STM32 microcontrollers I'm aware of have large onboard flash: typically about 3x as much flash as SRAM, sometimes even more. \$\endgroup\$ – user39382 Jan 29 '16 at 22:41
2
\$\begingroup\$

It depends. Some FLASH memories are XIP (eXecute In Place), namely the NOR ones, as they are word-addressable. Some are not, such as NAND FLASH. Programs running directly from FLASH (or any other ROM) have to be written/compiled into a so-called "ROMable" image. The main difference of such a program from the one running from RAM, that it has first "relocate" all of it's variables into RAM and initialize them, because it can't change them in the read-only memory.

But sometimes the program is happily copied to RAM and running from there. If there is not enough RAM to fit the whole FLASH, a multi-level bootloaders are used. First the L0 bootloader is loaded into RAM, executing some low-level initializations and then loading the L1 bootloader, sometimes overwriting itself. It can be the final binary instead of L1 of course. But it can go up levels as well. this way the code which is not needed anymore is just not kept in RAM, reducing it's usage.

So the extra FLASH memory can be used to store different binaries loaded one by one. Or just store some extra data the program can access in the run-time (just like the hard drive in a computer).

\$\endgroup\$
0
\$\begingroup\$

Coping a program to memory before executing is a function of the operating system. On MCUs, you usually just run the program from the FLASH. There are notable exceptions, such as boot loaders, but generally, you run from FLASH. Even on a PC or something with an OS, you could run a program from a disk and not memory, but you need to think back to the days of the tape drive when IO was slow.

\$\endgroup\$
0
\$\begingroup\$

Most microcontrollers use Harvard Architecture, which has separate program and data memory areas. With this arrangement, the processor cannot execute code from RAM, as the RAM is strictly data memory. The FLASH memory holds the program code, and the processor fetches instructions directly from FLASH.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ That's not true of ARM microcontrollers (like the STM32 mentioned in the question), which are increasingly common nowadays. \$\endgroup\$ – user39382 Jan 29 '16 at 22:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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