[riot-devel] bitfeilds

Kees Bakker kees at sodaq.com
Sun Oct 30 13:03:18 CET 2016


On 30-10-16 10:55, Juergen Stuber wrote:
> On Fri, 28 Oct 2016 10:03:17 +0200
> Juergen Stuber <juergen at jstuber.net> wrote:
>> When you use shift and mask you usually do a single access for
>> all fields of a register.
> Note that you shouldn't do it in two assignments
> (I'm seeing this in cpu/stm32l1/periph/gpio.c):
>
>      port &= ~mask;
>      port |= (new_value << shift);
>
> This will result in bigger code and the first assignment will write a
> spurious value to the register, which might cause problems.
>
>      port = (port & ~mask) | (new_value << shift);
>
> is better. Or
>
>      port = (port & ~mask)
>           | (new_value1 << shift1)
> 	 ...
>           | (new_valueN << shiftN);
>
> for multiple fields.

True.

But this would not be a good example for (named) bitfields, because
in this example the bitfield is dynamic. (Depends on runtime value of
pin number.)
-- 
Kees


More information about the devel mailing list