Diapositivo 1 - Texas Instruments

Download Report

Transcript Diapositivo 1 - Texas Instruments

MSP430 Teaching Materials
UBI
Chapter 2
Software Development tools
Code Composer Essentials v3: Laboratory
Texas Instruments Incorporated
University of Beira Interior (PT)
Pedro Dinis Gaspar, António Espírito Santo, Bruno Ribeiro, Humberto Santos
University of Beira Interior, Electromechanical Engineering Department
www.msp430.ubi.pt
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
Contents
UBI
 Code Composer v3: Laboratory 1: “Hello World”
Beginner’s project:
 Lab1.1: Introduction to the application debug
 Lab1.2: Using breakpoints to save/load data to/from file
 Lab1.3: Advanced breakpoints with triggers
 Lab1.4: Memory and usage Register
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
2
Laboratory 1: “Hello World” Beginner’s
project
UBI
 The following laboratories provide an overview of the
features of CCE:
 Lab1.1: Introduction to debugging an application;
 Lab1.2: Using breakpoints to save and load data to/from a
file;
 Lab1.3: Advanced breakpoints with triggers;
 Lab1.4: Memory and register usage.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
3
Lab1.1 Introduction to debugging an
application
UBI
 Overview:
 This laboratory demonstrates the IDE features that allow
you build and debug an application using CCE;
 Examples are given of all the steps required to create a
project and set up its configuration;
 After successful project compilation, the application is
downloaded to the device;
 The main debug actions are explained:
• Step-by-step execution;
• Analysis of the contents of local and global variables;
• Resetting the device etc…
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
4
Lab1.1 Introduction to debugging an
application
UBI
 Step 1: Creation of the project:
 An application will be developed that sends a series of
Fibonacci numbers to the CCE console. The numbers
corresponding to the Fibonacci series are defined recursively
using the mathematical expression:
 0,

F (n)   1 ,
 F (n  1)  F (n  2)

if n  0;
if n  1;
other cas es
 The algorithm that solves this problem is not difficult to
implement;
 Beginning with the first two values of the sequence, the
remaining numbers are successively calculated;
 The sequence of tasks required to build the project are now
described.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
5
Lab1.1 Introduction to debugging an
application
UBI
 A. Creating the project:
 Create a new project in Project> New Managed C/ASM
Project;
 In the project name field write Project1;
 Accept the default settings in Select a type of project;
 There should be no dependencies with other existing
projects;
 In Device Selection Page, choose in the option Device
Variant, then select MSP430FG4618;
 Automatically, CCE will select the appropriate debug
command file (lnk_msp430fg4618.cmd) and the support
library (rts430x.lib);
 Finalize the creation of the project by clicking the finish
button.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
6
Lab1.1 Introduction to debugging an
application
Step 1. Creating the project
 B. Add a source code file:
UBI
 The project created is visible in the C/C++ Projects window
of the C/C++ perspective;
 Add a file to the project where the source code will be written
by selecting File > New > Source File;
 The file to create should be named Lab1a.c;
 Write the following code that solves the problem:
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
7
Lab1.1 Introduction to debugging an
application
UBI
Step 1. Creating the project
//*************************************************************************
// Basic debug introduction using CCE. Application conditional execution
// author: aes
// data: 10/08/2008
// version: 1.0
//*************************************************************************
#include <msp430xG46x.h>
#include <stdio.h>
//*************************************************************************
// Global data
//*************************************************************************
unsigned int a, b, i;
//*************************************************************************
// Main routine
//*************************************************************************
void main (void)
{
// Stop watch dog
WDTCTL = WDTPW + WDTHOLD;
// Stop WDT
// Global data initialization
a = 0;
b = 1;
// First message to CIO
printf("Lab 1 - Introduction to Debug with CCE V3\n");
printf("Fibonacci sequence computation\n");
printf("Number n = 0 - %d\n", a);
printf("Number n = 1 - %d\n", b);
for(i = 0; i < 7; i++){
int c;
c = a + b;
a = b;
b = c;
printf("Number n = %d - %d\n", i, c);
}
}
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
8
Lab1.1 Introduction to debugging an
application
Step 1. Creating the project
 B. Add a source code file (continued):
UBI
 The code starts with a descriptive header:
