-
Notifications
You must be signed in to change notification settings - Fork 1
/
gpio.h
171 lines (144 loc) · 5.26 KB
/
gpio.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
* Constants for gpio registers
* (Offsets from the gpio base address)
*/
#define GPFSEL 0x00 //GPIO Function Select Read/Write access
#define GPSET 0x1C //GPIO Set Pin Output (HIGH), Write 1 bit
#define GPCLR 0x28 //GPIO Clear Pin Output (LOW), Write 1 bit
#define GPLEV 0x34 //GPIO Pin Level, Read access
#define GPEDS 0x40 //GPIO Pin Event Detect Status Read/Write 1 bit to clear
#define GPREN 0x4C //GPIO Pin Rising Edge Detect Enable Read/Write access
#define GPFEN 0x58 //GPIO Pin Falling Edge Detect Enable Read/Write access
#define GPHEN 0x64 //GPIO Pin High Detect Enable Read/Write access
#define GPLEN 0x70 //GPIO Pin Low Detect Enable Read/Write access
#define GPAREN 0x7C //GPIO Pin Async. Rising Edge Detect Read/Write access
#define GPAFEN 0x88 //GPIO Pin Async. Falling Edge Detect Read/Write access
#define GPPULL 0xe4 //GPIO_PUP_PDN_CNTRL_REG Pull-up/-down Read/Write access
#define PIN_COUNT 58 //RPi 4
//Pin functions for the GPFSEL-register
#define INPUT 0
#define OUTPUT 1
#define ALTERNATE_FUNCTION_0 4
#define ALTERNATE_FUNCTION_1 5
#define ALTERNATE_FUNCTION_2 6
#define ALTERNATE_FUNCTION_3 7
#define ALTERNATE_FUNCTION_4 3
#define ALTERNATE_FUNCTION_5 2
//Return values of functions
#define NO_ACCESS -13 //if mapping /dev/gpiomem fails
#define SUCCESS 0
#define EVENT_OCCURED 1
#define NO_EVENT 0
//Level of a pin
#define LOW 0
#define HIGH 1
//Direction of pull-up/pull-down-register
#define NONE 0
#define UP 1
#define DOWN 2
/**
* Maps the GPIO device (/dev/gpiomem) of the Raspberry Pi 4
* into the user programs' memory space.
* Returns 0 for success, NO_ACCESS if user has no permission for
* reading/writing to/from the /dev/gpiomem file.
* [In Ubuntu Mate 20.04.1, MATE 1.24.0 for Raspberry Pi 4B
* /dev/gpiomem belongs to a group 'dialout' which has read/write access.
* After adding the username to this group in the /etc/group file
* (dialout:x:20:myname) mapping should succeed.]
*/
int init_gpio(void);
/**
* Unmaps the GPIO device of the Raspbery Pi.
*/
int close_gpio(void);
/**
* Returns gpio base addresss.
* Requires initializiation with init_gpio()
*/
long long gpio_base(void);
/**
* All functions take a pin number.
* It's up to the user to provide a valid number
* in the range from 0 to PIN_COUNT-1.
* Invalid numbers can cause errors and crashes.
*/
/**
* Set gpio function for a pin
*/
void input(int pin); // GPFSEL INPUT
void output(int pin); // GPFSEL OUTPUT
void alt0(int pin); // GPFSEL ALTERNATE_FUNCTION_0
void alt1(int pin); // GPFSEL ALTERNATE_FUNCTION_1
void alt2(int pin); // GPFSEL ALTERNATE_FUNCTION_2
void alt3(int pin); // GPFSEL ALTERNATE_FUNCTION_3
void alt4(int pin); // GPFSEL ALTERNATE_FUNCTION_4
void alt5(int pin); // GPFSEL ALTERNATE_FUNCTION_5
/**
* Set output pin to low or high
*/
void low(int pin); //GPCLR
void high(int pin); //GPSET
/**
* Getter functions for readable gpio registers
*/
int function(int pin); //GPFSEL
int level(int pin); //GPLEV
int rising_edge_detect(int pin); //GPREN
int falling_edge_detect(int pin); //GPFEN
int high_detect(int pin); //GPHEN
int low_detect(int pin); //GPLEN
int async_rising_edge_detect(int pin); //GPAREN
int async_falling_edge_detect(int pin); //GPAFEN
int pull_direction(int pin); //GPPULL
/**
* Set functions for pullup-pull-down register
*/
void pull_up(int pin); //GPPULL PULL_UP
void pull_down(int pin); //GPPULL PULL_DOWN
void no_pull(int pin); //GPPULL NO_PULL
/**
* Returns EVENT_OCCURED if an event had occured, otherwise NO_EVENT.
* Does not clear the event in the GPEDS register.
*/
int event(int pin); //GPEDS
/**
* Clears an event.
*/
void clear_event(int pin);
/**
* Returns EVENT_OCCURED if an event had occured, otherwise NO_EVENT.
* If there was an event, the GPEDS register entry for the pin is
* immediatly cleared.
* This function is faster than
* int e = event(pin);
* if(e==EVENT_OCCURED){
* clear_event(pin);
* }
* return e;
*/
int get_event_and_clear(int pin);
/**
* Enable/disable gpio 'detect'registers
* Activation of one of these event detection registers can cause crashes in
* case of an event:
* "The GPIO peripheral has four dedicated interrupt lines.
* These lines are triggered by the setting of bits in the event detect
* status register."
* BCM2711 ARM Peripherals manual, p.64
*/
void enable_rising_edge_detect(int pin); // GPREN
void enable_falling_edge_detect(int pin); // GPFEN
void enable_high_detect(int pin); // GPHEN
void enable_low_detect(int pin); // GPLEN
void enable_async_rising_edge_detect(int pin); // GPAREN
void enable_async_falling_edge_detect(int pin);// GPAFEN
/**
* It is recommended to disable the 'detect' registers as soon as they are
* no longer needed.
*/
void disable_rising_edge_detect(int pin); // GPREN
void disable_falling_edge_detect(int pin); // GPFEN
void disable_high_detect(int pin); // GPHEN
void disable_low_detect(int pin); // GPLEN
void disable_async_rising_edge_detect(int pin); // GPAREN
void disable_async_falling_edge_detect(int pin);// GPAFEN