Some pins not usable when using ST-Link ?

rolo644u
Posts: 32
Joined: Fri May 06, 2016 10:02 am
Location: Netherlands

Some pins not usable when using ST-Link ?

Post by rolo644u » Thu May 26, 2016 4:59 pm

I'm experiencing a strange effect when using ST-LINK as upload tool. Some I/O pins are not usable (PB3, PB4, PA15, PA12, PA11). I test this with a simple blink sketch, using the standard digitalWrite command. All other pins work fine. Same on all my STM boards (2x Maple Mini and 2x Blue Pill).
Using the bootloader as upload tool the missing pins work, except PA12 and PA11, these are now in use for the USB so left alone.
I can't get my head arround it, why is this ? I was thinking that the same code is flashed in both methods, only using a different transport method.
Are these pins reserved for something else or am I missing something basic info here ?

User avatar
martinayotte
Posts: 1229
Joined: Mon Apr 27, 2015 1:45 pm

Re: Some pins not usable when using ST-Link ?

Post by martinayotte » Thu May 26, 2016 6:12 pm

This is because those pins are JTAG pins.
When using ST-Link, it doesn't do any call to disable SWD and JTAG.
The relevant code is located in STM32F1/system/libmaple/stm32f1/include/series/gpio.h, triggered by define in board.cpp of the variant.

Code: Select all

/**
 * @brief Debug port configuration
 *
 * Used to configure the behavior of JTAG and Serial Wire (SW) debug
 * ports and their associated GPIO pins.
 *
 * @see afio_cfg_debug_ports()
 */
typedef enum afio_debug_cfg {
    /** Full Serial Wire and JTAG debug */
    AFIO_DEBUG_FULL_SWJ          = AFIO_MAPR_SWJ_CFG_FULL_SWJ,
    /** Full Serial Wire and JTAG, but no NJTRST. */
    AFIO_DEBUG_FULL_SWJ_NO_NJRST = AFIO_MAPR_SWJ_CFG_FULL_SWJ_NO_NJRST,
    /** Serial Wire debug only (JTAG-DP disabled, SW-DP enabled) */
    AFIO_DEBUG_SW_ONLY           = AFIO_MAPR_SWJ_CFG_NO_JTAG_SW,
    /** No debug; all JTAG and SW pins are free for use as GPIOs. */
    AFIO_DEBUG_NONE              = AFIO_MAPR_SWJ_CFG_NO_JTAG_NO_SW,
} afio_debug_cfg;

/**
 * @brief Enable or disable the JTAG and SW debug ports.
 * @param config Desired debug port configuration
 * @see afio_debug_cfg  
 */
static inline void afio_cfg_debug_ports(afio_debug_cfg config) {
    __io uint32 *mapr = &AFIO_BASE->MAPR;
    *mapr = (*mapr & ~AFIO_MAPR_SWJ_CFG) | config;
}
As you can see, for other upload method, it is disabling both SWD and JTAG by using AFIO_MAPR_SWJ_CFG.
You can do the same in your sketch, but using AFIO_DEBUG_SW_ONLY instead to still allow SWD but disabling the other JTAG pins.

rolo644u
Posts: 32
Joined: Fri May 06, 2016 10:02 am
Location: Netherlands

Re: Some pins not usable when using ST-Link ?

Post by rolo644u » Thu May 26, 2016 6:30 pm

Thanks ! That explains a lot. Now I used this piece of code in the setup section and the led on PA15 blinks :D

Code: Select all

