s-matyukevich/raspberry-pi-os

lesson 01 set alt5 for gpio 14/15

remi-boivin opened this issue · 4 comments

I'm currently read the first lesson and I can't understand why we use 12 and 15 to speak about 14/15 gpio

    unsigned int selector;
    selector = get32(GPFSEL1);
    selector &= ~(7<<12);                   // clean gpio14
    selector |= 2<<12;                      // set alt5 for gpio14
    selector &= ~(7<<15);                   // clean gpio15
    selector |= 2<<15;                      // set alt5 for gpio 15
    put32(GPFSEL1,selector);```
    Anyone can explain to me pls ?

for the GPFSEL1 register, bits 12-14 control gpio 14 and bits 15-17 control gpio 15. (7 << 12) is putting 111 in the bits 12-14 and &= ~ is clearing them, essentially. 2 << 12 is setting bits 12-14 as 010. Same goes for the 15-17 bits.

@rockytriton Thx for your answer. It's more clear for me. I've a last question how we choose 010 and 111 ?
According to
image
010 it's use to choose the 5th alternative function and 111 it's to choose the 3rd alternative function but according to
image
the 15th GPIO 3rd alternative function isn't RXD1. So I'm a little but confused.

for the GPFSEL1 register, bits 12-14 control gpio 14 and bits 15-17 control gpio 15. (7 << 12) is putting 111 in the bits 12-14 and &= ~ is clearing them, essentially. 2 << 12 is setting bits 12-14 as 010. Same goes for the 15-17 bits.

15th GPIO alt 5 = RXD1. From the table at the top 010 sets alt 5. The 111 is just to mask all 3 bits so we can clear them first before setting the values.

selector &= ~(7<<15);

That creates a mask with 15 to 17 bits set to 111, then ~ inverts the bits so all bits are set except for 15 to 17, then selector &= that to ensure that bits 15 to 17 are cleared (set to 0) but the remaining bits on selector are left as-is. Now that we clear those 3 bits, we can set the value to 010 by ORing it with this line:

selector |= 2<<15;

@rockytriton Okay thank you very much for your explanations :)

15th GPIO alt 5 = RXD1. From the table at the top 010 sets alt 5. The 111 is just to mask all 3 bits so we can clear them first before setting the values.

selector &= ~(7<<15);

That creates a mask with 15 to 17 bits set to 111, then ~ inverts the bits so all bits are set except for 15 to 17, then selector &= that to ensure that bits 15 to 17 are cleared (set to 0) but the remaining bits on selector are left as-is. Now that we clear those 3 bits, we can set the value to 010 by ORing it with this line:

selector |= 2<<15;