-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmmapGpio.cpp
230 lines (197 loc) · 7.32 KB
/
mmapGpio.cpp
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#include "mmapGpio.h"
// GPIO Registers that can be accessed as a part of the class
const unsigned int mmapGpio::GPFSET0;
const unsigned int mmapGpio::GPFCLR0;
const unsigned int mmapGpio::GPFLEV0;
const unsigned int mmapGpio::GPFSEL0;
const unsigned int mmapGpio::GPFSEL1;
const unsigned int mmapGpio::GPFSEL2;
const unsigned int mmapGpio::GPFSEL3;
//defines the two possible GPIO directions
const unsigned int mmapGpio::INPUT;
const unsigned int mmapGpio::OUTPUT;
//defines the two possible states of output/input pins
const unsigned int mmapGpio::LOW;
const unsigned int mmapGpio::HIGH;
//private constants
const unsigned int mmapGpio::GPIO_BASE;
const unsigned int mmapGpio::GPIO_LEN;
/*******************************************************************
* Default constructor....
* Simply calls mapRegAddri() function to map the physical addresses
* of the GPIO registers into local process memory
*
* Parameters - None
* Return Value - None
*******************************************************************/
mmapGpio::mmapGpio(){
this->gpio = mapRegAddr(GPIO_BASE);
}
/*******************************************************************
* Destructor - unmaps GPIO registers (physical memory) from
* process memoy
*
* Parameters - None
* Return Value - None
******************************************************************/
mmapGpio::~mmapGpio(){
//unmap GPIO registers (physicalmemory) from process memoy
if(munmap((void*)gpio, GPIO_LEN) < 0){
perror("munmap (gpio) failed");
exit(1);
}
}
/*******************************************************************
* writeGPIOReg() - Writes a 32-bit value to one of the GPIO
* addresses listed on lines 4-9. This function is not required for
* basic GPIO usage (low level access function) but is made available
* anyways.
*
* Parameters reg - Register address to write to.....see lines 4-9
* val - unsigned 32-bit value to write to the reg
* Return Value - none
* ****************************************************************/
void mmapGpio::writeGPIOReg(unsigned int reg, unsigned int val){
*(this->gpio + reg) = val;
}
/*******************************************************************
* readGPIOReg() - reads a 32-bit value from one of the GPIO
* addresses listed on lines 4-9. This function is not required for
* basic GPIO usage (low level access function) but is made available
* anyways.
*
* Parameters reg - Register address to read from.....see lines 4-9
* val - Value of reg is written to val and passed back to
* calling function/method by reference
* Return Value - none
* ****************************************************************/
void mmapGpio::readGPIOReg(unsigned int reg, unsigned int &val){
val = *(this->gpio + reg);
}
/*******************************************************************
* setPinDir() - sets the direction of a pin to either input or
* output
*
* Parameters - pinnum - GPIO pin number as per the RPI's BCM2835's
* standard definition
* dir - pin direction can be mmapGpio::INPUT for input
* or mmapGpio::OUTPUT for output
* Return Value -None
* *****************************************************************/
void mmapGpio::setPinDir(unsigned int pinnum, const unsigned int &dir){
if (dir == OUTPUT){
switch(pinnum/10) {
case 0:
*(this->gpio + GPFSEL0) &= ~(7<<(((pinnum)%10)*3));
*(this->gpio + GPFSEL0) |= (1<<(((pinnum)%10)*3));
break;
case 1:
*(this->gpio + GPFSEL1) &= ~(7<<(((pinnum)%10)*3));
*(this->gpio + GPFSEL1) |= (1<<(((pinnum)%10)*3));
break;
case 2:
*(this->gpio + GPFSEL2) &= ~(7<<(((pinnum)%10)*3));
*(this->gpio + GPFSEL2) |= (1<<(((pinnum)%10)*3));
break;
case 3:
*(this->gpio + GPFSEL3) &= ~(7<<(((pinnum)%10)*3));
*(this->gpio + GPFSEL3) |= (1<<(((pinnum)%10)*3));
break;
default:
break;
}
}
else{
switch(pinnum/10) {
case 0:
*(this->gpio + GPFSEL0) &= ~(7<<(((pinnum)%10)*3));
break;
case 1:
*(this->gpio + GPFSEL1) &= ~(7<<(((pinnum)%10)*3));
break;
case 2:
*(this->gpio + GPFSEL2) &= ~(7<<(((pinnum)%10)*3));
break;
case 3:
*(this->gpio + GPFSEL3) &= ~(7<<(((pinnum)%10)*3));
break;
default:
break;
}
}
}
/*******************************************************************
* readPin() - reads the state of a GPIO pin and returns its value
*
* Parameter - pinnum - the pin number of the GPIO to read
*
* return Value - pin value. Either 1 (mmapGpio::HIGH) if pin state
* is high or 0 (mmapGpio::LOW) if pin is low
* ****************************************************************/
unsigned int mmapGpio::readPin(unsigned int pinnum){
unsigned int retVal = 0;
if ((*(this->gpio + GPFLEV0) & (1 << pinnum)) != 0)
retVal = 1;
return retVal;
}
/*******************************************************************
* writePinState() - sets (to 1) or clears (to 0) the state of an
* output GPIO. This function has no effect on input GPIOs.
* For faster output GPIO pin setting/clearing..use inline functions
* 'writePinHigh()' & 'writePinLow()' defined in the header file
*
* Parameters - pinnum - GPIO number as per RPI and BCM2835
* standard definition
* pinstate - value to write to output pin...either
* mmapGpio::HIGH for setting or mmapGpio::LOW for
* clearing
* Return Value - None
* ****************************************************************/
void mmapGpio::writePinState(unsigned int pinnum, const unsigned int &pinstate){
if(pinstate == HIGH)
*(this->gpio + GPFSET0) = (1 << pinnum) ;
else
*(this->gpio + GPFCLR0) = (1 << pinnum);
}
/********************************************************************
* volatile unsigned *mmapGpio::mapRegAddr(unsigned long baseAddr)
* This function maps a block of physical memory into the memory of
* the calling process. It enables a user space process to access
* registers in physical memory directly without having to interact
* with in kernel side code i.e. device drivers
*
* Parameter - baseAddr (unsigned long) - this is the base address of
* a block of physical memory that will be mapped into the userspace
* process memory.
*******************************************************************/
volatile unsigned *mmapGpio::mapRegAddr(unsigned long baseAddr){
int mem_fd = 0;
void *regAddrMap = MAP_FAILED;
/* open /dev/mem.....need to run program as root i.e. use sudo or su */
if (!mem_fd) {
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
perror("can't open /dev/mem");
exit (1);
}
}
/* mmap IO */
regAddrMap = mmap(
NULL, //Any adddress in our space will do
GPIO_LEN, //Map length
PROT_READ|PROT_WRITE|PROT_EXEC,// Enable reading & writting to mapped memory
MAP_SHARED|MAP_LOCKED, //Shared with other processes
mem_fd, //File to map
baseAddr //Offset to base address
);
if (regAddrMap == MAP_FAILED) {
perror("mmap error");
close(mem_fd);
exit (1);
}
if(close(mem_fd) < 0){ //No need to keep mem_fd open after mmap
//i.e. we can close /dev/mem
perror("couldn't close /dev/mem file descriptor");
exit(1);
}
return (volatile unsigned *)regAddrMap;
}