Skip to content

Commit 4fb94f4

Browse files
Blargiancpq
authored andcommitted
Update appendix
1 parent 1a39555 commit 4fb94f4

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

README.md

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ GPIOA is at address 0x40020000, GPIOB is at 0x40020400, and so on:
257257

258258
We can create pin numbering that includes the bank and the pin number.
259259
To do that, we use 2-byte `uint16_t` value, where upper byte indicates
260-
GPIO bank, and lower byte indicates pin number:
260+
GPIO bank, and lower byte indicates pin number (see the [appendix](##Appendix) for further explanation of the functions below):
261261

262262
```c
263263
#define PIN(bank, num) ((((bank) - 'A') << 8) | (num))
@@ -270,7 +270,7 @@ This way, we can specify pins for any GPIO bank:
270270
```c
271271
uint16_t pin1 = PIN('A', 3); // A3 - GPIOA pin 3
272272
uint16_t pin2 = PIN('G', 11); // G11 - GPIOG pin 11
273-
```
273+
```
274274

275275
Let's rewrite the `gpio_set_mode()` function to take our pin specification:
276276
@@ -1755,6 +1755,37 @@ print the result to the UART, and check for the expected output in the test.
17551755
17561756
Happy testing!
17571757
1758+
## Appendix
1759+
1760+
In this section you will find some further explanations for select points in this guide.
1761+
1762+
### PIN function
1763+
1764+
We defined `PIN` as below:
1765+
1766+
```c
1767+
#define PIN(bank, num) ((((bank) - 'A') << 8) | (num))
1768+
```
1769+
1770+
This function is perhaps most easily understood through a worked example. Let's take pins `A3` and `G11` and see what is happening in the function above, step by step.
1771+
1772+
```c
1773+
uint16_t pin1 = PIN('A', 3); // A3 - GPIOA pin 3
1774+
uint16_t pin2 = PIN('G', 11); // G11 - GPIOG pin 11
1775+
```
1776+
1777+
Let's look first at what happens for `PIN('A', 3)`:
1778+
1779+
- `(bank) - 'A'` results in `'A' - 'A'` which equals ``. As a 16 bit binary value this would be `0b00000000,00000000`.
1780+
- Next we bit shift this value left by 8 bits because we want to store `bank` in the upper byte of this 16 bit, or 2 byte value. In this case the result remains the same: `0b00000000,00000000`.
1781+
- Finally we bitwise OR the value above with `num`, in our case `3` which has a 16 bit binary representation of `0b00000000,00000011`. The result in binary is `0b00000000,00000011`
1782+
1783+
Let's take a look at what happens for `PIN('G',11)`
1784+
1785+
- `(bank) - 'G'` results in `'G' - 'A'` which equals `6`. As a 16 bit binary value this would be `0b00000000,00000110`.
1786+
- Next we bit shift this value left by 8 bits because we want to store `bank` in the upper byte of this 16 bit, or 2 byte value. This results in a binary value of: `0b00000110,00000000`.
1787+
- Finally we bitwise OR the value above with `num`, in our case `11` which has a 16 bit binary representation of `0b00000000,00001011`. The result of the bitwise OR in binary is `0b00000110,00001011` which is a combination of `bank` in the upper byte and `pin` in the lower byte.
1788+
17581789
## About me
17591790
17601791
I am Sergey Lyubka, an engineer and entrepreneur. I hold a MSc in Physics from

0 commit comments

Comments
 (0)