2025-12-08 10:47 PM
Hi !
I am using a stm32f103RCT6 MCU for my bare metal code. I want to use high speed SPI 1 peripheral (PB3- SCK, PB4- MISO, PB5- MOSI). By defualt, PB4 and PB5 are enabled as JTAG pins. I use the AFIO->MAPR register to disable JTAG and enable only SWDIO. The issue I am facing is that I am not able to flash my code on the controller after changing the value in the register. I have to power off my stm32 and flash the code before the register value changes. I added a delay so that I can have some time to connect my stm32 and flash the code. My code works fine and throws no errors. Only flashing the code has now become difficult. Please advice me what to do.
Below is my code snippet.
void SPIx_init(SPI_TypeDef *SPI){
Delay(1000);
// 1. GPIO clock enable
RCC->APB2ENR |= (1 << 0); // enable AFIO
if(SPI == SPI1){
Delay(1);
AFIO->MAPR &= ~(0b111 << 24); //Reset
AFIO->MAPR |= (0b010 << 24); // disable JTAG and enable SWDIO
Delay(1);
// SPI1 remap PB3, PB4, PB5
AFIO->MAPR |= (1 << 0);
GPIOB_CLK_ENABLE();
RCC->APB2ENR |= (1 << 12); // SPI1 clock
GPIOPin_init(PB3, AF_PP); // SCK
GPIOPin_init(PB4, INPUT_FI); // MISO
GPIOPin_init(PB5, AF_PP); // MOSI
}
below is the script I use for flashing the code -
FLASH_ADDR=0x08000000
echo "Flashing STM32F103RCT6 using OpenOCD with BIN file..."
openocd -f interface/stlink.cfg -f target/stm32f1x.cfg -c "program $BIN_FILE $FLASH_ADDR verify reset exit"