STMicroelectronics STM32 Application Note

STMicroelectronics STM32 Application Note

Microcontroller debug toolbox
Hide thumbs Also See for STM32:
Table of Contents

Advertisement

Introduction
STM32 end-users are sometimes confronted with non- or partially-functional systems during
product development. The best approach to use for the debug process is not always
obvious, particularly for inexperienced users.
To address the above concerns, this application note provides a toolbox describing the most
common debug techniques and their application to popular recommended IDEs for STM32
32-bit Arm
hints and tips to make the best use of STM32 Software Development Tools in STM32
ecosystem.
This application note applies to the microcontrollers listed in
Microcontrollers
January 2021
®
®
Cortex
MCUs. It contains detailed information for getting started as well as

Table 1. Applicable products

Type
STM32 microcontroller debug toolbox
STM32 High Performance MCUs
STM32 Mainstream MCUs
STM32 Ultra Low Power MCUs
AN4989 Rev 3
AN4989
Application note
Table
1.
Sub class
1/118
www.st.com
1

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the STM32 and is the answer not in the manual?

Questions and answers

Summary of Contents for STMicroelectronics STM32

  • Page 1: Table 1. Applicable Products

    32-bit Arm Cortex MCUs. It contains detailed information for getting started as well as hints and tips to make the best use of STM32 Software Development Tools in STM32 ecosystem. This application note applies to the microcontrollers listed in Table Table 1.
  • Page 2: Table Of Contents

    Acronyms ..........8 STM32 ecosystem outlines ........9 Hardware development tools .
  • Page 3 Printf debugging ......... . . 68 STM32 Virtual COM port driver ....... . 68 Printf via UART .
  • Page 4 8.2.3 STM32 Series differences ........90 Dual-Core microcontroller debugging .
  • Page 5 STMicroelectronics documentation guide ........
  • Page 6 STM32 software development ........
  • Page 7 AN4989 List of figures Figure 48. USART Pinout configuration with STM32CubeMX ....... 69 Figure 49.
  • Page 8: Foreword

    Foreword AN4989 Foreword General information ® ® This document applies to STM32 32-bit Arm Cortex MCUs. Note: Arm is a registered trademark of Arm Limited (or its subsidiaries) in the US and/or elsewhere. Software versions The various examples in this application note are illustrated on basis of the following versions of the tools: •...
  • Page 9: Stm32 Ecosystem Outlines

    AN4989 STM32 ecosystem outlines STM32 ecosystem outlines STMicroelectronics and its partners are providing a full hardware and software ecosystem to support rapid evaluation, prototyping, and productizing of complete systems using STM32 microcontrollers. As presented in Figure 1, the ecosystem is composed of all the collaterals required to develop a project with STM32.
  • Page 10: Figure 2. Development Tools Overview

    AN4989 Figure 2. Development tools overview STM32 Nucleo STM32 Nucleo boards are affordable solutions for user willing to try out new ideas and to quickly create prototypes based on STM32 MCU. Figure 3. Nucleo-144, Nucleo-64 and Nucleo-32 boards STM32 Nucleo boards feature the same connectors. They can easily be extended with a large number of specialized application hardware add-ons.
  • Page 11: Figure 4. Stm32 Nucleo-144 Structure

    AN4989 STM32 ecosystem outlines All STM32 Nucleo boards integrate an ST-LINK debugger/programmer, so there is no need for a separate probe. The figure below shows an example of STM32 Nucleo structure Figure 4. STM32 Nucleo-144 structure A complete description of the embedded ST-LINK features is provided in Section 2.1.2: ST-...
  • Page 12: Figure 5. Discovery Board Example

    STM32 ecosystem outlines AN4989 Discovery kits STM32 Discovery kits are a cheap and complete solution for the evaluation of the outstanding capabilities of STM32 MCUs. They carry the necessary infrastructure for demonstration of specific device characteristics, the HAL library, and comprehensive software examples allow to fully benefit from the devices features and added values.
  • Page 13: Figure 6. Eval Board Example

    For further information and access to complete documentation visit www.st.com/stm32evaltools. STM32 nucleo expansion STM32 Nucleo expansion boards carry all the required components to Evaluate ST devices to be used together with an STM32 MCU. Build STM32-based applications leveraging functionality and performance of ST’s device portfolio.
  • Page 14: Figure 7. 7X-Nucleo-Lpm01A

    USB (using the STM32CubeMonPwr software tool with its comprehensive graphical user interface). It can be used to supply and measure the consumption of STM32 Nucleo-32, Nucleo-64, Nucleo-68 or Nucleo-144 boards, using ARDUINO connectors. Alternatively, it supplies and measures the consumption of any target connected by wires via the basic connector.
  • Page 15 AN4989 STM32 ecosystem outlines KEY FEATURES ® ® • STM32L496VGT6 microcontroller featuring Arm Cortex -M4 core at 80 MHz / 100 DMIPS and three 12-bit ADC at 5 Msps • Programmable voltage source from 1.8 V to 3.3 V •...
  • Page 16: St-Link Probe

    It is available as: • Stand-alone in-circuit debugger • Embedded in all STM32 hardware kits (Nucleo boards, Discovery kits, EVAL boards) ST-LINK/V2 and ST-LINK-V3 are the main used versions. Figure 8 shows ST-LINK/V2 and ST-LINK/V2- ISOL stand-alone probes on the right.
  • Page 17: Figure 10. On-Board St-Link-V3 On Nucleo

    AN4989 STM32 ecosystem outlines Figure 10 shows an example of an embedded ST-LINK/V2 as part of a Nucleo board. Figure 10. On-board ST-LINK-V3 on Nucleo AN4989 Rev 3 17/118...
  • Page 18 STM32 ecosystem outlines AN4989 ST-LINK/V2 basic features • 5 V power supplied by a USB connector • USB 2.0 full-speed-compatible interface • USB standard Type-A to Mini- B cable • JTAG/serial wire debug (SWD) specific features: – 1.65 V to 3.6 V application voltage supported on the JTAG/SWD interface and 5 V tolerant inputs –...
  • Page 19: Alternative Debugger Probes

    STM32 ecosystem outlines In order to identify the ST-LINK version on a board and the related features associated with it, please refer STMicroelectronics technical note Overview of the ST-LINK embedded in STM32 MCU Nucleo, Discovery Kits and Eval Boards (TN1235).
  • Page 20: Software Development Tools

    STM32 ecosystem outlines AN4989 Software development tools ® ® The STM32 family of 32-bit Arm Cortex -M core-based microcontrollers is supported by a complete range of software tools. It encompasses traditional integrated development environments - IDEs with C/C++ compilers and debuggers from major third parties that are complemented with tools from ST allowing to configure and initialize the MCU or monitor its behavior in run time.
  • Page 21: Stm32Cubemx

    C code through a step-by-step process. The first step consists in selecting the STM32 microcontroller that matches the required set of peripherals. MCU can be selected as stand-alone for custom PCB (MCU Selector) or pre-...
  • Page 22: Stm32Cubeide

    After the selection of an empty STM32 MCU or MPU, or preconfigured microcontroller from the selection of a board, the project is created, and initialization code generated. At any time...
  • Page 23 Live variable watch view – System analysis and real-time tracing (SWV) – CPU fault analysis tool • Support of ST-LINK (STMicroelectronics) and J-Link (SEGGER) debug probes ® ® • Import project from Atollic TrueSTUDIO and AC6 System Workbench for STM32 (STM32CubeIDE) ®...
  • Page 24: Partner Ides

    Over 3100 sample projects for EVAL boards from many different manufacturers – User and reference guides in PDF format – Context-sensitive on-line help • Chip-specific support: – 4300 example projects included for STMicroelectronics EVAL boards ® ® – Support for 4 Gbyte applications in Arm and Thumb mode ®...
  • Page 25: Stm32Cubeprogrammer

    (UART, USB DFU, I C, SPI, and CAN). STM32CubeProgrammer offers a wide range of features to program STM32 internal memories (such as Flash, RAM, and OTP) as well as external memories. STM32CubeProgrammer also allows option programming and upload, programming content verification, and programming automation through scripting.
  • Page 26: Figure 14. Stm32Cube Programmer

    • Offers a command-line interface for automation through scripting • ST-LINK firmware update • Enables secure firmware creation using the STM32 Trusted Package Creator tool • Supports OTA programming for the STM32WB Series ® • Multi-OS support: Windows, Linux, macOS Figure 14.
  • Page 27: Stm32Cubemonitor

    STM32 ecosystem outlines 2.2.5 STM32CubeMonitor The STM32CubeMonitor family of tools helps to fine-tune and diagnose STM32 applications at run-time by reading and visualizing their variables in real-time. In addition to specialized versions (power, RF, USB-PD), the versatile STM32CubeMonitor provides a flow-based graphical editor to build custom dashboards simply, and quickly add widgets such as gauges, bar graphs and plots.
  • Page 28: Embedded Software

    A collection of Middleware components, like RTOS, USB library, file system, TCP/IP stack, Touch sensing library or Graphic Library (depending on the MCU series) • A complete set of code examples running on STMicroelectronics boards: STM32 Nucleo, Discovery kits and EVAL boards Tip: There is a fair chance that a Cube Project example matches the project in design.
  • Page 29: Information And Sharing

    AN4989 STM32 ecosystem outlines Figure 16. STM32CubeProjectList screenshot Information and sharing STMicroelectronics offers a very complete and wide range of solution on the web to get connected to STM32 World. Figure 17. Get connected to STM32 world AN4989 Rev 3...
  • Page 30: Documentation

    Errata sheet Contained known issues and device limitation. ® Target software developer with a full description of the STM32 Cortex Programmer manual processor programming model, instruction set and core peripherals It describes new features, known limitations and corrections on a specific...
  • Page 31: Wiki Platform

    The home page of ST Community is https://community.st.com/welcome. For any problem met, it is interesting to first browse the STM32 Forum for related topics and eventually to post a new one if no relevant thread is found. AN4989 Rev 3...
  • Page 32: Stm32 Education

    (search for STM32 Education). This site provides free educational resources created by STMicroelectronics engineers for bringing an STM32 project to life. On this site, a user learns at his own pace, watches classes as per his own schedule, anytime, anywhere, on any device, or apply to one of the live learning sessions led by STMicroelectronics experts at a nearby location.
  • Page 33: Compiling For Debug

    AN4989 Compiling for debug Compiling for debug This chapter reviews the various options for debug-friendly compiling solutions. Optimization Compiler are usually configured by default to optimize performance and/or code size. In most cases, this reduces or even prevents program debugging. The most common symptoms resulting from code optimization are: •...
  • Page 34: Iar™ Ewarm

    Compiling for debug AN4989 3.1.1 IAR™ EWARM In Project->option->C/C++Compiler->Optimization Figure 18. IAR™ EWARM Optimization option 34/118 AN4989 Rev 3...
  • Page 35: Keil ® Mdk-Arm Μvision

    AN4989 Compiling for debug ® 3.1.2 Keil MDK-Arm µVision In Project Option for Target->C/C++->Optimization ® Figure 19. Keil µVision Code Optimization option ® Keil documentation suggests that Level1 (-O1) can be a suitable alternative for debug. Refer to www.keil.com support page Compiler optimization levels and the debug view for details.
  • Page 36: Stm32Cubeide

    Compiling for debug AN4989 3.1.3 STM32CubeIDE In project Properties->Settings->Tool Settings->MCU GCC Compiler->Optimization Figure 20. STM32CubeIDE optimization level setting gcc also provides the -Og option: -Og enables optimizations that do not interfere with debugging. It offers a reasonable level of optimization while maintaining fast compilation and a good debugging experience. Debugging information Debugging information is generated by the compiler together with the machine code.
  • Page 37: Iar™ Ewarm

    AN4989 Compiling for debug 3.2.1 IAR™ EWARM “Generate debug information” option tick box is accessible in Project -> Options -> C/C++ Compiler -> Output Pane It is set by default. Figure 21. IAR™ EWARM Generate debug Information option AN4989 Rev 3 37/118...
  • Page 38: Keil ® -Mdk-Arm Μvision

    Compiling for debug AN4989 ® 3.2.2 Keil -MDK-Arm µVision Debug Information Tick box is accessible in Project -> Options -> Output Pane. It is set by default. ® Figure 22. Keil Debug Information option 38/118 AN4989 Rev 3...
  • Page 39: Stm32Cubeide

    AN4989 Compiling for debug 3.2.3 STM32CubeIDE Option to manage Debugging Information are in Properties -> C/C++ Build -> Settings -> Tool Settings -> Debugging. Figure 23. STM32CubeIDE debug information option Debug Level can be set among four levels: • None: Level 0 produces no debug information at all; •...
  • Page 40: Connecting To The Board

    This chapter intends to provide the minimal set of information in order to prevent or quickly fix issues related to connection. SWD/JTAG pinout On STMicroelectronics hardware kits, SWD must be made available for connection with ST- LINK. SWD is always mapped on PA13 (SWDIO) and PA14 (SWCLK). This is the default state after reset.
  • Page 41: Figure 25. Swd Pins Pa13 And Pa14 In Reserved But Inactive State

    AN4989 Connecting to the board Figure 25. SWD pins PA13 and PA14 in Reserved but inactive state under STM32CubeMX Figure 26. SWD pins PA13 and PA14 in Active State under STM32CubeMX All three states are functional from SWD connection point of view. It is anyway recommended to explicitly activate the SWD pins by selecting “Serial Wire”...
  • Page 42: Reset And Connection Mode

    Connection mode and reset mode are 2 different but dependent concepts: Reset mode can be either: • Hardware: drive the NRST pin of the MCU. In all STMicroelectronics hardware kits, the debugger can drive this NRST through ST-LINK/V2 or ST-LINK-V3. Hint: On Nucleo, check that relevant Solder Bridge SB12 is not OFF.
  • Page 43: Iar™ Ewarm

    AN4989 Connecting to the board 4.2.2 IAR™ EWARM Reset and Connection mode are seen as a single reset mode option as shown in Figure Figure 27. Reset Mode in IAR8.10: screenshot • System (default): Normal Connection. Software System Reset prior to jump at main. •...
  • Page 44: Keil ® Mdk-Arm Μvision

    Connecting to the board AN4989 ® 4.2.3 Keil MDK-Arm µVISION Can be set through Project -> Options -> Debug -> Settings -> Debug ® Figure 28. Connect and Reset option Keil Connect: controls the operations that are executed when the µVision debugger connects to the target device.
  • Page 45: Figure 29. Keil ® Hotplug Step1

    AN4989 Connecting to the board Reset: controls the reset operations performed by the target device. The available options vary with the selected device. • Autodetect selects the best suitable reset method for the target device. This can be a specialized reset or standard method. If Autodetect finds an unknown device, it uses the SYSRESETREQ method.
  • Page 46: Figure 30. Keil ® Hotplug Step2

    Connecting to the board AN4989 ® Figure 30. Keil hotplug step2 46/118 AN4989 Rev 3...
  • Page 47: Figure 31. Keil ® Hotplug Step3

    AN4989 Connecting to the board ® Figure 31. Keil hotplug step3 With these options disabled, the debugger starts, and the target hardware stops at the current location of the program counter. This allows to analyze the memory and register content. Because Options For Target - Debug - Load Application at startup is disabled, the debugger does not have any application program and debug information.
  • Page 48: Stm32Cubeide

    In case of problem to connect to the board with STM32CubeIDE, make sure that NRST from ST-LINK is properly connected to STM32 NRST. Hotplug mode is not proposed by STM32CubeIDE. STM32CubeProgrammer can be used instead.
  • Page 49: Stm32Cubeprogrammer

    Software system reset: Resets all STM32 components except the Debug via the Cortex-M application interrupt and reset control register (AIRCR). • Hardware reset: Resets the STM32 device via the nRST pin. The RESET pin of the JTAG connector (pin 15) must be connected to the device reset pin. •...
  • Page 50: Low-Power Case

    Connecting to the board AN4989 • With ‘Normal’ connection mode, the target is reset then halted. The type of reset is selected using the ‘Reset Mode’ option. • The ‘Connect Under Reset’ mode enables connection to the target using a reset vector catch before executing any instructions.
  • Page 51: Breaking And Stepping Into Code

    AN4989 Breaking and stepping into code Breaking and stepping into code This chapter provides users with highlights about a few points affecting system behavior at code break. Debug support for timers, RTC, watchdog, BxCAN and I During a breakpoint, it is necessary to choose how the counter of timers, RTC and watchdog should behave: •...
  • Page 52: Iar™ Ewarm

    Breaking and stepping into code AN4989 5.2.1 IAR™ EWARM In Project -> Option -> ST-LINK -> Interface speed Figure 35. IAR™ EWARM ST-LINK SWD Speed setting 52/118 AN4989 Rev 3...
  • Page 53: Keil ® Mdk-Arm Μvision

    AN4989 Breaking and stepping into code ® 5.2.2 Keil MDK-Arm µVISION SWD Speed setting is accessible in Project -> Options for Target.. -> Debug -> Settings -> Target Com ® Figure 36. Keil SWD Speed Setting AN4989 Rev 3 53/118...
  • Page 54: Stm32Cubeide

    Breaking and stepping into code AN4989 5.2.3 STM32CubeIDE In Run -> Debug Configuration -> Debugger Pane Figure 37. Access to Generator Options in STM32CubeIDE V2.0.0 Note: SWD communication is always possible on all ST boards whereas JTAG is only present on EVAL boards.
  • Page 55: Secure Platform Limitation

    A simple reset does re- enable code execution; power must be switched off and on so that power-on reset enables code execution. The restriction depends on the STM32 Series as described in Table...
  • Page 56: Pcrop

    Level 2: No Debug. JTAG/SWD connection is killed. There is no way back. In this case, nobody - even STMicroelectronics - can perform any analysis of defective parts. • Level 0.5 is an additional protection level associated with TrustZone(only available in STM32L5 Serie).
  • Page 57: Exception Handling

    In order to get deeper into the subject, the user can usefully refer to Joseh Yiu’s work and book collection The Definitive Guide to Arm-Cortex-M, and to Carmelo Noviello’s recent on- line guide Mastering STM32. Default weak Handlers By default Handlers are implemented as __weak functions which perform endless loops:...
  • Page 58: Custom Handlers

    Exception handling AN4989 Custom Handlers One way to generate templates of Handler functions is to use STM32CubeMX. In Configuration -> NVIC Configuration -> Code Generation, use Generate IRQ handler tick boxes as shown in Figure Figure 38. Asking for Handler code generation When Non maskable interrupt and Hard fault interrupt are selected, the following code is generated: void NMI_Handler(void)
  • Page 59 AN4989 Exception handling In order to trap the exception, a hardware or a software breakpoint can be set in the IDE or ® directly programmed in the source code using Arm instruction BKPT. Caution: BKPT is not tolerated if no debugger is connected (refer to Chapter 9: Dual-Core microcontroller debugging on page 92).
  • Page 60: Trapping Div/0 Exception

    Exception handling AN4989 ® Figure 39. Keil Access to Show Caller Code in Contextual menu Trapping div/0 exception Most often, code execution causing a division by zero are difficult to investigate: • Nothing is neither triggered nor trapped. • Erroneous returned value generates an unexpected and unpredictable behavior that is very difficult to analyze.
  • Page 61: Cortex ® -M3/4/7 Case

    AN4989 Exception handling ® 6.3.2 Cortex -M3/4/7 case For targets that support hardware division instructions, Trapping of DIV0 operation is possible by configuring System Control Block (SCB) registers, accessible through CMSIS library. ® For example on Cortex -M3: SCB_CCR register description is provided in Figure ®...
  • Page 62: Figure 42. Iar™ Ewarm Exception Handling

    Exception handling AN4989 IAR™ EWARM Detailed R/W access to the values of each SCB registers bits at runtime can be obtained through View -> Register -> System Control Block (from Pick List) as shown in Figure Figure 42. IAR™ EWARM exception handling 62/118 AN4989 Rev 3...
  • Page 63: Figure 43. Keil ® System Control And Configure

    AN4989 Exception handling ® Keil MDK-Arm µVISION SCB->CCR can be managed at run time through View -> System Viewer -> Core Peripheral -> System Control and Configure. Refer to Figure 43 for details. ® Figure 43. Keil System Control and Configure AN4989 Rev 3 63/118...
  • Page 64: Figure 44. Keil ® Fault Reports

    Exception handling AN4989 The fault type can be investigated using Peripherals -> Core Peripherals -> Fault Reports as shown in Figure ® Figure 44. Keil Fault Reports 64/118 AN4989 Rev 3...
  • Page 65: Figure 45. Stm32Cubeide Scb Register Access

    AN4989 Exception handling STM32CubeIDE At runtime, while debug is in break state, the SCB register can be accessed in read mode or in write mode through: Window -> Show View -> SFRs as shown in Figure Figure 45. STM32CubeIDE SCB register access If you need more informations about the Hardfault,you can first enable faulting or halting when the processor executes an SDIV or UDIV instruction with a divisor of 0: SCB->CCR | = 0x01.
  • Page 66: Figure 46. Fault Analyzer In Stm32Cubeide

    Exception handling AN4989 If the code performs an SDIV or UDIV instruction with a divisor of 0, code stops with informations in “Fault Analyzer” (see Figure 46). Figure 46. Fault Analyzer in STM32CubeIDE 66/118 AN4989 Rev 3...
  • Page 67 AN4989 Exception handling Independently from the IDE, for projects including the CMSIS library, the content of the registers in the code can also be printed: void HardFault_Handler(void) volatile uint32_t csfr= SCB-> CSFR ; // load into variable printf ( "SCB-> CSFR 0x%08x \n", SCB-> CSFR) // print while (1) The same content can as well be obtained directly from the memory with any memory browser.
  • Page 68: Printf Debugging

    USART or SWO, benefiting from facilities offered by STMicroelectronics hardware kits and ecosystem tools. STM32 Virtual COM port driver STM32 Virtual COM Port Driver (VCP) is a feature supported by ST-LINK/V2-B embedded in most of recent hardware kits (refer to Section 2.1: Hardware development tools on page 9).
  • Page 69: Printf Via Uart

    AN4989 Printf debugging Printf via UART Direct connection from PC UART to board pinout does not work due to signal level incompatibility. Take care to use external adapter (such as MAX232, ST3241EB, FTDI USB/UART) or the USART connected to Virtual COM port. Trick: Appendix B: Use Nucleo “cuttable”...
  • Page 70: Figure 49. Usart2 Setting With Stm32Cubemx

    Printf debugging AN4989 Figure 49. USART2 setting with STM32CubeMX Retargeting printf to UART depends on the toolchain. ® For IAR™ EWARM and Keil MDK-Arm µVISION this is done by overriding the stdio fputc function #include "stdio.h" int fputc(int ch, FILE *f) HAL_UART_Transmit(&UartHandle, (uint8_t *)&ch, 1, 0xFFFF);...
  • Page 71: Printf Via Swo/Swv

    _write(int file, char *ptr, int len) int DataIdx; for (DataIdx = 0; DataIdx < len; DataIdx++){ __io_putchar( *ptr++ );} return len; Refer to STM32Cube provided example UART_Printf() available for almost all STM32 Series. An example is available in STM32Cube_FW_F3_V1.7.0\Projects\ STM32F303ZE-Nucleo\Examples\UART\UART_Printf. Caution: USART word length includes parity which is not the case for most of UART terminal.
  • Page 72: Figure 50. Swo Pin Configuration With Stm32Cubemx

    Printf debugging AN4989 It has to be used in combination with a Serial Wire Viewer (SWV) on host side which provides the following features: • PC (Program Counter) sampling • Event counters that show CPU cycle statistics • Exception and Interrupt execution with timing statistics •...
  • Page 73: Figure 51. Semihosting/Swo Configuration With Iar™ Ewarm

    AN4989 Printf debugging IAR™ EWARM IAR™ EWARM provides an integrated access to SWO. Redirection of printf and scanf is possible using Library Configuration options as shown in Figure Figure 51. Semihosting/SWO configuration with IAR™ EWARM Care must be taken that clock setup is correct by using ST-LINK -> Communication Pane as illustrated in Figure AN4989 Rev 3...
  • Page 74: Figure 52. Iar™ Ewarm Swo Clock Setting

    Printf debugging AN4989 Figure 52. IAR™ EWARM SWO Clock setting Once configured, IAR™ EWARM properly sets TRACE_IOEN and TRACE_MODE and configures the related GPIO. SWO printf occurrences are visible in Terminal I/O windows. Port Stimulus 0 is used by printf and scanf. It is not configurable. ®...
  • Page 75: Figure 53. Swo Configuration With Keil

    AN4989 Printf debugging ® Figure 53. SWO configuration with Keil SWV viewer is called “Debug (printf) Viewer” and is accessible while in debug through View -> Serial Windows -> Debug (prinf) Viewer as shown in Figure ® Figure 54. Access to SWV in Keil ®...
  • Page 76 Printf debugging AN4989 library which split trace between stimulus based on their importance (info, debug, error) or there source. STM32CubeIDE With STM32CubeIDE you also have to redirect printf to SWO by some piece of code. With syscall.c integrated to the project: #include "stdio.h"...
  • Page 77: Figure 55. Enable Swd In Stm32Cubeide

    AN4989 Printf debugging Figure 55. Enable SWD in STM32CubeIDE Enable SWV ITM Data Console in Window ->Show View -> SWV -> SWV ITM Data Console as shown in the figure below: AN4989 Rev 3 77/118...
  • Page 78: Figure 56. Enable Swv Itm Data Console In Stm32Cubeide

    Printf debugging AN4989 Figure 56. Enable SWV ITM Data Console in STM32CubeIDE Enable ITM Stimulus Port 0 after clicking on “Configure trace” as shown in the below figure. 78/118 AN4989 Rev 3...
  • Page 79: Semihosting

    AN4989 Printf debugging Figure 57. Enable ITM stimulus Port 0 in STM32CubeIDE Click on “Start Trace” button Figure 58. Start Trace button in STM32CubeIDE Press “Resume” button, and your printf message is printed in SWV ITM Data Console. Semihosting ® Semihosting is a mechanism that enables code running on an Arm target to communicate and use the Input/Output facilities on a host computer that is running a debugger.
  • Page 80: Iar™ Ewarm

    Printf debugging AN4989 Semihosting depends on the library provided by the IDE. The next sections present how to set semihosting using the three main IDEs covered in this application note. 7.4.1 IAR™ EWARM IAR™ EWARM provides a highly integrated semihosting feature, enabled by default. Figure 59 shows how to check if it is the case for the project in Options ->...
  • Page 81: Stm32Cubeide

    AN4989 Printf debugging 7.4.3 STM32CubeIDE Set linker parameters First the linker must ignore the default syscalls.c file and has to use the newlib-nano librairies, which contains printf() function. in Project -> Properties -> C/C++ General -> Paths and Symbol Click on the Source Location tab. Click on the arrow near to “[Project name]/Core”, and select “Filter(empty)”.
  • Page 82: Figure 61. Properties For Semihosting In Stm32Cubeide- Librairies

    Printf debugging AN4989 Figure 61. Properties for semihosting in STM32CubeIDE- Librairies Next, select MCU GCC Linker -> Miscellaneous while still in the Tool Settings tab. Click the Add… button and enter -specs=rdimon.specs into the dialog box. This add the linker flags in order to include the librdimon library. Figure 62.
  • Page 83 AN4989 Printf debugging Add printf Code Above int main(void) (USER CODE 0 section), add: extern void initialize_monitor_handles(void); Then configure the semihosting system call: In int main(void) before the while(1) loop (USER CODE 1 section) add: Initialise_monitor_handles(); Then inside the while(1) loop, add: Printf(“Hello World!\n”);...
  • Page 84: Figure 63. Semihosting In Stm32Cubeide - Debug Configuration

    Printf debugging AN4989 Debug configuration In Run -> Debug configuration -> Debugger tab, change the debugger probe to ST-LINK (Open OCD). In Generator options, choose “Software system reset“as reset Mode. Figure 63. Semihosting in STM32CubeIDE – Debug configuration In the Startup tab enter the command: monitor arm semihosting enable. 84/118 AN4989 Rev 3...
  • Page 85: Figure 64. Semihosting In Stm32Cubeide - Startup

    AN4989 Printf debugging Figure 64. Semihosting in STM32CubeIDE – Startup Click on Debug button. AN4989 Rev 3 85/118...
  • Page 86: Figure 65. Semihosting In Stm32Cubeide - Run

    Printf debugging AN4989 In the debugging perspective, click Run -> Resume, and you should see “Hello, World!” being printed at the bottom of the console once per second. Figure 65. Semihosting in STM32CubeIDE – Run 86/118 AN4989 Rev 3...
  • Page 87: Debug Through Hardware Exploration

    Debug through hardware exploration As a complement to software instrumentation, a user facing a non-working system may take great advantage to monitor STM32 pin states (GPIO or clock among others) with external tools such as oscilloscopes or logic analyzers. This chapter presents the possibilities offered by STMicroelectronics hardware kits and...
  • Page 88: Figure 67. Mco Alternate Pin Highlight Exemple With L073

    Debug through hardware exploration AN4989 This is typically pin PA8 for all STM32 families. ® For Nucleo kits, the PA8 pin is accessible on the D7 pin of the ARDUINO connector. For other board pin configuration, please refer to the board schematics.
  • Page 89: Hal_Rcc_Mcoconfig

    AN4989 Debug through hardware exploration Figure 68. MCO Multiplexer in STM32CubeMX Clock Configuration Pane The divider allows to output a signal frequency compatible with output capabilities. 8.2.2 HAL_RCC_MCOConfig Independantly of the fact that STM32CubeMX is used or not, MCO configuration is done using the hal_rcc or LL function: stm32XXxx_hal_rcc.c/ stm32XXxx_hal_rcc.h void HAL_RCC_MCOConfig( uint32_t RCC_MCOx, uint32_t RCC_MCOSource, uint32_t...
  • Page 90: Stm32 Series Differences

    STM32L4 Series also provides an LSCO (Low Speed Clock Output) on PA2 in order to output LSE or LSI, same as MCO, but with benefit to be still available during stop and standby mode. Refer to section 6.2.15 Clock-out capability of STMicroelectronics reference manual ® STM32L4x5 and STM32L4x6 advanced Arm -based 32-bit MCUs (RM035) for details.
  • Page 91: Figure 69. Stm32F4/F7 Dual Mco Capabilities

    AN4989 Debug through hardware exploration Figure 69. STM32F4/F7 dual MCO capabilities AN4989 Rev 3 91/118...
  • Page 92: Dual-Core Microcontroller Debugging

    Dual-Core microcontroller debugging AN4989 Dual-Core microcontroller debugging STM32H7x5/x7 Series are dual core microcontrollers using heterogeneous core architecture: An Arm Cortex-M7 core and an Arm Cortex-M4 core. Debug process is different for these dual core microcontrollers as we need a simultaneous debug of both cores using a single hardware debug probe.
  • Page 93: From Debug To Release

    AN4989 From debug to release From debug to release It is important to have in mind that most of technics presented in this AN and suitable for debugging have to be properly cleaned to prevent problem while releasing the application. The following action list can be used as a checklist helping to avoid the most common problems: •...
  • Page 94: Troubleshooting

    Troubleshooting AN4989 Troubleshooting Table 6 summarizes solutions to overcome some of the most frequent issues faced during debug setting and operation. Table 6. Troubleshooting Problem Solution Ensure debug in low-power in DBGMCU register is enabled. Connection with target lost during debug of low-power Ensure SWD pin not set in analog state.
  • Page 95: Appendix A Managing Dbgmcu Registers

    This appendix provides a tutorial for the different ways to Read/Write the DBGMCU registers with various tools and IDEs. By software HAL and LL provide functions to set/reset DBGMCU registers. Refer to STM32Cube\Repository\STM32Cube_FW_[MCU] _[Version]\Drivers\STM32[MCU]xx_HAL_Driver\ STM32[MCU]xx_User_Manual.chm Figure 70 Figure 71 show the positions of the DBGMCU registers iwithin the LL and HAL libraries.
  • Page 96: By Debugger

    Managing DBGMCU registers AN4989 Figure 71. DBGMCU_CR HAL Library Functions For M0 Cortex based families (L0/F0) DBGMCU module need to be clocked by setting bit 22 of register RCC_APB2ENR (refer to the corresponding reference manual) prior to be written. RCC->APB2ENR |= RCC_APB2ENR_DBGMCUEN; Some HAL macros are also available to Enable/Disable this clock.
  • Page 97: Figure 72. Access To Dbgmcu Register With Iar™ Ewarm

    AN4989 Managing DBGMCU registers IAR™ EWARM Read/Write of DBGMCU registers is possible through the register window as shown in Figure Figure 72. Access to DBGMCU register with IAR™ EWARM ® In case a more permanent setup is required EWARM C-SPY debugger macros enable to define execUserSetup(), which is executed at debugger start prior to program execution.
  • Page 98: Figure 73. Ewarm C-Spy ® Macro Script Setting

    Managing DBGMCU registers AN4989 ® Figure 73. EWARM C-SPY Macro script setting A basic sample code of execUserSetup() function used to enable low-power debug on L0 is provided below: execUserSetup(){/* Write a message to the debug log __message "L0 DBGMCU Setup IAR Macro \n"; __writeMemory32 (0x00400000, 0x40021034, "Memory");...
  • Page 99: Figure 74. Accessing Dbgmcu Register In Keil ® Mdk-Arm Μvision (1/2)

    AN4989 Managing DBGMCU registers ® Keil MDK-Arm µVision At runtime, access to the DBGMCU register is possible through View -> System Viewer -> DBG. ® Figure 74. Accessing DBGMCU register in Keil MDK-Arm µVision (1/2) AN4989 Rev 3 99/118...
  • Page 100: Figure 75. Accessing Dbgmcu Register In Keil ® Mdk-Arm Μvision (2/2))

    Managing DBGMCU registers AN4989 ® Figure 75. Accessing DBGMCU register in Keil MDK-Arm µVision (2/2)) Each bit in the register can be set or reset independently. ® For a permanent debug configuration, use Keil MDK-Arm µVision initialization file capability. Debugger script files are plain text files that contain debugger commands. These files are not created by the tools.
  • Page 101: Figure 76. Keil ® Initialization Script Setting

    AN4989 Managing DBGMCU registers ® Figure 76. Keil Initialization script setting Sample code for Init file setting DBGMCU registers on M0 based MCU (Clock enabling) FUNC void DBGMCUSetup (void) { // DBGMCU configuration _WDWORD(0x40021034, 0x00400000); // Enable clock DBG _WDWORD(0x40015804, 0x00000007); // DBG_CR _WDWORD(0x40015808, 0x00000001);...
  • Page 102: Figure 77. Access To Generator Options In Stm32Cubeide V2.0.0

    Managing DBGMCU registers AN4989 STM32CubeIDE By default, STM32CubeIDEenable Low-Power debug. This default setting can be changed through Run->Debug Configurations->Debugger Pane. • Setting up with OpenOCD server By clicking on the Show generator options as presented in the figure below. Figure 77. Access to Generator Options in STM32CubeIDE V2.0.0 DBGMCU options are available under Reset Mode in the Mode Setup group as shown in Figure 102/118...
  • Page 103: Figure 78. Generator Options Debug Mcu In Stm32Cubeide

    AN4989 Managing DBGMCU registers Figure 78. Generator Options debug MCU in STM32CubeIDE AN4989 Rev 3 103/118...
  • Page 104: Figure 79. Access To Dbgmcu Settings With Stm32Cubeide V1.3.0

    Managing DBGMCU registers AN4989 Figure 79. Access to DBGMCU settings with STM32CubeIDE V1.3.0 If needed, the DBGMCU value can be changed at run time through the I/O Registers window as shown in Figure 104/118 AN4989 Rev 3...
  • Page 105: Figure 80. Runtime R/W Access To Dbgmcu Register With Sstm32Cubeide

    AN4989 Managing DBGMCU registers Figure 80. Runtime R/W access to DBGMCU register with SSTM32CubeIDE Note: All DBGMCU registers values are kept while reset. Pay attention to not let a debug or unwanted state when returning to normal execution. (refer to Chapter 9: Dual-Core microcontroller debugging on page 92).
  • Page 106: Appendix B Use Nucleo "Cuttable" St-Link As Stand-Alone Vcp

    As debugger interface to program and debug an external application as documented in the user manual – STM32 Nucleo-144 board: section 6.3.4 of Using ST-LINK/V2-1 to program and debug an external STM32 application (UM1974) – STM32 Nucleo-64 board: Using ST-LINK/V2-1 to program and debug an external...
  • Page 107: Figure 82. Using St-Link Stand-Alone Part Of Nucleo-L476Rg As Vcp

    AN4989 Use Nucleo “cuttable” ST-LINK as stand-alone VCP Any available UART of the STM32 application can be connected to the CN3 connector of the ST-LINK part. Figure 82 illustrates a project using NUCLEO-F302R8 is using ST-LINK part of a NUCLEO-L476RG for connection of UART1 to the host.
  • Page 108: Figure 83. Virtual Com Port On Pc Side

    Use Nucleo “cuttable” ST-LINK as stand-alone VCP AN4989 Figure 83. Virtual COM port on PC side Note: This usage implies to have several targets connected to a single host PC. In order to properly identify the target and the VCP, refer to Appendix C: Managing various targets on the same 108/118...
  • Page 109: Appendix C Managing Various Targets On The Same Pc

    AN4989 Managing various targets on the same PC Appendix C Managing various targets on the same PC This appendix provides hints to identify and control the connection to a specific target among several ones using ST-LINK probe. Each ST-LINK connection is identified by a serial number. In order to correlate a serial number with a board, it is advised to use STM32CubeProgrammer.
  • Page 110: Figure 85. Getting Target St-Link S/N From The Console

    Managing various targets on the same PC AN4989 Figure 85. Getting target ST-LINK S/N from the console The next sections detail the selection of a specific target with each of the main IDEs considered in this application note. IAR™ EWARM The first time a debug session is launched while several targets are connected, a Debug Probe Selection window pops up.
  • Page 111: Figure 87. Iar™ Ewarm Debug Probe Selection With Nickname

    AN4989 Managing various targets on the same PC Figure 87. IAR™ EWARM Debug Probe Selection with nickname Important: The pop-up window is displayed only at first time. The selection made is then applied by default to further connections. Changing this initial selection requires that the "Debug Probe Selection"...
  • Page 112: Figure 89. Keil ® St-Link Selection

    Managing various targets on the same PC AN4989 ® Keil MDK-Arm µVision The list of connected targets is visible in ST-LINK debug pane (Options -> Debug -> ST- LINK -> Settings -> Debug Pane) as presented in Figure ® Figure 89. Keil ST-LINK selection In the Debug Adapter section, the pick list allows to select among all connected targets.
  • Page 113: Figure 90. Error Message For Multiple St-Link Detected In Stm32Cubeide

    AN4989 Managing various targets on the same PC Figure 90. Error message for multiple ST-LINK detected in STM32CubeIDE • Setting up with OpenOCD rIt is possible to force the connection to a specific target using the ST-LINK S/N. In Run -> Debug Configurations -> Debugger Pane, add the following OpenOCD option: -c hla_serial [ST-LINK S/N] Figure below illustrates the setting of an OpenOCD option for forcing a connection.
  • Page 114: Figure 91. Forcing Specific St-Link S/N With Stm32Cubeide With Openocd Option

    Managing various targets on the same PC AN4989 Figure 91. Forcing specific ST-LINK S/N with STM32CubeIDE with OpenOCD option • Setting up with ST-LINK GDB server 114/118 AN4989 Rev 3...
  • Page 115: Figure 92. Forcing Specific St-Link S/N With Stm32Cubeide With St-Link Gdb Server

    AN4989 Managing various targets on the same PC Figure 92. Forcing specific ST-LINK S/N with STM32CubeIDE with ST-LINK GDB server AN4989 Rev 3 115/118...
  • Page 116: Appendix D Cortex ® -M Debug Capabilities Reminder

    ® Cortex -M debug capabilities reminder AN4989 ® Appendix D Cortex -M debug capabilities reminder ® STM32 families debug capabilities depend on their Cortex -M type. Table 7. STM32 Series vs. debug capabilties STM32 Hardware Core Cortex type JTAG Series...
  • Page 117: Revision History

    Section 1.1: General information 26-Jan-2021 – Section 2.4.2: Wiki platform – Section 2.4.3: Github – Figure 2: Development tools overview – Figure 4: STM32 Nucleo-144 structure – Figure 15: STM32Cube monitor – Figure 14: STM32Cube programmer AN4989 Rev 3 117/118...
  • Page 118 IMPORTANT NOTICE – PLEASE READ CAREFULLY STMicroelectronics NV and its subsidiaries (“ST”) reserve the right to make changes, corrections, enhancements, modifications, and improvements to ST products and/or to this document at any time without notice. Purchasers should obtain the latest relevant information on ST products before placing orders.

Table of Contents

Save PDF