I have to write 16 bit value in a 16 bit register, & variable which holds that value is 32 bit. I have STM32, BSRRL register which is 16 bit, so if I write
Like uint32_t x = 255; BSRRL = x;
What will happen will compiler only take lower 16 bits of variable or will take entire 32 bitys & write upper 16 bits to BSRRH. If yes then what is correct way to do that, manually typecast it:
BSRRL = (uint16_t)x;
STM32 typedef struct { __IO uint32_t MODER; /*!< GPIO port mode register, Address offset: 0x00 */ __IO uint32_t OTYPER; /*!< GPIO port output type register, Address offset: 0x04 */ __IO uint32_t OSPEEDR; /*!< GPIO port output speed register, Address offset: 0x08 */ __IO uint32_t PUPDR; /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ __IO uint32_t IDR; /*!< GPIO port input data register, Address offset: 0x10 */ __IO uint32_t ODR; /*!< GPIO port output data register, Address offset: 0x14 */ __IO uint16_t BSRRL; /*!< GPIO port bit set/reset low register, Address offset: 0x18 */ __IO uint16_t BSRRH; /*!< GPIO port bit set/reset high register, Address offset: 0x1A */ __IO uint32_t LCKR; /*!< GPIO port configuration lock register, Address offset: 0x1C */ __IO uint32_t AFR[2]; /*!< GPIO alternate function registers, Address offset: 0x24-0x28 */ } GPIO_TypeDef;
BSRRL = (uint16_t) (x & (( 1 << ((sizeof(uint16_t) * 8 )) - 1 ) ;
The best way
... isn't. Not by quite a margin, for two opposing reasons.
1) it doesn't do anything that the OP's simpler
BSRRL = x
didn't already do with much less hassle.
2) (sizeof(uint16_t) * 8 ) is either a ridiculously over-complicated way of spelling '16', or it's flat-out wrong. Which of the two it is depends on the target platform.
I kneel to acknowledge your inferior intellect.