#include "config.h"
#endif
+#include <helper/time_support.h>
#include <jtag/interface.h>
#include "bitbang.h"
*/
static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
{
+ struct timeval timeout, now;
char buf[40];
char gpiostr[5];
int ret;
}
}
+ gettimeofday(&timeout, NULL);
+ timeval_add_time(&timeout, 0, 500000);
+
snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
- ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
+ for (;;) {
+ ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
+ if (ret >= 0 || errno != EACCES)
+ break;
+ gettimeofday(&now, NULL);
+ if (timeval_compare(&now, &timeout) >= 0)
+ break;
+ jtag_sleep(10000);
+ }
if (ret < 0) {
LOG_ERROR("Couldn't set direction for gpio %d", gpio);
perror("sysfsgpio: ");
}
snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
- ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
+ for (;;) {
+ ret = open(buf, O_RDWR | O_NONBLOCK | O_SYNC);
+ if (ret >= 0 || errno != EACCES)
+ break;
+ gettimeofday(&now, NULL);
+ if (timeval_compare(&now, &timeout) >= 0)
+ break;
+ jtag_sleep(10000);
+ }
if (ret < 0) {
LOG_ERROR("Couldn't open value for gpio %d", gpio);
perror("sysfsgpio: ");