Electrical – STM32F103. How to use PB4 as normal GPIO? NJTRST remap not working

hal-librarymicrocontrollerstm32stm32f10x

I need to use PB4 as a normal GPIO digital output pin. I have a custom designed board where this pin is connected to the CS of a SPI device, so just using another pin isn’t possible.

I’m using the HAL for developing the firmware. The code I use for configuring GPIO is:

__HAL_AFIO_REMAP_SWJ_NONJTRST();
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitTypeDef config_gpio = {0};
config_gpio.Pin = GPIO_PIN_4;
config_gpio.Mode = GPIO_MODE_OUTPUT_PP;
config_gpio.Pull = GPIO_NOPULL;
config_gpio.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &config_gpio);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);

After that, PB4 is always pulled high and I can't set it low, probably because it keeps behaving as NJTRST, so I can’t use it as a normal output.

It seems that the macro __HAL_AFIO_REMAP_SWJ_NONJTRST() doesn’t do anything.

Best Answer

It is needed to call

__HAL_RCC_AFIO_CLK_ENABLE();

before configuring remaps.