1
Hệ thống nhúng
Thạc sĩ Lê Mạnh Hải
Embedded Systems
2
Lesson 5 : Taking a look under the
hood
Motivation:
•
Memory space allocation
•
Program space visibility
•
Investigating memory allocation
•
Looking at the MAP
•
Pointers
•
The heap
•
MPLAB® C30 memory models
3
Flight plan
•
In this lesson we will review the basics of string
declaration as an excuse to introduce the memory
allocation techniques used by the MPLAB C30 compiler.
•
The RISC architecture of the PIC24 poses some
interesting challenges and offers innovative solutions.
4
Preflight checklist
•
This lesson will be performed exclusively with software
tools including the MPLAB IDE, MPLAB C30 compiler
and the MPLAB SIM simulator.
•
Use the “New Project Set-up” checklist to create a new
project called “Strings” and a new source file similarly
called “strings.c”.
5
The flight
1. char s[5] = { 'H', 'E', 'L', 'L', 'O'};
2. char s[5] = "HELLO";
3. char s[] = "HELLO";
4. char s[6] = { 'H', 'E', 'L', 'L', 'O', '\0' };
6
The flight
char c; // declare c as an 8-bit signed integer
c = 'a'; // assign to it the value corresponding to 'a' in the ASCII table
c ++; // increment it it will represent the ASCII character 'b' now
Something wrong here
char s[15]; // declare s as a string of 15 characters
s = "Hello!"; // Error! This does not work!
copy the content of a string onto another:
strcpy( s, "HELLO"); // s : "HELLO"
append (or concatenate) two strings:
strcat( s, " WORLD"); // s : "HELLO WORLD"
determine the length of a string:
i = strlen( s); // i : 11
and many more.
7
Memory space allocation
char s[] = "Flying with the PIC24";
const char s[] = "Flying with the PIC24";
strcpy( s, “HELLO”);
The string “HELLO” was implicitly defi ned as of const char type,
and similarly assigned to the const section in program memory to
be accessible via the Program Space Visibility window.
8
Program space visibility
•
The PIC24 architecture is somewhat different from most other
16-bit microcontroller architectures you might be familiar with.
It was designed for maximum effi ciency according to the
Harvard model, as opposed to the more common Von Neumann
model.
•
The big difference between the two is that there are two
completely separated and independent buses available, one for
access to the Program Memory (Flash) and one for access to the
Data Memory (RAM).
•
The PIC24 architecture offers two methods to read data from
program memory: using special table access instructions (tblrd)
and through a second mechanism, called the Program Space
Visibility or PSV. This is a window of up to 32K bytes of program
memory accessible from the data memory bus. In other words the
PSV is a bridge between the program memory bus and the data
memory bus.
9
Testing
10
Post-flight briefing
•
In this lesson, we have learned not only what data types
are available and how much memory is allocated to them,
but also how they affect the resulting compiled program—
code size and the execution speed.
•
We used the MPLAB SIM simulator Stopwatch function to
measure the number of instruction cycles (and therefore
time) required for the execution of a series of code
segments.
•
Some of the information gathered will, hopefully, be useful
to guide our actions in the future when balancing our
needs for precision and performance in embedded-control
applications.
11
Investigating memory allocation
#include <p24fj128ga010.h>
#include <string.h>
// 1. variable declarations
const char a[] = “Learn to fly with the PIC24”;
char b[100] = “”;
// 2. main program
main()
{
strcpy( b, “MPLAB C30”); // assign new content to b
} //main
12
Watch window
13
14
Pointers
int *pi; // define a pointer to an integer
int i; // index/counter
int a[10]; // the array of integers
// 1. sequential access using array indexing
for( i=0; i<10; i++)
a[ i] = i;
// 2. sequential access using a pointer
pi = a;
for( i=0; i<10; i++)
{
*pi = i;
pi++;
}
15
Pointers
// 2. sequential access to array using pointers
for( i=0, p=a; i<10; i++)
*pi++ = i;
16
The heap
•
What is heap?
•
The “heap” is the area of data memory reserved for such
use, and a set of functions, part of the standard C library
“stdlib.h”, provides the tools to allocate and free the
memory blocks.
•
void *malloc(size_t size);
takes a block of memory of requested size from the heap, and
returns a pointer to it.
•
void free(void *ptr);
17
Exercises
Develop new string manipulation
functions to perform the following
operations:
1. Search for a string in an array of
strings, sequential.
2. Implement a Binary search.
3. Develop a simple Hash Table
management library.
18
What is next?
•
CHAPTER 7: Communication (pg 89 – pg103)
–
Synchronous serial interfaces
–
Asynchronous serial interfaces
–
Parallel interfaces
–
Synchronous communication using the SPI modules
–
Testing the Read Status Register command
–
Writing to the EEPROM
–
Reading the memory contents
–
A nonvolatile storage library
–
Testing the new NVM library
•
CHAPTER 8: Asynchronous communication (pg 104 – pg125)
–
UART configuration
–
Sending and receiving data
–
Testing the serial
–
communication routines