• Contents of the file;
• Authors;
• Date and revision…
 Then the file msp430xG46x.h is included. This contains the
definitions necessary for programming the device;
 The global variables a, b are defined as type unsigned
integer, and are used to store the Fibonacci numbers of
order N and N-1, respectively;
 These variables are initialized with the values {0, 1},
respectively;
 The control variable i is used as the iterations counter.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
9
Lab1.1 Introduction to debugging an
application
Step 1. Creating the project
 B. Add a source code file (continued):
UBI
 The first message is sent to the console using the function
printf; (Note: this is printf, not print);
 The message consists of a sequence of text lines, so the
escape character \n is used to cause a change of line on the
console;
 Within a code structure, the numbers of the Fibonacci
sequence are calculated successively using the mathematical
expression stated earlier;
 Finally, the code ends with the directive _NOP()
corresponding to the execution of a no operation, which has
no effect, except taking time to execute .
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
10
Lab1.1 Introduction to debugging an
application
Step 2. Configuration
 The project must be configured after writing the code:
UBI
 Go to Project > Properties > C/C++ Build > MSP430
Linker 3.0 Runtime Environment to:
 Specify heap Size for C/C++ dynamic memory
allocation (--heap_size) with the value 400;
 set C system stack size (--stack_size) with the value 200;
 Because the printf function is used, the type of implementation
must also be specified;
 In MSP430 Compiler V3.0 > Library Function Assumption
option select full;
 Confirm in the Run Time Model Options that the silicon
version (--silicon_version) is defined as mspx.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
11
Lab1.1 Introduction to debugging an
application
Step 2. Configuration
UBI
 In the TI Debug Settings field, specify the connection type
between the PC and hardware:
 Choose the link TI MSP430 USB1;
 At the Debugger tab, confirm the selection of options Connect to
exact CPU and load Program in loading options;
 At the Target tab, ensure through Program load options that the
use of the IO console CIO is active in Enable CIO Function Use
and that the verified program is written to the device in Perform
Verification During Program Load;
 Ensure that in Auto run option, the program begins to run at the
main routine;
 Select the two options On the Load or Restart Program and On
reset.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
12
Lab1.1 Introduction to debugging an
application
Step 3. Compilation
 Once the configuration is made, the project must be
compiled using the option Project > Build Active Project:
UBI
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
13
Lab1.1 Introduction to debugging an
application
Step 4. Project debug
 To debug the code, it is also necessary to process the
code using Run > Debug Active Project;
UBI
 CCE will automatically switch to the Debug perspective;
 The following tasks will show how the application can be
verified using the debug features of CCE .
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
14
Lab1.1 Introduction to debugging an
application
Step 4. Project debug
 A. Observe the contents of variables and expressions:
UBI
 In the Variables view, click on the icon
and mark the
global variables a, b, and i to identify them;
 The contents of these variables will be available immediately;
 As the program has not yet begun to run, the values are
undefined;
 In the Expressions view, add an expression that the debug
will automatically calculate during the debugging process;
 Example: using the context menu option , the global
variables addresses are available through the expressions
&a, &b and &i;
 Request the number of Fibonacci numbers yet to be
calculated by using the expression 7-i.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
15
Lab1.1 Introduction to debugging an
application
Step 4. Project debug
 B. Run the code step by step:
UBI
 Execute the application step by step;
 Press the F6 key to execute the first instruction;
 The program’s status indicator will indicate the next
instruction to be executed and stop the watchdog timer;
 Note that after the execution of the lines that initialize the
values of variables a and b, the Variables view displays its
new values;
 Observe the messages sent to the IO console;
 Display the CIO console by pressing the icon
choosing console 3;
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
, then
16
Lab1.1 Introduction to debugging an
application
Step 4. Project debug
 B. Run the code step by step (continued):
UBI
 To ensure that the display remains visible press the icon
;
 Later, as the application runs step-by-step (F6), the
messages will appear in the CIO console:
Lab 1 - Introduction to Debug with CCE V3
Fibonacci sequence computation
Number n = 0 – 0
Number n = 1 – 1
 At the end of this sequence of instructions, the variables have
the following values:
a = 0,
b = 1.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
17
Lab1.1 Introduction to debugging an
application Step 4. Project debug
UBI
 C. Run the application until a specific code line:
 Putting the cursor on line of code 48, corresponding to:
