0% found this document useful (0 votes)
18 views41 pages

Chapter Four

In embedded systems, memory management is crucial due to limited memory resources. Memory comes in different types like RAM, ROM, and hybrid memories. RAM includes SRAM that retains data as long as power is on, and DRAM that requires refreshing to retain data for short periods. System designers consider access time and cost when deciding between SRAM and DRAM for applications.

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views41 pages

Chapter Four

In embedded systems, memory management is crucial due to limited memory resources. Memory comes in different types like RAM, ROM, and hybrid memories. RAM includes SRAM that retains data as long as power is on, and DRAM that requires refreshing to retain data for short periods. System designers consider access time and cost when deciding between SRAM and DRAM for applications.

Uploaded by

Yohannes Dereje
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 41

www. Micro Digital Ed.

com
BIHE university

Chapter -4 :RTES Memory Management

Real time and Embedded Systems

B.Sc(SE), 2023
By Mikru L
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Memory Management in Embedded Systems
 In embedded systems, memory management is a crucial aspect of
www. Micro Digital Ed. com
BIHE university

designing and programming.


 Microcontrollers, such as the ATmega32 used in Arduino boards, have
limited memory resources that must be carefully managed to ensure
efficient and reliable operation.
 Embedded systems developers commonly implement custom memory-
management facilities on top of what the underlying RTOS provides
 Understanding memory management is therefore an important aspect
Common Requirements
 Regardless of the type of embedded system, the requirements placed on
a memory management system
 Minimal fragmentation
 Minimal management overhead
 Deterministic allocation time
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
2
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Memory Map
 All processes store their programs and data in memory.
www. Micro Digital Ed. com
BIHE university

 In some cases, this memory resides on the very same chip


as the processor.
 But, more often it is located in external memory chips.
 These chips are located in the processor’s memory space,
and then processors communicate with them by way of
two sets of electric wires called the address bus and the
data bus.
 To read or write a particular location in memory, the
processor first writes the desired address onto the address
bus.
 The data is then transferred over the data bus.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
3
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Types of Memory and Applications:
www. Micro Digital Ed. com
BIHE university

 Most software developers think of memory as being either


random-access(RAM) or Read-Only(ROM).
 But, in fact, there are subtypes of each and even a third class of
hybrid memories.
 In a RAM device, the data stored at each memory location can
be read or written, as desired.
 In a ROM device, the data stored at each memory location can
be read at will but never written.
 In some cases, it is possible to overwrite the data in a ROM-
like device.
 Such devices are called hybrid memories because they exhibit
some of the characteristics of both RAM and ROM.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
4
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Common Memory Types in Embedded Systems
www. Micro Digital Ed. com
BIHE university

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
5
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Types of RAM:
 There are two important memory devices in the RAM family:
www. Micro Digital Ed. com
BIHE university

SRAM and DRAM.


 The main difference between them is the lifetime of the data
stored.
 SRAM (static RAM) retains its contents as long as electrical
power is applied to the chip.
 However, if the power is turned off or lost temporarily then its
contents will be lost forever.
 The main difference between them is the lifetime of the data
stored.
• SRAM (static RAM) retains its contents as long as electrical
power is applied to the chip.
• However, if the power is turned off or lost temporarily then its
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
6
contents will be lost forever.
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Types of RAM:
 DRAM (dynamic RAM), on the other hand, has an extremely
www. Micro Digital Ed. com
BIHE university

short data lifetime-usually less than a quarter of a second.


 This is true even when power is applied constantly.
 But, using hardware called a DRAM controller can be used to
make DRAM behave more like SRAM.
 The job of the DRAM controller is to periodically refresh the
data stored in the DRAM.
 By refreshing the data several times a second, the DRAM
controller keeps the contents of memory alive for as long as
they are needed.
 So, DRAM is as useful as SRAM after all.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
7
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Types of RAM:
www. Micro Digital Ed. com
BIHE university

The decision to make which type of RAM to use:


•When deciding which type of RAM to use, a system
designer must consider access time and cost.
•RAM devices offer extremely fast access times
(approximately four times faster than DRAM) but are much
more expensive to produce.
•Generally, SRAM is used only where access speed is
extremely important.
•A lower cost per byte makes DRAM attractive whenever
large amounts of RAM are required.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
8
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Types of ROM:
www. Micro Digital Ed. com  ROM devices from hardwired to one-time programmable to BIHE university

erasable-and-programmable.
 A common feature across all these devices is their ability to
retain data and programs forever, even during a power failure.
Masked ROM:
 The contents of the ROM had to be specified before chip
production, so the actual data could be used to arrange the
transistors inside the chip!
 Hardwired memories are still used, though they are now
