Difference between revisions of "Delayed Program Start in Debug"

From SEGGER Wiki
Jump to: navigation, search
(Solution)
(SETUP)
Line 31: Line 31:
   
 
The project can be easily ported to any other Cortex-M based MCU.<br>
 
The project can be easily ported to any other Cortex-M based MCU.<br>
===SETUP===
+
===SETUP Cortex-M===
 
*J-Link software: V7.58 (or later)
 
*J-Link software: V7.58 (or later)
 
*Embedded Studio: V5.62 (or later)
 
*Embedded Studio: V5.62 (or later)

Revision as of 17:20, 6 December 2021

After issuing a reset in debug mode, the MCU is expected to halt right before executing the target application. Usually, the J-Link archives this by configuring debug logic of the MCU accordingly. Of course, this assumes that a debug reset does not clear the debug logic. This applies for most MCUs but unfortunately not for all. For MCUs where the debug logic does not survive the reset, the J-Link has no chance to force the MCU to automatically halt after reset.

Problem

After reset, the MCU automatically jumps to the user application and starts executing. The J-Link tries to re-enable the debug logic + halt the MCU as soon as possible in such scenarios but there is no guarantee that this is done fast enough. In case of the application has been already started, any further action may result in unexpected behavior.

Solution

Add a delay loop (e.g. 1 second) in the beginning of the startup code. The idea is to give the J-Link a defined timeframe to re-enable the debug logic + perform the halt request before the actual application code is executed. We recommend to enable this delay loop in the debug configuration of your project, only.

Instrumented Startup Code - Cortex-M

The following instructions needs to be added to the startup code. Please note that the number of the delay loops needs to be adjusted depending on the CPU frequency. In this example, a STM32F4 has been used which runs at 16 MHz after reset.

//
// Execute a ~1 second delay loop
//
#IF DEBUG
        mov r0, 0x300000
Delay_Loop:
        subs r0, #1
        cmp  r0, #0
        bne Delay_Loop
#endif

Instrumented Startup Code - Cortex-A/R

TBD

Instrumented Startup Code - RISC-V

TBD

Example

The following example project was created with the SEGGER Embedded Studio and runs out-of-the-box on SEGGERs Cortex-M Trace Reference Board. It is a simple blinky application which toggles LED0. It makes use of a generic startup code which has been instrumented as described above. Please note that this is a theoretical example because the STM32F4 *does not* clear the debug logic on reset. However, you can see that after a power on reset, it takes ~1 second until the actual application boots. This would be enough time to re-connect + issue a halt request on MCUs where the reset does clear the debug logic.

The project can be easily ported to any other Cortex-M based MCU.

SETUP Cortex-M