printf("Number n = %d - %d\n", i, c);
 Select the command Run to line, to set the program
execution status indicator to that line;
 The sequence of instructions that led up to this point has
been executed, modifying the program variables on the way;
 The local variable c was automatically added to the
Variables view.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
18
Lab1.1 Introduction to debugging an
application
Step 4. Project debug
UBI
 D. Restart the debugging task:
 Once the application is finished, or whenever a repeated debug of
code is required, it is possible to restart the device;
 Choose the reset CPU option in Run > Reset;
 Note that the memory of the processor remains unchanged;
 Re-run the application. Place the cursor at the line of code 36 and
run the program until it reaches the cursor position;
 Using the Variables view, change the contents of variables a and b
to 3 and 4, respectively;
 Execute the application until line of code 52;
 Observe the new sequence of values for the Fibonacci numbers.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
19
Lab1.1 Introduction to debugging an
application
Step 4. Project debug
 E. Include the project in the workspace:
UBI
 The project built during this laboratory can be found in the
Chapter 2 > Lab1_CCE directory of the CD ROM;
 To include it in the workspace, choose the project using
Project > Open Existing Project;
 Perform this laboratory starting at point B.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
20
Lab1.2 Breakpoint to save/load data
UBI
 Overview:
 During debugging, it is useful to be able to exchange
information between the application and files on disk;
 This laboratory will make use of this capability, using the
project developed in Lab1.1;
 This time, the two initial values required to determine the
sequence of Fibonacci numbers are collected from a data file;
 The numbers are successively calculated by the application
and are then stored in a data file.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
21
Lab1.2 Breakpoint to save/load data
Step 1. Include the project in the workspace
UBI
 This task is identical to that carried out point E of Lab 1.1;
 Compile the lab with the file Lab1a.c.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
22
Lab1.2 Breakpoint to save/load data
Step 2. Creating input and output data files
UBI
 The code debugger can access binary files in COFF
(Common Object File Format) format or files in text
format;
 The latter format is used in this laboratory;
 This file has a header line followed by several lines with
one value per line;
 The data can be stored in the following formats:
 Hexadecimal (representation of all header number);
 Integer;
 Long;
 Float.
 The information contained in the header line uses the
following syntax:
MagicNumber Format InitialAddress PageNumber Length
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
23
Lab1.2 Breakpoint to save/load data
Step 2. Creating input and output data files
UBI
MagicNumber Format InitialAddress PageNumber Length
where:
• MagicNumber: Constant value equal to 1651;
• Format: Value between 1 and 4, indicating the format
used in the file samples;
• InitialAddress: Address of the start of the data block;
• PageNumber: Page number from which the data block was
obtained;
• Length: Number of samples in the block.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
24
Lab1.2 Breakpoint to save/load data
Step 2. Creating input and output data files
UBI
 A. Create the data files:
 CCE can create this file through File > New > File;
 Include the name DataIn_a.txt on file dialog box to create
the file containing the data to be allocated to the variable a:
1651 1 135c 0 2
2
 Repeat the process for the data file of variable b:
1651 1 135c 0 2
3
 Note that CCE expects that the read values are represented
using 5-digit of values with 4 digits. If the data values are
represented in hexadecimal, the CCE expects the first digit to
be zero.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
25
Lab1.2 Breakpoint to save/load data
Step 2. Creating input and output data files
UBI
 B. Associate the input data files with the project:
 This file is available in the directory Chapter_2 > Lab1_CCE
 There are two different ways to add the file to the project:
• Copying the file to the project directory:
– Loses the freedom to control the file contents if it is
used in several projects;
• Link the file to the project:
– This option allows the file to be stored in a common
location and associated with the project;
– The same file can be used on different projects (a
change to its content will be observed by all projects
that use it).
 In this example, the files are in the project directory.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
26
Lab1.2 Breakpoint to save/load data
UBI
Step 3. Adding and associating breakpoints
 The easiest way to set a breakpoint is through the
Breakpoint view;
 This view allows choice of action, including the reading or
writing data on file;
 Choosing one of these actions allows a file to be linked to
the breakpoint.
 A. Activate the Breakpoint view:
 If the breakpoint window is not already open, then do so in
