text
stringlengths
1
3.82k
__index_level_0__
int64
0
366
- The model is used to support the configuration of the advertising with Private Network Identity type functionality of a node. - The On-Demand Private Proxy Server model is a main model that extends the Mesh Private Beacon Server model and corresponds with the Solicitation PDU RPL Configuration Server model. - The model is used to support the functionality of a node that can configure the advertising with Private Network Identity type functionality of another node. - The On-Demand Private Proxy Client model is a root model and a main model that does not extend any other models. The On-Demand Private Proxy Client model may operate on states defined by the On-Demand Private Proxy Server model using On-Demand Private Proxy messages. - The model is used to support the configuration of the segmentation and reassembly behavior of a node. - The SAR Configuration Server model is a root model and a main model that does not extend any other models. - The SAR Configuration Client model is used to support the functionality of configuring the behavior of the lower transport layer of a node that supports the SAR Configuration Server model.
306
- The SAR Configuration Client model is a root model and a main model that does not extend any other models. The SAR Configuration Client model may operate on states defined by the SAR Configuration Server model using SAR Configuration messages. - The Solicitation PDU RPL Configuration Server model is used to support the functionality of removing items from the solicitation replay protection list of a node. - The Solicitation PDU RPL Configuration Server model corresponds with the On-Demand Private Proxy Server model. - The model is used to support the functionality of removing addresses from the solicitation replay protection list of a node that supports the Solicitation PDU RPL Configuration Server model. - The Solicitation PDU RPL Configuration Client model is a root model and a main model that does not extend any other models. The Solicitation PDU RPL Configuration Client model may be used to remove items from a solicitation replay protection list of a peer node by using Solicitation PDU RPL Configuration messages.
306
- The model is used to support the functionality of processing a sequence of access layer messages. - The Opcodes Aggregator Server model is a root model that does not extend any other models. - The model is used to support the functionality of dispatching a sequence of access layer messages to nodes supporting the Opcodes Aggregator Server model. - The Opcodes Aggregator Client model is a root model and a main model that does not extend any other models. - The model is used to support the functionality of exposing pages of Composition Data that do not fit in a Config Composition Data Status message and to expose metadata of the model instances. - The Large Composition Data Server is a main model that extends the Configuration Server model. - The model is used to support the functionality of reading pages of Composition Data that do not fit in a Config Composition Data Status message and reading the metadata of the model instances. - The Large Composition Data Client model is a root model that does not extend any other models.
306
The Large Composition Data Client model may operate on states defined by the Large Composition Data Server model using Large Composition Data messages. .. _ble-mesh-terminology-network-management: .. list-table:: Table 9 ESP-BLE-MESH Terminology - Network Management :widths: 10 40 60 :header-rows: 1 - Official Definition - Detailed Explanation - This procedure is used when the security of one or more network keys and/or one or more of the application keys has been compromised or could be compromised. - Key Refresh Procedure is used to update network key and application key of ESP-BLE-MESH network. Key Refresh Procedure is used when the security of one or more network keys and/or one or more application keys is threatened or potentially threatened. Keys are usually updated after some nodes in the network are removed. - A node can also use an IV Update procedure to signal to peer nodes that it is updating the IV Index. - The IV Update procedure is used to update the value of ESP-BLE-MESH network's IV Index.
306
This value is related to the random number required for message encryption. To ensure that the value of the random number is not repeated, this value is periodically incremented. IV Index is a 32-bit value and a shared network resource. For example, all nodes in a mesh network share the same IV Index value. Starting from 0x00000000, the IV Index increments during the IV Update procedure and maintained by a specific process, ensuring the IV Index shared in the mesh network is the same. This can be done when the node believes that it has the risk of exhausting its sequence number, or when it determines that another node is nearly exhausting its sequence number. Note: The update time must not be less than 96 hours. It can be triggered when a secure network beacon is received, or when the node determines that its sequence number is greater than a certain value. For more terms, please see: `ESP-BLE-MESH Glossary of Terms `_.
306
ESP-BLE-MESH Feature List Supported Features Mesh Core """"""""" Mesh Models """"""""""" Mesh Applications """"""""""""""""" Future Release Features Mesh Core """"""""" Mesh Applications """""""""""""""""
307
ESP-BLE-MESH Architecture This document introduces ESP-BLE-MESH architecture overview, ESP-BLE-MESH architecture implementation as well as ESP-BLE-MESH auxiliary routines. - ESP-BLE-MESH Architecture Overview - Describes the five major parts of ESP-BLE-MESH architecture and the functionality of each part. - ESP-BLE-MESH Architecture Implementation - Describes the basic functions of ESP-BLE-MESH files, the correspondence between files and ESP-BLE-MESH architecture, and the interface for calling among files. - ESP-BLE-MESH Auxiliary Routines - Describe the auxiliary routines of ESP-BLE-MESH, such as Mesh network management, Mesh features, etc. Currently ESP-BLE-MESH has implemented most functions of Mesh Profile and all the Client Models defined in Mesh Model specification. Those missing functions/models are under development and will be provided soon. ESP-BLE-MESH architecture has been granted the official Bluetooth `certification `__. .. figure:: ../../../_static/esp-ble-mesh-architecture.
308
png :align: center Figure 1.1 ESP-BLE-MESH Architecture Diagram ESP-BLE-MESH architecture includes five key parts: - ``Mesh Protocol Stack`` - ``Mesh Networking`` is responsible for processing of messages of ESP-BLE-MESH nodes. - ``Mesh Provisioning`` is responsible for provisioning flow of ESP-BLE-MESH devices. - ``Mesh Models`` is responsible for the implementation of SIG-defined models. - ``Network Management`` - Implements several network management procedures, including node removal procedure, IV Index recovery procedure, etc. - ``Features`` - Include several ESP-BLE-MESH features, e.g., Low Power feature, Friend feature, Relay feature, etc. - ``Mesh Bearer Layer`` - Includes ``Advertising Bearer`` and ``GATT Bearer``. The bearer layer is crucial to ESP-BLE-MESH protocol stack which is built on Bluetooth Low-Energy technology, because the protocol stack must make use of the bearer layer to transmit data via the BLE advertising channel and connection channel.
308
- ``Applications`` - Based on ESP-BLE-MESH protocol stack and ``Mesh Models``. - By calling API and handling Event, ``Applications`` interact with ``Mesh Networking`` and ``Mesh Provisioning`` in ESP-BLE-MESH protocol stack, as well as a series of Models provided by ``Mesh Models``. 1.1 Mesh Protocol Stack 1.1.1 Mesh Networking ``Mesh Networking`` in the protocol stack architecture implements the following functions: - The communication between nodes in the Mesh network. - Encryption and decryption of messages in the Mesh network. - Management of Mesh network resources (Network Key, IV Index, etc.). - Segmentation and reassembly of Mesh network messages. - Model mapping of messages between different models. - For more features, please see :doc:`ble-mesh-feature-list`. The implementation of ``Mesh Networking`` functions is based on hierarchy structure. Functions of each layer are shown in Table 1.1: .. list-table:: Table 1.1 Mesh Networking Architecture Description :widths: 40 150 :header-rows: 1 - Function - Access Layer not only defines the format of application data, but also defines and controls the encryption and decryption of the data packets conducted by Upper Transport Layer.
308
- Upper Transport Layer encrypts, decrypts, and authenticates application data to and from the access layer; it also handles special messages called "transport control messages", including messages related to "friendship" and heartbeat messages. - Lower Transport Layer handles segmentation and reassembly of PDU. - Network Layer defines the address type and format of the network messages, and implements the relay function of the device. 1.1.2 Mesh Provisioning ``Mesh Provisioning`` in the protocol stack architecture implements the following functions: - Provisioning of unprovisioned devices. - Allocation of Mesh network resources (unicast address, IV Index, NetKey, etc.). - Four authentication methods support during provisioning. - For more features, please see :doc:`ble-mesh-feature-list`. The implementation of ``Mesh Provisioning`` functions is based on hierarchy structure. Functions of each layer are shown in Table 1.2: .. list-table:: Table 1.2 Mesh Provisioning Architecture Description :widths: 40 150 :header-rows: 1 - Function - Provisioning PDUs from different layers are handled using provisioning protocol.
308
- The Provisioning PDUs are transmitted to an unprovisioned device using a Generic Provisioning layer or Proxy protocol layer. - These layers define how the Provisioning PDUs are transmitted as transactions that can be segmented and reassembled. - The provisioning bearers define how sessions are established such that the transactions from the generic provisioning layer can be delivered to a single device. 1.1.3 Mesh Models ``Mesh Models`` in the protocol stack architecture implements the following functions: - Configuration Client/Server Models - Health Client/Server Models - Generic Client/Server Models - Sensor Client/Server Models - Time and Scenes Client/Server Models - Lighting Client/Server Models Functions of each layer are shown in Table 1.3: .. list-table:: Table 1.3 Mesh Models Architecture Description :widths: 40 150 :header-rows: 1 - Function - Model Layer implements models used to standardize the operation of typical user scenarios, including Generic Client/Server Models, Sensor Client/Server Models, Time and Scenes Client/Server Models, Lighting Client/Server Models and several vendor models.
308
- Foundation Model Layer implements models related to ESP-BLE-MESH configuration, management, self diagnosis, etc. 1.2 Mesh Network Management ``Network Management`` implements the following functions: - Node removal procedure is used to remove a node from the network. - IV Index recovery procedure is used to recover a node's IV Index. - IV update procedure is used to update the nodes' IV Index. - Key refresh procedure is used to update the nodes' NetKey, AppKey, etc. - Network creation procedure is used to create a mesh network. - NVS storage is used to store node's networking information. 1.3 Mesh Features ``Features`` includes the following options: - Low Power feature is used to reduce node's power consumption. - Friend feature is used to store messages for Low Power nodes. - Relay feature is used to relay/forward Network PDUs received by a node over the advertising bearer. - Proxy Server/Client are two node roles in proxy protocol, which enable nodes to send and receive Network PDUs, mesh beacons, proxy configuration messages and Provisioning PDUs over a connection-oriented bearer.
308
1.4 Mesh Bearer Layer ``Bearers`` in the protocol stack architecture are responsible for passing of data between ESP-BLE-MESH protocol stack and Bluetooth Low Energy Core. ``Bearers`` can be taken as a carrier layer based on Bluetooth Low Energy Core, which implements the function of receiving and transmitting data for the ESP-BLE-MESH protocol stack. .. list-table:: Table 1.3 Mesh Bearers Description :widths: 40 150 :header-rows: 1 - Function - The GATT Bearer uses the Proxy protocol to transmit and receive ``Proxy PDUs`` between two devices over a GATT connection. - When using the Advertising Bearer, a mesh packet shall be sent in the Advertising Data of a ``Bluetooth Low Energy advertising PDU`` using the Mesh Message AD Type. 1.5 Mesh Applications The ``Applications`` in the protocol stack architecture implement the corresponding functions by calling the API provided by the ESP-BLE-MESH protocol stack and processing the Event reported by the protocol stack.
308
There are some common applications, such as gateway, lighting and etc. Interaction between application layer (``Applications``) and ``API/Event`` - Application layer calls API - Call the provisioning-related API for provisioning. - Call the model-related API to send messages. - Call the device-attributes-related API to get local information about the device. - Application layer processes Event The application layer is designed based on events, which take parameters to the application layer. Events are mainly divided into two categories. - The events completed by calling API. - Such as nodes sending messages. - The events that the protocol stack actively reports to the application layer. - The Event that the protocol stack actively reports. - The Event that Model actively reports. - The event is reported by the callback function registered by the application layer, and the callback function also contains the corresponding processing of the event.
308
Interaction between ``API / Event`` and ESP-BLE-MESH protocol stack - API used by user mainly calls functions provided by ``Mesh Networking``, ``Mesh Provisioning`` and ``Mesh Models``. - The interaction between ``API / Event`` and the protocol stack does not operate across the hierarchy of the protocol stack. For example, API does not call functions related to ``Network Layer``. The design and implementation of ESP-BLE-MESH architecture is based on layers and modules. In details, Section 2.1 (Mesh Networking Implementation), Section 2.2 (Mesh Provisioning Implementation) and Section 2.3 (Mesh Bearers Implementation) are based on layers, and Section 2.4 (Mesh Models Implementation) is on modules. - **Layer-based Approach**: With Layer-based approach, the architecture is designed according to the layers specified in the Mesh Profile Specification. Each layer has its unique files which include APIs of this layer and etc. The specific design is shown in Figure 2.1. - **Module-based Approach**: Every file implements an independent function that can be called by other programs.
308
.. figure:: ../../../_static/esp-ble-mesh-interface.png :align: center Figure 2.1 ESP-BLE-MESH Architecture Implementation Diagram The design of ESP-BLE-MESH architecture uses layer-based approach. The sequence of layers which data packets are processed through is fixed, i.e., the processing of packets will form a ``message flow``. Thus, we could see flows of messages from the Protocol Stack Interface Diagram in Figure 2.1. 2.1 Mesh Protocol Stack Implementation 2.1.1 Mesh Networking Implementation The list of files and the functions implemented in each file in ``Mesh Networking`` are shown in Table 2.1: .. list-table:: Table 2.1 Mesh Networking File Description :widths: 40 150 :header-rows: 1 - Functionality - ESP-BLE-MESH Access Layer - ESP-BLE-MESH Lower/Upper Transport Layer - ESP-BLE-MESH Network Layer - A task used to send ESP-BLE-MESH advertising packets, a callback used to handle received advertising packets and APIs used to allocate adv buffers 2.
308
1.2 Mesh Provisioning Implementation The implementation of Mesh Provisioning is divided into two chunks due to the Node/Provisioner coexistence. Specific files that provide implementation of provisioning of Node are shown in Table 2.2: .. list-table:: Table 2.2 Mesh Provisioning (Node) File Description :widths: 40 150 :header-rows: 1 - Functionality - ESP-BLE-MESH Node provisioning (PB-ADV & PB-GATT) - ESP-BLE-MESH Proxy Server related functionalities - APIs used to handle ESP-BLE-MESH Beacons Specific files that implement functions of Provisioner are shown in Table 2.3: .. list-table:: Table 2.3 Mesh Provisioning (Provisioner) File Description :widths: 40 150 :header-rows: 1 - Functionality - ESP-BLE-MESH Provisioner provisioning (PB-ADV & PB-GATT) - ESP-BLE-MESH Proxy Client related functionalities - ESP-BLE-MESH Provisioner networking related functionalities 2.1.3 Mesh Models Implementation Mesh Models are used to implement the specific functions of model in nodes.
308
Server model is used to maintain node status. Client model is used to obtain and modify node state. .. list-table:: Table 2.4 Mesh Models File Description :widths: 40 150 :header-rows: 1 - Functionality - Send Configuration Client messages and receive corresponding response messages - Receive Configuration Client messages and send proper response messages - Send Health Client messages and receive corresponding response messages - Receive Health Client messages and send proper response messages - ESP-BLE-MESH model related operations - Send ESP-BLE-MESH Generic Client messages and receive corresponding response messages - Send ESP-BLE-MESH Lighting Client messages and receive corresponding response messages - Send ESP-BLE-MESH Sensor Client messages and receive corresponding response messages - Send ESP-BLE-MESH Time Scene Client messages and receive corresponding response messages - Receive ESP-BLE-MESH Generic Client messages and send corresponding response messages - Receive ESP-BLE-MESH Lighting Client messages and send corresponding response messages - Receive ESP-BLE-MESH Sensor Client messages and send corresponding response messages - Receive ESP-BLE-MESH Time Scene Client messages and send corresponding response messages 2.
308
2 Mesh Bearers Implementation Portability is fully considered in the implementation of Mesh Bearers. When the ESP-BLE-MESH protocol stack is being ported to other platforms, users only need to modify :component_file:`adapter.c ` (example of :component_file:`NimBLE version `). .. list-table:: Table 2.5 Mesh Bearers File Description :widths: 40 150 :header-rows: 1 - Functionality - ESP-BLE-MESH Bearer Layer adapter,This file provides the interfaces used to receive and send ESP-BLE-MESH ADV & GATT related packets. .. note:: :component_file:`adapter.c ` is the implementation of ``Advertising Bearer`` and ``GATT Bearer`` in Mesh Networking framework. 2.3 Mesh Applications Implementation We have provided a series of application examples for customer development, and users can develop products based on :ref:`esp-ble-mesh-examples`. Auxiliary routine refers to optional functions in the ESP-BLE-MESH protocol stack. The design of the auxiliary routine generally implement the truncation of code through :ref:`CONFIG_BLE_MESH`.
308
3.1 Features - Low Power - Friend - Relay - Proxy Client/Server 3.2 Network Management - Node Removal procedure - IV Index Recovery procedure - IV Update procedure - Key Refresh procedure - Network Creation procedure - NVS Storage 3.3 Auxiliary Routine Implementation When adopting the design of independent module, the two main factors should be considered: - The module can not be implemented hierarchically, and it can be completely independent, which means it does not rely on the implementation of other modules. - The functions in the module will be used repeatedly, so it is reasonable to design it into a module. Independent module is shown in Table 3.1: .. list-table:: Table 3.1 Module File Description :widths: 40 150 :header-rows: 1 - Functionality - ESP-BLE-MESH Low Power functionality - ESP-BLE-MESH Friend functionality - ESP-BLE-MESH Relay feature, network creation, IV Update procedure, IV Index recovery procedure, Key Refresh procedure related functionalities - ESP-BLE-MESH Proxy Server related functionalities - ESP-BLE-MESH Proxy Client related functionalities - ESP-BLE-MESH NVS storage functionality - ESP-BLE-MESH stack initialize, stack enable, node removal related functionalities
308
Using Debugger This section covers the steps to configure and run a debugger using various methods, including: For how to run a debugger from VS Code, see `Configuration for Visual Studio Code Debug `__. .. _jtag-debugging-using-debugger-eclipse: Eclipse .. note:: It is recommended to first check if debugger works using :ref:`jtag-debugging-with-idf-py` or from :ref:`jtag-debugging-using-debugger-command-line` and then move to using Eclipse. Eclipse is an integrated development environment (IDE) that provides a powerful set of tools for developing and debugging software applications. For ESP-IDF applications, `IDF Eclipse plugin `_ provides two ways of debugging: By default, Eclipse supports OpenOCD Debugging via the GDB Hardware Debugging plugin, which requires starting the OpenOCD server from the command line and configuring the GDB client from Eclipse to start with the debugging. This approach can be time-consuming and error-prone. To make the debugging process easier, the IDF Eclipse plugin has a customized ESP-IDF GDB OpenOCD Debugging functionality.
309
This functionality supports configuring the OpenOCD server and GDB client from within Eclipse. All the required configuration parameters will be pre-filled by the plugin, and you can start debugging with just a click of a button. Therefore, it is recommended to use the `ESP-IDF GDB OpenOCD Debugging `_ via the IDF Eclipse plugin. **GDB Hardware Debugging** .. note:: This approach is recommended only if you are unable to debug using `ESP-IDF GDB OpenOCD Debugging `_ for some reason. To install the ``GDB Hardware Debugging`` plugin, open Eclipse and select ``Help`` > ``Install`` New Software. After installation is complete, follow these steps to configure the debugging session. Please note that some configuration parameters are generic, while others are project-specific. This will be shown below by configuring debugging for "blink" example project. If not done already, add this project to Eclipse workspace following `Eclipse Plugin `_. The source of :example:`get-started/blink` application is available in :idf:`examples` directory of ESP-IDF repository.
309
A sample window with settings entered in points 1 - 5 is shown below. .. figure:: ../../../_static/hw-debugging-main-tab.jpg :align: center :alt: Configuration of GDB Hardware Debugging - Main tab :figclass: align-center Configuration of GDB Hardware Debugging - Main tab Configuration entered in points 6 and 7 is shown on the following picture. .. figure:: ../../../_static/hw-debugging-debugger-tab.jpg :align: center :alt: Configuration of GDB Hardware Debugging - Debugger tab :figclass: align-center Configuration of GDB Hardware Debugging - Debugger tab :: mon reset halt maintenance flush register-cache set remote hardware-watchpoint-limit 2 .. note:: To automatically update the image in the flash before starting a new debug session, add the following command lines to the beginning of the ``Initialization Commands`` textbox:: mon reset halt mon program_esp ${workspace_loc:blink/build/blink.
309
bin} 0x10000 verify For description of ``program_esp`` command, see :ref:`jtag-upload-app-debug`. Configuration described in points 8 - 11 is shown below. .. figure:: ../../../_static/hw-debugging-startup-tab.jpg :align: center :alt: Configuration of GDB Hardware Debugging - Startup tab :figclass: align-center Configuration of GDB Hardware Debugging - Startup tab If the ``Startup`` sequence looks convoluted and respective ``Initialization Commands`` are unclear, check :ref:`jtag-debugging-tip-debugger-startup-commands` for additional explanation. Once all configuration steps 1-12 are satisfied, the new Eclipse perspective called "Debug" will open, as shown in the example picture below. .. figure:: ../../../_static/debug-perspective.jpg :align: center :alt: Debug Perspective in Eclipse :figclass: align-center Debug Perspective in Eclipse If you are not quite sure how to use GDB, check :ref:`jtag-debugging-examples-eclipse` example debugging session in section :ref:`jtag-debugging-examples`.
309
.. _jtag-debugging-using-debugger-command-line: Command Line .. highlight:: bash :: cd ~/esp/blink .. highlight:: none :: target remote :3333 set remote hardware-watchpoint-limit 2 mon reset halt maintenance flush register-cache thb app_main c Save this file in the current directory. For more details on what is inside ``gdbinit`` file, see :ref:`jtag-debugging-tip-debugger-startup-commands` .. highlight:: bash :: {IDF_TARGET_TOOLCHAIN_PREFIX}-gdb -x gdbinit build/blink.elf .. highlight:: none :: user-name@computer-name:~/esp/blink$ {IDF_TARGET_TOOLCHAIN_PREFIX}-gdb -x gdbinit build/blink.elf GNU gdb (crosstool-NG crosstool-ng-1.22.0-61-gab8375a) 7.10 Copyright (C) 2015 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.
309
Type "show copying" and "show warranty" for details. This GDB was configured as "--host=x86_64-build_pc-linux-gnu --target={IDF_TARGET_TOOLCHAIN_PREFIX}". Type "show configuration" for configuration details. For bug reporting instructions, please see: . Find the GDB manual and other documentation resources online at: . For help, type "help". Type "apropos word" to search for commands related to "word"... Reading symbols from build/blink.elf...done. 0x400d10d8 in esp_vApplicationIdleHook () at /home/user-name/esp/esp-idf/components/{IDF_TARGET_PATH_NAME}/./freertos_hooks.c:52 52 asm("waiti 0"); JTAG tap: {IDF_TARGET_PATH_NAME}.cpu0 tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1) JTAG tap: {IDF_TARGET_PATH_NAME}.slave tap/device found: 0x120034e5 (mfg: 0x272 (Tensilica), part: 0x2003, ver: 0x1) {IDF_TARGET_PATH_NAME}: Debug controller was reset (pwrstat=0x5F, after clear 0x0F).
309
{IDF_TARGET_PATH_NAME}: Core was reset (pwrstat=0x5F, after clear 0x0F). Target halted. PRO_CPU: PC=0x5000004B (active) APP_CPU: PC=0x00000000 {IDF_TARGET_PATH_NAME}: target state: halted {IDF_TARGET_PATH_NAME}: Core was reset (pwrstat=0x1F, after clear 0x0F). Target halted. PRO_CPU: PC=0x40000400 (active) APP_CPU: PC=0x40000400 {IDF_TARGET_PATH_NAME}: target state: halted Hardware assisted breakpoint 1 at 0x400db717: file /home/user-name/esp/blink/main/./blink.c, line 43. 0x0: 0x00000000 Target halted. PRO_CPU: PC=0x400DB717 (active) APP_CPU: PC=0x400D10D8 [New Thread 1073428656] [New Thread 1073413708] [New Thread 1073431316] [New Thread 1073410672] [New Thread 1073408876] [New Thread 1073432196] [New Thread 1073411552] [Switching to Thread 1073411996] Temporary breakpoint 1, app_main () at /home/user-name/esp/blink/main/./blink.
309
c:43 43 xTaskCreate(&blink_task, "blink_task", 512, NULL, 5, NULL); (gdb) Note the third-to-last line, which shows debugger halting at breakpoint established in ``gdbinit`` file at function ``app_main()``. Since the processor is halted, the LED should not be blinking. If this is what you see as well, you are ready to start debugging. If you are not sure how to use GDB, check :ref:`jtag-debugging-examples-command-line` example debugging session in section :ref:`jtag-debugging-examples`. .. _jtag-debugging-with-idf-py: Idf.py Debug Targets It is also possible to execute the described debugging tools conveniently from ``idf.py``. These commands are supported: Runs OpenOCD in a console with configuration defined in the environment or via command line. It uses default script directory defined as ``OPENOCD_SCRIPTS`` environmental variable, which is automatically added from an Export script (``export.sh`` or ``export.bat``). It is possible to override the script location using command line argument ``--openocd-scripts``.
309
.. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: idf-py-openocd-default-cfg :end-before: --- To configure the JTAG configuration for the current board, please use the environmental variable ``OPENOCD_COMMANDS`` or ``--openocd-commands`` command line argument. If none of the above is defined, OpenOCD is started with |idf-py-def-cfg| board definition. Starts the GDB the same way as the :ref:`jtag-debugging-using-debugger-command-line`, but generates the initial GDB scripts referring to the current project elf file. The same as `2`, but starts the gdb with ``tui`` argument, allowing for a simple source code view. Starts `gdbgui `_ debugger frontend enabling out-of-the-box debugging in a browser window. To enable this option, run the install script with the "--enable-gdbgui" argument, e.g., ``install.sh --enable-gdbgui``. You can combine these debugging actions on a single command line, allowing for convenient setup of blocking and non-blocking actions in one step.
309
``idf.py`` implements a simple logic to move the background actions (such as openocd) to the beginning and the interactive ones (such as gdb, monitor) to the end of the action list. An example of a very useful combination is:: idf.py openocd gdbgui monitor The above command runs OpenOCD in the background, starts `gdbgui `_ to open a browser window with active debugger frontend and opens a serial monitor in the active console.
309
JTAG Debugging This document provides a guide to installing OpenOCD for {IDF_TARGET_NAME} and debugging using GDB. .. note:: You can also debug your {IDF_TARGET_NAME} without needing to setup JTAG or OpenOCD by using ``idf.py monitor``. See: :doc:`../../api-guides/tools/idf-monitor` and :ref:`CONFIG_ESP_SYSTEM_GDBSTUB_RUNTIME`. .. highlight:: none The document is structured as follows: :ref:`jtag-debugging-introduction` Introduction to the purpose of this guide. :ref:`jtag-debugging-how-it-works` Description how {IDF_TARGET_NAME}, JTAG interface, OpenOCD and GDB are interconnected and working together to enable debugging of {IDF_TARGET_NAME}. :ref:`jtag-debugging-selecting-jtag-adapter` What are the criteria and options to select JTAG adapter hardware. :ref:`jtag-debugging-setup-openocd` Procedure to install OpenOCD and verify that it is installed. :ref:`jtag-debugging-configuring-target` Configuration of OpenOCD software and setting up of JTAG adapter hardware, which together make up the debugging target.
310
:ref:`jtag-debugging-launching-debugger` Steps to start up a debug session with GDB from :ref:`jtag-debugging-using-debugger-eclipse` and from :ref:`jtag-debugging-using-debugger-command-line`. :ref:`jtag-debugging-examples` If you are not familiar with GDB, check this section for debugging examples provided from :ref:`jtag-debugging-examples-eclipse` as well as from :ref:`jtag-debugging-examples-command-line`. :ref:`jtag-debugging-building-openocd` Procedure to build OpenOCD from sources for :doc:`Windows `, :doc:`Linux ` and :doc:`macOS ` operating systems. :ref:`jtag-debugging-tips-and-quirks` This section provides collection of tips and quirks related to JTAG debugging of {IDF_TARGET_NAME} with OpenOCD and GDB. .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: devkit-defs :end-before: --- .. _jtag-debugging-introduction: Introduction .. only:: esp32 The ESP32 has two powerful Xtensa cores, allowing for a great deal of variety of program architectures.
310
The FreeRTOS OS that comes with ESP-IDF is capable of multi-core preemptive scheduling, allowing for an intuitive way of writing software. The downside of the ease of programming is that debugging without the right tools is harder: figuring out a bug that is caused by two threads, running even simultaneously on two different CPU cores, can take a long time when all you have are ``printf()`` statements. A better (and in many cases quicker) way to debug such problems is by using a debugger, connected to the processors over a debug port. Espressif has ported OpenOCD to support the {IDF_TARGET_NAME} processor and the multi-core FreeRTOS (which is the foundation of most {IDF_TARGET_NAME} apps). Additionally, some extra tools have been written to provide extra features that OpenOCD does not support natively. This document provides a guide to installing OpenOCD for {IDF_TARGET_NAME} and debugging using GDB under Linux, Windows and macOS. Except for OS specific installation procedures, the s/w user interface and use procedures are the same across all supported operating systems.
310
.. note:: Screenshots presented in this document have been made for Eclipse Neon 3 running on Ubuntu 16.04 LTS. There may be some small differences in what a particular user interface looks like, depending on whether you are using Windows, macOS or Linux and/or a different release of Eclipse. .. _jtag-debugging-how-it-works: How it Works? The key software and hardware components that perform debugging of {IDF_TARGET_NAME} with OpenOCD over JTAG (Joint Test Action Group) interface is presented in the diagram below under the "Debugging With JTAG" label. These components include {IDF_TARGET_TOOLCHAIN_PREFIX}-gdb debugger, OpenOCD on chip debugger, and the JTAG adapter connected to {IDF_TARGET_NAME} target. .. figure:: ../../../_static/jtag-debugging-overview.jpg :align: center :alt: JTAG debugging - overview diagram :figclass: align-center JTAG debugging - overview diagram Likewise, the "Application Loading and Monitoring" label indicates the key software and hardware components that allow an application to be compiled, built, and flashed to {IDF_TARGET_NAME}, as well as to provide means to monitor diagnostic messages from {IDF_TARGET_NAME}.
310
"Debugging With JTAG" and "Application Loading and Monitoring" is integrated under the `Eclipse `_ IDE in order to provide a quick and easy transition between writing/compiling/loading/debugging code. The Eclipse IDE (and the integrated debugging software) is available for Windows, Linux and macOS platforms. Depending on user preferences, both the debugger and ``idf.py build`` can also be used directly from terminal/command line, instead of Eclipse. .. only:: esp32 or esp32s2 If the |devkit-name-with-link| is used, then connection from PC to {IDF_TARGET_NAME} is done effectively with a single USB cable. This is made possible by the FT2232H chip, which provides two USB channels, one for JTAG and the other for UART connection. .. only:: SOC_USB_SERIAL_JTAG_SUPPORTED The connection from PC to {IDF_TARGET_NAME} is done effectively with a single USB cable. This is made possible by the {IDF_TARGET_NAME} chip itself, which provides two USB channels, one for JTAG and the other for the USB terminal connection.
310
The USB cable should be connected to the D+/D- USB pins of {IDF_TARGET_NAME} and not to the serial RxD/TxD through a USB-to-UART chip. The proper connection is explained later in subsection :ref:`jtag-debugging-configuring-target`. .. only:: esp32c3 .. note:: Debugging through the USB interface implemented in {IDF_TARGET_NAME} requires to have a chip with revision 0.3 or newer. Please use other debugging options (e.g., with ESP-Prog) for chip revisions 0.1 and 0.2. The easiest way to determine the chip revision is to look for the ``boot: chip revision: v0.3`` message at the beginning of the boot log. See `SoC errata `_ for more details. .. _jtag-debugging-selecting-jtag-adapter: Selecting JTAG Adapter .. only:: esp32 or esp32s2 The quickest and most convenient way to start with JTAG debugging is by using |devkit-name-with-link|. Each version of this development board has JTAG interface already built in. No need for an external JTAG adapter and extra wiring/cable to connect JTAG to {IDF_TARGET_NAME}.
310
|devkit-name| is using FT2232H JTAG interface operating at 20 MHz clock speed, which is difficult to achieve with an external adapter. .. only:: SOC_USB_SERIAL_JTAG_SUPPORTED The quickest and most convenient way to start with JTAG debugging is through a USB cable connected to the D+/D- USB pins of {IDF_TARGET_NAME}. No need for an external JTAG adapter and extra wiring/cable to connect JTAG to {IDF_TARGET_NAME}. If you decide to use separate JTAG adapter, look for one that is compatible with both the voltage levels on the {IDF_TARGET_NAME} as well as with the OpenOCD software. The JTAG port on the {IDF_TARGET_NAME} is an industry-standard JTAG port which lacks (and does not need) the TRST pin. The JTAG I/O pins all are powered from the VDD_3P3_RTC pin (which normally would be powered by a 3.3 V rail) so the JTAG adapter needs to be able to work with JTAG pins in that voltage range. On the software side, OpenOCD supports a fair amount of JTAG adapters. See https://openocd.org/doc/html/Debug-Adapter-Hardware.
310
html for an (unfortunately slightly incomplete) list of the adapters OpenOCD works with. This page lists SWD-compatible adapters as well; take note that the {IDF_TARGET_NAME} does not support SWD. JTAG adapters that are hardcoded to a specific product line, e.g., ST-LINK debugging adapters for STM32 families, will not work. The minimal signalling to get a working JTAG connection are TDI, TDO, TCK, TMS and GND. Some JTAG debuggers also need a connection from the {IDF_TARGET_NAME} power line to a line called e.g., Vtar to set the working voltage. SRST can optionally be connected to the CH_PD of the {IDF_TARGET_NAME}, although for now, support in OpenOCD for that line is pretty minimal. `ESP-Prog `_ is an example for using an external board for debugging by connecting it to the JTAG pins of {IDF_TARGET_NAME}. .. _jtag-debugging-setup-openocd: Setup of OpenOCD .. highlight:: bash If you have already set up ESP-IDF with CMake build system according to the :doc:`Getting Started Guide `, then OpenOCD is already installed.
310
After :ref:`setting up the environment ` in your terminal, you should be able to run OpenOCD. Check this by executing the following command:: openocd --version .. highlight:: none The output should be as follows (although the version may be more recent than listed here):: Open On-Chip Debugger v0.10.0-esp32-20190708 (2019-07-08-11:04) Licensed under GNU GPL v2 For bug reports, read https://openocd.org/doc/doxygen/bugs.html You may also verify that OpenOCD knows where its configuration scripts are located by printing the value of ``OPENOCD_SCRIPTS`` environment variable, by typing ``echo $OPENOCD_SCRIPTS`` (for Linux and macOS) or ``echo %OPENOCD_SCRIPTS%`` (for Windows). If a valid path is printed, then OpenOCD is set up correctly. If any of these steps do not work, please go back to the :ref:`setting up the tools ` section (for Linux and macOS) or :ref:`ESP-IDF Tools Installer ` (for Windows) section of the Getting Started Guide. .. note:: It is also possible to build OpenOCD from source.
310
Please refer to :ref:`jtag-debugging-building-openocd` section for details. .. _jtag-debugging-configuring-target: Configuring {IDF_TARGET_NAME} Target Once OpenOCD is installed, you can proceed to configuring the {IDF_TARGET_NAME} target (i.e {IDF_TARGET_NAME} board with JTAG interface). Configuring the target is split into the following three steps: .. _jtag-debugging-configure-and-connect-JTAG-interface: Configure and Connect JTAG Interface This step depends on the JTAG and {IDF_TARGET_NAME} board you are using (see the two cases described below). .. toctree:: :maxdepth: 1 :esp32: configure-ft2232h-jtag :esp32s2: configure-ft2232h-jtag :SOC_USB_SERIAL_JTAG_SUPPORTED: configure-builtin-jtag configure-other-jtag .. _jtag-debugging-run-openocd: Run OpenOCD Once target is configured and connected to computer, you are ready to launch OpenOCD. .. highlight:: bash Open a terminal and set it up for using the ESP-IDF as described in the :ref:`setting up the environment ` section of the Getting Started Guide.
310
Then run OpenOCD (this command works on Windows, Linux, and macOS): .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: run-openocd :end-before: --- {IDF_TARGET_FTDI_CONFIG:default="Not Updated!", esp32s3="board/esp32s3-ftdi.cfg", esp32c3="board/esp32c3-ftdi.cfg", esp32c6="board/esp32c6-ftdi.cfg", esp32h2="board/esp32h2-ftdi.cfg"} .. note:: The files provided after ``-f`` above are specific for |run-openocd-device-name|. You may need to provide different files depending on the hardware that is used. For guidance see :ref:`jtag-debugging-tip-openocd-configure-target`. .. only:: SOC_USB_SERIAL_JTAG_SUPPORTED For example, ``{IDF_TARGET_FTDI_CONFIG}`` can be used for a custom board with an FT2232H or FT232H chip used for JTAG connection, or with ESP-Prog. .. highlight:: none You should now see similar output (this log is for |run-openocd-device-name|): .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: run-openocd-output :end-before: --- .. _jtag-upload-app-debug: Upload Application for Debugging Build and upload your application to {IDF_TARGET_NAME} as usual, see :ref:`get-started-build`.
310
Another option is to write application image to flash using OpenOCD via JTAG with commands like this: .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: run-openocd-upload :end-before: --- OpenOCD flashing command ``program_esp`` has the following format: ``program_esp [verify] [reset] [exit] [compress] [encrypt]`` - ``image_file`` - Path to program image file. - ``offset`` - Offset in flash bank to write image. - ``verify`` - Optional. Verify flash contents after writing. - ``reset`` - Optional. Reset target after programing. - ``exit`` - Optional. Finally exit OpenOCD. - ``compress`` - Optional. Compress image file before programming. - ``encrypt`` - Optional. Encrypt binary before writing to flash. Same functionality with ``idf.py encrypted-flash`` You are now ready to start application debugging. Follow the steps described in the section below. .. _jtag-debugging-launching-debugger: Launching Debugger The toolchain for {IDF_TARGET_NAME} features GNU Debugger, in short GDB.
310
It is available with other toolchain programs under filename: {IDF_TARGET_TOOLCHAIN_PREFIX}-gdb. GDB can be called and operated directly from command line in a terminal. Another option is to call it from within IDE (like Eclipse, Visual Studio Code, etc.) and operate indirectly with help of GUI instead of typing commands in a terminal. The options of using debugger are discussed under links below. It is recommended to first check if debugger works from :ref:`jtag-debugging-using-debugger-command-line` and then move to using :ref:`jtag-debugging-using-debugger-eclipse`. .. _jtag-debugging-examples: Debugging Examples This section is intended for users not familiar with GDB. It presents example debugging session from :ref:`jtag-debugging-examples-eclipse` using simple application available under :example:`get-started/blink` and covers the following debugging actions: Similar debugging actions are provided using GDB from :ref:`jtag-debugging-examples-command-line`. .. note:: :ref:`jtag-debugging-examples-command-line-08` is currently only available for command line debugging.
310
Before proceeding to examples, set up your {IDF_TARGET_NAME} target and load it with :example:`get-started/blink`. .. _jtag-debugging-building-openocd: Building OpenOCD from Sources Please refer to separate documents listed below, that describe build process. .. toctree:: :maxdepth: 1 Windows Linux macOS The examples of invoking OpenOCD in this document assume using pre-built binary distribution described in section :ref:`jtag-debugging-setup-openocd`. To use binaries build locally from sources, change the path to OpenOCD executable to ``src/openocd`` and set the ``OPENOCD_SCRIPTS`` environment variable so that OpenOCD can find the configuration files. For Linux and macOS: .. code-block:: bash cd ~/esp/openocd-esp32 export OPENOCD_SCRIPTS=$PWD/tcl For Windows: .. code-block:: batch cd %USERPROFILE%\esp\openocd-esp32 set "OPENOCD_SCRIPTS=%CD%\tcl" Example of invoking OpenOCD build locally from sources, for Linux and macOS: .. include:: {IDF_TARGET_PATH_NAME}.
310
inc :start-after: run-openocd-src-linux :end-before: --- and Windows: .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: run-openocd-src-win :end-before: --- .. _jtag-debugging-tips-and-quirks: Tips and Quirks This section provides collection of links to all tips and quirks referred to from various parts of this guide. .. toctree:: :maxdepth: 2 tips-and-quirks Related Documents .. toctree:: :hidden: :maxdepth: 1 using-debugger debugging-examples tips-and-quirks ../app_trace - :doc:`using-debugger` - :doc:`debugging-examples` - :doc:`tips-and-quirks` - :doc:`../app_trace` - `Introduction to ESP-Prog Board `__
310
Debugging Examples This section describes debugging with GDB from :ref:`jtag-debugging-examples-eclipse` as well as from :ref:`jtag-debugging-examples-command-line`. .. highlight:: none .. _jtag-debugging-examples-eclipse: Eclipse Verify if your target is ready and loaded with :example:`get-started/blink` example. Configure and start debugger following steps in section :ref:`jtag-debugging-using-debugger-eclipse`. Pick up where target was left by debugger, i.e., having the application halted at breakpoint established at ``app_main()``. .. figure:: ../../../_static/debug-perspective.jpg :align: center :alt: Debug Perspective in Eclipse :figclass: align-center Debug Perspective in Eclipse Examples in This Section .. _jtag-debugging-examples-eclipse-01: Navigating Through the Code, Call Stack and Threads When the target is halted, debugger shows the list of threads in "Debug" window. The line of code where program halted is highlighted in another window below, as shown on the following picture.
311
The LED stops blinking. .. figure:: ../../../_static/debugging-target-halted.jpg :align: center :alt: Target halted during debugging :figclass: align-center Target halted during debugging Specific thread where the program halted is expanded showing the call stack. It represents function calls that lead up to the highlighted line of code, where the target halted. The first line of call stack under Thread #1 contains the last called function ``app_main()``, that in turn was called from function ``main_task()`` shown in a line below. Each line of the stack also contains the file name and line number where the function was called. By clicking/highlighting the stack entries, in window below, you will see contents of this file. By expanding threads you can navigate throughout the application. Expand Thread #5 that contains much longer call stack. You will see there, besides function calls, numbers like ``0x4000000c``. They represent addresses of binary code not provided in source form.
311
.. figure:: ../../../_static/debugging-navigate-through-the-stack.jpg :align: center :alt: Navigate through the call stack :figclass: align-center Navigate through the call stack In another window on right, you can see the disassembled machine code no matter if your project provides it in source or only the binary form. Go back to the ``app_main()`` in Thread #1 to familiar code of ``blink.c`` file that will be examined in more details in the following examples. Debugger makes it easy to navigate through the code of entire application. This comes handy when stepping through the code and working with breakpoints and will be discussed below. .. _jtag-debugging-examples-eclipse-02: Setting and Clearing Breakpoints When debugging, we would like to be able to stop the application at critical lines of code and then examine the state of specific variables, memory and registers/peripherals. To do so we are using breakpoints. They provide a convenient way to quickly get to and halt the application at specific line.
311
Let's establish two breakpoints when the state of LED changes. Basing on code listing above, this happens at lines 33 and 36. To do so, hold the "Control" on the keyboard and double clink on number ``33`` in file ``blink.c`` file. A dialog will open where you can confirm your selection by pressing "OK" button. If you do not like to see the dialog just double click the line number. Set another breakpoint in line 36. .. figure:: ../../../_static/debugging-setting-breakpoint.jpg :align: center :alt: Setting a breakpoint :figclass: align-center Setting a breakpoint Information how many breakpoints are set and where is shown in window "Breakpoints" on top right. Click "Show Breakpoints Supported by Selected Target" to refresh this list. Besides the two just set breakpoints the list may contain temporary breakpoint at function ``app_main()`` established at debugger start. As maximum two breakpoints are allowed (see :ref:`jtag-debugging-tip-breakpoints`), you need to delete it, or debugging will fail.
311
.. figure:: ../../../_static/debugging-three-breakpoints-set.jpg :align: center :alt: Three breakpoints are set / maximum two are allowed :figclass: align-center Three breakpoints are set / maximum two are allowed If you now click "Resume" (click ``blink_task()`` under "Tread #8", if "Resume" button is grayed out), the processor will run and halt at a breakpoint. Clicking "Resume" another time will make it run again, halt on second breakpoint, and so on. You will be also able to see that LED is changing the state after each click to "Resume" program execution. Read more about breakpoints under :ref:`jtag-debugging-tip-breakpoints` and :ref:`jtag-debugging-tip-where-breakpoints` .. _jtag-debugging-examples-eclipse-03: Halting the Target Manually When debugging, you may resume application and enter code waiting for some event or staying in infinite loop without any break points defined. In such case, to go back to debugging mode, you can break program execution manually by pressing "Suspend" button.
311
To check it, delete all breakpoints and click "Resume". Then click "Suspend". Application will be halted at some random point and LED will stop blinking. Debugger will expand tread and highlight the line of code where application halted. .. figure:: ../../../_static/debugging-target-halted-manually.jpg :align: center :alt: Target halted manually :figclass: align-center Target halted manually In particular case above, the application has been halted in line 52 of code in file ``freertos_hooks.c`` Now you can resume it again by pressing "Resume" button or do some debugging as discussed below. .. _jtag-debugging-examples-eclipse-04: Stepping Through the Code It is also possible to step through the code using "Step Into (F5)" and "Step Over (F6)" commands. The difference is that "Step Into (F5)" is entering inside subroutines calls, while "Step Over (F6)" steps over the call, treating it as a single source line. Before being able to demonstrate this functionality, using information discussed in previous paragraph, make sure that you have only one breakpoint defined at line ``36`` of ``blink.
311
c``. Resume program by entering pressing F8 and let it halt. Now press "Step Over (F6)", one by one couple of times, to see how debugger is stepping one program line at a time. .. figure:: ../../../_static/debugging-step-over.jpg :align: center :alt: Stepping through the code with "Step Over (F6)" :figclass: align-center Stepping through the code with "Step Over (F6)" If you press "Step Into (F5)" instead, then debugger will step inside subroutine calls. .. figure:: ../../../_static/debugging-step-into.jpg :align: center :alt: Stepping through the code with "Step Into (F5)" :figclass: align-center Stepping through the code with "Step Into (F5)" In this particular case debugger stepped inside ``gpio_set_level(BLINK_GPIO, 0)`` and effectively moved to ``gpio.c`` driver code. See :ref:`jtag-debugging-tip-why-next-works-as-step` for potential limitation of using ``next`` command. .. _jtag-debugging-examples-eclipse-05: Checking and Setting Memory To display or set contents of memory use "Memory" tab at the bottom of "Debug" perspective.
311
With the "Memory" tab, we will read from and write to the memory location ``0x3FF44004`` labeled as ``GPIO_OUT_REG`` used to set and clear individual GPIO's. For more information, see *{IDF_TARGET_NAME} Technical Reference Manual* > *IO MUX and GPIO Matrix (GPIO, IO_MUX)* [`PDF `__]. Being in the same ``blink.c`` project as before, set two breakpoints right after ``gpio_set_level`` instruction. Click "Memory" tab and then "Add Memory Monitor" button. Enter ``0x3FF44004`` in provided dialog. Now resume program by pressing F8 and observe "Monitor" tab. .. figure:: ../../../_static/debugging-memory-location-on.jpg :align: center :alt: Observing memory location 0x3FF44004 changing one bit to ON" :figclass: align-center Observing memory location 0x3FF44004 changing one bit to "ON" You should see one bit being flipped over at memory location ``0x3FF44004`` (and LED changing the state) each time F8 is pressed. .. figure:: ../../../_static/debugging-memory-location-off.
311
jpg :align: center :alt: Observing memory location 0x3FF44004 changing one bit to ON" :figclass: align-center Observing memory location 0x3FF44004 changing one bit to "OFF" To set memory use the same "Monitor" tab and the same memory location. Type in alternate bit pattern as previously observed. Immediately after pressing enter you will see LED changing the state. .. _jtag-debugging-examples-eclipse-06: Watching and Setting Program Variables A common debugging tasks is checking the value of a program variable as the program runs. To be able to demonstrate this functionality, update file ``blink.c`` by adding a declaration of a global variable ``int i`` above definition of function ``blink_task``. Then add ``i++`` inside ``while(1)`` of this function to get ``i`` incremented on each blink. Exit debugger, so it is not confused with new code, build and flash the code to the ESP and restart debugger. There is no need to restart OpenOCD. Once application is halted, enter a breakpoint in the line where you put ``i++``.
311
In next step, in the window with "Breakpoints", click the "Expressions" tab. If this tab is not visible, then add it by going to the top menu Window > Show View > Expressions. Then click "Add new expression" and enter ``i``. Resume program execution by pressing F8. Each time the program is halted you will see ``i`` value being incremented. .. figure:: ../../../_static/debugging-watch-variable.jpg :align: center :alt: Watching program variable "i" :figclass: align-center Watching program variable "i" To modify ``i`` enter a new number in "Value" column. After pressing "Resume (F8)" the program will keep incrementing ``i`` starting from the new entered number. .. _jtag-debugging-examples-eclipse-07: Setting Conditional Breakpoints Here comes more interesting part. You may set a breakpoint to halt the program execution, if certain condition is satisfied. Right click on the breakpoint to open a context menu and select "Breakpoint Properties". Change the selection under "Type:" to "Hardware" and enter a "Condition:" like ``i == 2``.
311
.. figure:: ../../../_static/debugging-setting-conditional-breakpoint.jpg :align: center :alt: Setting a conditional breakpoint :figclass: align-center Setting a conditional breakpoint If current value of ``i`` is less than ``2`` (change it if required) and program is resumed, it will blink LED in a loop until condition ``i == 2`` gets true and then finally halt. .. _jtag-debugging-examples-command-line: Command Line Verify if your target is ready and loaded with :example:`get-started/blink` example. Configure and start debugger following steps in section :ref:`jtag-debugging-using-debugger-command-line`. Pick up where target was left by debugger, i.e. having the application halted at breakpoint established at ``app_main()``:: Temporary breakpoint 1, app_main () at /home/user-name/esp/blink/main/./blink.c:43 43 xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL); (gdb) Examples in This Section .. _jtag-debugging-examples-command-line-01: Navigating Through the Code, Call Stack and Threads When you see the ``(gdb)`` prompt, the application is halted.
311
LED should not be blinking. To find out where exactly the code is halted, enter ``l`` or ``list``, and debugger will show couple of lines of code around the halt point (line 43 of code in file ``blink.c``) :: (gdb) l 38 } 39 } 40 41 void app_main() 42 { 43 xTaskCreate(&blink_task, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL); 44 } (gdb) Check how code listing works by entering, e.g., ``l 30, 40`` to see particular range of lines of code. You can use ``bt`` or ``backtrace`` to see what function calls lead up to this code:: (gdb) bt #0 app_main () at /home/user-name/esp/blink/main/./blink.c:43 #1 0x400d057e in main_task (args=0x0) at /home/user-name/esp/esp-idf/components/{IDF_TARGET_PATH_NAME}/./cpu_start.c:339 (gdb) Line #0 of output provides the last function call before the application halted, i.e., ``app_main ()`` we have listed previously. The ``app_main ()`` was in turn called by function ``main_task`` from line 339 of code located in file ``cpu_start.
311
c``. To get to the context of ``main_task`` in file ``cpu_start.c``, enter ``frame N``, where N = 1, because the ``main_task`` is listed under #1):: (gdb) frame 1 #1 0x400d057e in main_task (args=0x0) at /home/user-name/esp/esp-idf/components/{IDF_TARGET_PATH_NAME}/./cpu_start.c:339 339 app_main(); (gdb) Enter ``l`` and this will reveal the piece of code that called ``app_main()`` (in line 339):: (gdb) l 334 ; 335 } 336 #endif 337 //Enable allocation in region where the startup stacks were located. 338 heap_caps_enable_nonos_stack_heaps(); 339 app_main(); 340 vTaskDelete(NULL); 341 } 342 (gdb) By listing some lines before, you will see the function name ``main_task`` we have been looking for:: (gdb) l 326, 341 326 static void main_task(void* args) 327 { 328 // Now that the application is about to start, disable boot watchdogs 329 REG_CLR_BIT(TIMG_WDTCONFIG0_REG(0), TIMG_WDT_FLASHBOOT_MOD_EN_S); 330 REG_CLR_BIT(RTC_CNTL_WDTCONFIG0_REG, RTC_CNTL_WDT_FLASHBOOT_MOD_EN); 331 #if !
311
CONFIG_FREERTOS_UNICORE 332 // Wait for FreeRTOS initialization to finish on APP CPU, before replacing its startup stack 333 while (port_xSchedulerRunning[1] == 0) { 334 ; 335 } 336 #endif 337 //Enable allocation in region where the startup stacks were located. 338 heap_caps_enable_nonos_stack_heaps(); 339 app_main(); 340 vTaskDelete(NULL); 341 } (gdb) To see the other code, enter ``i threads``. This will show the list of threads running on target:: (gdb) i threads Id Target Id Frame 8 Thread 1073411336 (dport) 0x400d0848 in dport_access_init_core (arg=) at /home/user-name/esp/esp-idf/components/{IDF_TARGET_PATH_NAME}/./dport_access.c:170 7 Thread 1073408744 (ipc0) xQueueGenericReceive (xQueue=0x3ffae694, pvBuffer=0x0, xTicksToWait=1644638200, xJustPeeking=0) at /home/user-name/esp/esp-idf/components/freertos/./queue.c:1452 6 Thread 1073431096 (Tmr Svc) prvTimerTask (pvParameters=0x0) at /home/user-name/esp/esp-idf/components/freertos/.
311
/timers.c:445 5 Thread 1073410208 (ipc1 : Running) 0x4000bfea in ?? () 4 Thread 1073432224 (dport) dport_access_init_core (arg=0x0) at /home/user-name/esp/esp-idf/components/{IDF_TARGET_PATH_NAME}/./dport_access.c:150 3 Thread 1073413156 (IDLE) prvIdleTask (pvParameters=0x0) at /home/user-name/esp/esp-idf/components/freertos/./tasks.c:3282 2 Thread 1073413512 (IDLE) prvIdleTask (pvParameters=0x0) at /home/user-name/esp/esp-idf/components/freertos/./tasks.c:3282 (gdb) The thread list shows the last function calls per each thread together with the name of C source file if available. You can navigate to specific thread by entering ``thread N``, where ``N`` is the thread Id. To see how it works go to thread thread 5:: (gdb) thread 5 [Switching to thread 5 (Thread 1073410208)] #0 0x4000bfea in ?? () (gdb) Then check the backtrace:: (gdb) bt #0 0x4000bfea in ?? () #1 0x40083a85 in vPortCPUReleaseMutex (mux=) at /home/user-name/esp/esp-idf/components/freertos/.
311
/port.c:415 #2 0x40083fc8 in vTaskSwitchContext () at /home/user-name/esp/esp-idf/components/freertos/./tasks.c:2846 #3 0x4008532b in _frxt_dispatch () #4 0x4008395c in xPortStartScheduler () at /home/user-name/esp/esp-idf/components/freertos/./port.c:222 #5 0x4000000c in ?? () #6 0x4000000c in ?? () #7 0x4000000c in ?? () #8 0x4000000c in ?? () (gdb) As you see, the backtrace may contain several entries. This will let you check what exact sequence of function calls lead to the code where the target halted. Question marks ``??`` instead of a function name indicate that application is available only in binary format, without any source file in C language. The value like ``0x4000bfea`` is the memory address of the function call. Using ``bt``, ``i threads``, ``thread N`` and ``list`` commands we are now able to navigate through the code of entire application. This comes handy when stepping through the code and working with breakpoints and will be discussed below.
311
.. _jtag-debugging-examples-command-line-02: Setting and Clearing Breakpoints When debugging, we would like to be able to stop the application at critical lines of code and then examine the state of specific variables, memory and registers/peripherals. To do so we are using breakpoints. They provide a convenient way to quickly get to and halt the application at specific line. Let's establish two breakpoints when the state of LED changes. Basing on code listing above this happens at lines 33 and 36. Breakpoints may be established using command ``break M`` where M is the code line number:: (gdb) break 33 Breakpoint 2 at 0x400db6f6: file /home/user-name/esp/blink/main/./blink.c, line 33. (gdb) break 36 Breakpoint 3 at 0x400db704: file /home/user-name/esp/blink/main/./blink.c, line 36. If you new enter ``c``, the processor will run and halt at a breakpoint. Entering ``c`` another time will make it run again, halt on second breakpoint, and so on:: (gdb) c Continuing.
311
Target halted. PRO_CPU: PC=0x400DB6F6 (active) APP_CPU: PC=0x400D10D8 Breakpoint 2, blink_task (pvParameter=0x0) at /home/user-name/esp/blink/main/./blink.c:33 33 gpio_set_level(BLINK_GPIO, 0); (gdb) c Continuing. Target halted. PRO_CPU: PC=0x400DB6F8 (active) APP_CPU: PC=0x400D10D8 Target halted. PRO_CPU: PC=0x400DB704 (active) APP_CPU: PC=0x400D10D8 Breakpoint 3, blink_task (pvParameter=0x0) at /home/user-name/esp/blink/main/./blink.c:36 36 gpio_set_level(BLINK_GPIO, 1); (gdb) You will be also able to see that LED is changing the state only if you resume program execution by entering ``c``. To examine how many breakpoints are set and where, use command ``info break``:: (gdb) info break Num Type Disp Enb Address What 2 breakpoint keep y 0x400db6f6 in blink_task at /home/user-name/esp/blink/main/./blink.c:33 breakpoint already hit 1 time 3 breakpoint keep y 0x400db704 in blink_task at /home/user-name/esp/blink/main/.
311
/blink.c:36 breakpoint already hit 1 time (gdb) Please note that breakpoint numbers (listed under ``Num``) start with ``2``. This is because first breakpoint has been already established at function ``app_main()`` by running command ``thb app_main`` on debugger launch. As it was a temporary breakpoint, it has been automatically deleted and now is not listed anymore. To remove breakpoints enter ``delete N`` command (in short ``d N``), where ``N`` is the breakpoint number:: (gdb) delete 1 No breakpoint number 1. (gdb) delete 2 (gdb) Read more about breakpoints under :ref:`jtag-debugging-tip-breakpoints` and :ref:`jtag-debugging-tip-where-breakpoints` .. _jtag-debugging-examples-command-line-03: Halting and Resuming the Application When debugging, you may resume application and enter code waiting for some event or staying in infinite loop without any break points defined. In such case, to go back to debugging mode, you can break program execution manually by entering Ctrl+C.
311
To check it delete all breakpoints and enter ``c`` to resume application. Then enter Ctrl+C. Application will be halted at some random point and LED will stop blinking. Debugger will print the following:: (gdb) c Continuing. ^CTarget halted. PRO_CPU: PC=0x400D0C00 APP_CPU: PC=0x400D0C00 (active) [New Thread 1073433352] Program received signal SIGINT, Interrupt. [Switching to Thread 1073413512] 0x400d0c00 in esp_vApplicationIdleHook () at /home/user-name/esp/esp-idf/components/{IDF_TARGET_PATH_NAME}/./freertos_hooks.c:52 52 asm("waiti 0"); (gdb) In particular case above, the application has been halted in line 52 of code in file ``freertos_hooks.c``. Now you can resume it again by enter ``c`` or do some debugging as discussed below. .. _jtag-debugging-examples-command-line-04: Stepping Through the Code It is also possible to step through the code using ``step`` and ``next`` commands (in short ``s`` and ``n``). The difference is that ``step`` is entering inside subroutines calls, while ``next`` steps over the call, treating it as a single source line.
311
To demonstrate this functionality, using command ``break`` and ``delete`` discussed in previous paragraph, make sure that you have only one breakpoint defined at line ``36`` of ``blink.c``:: (gdb) info break Num Type Disp Enb Address What 3 breakpoint keep y 0x400db704 in blink_task at /home/user-name/esp/blink/main/./blink.c:36 breakpoint already hit 1 time (gdb) Resume program by entering ``c`` and let it halt:: (gdb) c Continuing. Target halted. PRO_CPU: PC=0x400DB754 (active) APP_CPU: PC=0x400D1128 Breakpoint 3, blink_task (pvParameter=0x0) at /home/user-name/esp/blink/main/./blink.c:36 36 gpio_set_level(BLINK_GPIO, 1); (gdb) Then enter ``n`` couple of times to see how debugger is stepping one program line at a time:: (gdb) n Target halted. PRO_CPU: PC=0x400DB756 (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DB758 (active) APP_CPU: PC=0x400D1128 Target halted.
311
PRO_CPU: PC=0x400DC04C (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DB75B (active) APP_CPU: PC=0x400D1128 37 vTaskDelay(1000 / portTICK_PERIOD_MS); (gdb) n Target halted. PRO_CPU: PC=0x400DB75E (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400846FC (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DB761 (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DB746 (active) APP_CPU: PC=0x400D1128 33 gpio_set_level(BLINK_GPIO, 0); (gdb) If you enter ``s`` instead, then debugger will step inside subroutine calls:: (gdb) s Target halted. PRO_CPU: PC=0x400DB748 (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DB74B (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DC04C (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DC04F (active) APP_CPU: PC=0x400D1128 gpio_set_level (gpio_num=GPIO_NUM_4, level=0) at /home/user-name/esp/esp-idf/components/esp_driver_gpio/src/gpio.
311
c:183 183 GPIO_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "GPIO output gpio_num error", ESP_ERR_INVALID_ARG); (gdb) In this particular case debugger stepped inside ``gpio_set_level(BLINK_GPIO, 0)`` and effectively moved to ``gpio.c`` driver code. See :ref:`jtag-debugging-tip-why-next-works-as-step` for potential limitation of using ``next`` command. .. _jtag-debugging-examples-command-line-05: Checking and Setting Memory Displaying the contents of memory is done with command ``x``. With additional parameters you may vary the format and count of memory locations displayed. Run ``help x`` to see more details. Companion command to ``x`` is ``set`` that let you write values to the memory. We will demonstrate how ``x`` and ``set`` work by reading from and writing to the memory location ``0x3FF44004`` labeled as ``GPIO_OUT_REG`` used to set and clear individual GPIO's. For more information, see *{IDF_TARGET_NAME} Technical Reference Manual* > *IO MUX and GPIO Matrix (GPIO, IO_MUX)* [`PDF `__].
311
Being in the same ``blink.c`` project as before, set two breakpoints right after ``gpio_set_level`` instruction. Enter two times ``c`` to get to the break point followed by ``x /1wx 0x3FF44004`` to display contents of ``GPIO_OUT_REG`` memory location:: (gdb) c Continuing. Target halted. PRO_CPU: PC=0x400DB75E (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DB74E (active) APP_CPU: PC=0x400D1128 Breakpoint 2, blink_task (pvParameter=0x0) at /home/user-name/esp/blink/main/./blink.c:34 34 vTaskDelay(1000 / portTICK_PERIOD_MS); (gdb) x /1wx 0x3FF44004 0x3ff44004: 0x00000000 (gdb) c Continuing. Target halted. PRO_CPU: PC=0x400DB751 (active) APP_CPU: PC=0x400D1128 Target halted. PRO_CPU: PC=0x400DB75B (active) APP_CPU: PC=0x400D1128 Breakpoint 3, blink_task (pvParameter=0x0) at /home/user-name/esp/blink/main/./blink.c:37 37 vTaskDelay(1000 / portTICK_PERIOD_MS); (gdb) x /1wx 0x3FF44004 0x3ff44004: 0x00000010 (gdb) If your are blinking LED connected to GPIO4, then you should see fourth bit being flipped each time the LED changes the state:: 0x3ff44004: 0x00000000 .
311
.. 0x3ff44004: 0x00000010 Now, when the LED is off, that corresponds to ``0x3ff44004: 0x00000000`` being displayed, try using ``set`` command to set this bit by writting ``0x00000010`` to the same memory location:: (gdb) x /1wx 0x3FF44004 0x3ff44004: 0x00000000 (gdb) set {unsigned int}0x3FF44004=0x000010 You should see the LED to turn on immediately after entering ``set {unsigned int}0x3FF44004=0x000010`` command. .. _jtag-debugging-examples-command-line-06: Watching and Setting Program Variables A common debugging tasks is checking the value of a program variable as the program runs. To be able to demonstrate this functionality, update file ``blink.c`` by adding a declaration of a global variable ``int i`` above definition of function ``blink_task``. Then add ``i++`` inside ``while(1)`` of this function to get ``i`` incremented on each blink. Exit debugger, so it is not confused with new code, build and flash the code to the ESP and restart debugger. There is no need to restart OpenOCD.
311
Once application is halted, enter the command ``watch i``:: (gdb) watch i Hardware watchpoint 2: i (gdb) This will insert so called "watchpoint" in each place of code where variable ``i`` is being modified. Now enter ``continue`` to resume the application and observe it being halted:: (gdb) c Continuing. Target halted. PRO_CPU: PC=0x400DB751 (active) APP_CPU: PC=0x400D0811 [New Thread 1073432196] Program received signal SIGTRAP, Trace/breakpoint trap. [Switching to Thread 1073432196] 0x400db751 in blink_task (pvParameter=0x0) at /home/user-name/esp/blink/main/./blink.c:33 33 i++; (gdb) Resume application couple more times so ``i`` gets incremented. Now you can enter ``print i`` (in short ``p i``) to check the current value of ``i``:: (gdb) p i $1 = 3 (gdb) To modify the value of ``i`` use ``set`` command as below (you can then print it out to check if it has been indeed changed):: (gdb) set var i = 0 (gdb) p i $3 = 0 (gdb) You may have up to two watchpoints, see :ref:`jtag-debugging-tip-breakpoints`.
311
.. _jtag-debugging-examples-command-line-07: Setting Conditional Breakpoints Here comes more interesting part. You may set a breakpoint to halt the program execution, if certain condition is satisfied. Delete existing breakpoints and try this:: (gdb) break blink.c:34 if (i == 2) Breakpoint 3 at 0x400db753: file /home/user-name/esp/blink/main/./blink.c, line 34. (gdb) Above command sets conditional breakpoint to halt program execution in line ``34`` of ``blink.c`` if ``i == 2``. If current value of ``i`` is less than ``2`` and program is resumed, it will blink LED in a loop until condition ``i == 2`` gets true and then finally halt:: (gdb) set var i = 0 (gdb) c Continuing. Target halted. PRO_CPU: PC=0x400DB755 (active) APP_CPU: PC=0x400D112C Target halted. PRO_CPU: PC=0x400DB753 (active) APP_CPU: PC=0x400D112C Target halted. PRO_CPU: PC=0x400DB755 (active) APP_CPU: PC=0x400D112C Target halted. PRO_CPU: PC=0x400DB753 (active) APP_CPU: PC=0x400D112C Breakpoint 3, blink_task (pvParameter=0x0) at /home/user-name/esp/blink/main/.
311
/blink.c:34 34 gpio_set_level(BLINK_GPIO, 0); (gdb) .. _jtag-debugging-examples-command-line-08: Debugging FreeRTOS Objects This part might be interesting when you are debugging FreeRTOS tasks interactions. Users that need to use the FreeRTOS task interactions can use the GDB ``freertos`` command. The ``freertos`` command is not native to GDB and comes from the `freertos-gdb `_ Python extension module. The ``freertos`` command contains a series of sub-commands as demonstrated in the code snippet:: (gdb) freertos "freertos" must be followed by the name of a subcommand. List of freertos subcommands: freertos queue -- Generate a print out of the current queues info. freertos semaphore -- Generate a print out of the current semaphores info. freertos task -- Generate a print out of the current tasks and their states. freertos timer -- Generate a print out of the current timers info. For a more detailed description of this extension, please refer to https://pypi.
311
org/project/freertos-gdb. .. note:: The freertos-gdb Python module is included as a Python package requirement by ESP-IDF, thus should be automatically installed (see :ref:`get-started-set-up-tools` for more details). The FreeRTOS extension automatically loads in case GDB is executed with command via ``idf.py gdb``. Otherwise, the module could be enabled via the ``python import freertos_gdb`` command inside GDB. Users only need to have Python 3.6 (or above) that contains a Python shared library. Obtaining Help on Commands Commands presented so for should provide are very basis and intended to let you quickly get started with JTAG debugging. Check help what are the other commands at you disposal. To obtain help on syntax and functionality of particular command, being at ``(gdb)`` prompt type ``help`` and command name:: (gdb) help next Step program, proceeding through subroutine calls. Usage: next [N] Unlike "step", if the current source line calls a subroutine, this command does not enter the subroutine, but instead steps over the call, in effect treating it as a single source line.
311
(gdb) By typing just ``help``, you will get top level list of command classes, to aid you drilling down to more details. Optionally refer to available GDB cheat sheets, for instance https://darkdust.net/files/GDB%20Cheat%20Sheet.pdf. Good to have as a reference (even if not all commands are applicable in an embedded environment). Ending Debugger Session To quit debugger enter ``q``:: (gdb) q A debugging session is active. Inferior 1 [Remote target] will be detached. Quit anyway? (y or n) y Detaching from program: /home/user-name/esp/blink/build/blink.elf, Remote target Ending remote debugging. user-name@computer-name:~/esp/blink$
311
Tips and Quirks This section provides collection of all tips and quirks referred to from various parts of this guide. .. _jtag-debugging-tip-breakpoints: Breakpoints and Watchpoints Available {IDF_TARGET_NAME} debugger supports {IDF_TARGET_SOC_CPU_BREAKPOINTS_NUM} hardware implemented breakpoints and 64 software ones. Hardware breakpoints are implemented by {IDF_TARGET_NAME} chip's logic and can be set anywhere in the code: either in flash or IRAM program's regions. Additionally there are 2 types of software breakpoints implemented by OpenOCD: flash (up to 32) and IRAM (up to 32) breakpoints. Currently GDB can not set software breakpoints in flash. So until this limitation is removed those breakpoints have to be emulated by OpenOCD as hardware ones (see :ref:`below ` for details). {IDF_TARGET_NAME} also supports {IDF_TARGET_SOC_CPU_WATCHPOINTS_NUM} watchpoints, so {IDF_TARGET_SOC_CPU_WATCHPOINTS_NUM} variables can be watched for change or read by the GDB command ``watch myVariable``.
312
Note that menuconfig option :ref:`CONFIG_FREERTOS_WATCHPOINT_END_OF_STACK` uses the last watchpoint and will not provide expected results, if you also try to use it within OpenOCD/GDB. See menuconfig's help for detailed description. .. _jtag-debugging-tip-where-breakpoints: What Else Should I Know About Breakpoints? Emulating part of hardware breakpoints using software flash ones means that the GDB command ``hb myFunction`` which is invoked for function in flash will use pure hardware breakpoint if it is avalable otherwise one of the 32 software flash breakpoints is used. The same rule applies to ``b myFunction``-like commands. In this case GDB will decide what type of breakpoint to set itself. If ``myFunction`` is resided in writable region (IRAM) software IRAM breakpoint will be used otherwise hardware or software flash breakpoint is used as it is done for ``hb`` command. .. _jtag-debugging-tip-flash-mappings: Flash Mappings vs SW Flash Breakpoints In order to set/clear software breakpoints in flash, OpenOCD needs to know their flash addresses.
312
To accomplish conversion from the {IDF_TARGET_NAME} address space to the flash one, OpenOCD uses mappings of program's code regions resided in flash. Those mappings are kept in the image header which is prepended to program binary data (code and data segments) and is specific to every application image written to the flash. So to support software flash breakpoints OpenOCD should know where application image under debugging is resided in the flash. By default OpenOCD reads partition table at 0x8000 and uses mappings from the first found application image, but there can be the cases when it will not work, e.g., partition table is not at standard flash location or even there can be multiple images: one factory and two OTA and you may want to debbug any of them. To cover all possible debugging scenarios OpenOCD supports special command which can be used to set arbitrary location of application image to debug. The command has the following format: ``esp appimage_offset `` Offset should be in hex format.
312
To reset to the default behaviour you can specify ``-1`` as offset. .. note:: Since GDB requests memory map from OpenOCD only once when connecting to it, this command should be specified in one of the TCL configuration files, or passed to OpenOCD via its command line. In the latter case command line should look like below: .. highlight:: bash .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: run-openocd-appimage-offset :end-before: --- Another option is to execute that command via OpenOCD telnet session and then connect GDB, but it seems to be less handy. .. _jtag-debugging-tip-why-next-works-as-step: Why Stepping with "next" Does Not Bypass Subroutine Calls? When stepping through the code with ``next`` command, GDB is internally setting a breakpoint ahead in the code to bypass the subroutine calls. If all {IDF_TARGET_SOC_CPU_BREAKPOINTS_NUM} breakpoints are already set, this functionality will not work. If this is the case, delete breakpoints to have one "spare".
312
With all breakpoints already used, stepping through the code with ``next`` command will work as like with ``step`` command and debugger will step inside subroutine calls. .. _jtag-debugging-tip-code-options: Support Options for OpenOCD at Compile Time ESP-IDF has some support options for OpenOCD debugging which can be set at compile time: Please see the :ref:`project configuration menu ` menu for more details on setting compile-time options. .. _jtag-debugging-tip-freertos-support: FreeRTOS Support OpenOCD has explicit support for the ESP-IDF FreeRTOS. GDB can see FreeRTOS tasks as threads. Viewing them all can be done using the GDB ``i threads`` command, changing to a certain task is done with ``thread n``, with ``n`` being the number of the thread. FreeRTOS detection can be disabled in target's configuration. For more details see :ref:`jtag-debugging-tip-openocd-configure-target`. GDB has a Python extension for FreeRTOS support. ESP-IDF automatically loads this module into GDB with the ``idf.
312
py gdb`` command when the system requirements are met. See more details in :ref:`jtag-debugging-examples-command-line-08`. .. only:: esp32 .. _jtag-debugging-tip-code-flash-voltage: Why to Set SPI Flash Voltage in OpenOCD Configuration? The MTDI pin of ESP32, being among four pins used for JTAG communication, is also one of ESP32's bootstrapping pins. On power up ESP32 is sampling binary level on MTDI to set it's internal voltage regulator used to supply power to external SPI flash chip. If binary level on MDTI pin on power up is low, the voltage regulator is set to deliver 3.3 V, if it is high, then the voltage is set to 1.8 V. The MTDI pin should have a pull-up or may rely on internal weak pull down resistor (see `ESP32 Series Datasheet `_ for details), depending on the type of SPI chip used. Once JTAG is connected, it overrides the pull-up or pull-down resistor that is supposed to do the bootstrapping. To handle this issue OpenOCD's board configuration file (e.
312
g. ``board\esp32-wrover-kit-3.3v.cfg`` for ESP-WROVER-KIT board) provides ``ESP32_FLASH_VOLTAGE`` parameter to set the idle state of the ``TDO`` line to a specified binary level, therefore reducing the chance of a bad bootup of application due to incorrect flash voltage. Check specification of ESP32 module connected to JTAG, what is the power supply voltage of SPI flash chip. Then set ``ESP32_FLASH_VOLTAGE`` accordingly. Most WROOM modules use 3.3 V flash. WROVER earlier than ESP32-WROVER-B use 1.8 V flash, while ESP32-WROVER-B and -E modules use 3.3 V flash. .. _jtag-debugging-tip-optimize-jtag-speed: .. only:: not esp32 .. _jtag-debugging-tip-optimize-jtag-speed: Optimize JTAG Speed In order to achieve higher data rates and minimize number of dropped packets it is recommended to optimize setting of JTAG clock frequency, so it is at maximum and still provides stable operation of JTAG. To do so use the following tips. .. _jtag-debugging-tip-debugger-startup-commands: What Is the Meaning of Debugger's Startup Commands?
312
On startup, debugger is issuing sequence of commands to reset the chip and halt it at specific line of code. This sequence (shown below) is user defined to pick up at most convenient/appropriate line and start debugging. .. _jtag-debugging-tip-openocd-configure-target: Configuration of OpenOCD for Specific Target There are several kinds of OpenOCD configuration files (``*.cfg``). All configuration files are located in subdirectories of ``share/openocd/scripts`` directory of OpenOCD distribution (or ``tcl/scripts`` directory of the source repository). For the purposes of this guide, the most important ones are ``board``, ``interface`` and ``target``. The following configuration files are available for {IDF_TARGET_NAME}: .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: openocd-cfg-files :end-before: --- If you are using one of the boards which have a pre-defined configuration file, you only need to pass one ``-f`` argument to OpenOCD, specifying that file. If you are using a board not listed here, you need to specify both the interface configuration file and target configuration file.
312
Custom Configuration Files """""""""""""""""""""""""" OpenOCD configuration files are written in TCL, and include a variety of choices for customization and scripting. This can be useful for non-standard debugging situations. Please refer to `OpenOCD Manual`_ for the TCL scripting reference. .. _jtag-debugging-tip-openocd-config-vars: OpenOCD Configuration Variables """"""""""""""""""""""""""""""" The following variables can be optionally set before including the ESP-specific target configuration file. This can be done either in a custom configuration file, or from the command line. The syntax for setting a variable in TCL is: .. code-block:: tcl set VARIABLE_NAME value To set a variable from the command line (replace the name of .cfg file with the correct file for your board): .. code-block:: bash openocd -c 'set VARIABLE_NAME value' -f board/esp-xxxxx-kit.cfg It is important to set the variable before including the ESP-specific configuration file, otherwise the variable will not have effect.
312
You can set multiple variables by repeating the ``-c`` option. .. list-table:: Common ESP-related OpenOCD variables :widths: 25 75 :header-rows: 1 - Description - Set to ``none`` to disable RTOS support. In this case, thread list will not be available in GDB. Can be useful when debugging FreeRTOS itself, and stepping through the scheduler code. - Set to ``0`` to disable Flash breakpoints support. - Set to the path (on the host) which will be the default directory for semihosting functions. .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: openocd-target-specific-config-vars :end-before: --- .. _jtag-debugging-tip-reset-by-debugger: How Debugger Resets {IDF_TARGET_NAME}? The board can be reset by entering ``mon reset`` or ``mon reset halt`` into GDB. .. _jtag-debugging-tip-jtag-pins-reconfigured: Can JTAG Pins Be Used for Other Purposes? .. only:: SOC_USB_SERIAL_JTAG_SUPPORTED {IDF_TARGET_NAME} contains a USB Serial/JTAG Controller which can be used for debugging.
312
By default, {IDF_TARGET_NAME} JTAG interface is connected to the built-in USB SERIAL/JTAG peripheral. For details, please refer to :doc:`Configure {IDF_TARGET_NAME} built-in JTAG Interface `. When you use USB Serial/JTAG Controller for debugging, |jtag-gpio-list| can be used for other purposes. However, if you switch the USB JTAG interface to the GPIOs by burning eFuses, |jtag-gpio-list| can be used for JTAG debugging. When they perform this function, they cannot be used for other purposes. Operation of JTAG may be disturbed, if some other hardware is connected to JTAG pins besides {IDF_TARGET_NAME} module and JTAG adapter. {IDF_TARGET_NAME} JTAG is using the following pins: .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: jtag-pins :end-before: --- JTAG communication will likely fail, if configuration of JTAG pins is changed by a user application. If OpenOCD initializes correctly (detects all the CPU cores in the SOC), but loses sync and spews out a lot of DTR/DIR errors when the program is running, it is likely that the application reconfigures the JTAG pins to something else, or the user forgot to connect Vtar to a JTAG adapter that requires it.
312
.. only:: esp32 .. highlight:: none Below is an excerpt from series of errors on the dual-core {IDF_TARGET_NAME} reported by GDB after the application stepped into the code that reconfigured MTDO pin to be an input:: cpu0: xtensa_resume (line 431): DSR (FFFFFFFF) indicates target still busy! cpu0: xtensa_resume (line 431): DSR (FFFFFFFF) indicates DIR instruction generated an exception! cpu0: xtensa_resume (line 431): DSR (FFFFFFFF) indicates DIR instruction generated an overrun! cpu1: xtensa_resume (line 431): DSR (FFFFFFFF) indicates target still busy! cpu1: xtensa_resume (line 431): DSR (FFFFFFFF) indicates DIR instruction generated an exception! cpu1: xtensa_resume (line 431): DSR (FFFFFFFF) indicates DIR instruction generated an overrun! .. _jtag-debugging-security-features: JTAG with Flash Encryption or Secure Boot By default, enabling Flash Encryption and/or Secure Boot will disable JTAG debugging. On first boot, the bootloader will burn an eFuse bit to permanently disable JTAG at the same time it enables the other features.
312
.. only:: SOC_HMAC_SUPPORTED Please note that once JTAG is permanently disabled, it cannot be re-enabled for JTAG access. However, we do have the option of disabling JTAG softly. For more details on soft disabling and re-enabling soft-disabled JTAG, please refer to the :ref:`hmac_for_enabling_jtag`. The project configuration option :ref:`CONFIG_SECURE_BOOT_ALLOW_JTAG` will keep JTAG enabled at this time, removing all physical security but allowing debugging. (Although the name suggests Secure Boot, this option can be applied even when only Flash Encryption is enabled). However, OpenOCD may attempt to automatically read and write the flash in order to set :ref:`software breakpoints `. This has two problems: - Software breakpoints are incompatible with Flash Encryption, OpenOCD currently has no support for encrypting or decrypting flash contents. - If Secure Boot is enabled, setting a software breakpoint will change the digest of a signed app and make the signature invalid. This means if a software breakpoint is set and then a reset occurs, the signature verification will fail on boot.
312
To disable software breakpoints while using JTAG, add an extra argument ``-c 'set ESP_FLASH_SIZE 0'`` to the start of the OpenOCD command line, see :ref:`jtag-debugging-tip-openocd-config-vars`. .. note:: For the same reason, the ESP-IDF app may fail bootloader verification of app signatures, when this option is enabled and a software breakpoint is set. .. only:: esp32 JTAG and ESP32-WROOM-32 AT Firmware Compatibility Issue The ESP32-WROOM series of modules come pre-flashed with AT firmware. This firmware configures the pins GPIO12 to GPIO15 as SPI slave interface, which makes using JTAG impossible. To make JTAG available, build new firmware that is not using pins GPIO12 to GPIO15 dedicated to JTAG communication. After that, flash the firmware onto your module. See also :ref:`jtag-debugging-tip-jtag-pins-reconfigured`. .. _jtag-debugging-tip-reporting-issues: Reporting Issues with OpenOCD/GDB In case you encounter a problem with OpenOCD or GDB programs itself and do not find a solution searching available resources on the web, open an issue in the OpenOCD issue tracker under https://github.
312
com/espressif/openocd-esp32/issues. a. JTAG adapter type, and the chip/module being debugged. b. Release of ESP-IDF used to compile and load application that is being debugged. c. Details of OS used for debugging. d. Is OS running natively on a PC or on a virtual machine? .. highlight:: bash OpenOCD: .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: run-openocd-d3 :end-before: --- Logging to a file this way will prevent information displayed on the terminal. This may be a good thing taken amount of information provided, when increased debug level ``-d3`` is set. If you still like to see the log on the screen, then use another command instead: .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: run-openocd-d3-tee :end-before: --- Debugger: .. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: run-gdb-remotelog :end-before: --- Optionally add command ``remotelogfile gdb_log.txt`` to the ``gdbinit`` file.
312
.. _OpenOCD Manual: https://openocd.org/doc/html/index.html
312
Building OpenOCD from Sources for Windows .. note:: This document outlines how to build a binary of OpenOCD from its source files instead of downloading the pre-built binary. For a quick setup, users can download a pre-built binary of OpenOCD from `Espressif GitHub `_ instead of compiling it themselves (see :ref:`jtag-debugging-setup-openocd` for more details). .. note:: All code snippets in this document are assumed to be running in an MSYS2 shell with the MINGW32 subsystem. Install Dependencies Install packages that are required to compile OpenOCD: .. code-block:: bash pacman -S --noconfirm --needed autoconf automake git make \ mingw-w64-i686-gcc \ mingw-w64-i686-toolchain \ mingw-w64-i686-libtool \ mingw-w64-i686-pkg-config \ mingw-w64-cross-winpthreads-git \ p7zip Download Sources of OpenOCD The sources for the {IDF_TARGET_NAME}-enabled variant of OpenOCD are available from Espressif's GitHub under https://github.com/espressif/openocd-esp32.
313
These source files can be pulled via Git using the following commands: .. code-block:: bash cd ~/esp git clone --recursive https://github.com/espressif/openocd-esp32.git The clone of sources should be now saved in ``~/esp/openocd-esp32`` directory. Downloading libusb The libusb library is also required when building OpenOCD. The following commands will download a particular release of libusb and uncompress it to the current directory. .. code-block:: bash wget https://github.com/libusb/libusb/releases/download/v1.0.22/libusb-1.0.22.7z 7z x -olibusb ./libusb-1.0.22.7z We now need to export the following variables such that the libusb library gets linked into the OpenOCD build. .. code-block:: bash export CPPFLAGS="$CPPFLAGS -I${PWD}/libusb/include/libusb-1.0" export LDFLAGS="$LDFLAGS -L${PWD}/libusb/MinGW32/.libs/dll" Build OpenOCD The following commands will configure OpenOCD then build it. .. code-block:: bash cd ~/esp/openocd-esp32 export CPPFLAGS="$CPPFLAGS -D__USE_MINGW_ANSI_STDIO=1 -Wno-error"; export CFLAGS="$CFLAGS -Wno-error" .
313
/bootstrap ./configure --disable-doxygen-pdf --enable-ftdi --enable-jlink --enable-ulink --build=i686-w64-mingw32 --host=i686-w64-mingw32 make cp ../libusb/MinGW32/dll/libusb-1.0.dll ./src cp /opt/i686-w64-mingw32/bin/libwinpthread-1.dll ./src Once the build is completed, the OpenOCD binary will be placed in ``~/esp/openocd-esp32/src/``. You can then optionally call ``make install``. This will copy the OpenOCD binary to a user specified location. - This location can be specified when OpenOCD is configured, or by setting ``export DESTDIR="/custom/install/dir"`` before calling ``make install``. - If you have an existing OpenOCD (from e.g., another development platform), you may want to skip this call as your existing OpenOCD may get overwritten. .. note:: Once ``make`` process is successfully completed, the executable of OpenOCD will be saved in ``~/esp/openocd-esp32/src`` directory. Full Listing For greater convenience, all of commands called throughout the OpenOCD build process have been listed in the code snippet below.
313
Users can copy this code snippet into a shell script then execute it: .. code-block:: bash pacman -S --noconfirm --needed autoconf automake git make mingw-w64-i686-gcc mingw-w64-i686-toolchain mingw-w64-i686-libtool mingw-w64-i686-pkg-config mingw-w64-cross-winpthreads-git p7zip cd ~/esp git clone --recursive https://github.com/espressif/openocd-esp32.git wget https://github.com/libusb/libusb/releases/download/v1.0.22/libusb-1.0.22.7z 7z x -olibusb ./libusb-1.0.22.7z export CPPFLAGS="$CPPFLAGS -I${PWD}/libusb/include/libusb-1.0"; export LDFLAGS="$LDFLAGS -L${PWD}/libusb/MinGW32/.libs/dll" export CPPFLAGS="$CPPFLAGS -D__USE_MINGW_ANSI_STDIO=1 -Wno-error"; export CFLAGS="$CFLAGS -Wno-error" cd ~/esp/openocd-esp32 ./bootstrap ./configure --disable-doxygen-pdf --enable-ftdi --enable-jlink --enable-ulink --build=i686-w64-mingw32 --host=i686-w64-mingw32 make cp ../libusb/MinGW32/dll/libusb-1.0.dll ./src cp /opt/i686-w64-mingw32/bin/libwinpthread-1.
313
dll ./src # # optional # export DESTDIR="$PWD" # make install # cp ./src/libusb-1.0.dll $DESTDIR/mingw32/bin # cp ./src/libwinpthread-1.dll $DESTDIR/mingw32/bin Next Steps To carry on with debugging environment setup, proceed to section :ref:`jtag-debugging-configuring-target`.
313
.. include:: {IDF_TARGET_PATH_NAME}.inc :start-after: devkit-defs :end-before: --- Configure {IDF_TARGET_NAME} Built-in JTAG Interface {IDF_TARGET_JTAG_PIN_Dneg:default="Not Updated!", esp32c3="GPIO18", esp32c6="GPIO12", esp32s3="GPIO19", esp32h2="GPIO26"} {IDF_TARGET_JTAG_PIN_Dpos:default="Not Updated!", esp32c3="GPIO19", esp32c6="GPIO13", esp32s3="GPIO20", esp32h2="GPIO27"} {IDF_TARGET_NAME} has a built-in JTAG circuitry and can be debugged without any additional chip. Only an USB cable connected to the D+/D- pins is necessary. The necessary connections are shown in the following section. Configure Hardware .. list-table:: {IDF_TARGET_NAME} pins and USB signals :widths: 25 75 :header-rows: 1 - USB Signal - D- - D+ - V_BUS - Ground Please verify that the {IDF_TARGET_NAME} pins used for USB communication are not connected to some other HW that may disturb the JTAG operation. Configure USB Drivers JTAG communication should work on all supported platforms.
314
Windows users might get `LIBUSB_ERROR_NOT_FOUND` errors. Please use version 2.8 (or newer) of the :ref:`get-started-windows-tools-installer` and select the driver "Espressif - WinUSB support for JTAG (ESP32-C3/S3)" in order to resolve this issue. If you do not want to re-run the installer then the same can be achieved with `idf-env `_ by running the following command from PowerShell:: Invoke-WebRequest 'https://dl.espressif.com/dl/idf-env/idf-env.exe' -OutFile .\idf-env.exe; .\idf-env.exe driver install --espressif On Linux adding OpenOCD udev rules is required and is done by placing the following `udev rules file `_ in the ``/etc/udev/rules.d`` folder.
314
Building OpenOCD from Sources for MacOS The following instructions are alternative to downloading binary OpenOCD from `Espressif GitHub `_. To quickly setup the binary OpenOCD, instead of compiling it yourself, backup and proceed to section :ref:`jtag-debugging-setup-openocd`. .. highlight:: bash Download Sources of OpenOCD The sources for the {IDF_TARGET_NAME}-enabled variant of OpenOCD are available from Espressif GitHub under https://github.com/espressif/openocd-esp32. To download the sources, use the following commands:: cd ~/esp git clone --recursive https://github.com/espressif/openocd-esp32.git The clone of sources should be now saved in ``~/esp/openocd-esp32`` directory. Install Dependencies Install packages that are required to compile OpenOCD using Homebrew:: brew install automake libtool libusb wget gcc@4.9 pkg-config Build OpenOCD Proceed with configuring and building OpenOCD:: cd ~/esp/openocd-esp32 ./bootstrap ./configure make Optionally you can add ``sudo make install`` step at the end.
315
Skip it, if you have an existing OpenOCD (from e.g., another development platform), as it may get overwritten. .. note:: brew install texinfo export PATH=/usr/local/opt/texinfo/bin:$PATH Once ``make`` process is successfully completed, the executable of OpenOCD will be saved in ``~/esp/openocd-esp32/src/openocd`` directory. Next Steps To carry on with debugging environment setup, proceed to section :ref:`jtag-debugging-configuring-target`.
315