called “masked ROMs” to distinguish them from other types
of ROM.
 The main advantage of a masked ROM is its low production
cost.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
9
Assembly and C Upper Saddle River, NJ 07458. • All Rights
PROM:
 One step up from the masked ROM is the PROM
www. Micro Digital Ed. com
BIHE university

(programmable ROM), which is purchased in an


unprogrammed state.
 The process of writing your data to the PROM involves a
special piece of equipment called a device programmer.
 Once a PROM has been programmed in this way, its
contents can never be changed.
 If the code or data stored in the PROM must be changed,
the current device must be discarded.
 As a result, PROMs are also known as one-time
programmable (OTP) devices.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
10
Assembly and C Upper Saddle River, NJ 07458. • All Rights
EPROM:
 Is programmed in exactly the same manner as a PROM.
www. Micro Digital Ed. com
BIHE university

 EPROMs can be erased and reprogrammed repeatedly.


 To erase an EPROM, you simply expose the device to a
strong source of ultraviolet light.
 There is a “window” in the top of the device to let the
ultraviolet light reach the silicon.) By doing this, you
essentially reset the entire chip to its initial-
unprogrammed-state.
 Though more expensive than PROMs.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
11
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Hybrid Types:

EEPROM:
www. Micro Digital Ed. com
BIHE university

EEPROMs are electrically-erasable-and programmable.


Any byte within an EEPROM can be erased and rewritten.
Once written, the new data will remain in the device
forever-or at least until it is electrically erased.
The tradeoff for this improved functionality is mainly higher
cost.
Write cycles are also significantly longer than writes to a
RAM, so you wouldn’t want to use an EEPROM for your
main system memory.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
12
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Flash Memory:
 Flash memory is the most recent advancement in-memory
www. Micro Digital Ed. com
BIHE university

technology.
 Flash memory devices are high density, low cost, nonvolatile, fast
(to read, but not to write), and electrically reprogrammable.
 These advantages are overwhelming and the use of Flash memory
has increased dramatically in embedded systems.
 From a software viewpoint, Flash and EEPROM technologies are
very similar.
 The major difference is that Flash devices can be erased only one
sector at a time, not byte by byte.
 Typical sector sizes are in the range of 256 bytes to 16 kilobytes.
Despite this disadvantage, Flash is much more popular than
EEPROM and is rapidly displacing many of the ROM devices as
well.
AVR Microcontroller and Embedded System Using
Assembly and C
© 2011 Pearson Higher Education,
13
Upper Saddle River, NJ 07458. • All Rights
NVRAM:
 The third member of the hybrid memory class is
www. Micro Digital Ed. com
BIHE university

NVRAM (nonvolatile RAM).


 An NVRAM is usually just an SRAM with a battery
backup.
 When the power is turned on, the NVRAM operates just
like any other SRAM.
 But when the power is turned off, the NVRAM draws
just enough electrical power from the battery to retain its
current contents.
 NVRAM is fairly common in embedded systems.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
14
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Memory Device Characteristics:
www. Micro Digital Ed. com
BIHE university

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
15
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Working with Flash Memory
www. Micro Digital Ed. com
BIHE university

 Reading data from a Flash memory is fast and is not all


that different from reading from any other memory
device.
 The erase and write cycles take longer than the read
cycle.
 So if a read is attempted in the middle of one of those
operations, the result will be either delayed or incorrect,
depending on the device.
 Writing data to a Flash is much harder. Three factors
make writes difficult.
– each memory location must be erased before it can be
rewritten.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
16
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Working with Flash Memory
www. Micro Digital Ed. com
BIHE university

 only one sector, or block, of the device can be erased


at a time; it is impossible to erase a single byte.
 The size of an individual sector varies by device
 the process of erasing the old data and writing the new
varies from one manufacturer to another and is usually
rather complicated.
• it is usually best to add a layer of software to make
the Flash memory easier to use.
• If implemented, this hardware- specific layer of
software is usually called the Flash driver.
 Because it can be difficult to write data to the Flash
device, it often to create a flash driver.
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
17
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Working with Flash Memory
www. Micro Digital Ed. com
BIHE university

 The purpose of the Flash driver is to hide the details of a specific

 This driver should present a simple application programming

 Because the Flash memory provides nonvolatile storage that is also


rewriteable,
• it can be thought of as similar to any other secondary storage system,
such as a hard drive.
• In the files system case, the functions provided by the driver would be
more file-oriented.
• Standard file system functions like open, close, read, and write provide

• The underlying file system structure can be as simple or complex as your

• A well-understood format like the File Allocation Table (FAT) structure

