You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

407 lines
10 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2013 Paul Fertser <fercerpav@gmail.com> *
  3. * Copyright (C) 2012 by Creative Product Design, marc @ cpdesign.com.au *
  4. * *
  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. *
  9. * *
  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. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. /*
  19. This is a test application to be used as a remote bitbang server for
  20. the OpenOCD remote_bitbang interface driver.
  21. To compile run:
  22. gcc -Wall -ansi -pedantic -std=c99 -o remote_bitbang_sysfsgpio remote_bitbang_sysfsgpio.c
  23. Usage example:
  24. On Raspberry Pi run:
  25. socat TCP6-LISTEN:7777,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
  26. On host run:
  27. openocd -c "interface remote_bitbang; remote_bitbang host raspberrypi; remote_bitbang port 7777" \
  28. -f target/stm32f1x.cfg
  29. Or if you want to test UNIX sockets, run both on Raspberry Pi:
  30. socat UNIX-LISTEN:/tmp/remotebitbang-socket,fork EXEC:"sudo ./remote_bitbang_sysfsgpio tck 11 tms 25 tdo 9 tdi 10"
  31. openocd -c "interface remote_bitbang; remote_bitbang host /tmp/remotebitbang-socket" -f target/stm32f1x.cfg
  32. */
  33. #include <sys/types.h>
  34. #include <sys/stat.h>
  35. #include <fcntl.h>
  36. #include <unistd.h>
  37. #include <stdlib.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <errno.h>
  41. #define LOG_ERROR(...) do { \
  42. fprintf(stderr, __VA_ARGS__); \
  43. fputc('\n', stderr); \
  44. } while (0)
  45. #define LOG_WARNING(...) LOG_ERROR(__VA_ARGS__)
  46. #define ERROR_OK (-1)
  47. #define ERROR_FAIL (-2)
  48. #define ERROR_JTAG_INIT_FAILED ERROR_FAIL
  49. /*
  50. * Helper func to determine if gpio number valid
  51. *
  52. * Assume here that there will be less than 1000 gpios on a system
  53. */
  54. static int is_gpio_valid(int gpio)
  55. {
  56. return gpio >= 0 && gpio < 1000;
  57. }
  58. /*
  59. * Helper func to open, write to and close a file
  60. * name and valstr must be null terminated.
  61. *
  62. * Returns negative on failure.
  63. */
  64. static int open_write_close(const char *name, const char *valstr)
  65. {
  66. int ret;
  67. int fd = open(name, O_WRONLY);
  68. if (fd < 0)
  69. return fd;
  70. ret = write(fd, valstr, strlen(valstr));
  71. close(fd);
  72. return ret;
  73. }
  74. /*
  75. * Helper func to unexport gpio from sysfs
  76. */
  77. static void unexport_sysfs_gpio(int gpio)
  78. {
  79. char gpiostr[4];
  80. if (!is_gpio_valid(gpio))
  81. return;
  82. snprintf(gpiostr, sizeof(gpiostr), "%d", gpio);
  83. if (open_write_close("/sys/class/gpio/unexport", gpiostr) < 0)
  84. LOG_ERROR("Couldn't unexport gpio %d", gpio);
  85. return;
  86. }
  87. /*
  88. * Exports and sets up direction for gpio.
  89. * If the gpio is an output, it is initialized according to init_high,
  90. * otherwise it is ignored.
  91. *
  92. * If the gpio is already exported we just show a warning and continue; if
  93. * openocd happened to crash (or was killed by user) then the gpios will not
  94. * have been cleaned up.
  95. */
  96. static int setup_sysfs_gpio(int gpio, int is_output, int init_high)
  97. {
  98. char buf[40];
  99. char gpiostr[4];
  100. int ret;
  101. if (!is_gpio_valid(gpio))
  102. return ERROR_OK;
  103. snprintf(gpiostr, sizeof(gpiostr), "%d", gpio);
  104. ret = open_write_close("/sys/class/gpio/export", gpiostr);
  105. if (ret < 0) {
  106. if (errno == EBUSY) {
  107. LOG_WARNING("gpio %d is already exported", gpio);
  108. } else {
  109. LOG_ERROR("Couldn't export gpio %d", gpio);
  110. perror("sysfsgpio: ");
  111. return ERROR_FAIL;
  112. }
  113. }
  114. snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/direction", gpio);
  115. ret = open_write_close(buf, is_output ? (init_high ? "high" : "low") : "in");
  116. if (ret < 0) {
  117. LOG_ERROR("Couldn't set direction for gpio %d", gpio);
  118. perror("sysfsgpio: ");
  119. unexport_sysfs_gpio(gpio);
  120. return ERROR_FAIL;
  121. }
  122. snprintf(buf, sizeof(buf), "/sys/class/gpio/gpio%d/value", gpio);
  123. if (is_output)
  124. ret = open(buf, O_WRONLY | O_NONBLOCK | O_SYNC);
  125. else
  126. ret = open(buf, O_RDONLY | O_NONBLOCK | O_SYNC);
  127. if (ret < 0)
  128. unexport_sysfs_gpio(gpio);
  129. return ret;
  130. }
  131. /*
  132. * file descriptors for /sys/class/gpio/gpioXX/value
  133. * Set up during init.
  134. */
  135. static int tck_fd = -1;
  136. static int tms_fd = -1;
  137. static int tdi_fd = -1;
  138. static int tdo_fd = -1;
  139. static int trst_fd = -1;
  140. static int srst_fd = -1;
  141. /*
  142. * Bitbang interface read of TDO
  143. *
  144. * The sysfs value will read back either '0' or '1'. The trick here is to call
  145. * lseek to bypass buffering in the sysfs kernel driver.
  146. */
  147. static int sysfsgpio_read(void)
  148. {
  149. char buf[1];
  150. /* important to seek to signal sysfs of new read */
  151. lseek(tdo_fd, 0, SEEK_SET);
  152. int ret = read(tdo_fd, &buf, sizeof(buf));
  153. if (ret < 0) {
  154. LOG_WARNING("reading tdo failed");
  155. return 0;
  156. }
  157. return buf[0];
  158. }
  159. /*
  160. * Bitbang interface write of TCK, TMS, TDI
  161. *
  162. * Seeing as this is the only function where the outputs are changed,
  163. * we can cache the old value to avoid needlessly writing it.
  164. */
  165. static void sysfsgpio_write(int tck, int tms, int tdi)
  166. {
  167. const char one[] = "1";
  168. const char zero[] = "0";
  169. static int last_tck;
  170. static int last_tms;
  171. static int last_tdi;
  172. static int first_time;
  173. size_t bytes_written;
  174. if (!first_time) {
  175. last_tck = !tck;
  176. last_tms = !tms;
  177. last_tdi = !tdi;
  178. first_time = 1;
  179. }
  180. if (tdi != last_tdi) {
  181. bytes_written = write(tdi_fd, tdi ? &one : &zero, 1);
  182. if (bytes_written != 1)
  183. LOG_WARNING("writing tdi failed");
  184. }
  185. if (tms != last_tms) {
  186. bytes_written = write(tms_fd, tms ? &one : &zero, 1);
  187. if (bytes_written != 1)
  188. LOG_WARNING("writing tms failed");
  189. }
  190. /* write clk last */
  191. if (tck != last_tck) {
  192. bytes_written = write(tck_fd, tck ? &one : &zero, 1);
  193. if (bytes_written != 1)
  194. LOG_WARNING("writing tck failed");
  195. }
  196. last_tdi = tdi;
  197. last_tms = tms;
  198. last_tck = tck;
  199. }
  200. /*
  201. * Bitbang interface to manipulate reset lines SRST and TRST
  202. *
  203. * (1) assert or (0) deassert reset lines
  204. */
  205. static void sysfsgpio_reset(int trst, int srst)
  206. {
  207. const char one[] = "1";
  208. const char zero[] = "0";
  209. size_t bytes_written;
  210. /* assume active low */
  211. if (srst_fd >= 0) {
  212. bytes_written = write(srst_fd, srst ? &zero : &one, 1);
  213. if (bytes_written != 1)
  214. LOG_WARNING("writing srst failed");
  215. }
  216. /* assume active low */
  217. if (trst_fd >= 0) {
  218. bytes_written = write(trst_fd, trst ? &zero : &one, 1);
  219. if (bytes_written != 1)
  220. LOG_WARNING("writing trst failed");
  221. }
  222. }
  223. /* gpio numbers for each gpio. Negative values are invalid */
  224. static int tck_gpio = -1;
  225. static int tms_gpio = -1;
  226. static int tdi_gpio = -1;
  227. static int tdo_gpio = -1;
  228. static int trst_gpio = -1;
  229. static int srst_gpio = -1;
  230. /* helper func to close and cleanup files only if they were valid/ used */
  231. static void cleanup_fd(int fd, int gpio)
  232. {
  233. if (gpio >= 0) {
  234. if (fd >= 0)
  235. close(fd);
  236. unexport_sysfs_gpio(gpio);
  237. }
  238. }
  239. static void cleanup_all_fds(void)
  240. {
  241. cleanup_fd(tck_fd, tck_gpio);
  242. cleanup_fd(tms_fd, tms_gpio);
  243. cleanup_fd(tdi_fd, tdi_gpio);
  244. cleanup_fd(tdo_fd, tdo_gpio);
  245. cleanup_fd(trst_fd, trst_gpio);
  246. cleanup_fd(srst_fd, srst_gpio);
  247. }
  248. static void process_remote_protocol(void)
  249. {
  250. int c;
  251. while (1) {
  252. c = getchar();
  253. if (c == EOF || c == 'Q') /* Quit */
  254. break;
  255. else if (c == 'b' || c == 'B') /* Blink */
  256. continue;
  257. else if (c >= 'r' && c <= 'r' + 3) { /* Reset */
  258. char d = c - 'r';
  259. sysfsgpio_reset(!!(d & 2),
  260. (d & 1));
  261. } else if (c >= '0' && c <= '0' + 7) {/* Write */
  262. char d = c - '0';
  263. sysfsgpio_write(!!(d & 4),
  264. !!(d & 2),
  265. (d & 1));
  266. } else if (c == 'R')
  267. putchar(sysfsgpio_read());
  268. else
  269. LOG_ERROR("Unknown command '%c' received", c);
  270. }
  271. }
  272. int main(int argc, char *argv[])
  273. {
  274. LOG_WARNING("SysfsGPIO remote_bitbang JTAG driver\n");
  275. for (int i = 1; i < argc; i++) {
  276. if (!strcmp(argv[i], "tck"))
  277. tck_gpio = atoi(argv[++i]);
  278. else if (!strcmp(argv[i], "tms"))
  279. tms_gpio = atoi(argv[++i]);
  280. else if (!strcmp(argv[i], "tdo"))
  281. tdo_gpio = atoi(argv[++i]);
  282. else if (!strcmp(argv[i], "tdi"))
  283. tdi_gpio = atoi(argv[++i]);
  284. else if (!strcmp(argv[i], "trst"))
  285. trst_gpio = atoi(argv[++i]);
  286. else if (!strcmp(argv[i], "srst"))
  287. srst_gpio = atoi(argv[++i]);
  288. else {
  289. LOG_ERROR("Usage:\n%s ((tck|tms|tdo|tdi|trst|srst) num)*", argv[0]);
  290. return -1;
  291. }
  292. }
  293. if (!(is_gpio_valid(tck_gpio)
  294. && is_gpio_valid(tms_gpio)
  295. && is_gpio_valid(tdi_gpio)
  296. && is_gpio_valid(tdo_gpio))) {
  297. if (!is_gpio_valid(tck_gpio))
  298. LOG_ERROR("gpio num for tck is invalid");
  299. if (!is_gpio_valid(tms_gpio))
  300. LOG_ERROR("gpio num for tms is invalid");
  301. if (!is_gpio_valid(tdo_gpio))
  302. LOG_ERROR("gpio num for tdo is invalid");
  303. if (!is_gpio_valid(tdi_gpio))
  304. LOG_ERROR("gpio num for tdi is invalid");
  305. LOG_ERROR("Require tck, tms, tdi and tdo gpios to all be specified");
  306. return ERROR_JTAG_INIT_FAILED;
  307. }
  308. /*
  309. * Configure TDO as an input, and TDI, TCK, TMS, TRST, SRST
  310. * as outputs. Drive TDI and TCK low, and TMS/TRST/SRST high.
  311. */
  312. tck_fd = setup_sysfs_gpio(tck_gpio, 1, 0);
  313. if (tck_fd < 0)
  314. goto out_error;
  315. tms_fd = setup_sysfs_gpio(tms_gpio, 1, 1);
  316. if (tms_fd < 0)
  317. goto out_error;
  318. tdi_fd = setup_sysfs_gpio(tdi_gpio, 1, 0);
  319. if (tdi_fd < 0)
  320. goto out_error;
  321. tdo_fd = setup_sysfs_gpio(tdo_gpio, 0, 0);
  322. if (tdo_fd < 0)
  323. goto out_error;
  324. /* assume active low */
  325. if (trst_gpio > 0) {
  326. trst_fd = setup_sysfs_gpio(trst_gpio, 1, 1);
  327. if (trst_fd < 0)
  328. goto out_error;
  329. }
  330. /* assume active low */
  331. if (srst_gpio > 0) {
  332. srst_fd = setup_sysfs_gpio(srst_gpio, 1, 1);
  333. if (srst_fd < 0)
  334. goto out_error;
  335. }
  336. LOG_WARNING("SysfsGPIO nums: tck = %d, tms = %d, tdi = %d, tdo = %d",
  337. tck_gpio, tms_gpio, tdi_gpio, tdo_gpio);
  338. LOG_WARNING("SysfsGPIO num: srst = %d", srst_gpio);
  339. LOG_WARNING("SysfsGPIO num: trst = %d", trst_gpio);
  340. setvbuf(stdout, NULL, _IONBF, 0);
  341. process_remote_protocol();
  342. cleanup_all_fds();
  343. return 0;
  344. out_error:
  345. cleanup_all_fds();
  346. return ERROR_JTAG_INIT_FAILED;
  347. }