void setup() {
    afio_cfg_debug_ports(AFIO_DEBUG_NONE);

User avatar
martinayotte
Posts: 1229
Joined: Mon Apr 27, 2015 1:45 pm

Re: Some pins not usable when using ST-Link ?

Post by martinayotte » Thu May 26, 2016 7:27 pm

Beware that with AFIO_DEBUG_NONE you won't be able to debug using ST-Link, but with AFIO_DEBUG_SW_ONLY it is allowed.

rolo644u
Posts: 32
Joined: Fri May 06, 2016 10:02 am
Location: Netherlands

Re: Some pins not usable when using ST-Link ?

Post by rolo644u » Thu May 26, 2016 7:47 pm

Yes, thanks, have to dive into this debug functions later, on my "to learn" list !

stevestrong
Posts: 1735
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Some pins not usable when using ST-Link ?

Post by stevestrong » Sun Jan 15, 2017 2:22 pm

Well, I also got infected by this "PB4 not toggling" virus...

I cannot find the place in the repo where the afio will configure the ports to debug/non-debug.

I am using the blue pill with "Stm32duino bootloader" upload method, and I cannot toggle PB4 unless I call

Code: Select all

afio_cfg_debug_ports(AFIO_DEBUG_NONE);
in setup, just like @rolo644u.

I thought it should be somewhere in boards.cpp but it isn't.
What am I missing?

stevestrong
Posts: 1735
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Some pins not usable when using ST-Link ?

Post by stevestrong » Sun Jan 15, 2017 2:45 pm

Hm, strange, I have found in line 48 of board.cpp where the debug port should be disabled, but it will be not disabled, although the compiler will be called with the appropriate directive:

Code: Select all

Compiling sketch...
"C:\Users\me\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -DDEBUG_LEVEL=DEBUG_NONE -MMD -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DBOARD_generic_stm32f103c -DVECT_TAB_ADDR=0x8002000 -DERROR_LED_PORT=GPIOB -DERROR_LED_PIN=1 -mcpu=cortex-m3 -DF_CPU=72000000L -DARDUINO=10612 -DARDUINO_GENERIC_STM32F103C -DARDUINO_ARCH_STM32F1  -DSERIAL_USB -DGENERIC_BOOTLOADER -DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG=1 -DMCU_STM32F103CB  -mthumb  -march=armv7-m -D__STM32F1__ -DMCU_STM32F103CB  -mthumb  -march=armv7-m -D__STM32F1__ "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple/include" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple/stm32f1/include"                                                           "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple/usb/stm32f1" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple/usb/usb_lib" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\cores\maple" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\variants\generic_stm32f103c" "C:\Users\me\AppData\Local\Temp\arduino_build_226650\sketch\Blink.ino.cpp" -o "C:\Users\me\AppData\Local\Temp\arduino_build_226650\sketch\Blink.ino.cpp.o"

stevestrong
Posts: 1735
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Some pins not usable when using ST-Link ?

Post by stevestrong » Sun Jan 15, 2017 3:04 pm

It's getting weird...

Compiling this code being part of board.cpp:

Code: Select all

void boardInit(void) {
#warning "Initializing board..."
#ifndef CONFIG_MAPLE_MINI_NO_DISABLE_DEBUG
  #warning "Disabling debug port..."
    disableDebugPorts();
#endif
}
gives the following build output:

Code: Select all

...
"C:\Users\me\AppData\Local\Arduino15\packages\arduino\tools\arm-none-eabi-gcc\4.8.3-2014q1/bin/arm-none-eabi-g++" -c -g -Os -DDEBUG_LEVEL=DEBUG_NONE -MMD -ffunction-sections -fdata-sections -nostdlib --param max-inline-insns-single=500 -fno-rtti -fno-exceptions -DBOARD_generic_stm32f103c -DVECT_TAB_ADDR=0x8002000 -DERROR_LED_PORT=GPIOB -DERROR_LED_PIN=1 -mcpu=cortex-m3 -DF_CPU=72000000L -DARDUINO=10612 -DARDUINO_GENERIC_STM32F103C -DARDUINO_ARCH_STM32F1  -DSERIAL_USB -DGENERIC_BOOTLOADER -DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG=1 -DMCU_STM32F103CB  -mthumb  -march=armv7-m -D__STM32F1__ -DMCU_STM32F103CB  -mthumb  -march=armv7-m -D__STM32F1__ "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple/include" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple/stm32f1/include"                                                           "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple/usb/stm32f1" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\system/libmaple/usb/usb_lib" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\cores\maple" "-IC:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\variants\generic_stm32f103c" "C:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\variants\generic_stm32f103c\board.cpp" -o "C:\Users\me\AppData\Local\Temp\arduino_build_226650\core\board.cpp.o"
C:\Users\me\Documents\Arduino\hardware\Arduino_STM32\STM32F1\variants\generic_stm32f103c\board.cpp:48:2: warning: #warning "Initializing board..." [-Wcpp]

 #warning "Initializing board..."

  ^

Using previously compiled file: C:\Users\me\AppData\Local\Temp\arduino_build_226650\core\wirish\boards.cpp.o
...
and PB4 will not toggle.
So "disableDebugPorts();" will not be compiled (second warning not shown) even if "-DCONFIG_MAPLE_MINI_NO_DISABLE_DEBUG=1" is listed under compiler options...

User avatar
martinayotte
Posts: 1229
Joined: Mon Apr 27, 2015 1:45 pm

Re: Some pins not usable when using ST-Link ?

Post by martinayotte » Sun Jan 15, 2017 5:00 pm

It is " #ifndef ", not " #ifdef ".

stevestrong
Posts: 1735
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Some pins not usable when using ST-Link ? [SOLVED]

Post by stevestrong » Sun Jan 15, 2017 5:13 pm

Yepp, that was it.
And also that in boards.txt I should't have had that define (don't know where is this coming from).
I removed that define and everything is ok now.

Post Reply