From bb7af9534aa96678d8072dc1bf7cc64900b95af9 Mon Sep 17 00:00:00 2001 From: Mike Frysinger Date: Thu, 26 Aug 2010 20:10:56 +0000 Subject: [PATCH] Rewrite to use raw file descriptors when working with the gpio value. Also assume the gpio layer only has a value of '0' or '1'. Work by Frans Meulenbroeks. git-svn-id: https://urjtag.svn.sourceforge.net/svnroot/urjtag/trunk@1833 b68d4a1b-bc3d-0410-92ed-d4ac073336b7 --- urjtag/ChangeLog | 6 ++++ urjtag/THANKS | 1 + urjtag/src/tap/cable/gpio.c | 62 ++++++++++++++++--------------------- 3 files changed, 34 insertions(+), 35 deletions(-) diff --git a/urjtag/ChangeLog b/urjtag/ChangeLog index 08a5f17b..568c0038 100644 --- a/urjtag/ChangeLog +++ b/urjtag/ChangeLog @@ -1,3 +1,9 @@ +2010-08-26 Mike Frysinger + + * src/tap/cable/gpio.c: Rewrite to use raw file descriptors when working with + the gpio value. Also assume the gpio layer only has a value of '0' or '1'. + Work by Frans Meulenbroeks. + 2010-08-24 Mike Frysinger * src/tap/cable/gpio.c (gpio_get_value): Need to seek back to the start of diff --git a/urjtag/THANKS b/urjtag/THANKS index 6b0592e8..426fc241 100644 --- a/urjtag/THANKS +++ b/urjtag/THANKS @@ -61,6 +61,7 @@ Raphael Mack Trevor Man Detrick Martin Adam Megacz +Frans Meulenbroeks Andreas Mohr Jani Monoses Masaki Muranaka diff --git a/urjtag/src/tap/cable/gpio.c b/urjtag/src/tap/cable/gpio.c index efcaec88..f02fdead 100644 --- a/urjtag/src/tap/cable/gpio.c +++ b/urjtag/src/tap/cable/gpio.c @@ -56,7 +56,7 @@ typedef struct { unsigned int jtag_gpios[4]; int signals; uint32_t lastout; - FILE *fp_gpios[4]; + int fd_gpios[4]; } gpio_params_t; static int gpio_export (unsigned int gpio, int export) @@ -113,11 +113,12 @@ static int gpio_direction (unsigned int gpio, int out) return URJ_STATUS_OK; } -static int gpio_set_value (FILE *fp, int value) +static int gpio_set_value (int fd, int value) { - int ret; + ssize_t ret; + char gpio_value = value + '0'; - ret = fprintf (fp, "%u", value ? 1 : 0); + ret = write (fd, &gpio_value, 1); if (ret != 1) { urj_warning (_("Error setting value gpio\n")); @@ -127,13 +128,13 @@ static int gpio_set_value (FILE *fp, int value) return URJ_STATUS_OK; } -static int gpio_get_value (FILE *fp, unsigned int gpio) +static int gpio_get_value (int fd, unsigned int gpio) { - int ret; - int value; + ssize_t ret; + char value; - ret = fscanf (fp, "%i", &value); - rewind (fp); + ret = read (fd, &value, 1); + lseek (fd, 0, SEEK_SET); if (ret != 1) { @@ -141,7 +142,7 @@ static int gpio_get_value (FILE *fp, unsigned int gpio) return URJ_STATUS_FAIL; } - return value; + return value == '1'; } static int @@ -164,23 +165,14 @@ gpio_open (urj_cable_t *cable) } gpio_direction (gpio, (i == GPIO_TDO) ? 0 : 1); - p->fp_gpios[i] = NULL; snprintf (fname, sizeof (fname), "%sgpio%u/value", GPIO_PATH, gpio); fname[sizeof (fname) - 1] = '\0'; - if (i != GPIO_TDO) - p->fp_gpios[i] = fopen (fname, "w"); - else - p->fp_gpios[i] = fopen (fname, "r"); - if (!p->fp_gpios[i]) + p->fd_gpios[i] = open (fname, O_RDWR); + if (p->fd_gpios[i] < 0) { urj_warning (_("%s: cannot open gpio[%d] %u\n"), fname, i, gpio); return URJ_STATUS_FAIL; } - if (setvbuf(p->fp_gpios[i], (char *)NULL, _IONBF, 0)) - { - urj_warning (_("gpio[%d] %u still in buffered mode\n"), i, gpio); - return URJ_STATUS_FAIL; - } } return URJ_STATUS_OK; @@ -194,8 +186,8 @@ gpio_close (urj_cable_t *cable) for (i = 0; i < GPIO_REQUIRED; i++) { - if (p->fp_gpios[i]) - fclose (p->fp_gpios[i]); + if (p->fd_gpios[i]) + close (p->fd_gpios[i]); gpio_export (p->jtag_gpios[i], 0); } @@ -319,14 +311,14 @@ gpio_clock (urj_cable_t *cable, int tms, int tdi, int n) tms = tms ? 1 : 0; tdi = tdi ? 1 : 0; - gpio_set_value (p->fp_gpios[GPIO_TMS], tms); - gpio_set_value (p->fp_gpios[GPIO_TDI], tdi); + gpio_set_value (p->fd_gpios[GPIO_TMS], tms); + gpio_set_value (p->fd_gpios[GPIO_TDI], tdi); for (i = 0; i < n; i++) { - gpio_set_value (p->fp_gpios[GPIO_TCK], 0); - gpio_set_value (p->fp_gpios[GPIO_TCK], 1); - gpio_set_value (p->fp_gpios[GPIO_TCK], 0); + gpio_set_value (p->fd_gpios[GPIO_TCK], 0); + gpio_set_value (p->fd_gpios[GPIO_TCK], 1); + gpio_set_value (p->fd_gpios[GPIO_TCK], 0); } } @@ -335,14 +327,14 @@ gpio_get_tdo ( urj_cable_t *cable ) { gpio_params_t *p = cable->params; - gpio_set_value(p->fp_gpios[GPIO_TCK], 0); - gpio_set_value(p->fp_gpios[GPIO_TDI], 0); - gpio_set_value(p->fp_gpios[GPIO_TMS], 0); + gpio_set_value(p->fd_gpios[GPIO_TCK], 0); + gpio_set_value(p->fd_gpios[GPIO_TDI], 0); + gpio_set_value(p->fd_gpios[GPIO_TMS], 0); p->lastout &= ~(URJ_POD_CS_TMS | URJ_POD_CS_TDI | URJ_POD_CS_TCK); urj_tap_cable_wait (cable); - return gpio_get_value (p->fp_gpios[GPIO_TDO], p->jtag_gpios[GPIO_TDO]); + return gpio_get_value (p->fd_gpios[GPIO_TDO], p->jtag_gpios[GPIO_TDO]); } static int @@ -370,11 +362,11 @@ gpio_set_signal (urj_cable_t *cable, int mask, int val) if (mask != 0) { if (mask & URJ_POD_CS_TMS) - gpio_set_value (p->fp_gpios[GPIO_TMS], val & URJ_POD_CS_TMS); + gpio_set_value (p->fd_gpios[GPIO_TMS], val & URJ_POD_CS_TMS); if (mask & URJ_POD_CS_TDI) - gpio_set_value (p->fp_gpios[GPIO_TDI], val & URJ_POD_CS_TDI); + gpio_set_value (p->fd_gpios[GPIO_TDI], val & URJ_POD_CS_TDI); if (mask & URJ_POD_CS_TCK) - gpio_set_value (p->fp_gpios[GPIO_TCK], val & URJ_POD_CS_TCK); + gpio_set_value (p->fd_gpios[GPIO_TCK], val & URJ_POD_CS_TCK); } p->lastout = val & mask;