[En-Nut-Discussion] STM32 GPIO BSRR

Holger Mai mai at gemac.info
Thu Apr 21 11:14:10 CEST 2016

for my sources i use often the BSRRH and BSRRL registers in GPIO_Typedef. They
based on BSRR (32bit), but if splitted in BSRRH and BSRRL, setting/resetting
bits is simpler than on BSRR, because can use the same bitmask for set/reset,
without any shifting.
However, i found that some headerfiles from ST has BSRR in GPIO_TypeDef, some
other has BSRRH and BSRRL. And there are  files for the same MCU with different
For me, i find it a good idea, to work with one or other access, so as i need
it. To realise this, the MCU decription files (i.e. STM32F756xx.h) has to be

typedef struct
  __IO uint32_t MODER;    /*!< GPIO port mode register,               Address
offset: 0x00      */
  __IO uint32_t ODR;      /*!< GPIO port output data register,        Address
offset: 0x14      */
- __IO uint32_t BSRR;     /*!< GPIO port bit set/reset register,      Address
offset: 0x18      */
+ union  /* Allow BSRR register access as 32-bit register or two 16-bit
registers */ 
+ {
+   __IO uint32_t BSRR;                     // 32-bit BSSR register for
set/reset at same command
+   struct { __IO uint16_t BSRRL, BSRRH; }; // Two 16-bit registers, set or
reset separately
+ };
  __IO uint32_t LCKR;     /*!< GPIO port configuration lock register, Address
offset: 0x1C      */
  __IO uint32_t AFR[2];   /*!< GPIO alternate function registers,     Address
offset: 0x20-0x24 */
} GPIO_TypeDef;
The charme is, that this works without any changes in existing code (because the
union and struct are anonymous).
Now are all three accesses possible:
GPIOA->BSRR = set_and_reset_bitmask_32;
GPIOA->BSRRH = reset_bitmask_16;
GPIOA->BSRRL = set_bitmask_16;

If you use a wrong bitmask type (i.e. a 16bit for BSRR or vice versa), the
compiler will give a warning.

mit freundlichen Grüßen /Best Regards

Holger Mai

mai at gemac-chemnitz.de

GEMAC - Gesellschaft für Mikroelektronik-
anwendung Chemnitz mbH
Zwickauer Straße 227
D-09116 Chemnitz
Tel. +49 371 3377 - 0
Fax +49 371 3377 272
UST-ID: DE140851265
HRB 6443 Chemnitz/Stadt
Geschäftsführer: Dirk Hübner / Karsten Grönwoldt

More information about the En-Nut-Discussion mailing list