Windows > Show View > Breakpoints.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
27
Lab1.2 Breakpoint to save/load data
Step 3. Adding and associating breakpoints
UBI
 B. Create Breakpoints:
 Create two breakpoints, one at line of code 36 and another
at line of code 37;
 Move the cursor to the line of code and add the breakpoint by
clicking on the left mouse button to provide access to the C
editor content menu;
 In this menu choose the option Toggle Breakpoint.
 C. Setting the Breakpoint:
 In Breakpoint view, after selecting line of code 36
breakpoint, edit its properties;
 Choose the option Action and open the list of options then
choose Read Data from File.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
28
Lab1.2 Breakpoint to save/load data
Step 3. Adding and associating breakpoints
UBI
 D. Filling the Breakpoint options:
 It is necessary to input several items of data:
• File: Location and file name from where the data will be
read;
• Wrap around: Mark this selection to start reading at the
beginning of the file again after it reaches the end;
• Start Address: Location to send the data that has been
read from the file. This address can be changed, because
it is always accessed at the beginning of each read;
• Length: The length of memory. This parameter can also
be modified through the debug process.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
29
Lab1.2 Breakpoint to save/load data
Step 3. Adding and associating breakpoints
UBI
 D. Filling the Breakpoint options (continued):
 To set the Breakpoint associated with line of code 36:
• File: DataIn_a.txt
• Warp around: Yes
• Start Address: &a
• Length: 1
• Name: Breakpoint Load a.
 To set the Breakpoint associated with line of code 37:
• File: DataIn_b.txt
• Warp around: Yes
• Start Address: &b
• Length: 1
• Name: Breakpoint Load b.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
30
Lab1.2 Breakpoint to save/load data
Step 3. Adding and associating breakpoints
UBI
 D. Filling the Breakpoint options (continued):
 Create a breakpoint at line 48 to associate the file used to
write the values of variable c (Fibonacci numbers);
 Follow the same steps for setting this breakpoint, edit its
properties and choose Write Date to File in option Action;
 The data values to fill in are:
• File: DataOut_c.txt
• Format: Hex
• Start Address: &b
• Length: 1
• Name: Breakpoint Write c.
 Finally, add a breakpoint on line of code 52 named Final
Point to suspend the execution of the application at the end
of the Fibonacci numbers calculation.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
31
Lab1.2 Breakpoint to save/load data
Step 4. Save file and load files with the breakpoints
UBI
 The breakpoints created in this laboratory are read and
written;
 This feature is useful to reuse the breakpoints
configuration;
 A. Export the breakpoints to file:
 After creating and setting up breakpoints, this information
can be saved by exporting it to a file;
 Go to File > Export;
 In the General item, choose the option Breakpoints;
 All breakpoints defined in the debugger will be shown. Select
them all;
 Inform the file name to make, name it Lab1_breakpoint.
 B. Import breakpoints from the file:
 To import the breakpoint use the Import feature;
 Indicate the file name and select the two options to
automatically create and update the breakpoint imported.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
32
Lab1.2 Breakpoint to save/load data
Step 5. Code debugging
UBI
 Tasks required to debug the code:
 To debug the code, perform the following tasks:
 Verify that the execution state indicator points to line of code
27. If this does not happen, restart
the device;
 Verify that the CIO console is clean and visible. If these
conditions are not fulfilled, use the context menu to order its
cleaning
and activate the icon
to always make the
console visible;
 Execute the application step by step
and b are initialized;
until the variables a
 Verify in the Variables view that they are assigned the
values 0 and 1, respectively;
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
33
Lab1.2 Breakpoint to save/load data
Step 5. Code debugging
UBI
 Tasks required to debug the code (continued):
 When the line of code 36 is reached (before being executed),
variable a is assigned the value 2, read from the associated
data file;
 The same thing happens when it reaches line of code 37,
because when it is reached, it leads to the execution of
breakpoint Load B, resulting in reading the value 3 for the
variable b;
 The Fibonacci numbers calculation is initiated within the
computing cycle, using these initial conditions ;
 Whenever the line of code is reached, the breakpoint Save