• used by DOS is good enough for most embedded ©projects


AVR Microcontroller and Embedded System Using 2011 Pearson Higher Education,
18
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Dynamic Memory Allocation
in Embedded Systems
 The program code, program data, and system stack occupy
www. Micro Digital Ed. com
BIHE university

the physical memory after program initialization completes


 The kernel uses the remaining physical memory for
dynamic memory allocation. – heap
 Contiguous vs. non-contiguous block of physical memory
 Maintains internal information for a heap in a reserved
memory area
– The starting address of the physical memory block used
for dynamic memory allocation
– The size of this physical memory block
– Allocation table indicates which memory areas are in
use, which memory areas are free
– The size of each free region
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
19
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Typical Implementation
www. Micro Digital Ed. com
BIHE university

 The heap is broken into small, fixed-size blocks.


 Each block has a unit size that is power of two to
ease translating a requested size into the
corresponding required number of units
 The dynamic memory allocation function, malloc, has
an input parameter that specifies the size of the
allocation request in bit
 malloc allocates a larger block, which is made up of
one or more of the smaller, fixed-size blocks.
 The size of this larger memory block is at least as
large as the requested size;
 it is the closest to the multiple of the unit size .
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
20
Assembly and C Upper Saddle River, NJ 07458. • All Rights
States of a Memory Allocation Map
www. Micro Digital Ed. com
BIHE university

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
21
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Memory Allocation Map with Possible
www. Micro Digital Ed. com
Fragmentation BIHE university

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
22
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Problems with Memory Compaction
www. Micro Digital Ed. com

 Is allowed if the tasks that own those memory


BIHE university

blocks reference the blocks using virtual


addresses
 Time-consuming
 The tasks that currently hold ownership of those
memory blocks are prevented from accessing the
contents of those memory locations
 Almost never done in practice in embedded
designs
 The free memory blocks are combined only if
they are immediate neighbors
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
23
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Needs of an Efficient Memory Manager
www. Micro Digital Ed. com
BIHE university

 An efficient memory manager needs to perform


the following chores quickly:
– Determine if a free block that is large enough
exists to satisfy the allocation request.
(malloc)
– Update the internal management information
(malloc and free).
– Determine if the just-freed block can be
combined with its neighboring free blocks to
form a larger piece. (free)
– The structure of the allocation table is the
key to efficient memory management
AVR Microcontroller and Embedded System Using 24 © 2011 Pearson Higher Education,
Assembly and C Upper Saddle River, NJ 07458. • All Rights
An Example of malloc and free
www. Micro Digital Ed. com
BIHE university

• Static array implementation of the allocation map

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
25
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Finding Free Blocks Quickly
www. Micro Digital Ed. com
BIHE university

• Always allocates from the largest available range


of free blocks
• The sizes of free blocks within the allocation
array are maintained using the heap data
structure
• Each node in the heap contain the size of a free
range and its starting index in the allocation array

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
26
Assembly and C Upper Saddle River, NJ 07458. • All Rights
The malloc Operation
• Examine the heap to determine if a free block that is
www. Micro Digital Ed. com
BIHE university

large enough for the allocation request exists.


• If no such block exists, return an error to the
caller.
• Retrieve the starting allocation-array index of the
free range from the top of the heap.
• Update the allocation array
• If the entire block is used to satisfy the
allocation, update the heap by deleting the largest
node. Otherwise update the size.
• Rearrange the heap array

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
27
Assembly and C Upper Saddle River, NJ 07458. • All Rights
The free Operation
www. Micro Digital Ed. com
BIHE university

 Update the allocation array and merge neighboring


blocks if possible.
 If the newly freed block cannot be merged with any of
its neighbors, insert a new entry into the heap array.
 If the newly freed block can be merged with one of its
neighbors, the heap entry representing the neighboring
block must be updated, and the updated entry
rearranged according to its new size.
 If the newly freed block can be merged with both of its
neighbors, the heap entry representing one of the
neighboring blocks must be deleted from the heap, and
the heap entry representing the other neighboring
block must be updated and rearranged according to its
new size. © 2011 Pearson Higher Education,
AVR Microcontroller and Embedded System Using 28
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Fixed-Size Memory Management in Embedded Systems
www. Micro Digital Ed. com
BIHE university

• Management based on memory pools

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
29
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Fixed-Size Memory Management in Embedded Systems
www. Micro Digital Ed. com
BIHE university

Advantages
more deterministic than the heap method
algorithm (constant time)
provide high utilization for static embedded
applications
Disadvantages
This issue results in increased internal memory
fragmentation per allocation for embedded
applications that constantly operate in dynamic
environments

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
30
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Blocking vs. Non-Blocking Memory Functions
 If tasks know that the memory congestion
