Framework - Debug

Created:2018-02-11  Last modified:2018-02-11


  1. Introduction

    Debugging a hardware has two mode, using a software simulator or basing a real hardware. Keil-MDK5 support both of modes.

    Debug with a real device

    Debugging a real device would require the device to support debug protocols. For example, the AVR atmega328p support debugWire interface. STM32F103rb supports SWD (serial wire debugg) interface, JTAG interface.

    In STM32F1, the debug feature is supported by the cortex-M3 core. It is an ARM CoreSight debug technology that combines the JTAG port and SW port together, and internally connect to PPB (Private peripheral bus) AHB-lite bus. The cortex-M3 core supports following debug features:

    Pinouts

    After enable debugging function, the relevant pins cannot be used by application code.

    The interface can be used for debugging and tracing.

  2. SW debug example

    STM32F1 supports Serial Wire debug interface (2 wires: clock + data). The ST-Link and Keil-MDK5 also support SW debug. The nucleo board has a st-link debugger.

    1. Using CubeMX to initialize a project

      Enable the SYS->Serial Wire, generate a MDK5 project

    2. Verfiy SW enabled and JTAG disabled

      Go to stm32f1xx_hal_msp.c -> HAL_MspInit(), see the last statement is "__HAL_AFIO_REMAP_SWJ_NOJTAG();"

      HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
      
          /**NOJTAG: JTAG-DP Disabled and SW-DP Enabled 
          */
        __HAL_AFIO_REMAP_SWJ_NOJTAG();
      
        /* USER CODE BEGIN MspInit 1 */
      
    3. Select the correct debug mode in MDK5

      Options for target -> Debug -> Settings [right side] -> uncheck the "verify code download" and "download to flash"

    4. Write application code, and add breakpoint

      Not every line of C code can be added a breakpoint. For example, DDRB |= 0x01; DDRB |= 0x02; may merge as a single statement in assembly code, then the DDRB |= 0x01 cannot be added a breakpoint.

      Only those lines that precede with deeper gray can be added breakpoints