@@ -4,7 +4,7 @@ Copyright (c) 2013 Adafruit
44Original RPi.GPIO Author Ben Croston
55Modified for BBIO Author Justin Cooper
66
7- This file incorporates work covered by the following copyright and
7+ This file incorporates work covered by the following copyright and
88permission notice, all modified code adopts the original license:
99
1010Copyright (c) 2013 Ben Croston
@@ -85,7 +85,7 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
8585 if (!module_setup ) {
8686 init_module ();
8787 }
88-
88+
8989
9090 if (direction != INPUT && direction != OUTPUT )
9191 {
@@ -102,15 +102,33 @@ static PyObject *py_setup_channel(PyObject *self, PyObject *args, PyObject *kwar
102102 return NULL ;
103103 }
104104
105-
105+
106106 err = get_gpio_number (channel , & gpio );
107107 if (err != BBIO_OK )
108108 return NULL ;
109109
110- gpio_export (gpio );
111- gpio_set_direction (gpio , direction );
110+ unsigned int count = 100000 ;
111+ int res = -1 ;
112+ do { // wait until gpio file appears on the filesystem
113+ res = gpio_export (gpio );
114+ } while (res != 0 && count -- > 0 );
115+ if (count == 0 )
116+ return NULL ;
117+
118+ count = 100000 ;
119+ do {
120+ res = gpio_set_direction (gpio , direction );
121+ } while (res != 0 && count -- > 0 );
122+ if (count == 0 )
123+ return NULL ;
124+
112125 if (direction == OUTPUT ) {
113- gpio_set_value (gpio , initial );
126+ count = 100000 ;
127+ do {
128+ res = gpio_set_value (gpio , initial );
129+ } while (res != 0 && count -- > 0 );
130+ if (count == 0 )
131+ return NULL ;
114132 } else {
115133 if (pud == PUD_DOWN )
116134 set_pin_mode (channel , "gpio_pd" );
@@ -138,7 +156,7 @@ static PyObject *py_output_gpio(PyObject *self, PyObject *args)
138156
139157 err = get_gpio_number (channel , & gpio );
140158 if (err != BBIO_OK )
141- return NULL ;
159+ return NULL ;
142160
143161 if (!module_setup || gpio_direction [gpio ] != OUTPUT )
144162 {
@@ -196,10 +214,10 @@ static void run_py_callbacks(unsigned int gpio)
196214 gettimeofday (& tv_timenow , NULL );
197215 timenow = tv_timenow .tv_sec * 1E6 + tv_timenow .tv_usec ;
198216 if (cb -> bouncetime == 0 || timenow - cb -> lastcall > cb -> bouncetime * 1000 || cb -> lastcall == 0 || cb -> lastcall > timenow ) {
199-
217+
200218 // save lastcall before calling func to prevent reentrant bounce
201219 cb -> lastcall = timenow ;
202-
220+
203221 // run callback
204222 gstate = PyGILState_Ensure ();
205223 result = PyObject_CallFunction (cb -> py_cb , "s" , cb -> channel );
@@ -455,7 +473,7 @@ static PyObject *py_wait_for_edge(PyObject *self, PyObject *args)
455473 PyErr_SetString (PyExc_RuntimeError , error );
456474 return NULL ;
457475 }
458-
476+
459477 Py_RETURN_NONE ;
460478}
461479
0 commit comments