c runs;
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
34
Lab1.2 Breakpoint to save/load data
Step 5. Code debugging
UBI
 Tasks required to debug the code (continued):
 Successive data values are stored in the data file;
 Start execution of the application using the command run;
 The execution runs until the breakpoint Final Point is
reached. In this execution state, the application has already
determined and stored all the Fibonacci numbers in the file;
 Switching to the C/C++ perspective, in the C/C++
Projects view, the data file DataOut_c created is visible ;
 Edit this file to see the results. Observe that the
mathematical process returned the sequence: 2, 3, 5, 8, 13,
21, 34, 55 and 89.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
35
Lab1.3 Advanced breakpoints with triggers
UBI
 Overview:
 Laboratory 1.2 has shown how breakpoints can be used to
run code to certain points in the program and control access.
It has also shown how, under specific conditions, data can be
exchanged with files;
 The use of these features assists the debugging task of some
complex applications;
 This laboratory shows how breakpoints can be set through
the appropriate use of triggers;
 Continue to use code example from Laboratory 1.2, since the
changes to be introduced are only small;
 This time breakpoints will be implemented under special
conditions.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
36
Lab1.3 Advanced breakpoints with triggers
Step 1: Preliminary conditions
UBI
 The change introduced in the source code of this project
is to add a data array to store the calculated results;
 The file Lab1b.c incorporates this modification. Compile
the project with this new source file then debug it;
 There is a wide variety of available triggers that can
generate an appropriate response by the debugger;
 To understand the logic associated with the configuration
of these features, let us perform some actions for that
purpose.
 Remove all existing breakpoints by selecting the icon
in
the Breakpoint view.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
37
Lab1.3 Advanced breakpoints with triggers
Step 1: Preliminary conditions
UBI
 A. Read access to a variable:
 Set up a new breakpoint using the icon
Breakpoint view;
of the
 Chose the option watchpoint;
 Introduce the following configuration:
• Location: &a
• Access type: Read.
 Execute the application with Run;
 Note that at the first access read of variable a, the execution
of the application is suspended.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
38
Lab1.3 Advanced breakpoints with triggers
Step 1: Preliminary conditions
UBI
 B. Access to a variable using a condition:
 Specify and provide that the suspension should only occur if
a particular value is read;
 Remove the last breakpoint and set a new one (watchpoint
with Data type);
 Introduce the following configuration:
• Location: &a
• Data value: 5
• Access type: Read.
 Execute the application with Run;
 At the first access, read the variable: a = 5.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
39
Lab1.4 Memory and register use
UBI
 Overview:
 The assessment of memory resources used by the application
is crucial to the development of the application;
 This laboratory illustrates how to access to the memory and
registers of the device ;
 The use of the system stack by the C/C++ is described;
 The practical process is illustrated by an example;
 For this, the program will change from C/C++ to assembly
language. Some of the tools used to analyze programs in
assembly language are briefly described.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
40
Lab1.4 Memory and register use
Step 1. Using the stack with C/C++
UBI
 The C/C++ compiler uses a system stack in memory to:
 Store local variables;
 Pass necessary arguments to execute a task;
 Save the register contents for subsequent restores.
 The system stack grows from high value addresses to low
value addresses;
 Its management is done using the device register R1,
commonly referred to as the Stack Pointer ( SP);
 The size of the system stack is specified in the process of
building the application(_stack_size):
 The default value is 128 bytes;
 Can be modified using the option _stack_size.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
41
Lab1.4 Memory and register use
Step 1. Using the stack with C/C++
UBI
 The size of registers is greater in MSP430X devices;
 Saving and restoring stack contents requires the use of a
32-bit stack (2 words);
 To reuse code originally written for 16-bit devices, it is
necessary to increase the system stack size;
 When the application boots up:
 The SP points to the address at the top of the system stack;
 This address is the first location after the end of the section
reserved for the system stack;
 Before performing a function, the C/C++ automatically
decreases the SP to reserve the space needed for its
execution (push operation);
 When the function returns, the SP is increased to restore the
original value (pop operation).
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
42
Lab1.4 Memory and register use
Step 1. Using the stack with C/C++
UBI
 By convention, some registers are dedicated to specific
