Skip to main content
chriskuku
Senior II
April 23, 2026
Solved

Setting GPIO bits nondestructively - HAL or LL? (Using an STM32F103CBT6 - Blue Pill board)

  • April 23, 2026
  • 3 replies
  • 129 views

This is probably an old hat to most of you here but I'm facing this problem momentarily:
Using an STM32F103CBT6 (Blue Pill) where GPIOB Port is used already with the following

PB3 - SPI1_SCK
PB5 - SP1_MOSI
PB6 - Some Interface Function (OUTPUT)
PB7 - Some Interface Function (OUTPUT)

 

I want to use PB0, PB1, PB4, PB8, PB12,PB13,PB14,PB15 for a keyboard matrix scan and output
a running pattern to these pins. Writing the bits should be nondestructive what the remaining bits is concerned. 
At the back of my mind I recall something like BSRR, but could someone help me to get on the right track?
I'm using HAL in this app (under STM32CubeIDE).

Best answer by Ozone

I'd recommend to consult the reference manual.
The F103 has a BSRR register for setting and resetting of individual bits, and a BRR for resetting bits.
Section 9.2.5 and 9.2.6 in RM008 : https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

 

3 replies

Ozone
OzoneBest answer
Principal
April 23, 2026

I'd recommend to consult the reference manual.
The F103 has a BSRR register for setting and resetting of individual bits, and a BRR for resetting bits.
Section 9.2.5 and 9.2.6 in RM008 : https://www.st.com/resource/en/reference_manual/rm0008-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

 

Peter BENSCH
Technical Moderator
April 23, 2026

@chriskuku

And additionally, I would like to point out once again that the Bluepills have contained almost exclusively fakes for years, which we cannot support. It would make more sense to contact the manufacturer of the counterfeits.

Regards
/Peter

Lead II
April 23, 2026

LL_GPIO_SetOutputPin sets individual pins or masks of pins on the same port using lower 16 bits of BSRRBSy:.

LL_GPIO_ResetOutputPin clears individual pins or masks of pins on the same port using upper 16 bits of BSRRBRy.

HAL_GPIO_WritePin sets or clears individual pins or masks of pins on the same port using BSRR.

If you want to set and clear multiple pins on the same port in one operation you need to write to BSRR yourself: 

BSRR = (pinsToClear<<16) | pinsToSet;



"Kudo posts if you have the same problem and kudo replies if the solution works.Click ""Accept as Solution"" if a reply solved your problem. If no solution was posted please answer with your own."