-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGPIOlowlevel.cpp
More file actions
executable file
·141 lines (129 loc) · 3.59 KB
/
GPIOlowlevel.cpp
File metadata and controls
executable file
·141 lines (129 loc) · 3.59 KB
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
#include "GPIOlowlevel.h"
int map_peripheral(bcm_peripheralPtr p, int memInterface){
if (memInterface == IFACE_DEV_GPIOMEM){
// Open newfangled dev/gpiomem instead of /dev/mem for access without sudo
if ((p->mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC) ) < 0) {
perror("Failed to open /dev/gpiomem");
return 1;
}
}else{
if ((p->mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
perror("Failed to open /dev/mem. Did you forget to sudo");
return 1;
}
}
/* mmap IO */
p->map = mmap(
NULL, //Any address in our space will do
BLOCK_SIZE, //Map length
PROT_READ|PROT_WRITE|PROT_EXEC, // Enable reading & writing to mapped memory
MAP_SHARED| MAP_LOCKED, //Shared with other processes
p->mem_fd, //File to map
p->addr_p //Offset to base address
);
//p->map = mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, p->mem_fd, p->addr_p);
if (p->map == MAP_FAILED) {
perror("mmap error");
close (p->mem_fd);
return 1;
}
p ->addr = (volatile unsigned int *)p->map;
// close file descriptor
if (close(p -> mem_fd) < 0){
perror("couldn't close memory file descriptor");
return 1;
}
return 0;
}
/* ******************** Un-Map a Peripheral *******************************************/
void unmap_peripheral(bcm_peripheralPtr p) {
munmap(p->map, BLOCK_SIZE);
}
/* ****** Utility functions for peripheral mapping ***********************
have to initialize these somewhere */
bcm_peripheralPtr GPIOperi = nullptr;
int GPIOperi_users=0;
volatile unsigned int * useGpioPeri (void){
// map GPIO peripheral, if needed
int errCode =0;
if (GPIOperi ==nullptr) {
GPIOperi = new bcm_peripheral {GPIO_BASE};
errCode = map_peripheral (GPIOperi, IFACE_DEV_GPIOMEM);
if (errCode){
GPIOperi = nullptr;
GPIOperi_users =0;
return nullptr;
}
}
GPIOperi_users +=1;
return GPIOperi->addr;
}
void unUseGPIOperi (void){
if (GPIOperi_users > 0){ // no-one would call unUseGPIOperi before useGpioPeri, would they?
GPIOperi_users -=1;
if (GPIOperi_users ==0){
unmap_peripheral (GPIOperi);
delete (GPIOperi);
GPIOperi = nullptr;
}
}
}
bcm_peripheralPtr PWMperi = nullptr;
int PWMperi_users=0;
volatile unsigned int * usePWMPeri (void){
// map PWM peripheral, if needed
int errCode =0;
if (PWMperi ==nullptr) {
PWMperi = new bcm_peripheral {PWM_BASE};
errCode = map_peripheral (PWMperi, IFACE_DEV_MEM);
if (errCode){
PWMperi = nullptr;
PWMperi_users = 0;
return nullptr;
}
}
PWMperi_users +=1;
return PWMperi->addr;
}
void unUsePWMperi (void){
if (PWMperi_users > 0){
PWMperi_users -= 1;
if (PWMperi_users == 0){
*(PWMperi->addr + PWM_CTL) = 0;
unmap_peripheral (PWMperi);
delete (PWMperi);
PWMperi = nullptr;
}
}
}
bcm_peripheralPtr PWMClockperi = nullptr;
int PWMClockperi_users=0;
volatile unsigned int * usePWMClockPeri (void){
// map PWM clock peripheral, if needed
int errCode =0;
if (PWMClockperi ==nullptr) {
PWMClockperi = new bcm_peripheral {CM_PWMBASE};
errCode = map_peripheral (PWMClockperi, IFACE_DEV_MEM);
if (errCode){
PWMClockperi = nullptr;
PWMClockperi_users = 0;
return nullptr;
}
}
PWMClockperi_users +=1;
return PWMClockperi->addr;
}
void unUsePWMClockperi (void){
if (PWMClockperi_users > 0){
PWMClockperi_users -= 1;
if (PWMClockperi_users == 0){
// Turn off PWM.
*(PWMperi->addr + PWM_CTL) = 0;
// Turn off PWM clock enable flag.
*(PWMClockperi->addr + CM_PWMCTL) = ((*PWMClockperi->addr + CM_PWMCTL) & CM_DISAB) | CM_PASSWD;
unmap_peripheral (PWMClockperi);
delete (PWMClockperi);
PWMClockperi = nullptr;
}
}
}