operations in the C/C++ environment:
Register type
Argument register
Return register
Expression register
Argument pointer
Stack pointer
Program counter
>> Contents
Description
Passes arguments during a function call
Holds the return value from a function call
Holds a value
Used as a base value from which a function’s parameters
(incoming arguments) are accessed
Holds the address of the top of the software stack
Contains the current address of code being executed
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
43
Lab1.4 Memory and register use
Step 1. Using the stack with C/C++
UBI
 The debugger uses each of these registers:
Register
R0
R1
R2
R3
R4 – R10
R11
R12
R13
R14
R15
Alias
PC
SP
SR
Usage
Program counter
Stack pointer
Status register
Constant generator
Expression register
Expression register
Expression register, Argument pointer,
return register
Expression register, Argument pointer,
return register
Expression register, Argument pointer
Expression register, Argument pointer
Preserved by function(1)
N/A
N/A (2)
N/A
N/A
Child
Parent
Parent
Parent
Parent
Parent
The parent function refers to function making the function call. The
child function refers to function being called.

(1)

(2)
The SP is preserved by the convention that everything pushed on the
stack is popped before returning.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
44
Lab1.4 Memory and register use
Step 1. Using the stack with C/C++
UBI
 Any function (Parent function) that calls or is called by
another function (Child function), must follow a set of
rules;
 How a function call is processed:
 Argument block:
• Part of the local frame used to pass arguments to other
functions;
• The arguments are passed to the functions by moving
them to this data block instead of placing them on the
system stack;
• A local frame and the argument block are preserved
simultaneously;
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
45
Lab1.4 Memory and register use
Step 1. Using the stack with C/C++
UBI
 How a function call is processed (continued):
 Register save area: Part of the local frame used to store the
contents of registers when the application invokes a function,
allowing it to be restored on return;
 Save-on-call register: Registers R11-R15.
• The function invoked does not preserve the values of these
registers, so the function that performs their invocation must
save their contents then restore them, as necessary;
 Save-on-entry registers: Registers R4-R10.
• It is responsibility of the function preserve the values of these
registers;
• If the function invoked modifies the contents of these registers,
their contents are stored and restored when the function
returns.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
46
Lab1.4 Memory and register use
Step 1. Using the stack with C/C++
UBI
 Typical function invocation:
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
47
Lab1.4 Memory and register use
Step 2. Include the project in the workspace
UBI
 Start the task, including the compilation of the file
Lab1c.c;
 Alternatively, create of the project from the beginning,
following the steps described in previous laboratories;
 The source code may be imported or you may write the
code from scratch into a new file;
 The task performed by this application is similar to that
performed so far;
 In this laboratory, the determination of the new number
of the Fibonacci sequence is performed by a function;
 This function receives the addresses of the variables a
and b, and returns the result of the mathematical
operation.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
48
Lab1.4 Memory and register use
Step 3. Program and assembly view
UBI
 Once the compiling process is completed, observe the
Debug perspective;
 It is possible to observe the assembly code in the
Disassembly view.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
49
Lab1.4 Memory and register use
Step 4. Analysis of the device's memory
UBI
 The contents of the device’s memory are accessible
through the Memory view:
 The Memory view has two panels:
• Memory Monitors panel: Collects the memory monitors
added during the debug session;
• Memory Renderings panel: The content of this panel is
controlled by the selection made in Memory Monitors
panel and lists the contents of the desired memory
address.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
50
Lab1.4 Memory and register use
Step 4. Analysis of the device's memory
UBI
 Add memory addresses to monitor some of the
application’s variables. Use the button
to add the
address to be monitored.
 Examine:
 Global Variables a and b: Insert &a and &b
 Top C system stack: Insert &_stack
 Address pointed by SP: Insert SP.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
51
Lab1.4 Memory and register use
Step 4. Analysis of the device's memory
UBI
 Determine the locations of variables. From the project
compilation results:
• Address 0x01344 is used for variable a;
• Address 0x01346 is used for variable b.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
52
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 During the introduction to this laboratory, a brief
description was given of the procedure used by the
system to pass and receive data from a function;
 Use the CCE debugging tools to see an example of this
process.
 A. Reinitiate the debug process:
 The first task is to ensure that the application is ready to be
launched;
 The starting point is the main position;
 Proceed with device restart through the icon
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
.
53
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 B. Preparation to call a function:
 Put the cursor at line 50 and then execute the application
