0% found this document useful (0 votes)
22 views40 pages

05 Lec Memory - Architectures

This document provides an overview of memory in embedded systems. It discusses the traditional roles of memory for storage and communication of programs and with sensors and actuators. Memory in embedded systems is often more constrained than general purpose computing in terms of size, power, reliability, and other factors. The document outlines different types of memory including volatile and non-volatile, SRAM and DRAM. It discusses memory hierarchies including scratchpads, caches, and virtual memory. Memory organization techniques like static allocation, stacks, and heaps are presented. Examples of memory architectures for microcontrollers like STM32, AVR, and Luminary LM3S8962 are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views40 pages

05 Lec Memory - Architectures

This document provides an overview of memory in embedded systems. It discusses the traditional roles of memory for storage and communication of programs and with sensors and actuators. Memory in embedded systems is often more constrained than general purpose computing in terms of size, power, reliability, and other factors. The document outlines different types of memory including volatile and non-volatile, SRAM and DRAM. It discusses memory hierarchies including scratchpads, caches, and virtual memory. Memory organization techniques like static allocation, stacks, and heaps are presented. Examples of memory architectures for microcontrollers like STM32, AVR, and Luminary LM3S8962 are provided.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

EE522

Advanced Embedded Systems

Sources: [Introduction to Embedded Systems, Edward A.


Lee and Sanjit A. Seshia, UC Berkeley.
Embedded Networked Systems, Kai Huang, TU Munich]

2-1
Role of Memory in Embedded
Systems
 Traditional roles: Storage and Communication for
Programs

 Communication with Sensors and Actuators

 Often much more constrained than in general-


purpose computing
 Size, power, reliability, etc.

 Can be important for programmers to


understand these constraints
2-2
Memory Architecture: Issues
 Types of memory These issues loom
larger in embedded
 volatile vs. non-volatile, SRAM vs. DRAM systems than in
 Memory hierarchies general-purpose
computing.
 scratchpads, caches, virtual memory
 Memory organization
 statically allocated
 stacks
 heaps (allocation, fragmentation, garbage collection)
 The memory model of C
Non-Volatile Memory
Preserves contents when power is off
 EPROM: erasable programmable read only memory
 Invented by Dov Frohman of Intel in 1971
 Erase by exposing the chip to strong UV light
 EEPROM: electrically erasable programmable ROM
 Invented by George Perlegos at Intel in 1978
 Flash memory
 Invented by Dr. Fujio Masuoka at Toshiba around 1980
 Erased a “block” at a time
 Limited number of program/erase cycles
(~ 100,000)
 Controllers can get quite complex
 Disk drives
 Not as well suited for embedded systems 2-4
Volatile Memory
Loses contents when power is off
 SRAM: static random-access memory
 Fast, deterministic access time
 But more power hungry and less dense than DRAM
 Used for caches, scratchpads, and small embedded
memories
 DRAM: dynamic random-access memory
 Slower than SRAM
 Access time depends on the sequence of addresses
 Denser than SRAM (higher capacity)
 Requires periodic refresh (typically every 64 msec)
 Typically used for main memory
 Boot loader
 On power up, transfers data from non-volatile to
volatile memory 2-5
Example:
Die of a
STM32F103VGT6
ARM Cortex-M3
microcontroller with
1 megabyte flash
memory by
STMicroelectronics.

2-6
Memory Map
of an ARM
CortexTM - M3
Architecture
 Defines the
mapping of
addresses to
physical memory

 Note that this


does not define
how much
physical memory
there is! 2-7
Another Example: Atmel AVR
 The AVR is an 8-bit single chip microcontroller first
developed by Atmel in 1996. The AVR was one of
the first microcontroller families to use on-chip flash
memory for program storage. It has a modified
Harvard architecture.1

 AVR was conceived by two students at the


Norwegian Institute of Technology (NTH) Alf-Egil
Bogen and Vegard Wollan.

1A Harvard architecture uses separate memory spaces for program


and data. It originated with the Harvard Mark I relay-based computer
(used during World War II), which stored the program on punched
tape (24 bits wide) and the data in electro-mechanical counters.
2-8
A Use of AVR: Arduino
 Arduino is a family of open-source hardware boards
built around either 8-bit AVR processors or 32-bit
ARM processors.

Example:
Atmel AVR
Atmega328
28-pin DIP on an
Arduino Duemilanove
board
2-9
Another example
of use of an AVR
processor
The iRobot Create
Command
Module

9-pin I/O port

Atmel ATMega 168


Microcontroller
ATMega168 Memory
Architecture
An 8-bit microcontroller with 16-bit addresses
iRobot AVR microcontroller
command architecture. Used in
module has iRobot command
16K bytes flash module.
memory
(14,336
available for
the user
program.
Includes
interrupt
vectors and Additional I/O on the
boot loader.) command module:
•Two 8-bit timer/counters
1 k bytes RAM •One 16-bit timer/counter
•6 PWM channels
•8-channel, 10-bit ADC
•One serial UART
•2-wire serial interface
stack
Source: ATmega168 Reference Manual
Questions to test your
understanding
1. What is the difference between an 8-bit
microcontroller and a 32-bit microcontroller?

2. Why use volatile memory? Why not always


use nonvolatile memory?

2-12
Memory hierarchy
Cache:
 A subset of memory addresses is mapped to SRAM
 Accessing an address not in SRAM results in cache miss
 A miss is handled by copying contents of DRAM to SRAM
Scratchpad:
 SRAM and DRAM occupy disjoint regions of memory space
 Software manages what is stored where
 Segmentation
 Logical addresses are mapped to a subset of physical
addresses
 Permissions regulate which tasks can access which memory
