Terminating an Active Debug Session Using Code (Processor Side)

1 post / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've added code to my SAME70 project to self-set the security bit and reboot.  The code works as expected.  But if I happen to execute this code during a debug session, the debug session freaks out going into a boot loop resetting the processor over and over until I manually disconnect the debugger.  It seems reasonable since the debugger can no longer access the processor in the middle of a session once the processor self-sets the security bit.

 

I'd like to extend my code to be a little smarter by first terminating/disconnecting any active debug sessions, then self-setting the security bit.  This should disconnect the debugger and prevent the debugger boot loop side effect.

 

How can I check via code if a debug session is active?  And how can I terminate that session from the processor side?  Basically please help with the // ***** TBD ***** code below.  Perhaps there's something in CMSIS / SWD library code?

 

static inline void SetSecurityBit(void)
{
// set BYPASS_SECURITY_BIT=1 to prevent the security bit from being self-set
#if BYPASS_SECURITY_BIT
    #ifdef DEBUG_BENCH
        #warning "BYPASS_SECURITY_BIT is enabled, ATSAME70 security bit will NOT be set!!"
    #else
        #warning "BYPASS_SECURITY_BIT is enabled in production mode for debugging purposes only"
        #warning "DO NOT INSTALL THIS FIRMWARE ON PRODUCTION MACHINES!!"
    #endif
#else
    // wait for FRDY before sending any commands
    while (!(EFC->EEFC_FSR & EEFC_FSR_FRDY))
        ;

    // send command to get the GPNVM bits
    EFC->EEFC_FCR = (EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FCMD_GGPB);
    while (!(EFC->EEFC_FSR & EEFC_FSR_FRDY))
        ;

    // set the security bit only if it is clear
    if ((EFC->EEFC_FRR & 0x0001) != 0x0001) {
        // check for and terminate any active debug sessions here
        // ***** TBD ***** 

        // send command to set GPNVM.b0 (Security Bit)
        EFC->EEFC_FCR = (EEFC_FCR_FKEY_PASSWD | EEFC_FCR_FCMD_SGPB | EEFC_FCR_FARG(0));
        while (!(EFC->EEFC_FSR & EEFC_FSR_FRDY))
            ;

        // reboot for the new GPNVM bit settings to take effect
        NVIC_SystemReset(); // never returns
    }
#endif
} // end SetSecurityBit()