0

I am trying to change the state (output/input) for more then one pin at the same time (with a bitmask).

The code for one pin is:

#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |=  (1<<(((g)%10)*3))

I don't really understand what this code does.

Let's say, gpio := 0x20200000 so for Pin 1 it should be

 10 0000 0010 0000 0000 0000 0000 0000 + 0 = (10 0000 0010 0000 0000 0000 0000 0000 + 0) & ~11 1000 = 0

I think this can't be correct. What am i doing wrong?

metacubed
  • 7,031
  • 6
  • 36
  • 65

1 Answers1

0

so from the broadcom arm peripherals manual which you should already be referencing before asking us to read it for you...0x20200000 is the function select register for gpio pins 0 to 9 for each set of 10 pins there is a register 3 bits per gpio to select one of 8 functions 2 bits unused. so the modulo 10 is to figure out which function select register, then times 3 is three bits per gpio pin.

the bit pattern 0b000 defines the pin as an input and the bit pattern 0b001 as an output, so this code you referenced either zeros the three bits or it ors the three bits with a 1 which of course is buggy since the other two bits are not guaranteed to be zero. to use that code properly you should probably either fix it or set the gpio as an input then an output so that it clears the three bits then sets the one bit.

old_timer
  • 69,149
  • 8
  • 89
  • 168