# Zephyr Lab RT1060 Debug ## Objectives In this lab, you will learn: - How to build and debug a Zephyr application in VS Code - How to use various debugging tools and controls - How to enable Zephyr thread awareness using Kconfig symbols - How to view thread details in the debugger - How to enable and use the Peripheral view ## Debug Lab In this lab, we will go through detailed steps to run the Zephyr *Synchronization* sample application on the RT1060 EVKB. The Debug lab includes steps to build/debug the application and details on debug tools/controls in VS Code. It also includes steps to enable thread awareness by setting up Kconfig symbols for the application example. We will take a closer look at key features like Thread status, Stack usage, Call Stack, Thread structure, and more. ### 1. Import *Synchronization* application example Follow the steps to import the example, similar to the steps in the [Zephyr Lab RT1060 Hello World](./Zephyr-Lab-RT1060-Hello-World). 1. To import the *synchronization* application from the Zephyr Repository, click **Import Example from Repository** in the Quickstart Panel. ![Import Example](./pictures/training-zephyr-install-import-example.png)
2. Select the following board settings to import the example: - For selecting the board, type **RT1060** to find the board **NXP MIMXRT1060-EVKB**. Make sure that the board ends with ‘B’ for the EVK**B**. - For selecting the example, type **synchronization** to find the application **samples/synchronization** - For application type, select **Repository Application**. - Click **Import** and the example should be added to the PROJECTS view. ![Synchronization Zephyr](./pictures/training-zephyr-rt1060-debug-import-sync.png)
### 2. Enable thread awareness and build the example To enable thread awareness for debugging, we need to add specific Kconfig symbols to the application example. These steps will modify the *prj.conf* file and verify the generated *.config* file, similar to the [Zephyr Lab RT1060 Kconfig](./Zephyr-Lab-RT1060-Kconfig) guide. 3. Add the following Kconfig symbols to the **prj.conf** file and save the file (Ctrl+S): ```python CONFIG_DEBUG_THREAD_INFO=y CONFIG_INIT_STACKS=y CONFIG_THREAD_STACK_INFO=y ``` ![CONFIG Values](./pictures/training-zephyr-3-2-3.png)
4. Build the project by clicking the **Build Selected** icon. ![Build Selected](./pictures/training-zephyr-3-2-4a.png)
After the build, the Terminal window displays the memory usage (or compiler errors if any). ![Build Output Results](./pictures/training-zephyr-rt1060-debug-sync-built.png)
To enable the Peripheral view in VS Code for debugging, we need to edit the debugger `launch.json` file, and add the `svdPath` pointing to the SVD file. The SVD files are located in a repository included with the MCUXpresso SDK. Please refer to the [Zephyr Lab Installation and Preparation](./Zephyr-Lab-Installation-and-Preparation) guide and follow the steps to import the MCUXpresso SDK repository. 5. Switch to the **Explorer view** (Ctrl+Shift+E) and open the file **synchronization > .vscode > launch.json** Add this line to the `launch.json` file. Note, be sure to change the path to your SVD file, and save the file (Ctrl+S): ```json "svdPath": "C:/Users/NXP/mcux-sdk/mcux_sdk_v25_03_00/svd/MIMXRT1062/MIMXRT1062.xml", ``` ![SVD Path Added](./pictures/training-zephyr-rt1060-debug-svd-file.png)
### 3. Exploring Debug Tools and Controls After building the application and enabling thread awareness, let’s explore the debug tools and controls in the Run and Debug view. 6. If not already connected to the board, connect the micro-USB cable to J1, to power and debug the EVK. Connect the Serial monitor to the EVK’s COM port and click **Start Monitoring**. ![Serical Monitor](./pictures/training-zephyr-rt1060-start-serial-monitor.png)
7. Click the play icon to **Debug** the Synchronization application. ![Debug Project](./pictures/training-zephyr-3-3-7.png)
8. The following debug tools/controls are enabled in the Run and Debug view: 1. **Debug controls**: Resume/Pause, Step over, Step into, Restart, Stop 2. **Variables**: Locals and Registers 3. **Watch**: Add expressions to monitor 4. **Call Stack**: List of active subroutines in the program 5. **Breakpoints**: Add, toggle, and view breakpoints in the code ![Debug Perspective](./pictures/training-zephyr-3-3-8.png)
9. To continue with the debug execution click **Resume**. The application prints hello world (both threads) in the Serial Monitor view. ![Serial Monitor Output](./pictures/training-zephyr-rt1060-debug-sync-prints.png)
### 4. Debugging with Thread awareness – RTOS Details The synchronization application has two threads (threadA and threadB) taking turns printing a message to the console. The example uses semaphores to synchronize the two tasks. 10. While the debug session is active, go to the **Run and Debug** view, and open the source file with the main() subroutine in the editor. **Add a breakpoint** by double-clicking on the left side of line 48. A red dot next to the line and an entry in the Breakpoints window confirms the addition of the breakpoint. ![Add Breakpoint](./pictures/training-zephyr-3-4-10.png)
11. The debug session will halt at the helloLoop(), and the debug controls can be used to resume or step through the code. The synchronization application will print to the terminal after **clicking** **Resume** a couple of times. When the debugger halts at the breakpoint, the application threads and details are displayed in the RTOS details view. Click the **RTOS DETAILS** tab to see the thread details. These details include the status of each thread along with its priorities, stack pointer, and stack usage. **Pause** the debug session to notice the changes in the status of each thread. ![RTOS DETAILS](./pictures/training-zephyr-rt1060-debug-sync-threads.png)
12. Along with the RTOS DETAILS view, the **Call Stack** window provides more information about the threads in the application: - Running thread: ![Running Thread](./pictures/Training-zephyr-debug-sync-running-thread.png)
- Pending thread: ![Pending Thread](./pictures/Training-zephyr-debug-sync-pending-thread.png)
- Idle Thread: ![Idle Thread](./pictures/Training-zephyr-debug-sync-idle-thread.png)
13. The **Watch** window and the **Variables** window can be used to add local variables and CPU registers that need to be monitored. ![Watch and Variables](./pictures/training-zephyr-rt1060-debug-watch-variables.png)
14. The **PERIPHERALS** View will be added to the Debug view when the debugger is connected and the `svdPath` is added to the `launch.json` file. ![Peripheral View](./pictures/training-zephyr-rt1060-debug-peripheral-view-default.png)
Registers and bitfields can be viewed for the different peripherals (eg. LPUART1). ![Peripheral View LPUART](./pictures/training-zephyr-rt1060-debug-peripheral-view-lpuart1.png)
### 5. Clean up after lab This lab is completed. But the following steps will clean up the VS Code workspace: 15. Click **Stop** to end the debug session. ![Stop Debugger](./pictures/training-zephyr-stop-debugger.png)
16. Close all Editor tabs. Right-Click on a tab, and select **Close All**. 17. If this is the last lab and you are done using the board, you should disconnect the Serial Monitor. Find the Serial Monitor view, and click **Stop Monitoring**. ![Stop Serial Monitor](./pictures/training-zephyr-rt1060-stop-serial-monitor.png)
# Additional Resources [VS Code Debugging](https://code.visualstudio.com/docs/editor/debugging) [Peripherals View](https://github.com/nxp-mcuxpresso/vscode-for-mcux/wiki/Peripherals) [RTOS Details View](https://github.com/nxp-mcuxpresso/vscode-for-mcux/wiki/RTOS-Details) --- > Lab completed. Return to the RT1060 Zephyr Labs Overview [Training Zephyr Getting Started RT1060](./Training-Getting-Started-RT1060)