1 /***************************************************************************
2 * Copyright (C) 2013 Paul Fertser <fercerpav@gmail.com> *
3 * Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
19 ***************************************************************************/
22 This is a test application to be used as a remote bitbang server for
23 the OpenOCD remote_bitbang interface driver.
26 gcc -Wall -ansi -pedantic -std=c99 -o remote_bitbang_sysfsgpio remote_bitbang_sysfsgpio.c
32 socat TCP6-LISTEN:7777,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
35 openocd -c "interface remote_bitbang; remote_bitbang_host raspberrypi; remote_bitbang_port 7777" \
36 -f target/stm32f1x.cfg
38 Or if you want to test UNIX sockets, run both on Raspberry Pi:
39 socat UNIX-LISTEN:/tmp/remotebitbang-socket,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
40 openocd -c "interface remote_bitbang; remote_bitbang_host /tmp/remotebitbang-socket" -f target/stm32f1x.cfg
43 #include <sys/types.h>
52 #define LOG_ERROR(...) do { \
53 fprintf(stderr, __VA_ARGS__); \
54 fputc('\n', stderr); \
56 #define LOG_WARNING(...) LOG_ERROR(__VA_ARGS__)
59 #define ERROR_FAIL (-2)
60 #define ERROR_JTAG_INIT_FAILED ERROR_FAIL
63 * Helper func to determine if gpio number valid
65 * Assume here that there will be less than 1000 gpios on a system
67 static int is_gpio_valid(int gpio
)
69 return gpio
>= 0 && gpio
< 1000;
73 * Helper func to open, write to and close a file
74 * name and valstr must be null terminated.
76 * Returns negative on failure.
78 static int open_write_close(const char *name
, const char *valstr
)
81 int fd
= open(name
, O_WRONLY
);
85 ret
= write(fd
, valstr
, strlen(valstr
));
92 * Helper func to unexport gpio from sysfs
94 static void unexport_sysfs_gpio(int gpio
)
98 if (!is_gpio_valid(gpio
))
101 snprintf(gpiostr
, sizeof(gpiostr
), "%d", gpio
);
102 if (open_write_close("/sys/class/gpio/unexport", gpiostr
) < 0)
103 LOG_ERROR("Couldn't unexport gpio %d", gpio
);
109 * Exports and sets up direction for gpio.
110 * If the gpio is an output, it is initialized according to init_high,
111 * otherwise it is ignored.
113 * If the gpio is already exported we just show a warning and continue; if
114 * openocd happened to crash (or was killed by user) then the gpios will not
115 * have been cleaned up.
117 static int setup_sysfs_gpio(int gpio
, int is_output
, int init_high
)
123 if (!is_gpio_valid(gpio
))
126 snprintf(gpiostr
, sizeof(gpiostr
), "%d", gpio
);
127 ret
= open_write_close("/sys/class/gpio/export", gpiostr
);
129 if (errno
== EBUSY
) {
130 LOG_WARNING("gpio %d is already exported", gpio
);
132 LOG_ERROR("Couldn't export gpio %d", gpio
);
133 perror("sysfsgpio: ");
138 snprintf(buf
, sizeof(buf
), "/sys/class/gpio/gpio%d/direction", gpio
);
139 ret
= open_write_close(buf
, is_output
? (init_high
? "high" : "low") : "in");
141 LOG_ERROR("Couldn't set direction for gpio %d", gpio
);
142 perror("sysfsgpio: ");
143 unexport_sysfs_gpio(gpio
);
147 snprintf(buf
, sizeof(buf
), "/sys/class/gpio/gpio%d/value", gpio
);
149 ret
= open(buf
, O_WRONLY
| O_NONBLOCK
| O_SYNC
);
151 ret
= open(buf
, O_RDONLY
| O_NONBLOCK
| O_SYNC
);
154 unexport_sysfs_gpio(gpio
);
160 * file descriptors for /sys/class/gpio/gpioXX/value
161 * Set up during init.
163 static int tck_fd
= -1;
164 static int tms_fd
= -1;
165 static int tdi_fd
= -1;
166 static int tdo_fd
= -1;
167 static int trst_fd
= -1;
168 static int srst_fd
= -1;
171 * Bitbang interface read of TDO
173 * The sysfs value will read back either '0' or '1'. The trick here is to call
174 * lseek to bypass buffering in the sysfs kernel driver.
176 static int sysfsgpio_read(void)
180 /* important to seek to signal sysfs of new read */
181 lseek(tdo_fd
, 0, SEEK_SET
);
182 int ret
= read(tdo_fd
, &buf
, sizeof(buf
));
185 LOG_WARNING("reading tdo failed");
193 * Bitbang interface write of TCK, TMS, TDI
195 * Seeing as this is the only function where the outputs are changed,
196 * we can cache the old value to avoid needlessly writing it.
198 static void sysfsgpio_write(int tck
, int tms
, int tdi
)
200 const char one
[] = "1";
201 const char zero
[] = "0";
207 static int first_time
;
208 size_t bytes_written
;
217 if (tdi
!= last_tdi
) {
218 bytes_written
= write(tdi_fd
, tdi
? &one
: &zero
, 1);
219 if (bytes_written
!= 1)
220 LOG_WARNING("writing tdi failed");
223 if (tms
!= last_tms
) {
224 bytes_written
= write(tms_fd
, tms
? &one
: &zero
, 1);
225 if (bytes_written
!= 1)
226 LOG_WARNING("writing tms failed");
230 if (tck
!= last_tck
) {
231 bytes_written
= write(tck_fd
, tck
? &one
: &zero
, 1);
232 if (bytes_written
!= 1)
233 LOG_WARNING("writing tck failed");
242 * Bitbang interface to manipulate reset lines SRST and TRST
244 * (1) assert or (0) deassert reset lines
246 static void sysfsgpio_reset(int trst
, int srst
)
248 const char one
[] = "1";
249 const char zero
[] = "0";
250 size_t bytes_written
;
252 /* assume active low */
254 bytes_written
= write(srst_fd
, srst
? &zero
: &one
, 1);
255 if (bytes_written
!= 1)
256 LOG_WARNING("writing srst failed");
259 /* assume active low */
261 bytes_written
= write(trst_fd
, trst
? &zero
: &one
, 1);
262 if (bytes_written
!= 1)
263 LOG_WARNING("writing trst failed");
267 /* gpio numbers for each gpio. Negative values are invalid */
268 static int tck_gpio
= -1;
269 static int tms_gpio
= -1;
270 static int tdi_gpio
= -1;
271 static int tdo_gpio
= -1;
272 static int trst_gpio
= -1;
273 static int srst_gpio
= -1;
275 /* helper func to close and cleanup files only if they were valid/ used */
276 static void cleanup_fd(int fd
, int gpio
)
282 unexport_sysfs_gpio(gpio
);
286 static void cleanup_all_fds(void)
288 cleanup_fd(tck_fd
, tck_gpio
);
289 cleanup_fd(tms_fd
, tms_gpio
);
290 cleanup_fd(tdi_fd
, tdi_gpio
);
291 cleanup_fd(tdo_fd
, tdo_gpio
);
292 cleanup_fd(trst_fd
, trst_gpio
);
293 cleanup_fd(srst_fd
, srst_gpio
);
296 static void process_remote_protocol(void)
301 if (c
== EOF
|| c
== 'Q') /* Quit */
303 else if (c
== 'b' || c
== 'B') /* Blink */
305 else if (c
>= 'r' && c
<= 'r' + 2) { /* Reset */
307 sysfsgpio_reset(!!(d
& 2),
309 } else if (c
>= '0' && c
<= '0' + 7) {/* Write */
311 sysfsgpio_write(!!(d
& 4),
315 putchar(sysfsgpio_read());
317 LOG_ERROR("Unknown command '%c' received", c
);
321 int main(int argc
, char *argv
[])
323 LOG_WARNING("SysfsGPIO remote_bitbang JTAG driver\n");
325 for (int i
= 1; i
< argc
; i
++) {
326 if (!strcmp(argv
[i
], "tck"))
327 tck_gpio
= atoi(argv
[++i
]);
328 else if (!strcmp(argv
[i
], "tms"))
329 tms_gpio
= atoi(argv
[++i
]);
330 else if (!strcmp(argv
[i
], "tdo"))
331 tdo_gpio
= atoi(argv
[++i
]);
332 else if (!strcmp(argv
[i
], "tdi"))
333 tdi_gpio
= atoi(argv
[++i
]);
334 else if (!strcmp(argv
[i
], "trst"))
335 trst_gpio
= atoi(argv
[++i
]);
336 else if (!strcmp(argv
[i
], "srst"))
337 srst_gpio
= atoi(argv
[++i
]);
339 LOG_ERROR("Usage:\n%s ((tck|tms|tdo|tdi|trst|srst) num)*", argv
[0]);
344 if (!(is_gpio_valid(tck_gpio
)
345 && is_gpio_valid(tms_gpio
)
346 && is_gpio_valid(tdi_gpio
)
347 && is_gpio_valid(tdo_gpio
))) {
348 if (!is_gpio_valid(tck_gpio
))
349 LOG_ERROR("gpio num for tck is invalid");
350 if (!is_gpio_valid(tms_gpio
))
351 LOG_ERROR("gpio num for tms is invalid");
352 if (!is_gpio_valid(tdo_gpio
))
353 LOG_ERROR("gpio num for tdo is invalid");
354 if (!is_gpio_valid(tdi_gpio
))
355 LOG_ERROR("gpio num for tdi is invalid");
357 LOG_ERROR("Require tck, tms, tdi and tdo gpios to all be specified");
358 return ERROR_JTAG_INIT_FAILED
;
362 * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
363 * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
365 tck_fd
= setup_sysfs_gpio(tck_gpio
, 1, 0);
369 tms_fd
= setup_sysfs_gpio(tms_gpio
, 1, 1);
373 tdi_fd
= setup_sysfs_gpio(tdi_gpio
, 1, 0);
377 tdo_fd
= setup_sysfs_gpio(tdo_gpio
, 0, 0);
381 /* assume active low */
383 trst_fd
= setup_sysfs_gpio(trst_gpio
, 1, 1);
388 /* assume active low */
390 srst_fd
= setup_sysfs_gpio(srst_gpio
, 1, 1);
395 LOG_WARNING("SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
396 tck_gpio
, tms_gpio
, tdi_gpio
, tdo_gpio
);
397 LOG_WARNING("SysfsGPIO num: srst = %d", srst_gpio
);
398 LOG_WARNING("SysfsGPIO num: trst = %d", trst_gpio
);
400 setvbuf(stdout
, NULL
, _IONBF
, 0);
401 process_remote_protocol();
407 return ERROR_JTAG_INIT_FAILED
;