See your textbook for more details.
2-13
Memory Hierarchy

 Here, the cache or scratchpad, main memory, and disk or


flash share the same address space.
2-14
Memory Hierarchy
Here, each distinct
piece of memory
hardware has its own
segment of the
address space.

This requires more


careful software
design, but gives
more direct control
over timing.
2-15
2-16
2-17
2-18
Luminary LM3S8962
I/O Architecture
32-bit microcomputer.

Interfaced to the iRobot used in our


labs

Relevant features:
 UART interface to iRobot
 ADC to accelerometer
 SysTick/other Timers
 UART to Bluetooth
 JTAG interface for programming
 No floating point!

Source: Luminary Micro Datasheet


Memory Architecture Luminary LM3S8962

ARM Cortex M3

Source: Luminary Micro Datasheet


Memory Organization for Programs
 Statically-allocated memory
 Compiler chooses the address at which to
store a variable
 Stack
 Dynamically allocated memory with a Last-in,
First-out (LIFO) strategy
 Heap
 Dynamically allocated memory

2-21
Statically-Allocated Memory in C
char x;
int main(void) {
x = 0x20;

}

 Compiler chooses what address to use for x, and


the variable is accessible across procedures. The
variable’s lifetime is the total duration of the
program execution.

2-22
Statically-Allocated Memory in C
with Limited Scope
void foo(void) {
static char x;
x = 0x20;

}

 Compiler chooses what address to use for x, but the


variable is meant to be accessible only in foo( ).
The variable’s lifetime is the total duration of the
program execution (values persist across calls to
foo( )).

2-23
Variables on the Stack
(“automatic variables”)
void foo(void) {
char x;
x = 0x20;

}

 When the procedure is called, x is assigned an


address on the stack (by decrementing the stack
pointer). When the procedure returns, the memory
is freed (by incrementing the stack pointer). The
variable persists only for the duration of the call to
foo( ).
2-24
Dynamically-Allocated Memory
The Heap

 An operating system typically offers a way to dynamically


allocate memory on a “heap”.
 Memory management (malloc( ) and free( )) can
lead to many problems with embedded systems:
 Memory leaks (allocated memory is never freed)
 Memory fragmentation (allocatable pieces get smaller)

 Automatic techniques (“garbage collection”) often


require stopping everything and reorganizing the
allocated memory. This is deadly for real-time programs.
2-25
Question 1
What is meant by the
following C code:

char x;
void foo(void) {
x = 0x20;

}
Answer 1
What is meant by the
following C code:

char x;
void foo(void) {
x = 0x20;

}

An 8-bit quantity (hex 0x20) is


stored at an address in
statically allocated memory in
internal RAM determined by
the compiler.
Question 2
What is meant by the
following C code:

char *x;
void foo(void) {
x = 0x20;

}
Answer 2
What is meant by the
following C code:

char *x;
void foo(void) {
x = 0x20;

}

A 16-bit quantity (hex 0x0020)


is stored at an address in
statically allocated memory in
internal RAM determined by
the compiler.
Question 3
What is meant by the
following C code:

char *x, y;
void foo(void) {
x = 0x20;
y = *x;

}
Answer 3
What is meant by the
following C code:

char *x, y;
void foo(void) {
x = 0x20;
y = *x;

}

The 8-bit quantity in the I/O


register at location 0x20 is
loaded into y, which is at a
location in Internal SRAM
determined by the compiler.
Question 4
What is meant by the
following C code:

char foo() {
char *x, y;
x = 0x20;
y = *x;
return y;
}
char z;
int main(void) {
z = foo();

}

Where are x, y, z in memory?


Where are x, y, z in memory?
Answer 4 char foo() {
char *x, y;
x = 0x20;
y = *x;
return y;
}
char z;
int main(void) {
z = foo();

}

x occupies 2 bytes on the


stack, y occupies 1 byte on
the stack, and z occupies 1
byte in static memory.
Question 5
 What is meant by the
following C code:

void foo(void) {
char *x, y;
x = &y;
*x = 0x20;

}

stack
Answer 5
 What is meant by the
following C code:

void foo(void) {
char *x, y;
x = &y;
*x = 0x20;

}

16 bits for x and 8 bits for y are


allocated on the stack, then x is
loaded with the address of y,
and then y is loaded with the 8-
bit quantity 0x20.
stack
Question 6  What goes into z in the
following program:

char foo() {
char y;
uint16_t x;
x = 0x20;
y = *x;
return y;
}
char z;
int main(void) {
z = foo();

}
 What goes into z in the
Answer 6 following program:

char foo() {
char y;
uint16_t x;
x = 0x20;
y = *x;
return y;
}
char z;
int main(void) {
z = foo();

}

z is loaded with the 8-bit quantity in


the I/O register at location 0x20.
Memory usage: Understanding the stack.
Quiz: Find the flaw in this program
(begin by thinking about where each variable is allocated)
statically allocated: compiler assigns a memory location.
int x = 2;
arguments on the stack
int* foo(int y) {
int z; automatic variables on the stack
z = y * x;
return &z;
}

int main(void) {
int* result = foo(10); program counter, argument 10
and z go on the stack (and
... possibly more, depending on the
} complier)

The procedure foo( ) returns a pointer to a variable on


the stack. What if another procedure call (or interrupt)
occurs before the returned pointer is de-referenced?
Watch out for Recursion!!
Quiz: What is the Final Value of z?

void foo(uint16_t x) {
char y;
y = *x;
if (x > 0x100) {
foo(x – 1);
}
}
char z;
void main(…) {
z = 0x10;
foo(0x04FF);

}
Summary
Understanding memory architectures is essential
to programming embedded systems.

You might also like