until this line is reached, using the feature Run to line;
 In the Disassembly view, the instructions before the
addData function execution call are shown:
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
54
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 B. Preparation to call a function (continued):
 The address of the variable a is 0x1344 and the address of
variable b is 0x1346;
 In this case, the compiler begins by loading the address of
each of these variables into the registers R12 and R13,
respectively, through the instructions:
MOV.W #0x1344, R12
MOV.W #0x1346, R13
 Verify these instructions (Registers view);
 Stack address pointed by SP is 0x0030F0;
 Stack begins at 0x03038 (supports 180 bytes).
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
55
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 B. Preparation to call a function (continued):
 Using the instruction:
CALLA #addData
 The function is then invoked;
 Following the call, the processor automatically puts the return
address on the system stack ;
 It requires 2 bytes to store the 16-bit address, being stored
on the system stack at addresses 0x30EC and 0x30ED.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
56
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 C. Analysis of the execution in assembly language:
 The function begins by reserving space on the system stack
then passing two arguments on the stack:
SUB.W
MOV.W R13,
MOV.W R12,
#0x0006,SP
0x0002(SP)
0x0000(SP)
 Initially, the SP points to the address 0x30EC, then the
constant value #6 is subtracted from it. This makes the stack
base address 0x030E6;
 Next the parameters passed by the register R12 and R13 are
saved on the stack at SP+0 and SP+2 (addresses of variables
a and b).
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
57
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 C. Analysis of the execution in assembly language
(continued):
 The new Fibonacci number is now determined ;
 Start by adding the contents of the memory addresses sent;
 This is accomplished with the following sequence of
instructions:
MOV.W 0x0002(SP),R15
MOV.W @R15,R15
ADD.W @R12,R15
MOV.W R15,0x0004(SP)
 The processor begins by copying the value stored on the
system stack at SP+2 (variable b address) to R15. Then the
contents of the register pointed to by R15 (variable b
address) is copied to register R15 itself.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
58
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 C. Analysis of execution in assembly language
(continued):
 Following this statement, register R15 contains variable b;
 The addition operation is then performed by adding the value
contained at the address stored in register R12 (variable a)
with the value contained in register R15 (variable b);
 The result of the addition in R15 is placed on the system
stack at the address SP+4, which is the space reserved for
the local variable c;
 Next part of code updates variable a:
MOV.W 0x0002(SP),R15
MOV.W @SP,R14
MOV.W @R15,0x0000(R14)
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
59
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 C. Analysis of execution in assembly language
(continued):
 The first instruction moves the address stored on the system
stack at position SP+2 (address of variable a) into register
R15;
 The second instruction moves the address contained on the
system stack at position SP+0 (address of variable b) into
register R14;
 Finally, the contents of the data memory location whose
address is stored in R15 (variable b) are moved to the
contents of the data memory location whose address is
stored in the register R14 (variable a);
 With this sequence of operations, the value in variable a will
be equal to the value of variable b;
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
60
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 C. Analysis execution in assembly language (continued)
 The next part of code is to update of variable b by writing to
it the result stored in local variable c;
MOV.W 0x0002(SP),R15
MOV.W 0x0004(SP),0x0000(R15)
 Initially, the address stored on the system stack at SP+2 is
moved into register R15. Register R15 now holds the address
of variable b;
 Next, the value stored on the system stack at SP+4 (local
variable c) is copied to the data memory value whose
address is stored in R15 (address of variable b);
 This copies variable c to the variable b.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
61
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 D. Returning the results function
 The final task is to return the result in register R12 then
restore the SP to its original value ;
MOV.W 0x0004(SP),R12
ADD.W #0x0006,SP
RETA
 The code begins by moving the value stored on the system
stack at SP+4 (local variable c) to register R12;
 Add to SP register SP the value subtracted at the beginning
to restore the original stack state;
 The register SP will point back to the position 0x030EC;
 Finally, return to the main function.
>> Contents
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
62
Lab1.4 Memory and register use
Step 5. Interface with a function
UBI
 E. Function result storage
 After returning to the main function, the result is again
stored on the restored the system stack using the
instruction:
MOV.W R12,
>> Contents
0x0006 (SP)
Copyright 2009 Texas Instruments
All Rights Reserved
www.msp430.ubi.pt
63