5
\$\begingroup\$

I have a two-part question based on the memory of the Microcontroller (Common Platform not to any controller specific).

  1. How to access the data/variable stored in RAM and flash. I know the about the RAM access for both static and dynamic in RAM, but I don't know how to read the data from flash/ROM. For example: If I am allocating dynamic memory in RAM using malloc and is allocating from the address 0x0303, will this address and ROM/Flash Address conflict?

  2. What is 'Memory Protection'. It is only for reserved and accessing Permission for specified address or any other use. It is possible in both RAM and Flash.

\$\endgroup\$
  • 4
    \$\begingroup\$ Those questions are extremely controller-specific. \$\endgroup\$ – Ignacio Vazquez-Abrams Apr 29 '15 at 6:05
  • \$\begingroup\$ But I need big Picture.. Is there any chances that address of RAM/Flash will be same. If I am pointing to that address using a pointer to fetch a value. Which value will fetch. I know will fetch from RAM. what about Flash \$\endgroup\$ – user2732944 Apr 29 '15 at 6:26
  • \$\begingroup\$ @user2732944 Most µCs I know of have Flash and RAM in separate memory areas (Harvard architecture). Here you need different commands to access them. \$\endgroup\$ – glglgl Apr 29 '15 at 6:29
  • 2
    \$\begingroup\$ @glglgl: Harvard architecture has nothing to do with having Flash and RAM in separate memory areas. Harvard architecture is about accessing code and data via separate busses (and maybe as a consequence but not neccessarily also from separate memories). Both, code and data, be be located in RAM or Flash. \$\endgroup\$ – Curd Apr 29 '15 at 9:30
4
\$\begingroup\$

It depends on the architecture -- there are two schools of thought on this, going back to the 1940's.

One is called Harvard architecture, and means that the addresses for flash program code, RAM storage, and I/O are all in separate address spaces. That means there can be an address 0x100 for program, 0x100 for RAM, and 0x100 for I/O and they are all different.

The second type is called Von Neumann architecture, in which flash program code and RAM are in the same address space. I/O may also be in the same address space, if it is "memory-mapped", or separate if it uses I/O port addressing.

Most microprocessors like the 0x86 in your desktop have a Von Neumann architecture. Both program code and data are stored together in the same RAM. There is also a separate read-only memory (ROM) that holds a bit of code that is executed when the processor first boots up, to load the OS (Windows, Linux, etc) off of the hard drive and into RAM. This ROM is also in the same address space, typically at the very high end.

Most, but not all microcontrollers on the other hand use use Harvard architecture. 8 or 16-bit microcontrollers typically have up to 64K of flash program memory, and several thousands bytes of RAM. (Flash may be extended beyond 64K using various paging schemes -- but the actual addresses are still 16-bit and stay in the same address space). I/O is addressed via internal I/O register addresses.

An exception to this, among others, are Freescale microcontrollers like the MC9S08 etc. which put flash program addresses, RAM, and memory-mapped I/O all in the same address space.

enter image description here

Note in this diagram, memory is shown with address 0x0000 at the top, and 0xFFFF (64K-1) at the bottom. The low-order memory-mapped I/O addresses 0x0000 through 0x007F are allocated first, followed by RAM at 0x0080 to 0x17FF. This is done so low addressed I/O and low addressed RAM (0x0080-0xFF) can use special instructions that target the first 256 bytes of the 64K address space -- those address below 0x00FF which only need one byte for the address field (known as "page 0 addressing").

The RAM segment is followed by more memory-mapped I/O, then more RAM, and then the first block of flash starting at 0x2180. This is interrupted by 2K of EEPROM at 0x3C00, and then the rest of the program space up to 0xFFFF is flash. There is a paging window at 0x8000 through 0xBFFF to provide for an additional 32K of flash.

In a Von Neumann machine, if you try to allocate at address 0x300, then that address will point to either RAM, or flash, but not both. So there is no conflict.

In a Harvard architecture machine, address 0x300 could be either RAM or flash. So how is that differentiated? This is C-compiler specific, so it will be different for different microcontrollers. But by default, all references to memory addresses will be for RAM. However if a variable is preceded by the C keyword const, then that data is put into flash. (And of course all other constants, such as strings, are automatically put into flash by the compiler.)

If a program wants to read flash memory directly, it typically has to call a vendor supplied function and/or writing to a set of special registers to do so. For example, on the Microchip PIC16F microcontrollers, it takes 13 machine instructions just to read one program word (14 bits) out of flash. And this is certainly the case if a program wants to write to flash, since the MCU has to go through several steps to erase and then reprogram the bytes in flash that are to be changed.

For microcontrollers, memory protection is generally available only for flash memory -- and this protection simply means neither a program or external programmer can modify the flash area while protection is on. This is usually done in pages, so some of the flash can be protected (e.g. a boot area) and the rest left unprotected so it can be updated in the field FOTA (firmware over the air). Many microcontrollers also have the ability to make the flash memory unreadable by an external programmer after the microcontroller has been programmed. This is to prevent a competitor from trying to read the binary program out of a company's products.

Microprocessors, like the 0x86, have much more complicated protection schemes, tied into their virtual address schemes, and at a basic level divide the RAM into kernel and user spaces. (Remember, they are 99.99% RAM, and hardly any ROM). Kernel space is where the core of the operating system lives, along with most device drivers. Application programs are in the user space. A memory protection fault occurs if a user program tries to execute a privileged kernel instruction, or access hardware directly.

\$\endgroup\$
  • \$\begingroup\$ "Both program code and data are stored together in the same RAM" I didn't get this. You mean program will stored in RAM? \$\endgroup\$ – user2732944 Apr 29 '15 at 9:31
  • 1
    \$\begingroup\$ "However if a variable is preceded by the C keyword const, then that data is put into flash", What should be the Const? Data or pointer? \$\endgroup\$ – user2732944 Apr 29 '15 at 9:35
  • \$\begingroup\$ @user2732944 Re programs and data in RAM -- in computers with no flash (except for booting), like desktop PC's, programs are read off of a hard drive or equivalent each time they are run, and put into RAM where they are executed (unlike most embedded systems, where programs are both stored and executed out of flash). So all the program on your PC are executing out of RAM. That is one of the key points of Von Neumann architecture -- programs and data are put into the same address space. When the program is done running, it can be deleted and another program can take its place. \$\endgroup\$ – tcrosley Apr 29 '15 at 10:47
  • \$\begingroup\$ @user2732944 You can have both const pointers, and const data, or both. This article goes into a lot of variations. \$\endgroup\$ – tcrosley Apr 29 '15 at 10:48
1
\$\begingroup\$

Memory protection unit also called MPU can be found on many microcontrollers, and its purpose is to protect some memory areas from being overwritten. Think about system files for example, they are very important and everything will go to hell if that part of the memory to be overwritten. It can be used for many other purposes, but most often for the data which are very important to your project and must not be lost or overwritten.

Read variable values from RAM using pointers, the same applies for a flash memory, but is a little bit complicated, since you have to be aware of flash memory arhitecture, it is composed of sectors, and from vendors to vendors, these sectors are of different sizes and to read variables stored in flash you need to know: Flash memory base address, sector offset, and which part of sector to read.

Base adresses for both RAM and Flash memories need to be different or there will be conflict.

enter image description here

\$\endgroup\$

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.