www. Micro Digital Ed. com
BIHE university

condition can occur but only momentarily,


 and it can tolerate the allocation delay,
the tasks can choose to wait for memory to
become available instead of either failing
entirely or backtracking
 In practice, a well-designed memory
allocation function should allow for
allocation that permits blocking forever,
blocking for a timeout period, or no
blocking at all.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
31
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Blocking Allocation/Deallocation
www. Micro Digital Ed. com
BIHE university

 Pseudo code for memory allocation


Acquire(Counting_Semaphore) Lock(mutex)
 Retrieve the memory block from the pool
Unlock(mutex)
 Pseudo code for memory deallocation
 Lock(mutex)
 Release the memory block back to into the pool
Unlock(mutex)
 Release(Counting_Semaphore)

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
32
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Memory Leak Detection
www. Micro Digital Ed. com
BIHE university

 An error in a program's dynamic-store


allocation logic that causes it to fail to reclaim
discarded memory, leading to eventual collapse
due to memory exhaustion
 How to detect memory leak?
 employ special debugging libraries to ensure
their correct behavior in term of memory use
 Mtrace, Electric Fence, dmalloc,
 MEMWATCH
 hardware tools
 Logic analyzer, ICE
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
33
Assembly and C Upper Saddle River, NJ 07458. • All Rights
mtrace
www. Micro Digital Ed. com
BIHE university

 A feature of the GNU C library


 Implemented as a function call, mtrace()
 Detection of memory leaks caused by unbalanced
malloc/free calls
 Creates a log file of addresses malloc'd and freed
Electric Fence
 It is a library that replaces the C library’s
memory allocation functions, such as malloc(), and
free(), with equivalent functions that implement
limit testing
 Very effective at detecting out-of-bounds memory
reference
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
34
Assembly and C Upper Saddle River, NJ 07458. • All Rights
dmalloc
www. Micro Digital Ed. com
BIHE university

• A library
• Designed as a drop-in substitute for malloc,
realloc, calloc, free and other memory
management functions
• Keep track debug information about your
pointer
• Verify the debug information and logged on
errors.

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
35
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Hardware Design Challenges
 High Power Dissipation: An embedded hardware system
www. Micro Digital Ed. com
BIHE university

needs an increasing number of transistors with an acceptable


power consumption ratio.
 Security Concern: Security is a major concern in IoT devices.
 Cost Considerations:
 Size Constraints:
 Testing:
Challenges in embedded system design
 How much hardware do we need?
 How big is the CPU? Memory?
 How do we meet our deadlines?
 Faster hardware or cleverer software?
 How do we minimize power?
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
36
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Hardware Design Challenges
www. Micro Digital Ed. com
BIHE university

 Turn off unnecessary logic? Reduce memory


accesses?
 Does the implementation meet the specification?
 How do we test for real-time characteristics?
 How do we test on real data?
 How do we work on the system?
 Observability, controllability?
 What is our development platform?
Hardware implementation advantages
(i)Reduced memory for the program
(ii)Reduced number of chips but at an increased cost
(iii) Internally embedded codes, more secure than at
the external ROM

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
37
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Software of Embedded System
 Embedded System: Hardware and Software
www. Micro Digital Ed. com
BIHE university

Embedded software:
 It is a program that controls the ES.
 Program is a piece of code written to perform a
task means it is going to perform some action.
 So its based on the time settings and the based
on the intervals So it's just a software
program.
 It is a piece of code written to perform a task
 so written speech specifically for the
particular hardware that is runs on so whatever
the hardware we have taken if you want to work
if you want to run that hardware we need
 software is a program which controls all the
hardware's .
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
38
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Software of Embedded System
Embedded software example:
www. Micro Digital Ed. com
BIHE university

Hidden in watches
Cellular phones
VCS, toaster
Guide the missile
Control satellite, use in medical instrument
Software complexity:
8-bit microcontroller with just a few kilo
byte of memory
(controlling lighting in the home by using
remote or by chip)
complex embedded software(in aircrafts,
missile guidance
AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
system, navigation system
39
Assembly and C Upper Saddle River, NJ 07458. • All Rights
Embedded software design process
www. Micro Digital Ed. com
BIHE university

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
40
Assembly and C Upper Saddle River, NJ 07458. • All Rights
www. Micro Digital Ed. com
BIHE university

END of The
CHAPTER

AVR Microcontroller and Embedded System Using © 2011 Pearson Higher Education,
41
Assembly and C Upper Saddle River, NJ 07458. • All Rights

You might also like