The Pico/W In C: Direct To Hardware |
Written by Harry Fairhead | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Tuesday, 21 February 2023 | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Page 1 of 4 Sometimes you just have to go beyond the SDK and work with the hardware - it's not so difficult. This is an extract from the latest book in the I Programmer Library, all about the Pico/W in C. Programming the Raspberry Pi Pico In C
|
Offset |
Register Name |
Description |
0x000 |
GPIO0_STATUS |
GPIO status |
0x004 |
GPIO0_CTRL |
GPIO control including function select and overrides |
0X008 |
GPIO1_STATUS |
GPIO status |
0x00c |
GPIO1_CTRL |
GPIO control including function select and overrides |
… and so on down to |
||
0x0ec |
GPIO29_CTRL |
GPIO control including function select and overrides |
You can see that there are two registers for each GPIO line from GP0 to GP29, one control register and one status register.
Each register has the same format for each GPIO line. For example the status register is:
Bits |
Name |
Description |
Type |
Reset |
31:27 |
Reserved |
|
- |
- |
26 |
IRQTOPROC |
Interrupt to processors, after override applied |
RO |
0x0 |
25 |
Reserved |
- |
- |
|
24 |
IRQFROMPAD |
Interrupt from pad, before override applied |
RO |
0x0 |
23:20 |
Reserved |
|
- |
- |
19 |
INTOPERI |
Input signal to peripheral, after override applied |
RO |
0x0 |
18 |
Reserved |
|
- |
- |
17 |
INFROMPAD |
Input signal from pad, before override applied |
RO |
0x0 |
16:14 |
Reserved |
|
- |
- |
13 |
OETOPAD |
Output enable to pad, after override applied |
RO |
0x0 |
12 |
OEFROMPERI |
Output enable from selected peripheral, before override applied |
RO |
0x0 |
11:10 |
Reserved |
|
- |
- |
9 |
OUTTOPAD |
Output signal to pad after override applied |
RO |
0x0 |
8 |
OUTFROMPERI |
Output signal from selected peripheral, before override applied |
RO |
0x0 |
7:0 |
Reserved |
|
- |
- |
You can see that many of the 32 bits in the register are not used, but bit 9 is OUTTOPAD which is the final state of the GPIO line after register overrides have been applied.
You can read its current value using:
#include <stdio.h> #include "pico/stdlib.h" int main()
{
stdio_init_all(); uint32_t *addrGP0Status=(uint32_t*) 0x40014000; uint32_t value=*addrGP0Status; printf("%b",value); }
This prints the current status of GP0 in binary. If you want to find the status of GPn you need to use address 0x40014000+2n.
This is the general way you work with peripheral devices such as the PWM units or I2C hardware, but the GPIO is special in that it has another set of registers that control it.