Patrick Williams | 8b8bc41 | 2016-08-17 15:02:23 -0500 | [diff] [blame] | 1 | From 23d7ab77865f8b17042f5cd4c6720cca475e0eb5 Mon Sep 17 00:00:00 2001 |
| 2 | From: Khem Raj <raj.khem@gmail.com> |
| 3 | Date: Wed, 13 Jan 2016 14:27:10 -0800 |
| 4 | Subject: [PATCH] Remove nested functions |
| 5 | |
| 6 | nested functions are not supported on llvm/clang compiler, replacing |
| 7 | them helps make code portable and be compilable with non-gcc compilers |
| 8 | additionally fix the diagnostic messages clang reported |
| 9 | |
| 10 | source/c_gpio.c:130:18: warning: comparison of distinct pointer types |
| 11 | ('volatile uint32_t *' (aka 'volatile unsigned int *') an |
| 12 | d 'void *') [-Wcompare-distinct-pointer-types] |
| 13 | if (gpio_map < MAP_FAILED) |
| 14 | ~~~~~~~~ ^ ~~~~~~~~~~ |
| 15 | |
| 16 | source/c_gpio.c:89:13: warning: variable 'peri_base' is used |
| 17 | uninitialized whenever 'if' condition is false [-Wsometimes-uninit |
| 18 | ialized] |
| 19 | if (fread(buf, 1, sizeof buf, fp) == sizeof buf) { |
| 20 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 21 | source/c_gpio.c:116:17: note: uninitialized use occurs here |
| 22 | gpio_base = peri_base + GPIO_BASE_OFFSET; |
| 23 | ^~~~~~~~~ |
| 24 | source/c_gpio.c:89:9: note: remove the 'if' if its condition is always |
| 25 | true |
| 26 | if (fread(buf, 1, sizeof buf, fp) == sizeof buf) { |
| 27 | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 28 | source/c_gpio.c:64:23: note: initialize the variable 'peri_base' to |
| 29 | silence this warning |
| 30 | uint32_t peri_base; |
| 31 | |
| 32 | Signed-off-by: Khem Raj <raj.khem@gmail.com> |
| 33 | --- |
| 34 | Upstream-Status: Submitted |
| 35 | |
| 36 | source/c_gpio.c | 6 +-- |
| 37 | source/py_gpio.c | 135 ++++++++++++++++++++++++++++--------------------------- |
| 38 | 2 files changed, 71 insertions(+), 70 deletions(-) |
| 39 | |
| 40 | diff --git a/source/c_gpio.c b/source/c_gpio.c |
| 41 | index c96a2b0..b69880f 100644 |
| 42 | --- a/source/c_gpio.c |
| 43 | +++ b/source/c_gpio.c |
| 44 | @@ -61,7 +61,7 @@ int setup(void) |
| 45 | { |
| 46 | int mem_fd; |
| 47 | uint8_t *gpio_mem; |
| 48 | - uint32_t peri_base; |
| 49 | + uint32_t peri_base = 0; |
| 50 | uint32_t gpio_base; |
| 51 | unsigned char buf[4]; |
| 52 | FILE *fp; |
| 53 | @@ -73,7 +73,7 @@ int setup(void) |
| 54 | if ((mem_fd = open("/dev/gpiomem", O_RDWR|O_SYNC)) > 0) |
| 55 | { |
| 56 | gpio_map = (uint32_t *)mmap(NULL, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED, mem_fd, 0); |
| 57 | - if ((uint32_t)gpio_map < 0) { |
| 58 | + if (gpio_map == MAP_FAILED) { |
| 59 | return SETUP_MMAP_FAIL; |
| 60 | } else { |
| 61 | return SETUP_OK; |
| 62 | @@ -127,7 +127,7 @@ int setup(void) |
| 63 | |
| 64 | gpio_map = (uint32_t *)mmap( (void *)gpio_mem, BLOCK_SIZE, PROT_READ|PROT_WRITE, MAP_SHARED|MAP_FIXED, mem_fd, gpio_base); |
| 65 | |
| 66 | - if ((uint32_t)gpio_map < 0) |
| 67 | + if (gpio_map == MAP_FAILED) |
| 68 | return SETUP_MMAP_FAIL; |
| 69 | |
| 70 | return SETUP_OK; |
| 71 | diff --git a/source/py_gpio.c b/source/py_gpio.c |
| 72 | index d54cc7f..007bad5 100644 |
| 73 | --- a/source/py_gpio.c |
| 74 | +++ b/source/py_gpio.c |
| 75 | @@ -69,6 +69,20 @@ static int mmap_gpio_mem(void) |
| 76 | return 0; |
| 77 | } |
| 78 | } |
| 79 | +static inline int cleanup_one(unsigned int gpio) |
| 80 | +{ |
| 81 | + // clean up any /sys/class exports |
| 82 | + event_cleanup(gpio); |
| 83 | + |
| 84 | + // set everything back to input |
| 85 | + if (gpio_direction[gpio] != -1) { |
| 86 | + setup_gpio(gpio, INPUT, PUD_OFF); |
| 87 | + gpio_direction[gpio] = -1; |
| 88 | + return 1; |
| 89 | + } |
| 90 | + return 0; |
| 91 | +} |
| 92 | + |
| 93 | |
| 94 | // python function cleanup(channel=None) |
| 95 | static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) |
| 96 | @@ -83,19 +97,6 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) |
| 97 | PyObject *tempobj; |
| 98 | static char *kwlist[] = {"channel", NULL}; |
| 99 | |
| 100 | - void cleanup_one(void) |
| 101 | - { |
| 102 | - // clean up any /sys/class exports |
| 103 | - event_cleanup(gpio); |
| 104 | - |
| 105 | - // set everything back to input |
| 106 | - if (gpio_direction[gpio] != -1) { |
| 107 | - setup_gpio(gpio, INPUT, PUD_OFF); |
| 108 | - gpio_direction[gpio] = -1; |
| 109 | - found = 1; |
| 110 | - } |
| 111 | - } |
| 112 | - |
| 113 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|O", kwlist, &chanlist)) |
| 114 | return NULL; |
| 115 | |
| 116 | @@ -140,7 +141,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) |
| 117 | } else if (channel != -666) { // channel was an int indicating single channel |
| 118 | if (get_gpio_number(channel, &gpio)) |
| 119 | return NULL; |
| 120 | - cleanup_one(); |
| 121 | + found = cleanup_one(gpio); |
| 122 | } else { // channel was a list/tuple |
| 123 | for (i=0; i<chancount; i++) { |
| 124 | if (chanlist) { |
| 125 | @@ -169,7 +170,7 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) |
| 126 | |
| 127 | if (get_gpio_number(channel, &gpio)) |
| 128 | return NULL; |
| 129 | - cleanup_one(); |
| 130 | + found = cleanup_one(gpio); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | @@ -182,6 +183,37 @@ static PyObject *py_cleanup(PyObject *self, PyObject *args, PyObject *kwargs) |
| 135 | Py_RETURN_NONE; |
| 136 | } |
| 137 | |
| 138 | +static inline int setup_one(unsigned int *gpio, int channel, int pud, int direction, int initial) { |
| 139 | + if (get_gpio_number(channel, gpio)) |
| 140 | + return 0; |
| 141 | + |
| 142 | + int func = gpio_function(*gpio); |
| 143 | + if (gpio_warnings && // warnings enabled and |
| 144 | + ((func != 0 && func != 1) || // (already one of the alt functions or |
| 145 | + (gpio_direction[*gpio] == -1 && func == 1))) // already an output not set from this program) |
| 146 | + { |
| 147 | + PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", 1); |
| 148 | + } |
| 149 | + |
| 150 | + // warn about pull/up down on i2c channels |
| 151 | + if (gpio_warnings) { |
| 152 | + if (rpiinfo.p1_revision == 0) { // compute module - do nothing |
| 153 | + } else if ((rpiinfo.p1_revision == 1 && (*gpio == 0 || *gpio == 1)) || |
| 154 | + (*gpio == 2 || *gpio == 3)) { |
| 155 | + if (pud == PUD_UP || pud == PUD_DOWN) |
| 156 | + PyErr_WarnEx(NULL, "A physical pull up resistor is fitted on this channel!", 1); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if (direction == OUTPUT && (initial == LOW || initial == HIGH)) { |
| 161 | + output_gpio(*gpio, initial); |
| 162 | + } |
| 163 | + setup_gpio(*gpio, direction, pud); |
| 164 | + gpio_direction[*gpio] = direction; |
| 165 | + return 1; |
| 166 | +} |
| 167 | + |
| 168 | + |
| 169 | // python function setup(channel(s), direction, pull_up_down=PUD_OFF, initial=None) |
| 170 | static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwargs) |
| 171 | { |
| 172 | @@ -195,37 +227,6 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar |
| 173 | int pud = PUD_OFF + PY_PUD_CONST_OFFSET; |
| 174 | int initial = -1; |
| 175 | static char *kwlist[] = {"channel", "direction", "pull_up_down", "initial", NULL}; |
| 176 | - int func; |
| 177 | - |
| 178 | - int setup_one(void) { |
| 179 | - if (get_gpio_number(channel, &gpio)) |
| 180 | - return 0; |
| 181 | - |
| 182 | - func = gpio_function(gpio); |
| 183 | - if (gpio_warnings && // warnings enabled and |
| 184 | - ((func != 0 && func != 1) || // (already one of the alt functions or |
| 185 | - (gpio_direction[gpio] == -1 && func == 1))) // already an output not set from this program) |
| 186 | - { |
| 187 | - PyErr_WarnEx(NULL, "This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.", 1); |
| 188 | - } |
| 189 | - |
| 190 | - // warn about pull/up down on i2c channels |
| 191 | - if (gpio_warnings) { |
| 192 | - if (rpiinfo.p1_revision == 0) { // compute module - do nothing |
| 193 | - } else if ((rpiinfo.p1_revision == 1 && (gpio == 0 || gpio == 1)) || |
| 194 | - (gpio == 2 || gpio == 3)) { |
| 195 | - if (pud == PUD_UP || pud == PUD_DOWN) |
| 196 | - PyErr_WarnEx(NULL, "A physical pull up resistor is fitted on this channel!", 1); |
| 197 | - } |
| 198 | - } |
| 199 | - |
| 200 | - if (direction == OUTPUT && (initial == LOW || initial == HIGH)) { |
| 201 | - output_gpio(gpio, initial); |
| 202 | - } |
| 203 | - setup_gpio(gpio, direction, pud); |
| 204 | - gpio_direction[gpio] = direction; |
| 205 | - return 1; |
| 206 | - } |
| 207 | |
| 208 | if (!PyArg_ParseTupleAndKeywords(args, kwargs, "Oi|ii", kwlist, &chanlist, &direction, &pud, &initial)) |
| 209 | return NULL; |
| 210 | @@ -290,7 +291,7 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar |
| 211 | } else if (chantuple) { |
| 212 | chancount = PyTuple_Size(chantuple); |
| 213 | } else { |
| 214 | - if (!setup_one()) |
| 215 | + if (!setup_one(&gpio, channel, pud, direction, initial)) |
| 216 | return NULL; |
| 217 | Py_RETURN_NONE; |
| 218 | } |
| 219 | @@ -320,12 +321,29 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar |
| 220 | return NULL; |
| 221 | } |
| 222 | |
| 223 | - if (!setup_one()) |
| 224 | + if (!setup_one(&gpio, channel, pud, direction, initial)) |
| 225 | return NULL; |
| 226 | } |
| 227 | |
| 228 | Py_RETURN_NONE; |
| 229 | } |
| 230 | +static inline int output_val(unsigned int *gpio, int channel, int value) { |
| 231 | + if (get_gpio_number(channel, gpio)) |
| 232 | + return 0; |
| 233 | + |
| 234 | + if (gpio_direction[*gpio] != OUTPUT) |
| 235 | + { |
| 236 | + PyErr_SetString(PyExc_RuntimeError, "The GPIO channel has not been set up as an OUTPUT"); |
| 237 | + return 0; |
| 238 | + } |
| 239 | + |
| 240 | + if (check_gpio_priv()) |
| 241 | + return 0; |
| 242 | + |
| 243 | + output_gpio(*gpio, value); |
| 244 | + return 1; |
| 245 | +} |
| 246 | + |
| 247 | |
| 248 | // python function output(channel(s), value(s)) |
| 249 | static PyObject *py_output_gpio(PyObject *self, PyObject *args) |
| 250 | @@ -342,23 +360,6 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args) |
| 251 | int chancount = -1; |
| 252 | int valuecount = -1; |
| 253 | |
| 254 | - int output(void) { |
| 255 | - if (get_gpio_number(channel, &gpio)) |
| 256 | - return 0; |
| 257 | - |
| 258 | - if (gpio_direction[gpio] != OUTPUT) |
| 259 | - { |
| 260 | - PyErr_SetString(PyExc_RuntimeError, "The GPIO channel has not been set up as an OUTPUT"); |
| 261 | - return 0; |
| 262 | - } |
| 263 | - |
| 264 | - if (check_gpio_priv()) |
| 265 | - return 0; |
| 266 | - |
| 267 | - output_gpio(gpio, value); |
| 268 | - return 1; |
| 269 | - } |
| 270 | - |
| 271 | if (!PyArg_ParseTuple(args, "OO", &chanlist, &valuelist)) |
| 272 | return NULL; |
| 273 | |
| 274 | @@ -416,7 +417,7 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args) |
| 275 | } |
| 276 | |
| 277 | if (chancount == -1) { |
| 278 | - if (!output()) |
| 279 | + if (!output_val(&gpio, channel, value)) |
| 280 | return NULL; |
| 281 | Py_RETURN_NONE; |
| 282 | } |
| 283 | @@ -472,7 +473,7 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args) |
| 284 | return NULL; |
| 285 | } |
| 286 | } |
| 287 | - if (!output()) |
| 288 | + if (!output_val(&gpio, channel, value)) |
| 289 | return NULL; |
| 290 | } |
| 291 | |
| 292 | -- |
| 293 | 2.7.0 |
| 294 | |