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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
|
From 23d7ab77865f8b17042f5cd4c6720cca475e0eb5 Mon Sep 17 00:00:00 2001
From: Khem Raj <raj.khem@gmail.com>
Date: Wed, 13 Jan 2016 14:27:10 -0800
Subject: [PATCH] Remove nested functions
nested functions are not supported on llvm/clang compiler, replacing
them helps make code portable and be compilable with non-gcc compilers
additionally fix the diagnostic messages clang reported
source/c_gpio.c:130:18: warning: comparison of distinct pointer types
('volatile uint32_t *' (aka 'volatile unsigned int *') an
d 'void *') [-Wcompare-distinct-pointer-types]
if (gpio_map < MAP_FAILED)
~~~~~~~~ ^ ~~~~~~~~~~
source/c_gpio.c:89:13: warning: variable 'peri_base' is used
uninitialized whenever 'if' condition is false [-Wsometimes-uninit
ialized]
if (fread(buf, 1, sizeof buf, fp) == sizeof buf) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source/c_gpio.c:116:17: note: uninitialized use occurs here
gpio_base = peri_base + GPIO_BASE_OFFSET;
^~~~~~~~~
source/c_gpio.c:89:9: note: remove the 'if' if its condition is always
true
if (fread(buf, 1, sizeof buf, fp) == sizeof buf) {
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
source/c_gpio.c:64:23: note: initialize the variable 'peri_base' to
silence this warning
uint32_t peri_base;
Signed-off-by: Khem Raj <raj.khem@gmail.com>
---
Upstream-Status: Submitted
source/c_gpio.c | 6 +--
source/py_gpio.c | 135 ++++++++++++++++++++++++++++---------------------------
2 files changed, 71 insertions(+), 70 deletions(-)
diff --git a/source/c_gpio.c b/source/c_gpio.c
index c96a2b0..b69880f 100644
--- a/source/c_gpio.c
+++ b/source/c_gpio.c
@@ -61,7 +61,7 @@ int setup(void)
{
int mem_fd;
uint8_t *gpio_mem;
- uint32_t peri_base;
+ uint32_t peri_base = 0;
uint32_t gpio_base;
unsigned char buf[4];
FILE *fp;
@@ -73,7 +73,7 @@ int setup(void)
if ((mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC)) > 0)
{
gpio_map = (uint32_t *)mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, 0);
- if ((uint32_t)gpio_map < 0) {
+ if (gpio_map == MAP_FAILED) {
return SETUP_MMAP_FAIL;
} else {
return SETUP_OK;
@@ -127,7 +127,7 @@ int setup(void)
gpio_map = (uint32_t *)mmap( (void *)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, gpio_base);
- if ((uint32_t)gpio_map < 0)
+ if (gpio_map == MAP_FAILED)
return SETUP_MMAP_FAIL;
return SETUP_OK;
diff --git a/source/py_gpio.c b/source/py_gpio.c
index d54cc7f..007bad5 100644
--- a/source/py_gpio.c
+++ b/source/py_gpio.c
@@ -69,6 +69,20 @@ static int mmap_gpio_mem(void)
return 0;
}
}
+static inline int cleanup_one(unsigned int gpio)
+{
+ // clean up any /sys/class exports
+ event_cleanup(gpio);
+
+ // set everything back to input
+ if (gpio_direction[gpio] != -1) {
+ setup_gpio(gpio, INPUT, PUD_OFF);
+ gpio_direction[gpio] = -1;
+ return 1;
+ }
+ return 0;
+}
+
// python function cleanup(channel=None)
static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
@@ -83,19 +97,6 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
PyObject *tempobj;
static char *kwlist[] = {"channel", NULL};
- void cleanup_one(void)
- {
- // clean up any /sys/class exports
- event_cleanup(gpio);
-
- // set everything back to input
- if (gpio_direction[gpio] != -1) {
- setup_gpio(gpio, INPUT, PUD_OFF);
- gpio_direction[gpio] = -1;
- found = 1;
- }
- }
-
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &chanlist))
return NULL;
@@ -140,7 +141,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
} else if (channel != -666) { // channel was an int indicating single channel
if (get_gpio_number(channel, &gpio))
return NULL;
- cleanup_one();
+ found = cleanup_one(gpio);
} else { // channel was a list/tuple
for (i=0; i<chancount; i++) {
if (chanlist) {
@@ -169,7 +170,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
if (get_gpio_number(channel, &gpio))
return NULL;
- cleanup_one();
+ found = cleanup_one(gpio);
}
}
}
@@ -182,6 +183,37 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs)
Py_RETURN_NONE;
}
+static inline int setup_one(unsigned int *gpio, int channel, int pud, int direction, int initial) {
+ if (get_gpio_number(channel, gpio))
+ return 0;
+
+ int func = gpio_function(*gpio);
+ if (gpio_warnings && // warnings enabled and
+ ((func != 0 && func != 1) || // (already one of the alt functions or
+ (gpio_direction[*gpio] == -1 && func == 1))) // already an output not set from this program)
+ {
+ PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", 1);
+ }
+
+ // warn about pull/up down on i2c channels
+ if (gpio_warnings) {
+ if (rpiinfo.p1_revision == 0) { // compute module - do nothing
+ } else if ((rpiinfo.p1_revision == 1 && (*gpio == 0 || *gpio == 1)) ||
+ (*gpio == 2 || *gpio == 3)) {
+ if (pud == PUD_UP || pud == PUD_DOWN)
+ PyErr_WarnEx(NULL, "A physical pull up resistor is fitted on this channel!", 1);
+ }
+ }
+
+ if (direction == OUTPUT && (initial == LOW || initial == HIGH)) {
+ output_gpio(*gpio, initial);
+ }
+ setup_gpio(*gpio, direction, pud);
+ gpio_direction[*gpio] = direction;
+ return 1;
+}
+
+
// python function setup(channel(s), direction, pull_up_down=PUD_OFF, initial=None)
static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs)
{
@@ -195,37 +227,6 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
int pud = PUD_OFF + PY_PUD_CONST_OFFSET;
int initial = -1;
static char *kwlist[] = {"channel", "direction", "pull_up_down", "initial", NULL};
- int func;
-
- int setup_one(void) {
- if (get_gpio_number(channel, &gpio))
- return 0;
-
- func = gpio_function(gpio);
- if (gpio_warnings && // warnings enabled and
- ((func != 0 && func != 1) || // (already one of the alt functions or
- (gpio_direction[gpio] == -1 && func == 1))) // already an output not set from this program)
- {
- PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", 1);
- }
-
- // warn about pull/up down on i2c channels
- if (gpio_warnings) {
- if (rpiinfo.p1_revision == 0) { // compute module - do nothing
- } else if ((rpiinfo.p1_revision == 1 && (gpio == 0 || gpio == 1)) ||
- (gpio == 2 || gpio == 3)) {
- if (pud == PUD_UP || pud == PUD_DOWN)
- PyErr_WarnEx(NULL, "A physical pull up resistor is fitted on this channel!", 1);
- }
- }
-
- if (direction == OUTPUT && (initial == LOW || initial == HIGH)) {
- output_gpio(gpio, initial);
- }
- setup_gpio(gpio, direction, pud);
- gpio_direction[gpio] = direction;
- return 1;
- }
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii", kwlist, &chanlist, &direction, &pud, &initial))
return NULL;
@@ -290,7 +291,7 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
} else if (chantuple) {
chancount = PyTuple_Size(chantuple);
} else {
- if (!setup_one())
+ if (!setup_one(&gpio, channel, pud, direction, initial))
return NULL;
Py_RETURN_NONE;
}
@@ -320,12 +321,29 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
return NULL;
}
- if (!setup_one())
+ if (!setup_one(&gpio, channel, pud, direction, initial))
return NULL;
}
Py_RETURN_NONE;
}
+static inline int output_val(unsigned int *gpio, int channel, int value) {
+ if (get_gpio_number(channel, gpio))
+ return 0;
+
+ if (gpio_direction[*gpio] != OUTPUT)
+ {
+ PyErr_SetString(PyExc_RuntimeError, "The GPIO channel has not been set up as an OUTPUT");
+ return 0;
+ }
+
+ if (check_gpio_priv())
+ return 0;
+
+ output_gpio(*gpio, value);
+ return 1;
+}
+
// python function output(channel(s), value(s))
static PyObject *py_output_gpio(PyObject *self, PyObject *args)
@@ -342,23 +360,6 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args)
int chancount = -1;
int valuecount = -1;
- int output(void) {
- if (get_gpio_number(channel, &gpio))
- return 0;
-
- if (gpio_direction[gpio] != OUTPUT)
- {
- PyErr_SetString(PyExc_RuntimeError, "The GPIO channel has not been set up as an OUTPUT");
- return 0;
- }
-
- if (check_gpio_priv())
- return 0;
-
- output_gpio(gpio, value);
- return 1;
- }
-
if (!PyArg_ParseTuple(args, "OO", &chanlist, &valuelist))
return NULL;
@@ -416,7 +417,7 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args)
}
if (chancount == -1) {
- if (!output())
+ if (!output_val(&gpio, channel, value))
return NULL;
Py_RETURN_NONE;
}
@@ -472,7 +473,7 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args)
return NULL;
}
}
- if (!output())
+ if (!output_val(&gpio, channel, value))
return NULL;
}
--
2.7.0
|