MCBSTM32C Lab PDF
MCBSTM32C Lab PDF
MCBSTM32C Lab PDF
ARM Keil MDK Toolkit featuring Serial Wire Viewer and ETM Trace
For Keil MCBSTM32C Eval Board
Spring 2011
Version 1.0
by Robert Boys,
bob.boys@arm.com
Introduction:
The purpose of this lab is to introduce you to the STMicroelectronics Cortex-M3 processor using the ARM Keil MDK
toolkit featuring the IDE Vision. We will use the Serial Wire Viewer (SWV) and ETM trace on the Keil MCBSTM32C
evaluation board. At the end of this tutorial, you will be able to confidently work with STM32 processors and Keil MDK.
This tutorial will also work with the STMicroelectronics STM3210C-EVAL board with no modifications.
Keil MDK comes in an evaluation version that limits code and data size to 32 Kbytes. Nearly all Keil examples will compile
within this 32K limit. The addition of a license number will turn it into the full, unrestricted version. Contact Keil sales for a
temporary full version license if you need to evaluate MDK with programs greater than 32K. MDK includes a full version of
Keil RTX RTOS. No royalty payments are required. If you need RTX source code, this is included in Keil RL-ARM.
www.keil.com
Index:
6
7
2. Hardware Breakpoints:
3. Locals Window
10
10
10
10
11
12
12
13
14
15
16
17
18
19
19
20
c. PC Samples:
21
22
23
2. Code Coverage:
24
3. Performance Analysis:
4. Execution Profiling:
25
26
5. In-the-weeds Example:
27
28
29
30
2
www.keil.com
Processor
MCBSTM32
STM32F103VB
monochrome LCD
JTAG/SWD
color LCD
STM3210E-EVAL
ST board equivalent
MCBSTM32EXL
STM32F103ZG
color LCD
STM3210E-EVAL
MCBSTM32C
STM32F107VC
STM3210C-EVAL
EK-STM32F
STM32L152-EVAL
STM32100E-EVAL
STM32-Discovery
STM32F10X-EVAL
STM3220F-EVAL
Physically connect a ULINK to the MCBSTM32C or other target board. Power both of these with USB cables.
2.
Configure Vision to use a ULINK2, ULINK-ME or ULINKpro to communicate with the JTAG or SWD port.
3.
Configure the Flash programmer inside Vision to program the STM32 internal flash memory.
4.
If desired, configure the Serial Wire Viewer. Add the special STM32 ini initialization file (see below).
5.
If desired, configure the ETM trace with the ULINKpro. Add the special STM32 ini initialization file (see below).
STM32 processors need a special .ini file that configures the CoreSight Serial Wire Viewer and/or ETM trace. If you do not
intend to use SWV or ETM you do not need this file. It is entered in the Options for Target window under the Debug tab. It
needs to be configured for either SWO or 4 bit Trace Port operation. SWO is default. Instructions are provided later.
Software Installation:
This document was written for Keil MDK 4.14 or later which contains Vision 4. The evaluation copy of MDK is available
free on the Keil website. Do not confuse Vision4 with MDK 4.0. The number 4 is a coincidence.
To obtain a copy of MDK go to www.keil.com/arm and select Evaluation Software from the left column.
You can use the evaluation version of MDK and a ULINK2, ULINK-ME, ULINKpro, J-Link or JtagJetTrace for this lab.
You must make certain adjustments for non-ULINK adapters and not all features shown here will be available.
The addition of a license number converts the evaluation into a full, unrestricted copy of MDK.
The ULINKpro adds Cortex-M3 ETM trace support. It also adds faster programming time and better trace display. Most
STMicroelectronics Cortex-M3 parts are equipped with ETM. All have SWV.
JTAG: JTAG provides access to the CoreSight debugging module located on the STM32 processor. It uses 4 to 5 pins.
SWD: Serial Wire Debug is a two pin alternative to JTAG and has about the same capabilities except no Boundary Scan.
SWD is referenced as SW in the Vision Cortex-M Target Driver Setup. See page 5, middle picture.
SWV: Serial Wire Viewer: A trace capability providing display of reads, writes, exceptions, PC Samples and printf.
SWO: Serial Wire Output: SWV frames come out this one pin output.
Trace Port: A 4 bit port that ULINKpro uses to output ETM frames and optionally SWV (rather than the SWO pin).
ETM: Embedded Trace Macrocell: Provides all the program counter values. Only the ULINKpro works with ETM.
Example Programs:
Keil provides examples for evaluation boards made by Keil in C:\Keil\ARM\boards\Keil. Most example projects are preconfigured to use a ULINK2 or a ULINK-ME. Serial Wire Viewer is not usually configured. Projects that contain a Ulp in
their directory name are configured to use a ULINKpro. SWV and ETM are normally pre-configured. For instructions on
converting a project from ULINK2 or ULINK-ME to a ULINKpro and vice versa please see the instructions under Part A.
Example projects for STMicroelectronics boards are found under C:\Keil\ARM\boards\ST.
Most example projects will compile within the 32 K code and data limit of MDK. Exceptions are LCD_Demo and Demo. A
compiled executable .axf file is provided to allow you to run, evaluate and debug these programs. If you attempt to compile
these projects the .axf file will be erased. You must reinstall MDK to get the .axf file back unless you backed it up first.
3
www.keil.com
Part A)
1) Connecting ULINK2, ULINK-ME or ULINKpro:
Keil MCBSTM32C is equipped with the new ARM standard 10 and 20 pin Hi-density connectors for JTAG/SWD, SWO and
ETM access as shown here:
The legacy 20 pin JTAG connector is provided. This
provides JTAG, SWD and SWO access.
The 10 pin Cortex Debug provides JTAG, SWD and
SWO access in a much smaller footprint. This connector
is supported by ULINK2 and ULINK-ME with a special
supplied cable.
The 20 pin Cortex Debug + ETM provides JTAG, SWD,
SWO and adds 4 bit ETM support and connects to the
ULINKpro adapter.
Connecting a ULINKpro:
The ULINKpro connects to a STM32 board with its standard 20 pin Hi
Density connector or the standard JTAG connector with a supplied
adapter.
In order to use ETM trace you must connect the ULINKpro to the 20 pin
Hi-density connector as shown here.
If you use the legacy 20 pin connector you can use JTAG,
SWD and SWV but not ETM.
Pictured is a ULINKpro with a MCBSTM32C (right) and a
STM3210C-EVAL from STMicroelectronics (below).
www.keil.com
Select Settings and the next window below opens up. This is the
control panel for the ULINK 2 and ULINK-ME (they are the same).
3.
In Port: select SWJ and SW. SWV will not work with JTAG selected.
4.
In the SW Device area: ARM CoreSight SW-DP MUST be displayed. This confirms you are connected to the
target processor. If there is an error displayed or it is blank this must be fixed before you can continue. Check the
target power supply. Cycle the power to the ULINK and the board.
TIP: To refresh this screen select Port: and change it or click OK once to leave and then click on Settings again.
TIP: You can do regular debugging using JTAG. SWD and JTAG operate at approximately the same speed. Serial Wire
Viewer (SWV) will not operate in JTAG mode.
Step 2) Configure the Keil Flash Programmer:
5. Click on OK once and select the Utilities tab.
6.
7.
8.
9.
Click on OK once.
www.keil.com
Assume the ULINKpro is connected to a powered up STM32 target board, Vision is running in Edit mode (as it is
when first started the alternative to Debug mode) and you have selected a valid project. The ULINKpro is shown
connected to the MCBSTM32C on page 4.
2.
or ALT-F7 and select the Debug tab. In the drop-down menu box select the ULINK
Select Options for Target
Pro Cortex Debugger as shown here:
3.
4.
In Port: select SWJ and SW. SWV will not work with JTAG selected.
5.
2.
3.
4.
Select STM32F10x High-density Flash as shown below or the one for your processor:
5.
TIP: To program the Flash every time you enter Debug mode, check Update target before Debugging.
1.
2.
You have successfully connected to the STM32 target processor and selected the Vision Flash programmer.
www.keil.com
Assume the ST-LINK is connected to a powered up STM32 target board, Vision is running in Edit mode (as it is
when first started the alternative to Debug mode) and you have selected a valid project.
or ALT-F7 and select the Debug tab. In the drop-down menu box, select the STSelect Options for Target
LINK Debugger as shown here:
3.
4.
7.
You do not select any Flash algorithm. ST-LINK does this automatically.
3.
4.
You have successfully connected to the STM32 target processor and selected the STLink as your debugger.
5.
TIP: You do not need to click on the Load icon to program the Flash. Simply enter Debug mode and the Flash will be
automatically programmed.
TIP: You do not need the Initialization ini file since the ST-Link does not support either SWV or ETM trace.
ST-Link
Segger J-Link
www.keil.com
Assume the J-Link is connected to a powered up STM32 target board, Vision is running in Edit mode (as it is when
first started the alternative to Debug mode) and you have selected a valid project.
2.
3.
4.
In Port: select SW. SWV will not work with JTAG selected.
5.
7.
8.
9.
6.
7.
or make sure you program the Flash manually by clicking on the Load icon.
8
www.keil.com
Part B)
1) Blinky Example Programs using a ULINK2 or ULINK-ME:
We will connect a Keil MDK development system using real target hardware and a ULINK2 or ULINK-ME. These
instructions use a Keil MCBSTM32C board. It is possible to use the ULINKpro for this example but you must configure it.
The project referenced below is pre-configured to use ULINK2.
1.
2.
3.
4.
5.
6.
7.
8.
9.
The LEDs on the STM32 board will now blink at a rate determined by the setting of POT1
Now you know how to compile a program, load it into the STM32 processor Flash, run it and stop it.
STM3210C-EVAL Board: No LEDs will light but the program will run. Rotate RV1and monitor PE8 or PE9 with a meter.
2) Hardware Breakpoints:
1.
With Blink running, double-click in the left margin on a darker gray block in the source file Blinky.c between Lines
55 through 61 as shown below:
2.
A red block is created and soon the program will stop at this point.
3.
The yellow arrow is where the program counter is pointing to in both the disassembly and source windows.
4.
The cyan arrow is a mouse selected pointer and is associated with the yellow band in the disassembly window.
5.
Note you can set and unset hardware breakpoints while the program is running.
6.
www.keil.com
3) Locals Window:
Whenever the program is stopped, the Locals window will display the local variables for the active function. If possible, the
values of the local variables will be displayed and if not the message <out of scope> will be displayed.
1.
Shown is the Locals window. Leave the hardware breakpoint active from the previous page.
2.
3.
4.
2.
3.
4.
In the declaration for AD_val in Blinky.c, add the keyword static and make i a separate int variable like this:
int main (void) {
static int AD_val ;
int i;
2.
This will ensure that variable AD_val always exists and is visible to Vision.
3.
Exit debug mode. TIP: You can edit files in edit or debug mode, but can compile them only in edit mode.
4.
Compile the source files by clicking on the Rebuild icon. Hopefully they compile with no errors or warnings.
5.
The next page describes how to enter variables in the Watch and Memory windows.
TIP: You will have to re-enter AD_val into a window after modifying it because it isnt the same variable anymore it is a
static variable now instead of a local. Drag n Drop is the fastest way as you will see on the next page.
TIP: Vision in conjunction with CoreSight can display in real-time global and static variables, structures, peripheral
registers and physical memory locations. Local variables cannot be displayed because they are active only when in scope
while their function is executed. Convert locals to static or global variables to see them. This conversion usually means the
variable is stored in volatile memory rather than a CPU register. There will be a small time penalty incurred.
This feature is available on any Keil ULINK or the Segger J-Link version 6 or later.
10
www.keil.com
1.
You can do the following steps while the CPU is running. Click on RUN if desired.
2.
Find the static variable AD_val in Blinky.c. This was changed from a local to a static var in the previous example.
3.
Open the Watch 1 window by clicking on the Watch 1 tab as shown or select View/Watch Windows/Watch 1.
4.
In Blinky.c, block AD_val, click and hold and drag it into Watch 1.
Release it and it will be displayed updating as shown here:
5.
6.
TIP: To Drag n Drop into a tab that is not active, pick up the variable
and hold it over the tab you want to open; when it opens, move your
mouse into the window and release the variable.
6.
Double click on the value for AD_val in the Watch window. Enter the value 0 and press Enter. 0 will be inserted
into memory in real-time. It will quickly change as the variable is updated often by the program. You can also do
this in the Memory window with a right-click and select Modify Memory.
Memory window:
1.
Drag n Drop AD_val into the Memory 1 window or enter it manually. Rotate the pot and watch the window.
2.
Note the value of AD_val is displaying its address in Memory 1 as if it is a pointer. This is useful to see what
address a pointer is pointing to but this not what we want to see at this time.
3.
Add an ampersand & in front of the variable name and press Enter. Now the physical address is shown
(0x2000_00014).
4.
5.
TIP: You are able to configure the Watch and Memory windows and
change their values while the program is still running in real-time without
stealing any CPU cycles.
1.
2.
Stop the CPU and exit debug mode for the next step.
and
TIP: View/Periodic Window Update must be selected. Otherwise variables update only when the program is stopped.
This is just a small example of the capabilities of Serial Wire Viewer. We will demonstrate more features..
How It Works:
Vision uses ARM CoreSight technology to read or write memory locations without stealing any CPU cycles. This is nearly
always non-intrusive and does not impact the program execution timings. Remember the Cortex-M3 is a Harvard
architecture. This means it has separate instruction and data buses. While the CPU is fetching instructions at full speed,
there is plenty of time for the CoreSight debug module to read or write to memory without stealing any CPU cycles.
This can be slightly intrusive in the unlikely event the CPU and Vision reads or writes to the same memory location at
exactly the same time. Then the CPU will be stalled for one clock cycle. In practice, this cycle stealing never happens.
You are not able to view local variables while the program is running. They are visible only when the program is stopped in
their respective functions.
STM3210C-EVAL Board: The Watch and Memory windows will display exactly the same on the ST board.
11
www.keil.com
A) SWV for ULINK2 or ULINK-ME: (ULINKpro instructions are on the next page)
Configure SWV:
1.
2.
3.
In the box Initialization File: enter ..\Blinky_ULp\STM32_SWO.ini You can use the Browse button:
4.
Click on Settings: beside the name of your adapter (ULINK Cortex Debugger) on the right side of the window.
5.
Select the SWJ box and select SW in the Port: pulldown menu.
6.
In the area SW Device must be displayed: ARM CoreSight SW-DP. SWV will not work with JTAG.
7.
8.
In Core Clock: enter 72 and select the Trace Enable box. This is the default frequency for many STM32 projects.
9.
Select Periodic and leave everything else at default. Periodic activates PC Samples.
10. Click on OK twice to return to the main Vision menu. SWV is now configured.
Note: Any of these ini files will work in Step 3:
STM32_SWO.ini
C:\Keil\ARM\Boards\MCBSTM32C\Blinky_Ulp
STM32F10x_DBG.ini
C:\Keil\ARM\Boards\MCBSTM32E\Blinky
STM32DBG.ini
C:\Keil\ARM\Boards\MCBSTM32E\STLIB_Blinky
These are set by default to SWV operation. You must
change them to use the Trace Port and ETM.
To Display Trace Records:
1.
2.
3.
Open Trace Records window by clicking on the small arrow beside the Trace icon:
4.
The Race Records window will open and display PC Samples as shown below:
TIP: If you do not see PC Samples as shown and either nothing or erratic frames with strange data, the trace is not
configured correctly. The most probable cause is the Core Clock: frequency is wrong.
All frames have a timestamp displayed in CPU
cycles and accumulated time.
Double-click inside this window to clear it.
If you right click inside this window you can see
how to filter various types of frames out. No
other frames than PC Samples exist in this simple
example.
TIP: SWV is easily overloaded as indicated by
an x in the OVF or Dly column.
Select only that information needed.
There are more features of Serial Wire Viewer.
12
www.keil.com
2.
3.
In the box Initialization File: enter ..\Blinky_ULp\STM32_SWO.ini You can use the Browse button:
4.
Click on Settings: beside the name of your adapter (ULINK Pro Cortex Debugger) on the right side of the window.
5.
6.
Core Clock: No need to enter anything. ULINKpro determines this automatically. Select the Trace Enable box.
7.
In the Trace Port select Serial Wire Output Manchester. Selecting UART/NRZ will cause an error.
8.
Select Periodic and leave everything else at default. Selecting Periodic activates PC Samples.
9.
Click on OK twice to return to the main Vision menu. SWV is now configured.
TIP: Sync Trace Port with 4 bit Data field sends the trace records out the 4 bit trace port rather than the single pin SWO.
The Trace Port is faster and must be selected for ETM trace. It is available only with the ULINKpro.
We will examine this setting later. You could use it now
if you prefer.
Note: Any of these ini files will work in Step 3:
STM32_SWO.ini
C:\Keil\ARM\Boards\MCBSTM32C\Blinky_Ulp
STM32F10x_DBG.ini
C:\Keil\ARM\Boards\MCBSTM32E\Blinky
STM32DBG.ini
C:\Keil\ARM\Boards\MCBSTM32E\STLIB_Blinky
These are set by default to SWV operation. You must
change them to use the Trace Port and ETM.
Display Trace Records:
1.
2.
3.
Open the Instruction Trace window by clicking on the small arrow beside the Trace icon:
4.
The Instruction Trace window will open and display PC Samples as shown below:
TIP: The Instruction Trace window is different that the Trace Records window provided with the ULINK2. Note the
disassembled instructions are displayed and if available, the source code is also displayed. Clicking on a PC Sample line will
take you to that place in the source and disassembly windows.
If you want to see all the program counter values, use the ETM trace available with most STM32 processors. A Ulinkpro
using ETM trace will also provide Code Coverage, Performance Analysis and Execution Profiling in real time.
You cannot clear the Instruction Trace window by double-clicking inside it. To clear the trace, exit and re-enter debug mode.
13
www.keil.com
Create a global variable called count. Enter int count; before main() near line 16 in Blinky.c.
2.
Enter count ++; just after the for loop near line 60 in Blinky.c:
for (i = 0; i < ((AD_val << 8) + 100000); i++);
count ++;
3.
4.
5.
6.
7.
Unselect Periodic and EXCTRC. This is to prevent overload on the SWO pin. Click OK twice.
8.
9.
Open View/Analysis Windows and select Logic Analyzer or select the LA window on the toolbar.
. Note: You can configure the LA while the program is running or stopped.
10. Locate the variable count you created in Blinky.c. It is declared near line 16.
11. Block count and drag it into the LA window and release it. Or click on Setup in the LA and enter it manually.
12. Click on Setup and set Max: in Display Range to 0xFF. Click on Close. The LA is completely configured now.
13. Drag and drop count into the Watch 1 window. It should be incrementing if Blinky is running.
14. Adjust the Zoom OUT icon in the LA window to about 1 second or so to get a nice ramp as shown below.
15. In the Watch 1 window, double-click on the count value and enter 0 and press Enter.
16. This value will be displayed in the LA window as shown here: You can enter any reasonable value into count.
TIP: The Logic Analyzer can display static
and global variables, structures and arrays. It
cant see locals: make them static or global.
To see peripheral registers, enter them into the
Logic Analyzer and read or write to them.
1.
2.
3.
4.
5.
6.
www.keil.com
Using the example from the previous page, stop the program. Stay in Debug mode.
2.
3.
The SWV Trace does not need to be configured to use Watchpoints. However, we will use it in this exercise.
4.
In the Expression box enter: count == 0x15 without the quotes. Select both the Read and Write Access boxes.
5.
6.
Click on Close.
7.
8.
9.
Click on RUN.
10. When count equals 3, the program will stop. This is how a
Watchpoint works.
11. You will see count incremented to 0x15 in the Logic
Analyzer as well as in the Watch window.
12. Note the data write of 0x15 in the Trace Records window shown below in the Data column. The address the data
written to and the PC of the write instruction is displayed as well as the timestamps. This is with a ULINK2 or
ULINK-ME. The ULINKpro will display a different window.
13. There are other types of expressions
you can enter and are detailed in the
Help button in the Breakpoints
window.
14. To repeat this exercise, set count
to a number less than 15 or click on
RUN a few times to get past the
trigger value of 0x15.
15. When finished, delete this
Watchpoint by selecting Debug and
select Breakpoints and select Kill
All.
16. Leave Debug mode.
TIP: You cannot set Watchpoints on-the-fly while the program is running like you can with hardware breakpoints.
TIP: To edit a Watchpoint, double-click on it in the Breakpoints window and its information will be dropped down into the
configuration area. Clicking on Define will create another Watchpoint. You should delete the old one by highlighting it and
click on Kill Selected or try the next TIP:
TIP: The checkbox beside the expression in Current Breakpoints as shown above allows you to temporarily unselect or
disable a Watchpoint without deleting it.
15
www.keil.com
10) RTX_Blinky Example Program with Keil RTX RTOS: A Stepper Motor example
Keil provides RTX, a full feature RTOS. RTX is included for no charge as part of the Keil MDK full tool suite. It can have
up to 255 tasks and no royalty payments are required. If source code is required, this is included in the Keil RL-ARM
Real-Time Library which also includes USB, CAN, TCP/IP networking and a Flash File system. This example explores the
RTOS project. Keil will work with any RTOS. A RTOS is just a set of C functions that gets compiled with your project.
1.
This exercise will work with the ST STM3210C-EVAL board except the LCD and LEDs will not be active.
2.
Start Vision by clicking on its icon on your desktop if it is not already running.
3.
4.
RTX_Blinky uses the ULINK2 as default: if you are using a ULINKpro, please configure it as described on page 4
and configure the Serial Wire Viewer on page 13. You only have to do this once for each project it will be saved
in the project file. Select File/Save All.
5.
6.
7.
8.
The LEDs will blink indicating the four waveforms of a stepper motor driver. Click on STOP
1.
Click on the RTX_Conf_CM.c source file tab as shown below on the left. You can open it with File/Open.
2.
Click on Configuration Wizard at the bottom and your view will change to the Configuration Wizard.
3.
Open up the individual directories to show the various configuration items available.
4.
See how easy it is to modify these settings here as opposed to finding and changing entries in the source code.
5.
This is a great feature as it is much easier changing items here than in the source code.
6.
You can create Configuration Wizards in any source file with the scripting language as used in the Text Editor.
7.
This scripting language is shown below in the Text Editor as comments starting such as a </h> or <i>.
8.
The new Vision4 System Viewer windows are created in a similar fashion. Select View/System Viewer or click
on the icon.
TIP: If you dont see any System Viewer entries, either the System Viewer is not available for your processor or you are
using an older example project and it needs to be refreshed by the following instructions:
Exit Debug mode. Click on the Target Options icon and select the Device tab. Note which processor is
currently selected. Select a different one, reselect the original processor and click on OK. System Viewer is
now activated. Close this window and select File/Save All.
Configuration Wizard
16
System Viewer
www.keil.com
2.
3.
We must activate Serial Wire Viewer to get the Event Viewer working.
1.
2.
3.
4.
5.
6.
7.
8.
9.
You will not have to stop the program to view this data. No CPU
cycles are used. Your program runs at full speed. No instrumentation
code need be inserted into your source. You will find this feature very
useful ! Remember, RTX is included free with MDK.
TIP: You can use a ULINK2, ULINK-ME, ULINKpro or Segger J-Link for these RTX Kernel Awareness windows.
17
www.keil.com
Close the RTX Viewer windows. Stop the program and exit debug mode.
2.
Add 4 global variables unsigned int phasea through unsigned int phased to Blinky.c as shown here:
3.
Add 2 lines to each of the four tasks Task1 through Task4 in Blinky.c as
shown below: phasea=1; and phasea=0; :the first two lines are shown
added at lines 081 and 084 (just after LED_On and LED_Off function
calls). For each of the four tasks, add the corresponding variable assignment
statements phasea, phaseb, phasec and phased.
4.
We do this because in this simple program there are not enough suitable
variables to connect to the Logic Analyzer.
TIP: The Logic Analyzer can display static and global variables, structures and arrays. It cant see locals: just make them
static. To see peripheral registers merely read or write to them and enter them into the Logic Analyzer.
Program the Flash
5.
6.
7.
8.
9.
Click on the Blinky.c tab. Block phasea, click, hold and drag
up to the Logic Analyzer tab (dont let go yet!)
10. When it opens, bring the mouse down anywhere into the Logic Analyzer window and release.
11. Repeat for phaseb, phasec and phased. These variables will be listed on the left side of the LA window as
shown. Now we have to adjust the scaling.
12. Click on the Setup icon and click on each of the four variables and set Max. in the Display Range: to 0x3.
13. Click on Close to go back to the LA window.
14. Using the OUT and In buttons set the range to 20 seconds. Move the scrolling bar to the far right if needed.
15. You will see the following waveforms appear. Click to mark a place move the cursor to get timings. Place the
cursor on one of the waveforms and get timing and other information as shown in the inserted box labeled phasec:
TIP: You can also enter these variables into the Watch and Memory windows to display and change them in real-time.
TIP: You can view signals that exist mathematically in a variable and not available for measuring in the outside world.
18
www.keil.com
13) Serial Wire Viewer (SWV) and how to use it: (with ULINK2)
a) Data Reads and Writes: (Note: Data Reads but not Writes are disabled in the current version of Vision).
You have configured Serial Wire Viewer (SWV) two pages back in Section 11 under Configuring the Serial Wire Viewer:
Now we will examine some of the features available to you. SWV works with Vision and a ULINK2, ULINK-ME,
ULINKpro or a Segger J-Link V6 or higher. SWV is included with MDK and no other equipment must be purchased.
Everything shown here is done without stealing any CPU cycles and is completely non-intrusive. A user program runs at full
speed and needs no code stubs or instrumentation software added to your programs.
1.
Use RTX_Blinky from the last exercise. Enter Debug mode and run the program if not already running.
2.
3.
4.
The ITM frames are the data from the RTX Kernel
Viewer which uses Port 31 as shown under Num.
To turn this off select Debug/Debug Settings and
click on the Trace tab. Unselect ITM Stim. Port 31.
TIP: Port 0 is used for Debug printf Viewer.
5.
6.
7.
Click on OK to return.
8.
9.
2.
3.
4.
5.
www.keil.com
The STM32 family using the Cortex-M3 processor has many interrupts and it can be difficult to determine when they are
being activated. SWV on the Cortex-M3 processor makes the display of exceptions and interrupts easy.
1.
2.
Unselect On Data R/W Sample, PC Sample and ITM Ports 31 and 0. (this is to minimize overloading the SWO port)
3.
4.
Click OK twice.
5.
6.
7.
What Is Happening ?
1. You can see two exceptions (11 & 15) happening.
2.
3.
4.
5.
6.
2.
The next window opens up and more information about the exceptions is displayed as shown.
3.
Note the number of times these have happened under Count. This is very useful information in case interrupts come
at rates different from
what you expect.
4.
5.
6.
www.keil.com
For
c) PC Samples:
Serial Wire Viewer can display a sampling of the program counter. If you need to see all the PC values, use the ETM trace
with a Keil ULINKpro. ETM trace also provides Code Coverage, Execution Profiling and Performance Analysis.
SWV can display at best every 64th instruction but usually every 16,384 is more common. It is best to keep this number as
high as possible to avoid overloading the Serial Wire Output (SWO) pin. This is easily set in the Trace configuration.
1.
2.
Unselect EXCTRC, On Data R/W Sample and select Periodic in the PC Sampling area.
3.
4.
5.
6.
7.
8.
9.
21
www.keil.com
2.
Add this code to Blinky.c. A good place is near line 17, just after the declaration of count.
#define ITM_Port8(n)
3.
In the main function in Blinky.c right after the variable count enter these lines after near line 63:
ITM_Port8(0) = num + 0x30;
/*
4.
Rebuild the source files, program the Flash memory and enter debug mode.
5.
6.
Unselect On Data R/W Sample, PC Sample and ITM Port 31. (this is to help not overload the SWO port)
7.
Select EXCTRC and ITM Port 0. ITM Stimulus Port 0 enables the Debug (prinftf) Viewer.
8.
Click OK twice.
9.
Click on View/Serial Windows and select Debug (printf) Viewer and click on RUN.
10. In the Debug (printf) Viewer you will see the ASCII value of num appear.
11. Change the POT and the display rate will change.
12. How else could you detect this interesting effect of the program without SWV ?
Trace Records
1.
Open the Trace Records if not already open. Double click on it to clear it.
2.
You will see a window such as the one below with ITM and Data R/W frames.
What Is This ?
1.
ITM 0 frames (Num column) are our ASCII characters from num with carriage
return (0D) and line feed (0A) as displayed the Data column.
2.
All these are timestamped in both CPU cycles and time in seconds.
3.
Note the X in the Dly column. This means the timestamps might/are not be correct due to SWO pin overload.
ITM Conclusion
Super TIP: ITM_SendChar is a useful function you can use to send characters. It is found in the header core.CM3.h.
22
www.keil.com
Part C)
Using the ULINKpro with ETM Trace:
The examples previously shown with the ULINK2 will also work with the ULINKpro. There are two major differences:
1) The window containing the trace frames is now called Instruction Trace. More complete filtering is available.
2) The SWV (Serial Wire Viewer) data is sent out the SWO pin with the ULINK2 using UART encoding. The
ULINKpro can send SWV data either out the SWO pin using Manchester encoding or through the 4 bit Trace Port.
This is done so the ULINKpro can support those Cortex-M3 processors that have SWV but not ETM. The trace port
is found on the 20 pin Hi-density connector. It is configured in the Trace configuration window as shown below.
ETM data is always sent out the Trace Port and if ETM is being used, SWV data is also sent out this port.
ULINKpro offers:
1) Faster Flash programming than the ULINK2.
2) All Serial Wire Viewer features as the ULINK2 does.
3) Adds ETM trace which provides records of all Program Counter values. ULINK2
provides only PC Samples and is not nearly as useful.
4) Code Coverage: were all assembly isntructions executed ?
5) Performance Analysis: where the processor spent its time.
6) Execution Profiling: How long instructions, ranges of instructions, functions or C source code took in both time
and CPU cycles as well as number of times these were executed.
1) Blinky_ULp Example:
The project in C:\Keil\ARM\Boards\Keil\MCBSTM32C\Blinky_ULp is preconfigured for the ULINKpro.
1.
Connect the ULINKpro to the MCBSTM32C board using the Cortex Debug + ETM connector.
2.
3.
4.
5.
6.
7.
8.
Examine the Instruction Trace window as shown below: This is a complete record of all the program flow since
RESET until Vision halted the program at the start of main() since Run To main is selected in Vision.
9.
In this case, # 607 shows the last instruction to be executed. (BX r0). In the Register window the PC will display the
value of the next instruction to be executed (0x0800_045A in my case). Click on Single Step once.
www.keil.com
2) Code Coverage:
13. Click on the RUN icon.
14. Examine the Disassembly and Blinky.c windows. Scroll and notice different color blocks in the left margin:
15. This is Code Coverage provided by ETM trace. This indicates if an
instruction has been executed or not.
Colour blocks indicate which assembly instructions have been executed.
1.
2.
3.
4.
5.
6.
7.
In the window on the right you can easily see examples of each type of Code
Coverage block and if they were executed or not and if branches were taken (or not).
Why was 0x0800_046E never executed ? Or 0x0800_0490 ? You should devise tests to execute these instructions so you
can test the effects.
Code Coverage tells what assembly instructions were executed. It is important to ensure all assembly code produced by the
compiler is executed and tested. You do not want a bug or an unplanned circumstance to cause a sequence of untested
instructions to be executed. The result could be catastrophic as unexecuted instructions cannot be tested. Some agencies
such as the US FDA require Code Coverage for certification.
Good programming practice requires that these unexecuted instructions be identified and tested.
Code Coverage is captured by the ETM. Code Coverage is also available in the Keil Simulator.
A Code Coverage window is available as shown below. This window is available in View/Analysis/Code Coverage. You
can Clear and Update this window with the buttons provided.
24
www.keil.com
2.
Select View/Analysis Windows/Performance Analysis. A window similar to the one below will open up.
3.
4.
5.
Note the execution information that has been collected in this initial short run. Both times and number of calls is
displayed.
6.
We can tell that most of the time at this point in the program has been spent in the Reset_Handler.
7.
8.
Note the display changes in real-time while the program Blinky is running. There is no need to stop the processor to
collect the information. No code stubs are needed in your source files.
9.
Select Functions from the pull down box as shown here and notice the difference.
This clears the PA window and resets the STM32 and reruns it to
10. Exit and re-enter Debug mode again and click on RUN. Note the different data set displayed.
11. When you are done, exit Debug mode.
www.keil.com
4) Execution Profiling:
Execution profiling is used to display how much time a C source line took to execute and how many times it was called. This
information is provided by the ETM trace. It is possible to group source lines (called collapse) to get combined times and
number of calls. This is called Outlining. The Vision simulator also provides Execution Profiling.
1.
2.
3.
4.
Click on RUN.
5.
6.
7.
8.
Hover the cursor over a time and ands more information appears as
in the yellow box here:
9.
Recall you can also select Show Calls and this information rather
than the execution times will be displayed in the margin.
Outlining:
1) Block a section of source as similar to this:
2) Right click on the blue block and select Outlining and then
Collapse Section as shown below:
3) Note the section you blocked is now collapsed and the times are added together where the red arrow points.
4) Click on the + to expand it.
5) Stop the program and exit Debug mode.
26
www.keil.com
5) In-the-Weeds Example:
Some of the hardest problems to solve are those when a crash has occurred and you have no clue what caused this. You only
know that it happened and the stack is corrupted or provides no useful clues. Modern programs tend to be asynchronous with
interrupts and RTOS task switching plus unexpected and spurious events. Having a recording of the program flow is useful
especially when a problem occurs and the consequences are not immediately visible. Another problem is detecting race
conditions and determining how to fix them. ETM trace handles these problems and others easily and is not hard to use.
If a Bus Fault occurs in our example, the CPU will end up at 0x800_016A as shown in the disassembly window below. This
is the Bus Fault handler. This is a branch to itself and will run this Branch instruction forever. The trace buffer will save
millions of the same branch instructions. The Instruction Trace window below shows this branch forever. This is not useful.
This exception vector is found in the file startup_stm3210f10x_cl.s. If we set a breakpoint by double-clicking on the Hard
Fault handler and run the program: at the next Bus Fault event the CPU will again jump to the Hard Fault handler.
The difference this time is the breakpoint will stop the CPU and also the trace collection. The trace buffer will be visible and
extremely useful to investigate and determine the cause of the crash.
1.
Using the Blinky example from the previous exercise, exit and re-enter Debug mode to clear the trace.
2.
Locate the Hard fault vector near line 169 in the disassembly window or in startup_stm3210f10x_cl.s.
3.
4.
5.
6.
7.
Click on RUN and immediately the program will stop on the Hard Fault exception branch instruction.
8.
Examine the Instruction Trace window and you find this POP plus everything else that was previously executed.
The frames above the POP are a record of all previous instructions executed and tells you the complete program flow.
27
www.keil.com
5) In the box Initialization File: an ini file will be there. Click on the Edit box.
6) Click OK. At the bottom of the ini file, click on the Configuration Wizard tab.
7) Expand the menu and select Synchronous: Trace
Data Size 4 as shown here:
TIP: Asynchronous is used to select the SWO port and is
needed for the ULINK2 or ULINK-ME.
8) Click on File/Save All to enable this file. It will
be executed when you enter Debug mode.
or ALT-F7 and
9) Select Options for Target
select the Debug tab (again).
10) Click on Settings: beside the name of your
adapter (ULINK Pro Cortex Debugger) on the
right side of the window.
11) Click on the Trace tab. The window below is
displayed.
12) Core Clock: No need to enter anything.
ULINKpro determines this automatically
13) In Trace Port select Sync Trace Port with 4 bit data. It is possible to use other bit sizes but best to use the largest.
14) Select Trace Enable and ETM Trace Enable. Unselect Periodic and leave everything else at default as shown below.
15) Click on OK twice to return to the main Vision menu. Both ETM and SWV are now configured.
16) Select File/Save All.
TIP: We said that you must use SWD (also called
SW) in order to use the Serial Wire Viewer. With
the ULINKpro and with the Trace Port selected,
you can also select the JTAG port as well as the
SWD port.
28
www.keil.com
Global variables.
Static variables.
Structures.
Cant see DMA transfers DMA bypasses CPU and SWV by definition.
PC Samples.
CPU counters.
Trace adds significant power to debugging efforts. Tells where the program has been.
Especially where the bug occurs a long time before consequences are seen.
These are the types of problems that can be found with a quality trace:
Pointer problems.
Code overwrites writes to Flash, unexpected writes to peripheral registers (SFRs), corrupted stack.
How did I get here ?
Out of bounds data. Uninitialized variables and arrays.
Stack overflows. What causes the stack to grow bigger than it should ?
Runaway programs: your program has gone off into the weeds and you need to know what instruction caused this.
Is very tough to find these problems without a trace especially oif the stack is corrupted.
ETM trace with the ULINKpro is best for solving program flow problems.
For complete information on CoreSight for the Cortex-M3: Search for DDI0314F_coresight_component_trm.pdf on
www.arm.com.
29
www.keil.com
8) Keil Products:
Keil Microcontroller Development Kit (MDK-ARM)
MDK-Professional (Includes RL-ARM) Promotion with ULINKpro until September 30, 2011 - $9,995
MDK-Standard with included RTX RTOS) (MDK has a great simulator) - $4,895
Call Keil Sales for more details on current pricing. All products are available.
Call Keil Sales for special university pricing.
For the ARM University program: go to www.arm.com and search for university.
All products include Technical Support for 1 year. This can be renewed.
Keil Real Time Library (RL-ARM)
RTX sources, Flash File, TCP/IP, CAN, USB driver libraries - $4,195
30
www.keil.com