2025-09-04 2:59 AM
recently i want to control the SWD function manually by external pin level, in F1 HAL library,there are some defintions to configure this,like above, after call __HAL_AFIO_REMAP_SWJ_DISABLE(),when i connect MCU by jlink debugger,it fails, so it means this definition effect,but after call __HAL_AFIO_REMAP_SWJ_NOJTAG(),debugger still cannot connect the mcu,I try some time,then i find, when enable SWD,I must call __HAL_AFIO_REMAP_SWJ_ENABLE(),why this. i have test on stm32f103c8t6 and stm32f103vet6,both show the same result, thanks.
brief:why i must call __HAL_AFIO_REMAP_SWJ_ENABLE() not only __HAL_AFIO_REMAP_SWJ_NOJTAG() after i disable SWD
Solved! Go to Solution.
2025-09-08 5:28 AM - edited 2025-09-08 5:31 AM
Hi @builderx
The AFIO_MAPR register manages the reassignment of alternate functions on the MCU pins, including the configuration of the SWJ (Serial Wire JTAG) debug ports, which cover both JTAG and SWD. Thus, the pins used for debugging (SWD/JTAG) can be reassigned to other functions through this register.
The remapping functions are as follows:
HAL_AFIO_REMAP_SWJ_ENABLE(): Enables JTAG-DP + SW-DP (all pins)
HAL_AFIO_REMAP_SWJ_NOJTAG(): Disables JTAG, keeps SWD active
HAL_AFIO_REMAP_SWJ_DISABLE(): Disables both JTAG and SWD (all pins freed)
After completely disabling SWJ with the function HAL_AFIO_REMAP_SWJ_DISABLE(), all pins dedicated to debugging are freed for other GPIO uses. Calling this function disconnects the pins from the debug module, so even if you then call HAL_AFIO_REMAP_SWJ_NOJTAG(), it is not enough to restore the SWD connection.
Remapping acts like a switch that must be explicitly enabled or disabled.
To reactivate SWD, you must reassign the pins to the debug function by calling HAL_AFIO_REMAP_SWJ_ENABLE() function. This function resets the internal multiplexer to its default state, reconnecting the SWD and JTAG pins to the debug module.
If you want to disable only JTAG while keeping SWD active, you can then call HAL_AFIO_REMAP_SWJ_NOJTAG() function.
You can refer to the Reference Manual RM0008, specifically section 9.3 Alternate function I/O and debug configuration (AFIO), where you will find a detailed description of AFIO.
Thanks,
ELABI.1
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2025-09-08 5:28 AM - edited 2025-09-08 5:31 AM
Hi @builderx
The AFIO_MAPR register manages the reassignment of alternate functions on the MCU pins, including the configuration of the SWJ (Serial Wire JTAG) debug ports, which cover both JTAG and SWD. Thus, the pins used for debugging (SWD/JTAG) can be reassigned to other functions through this register.
The remapping functions are as follows:
HAL_AFIO_REMAP_SWJ_ENABLE(): Enables JTAG-DP + SW-DP (all pins)
HAL_AFIO_REMAP_SWJ_NOJTAG(): Disables JTAG, keeps SWD active
HAL_AFIO_REMAP_SWJ_DISABLE(): Disables both JTAG and SWD (all pins freed)
After completely disabling SWJ with the function HAL_AFIO_REMAP_SWJ_DISABLE(), all pins dedicated to debugging are freed for other GPIO uses. Calling this function disconnects the pins from the debug module, so even if you then call HAL_AFIO_REMAP_SWJ_NOJTAG(), it is not enough to restore the SWD connection.
Remapping acts like a switch that must be explicitly enabled or disabled.
To reactivate SWD, you must reassign the pins to the debug function by calling HAL_AFIO_REMAP_SWJ_ENABLE() function. This function resets the internal multiplexer to its default state, reconnecting the SWD and JTAG pins to the debug module.
If you want to disable only JTAG while keeping SWD active, you can then call HAL_AFIO_REMAP_SWJ_NOJTAG() function.
You can refer to the Reference Manual RM0008, specifically section 9.3 Alternate function I/O and debug configuration (AFIO), where you will find a detailed description of AFIO.
Thanks,
ELABI.1
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.