Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
1
For the ST STM3240G-EVAL Evaluation Board with STM32F407
STMicroelectronics STM32: Cortex™-M4 Lab
ARM® Keil MDK Toolkit featuring Serial Wire Viewer and ETM Trace
For the STM3240G-EVAL board Version 0.72 Robert Boys bob.boys@arm.com
Introduction:
The purpose of this lab is to introduce you to the STMicroelectronics Cortex-M4 processor family using the ARM® Keil™
MDK toolkit featuring the IDE μVision®. W e will use the Serial Wire Viewer (SWV) and ETM trace on the STM3240G-
EVAL eval uati on board from STMicroelectroncs. At the end of this tutoria l, you will be able to confidently work with
STM32 processors and MDK. Keil offers a similar board: MCBSTM32F400™. Examples are provided for both boards.
Keil MDK comes in an evaluation version that limits code a nd data size to 3 2 Kbytes. Nearly all Keil examples will compile
within this 32K limit. The addition of a licen se number will turn it into the f ull, 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 vers io n of
Keil RTX RTOS. No royalty payments are required. RTX source code is now included with all versions of Keil MDK.
Why Use Keil M D K ?
MDK provides these features particularly suited for Cortex-M3 and
Cortex-M4 users:
1. µVision IDE wit h Integrat ed Debug ger, Flash programmer
and the ARM® Compiler toolchain. MDK is a turn-key
product with included examples.
2. Serial Wire V iewer and ETM trace ca pability is included.
A full feature Keil RTOS ca lle d RTX is included with
MDK with source code.
3. RTX Kernel Awareness window is updated in real-time.
Kernel Awareness exists for Ke i l RTX, CMX, Qua dros
and Micrium. All RTOSs can compile with MDK.
Awareness can be provided by the supplier.
4. Choice of adapters: ULINK2, ULINK-ME, ULINKproor Segger J-Link (version 6 or later). ST-Link is
supported but it has no S WV or ETM support at this time. SWV for ST -Link is planned for 4Q11.
5. Keil Technical Support is included for one year and is renewable. This helps you get your pr oject completed faster.
This document details these features:
1. Serial Wire Viewer (SWV) with ULINK2, ULINK-ME a nd ULINKpro. ETM Trace using ULINKpro.
2. Real-time Read and Write to memory locations for Watch, Memory and RTX Tasks windows. These are non-
intrusive to your program. No CPU cycles are stolen. No instrumentation code is added to your so urc e fil es.
3. Six Hardware Breakpoints (ca n be set/unset on-the-fly) and four Wat chpoints (also called Access Breaks).
4. RTX Viewer: a kernel awareness program for the Keil RTX RTOS that updates while the program is running.
Serial Wire Viewer (SWV):
Serial Wire Viewer (SWV) displays PC S amples, Exceptio ns (including i nterrupts), data reads and writes, IT M (printf),
CPU counters and a timestamp. This information co mes from the ARM CoreSight™ debug module integrated into the
Cortex-M4. SWV is outp ut on the Seria l Wire Output (SWO) pin found on the JTAG/SWD adapter connector.
SWV does not steal any CPU cycles and is completely non-intrusive except for ITM Debug printf Viewer. SWV is provided
by the Keil ULINK2, ULINK-ME, ULINKpro and t he Segger J-Link. Best results are with a ULINK family adapter. The
STMicroelectronics ST-Link adapter does no t support SWV at this time.
Embedded Trace Macrocell (ETM):
ETM adds all the program counter values to the data provided by SWV. This allows advan ced debugging feature s incl udi ng
timing of areas of code (Exec ution Profili ng), Code Coverage, Performance Analysis and program flow debugging and
analysis. ETM requires a special debugger adapter s uch as the ULIN Kpro or Segger J-Trace. This document us es a
ULINKpro for ETM. A ULINK2 or ULINK-ME is used for the Serial Wire Viewer exercises in this lab.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
2
STM32 Evaluation Board list, 5 Steps, MDK Install, Useful Definitions 3
Part A: Connecti ng a nd C o nf igur ing to the target board:
1. Connecting ULINK2, ULINK-ME or ULINKpro to the STM3240G board: 4
2. ULINK2 or ULINK-ME and µVision Configuration: 5
3. ULINKpro and µVision Configuration: 6
4. ST-Link from STMicroelectronics and µVision Configuration: 7
5. Segger J-Link and µVision Configuration: 8
Part B: Blinky Example Programs using a ULINK2 or ULINK-ME:
1. Blinky Example Program using the STM32 and ULINK2 or ULINK-ME: 9
2. Hardwar e Break poin ts: 9
3. Call Stack + Locals Window 10
4. Variables for Watch and Memory Windows: 10
How to convert Local Variables to view in the Watch or Memory windows: 10
5. Watch and Memory Windows and how to use them: 11
6. Configuring the Serial Wire Viewer (SWV): 12
a. For ULINK2 or ULINK-ME: 12
b. For ULINKpro: 13
7. Using the Logic Analyzer (LA) with ULINK2 or ULINK-ME: 14
a. Another use of the Logic Analyzer: 15
8. Watchpoints: Conditional Breakpoints 16
9. RTX_Blinky example program with Keil RTX RTOS: 17
10. RTX Kernel Awareness using Serial Wire Viewer (SWV): 18
11. Logic Analyzer Window: Viewing Variables in real-time in a graphical format: 19
12. Serial Wire Viewer (SWV) and how to use it: (with ULINK2) 20
a. Data Reads and Writes: 20
b. Exceptions and Interrupts: 21
c. PC Samples: 22
13. ITM (Instruction Trace Macrocell) a printf feature: 23
Part C: Using t he ULINK pro with ETM Trac e
1. Target Selecto r Box : 24
2. Blinky_Ulp Example 25
3. Code Coverage: 26
4. Performance Analysis: 27
5. Execu tion Profi lin g: 28
6. In-the-weeds Example: 29
7. Configuring the ULINKpro ETM Trace: 30
8. Serial Wire Viewer Summary: 31
9. Modifying the processor speed: 32
10. Keil Products and contact information: 33
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
3
STM32 Evaluation Boards:
Keil makes six STM32 evaluation boards plus several with STR7 and STR9 pr ocesso rs. Examples are provided.
Keil part number Processor Characteristics Debug Conne ctors ST board equi valent
MCBSTM32 STM32F103VB monochrome LCD JTAG/SWD STM32F10X-128K-EVAL (color LCD)
MCBSTM32E replaced by EXL STM32F103ZE color LCD Cortex Debug and ETM STM3210E-EVAL
MCBSTM32EXL STM32F103ZG color LCD Cortex Debug and ETM STM3210E-EVAL
MCBSTM32C STM32F107VC color touch LCD Cortex Debug and ETM STM3210C-EVAL
MCBSTM32F200 Cortex-M4: MCBSTM32F400
Keil MDK provides example projects for these STMicroelectronics boards:
CQ-STARM EK-STM32F STM32-Discovery STM32F10X-EVAL STM32L152-EVAL
STM32100E-EVAL Cortex-M4: STM3240G-EVAL STM32F4-Discovery (M DK h as example s f or this board)
Five Steps to Get Conne c t e d and Configured:
1. Physically connect a ULINK to the STM3240G or other target board. Power both of these appropriately.
2. Configure µVision to use a ULINK2, ULINK-ME or ULINKpro to communicate with the JTAG or SWD port.
3. Configure the F lash programmer inside µVisio n to program the STM32 internal flash memory.
4. If desired, configure the Serial Wire Viewer. Add the STM32F4xx_SWO.ini initialization file (see belo w).
5. If desired, configure the ETM trace with the ULINKpro. Ad d th e STM32F4xx_TP.ini initializatio n file (see below).
STM32 processors need a special .ini file that configures the CoreSight Serial Wire Viewer and 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
can be configured for either SWO or 4 bit Trace Port operation. Instructions are provided on Page 30.
Software Installation:
This doc ument was written for Keil MDK 4.22a 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 the Download icon:
You c an use the eval uation version of MDK and a ULINK2, ULINK-ME, ULINKpro or J-Li nk for this lab. You must ma ke
cer t ain adjustments for no n-ULINK adapters such as the S T-Link and not all features shown here will be available.
The addition of a license numbe r co nverts t he 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/M4 parts are equipped with ET M . All have SWV.
JTAG and SWD Definitions: It is useful to have an unde rstand ing of these terms:
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 JT AG and has about the same capabilities except no Bo undary Scan.
SWD is referenced as SW in the µVision Cortex-M Target Driver Setup. See page 5, middle picture.
SWV: Serial Wire Vie wer: A trace capa bility providing dis play of reads, writes, exceptions, PC Sa mple s and printf.
SWO: Serial Wire Output: SWV frames usually come out this one pin output. It shares the JTAG signal TDIO.
Trace Port: A 4 bit port that ULINKpro use s t o output ETM frames and opti onally SWV (rather than the SWO p i n).
ETM: Embedded Trace Macrocell: Provides all the program counter values. Only the ULINKpro works with ETM .
Example Programs: See www.keil.com/st for additional information.
Example projects for STMicroelectronics boards are found in C:\Keil\ARM\boards\ST and in C:\Keil\ARM\boards\Keil for
Keil boards. Most example projects are pre-configured to use a ULINK2 or a ULINK-ME. Serial Wire Vie wer is not
usual ly configured: you can do this yourself easily. Projects that contain a Ulp in their name are configured to use a
ULINKpro and SWV and ETM are pre-configured. It is easy to select different debug adapters in µVision.
Most exa mple projects will compile within the 32 K c ode and d a ta limit of the evaluation version of MDK. An exception is
LCD_Blinky. A compiled executable called Blinky.axf file is provide d to allow you to run, evaluate and debug these
programs. If you attempt to compile t hese pro jects, the Blinky.axf file will be erased. It is a good idea to ba c k this file u p.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
4
Part A)
1) Connecting ULINK2, ULINK-ME or ULINKpro:
The STM3240G is equi pped with the new ARM standard 20 pin Hi-
Density connector for JTAG/SWD, SWO and E TM access as shown
pointed to by the pen. It is labeled CN13: Trace
The legacy 20 pin JTAG connector is also provided. This provides
JTAG, SWD and SWO access. No ETM is available on this
connector.
A 10 pin connector in the same form factor as the 20 pin Hi-Density
exists but is not provided on the STM3240G. This 10 pin 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. It is shown i n the ULIN K-ME photo below indicated
by the red arrow.
The 20 pin connector CN13 provides JTAG, SWD, SWO and adds 4 bit ETM support and connects to the ULINKpro.
Connecting a ULINK2 or ULINK-ME:
Legacy 20 Pin JTAG Connector:
A ULINK2 plugged to the STM3240G board is pictured on the first
page of this document.
The ULINK-ME is pictured here and the arrow points to the 10 pin
Hi-Density connector.
20 Pin Connector: Keil does have a 10 pin to 20 pin adapter cable
available to connect to this connector and is supplied with ULINK-
ME. The first 10 pins on the 20 pin replicate the 10 pin.
The second 10 pins on the 20 pin contain the five ETM signals.
Connecting a ULINKpro:
The ULINKpro connects to a STM32 board with its standard 20 pin HiDensity connector o r 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 conne ctor as shown below:
If you use the legacy 20 pin connector you can use JTAG, SWD and SWV but not ETM.
Pictured is a U LINKpro with the STM3220F-EVAL from STMicro electronics (below) and the Keil MCBSTM32C (right).
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
5
2) ULINK2 or ULINK-ME and µVision Configuration:
It is easy to select a USB debugging ada pter in µVisio n. You must c onfigure the connection t o both the target and to Flash
programming in two separate windows as described below. They are each selected us ing the Debug and Utilities tabs.
This doc ument will use a ULINK2 or ULINK-ME as described. You can substitute a ULINKpro with suitable adjustments.
Serial Wire V iewer (SWV) is completely supported by these two adapters. They are essentially the same devices electrically
and any reference to ULINK2 in this document includes the ME. STM32 processors require an .ini file to configure the
SWV or ETM features. The ULINKpro, which is a Cortex-Mx ETM trace adapter, has all the features of a ULINK2 with the
adva ntages of fast er p rogr amming ti me, adds ETM trace support and an enhanced Trace Data window.
Step 1) Select the debug connection to the target:
1. Assume the ULINK2 is connected to a powered up STM32 target board, µVision is runni ng in Edit mode (as it is
when first started the alternative to Debug mode) and you have selected a valid project. The ULINK2 is shown
connected to the STM3240G-EVAL board on page 1.
2. Select Options for Target or ALT-F7 and select the Debug tab.
In the drop-down menu b ox select ULINK as shown here:
3. Select Settin gs and the next window below opens up. Thi s is the
control panel for the ULINK 2 and ULINK-ME (they are the same).
4. In Port: select SWJ and SW. Serial Wire Viewer (SWV) will not work with JT AG selected. If you are not going to
use SWV with t he SWO port or will use it with t he 4 bit trace po rt selected, you can use JTAG.
5. 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 bla nk this must be fixed before you can continue. Che ck 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 Setting s agai n.
TIP: You can do regular debugging usi ng JTAG. SWD and JTAG operate at approximately the same speed. Serial Wir e
Viewer (SW V ) will not oper a te in JTAG mode.
Step 2) Co nfigure the Keil Flash Programmer:
6. Click on OK once and select t he Utilities tab.
7. Select the ULINK similar to Step 2 above.
8. Click Settings to select the programming
algorithm if it is not visible or is the wrong one.
9. Select STM32F4xx Flash as shown below or the
one for your processor:
10. Click on OK o nce.
TIP: To progr am the Flash every time yo u enter De bug
mode, check Update target before Debugging.
11. Click on OK to return to the µVision main screen.
Select File/Save All.
12. You have suc cessfully co nnected to the STM32
target processor and co nfigured the µVision Flash
programmer.
TIP: The Trace tab is where you configure the Serial
Wire Viewer (SWV) and ETM trace if you have a
ULINKpro. You will learn to do this later.
TIP: If you select ULINK or ULINKpro, a nd ha ve the
opposite ULINK physically connected to your PC; the
error message will say “No ULINK device found . T his
message actually means that µVision found the wrong Keil
adapter connected. Merely select the one connected.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
6
3) ULINKpro and µVision Configuration:
Step 1) Select the debug connection to the target:
1. Assume the ULINKpro is con nected to a po wered up STM32 target board, µVision is runnin g in Edit mode ( a s it is
when first started the alternative to Debug mode) and you have selected a valid project. The ULINKpro is sho wn
connected to the STM3240G-EVAL board on page 4.
2. Select Options for Target or ALT-F7 and select the Debug tab. In the drop-down menu box select the ULINK
Pr o Cortex Debugger as shown her e:
3. Select Settin gs and Target Driver window below opens up:
4. In Port: select SWJ and SW. SWV will not work with J TAG selected.
5. In the SW Device area: ARM CoreSight SW-DP MUST be display ed.
This confirms you are connected to the target processor. If there is an error displayed or 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 agai n.
TIP: You can do regular debugging usi ng JTAG.
SWD and JTAG operate at approximately the same
speed. Serial Wir e V ie wer (SWV) will not operate
in JTAG mode unless the ULINKpro is using the
Trace Port to output the trace frames.
This option is selected in the Tra c e tab.
Step 2) Co nfigure the Keil Flash Programmer:
1. Click on OK once and select t he Utilities
tab.
2. Select ULINKpro similar to Step 2 a bo ve.
3. Click Settin gs to sele c t the pro gramming
algorithm if one is not visible or is t he wrong one .
4. Select STM32F4xx Flash as shown below or the one for your processor:
5. Click on OK once . Select File/Save All.
TIP: To progr am the Flash every time yo u enter De bug mode, check Update target before Debugging.
1. Click on OK to return to t he µVision main screen.
2. You have successf ully c onnect ed to the STM32 target processor and selected the µVision Flash programmer.
TIP: If you select ULINK or ULINKpro, a nd ha ve
the opposite ULINK physically connected; the error
message will say “No ULINK device found”. This
message actually means that µVision found the
wrong Keil adapter connected.
TIP: A ULI NKpro will act very similar to a
ULINK2. The trace window (Trace Data) will be
quite different from the ULINK2 Trace Records as it
offers additional features.
Trace Data is linked to the Dis a ssembly and So urc e
windows. Double-click on a trace frame and it will
be loca t ed a nd highlighted in the two windows.
TIP: µVision windows can be floated anywhere.
You can restore them by setting Window/Reset
Views to default.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
7
4) ST-Link from STMicroelectronics and µVision Configuration:
The economical ST-LINK can be use d wit h µVision to provide stable JTAG or SWD debugging. It does not provide Serial
Wire Viewer, on-the-fly Watch and Memory updates and write capability, on-the-fly breakpoint setting or Watchpoints.
Step 1) Select the debug connection to the target:
1. Assume the ST-LINK is co nnected to a powered up STM32 target board, µVision is running in Edit mode (as it i s
when first started the alternative to Debug mode) and you have selected a valid project.
Important TIP: The S TM3240G contains a built-i n S T-LINK V2 via the USB port. You can use this device
instead of an external ST-Link.
2. Select Options for Target or ALT-F7 and select the Debug tab. In the drop-down menu box, select the ST-
LINK Debugger as s hown here:
3. Select Settings and Target Driver window below opens up:
4. In Protocol select either JTAG or SWD. You would only have to
select SW D if your ta rge t board only has the two SWD signals a nd
not the full set of JTAG signals. ST-LINK does not yet support SWV.
Step 2) Co nfigure the Keil Flash Programmer:
5. Click on OK once and select t he Utilities tab.
6. Select the ST-Li nk Debugger similar to Step 2 above.
7. You do not select any Flash algorithm. ST-LINK does this automatica lly.
3. Click on OK twice to return to the µVision main screen.
4. You have successfully connected to the STM32 target processor and selected the ST-
Link as your debugger.
5. Select File/Save All.
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 d o not need the Initialization ini file since the ST-Link do es not yet support either SWV or ETM trace.
ST-Link Segger J-Link
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
8
5) Segger J-Link and µVision Configuration:
The J-Link (black box) version 6 or higher provides Serial W ir e V iewer capabilities. It provide s all debug functions that the
Keil ULINK2 provides. This inc lud es br eakp oints, watchp oints, memor y read/writ e and t he RT X Viewer. J-Link displays
exceptions and PC Samples but does not provide ITM, data R/W trace frames in MDK 4.22. Segger has the new J-Link Ult ra
which is faster. Segger also provides a J-Trace for the Cortex-M3 ETM trace but this has not bee n tested with MDK for this
document. µVision will do an a utomatic firmware update on the J-Link if necessary. µVision contains all needed drivers.
The J-Link is challenged by a high output on the SWO pin especially where the Logic Analyzer is concerned. Make sure you
select only that data that you really need. Disable all others and that can include ITM 31 and 0. Lower the rate the variable is
changed or sample it if it is changing too fast. Try disabli ng the timestamps but some functions need them to operate and the
trace may then stop operating. Sometimes you must stop the program to see trace frames. We ar e worki ng on these issues .
The J-Link i s configured using very similar windows as with the ULINK2. This inc lude SWV configuration. The J-Link
uses an Instruction Trace wind ow similar to the ULINKpro. If you double click on a PC Sample frame, that instruction will
be highlighted in the Disas s emb ly and Source windo ws. The J-Link does not display any ETM frames. Use the J-Trace.
If you have trouble inst alling the J-Link USB drivers, go to C:\Keil\ARM\Segger\USBDriver and execute InstallDrivers.exe.
If the green LED on the J -Link b links quickly, this means the USB drivers are not installed co rrectly. This LED shou ld
normally be on steady when in Edit mode and off with periodic blinks when in Debug mode.
1. Assume the J-Link is connected to a powered up STM32 target board, µVision is running in Ed it mode (as it is when
first started the alternative to Deb ug mode) and you have selected a valid project.
Step 1: Select the debug connection to the target:
2. Select Options for Target or ALT-F7 and select the Debug tab. In the drop-down menu box select the J-LINK
or J-Trace as shown her e:
3. Select Settings and Target Driver window below opens up:
4. In Port: select SW. SWV will not wor k with JT AG selected.
5. 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 is blank this
must be fixed before you can continue. Check the target power supply. Cycle power to the J-Link and the board.
Step 2: Co nfigure the Keil Flash Programmer:
6. Click on OK once and select t he Utilities tab.
7. Select the ST-Li nk Debugger similar to Step 2
above.
8. Click Settings to sele c t the pro gramming algorithm
if one is not visible or is the wrong one.
9. Select STM32F4xx Fla sh as shown in the
directions for the ULINK2 or the a lgorithm for
your processor.
6. Click OK twice to r e turn to the main screen.
7. You have now selected the J-Link as your adapter,
successfully connected to the STM32 target
processor and c onfigured the F l ash pro gra mmer.
Config ur e the SWV Trace
Thi s is done the same way as the ULINK2 o r ULINK-ME. J-Lin k ha s an extra setting in the tra c e c onfiguration window to
store trace information on your hard drive called Use Cache File. Hover your mouse over this to get an explanation.
It is important with all Serial W ire Viewer con figura tions to choose as fe w signals as possible. T he single wire SWO pin is
easily overloaded.
TIP: It is easy to miss programming the Flash with your latest .axf executable. Select either the
Verify Code Download in the Target/Debug/Settings as shown here or select Update Target before
Deb uggi ng: or make sure you program the Flash manually by clicking on the Load icon.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
9
Part B)
1) Blinky Example Programs using a ULINK2 or ULIN K-ME:
We will connect a Keil MDK deve lopment system using the STM3240G-EVAL and a ULINK2 or ULINK-ME. It is
possible to use the ULINKpro fo r this example but you mus t configure it as in 3 ) ULINKpro and µVisio n Configuration: on
page 6. This proje c t is pre-configured to use ULINK2.
1. Connect the ULINK2 as pictured on the fi rst p age to the JTAG connector CN14.
2. Start µVisio n by clicking on its desktop icon.
3. Select Project/Open Project. Open the file C:\Keil\ARM\Boards\ST\STM3240G-EVAL\Blinky\Blinky.uvproj. If
this fi le i s not incl uded with your version of MDK, ple ase visit www. keil.co m/st.
4. Make sure “STM32F207 Flash” is selected.
Thi s is where yo u can create and select different target c onfigurations
such as to execute a program in RAM or Flash. If you wa nt to run in RAM, select STM3 2F407 RAM.
You then omit the Load step in number 7.
5. This projec t is configured by default to use eit her the ULINK2 or ULINK-ME.
6. Compile the source files by clicking on the Rebuild icon. . You can also use the Build icon beside it.
7. Progra m t he STM32 fla s h by c licking on the Load icon: Progress will be indicated in t he Outp ut Window.
8. Enter Debug mode by clicking on the Debug icon. Select OK if the Evaluation Mode box appears.
Note: You only need to use the Load icon to download to FLASH and not for RAM operation or the simulator.
9. Click on the RUN icon. No te : yo u stop the program with the STOP icon.
The LEDs on the STM32 board will now blink at a rate determined by the setting of RV1.
Rotate the potentiometer RV1. The LCD screen will display the value of Ad_value as shown below.
Now you know how to compile a program, load it into the STM32 processor Flash, run it and stop it.
2) Hardware Breakpoints:
1. With Blinky running, d ouble-c l ick i n the left margin on a da rker gra y bl ock somewhere appropriate between Lines
162 through179 in the source file Blink y.c a s shown below:
2. A red block is created and soon the program will stop at this po int.
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 disassembl y wind o w.
Click on a line in one window and this place will be indicated in the other windo w.
5. Note you can set and unset hardware bre akpoints while the progra m is running. ARM CoreSight technology does
this.
6. The STM32 has 6 hardware breakpoints.
A breakpoint does not execute the
instruction it is set to .
TIP: If you get multiple cyan arrows or can’t
understand the rela tionship between the C source
and assembly, try lo wering the compiler
optimization to Level 0 and rebuilding your
project.
The level is set in
Options for Target
under the
C/C++ tab.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
10
3) Call Stack + Locals Window:
Local Variables:
Starting with MDK 4.22 the C a ll Sta c k and Local wind ows are incorpor a te d into one integrate d windo w. Wheneve r the
program is stopped, the Call Stack + Locals windo w will display call stack co ntents as well as any local variables belonging
to 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 Loc als window for the main function with the hardware breakpoint active from the previous page.
2. The contents of the local variables AD_value a nd AD_ P r int are displayed.
3. With the breakpoint set as in the previous page, as you click on RUN, these locals will update as appropriate.
TIP: This is standard Stop and Godeb uggin g.
ARM CoreS ig ht debugging te chnology is much
better t han thi s. You ca n disp lay global or static
variables updated in real-time while the program is
running. No additions or changes to your code are
required. This is not possible wit h lo c a l var ia bles.
Call Stack:
The list of called funct ions is displayed when the pr ogram is stoppe d. This is very useful for debugging when you need to
know which functions have been called and are stored on
the stack.
1. Remove the hardware breakpoint by double -
clicking on it.
2. Click on RUN and then STOP
3. A window such as this one shows two functions
and their local variables.
4. Each time you clic k on RU N and then STOP ,
this wi nd ow wi l l be updated or changed
depending where the program happens to stop.
4) Variables for Watch and Memo ry W indow s:
It would be more useful if we could see the value s of va riables while the p rogr am is s t ill running. Even more valuabl e would
be the ability to change these values while the pro gra m is r unni ng. Even more valuable than that would be the abilit y to do
this without any co de stubs in yo ur sources. CoreSight and µVision can do this and more as we shall soon see.
µVision can display global a nd static variables, structures and p e ripheral ports as well as physical memory addresses. It
cannot display local variables which are constantl y moving in and out of scope as their functions are called.
The locals must first be converted into static or global variables. This is easy to do by moving the variable o ut of all
functions (including main) to create a global varia ble or b y adding the static keyword in front of the var ia ble declar a tion. An
example is: static int variable_name;
1. This will ensure that variable always exi st s and is visible to µVision.
2. If you ma ke this cha n ge , you mu st rebuild your project and Flash the processor again.
TIP: You can edit files in edit or debug mode, but can compile them only in edit mode.
The next page describes how to enter variables in the Watch and Memor y wi nd ows.
TIP: You will have to r e -enter any variables you converted into a window after modifying it because it is n’t the same
variable anymore it is a static variable or global now instead of a loc a l. D r a g ‘n Drop is the fastest way as you will see on
the next page.
TIP: Converting locals to static or global variables usually means the variable is now sto r e d in volatile memory (RAM)
rather than a CPU register. There will be a small time penalty incurred even if on-chip RAM is used..
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
11
5) Watch and Memory Windows and ho w t o use t he m :
The Watch and memory windows will display updated variable values in real-time. It does this throug h the ARM CoreSight
debugging technology. It is also possible to “put” or insert values i nto these memory locations in real-time. It is possible to
“drag and drop” variables or enter physical addresses into windo ws or ent er them manual ly while t he pr ogram is ru nning.
Watch window:
1. Stop the processor if running and exit debug mode.
2. Find the local variable AD_value in B l inky.c. This will be nea r line 140 at the start of the mai n function. Separate
it from AD_print and change it to static as such: unsigned short static AD_value = 0;
unsigned short AD_print = 0;
3. Rebuild the project. Program the Flash (Load) and re-enter debug mode. Cli ck o n RUN.
4. Open the Watch 1 window by c l icki ng on t he Wa t ch 1 tab as shown or select View/Watch Windows/Watch 1.
5. In Bl i nky.c , block AD_value, click and hold and dr a g it into Watch 1.
Release it and it will be displayed updating as shown here:
6. Rotate the pot RV1 to see AD_value update in real-time.
7. You can also enter a variable manually by double-clicking a nd
using copy and paste or typing the variable manually.
TIP: To Drag ‘n Drop into a tab that is not active, pick up the variable and ho ld it over the tab you want to ope n; when it
opens, move your mouse into the window and release the variable.
6. Doub le click on the value for AD_value i n the Watc h window. Enter the value 0 and press Enter. 0 will be inserted
into memory in real-time. It wil l quickl y change a s the variabl e is updated often by the pr ogram so you probably
will not see this happen. You can also do this in the Me mor y window with a ri ght -click and select Modify Memory.
Memory window :
1. Drag ‘n Drop AD_value i nto the Memory 1 window or enter it manually. Rotate the pot and watch the window.
2. Note the value of AD_value is d isplaying its add ress in Memory 1 as if i t is a pointer. This is useful to se e 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 ad dress is shown
(0x2000_00034).
4. Right click in t he memory window and select Unsigned/Int.
5. The data contents of AD_value is displayed as shown here:
TIP: You are able to configure the Watch and Memory windows and
chan ge their va lues whil e the progr am is still r unning in real-time without stealing any CPU cycles.
1. AD_value is now updated in real-time. This i s ARM Core Sight t ec hnology working.
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:
µVisio n use s ARM Cor eSig ht technology to read or write me mory locations witho ut stealing any CPU cycles. This is nearly
alwa ys no n-intrusive and does not impact the program execution timings. Remember the Cortex-M3and M4 are 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 witho ut stealing any CPU cycles.
This can be slightly intrusive i n t he unlikely event the CPU a nd µVision reads or writes to the same memory locatio n at
exactly the same time. Then the CPU will be stalled for one clock cycle. In practice, this cycle stealing never happens.
Remember you are not able to view local varia bles while the program is running. They ar e visi ble only when the pro gram is
stopped in their r e spective fun c tions. You must change them to a different type of variable to see them update.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
12
6) Configuring the Serial Wire Viewer (SWV):
Serial Wire V iewer provides program information in real-time.
A) SWV for ULINK2 or ULINK-ME: (ULINKpro instructions ar e on the nex t page)
Config ur e SW V :
1. µVision must be stopped and in edit mode (not debug mode).
2. Select Options for Target or ALT-F7 and select the Debug tab.
3. In the box Initialization File: enter ..\Blinky_ULp\STM32F4xx_SWO.ini You ca n us e 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 b ox and select SW in the Port: p ulldown menu.
6. In the area SW Device must be displayed: ARM CoreSight SW-DP. SWV will not work with JTAG.
7. Click on the Trace tab. The window below is displayed.
8. In Core Clock: enter 168 and select the Trace Enable box.
TIP: See Section 8) on page 32 for inst ructions on changing
the CPU clock speed.
9. Select Periodic and leave everything else at default.
Periodic activates PC Samples.
10. Click on OK twice to return to the main µVi sion
menu. SWV is now configured.
Note: The ini file is set to S WV/SWO operation by default.
You must edit it to use the Trace Port and ETM or select the
file STM32F4xx_TP.ini. You can use the Configuration
Wizard when you selec t Edit in the Debug tab.
To Display Trace Records:
1. Enter Debug mode.
2. Click on the RUN icon. .
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:
5. When you have completed this page, click on STOP.
TIP: I f you do not see PC Samples and Exceptio ns as shown and instead either nothing o r frames with st ran ge data, the trace
is not configured correctly. The most probable
cause is the Core Clock: frequency is wrong.
All frames have a timesta mp displa yed in CP U
cycles and accumulated time.
Double-click this wi nd ow to clear it.
If you right cli ck insid e thi s windo w you can see
how to filter various types of f rames out.
Unselect P C Samples and you will see only
exception frames displayed.
Did you know Exception 15 and 34 were being
activated ? Now you do. This is a very use ful
tools for displaying how many times an
exception is fi ring and when.
TIP: SWV is easily overloaded as indicated by an “x” in the OVF or Dly column. Select onl y that informati on ne ede d to
reduce overloading. There are more useful features of Serial W ire Viewer as we shall soon discover.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
13
B) SWV for ULINKpro:
Config ur e SW V : This uses t he SWO outp ut pin ra ther than the 4 bit Tra c e Port that is normally used with the ULINKpro.
1. µVision must be stopped and in edit mode (not debug mode) and with a ULINKpro connected to CN13 or CN14.
2. Select Project/Manage/Components, Environment, Books…
3. In Project Targets box select the Insert icon and enter ULINKpro and press Enter and then OK.
4. Select ULINKpro on the Select Target drop down box. Changes will not a ffect other targets.
5. Select Options for Target or ALT-F7 and select the Debug tab.
6. In the Use: box select ULINK Pro Cortex debugger. In the U tilities tab, sele c t U LINK Pro Cortex deb ug ger .
7. Select Settin gs: select Add and then select STM32F4xx Flash and Add. Click on OK once. Select the Debug tab.
8. In the box Initialization File: enter ..\Blinky_ULp\STM32F4xx_SWO.ini You ca n us e the Browse button:
9. Click on Settings: beside the name of your adapter (ULINK Pro Corte x Debugger) on the r ight sid e.
10. Make sure SWJ and SW are selected. This exercise will not work with JTAG selected.
11. Click on the Trace tab. The window below is displayed.
12. Core Cloc k: ULINKpro uses t his for calculating timing values. Enter 168 MHz. Select the Trace Enable box.
13. In the Trace Port select Serial Wire Output Manchester. Selecting UART/NRZ will cause an error.
14. Unselect Autodetect. Enter 2 into SWO Clock
Prescaler: as shown here.
15. Select Periodic and leave everything else at default.
Selecting Pe riodic activate s PC Samples.
16. Click on OK twice to return to the main µVi sion
menu. SWV i s now configured for the ULIN Kpro.
17. Rebuild this proje ct and progra m the Flash with the
Load icon. Select File/Save All.
Display Trace Records:
1. Enter Debug mode.
2. Click on the RUN icon. .
3. Open the Trace selection window by clicking on the small arrow beside the Trace icon:
4. Select Trace Data and the Trace Da ta window shown b elow will open.
5. Stop the processor and frames are displayed as shown be lo w:
6. Select various types of frames with the Displa y: box to filter o ut vario us types of frames. The default is ALL.
TIP: The Trace Data window is different than the Trace Records window provided with the ULINK2. Trace Records display
only SWV frames and Tr ace Data can display both SWV and E TM instruction frames. Note the disa ssembled i nstructions
are displayed and if available, the source code
is also displayed. If you want to see a ll the
program counter values, use the ETM trace
available with most STM32 proc essors. A
Ulinkpro using ETM tr a c e will also pro vid e
Code Coverage, Performance Analysis and
Execution Profiling in real time.
Clear and Save Trace Records:
You can clear the Trace Data window by
clic king on the Clear ic on.
You can also save the contents by clickin g on
the Save icon.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
14
7) Using the Logic Analyzer (LA) with the ULINK2 or ULINK-ME:
This example will use the ULINK2 with the Blinky example. Please connect a ULINK2 or ULKINK-ME to your STM32
board and configure it for SWV trace. If you want to use a ULINKpro you will have to make appropriate modifications.
µVision has a graphical Logic Analyzer (LA) window. Up to four variables can be displayed in real-time using the S erial
Wire Viewer. The Serial W ire Output pin is easily overloade d with many data reads and/or writes and data can be lost.
This exercise assumes AD_value is still a static variable as modified in Step 2 on page 11.
1. The project Blinky should still be open and is probably still in Debug mode. Click on STOP. Exit D ebug mode.
2. Make sure STM32F407 Flash is selected.
3. Create a global variable AD_dbg near line 40 in Blinky.c: unsigned int A D _dbg:
4. Near line 174 add this line, als o in Blinky.c, j ust after AD_pr int = AD_value; AD_dbg = AD_value;
5. Rebuild the source files, program into Flash using the Load icon and enter debug mode.
6. Select Debug/Debug Settings and select the Trace tab.
7. Unselect Pe r iodic and EX CTRC. This is to prevent overload on the SWO pin. Clic k OK twice.
8. Run t he pr ogram. . Note: You can c onfigure the LA while the program is running or stopped.
9. Open View/Analysis Windows and select Logic Analyzer or select the LA window on the toolbar.
10. Locate the variable AD_dbg you declared in Step 3 above in Blinky.c.
11. Block AD_dbg and drag it into t he LA window and release it. Or click on Setup in the LA and enter it manually.
12. Click on Setup and set Max: in D i s play Ra nge to 0xFFF. Cl i ck on Close. The LA is completely configured now.
13. Drag and drop AD_dbg into the Watch 1 window. Its value will now track the po t setting.
14. Adjust the Zoom OUT or the All icon i n the LA window to p r o vide a suitable scale of about 0.5 second.
15. Rotate the pot to ob ta in an interesting
waveform as shown here:
TIP: The Logic Analyzer can display static and global
variables, structures and arrays. It can’t see locals: make
them static or global. To see peripheral registers, enter
them into the Logic Analyzer and read or write to them.
1. Select Debug/Debug Settings and select the
Trace tab.
2. Select On Data R/W Sample. Click OK twice.
3. Run t he pr ogram. .
4. Open the Trace Records window and clear
it by double clic king in it.
5. The window similar below opens up:
6. The first line below says:
The instructi on at 0x0800_110C caused a
write of data 0x0AC9 to address
0x2000_0030 at the listed ti me in CPU
Cycles or accumulated Time in second s .
TIP: The PC column is activated when you selected
On Data R/W Sample in Step 2. You can leave this
unselected to save bandwid th on the S WO p in.
TIP: The ULINKpro will give a more sophistica te d
Trace Data window.
Watchpoints are described on the next page.
The next p age also demonstrates another use of the Logic Analyzer.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
15
Another Use for the Logic Analyzer:
In Blinky.c, there is a global variable called clock_1s that is imported from IRQ.c. It is activated once a second for u se as a
timer. A question might arise: what is the duty c ycle of this variable ? You co uld examine the code to determine this or
instrumentate your code to send it out to a GPIO port to see with a scopeor put the var i able in the Lo gi c Analyzer.
1. Locate the global variable clock_1s in Blinky.c around Line 39.
2. Enter this i nto the Logic Analyzer : eithe r ma nually or by dragging and d ropping. You can do this while the
program is running.
3. Open Setup and either set the Display Range: Max to 0x3 or select Display Type: to Bit. Click on Close.
4. Adjust the Zoom with IN, Out or All for a suitable display as shown below: You can easily see the duty cycle.
5. Stop the program. Select the Cursor checkbox.
6. Click on one of the clock_1s spikes and note a fixed red line is created.
7. A blue line follows the mouse and times are displayed as shown below.
8. Select Signal Info.
9. Hover the cursor for a few seconds
and timing information is displayed
as shown below in t he yellow box.
TIP: The data writes to the variables listed
in the LA will be vis ible in the Trace
Records window.
Optional Exercise:
1. Create an unsigned int global variable in B linky.c. An e xamp le is unsigned int counter;
2. In main() add counter++; as shown here near Line 182:
clock_1s = 0;
counter++;
3. Exit Debug mode, rebuild, program the Flash (Load) and enter debug mode. Click on RUN.
4. Remove all variables from the Logic Analyzer window. You can use the Kill All icon.
5. Add count er to the Logic Analyze r windo w a nd set Max: to 0xFF. Click on RUN if nec essary.
6. Add counter to the watch Window.
7. Counter will increment and will display as a ramp in the LA. Modify the value in the W atch wi ndow and see this
change in real-time in the Logic Analyzer. You do not need to stop the processor to modify the value of counter.
8. Remove all variables from the Logic Analyzer window for the next exercise.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
16
8) Watchpoints: Conditional Breakpoints
The STM32 Cortex-M4 processors have four Watchpoints. Watchpoints can be thought of as conditional breakpoints. The
Logic Analyzer uses watchpoint s in its ope rati ons. This means in µVision you must have two va riables free in the Logic
Analyzer to use Watchpoints.
1. Using the example from the previous page, stop the program. Stay in Debug mode.
2. Click on Debu g and se l ect Breakp oints or press Ctrl-B.
3. The SWV Trace does not need to be configured to use Watchpoints. However, we will use it in this exercise.
4. In the Expressi on box enter: “AD_dbg == 0x120” without the quotes. Select both the Read and Write Access.
5. Click on Define and it will be accepted as shown here:
6. Click on Close.
7. Double-click in the Trace Records window to clear it.
8. Enter AD_ dbg i nto the Logic Analyzer window.
9. Set its Max Display to 0xFFF. Click on Close.
10. Click on RUN.
11. Turn the pot to get AD_dbg to equal 0x120.
12. When AD_dbg equals 0x120, the program will stop. This is
how a Watchpoint works.
13. You will see AD_dbg displayed as 0x120 in the Logic
Analyzer as well as in the Watc h windo w.
TIP: There will probably be a different value displayed in the LCD. This is because difference because of a delay when the
LCD is updated. The trigger point represents the correct value and this will be displayed in the Watch window as well.
14. Note the data write of 0x120 in the Tr ace 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 i s with a ULINK2 or
ULINK-ME. The ULINKpro wil l display a different window.
15. There are other types of expressions
you ca n enter and they are detailed in
the Help button in the Bre akpo ints
window.
16. To repeat this exercise, clic k on RUN.
If the program stops immediately,
enter a differ ent value in AD_d bg in
Wat ch 1 window and try again.
17. When finished, click on STOP and
delete this Watchpoint by selecti ng
Debug and select Breakpoints and
select Kill All.
18. Leave Debug mode.
TIP: You cannot set Watchpo i nts on-the-fly while the p rogr am is running like you can with har dware breakpoi nt s .
TIP: To edit a Watc hp oint, double-cl ick on it in the Breakpo ints window and its information will be dropped down into the
configuration area. Cl icki ng on Define will c reat e another Wa tchpoint. You s hould delete the ol d one by hi ghlighting i t and
click on Kill Selecte d or try the next TIP:
TIP: The checkbox beside the expression i n Current Brea kpoints a s shown above a llows yo u to temporarily unselect or
disable a Watchpoint wit ho ut deleting it.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
17
9) RTX_Blinky Example Program with Keil RTX RTOS: A Stepper Motor example
Keil provides RTX, a full feature RTOS. RTX is inc luded 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. Source code is provided with all ver sio ns of MDK. This e xamp le
explore s the RTOS proj e c t. Keil will work with any RTOS. A RTOS is just a set of C fun c tio ns that gets compiled with
your project. A real-time awareness viewer for RTX is p rovided inside µ V ision.
1. Start µVision by clic kin g on its icon on your desktop if it is not already running.
2. Select Project/Open Project and o pen C:\Keil\ARM\Boards\ST\STM3240G-EVAL\RTX_Blinky\Blinky.uvproj.
3. RTX _B l ink y use s the ULINK2 as default: if you are using a ULINKpro, please configure it as described on page 3
and configure the Serial Wire Viewer as on pa ge 13. You only have t o do this once for each pro jectit will be
saved in the project file by selecting File/Save All.
4. Compile the source files by clicking on the Rebuild ic on. . They will compile with no err ors or warnings.
5. To program the Flash manually, click o n the Load icon. . A progress bar will be at the bottom left.
6. Enter the Debug mode by clic king o n the debug icon and cl ick on the RU N icon.
7. The LEDs and LC D will blink ind ic a ting the four waveforms of a stepper motor driver.
8. Click on STOP .
The Confi gurati on Wi zard for RTX:
1. Click on the RTX_Conf_CM.c source file tab as sho wn below on the l eft below. You can open it with File/Open.
2. Click on Configuration Wizard at the bottom and your view will change to the Configurati on Wi zard.
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 changin g entries in the source code.
5. This is a great feature as it is much eas ier changing items here than in the source code.
6. You can cr eate Config uration Wizard s in any source file with the scripting language as used in the Te xt 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 V iew/System Viewer or c lic k
on the icon. The window similar to the on the far right o pens.
TIP: I f you don’t see a ny Sys tem 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 follo wi ng 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 Vie wer is
now activated. Close this window and select File/Save All.
Text Editor: Source C ode Configuration Wizard System Viewer
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
18
10) RTX Kernel Awareness using Serial Wire Viewer (SWV):
Users often want to kno w the nu mber of the current operating task and the stat us of the other t asks. This informat ion is
usual ly stored in a structure or memory area by the RTOS. Keil provides a Task Aware window for RTX. Other RTOS
companies also provide awareness plug-ins for µVision.
1. Run RTX_ Blinky again by clicking on the Run icon.
2. Open Debug/OS Support and select RTX T asks and System
and the window on the right opens up. Yo u mig ht ha ve to
grab the window and move it into the center of the s creen.
These values are updated in real-time using the same read
write technology as used in the Watch and Memory windows.
Important TIP: V iew/Perio dic Window Update must be selected !
3. Open Debug/OS Support and select Event Vi ewer . T here i s
probably no data displayed because SWV is not configured.
RTX Viewer: Configuring Serial Wire Viewer (SWV):
We must activa te Se r ia l Wire Viewer to get the Event Viewe r working.
1. Stop the CPU and exit debug mode.
2. Click on the Target Options icon next to the target box.
3. Select the Deb ug tab. In the box Initialization File : e nter
.\Blinky_ULp\STM32F4xx_SWO.ini or use the Browse ...
4. Click the Settings box next to ULINK Cortex Debugger.
5. In the D ebug wi ndow as show n here, make sure SW J is
checked and Port: is set to SW and not JTAG.
6. Click on the Trace tab to open the Trace window.
7. Set Core Clock: to 168 MHz and select Trace Enable.
8. Unselect the Periodic and EXCTRC boxes as s hown here:
9. ITM Stimulus Port 31 must be checked. This is the method
the RTX Viewer gets the kernel awareness information out to
be displayed in the Event Viewer. It is slig htly intrusive.
10. Click on OK twice to r e turn to µVision.
The Seri al Wire Viewer is now con figured in µVision.
11. Enter Debug mode and click on RUN to start the program.
12. Select Tasks and Systemtab: note the display is upda te d.
13. Click on the Event Vie wer tab.
14. This window displays task events in a graphic al for mat as
sho wn in the RTX Kernel win dow below. You probably have
to change the Range to about 1 seco nds by clicking on t he
ALL and then the + and icons.
TIP: If Event Viewer doesn’t work, open up the Trace Records and
confirm there are good ITM 31 fra mes present. Is Core Clock correct ?
Cortex-M3 Alert: µVision will update all RTX information in real-
time on a target board due to its read/write capabilities as already
described. The Event Viewer uses I TM and is slightly intrusive.
The data is updated while the program is running. No instru mentation
code needs to be inserted into your source. You will find this feature
ver y usefu l ! Remember, RT X with source code is included with all
versions of MDK.
TIP: You can use a ULINK2, ULINK-ME, ULINKpro or Segger J-Link for these RTX Kernel Awareness windows.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
19
11) Logic Analyzer Window: View variables real-time in a graphical format:
µVision has a graphical Logic Analyzer window. Up to four variables can be displayed in real-time using the Serial Wire
Viewer in the STM32. RTX_Blinky uses four tas ks to create the waveforms. We will grap h these four wavefo rms.
1. Close the RT X Vie wer 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 B linky. c as
shown below: phasea=1; and phasea=0; :the first two lines are s hown
added at lines 084 and 087 (j ust after LED_ On and LE D_Off fun ction
calls). For each of the four tasks, ad d the corresponding variable
assignment statements phasea, phaseb, phasec and phased.
4. We do this because in this simple program there are not e nough suitable
variables to connect to the Logic Analyzer.
TIP: The Logic Analyzer can display static and global variables, structures and arrays. It can’t see locals: just make them
static. To see peripheral registers merely read or write to them and enter them into the Logic Analyzer.
5. Rebuild t he p rojec t. Pro gram the F las h .
6. Enter debug mode .
7. You can run the program at this point.
8. Open View/Analysis Windows and select Logic Analyzer or
select the LA window on the toolbar.
Enter the V a riable s into the Logic A na lyze r:
9. Click on the Blinky. c tab. Block phasea, click, ho ld and drag
up to the Logic Analyzer tab (don’t 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 b e listed on the left sid e 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 t he All, OUT and In buttons set the r ange t o 1second or so. Move the scrolling bar to the far right if needed.
15. Select S ignal Info and Show C ycles. Click to mark a p la c e move the cursor to ge t timings. P lace the cursor on one
of the waveforms and get timing a nd ot her information as sh own in the inserted box labeled phasec:
TIP: Y ou can also enter these variables into the Watch and Memory windows to display and change them in real-time.
TIP: Y ou can view signals that exist mathematically in a variable and not available for measuring in the outside world.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
20
12) Serial Wire Viewer (SWV) and ho w t o 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 Page 12 under Configuring the Serial Wire Vi ewer:
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 o r higher. SWV is included with MDK and no other equip ment must be pur chased.
Everything shown here i s done wit hout stealing any CPU cycles and is completely non-intrusive. A us er p rogram runs a t full
speed and needs no code stubs or instrumen ta tion software added to your programs.
1. Use RTX_Blinky from the last exercise. E nt er Debu g mode and run the progr am if not alre ady r unning.
2. Select View/Trace/Records or click on the Tr ace icon and select Records.
3. The Trace Records window will open up as shown her e:
4. The ITM frames are the data from the RTX Kernel
Viewer which uses Port 31 as shown under Num.
To t urn thi s off select Debug/Debug Settings a nd
click on the Trace tab. Unselect ITM Stim. Port 31.
Or you can right click in the trace records window
and unselect ITM frames to fil te r them out.
TIP: Port 0 is used for Debug printf Viewer.
5. Unselect EX CTRC and Perio di c.
6. Select On Data R/W Sample.
7. Click on OK to return.
8. Click on the RUN icon.
9. Double-click anywhere in the T r ace records
window to clear it.
10. Only Data Writes will appear no w.
TIP: Y ou co uld have also rig ht clicked on the
Trace Records window to filter the ITM frames out
but this will not r e duce any SWO pin overloa ds.
What is happening here ?
1. When variables are entered in the Logic Analyzer
(remember phasea through phased ?), the writes will appe a r in Trace Records.
2. The Address column shows where the four variables are located.
3. The Data column displays the dat a values written to phase a through phased.
4. PC is the address of the instruction causing
the writes. You activated it by selecting On
Data R/W Sa mple in the Trace c onfiguration
window.
5. The Cycles and Time(s) columns are when
these events happened.
TIP: Y ou can have up t o four var iabl es in the Logic
Analyzer and subsequently displ ayed in the Trace
Records window.
TIP: If you select View/Symbol Windo w you can see
where the addresses of the variables are.
Note: You must ha ve Bro wse r Info rmation selected
in the Options for Tar get/Outp ut tab to use the
Symbol Browser.
TIP: ULINKpro and t he Segger J-Link adapters display the trace frames in a different style trace window.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
21
b) Exceptions and Inte rrupt s:
The STM32 family using Cortex-M3 or M4 processors has many interrup t s and it can be difficult to dete rmine when they are
being activated and when. Serial Wir e Viewer (SWV) makes the d isp lay of exceptions and interrupts easy.
1. Open Debug/Debug Settings and select the Trace tab.
2. Unselect On Data R/W Sample, PC Sample and ITM Ports 31 and 0. (this is to minimize overloading the SWO port)
3. Select EXCTRC as shown here:
4. Click OK twice.
5. The Trace Records should still be open. Double
click on it to clear it.
6. Click RUN to start the program.
7. You will see a window similar to the one below
with Exceptio ns frames displayed.
What Is Happening ?
1. You can see two exceptions (11 & 15) happening.
Entry: when the exception enters.
Exit: When it exits or r eturns.
Return: When a ll the exce ptions have returned. This is useful to de te c t tail-chaining.
2. Num 11 is SVCall from the RTX calls.
3. Num 15 is the Systic k timer.
4. In my example you can see one data
write from the Logic Analyzer.
5. Note everything is time stampe d.
6. The “X in O vf is an ove rflow and some
data was lost. The “X” in Dly means
the timestamps are delayed because too
much infor mati on i s being fed out the
SWO pin.
TIP: The SWO pin is o ne pin on the Cortex-M3
family processors that all SWV information is
fed out. The exception is the ULINKpro which
can also send SWV out the 4 b it Trace Por t. Ther e are limitations on how much information we ca n feed out this one p i n.
These exceptions are happening at a very fast rate. Overloads are gracefully handled.
1. Select View/Trace/Exceptions or click on the Trace icon and select Exceptions.
2. The next window opens up and more information about the exceptions is displayed as shown.
3. Note the n umber of ti mes these have hap pene d unde r Count. This is very useful information in cas e int errupts come
at rates different from what you
expect.
4. ExtIRQ are the peripheral
interrupts.
5. You can clear this trace window
by double-clicking on it.
6. All this infor mation is displayed
in real-time and without stealing
CPU cycles !
TIP: Num is the exception number:
RESET is 1. External interrupts
(ExtIRQ), which are normally attached to peripherals, start at Num 16. For example, Num 41 is also known as 41-16 =
External IRQ 25. Num 16 = 16 16 = ExtIRQ 0.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
22
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 Profilin g 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. Open Debug/Debug Settings and select the Trace tab.
2. Unselect EXCTRC, On Data R/W Sample and select Periodic in the PC Sampling area.
3. Click on OK twice to return to the main scr een.
4. Close the Exception Trace window
and leave Trace Records open.
Double-click to clear it.
5. Click on RUN and this window
opens:
6. Most of the PC Samples are
0x0800_042E whic h is a bra nch to
itself in a loop forever routine.
7. Stop the program and the
Disassembly win dow will show
this B ranch:
8. Not all the PCs will be captured.
Still, PC Samples can give you
some idea of where your program is; especially if it
is caught in a tight loop like in this case.
9. Note: you can get different PC values
depending on the optimization level set
in µVision.
10. Set a breakpoint in one of the tasks i n
Blinky.c.
11. Run the program and when the
breakpo int is hit, the program and trace
collection is stop ped.
12. Scroll to the bottom of the Trace Records window and you might see the correct PC value displayed. Usually, it will
be a different PC depending on when the sampling took place.
13. Remove t he br eakp oint for t he next step.
TIP: In order to see all the program
Counter values, use ETM trace with the
ULINKpro. Most STM32 processors
have ETM.
ETM is much superior for progr am f l ow
debugging than PC S amp les.
µVision with a ULINKpro uses ETM to
provide Code Coverage, Execution
Pr ofiling and Performance Analysis .
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
23
13) ITM (Instruction Trace Macrocell) a printf Feature:
Recall that we showed you can display information about the RTOS in real-time us ing the RTX Viewer. This is done
thro ugh ITM Stimulus P ort 31. ITM P ort 0 is available for a printf type of instrumentation that requires minimal user code.
After the write to the ITM port, zero CPU cycles are required to get the data out of the processor and into µVision for display
in the Debug (printf) Viewer window.
1. Open the project Blinky.uvproj (not RTX Blinky).
2. Add this #define to Blinky.c. A good place is near line 34, just after the declaration o f char text[40];.
#define ITM_Port8(n) (*((volatile unsigned char *)(0xE0000000+4*n)))
3. In the function LED_Out in Blinky.c, enter these lines starting at
near line 128:
while (ITM_Port8(0) == 0);
ITM_Port8(0) = i + 0x30; /* displays i value: */
while (ITM_Port8(0) == 0);
ITM_Port8(0) = 0x0D;
while (ITM_Port8(0) == 0);
ITM_Port8(0) = 0x0A;
4. Rebuild the source files, program the Fla sh memory and enter
debug mode. Select File/Save All.
5. Open Debug/Debug Settings and select the Trace tab.
6. Unselec t O n Data R/W Sa mpl e , Periodic and ITM Port 31. (this is to help not overload the SWO port)
7. Select EXCTRC and ITM Port 0. ITM Stimulus Por t “0” e nables the Debug (prinftf) Viewer.
8. Click OK twice.
9. Click on View/Se rial Windows and select Debu g (printf) Vi ewer and clic k on RUN.
10. In the Debug (printf) Viewer you will see the ASCII value of i appear.
Trace Records
1. Open the Trace Records if not already open. Double click on it to clea r it.
2. You will see a window such as the one belo w with ITM and Exception frames. You may have to scroll to see them.
What I s T his ?
1. ITM 0 fram es (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 b oth CPU cycles and time in sec onds.
3. Note the “X” in the Dly column. This means the timestamps might/are not be correct due to SWO pin overload.
ITM Conclusi on
The writes to ITM Stimulus P ort 0 are intrusive and
are usually one cycle. It takes no CPU cycles to get
the data out the STM32 processor via the Serial
Wire Output pin.
Thi s is much fa s ter than using a UA R T and none of
your peripherals are used.
TIP: It is important to sele c t as few optio ns in the
Trace configuration as possible to avoid
overloa ding the SWO pi n. Enter only those features
that you really need.
Super TIP: ITM_ SendChar is a useful function you can use to send characters. It is found in the header core.CM3.h.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
24
Part C)
Usi ng t he ULINKpro with ETM Trace:
The examples previously s hown with the ULINK2 will also work with the ULI NKpro. There are two major differences:
1) The window containing the trace frames is now called Trace Data. Mor e co mplete filtering is available.
2) The SWV (Serial Wire Viewer) data is sent out the SWO pin to the ULINK2 u s ing UART encoding. T he
ULINKpro can send SWV data either out the SWO p in using M anchester encod ing o r out the 4 bit Trace Port. This
is done so the ULINKpro can still support those Cortex-M3 pr o cessors that have SWV but not ETM. The trace port
is found o n 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 be ing used, SWV frames must also sent out this port.
ULINKpro offers:
1) Faster F lash 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 Sampl es a nd is no t near ly as useful.
4) Code Coverage: were all the assembly isntruct ion s executed ? Untested code can be dangerous.
5) Performance Analysis: where did the processo r spent its time ?
6) Execution Profiling: Ho w long ins tructions, ranges of instructions, functions or C sourc e c ode too k in both time
and CPU cycles as well as number of times these were executed.
1) Target Selector Box:
Beside the Target Options icon is the Target Selector drop down menu as shown here. The
entries shown select between different settings in the Target Options menus and source files
association s. You can s elect an entry and then look in t he Target Options menus to see what is
selected. This is a handy method to rapidly select different configurations.
To create your own Target Options, select Project/Manage and select Components,…. You can name them anything.
1. SWO Trace: SWV frames are sent out the SWO pin just as with the ULKINK2. ETM trace is not enabled.
Manchester format is used (ULINKpro does not use UART mode). ULINKpro does not use the Core Clock: setting
in the Trace tab to determine what frequency to sample the SWO pin. It does use this value to determine various
trace timings. The file STM32F4xx_SWO.ini is selected in the Initialization File: box.
2. TracePort Trace: SWV frames are sent out the 4 bit Trace Port. ETM is not enabled. The file
STM32F4xx_TP.ini is selected in the Ini tia liz a tion File: bo x.
3. TracePort Instruction Trace: Both SWV and ETM are enabled and sent out the 4 bit Trace Port. The file
STM32F4xx_TP.ini is selected in the Ini tia liz a tion File: bo x.
Note: The STM3240G-EVAL board does not reliably outp ut frames out the T race Port with CPU speeds above
approximately 60 MHz. The Keil MCBSTM32F400 does not have this limitation. The suspect is improper trace
layout on the PCB and probably with the TrcClk signal and not the STM32 processor.
Please see 8) Mo difying processor speed for SWO and ETM with STM3240G-EVAL: on page 32.
4. RAM: Loads the program into RAM instead of Flash when entering Debug mode. You do not select the Load icon.
The memory settings are in t he Target tab in the Target Options menu. The file Dbg_RAM.ini is selected in the
Initialization File: box.
When you switch from RAM t o Flas h and vice vers a, you must reb ui ld and re-flash the project. This is because the
addresses where the executable is located is very different.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
25
2) Blinky_ULp Example:
The project in C:\Keil\ARM\Boards\ST\STM3240G-EVAL\Blinky_Ulp is preconfigured for the ULINKpro.
1. Connect the ULINKpro to the STM3240G board usin g the 20 pin CN13 Trace connector.
2. Start µVision by clicking on its desktop icon.
3. Select Project/Open Project. Open C:\Keil\ARM\Boards\ST\STM3240G-EVAL\ Blinky_Ulp\Blinky.uvproj.
4. Select TracePort Instruction Trace in the Target Options box as shown here:
5. Compile the source files by clicking on the Rebuild icon. . You can also use the Build icon beside it.
6. Program the STM32 flash by clicking on the Load icon: Progress will be ind ic a te d in the Output Window.
7. Enter Debug mode by clicking on the Debug icon. Select OK if the Evaluation Mode box appears.
8. DO NOT CLICK ON RUN YET !!!
9. Open the Data Trace window by clicking on the small arrow beside the Trace Windows icon.
10. Examine the Instruction Trace window as shown below: This is a complete record of all the program flow since
RESET until µVisio n halted the program at the start of main() since Run To main is selected in µVision.
11. In this case, 123 200 s shows the last instruction to be executed . (BX r0). In the Register windo w the PC will
display the value of the next instruct ion to be executed (0x0800_0192 in my case). Click on Single Step once.
12. The inst ruction PUSH will disp lay: | 0x080011DA | PUSH (r3,lr) | int main(void) { /* Main Program */ |
13. Scroll to the top of the Instruction Trace window to frame # 1. This is nearly the first instruction executed a fter
RESET.
Note: The STM3240G-EVAL does not reliably output frames out the Trace Port with CP U speed s above approximately 60
MHz. The Keil MCBSTM32F400 does not have this limitation. The suspect is improper trace layout on the PCB and
probably with the TrcClk signal and not the STM32 processor.
Changing C PU Speed:
Please see 8) Modifying processor speed for SWO and ETM with STM3240G-EVAL: on page 32.
The Blinky pro ject in Blinky_Ulp is configured to run at 60 MHz.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
26
3) Code Coverage:
14. Click on the R UN icon. After a second or so stop the program with the STOP icon.
15. Examine the Disassemb ly and Blinky.c windows. Scroll and notic e different colo r blocks in the left margi n:
16. This is Code Coverage provided by ETM trace. This indicates if an instruction has been executed or not.
Colour b l ocks indi cate whic h assembly inst ructions ha ve been
executed.
1. Green: this assembly instruction was executed.
2. Gray: this as se mbl y i nstruct ion was no t
executed.
3. Orange: a Branch is always not t aken.
4. Cyan: a Branch is always taken.
5. Light Gray: there is no a ssembly instruction at
this point.
6. RED: Breakpoint is set here.
7. Next instructi on to be e xecut ed.
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).
W h y w a s the branch BCC always taken resulting in 0x0800_1048 never being executed ? Or why t he br anch BGE at
0x800_105C was never taken ? You should de vise te sts to execute these i nstr ucti ons s o you c an test them.
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. Note
your display may look different due to different compile r options.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
27
4) Performance Analysis (PA):
Performance Analysis tells you how much time was spent in each function. The data can be provided by either the SWV PC
Samples or the ETM. If provided by the SWV, the results will be statistical and more accuracy is improved with longer runs.
Small loops could be entirely missed. ETM provides complete Performance Analysis. Keil provides only ETM PA.
Keil provid e s Performance A nalysis with the µVision simulator or with ETM and the ULINKpro. SWV PA is not offered.
The number of total calls made as well as the total time spent in each function is displayed. A graph ic a l display is ge ner ated
for a quick reference. If you are optimizing for speed, work first on those func tions taking the longe s t time to execute.
1. Use the same setup as used with Code Coverage.
2. Select View/A nalysis Windo ws/Performance Analysis. A window similar to the one belo w will open up.
3. Exit Debug mode and immediately re-enter it. This clears the PA window and resets the STM32 and reruns it to
main() as before. Or select the Reset icon in the PA window to clear it. Run the program for a short time.
4. Expand some of the module names as shown below.
5. Note the execution information that has been collected in th is initial s hort run. Both times and number o f calls is
displayed.
6. We can tell that most of the time at th is p oint in the pr ogram has been spent in the GLCD routines.
7. Click on the RUN icon.
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.
10. Exit and re-e nter D ebug mode again and cl ick on RUN. Note the differe nt d a ta set displayed.
11. When you are do ne, exit Debug mode.
TIP: You can also click on the RESET icon but the processo r will stay at the initial PC and will not run to main(). Y ou
can type g, main in the Command window to accomplish this.
When you c lic k on the RESET icon, the Initialization File .ini wil l no l onger be in effect and this can cause SWV and/or
ETM to stop working. Exiting and re-entering Debug mode executes the .ini script again.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
28
5) Execution Profiling:
Execution Profiling is used to disp lay how much time a C source line took to e xecute and how many times it was called.
This information is provide d by the ETM trac e . It is possible to group source lines (called collapse) to get combined times
and number of calls. This is called Outli ning. The µVision s imulator also p rovides Execution Profiling.
1. Enter Debug mode.
2. Select Debug/Execution Profiling/Show Time.
3. In the left margin of the disassembly and C s ource
windows will display various time values.
4. Click on RUN.
5. The times will start to fill up as shown b e low right:
6. Click inside the yellow margi n of B linky.c to re fre sh it.
7. This is done in r eal-time and without stealing CP U cycles.
8. Hover the cursor over a time and ands more information appears as
in the yellow box her e:
9. Recall you can also select S ho w 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 cli ck on the blue block and select Outlining and the n Col laps e
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.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
29
6) 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 i t happe ned and the stack is c orrupte d or provides no useful c l ues. Moder n programs tend to be as ynchr onous with
inter rupts and RTOS task switchi ng plus unexp ecte d and s purious event s . Having a recording of the pro gra m fl ow is use ful
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 a nd is not hard to use.
If a Hard Fault occurs, the CPU will end up at the address specified in the Hard Fault vector located at 0x00 000C. This
address points to the Hard Fault handler. This is usually a branch to i tself and thi s Bra nch instructio n wil l run foreve r. The
trace buffer will save million s of the same br a nch instructio ns. This is not useful. We need to stop the CPU at this point.
This exceptio n vector is found in the file startup_stm32f4xx.s. If we set a breakpoint by double-clicking o n t he Hard Fault
handler and run the p rogram: at the next Hard Fault event the CP U will jump to the Hard Fault handler (in this case located at
0x0800 01B0 as shown to the right) and stop.
The CPU and also the trace collection will stop. The trace
buffer will be vi sib le and extremely useful to investigate and
determine the cause of the crash.
1. Open the Blinky_Ulp example, rebuild, program the
Flash and enter Debug mo de . Open the Data Trace
window.
2. Locate the Hard fault vector near line 207 in the disas s embly window or in startup_stm32f4xx.s.
3. Set a breakpoint at this point. A red block will appear as shown above.
4. Run the Blinky example for a few seconds and click on STOP.
5. Click on the Step_Out icon to go back to the main() program as shown in the Call Stack + Locals window:
6. In the D isassembly window, scr oll down unt il you find a
POP instruction. I found one at 0x0800 1256 as shown
below in t he thi rd window:
7. Right click on the POP instruc tion (or at the MOV at
0x0800 124E as shown below) and select Set Program
Counter. This will be the next instruction executed.
8. Click on RUN and immedia tel y the progr am will sto p on the Har d Faul t exceptio n bra nch instructi on.
9. Exa mi ne the Data Trace window and you find this POP p lus e verything else that was previously execut ed. In the
bottom screen are the 4 M O V instructions plus the offending POP.
10. Note the Branch at the Hard Fault does not show in the trace window because a hardware breakpoint does execute
the instruction it is set to there fore it is not recor ded in the trace b uffer .
The frames above the POP are a record of all previous instructions executed and tells you the complete program flow.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
30
7) Configuring the ULINKpro E TM Trace:
The ULINKpro was configured for SWV operation using the SWO pin and Manchester encoding on page 11. The projec t
Blinky_Ulp is p re-configured for ETM trace. We will activate ET M trace here manually to illustrate how it is done.
1) Select the STM3240G-EVAL Blinky project.
2) Configur e U LINK pro for the STM32 processor as described on page 6: ULINKpro and µVision C onfiguration:
Do not forget to configure the Flash programmer as well.
3) µVision must be stopped and in edit mode (not debug mode).
4) Select Options for Target or ALT-F7 and select the Debug tab.
5) In the box Initialization File: insert STM32F4xx_TP.ini. Click on the Edit box. The specified ini file will
open.
6) Click OK. At the bottom of the ini file, click o n the Configuration Wiza rd tab.
7) Expand the menu and select Synchro nous: Trace
Data Size 4 as shown here:
TIP: Async hronous 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.
9) Select Options for Target or ALT-F7 and
select the Debug tab (again).
10) Click on Settings: beside the name o f your
adapter (ULINK Pro Cortex Debugger) on the
right side of the window.
11) Click on the Trace ta b. The window below is
displayed.
12) Core Clock: Enter 168 MHz. ULINKpro uses this
value only to calc ul ate vari ous t iming val ues.
13) In Trace Port select Sync Tra ce Port with 4 bit data . It is possible to use other b it 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 bel ow.
15) Click on OK twice to return to the main µV ision menu. Both ETM and S WV a re now con figure d.
16) Select File/Save All.
The ETM is now configured.
Note: The STM3240G-EVAL board has
challenges sending data to the T race Port
connector. At 168 MHz, trace collection is
unreliable. Slow the CPU down to 60 MHz.
See the instructions on page 32.
TIP: We sa id that you must u se SWD (also
called SW) in order to use the Serial Wire
Viewer. W ith the ULINKpro and with the
Trace Port selected, you can also select the
JTAG port a s well as the SWD port since no
conflict between SWO and T D I O signals will
no longer occur.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
31
8) Serial Wire Viewer Summary:
Serial Wire Viewer c an se e :
Global variables.
Static variables.
Structures.
Peripheral registers just read or write to them.
Can’t see local variables. (just make them glob a l or static).
Can’t see DMA transfers DMA bypasses CPU a nd SWV by definition.
Serial Wire Viewer displays in various ways:
PC Samples.
Data reads and writes.
Exception and inte rrupt events.
CPU counters.
Timestamps for these.
Trace is good for:
Trace adds s i gnificant power to de bugging ef forts. T ells where the p rogram has been.
A recorded history of t he pr ogram execution in the order it happened.
Trace can ofte n find nasty prob lems ver y quickly.
Weeks or months can be replaced by minutes.
Especially where the bug occurs a long time before consequences are seen.
Or where the state of the system disappears with a change in scope(s).
Plus - don’t have to stop the program. Crucial to some.
These a re the types of p roblem s t ha t c a n be found with a quality trace :
Pointer p roblems.
Illegal instr uctions and data a borts (such a s misaligned writes).
Code overwrite s writes to Flash, u nexpected writes to per ipheral registers (SFRs), corrupted stack.
How did I get here ?
Out of bounds data. Uninitialized variables and ar rays.
Stack overflows. What c auses the stack to grow bi gger than i t should ?
Runaway programs: your pr ogram has gone off into the weeds and you ne ed to know what inst ruc tion c ause d t his.
Is very tough to find these problems without a trace especially oif the stack is corrupted.
ETM trace wit h the ULINKpro is best for solving program flow problems.
Communication protocol and timing issues. System timing problems.
For complete information on CoreSight for the Cortex-M3: Search for DDI0314F_coresight_component_trm.pdf on
www.arm.com.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
32
9) Modifying the processor speed for SWO and ETM with STM3240G-EVAL:
The STM3240G-EVAL has an issue with high s peed SW V and ETM trace when using t he ULINKpro. T he Ke il
MCBSTM32F400 does not have this limitation. The ULINK2 and ULINK-ME are not affected as they cause the SWV to be
output the SWO pin at relatively slow data r a ts.
This might be a board layout issue: it is important that the ET M signal traces, especially the ETM clock, be as short in length
as po ssi ble. Recall the ULINKpro can access SWV information out either the one pin SWO po r t using the Manchester
protocol or the 4 bit Trace Port.
Using the SWO pin using the Manchester protocol:
1) Select Options for Target or ALT-F7 and select the Debug tab.
2) In the drop-down me nu box select the ULINK Pro Cortex Debugger.
3) Select the file STM32F4xx_SWO.ini.
4) Select Settings and the Target Driver window below opens up:
5) In the Trace setup window shown
here: Select Serial Wire Output
Manchester.
6) Unchec k Autodetect.
7) Set SWO Clock Prescaler to 2.
8) The signal fr om t he SWO pin will be
reduced to 84 MHz. Click OK twice.
9) SWO will now function properly.
Using 4 bit Trace Port:
The CPU clock must be slowed to 60 MHz.
In the file system_stm32f4xx.c there are three
variables to change to modify the clock speed .
PLL_M, PLL_N and PLL_P. Shown ar e the
value s for 60, 120 and 168 MHz. PLL_Q is shown for reference.
60 120 168 MHz
PLL_M 25 25 25
PLL_N 240 240 336
PLL_P 4 2 2
PLL_Q 5 5 7
1) Modify the va lues in s ystem_stm32f4xx.c for 60 MHz, rebuild to source files and program the Flash.
2) The file STM32F4xx_TP.ini mu s t be entered in the ini b ox in the Debug tab.
3) Re-enter deb ug mode and the ETM trace will now work.
ETM with this board works reliably at 60 MHz. With the Keil MCBSTM32F4 it works reliably to 168 MHz. On your own
custom board, place the ETM connector as close to the CPU as practical. Practice appropriate high speed PCB design. The
ETM TraceCLK is the most important and most easily corrupted signal.
Copyright © 2011 ARM Ltd. All rights reserved
STMicroelectronics Cortex-M3 Lab with STM3240G-EVAL board www.keil.com
33
10) Keil Products:
Kei l Microcontroller Dev e lopment K it (MDK -ARM™)
MDK-Professional (Includes Fl a sh File, TCP/IP, CAN and USB driver libraries) Promotion with UL INKpro until
December 31, 2011 - $9,995 Please contact Keil Sales for more details.
MDK-Standard (no compile or debug limit) - $4,895
MDK-Basic ( 2 56 K Compiler Limit, N o de bug Limit) - $2,695
MDK-Lite (Ev aluation vers ion) $0
All ver s ions include Ke il R TX RTOS w ith source code !
Call Keil Sales for more details on current pricing. All produc ts a re available.
Call Keil Sales for special univer s ity pricing.
For the ARM University program: go to www.arm.com and search for university.
All produ cts include Technical Support for 1 year . This can be renewed.
USB-JTAG adapter (for F lash programming too)
ULINK2 - $395 (ULINK2 and ME - SWV only no ETM)
ULINK-ME sold only with a board by K eil or O EM.
ULINKpro - $1,395 Cortex-Mx SWV & ETM trace
Note: USA prices. Contact sales.intl@keil.com for p rici ng i n ot her
countries.
Prices are for reference only and are subject to change without notice.
For the entire Keil catalog see www.keil.com or contact Keil or your local
distributor.
For more information:
Keil Sales In USA: sales.us@keil.com or 800-348-8051. Outside the US: sales.intl@keil.com
Keil Technical Suppo r t i n USA: support.us@keil.com or 800-348-8051. Outside the US: support.intl@keil.com.
For comments or corrections please email bob.boys@arm.com.
For the latest ver sion of this do c ument, contact the author, Keil Technical support or www.keil.com/st