## Section19.2GPIO Connections

Table 19.2.1 shows the relationship between the GPIO pins and the header pins for the 40-pin header. Table 19.2.2 shows the relationship for Revision 2.0 of the 26-bit header, and Table 19.2.3 shows the relationship for Revision 1.0 of the 26-pin header. If you have a Raspberry Pi with a 26-pin header, you will need to consult the Raspberry Pi website to determine which Revision you have.

Table 19.2.4 shows which bits in the GPIO function select registers control physical pins on the GPIO header.

Table 19.2.5 shows the locations of the three GPIO registers that are used to select the pin functions Table 19.2.4.

Thus if we wish to access pin number 12 on the GPIO header, we need to access bits $26$–$24$ at $\hex{0x004}$ relative to virtual address in our program that we get from the call to the mmap function.

Using one of the pins in the header connection is a two-step process:

1. Select the function of the pin. Each connector pin can be selected to be either an input or an output. Most can also be selected for alternate functions, but we will not do so in this book.

2. Turn the pin on (Set) or off (Clear). There are two separate sets of GPIO registers for either setting a pin function or clearing it.

The first step can be accomplished as shown in Listing 19.2.6.

Selecting the function of a pin involves working with three bits within a 32-bit register. We need to be careful not to disturb the other 29 bits in the register. The first step is to clear the bits within the desired 3-bit field. In the following code segment the value in register we are working with has been loaded into r2 and the pin number is in r1.

mov     r3, r1          @ need to multiply pin
add     r1, r1, r3 1    @    position by 3
mov     r3, PIN_FIELD   @ gpio pin field
lsl     r3, r3, r1      @ shift to pin position
bic     r2, r2, r3      @ clear pin field


Multiplying the pin number by $2$ (left shift one bit) and adding it to itself is a fast, simple way to multiply by $3\text{.}$

Clearing the three bits requires a new instruction, bic.

BIC

Performs a bitwise AND between the NOT of a bit pattern and a value in a register, thus clearing the bits in the register as specified by the bit pattern.

BIC{S}{<c>}  {<Rd>,} <Rn>, #<const>           % immediate
BIC{S}{<c>}  {<Rd>,} <Rn>, <Rm>{, <shift>}    % register
BIC{S}{<c>}  {<Rd>,} <Rn>, <Rm>, <type> <Rs>  % register-shifted register

• If ‘S’ is present the condition flags are updated according to the result. If absent, the condition flags are not changed.

• <c> is the condition code, Table 9.2.1.

• <Rd> specifies the destination register, and <Rm> and <Rn> are the source registers. <Rs> contains the shift amount in the “register-shifted register” form.

• $-257 \le const \le +256\text{,}$ or $const = +256, +260, +264, \ldots, +65280\text{,}$ or $const = -261, -265, \ldots, -65281\text{.}$ This odd sequence of values will be explained in Section 11.3.3

• <shift> and <type> are explained in Section 9.2.3

In the “immediate” form, a bitwise AND is performed between the NOT of <const> and the value in <Rn>. In the “register” and “register-shifted register” forms, a bitwise AND is performed between the value in <Rn> and the NOT of the value in <Rm>. If a shift is specified, the value in <Rm> is shifted by the specified amount before the AND is performed. If <Rd> is present the result is stored there and <Rn> is unchanged. If not, the result is stored in <Rn>. The values in <Rm> and <Rs> are unchanged.

After the bit field has been cleared, we can now use orr to store the new function code in the 3-bit field without disturbing the other 29 bits:

lsl     r6, r6, r1      @ shift function code to pin position
orr     r2, r2, r6      @ enter function code


For the second step, Table 19.2.7 shows the locations of the registers that are used to set or clear a pin once its function has been selected and the pin number that corresponds to each bit in the set and clear registers.

Setting a pin can be accomplished as shown in Listing 19.2.8.

It is important to understand that storing a $\binary{0}$ in the bit position corresponding to a pin does not clear the pin. To clear a pin, you need to store a $\binary{1}$ in the corresponding bit in the appropriate GPCLRn register. This eliminates the possiblity of accidentally clearing other pins while setting one pin. The clear operation operates in the same manner.