I'm facing with a problem regarding debug session.

When I start debug session, after the configuration of USART2 the debug session is closed.

Keil message:

Cannot access target. Shutting down debug session

The shutdown occur with this instruction:


This is my configuration:

  • STM32F103
  • IDE Keil
  • Keil project created with STM32Cube MX
  • Debug Serial Wire with SWCLK, SWDIO, NRST.

Without debug the board works well, but I need the debug to solve some bug.


  • 1
    \$\begingroup\$ Check your pin settings - you might remap one of those SWD pins required for debugging. \$\endgroup\$ – Turbo J Jun 21 '16 at 9:42
  • \$\begingroup\$ @TurboJ: The sw pins can't remaped to any other pin (from data sheet) \$\endgroup\$ – Federico Jun 21 '16 at 10:14
  • \$\begingroup\$ The other way around. A SWD pin remapped to different functionality (USART2 in your case). \$\endgroup\$ – domen Jun 21 '16 at 14:15
  • 1
    \$\begingroup\$ Problem Solved! Thanks to Misagh92 on STM Forum. Replace the macro '__HAL_AFIO_REMAP_USART2_ENABLE()' with 'AFIO->MAPR |= AFIO_MAPR_USART2_REMAP | AFIO_MAPR_SWJ_CFG_JTAGDISABLE;' that's remap the UART2 and enable the SW debug. Only one thing to keep it in mind: regenerating the code with ST CubeMx will remove the fix. \$\endgroup\$ – Federico Jun 24 '16 at 11:36
  • 1
    \$\begingroup\$ Possible duplicate of Timer1 remap cause debug crash on STM32F103 \$\endgroup\$ – SamGibson Jul 30 '18 at 12:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.