This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

set bits of register via mask

Sorry for probably stupid question..

All the time in SDK we set need bits through the mask, for example

NRF_GPIOTE->CONFIG[idx] |= (pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk)

I can't understand why we don't just write such way:

NRF_GPIOTE->CONFIG[idx] |= (pin << GPIOTE_CONFIG_PSEL_Pos)

In what case

(pin << GPIOTE_CONFIG_PSEL_Pos) is not equals to

(pin << GPIOTE_CONFIG_PSEL_Pos) & GPIOTE_CONFIG_PSEL_Msk) ???

  • What do you get if you specify pin as 0xffffffff, or in fact anything larger than 31? ANDing with the mask ensures that even if you entirely misuse the macro, you don't leak into other bits of the config register.

    Since usually pin is a constant known at compile time, it doesn't matter at all, the compiler will turn the whole thing into a constant.

Related