Laboratory 1: “Hello World” Beginner’s project
The following laboratories provide an overview of the features of CCE.
Lab1.1 – Introduction to the application debug
Project files
C source files: Chapter 2 > Lab1_CCE > Lab1a.c
Overview
This laboratory shows how to use the basic features of CCE to allow applications to be built and debugged. All the steps needed to create a project, including its configuration, as described above, are illustrated using an example. The application is downloaded to the device after the project has been successfully compiled and built. Several debugging techniques are used, such as: step-by-step execution, analysis of the contents of local and global variables, resetting the device, etc.
Step 1: Creation of the project
A project is to be developed that sends a set of numbers corresponding to the Fibonacci series to the CCE console. The Fibonacci series can be recursively defined by the expression:This sequence was originally used by Leonardo of Pisa (also known as Fibonacci, circa 1200 A.D.) to describe the growth of a rabbit population. With this formula, the sequence of Fibonacci numbers can be determined and answer to the question "how many rabbits were born in the sixth month?", in which the correct answer is, "in the sixth month 8 rabbits were born."
The algorithm that solves this problem is simple to develop. Beginning with the first two values of the sequence, the remaining values are successively calculated.
The sequence of actions to build the project is described below.
A. Creating the project
Create a new project in Project > New Managed C/ASM Project;
In project name write Project1;
Accept the default settings in Select a type of project;
There should not be any dependency on other existing projects;
In Device Selection Page, choose in the option Device Variant, the device MSP430FG4618;
Automatically, the CCE will select the appropriate debug file (lnk_msp430fg4618.cmd) and the support library (rts430x.lib);
Complete the creation of the project by clicking the button finish.
B. Add a source code file
The project created is visible in the C/C++ Projects window using 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 created should be named Lab1a.c; Write the code below that solves the problem:
//*************************************************************************
// Basic debug introduction using CCE. Application conditional execution
//*************************************************************************
#include
#include
//*************************************************************************
//*************************************************************************
//*************************************************************************
//*************************************************************************
void main (void)
{
// Stop watch dog
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
a = 0;
// First message to CIO
printf("Lab 1 - Introduction to Debug with CCE V3\n");
printf("Number n = 0 - %d\n", a);
printf("Number n = 1 - %d\n", b);
for(i = 0; i < 7; i++){
c = a + b;
printf("Number n = %d - %d\n", i, c);
}
//*************************************************************************
// Basic debug introduction using CCE. Application conditional execution
//*************************************************************************
#include
#include
//*************************************************************************
// 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);
}
}
The code starts with a descriptive header of the contents of the file and other essential aspects, such as: author, date and version. Other contents may be added;
The global variables a, b are defined as 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},
The control variable i is used to control the number of iterations used in the sequence
The first message is sent to the console using the instruction printf;
The message consists of a sequence of text lines, so it uses the escape character \n, causing a
Within the code structure, the numbers of the Fibonacci sequence are calculated successively
Finally, the code ends with the directive _NOP(), corresponding t
Step 2. Configuration
The project must be configured after writing the code.
Go to Project > Properties and select the field MSP430 Linker 3.0 Runtime Environment in the option C/C++ Build;
Since the printf library function is used, the type of implementation in use
In MSP430 Compiler V3.0 on the option Library Function Assumption choose to full;
Choose the link TI MSP430 USB1;
At Debugger tab confirm the selection of options Connect to exact CPU and load
Ensure that in Auto run options, the program begins to run in the main routine. Select the two options On the Load or Restart Program and On reset.
Step 3. Compilation
Once the configuration has been set up, the project must be compiled through the option
Step 4. Project debug
To debug the code it is necessary to download it to the device through the function Run > Debug Active Project.
The CCE will automatically switch to the Debug perspective;
A. Observe the contents of variables and expressions
In the Variables window press the
The contents of these variables can be monitored immediately;
In the Expressions window, add an expression that the debug will automatically calculate through the debugging process. For example, using the context menu option
Request the number of Fibonacci numbers remaining to calculate using the expression 7-i.
B. Run the code step-by-step
Execute the application step-by-step;
By pressing the key F6, the first instruction is executed;
Display the CIO console pressing the icon
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 should have the following values of a = 0, b = 1.
C. Run the application until a specific code line
printf("Number n = %d - %d\n", i, c)
Select the command Run to line execution, to lead 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.
The local variable c was automatically added to the Variables window.
D. Restart the debugging task
Once the application has finished running, or whenever it is desired to repeat the code debug, 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 code line 36 and order it to run the program until the cursor position;
Using the Variables window, 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.
E. Include the project in the workspace
The project built during this laboratory can be found in code folder Lab1. To include it in the workspace, choose the project through Project > Open Existing Project;
Perform this laboratory starting at point B.
Lab1.2 – Using breakpoint to save and load data to and from file
Project files
C source files: Chapter 2 > Lab1_CCE > Lab1a.c
Overview
The debugging of an application can benefit from the ability to exchange information with files on disk. This laboratory will exploit this capability using the project developed in Lab1. Now, the two initial values required to determine the sequence of Fibonacci numbers are collected from a data file. The numbers are successively calculated and are also stored in a data file.
Step 1. Include the project in the workspace
This task is identical to that carried out Step E of Lab 1.1. Compile the laboratory with the file Lab1a.c.
Step 2. Creating input and output data files
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, integer, long, float. The information contained in the header line has the following syntax:
MagicNumber Format InitialAddress PageNumber Length
where:
MagicNumber: constant value equal to 1651;
Format: Value between 1 and 4, indicating the format used in file samples;
InitialAddress: Address of the start of the data block;
PageNumber: Page number from which the data block was cobtained;
Length: Number of samples in the block.
All the numbers represented in the header are in hexadecimal format.
A. Create the data files
The 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 values read are represented using 5 values, each with 4 digits. If the data values are represented in hexadecimal, then CCE expects the first digit to be zero.
B. Associate the input data files to the project
The project built during this laboratory can be found in the code folder Lab1 and can be added to the project using two different procedures:
o Copy the file to the project directory: This solution has the disadvantage of copying the file to the directory of the project. It loses any freedom to control the file contents if it is used in several projects because there can be multiple sources of that file;
o Link the file to the project: This option allows the file to be stored in an appropriate location and connect it with the project without copying it to the project directory. The same file can be used on different projects, and a change to its content will be observed by all projects that use it.
In this example, the files are copied into the project directory.
Step 3. Add breakpoints to the application and associate them with files
The easiest way to set a breakpoint is through the Breakpoint window. This window allows the choice of action, including the reading or writing data on file. Choosing one of these actions allows the linking of a file to the breakpoint.
A. Activate the Breakpoint window
If the breakpoint window is not already open, do so in Window > Show View > Breakpoints.
B. Create Breakpoints
Create two breakpoints, one at line of code 36 and another at line of code 37;
Click on the line of code and add the breakpoint using the left mouse button, in order to provide access to the C editor content menu;
In this menu choose the option Toggle Breakpoint.
C. Setting the Breakpoint
In Breakpoint window, after selecting the line of code 28 breakpoint, edit its properties;
Choose the option Action and open the list of options to choose Read Data from File.
D. Filling the Breakpoint options
It is required to fill out the following fields:
o File: location and file name from where the data will be read;
o Wrap around: mark this selection to start reading at the beginning of the file once it reaches the end;
o Start Address: Location to send the data read from the file. This address can be changed, since it is always re-evaluated at the beginning of each read;
o Length: The length of memory. This parameter can also be modified by the debug process.
To set the Breakpoint associated with the line of code 36, configure:
o File: DataIn_a.txt o Wrap around: Yes o Start Address: &a
o Length: 1
o Name: Breakpoint Load a
To set the Breakpoint associated with the line of code 37, configure:
o File: DataIn_b.txt
o Wrap around: Yes
o Start Address: &b
o Length: 1
o Name: Breakpoint Load b
For the file used to write the values of variable c (Fibonacci series of numbers), create a breakpoint to associate the file to line of code 48;
Follow the same steps for setting the previous breakpoint, edit its properties and choose Write Date to File in the option Action;
The data values to configure are:
o File: DataOut_c.txt
o Format: Hex
o Start Address: &b
o Length: 1
o Name: Breakpoint Write c
Finally, add a breakpoint at line 52 named Final Point, to suspend the execution of the application at the end of the calculation of the Fibonacci numbers.
Step 4. Save file and load files with the breakpoint information
The breakpoints created in this laboratory can be written and read. This feature is useful in order to reuse the breakpoint configuration.
A. Export the breakpoints to file
After creating and setting up the breakpoints, as described in the previous steps, the breakpoint 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 debugger will be present. Select them all;
Specify the file name to make, name it Lab1_breakpoint.bkpt.
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.
Step 5. Code debugging:
The data file exchange operation can be verified during the debug process. 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, order a restart of the device;
Verify that the CIO console is clean and visible. If these conditions are not fulfilled, using the context menu, order its cleaning and activate the icon to turn the console to always visible;
Execute the application step-by-step until the variables a and b are initialized. Verify that in the Variables window that they take the values 0 and 1, respectively;
When the code line 36 is pointed to (before being executed), the variable a takes the value 2, read from the respective data file. The same thing will happen when it reaches code line 29, since when it is pointed to, it leads to the execution of breakpoint Load B, resulting in reading the value 3 for the variable b;
The Fibonacci number calculation process, using these initial conditions, is initiated within the computing cycle. Whenever the code line is reached, the breakpoint Save c runs. Successive data values are stored in the data file associated with the breakpoint. Order the execution of the application using the command run .The execution takes place until the breakpoint Final Point is reached. In this execution state, the application already determined and stored all the Fibonacci numbers in the file;
Switching to the C/C++ perspective, in the C/C++ Projects window, it is now possible to see that the data file DataOut_c has been created. Edit this file to see the results. Note that the calculation process returned the sequence: 2, 3, 5, 8, 13, 21, 34, 55, 89.
pag 2-47