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.
 
 
 
 
 
 

984 lines
23 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2010 by Michal Demin *
  3. * based on usbprog.c and arm-jtag-ew.c *
  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, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include <jtag/interface.h>
  24. #include <jtag/commands.h>
  25. #include <termios.h>
  26. #include <fcntl.h>
  27. #include <sys/ioctl.h>
  28. #undef DEBUG_SERIAL
  29. /*#define DEBUG_SERIAL */
  30. static int buspirate_execute_queue(void);
  31. static int buspirate_speed(int speed);
  32. static int buspirate_khz(int khz, int *jtag_speed);
  33. static int buspirate_init(void);
  34. static int buspirate_quit(void);
  35. static void buspirate_end_state(tap_state_t state);
  36. static void buspirate_state_move(void);
  37. static void buspirate_path_move(int num_states, tap_state_t *path);
  38. static void buspirate_runtest(int num_cycles);
  39. static void buspirate_scan(bool ir_scan, enum scan_type type,
  40. uint8_t *buffer, int scan_size, struct scan_command *command);
  41. #define CMD_UNKNOWN 0x00
  42. #define CMD_PORT_MODE 0x01
  43. #define CMD_FEATURE 0x02
  44. #define CMD_READ_ADCS 0x03
  45. /*#define CMD_TAP_SHIFT 0x04 // old protocol */
  46. #define CMD_TAP_SHIFT 0x05
  47. #define CMD_ENTER_OOCD 0x06
  48. #define CMD_UART_SPEED 0x07
  49. #define CMD_JTAG_SPEED 0x08
  50. /* Not all OSes have this speed defined */
  51. #if !defined(B1000000)
  52. #define B1000000 0010010
  53. #endif
  54. enum {
  55. MODE_HIZ = 0,
  56. MODE_JTAG = 1, /* push-pull outputs */
  57. MODE_JTAG_OD = 2, /* open-drain outputs */
  58. };
  59. enum {
  60. FEATURE_LED = 0x01,
  61. FEATURE_VREG = 0x02,
  62. FEATURE_TRST = 0x04,
  63. FEATURE_SRST = 0x08,
  64. FEATURE_PULLUP = 0x10
  65. };
  66. enum {
  67. ACTION_DISABLE = 0,
  68. ACTION_ENABLE = 1
  69. };
  70. enum {
  71. SERIAL_NORMAL = 0,
  72. SERIAL_FAST = 1
  73. };
  74. static int buspirate_fd = -1;
  75. static int buspirate_pinmode = MODE_JTAG_OD;
  76. static int buspirate_baudrate = SERIAL_NORMAL;
  77. static int buspirate_vreg;
  78. static int buspirate_pullup;
  79. static char *buspirate_port;
  80. /* TAP interface */
  81. static void buspirate_tap_init(void);
  82. static int buspirate_tap_execute(void);
  83. static void buspirate_tap_append(int tms, int tdi);
  84. static void buspirate_tap_append_scan(int length, uint8_t *buffer,
  85. struct scan_command *command);
  86. static void buspirate_tap_make_space(int scan, int bits);
  87. static void buspirate_reset(int trst, int srst);
  88. /* low level interface */
  89. static void buspirate_jtag_reset(int);
  90. static void buspirate_jtag_enable(int);
  91. static unsigned char buspirate_jtag_command(int, char *, int);
  92. static void buspirate_jtag_set_speed(int, char);
  93. static void buspirate_jtag_set_mode(int, char);
  94. static void buspirate_jtag_set_feature(int, char, char);
  95. static void buspirate_jtag_get_adcs(int);
  96. /* low level HW communication interface */
  97. static int buspirate_serial_open(char *port);
  98. static int buspirate_serial_setspeed(int fd, char speed);
  99. static int buspirate_serial_write(int fd, char *buf, int size);
  100. static int buspirate_serial_read(int fd, char *buf, int size);
  101. static void buspirate_serial_close(int fd);
  102. static void buspirate_print_buffer(char *buf, int size);
  103. static int buspirate_speed(int speed)
  104. {
  105. /* TODO */
  106. LOG_INFO("Want to set speed to %dkHz, but not implemented yet", speed);
  107. return ERROR_OK;
  108. }
  109. static int buspirate_khz(int khz, int *jtag_speed)
  110. {
  111. *jtag_speed = khz;
  112. return ERROR_OK;
  113. }
  114. static int buspirate_execute_queue(void)
  115. {
  116. /* currently processed command */
  117. struct jtag_command *cmd = jtag_command_queue;
  118. int scan_size;
  119. enum scan_type type;
  120. uint8_t *buffer;
  121. while (cmd) {
  122. switch (cmd->type) {
  123. case JTAG_RUNTEST:
  124. DEBUG_JTAG_IO("runtest %i cycles, end in %s",
  125. cmd->cmd.runtest->num_cycles,
  126. tap_state_name(cmd->cmd.runtest
  127. ->end_state));
  128. buspirate_end_state(cmd->cmd.runtest
  129. ->end_state);
  130. buspirate_runtest(cmd->cmd.runtest
  131. ->num_cycles);
  132. break;
  133. case JTAG_TLR_RESET:
  134. DEBUG_JTAG_IO("statemove end in %s",
  135. tap_state_name(cmd->cmd.statemove
  136. ->end_state));
  137. buspirate_end_state(cmd->cmd.statemove
  138. ->end_state);
  139. buspirate_state_move();
  140. break;
  141. case JTAG_PATHMOVE:
  142. DEBUG_JTAG_IO("pathmove: %i states, end in %s",
  143. cmd->cmd.pathmove->num_states,
  144. tap_state_name(cmd->cmd.pathmove
  145. ->path[cmd->cmd.pathmove
  146. ->num_states - 1]));
  147. buspirate_path_move(cmd->cmd.pathmove
  148. ->num_states,
  149. cmd->cmd.pathmove->path);
  150. break;
  151. case JTAG_SCAN:
  152. DEBUG_JTAG_IO("scan end in %s",
  153. tap_state_name(cmd->cmd.scan
  154. ->end_state));
  155. buspirate_end_state(cmd->cmd.scan
  156. ->end_state);
  157. scan_size = jtag_build_buffer(cmd->cmd.scan,
  158. &buffer);
  159. type = jtag_scan_type(cmd->cmd.scan);
  160. buspirate_scan(cmd->cmd.scan->ir_scan, type,
  161. buffer, scan_size, cmd->cmd.scan);
  162. break;
  163. case JTAG_RESET:
  164. DEBUG_JTAG_IO("reset trst: %i srst %i",
  165. cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  166. /* flush buffers, so we can reset */
  167. buspirate_tap_execute();
  168. if (cmd->cmd.reset->trst == 1)
  169. tap_set_state(TAP_RESET);
  170. buspirate_reset(cmd->cmd.reset->trst,
  171. cmd->cmd.reset->srst);
  172. break;
  173. case JTAG_SLEEP:
  174. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  175. buspirate_tap_execute();
  176. jtag_sleep(cmd->cmd.sleep->us);
  177. break;
  178. default:
  179. LOG_ERROR("BUG: unknown JTAG command type encountered");
  180. exit(-1);
  181. }
  182. cmd = cmd->next;
  183. }
  184. return buspirate_tap_execute();
  185. }
  186. static int buspirate_init(void)
  187. {
  188. if (buspirate_port == NULL) {
  189. LOG_ERROR("You need to specify the serial port!");
  190. return ERROR_JTAG_INIT_FAILED;
  191. }
  192. buspirate_fd = buspirate_serial_open(buspirate_port);
  193. if (buspirate_fd == -1) {
  194. LOG_ERROR("Could not open serial port");
  195. return ERROR_JTAG_INIT_FAILED;
  196. }
  197. buspirate_serial_setspeed(buspirate_fd, SERIAL_NORMAL);
  198. buspirate_jtag_enable(buspirate_fd);
  199. if (buspirate_baudrate != SERIAL_NORMAL)
  200. buspirate_jtag_set_speed(buspirate_fd, SERIAL_FAST);
  201. LOG_INFO("Buspirate Interface ready!");
  202. buspirate_tap_init();
  203. buspirate_jtag_set_mode(buspirate_fd, buspirate_pinmode);
  204. buspirate_jtag_set_feature(buspirate_fd, FEATURE_VREG,
  205. (buspirate_vreg == 1) ? ACTION_ENABLE : ACTION_DISABLE);
  206. buspirate_jtag_set_feature(buspirate_fd, FEATURE_PULLUP,
  207. (buspirate_pullup == 1) ? ACTION_ENABLE : ACTION_DISABLE);
  208. buspirate_reset(0, 0);
  209. return ERROR_OK;
  210. }
  211. static int buspirate_quit(void)
  212. {
  213. LOG_INFO("Shutting down buspirate.");
  214. buspirate_jtag_set_mode(buspirate_fd, MODE_HIZ);
  215. buspirate_jtag_set_speed(buspirate_fd, SERIAL_NORMAL);
  216. buspirate_jtag_reset(buspirate_fd);
  217. buspirate_serial_close(buspirate_fd);
  218. if (buspirate_port) {
  219. free(buspirate_port);
  220. buspirate_port = NULL;
  221. }
  222. return ERROR_OK;
  223. }
  224. /* openocd command interface */
  225. COMMAND_HANDLER(buspirate_handle_adc_command)
  226. {
  227. if (buspirate_fd == -1)
  228. return ERROR_OK;
  229. /* send the command */
  230. buspirate_jtag_get_adcs(buspirate_fd);
  231. return ERROR_OK;
  232. }
  233. COMMAND_HANDLER(buspirate_handle_vreg_command)
  234. {
  235. if (CMD_ARGC < 1)
  236. return ERROR_COMMAND_SYNTAX_ERROR;
  237. if (atoi(CMD_ARGV[0]) == 1)
  238. buspirate_vreg = 1;
  239. else if (atoi(CMD_ARGV[0]) == 0)
  240. buspirate_vreg = 0;
  241. else
  242. LOG_ERROR("usage: buspirate_vreg <1|0>");
  243. return ERROR_OK;
  244. }
  245. COMMAND_HANDLER(buspirate_handle_pullup_command)
  246. {
  247. if (CMD_ARGC < 1)
  248. return ERROR_COMMAND_SYNTAX_ERROR;
  249. if (atoi(CMD_ARGV[0]) == 1)
  250. buspirate_pullup = 1;
  251. else if (atoi(CMD_ARGV[0]) == 0)
  252. buspirate_pullup = 0;
  253. else
  254. LOG_ERROR("usage: buspirate_pullup <1|0>");
  255. return ERROR_OK;
  256. }
  257. COMMAND_HANDLER(buspirate_handle_led_command)
  258. {
  259. if (CMD_ARGC < 1)
  260. return ERROR_COMMAND_SYNTAX_ERROR;
  261. if (atoi(CMD_ARGV[0]) == 1) {
  262. /* enable led */
  263. buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
  264. ACTION_ENABLE);
  265. } else if (atoi(CMD_ARGV[0]) == 0) {
  266. /* disable led */
  267. buspirate_jtag_set_feature(buspirate_fd, FEATURE_LED,
  268. ACTION_DISABLE);
  269. } else {
  270. LOG_ERROR("usage: buspirate_led <1|0>");
  271. }
  272. return ERROR_OK;
  273. }
  274. COMMAND_HANDLER(buspirate_handle_mode_command)
  275. {
  276. if (CMD_ARGC < 1)
  277. return ERROR_COMMAND_SYNTAX_ERROR;
  278. if (CMD_ARGV[0][0] == 'n')
  279. buspirate_pinmode = MODE_JTAG;
  280. else if (CMD_ARGV[0][0] == 'o')
  281. buspirate_pinmode = MODE_JTAG_OD;
  282. else
  283. LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
  284. return ERROR_OK;
  285. }
  286. COMMAND_HANDLER(buspirate_handle_speed_command)
  287. {
  288. if (CMD_ARGC < 1)
  289. return ERROR_COMMAND_SYNTAX_ERROR;
  290. if (CMD_ARGV[0][0] == 'n')
  291. buspirate_baudrate = SERIAL_NORMAL;
  292. else if (CMD_ARGV[0][0] == 'f')
  293. buspirate_baudrate = SERIAL_FAST;
  294. else
  295. LOG_ERROR("usage: buspirate_speed <normal|fast>");
  296. return ERROR_OK;
  297. }
  298. COMMAND_HANDLER(buspirate_handle_port_command)
  299. {
  300. if (CMD_ARGC < 1)
  301. return ERROR_COMMAND_SYNTAX_ERROR;
  302. if (buspirate_port == NULL)
  303. buspirate_port = strdup(CMD_ARGV[0]);
  304. return ERROR_OK;
  305. }
  306. static const struct command_registration buspirate_command_handlers[] = {
  307. {
  308. .name = "buspirate_adc",
  309. .handler = &buspirate_handle_adc_command,
  310. .mode = COMMAND_EXEC,
  311. .help = "reads voltages on adc pins",
  312. },
  313. {
  314. .name = "buspirate_vreg",
  315. .usage = "<1|0>",
  316. .handler = &buspirate_handle_vreg_command,
  317. .mode = COMMAND_CONFIG,
  318. .help = "changes the state of voltage regulators",
  319. },
  320. {
  321. .name = "buspirate_pullup",
  322. .usage = "<1|0>",
  323. .handler = &buspirate_handle_pullup_command,
  324. .mode = COMMAND_CONFIG,
  325. .help = "changes the state of pullup",
  326. },
  327. {
  328. .name = "buspirate_led",
  329. .usage = "<1|0>",
  330. .handler = &buspirate_handle_led_command,
  331. .mode = COMMAND_EXEC,
  332. .help = "changes the state of led",
  333. },
  334. {
  335. .name = "buspirate_speed",
  336. .usage = "<normal|fast>",
  337. .handler = &buspirate_handle_speed_command,
  338. .mode = COMMAND_CONFIG,
  339. .help = "speed of the interface",
  340. },
  341. {
  342. .name = "buspirate_mode",
  343. .usage = "<normal|open-drain>",
  344. .handler = &buspirate_handle_mode_command,
  345. .mode = COMMAND_CONFIG,
  346. .help = "pin mode of the interface",
  347. },
  348. {
  349. .name = "buspirate_port",
  350. .usage = "/dev/ttyUSB0",
  351. .handler = &buspirate_handle_port_command,
  352. .mode = COMMAND_CONFIG,
  353. .help = "name of the serial port to open",
  354. },
  355. COMMAND_REGISTRATION_DONE
  356. };
  357. struct jtag_interface buspirate_interface = {
  358. .name = "buspirate",
  359. .execute_queue = buspirate_execute_queue,
  360. .speed = buspirate_speed,
  361. .khz = buspirate_khz,
  362. .commands = buspirate_command_handlers,
  363. .init = buspirate_init,
  364. .quit = buspirate_quit
  365. };
  366. /*************** jtag execute commands **********************/
  367. static void buspirate_end_state(tap_state_t state)
  368. {
  369. if (tap_is_state_stable(state))
  370. tap_set_end_state(state);
  371. else {
  372. LOG_ERROR("BUG: %i is not a valid end state", state);
  373. exit(-1);
  374. }
  375. }
  376. static void buspirate_state_move(void)
  377. {
  378. int i = 0, tms = 0;
  379. uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
  380. tap_get_end_state());
  381. int tms_count = tap_get_tms_path_len(tap_get_state(),
  382. tap_get_end_state());
  383. for (i = 0; i < tms_count; i++) {
  384. tms = (tms_scan >> i) & 1;
  385. buspirate_tap_append(tms, 0);
  386. }
  387. tap_set_state(tap_get_end_state());
  388. }
  389. static void buspirate_path_move(int num_states, tap_state_t *path)
  390. {
  391. int i;
  392. for (i = 0; i < num_states; i++) {
  393. if (tap_state_transition(tap_get_state(), false) == path[i]) {
  394. buspirate_tap_append(0, 0);
  395. } else if (tap_state_transition(tap_get_state(), true)
  396. == path[i]) {
  397. buspirate_tap_append(1, 0);
  398. } else {
  399. LOG_ERROR("BUG: %s -> %s isn't a valid "
  400. "TAP transition",
  401. tap_state_name(tap_get_state()),
  402. tap_state_name(path[i]));
  403. exit(-1);
  404. }
  405. tap_set_state(path[i]);
  406. }
  407. tap_set_end_state(tap_get_state());
  408. }
  409. static void buspirate_runtest(int num_cycles)
  410. {
  411. int i;
  412. tap_state_t saved_end_state = tap_get_end_state();
  413. /* only do a state_move when we're not already in IDLE */
  414. if (tap_get_state() != TAP_IDLE) {
  415. buspirate_end_state(TAP_IDLE);
  416. buspirate_state_move();
  417. }
  418. for (i = 0; i < num_cycles; i++)
  419. buspirate_tap_append(0, 0);
  420. DEBUG_JTAG_IO("runtest: cur_state %s end_state %s",
  421. tap_state_name(tap_get_state()),
  422. tap_state_name(tap_get_end_state()));
  423. /* finish in end_state */
  424. buspirate_end_state(saved_end_state);
  425. if (tap_get_state() != tap_get_end_state())
  426. buspirate_state_move();
  427. }
  428. static void buspirate_scan(bool ir_scan, enum scan_type type,
  429. uint8_t *buffer, int scan_size, struct scan_command *command)
  430. {
  431. tap_state_t saved_end_state;
  432. buspirate_tap_make_space(1, scan_size+8);
  433. /* is 8 correct ? (2 moves = 16) */
  434. saved_end_state = tap_get_end_state();
  435. buspirate_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  436. buspirate_state_move();
  437. buspirate_tap_append_scan(scan_size, buffer, command);
  438. /* move to PAUSE */
  439. buspirate_tap_append(0, 0);
  440. /* restore the saved state */
  441. buspirate_end_state(saved_end_state);
  442. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  443. if (tap_get_state() != tap_get_end_state())
  444. buspirate_state_move();
  445. }
  446. /************************* TAP related stuff **********/
  447. #define BUSPIRATE_BUFFER_SIZE 1024
  448. #define BUSPIRATE_MAX_PENDING_SCANS 32
  449. static char tms_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
  450. static char tdi_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
  451. static int tap_chain_index;
  452. struct pending_scan_result /* this was stolen from arm-jtag-ew */
  453. {
  454. int first; /* First bit position in tdo_buffer to read */
  455. int length; /* Number of bits to read */
  456. struct scan_command *command; /* Corresponding scan command */
  457. uint8_t *buffer;
  458. };
  459. static struct pending_scan_result
  460. tap_pending_scans[BUSPIRATE_MAX_PENDING_SCANS];
  461. static int tap_pending_scans_num;
  462. static void buspirate_tap_init(void)
  463. {
  464. tap_chain_index = 0;
  465. tap_pending_scans_num = 0;
  466. }
  467. static int buspirate_tap_execute(void)
  468. {
  469. char tmp[4096];
  470. uint8_t *in_buf;
  471. int i;
  472. int fill_index = 0;
  473. int ret;
  474. int bytes_to_send;
  475. if (tap_chain_index <= 0)
  476. return ERROR_OK;
  477. LOG_DEBUG("executing tap num bits = %i scans = %i",
  478. tap_chain_index, tap_pending_scans_num);
  479. bytes_to_send = (tap_chain_index+7) / 8;
  480. tmp[0] = CMD_TAP_SHIFT; /* this command expects number of bits */
  481. tmp[1] = (char)(tap_chain_index >> 8); /* high */
  482. tmp[2] = (char)(tap_chain_index); /* low */
  483. fill_index = 3;
  484. for (i = 0; i < bytes_to_send; i++) {
  485. tmp[fill_index] = tdi_chain[i];
  486. fill_index++;
  487. tmp[fill_index] = tms_chain[i];
  488. fill_index++;
  489. }
  490. ret = buspirate_serial_write(buspirate_fd, tmp, 3 + bytes_to_send*2);
  491. if (ret != bytes_to_send*2+3) {
  492. LOG_ERROR("error writing :(");
  493. return ERROR_JTAG_DEVICE_ERROR;
  494. }
  495. ret = buspirate_serial_read(buspirate_fd, tmp, bytes_to_send + 3);
  496. if (ret != bytes_to_send + 3) {
  497. LOG_ERROR("error reading");
  498. return ERROR_FAIL;
  499. }
  500. in_buf = (uint8_t *)(&tmp[3]);
  501. /* parse the scans */
  502. for (i = 0; i < tap_pending_scans_num; i++) {
  503. uint8_t *buffer = tap_pending_scans[i].buffer;
  504. int length = tap_pending_scans[i].length;
  505. int first = tap_pending_scans[i].first;
  506. struct scan_command *command = tap_pending_scans[i].command;
  507. /* copy bits from buffer */
  508. buf_set_buf(in_buf, first, buffer, 0, length);
  509. /* return buffer to higher level */
  510. if (jtag_read_buffer(buffer, command) != ERROR_OK) {
  511. buspirate_tap_init();
  512. return ERROR_JTAG_QUEUE_FAILED;
  513. }
  514. free(buffer);
  515. }
  516. tap_pending_scans_num = 0;
  517. tap_chain_index = 0;
  518. return ERROR_OK;
  519. }
  520. static void buspirate_tap_make_space(int scans, int bits)
  521. {
  522. int have_scans = BUSPIRATE_MAX_PENDING_SCANS - tap_pending_scans_num;
  523. int have_bits = BUSPIRATE_BUFFER_SIZE * 8 - tap_chain_index;
  524. if ((have_scans < scans) || (have_bits < bits))
  525. buspirate_tap_execute();
  526. }
  527. static void buspirate_tap_append(int tms, int tdi)
  528. {
  529. int chain_index;
  530. buspirate_tap_make_space(0, 1);
  531. chain_index = tap_chain_index / 8;
  532. if (chain_index < BUSPIRATE_BUFFER_SIZE) {
  533. int bit_index = tap_chain_index % 8;
  534. uint8_t bit = 1 << bit_index;
  535. if (tms)
  536. tms_chain[chain_index] |= bit;
  537. else
  538. tms_chain[chain_index] &= ~bit;
  539. if (tdi)
  540. tdi_chain[chain_index] |= bit;
  541. else
  542. tdi_chain[chain_index] &= ~bit;
  543. tap_chain_index++;
  544. } else
  545. LOG_ERROR("tap_chain overflow, bad things will happen");
  546. }
  547. static void buspirate_tap_append_scan(int length, uint8_t *buffer,
  548. struct scan_command *command)
  549. {
  550. int i;
  551. tap_pending_scans[tap_pending_scans_num].length = length;
  552. tap_pending_scans[tap_pending_scans_num].buffer = buffer;
  553. tap_pending_scans[tap_pending_scans_num].command = command;
  554. tap_pending_scans[tap_pending_scans_num].first = tap_chain_index;
  555. for (i = 0; i < length; i++) {
  556. int tms = (i < length-1 ? 0 : 1);
  557. int tdi = (buffer[i/8] >> (i%8)) & 1;
  558. buspirate_tap_append(tms, tdi);
  559. }
  560. tap_pending_scans_num++;
  561. }
  562. /*************** jtag wrapper functions *********************/
  563. /* (1) assert or (0) deassert reset lines */
  564. static void buspirate_reset(int trst, int srst)
  565. {
  566. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  567. if (trst)
  568. buspirate_jtag_set_feature(buspirate_fd,
  569. FEATURE_TRST, ACTION_DISABLE);
  570. else
  571. buspirate_jtag_set_feature(buspirate_fd,
  572. FEATURE_TRST, ACTION_ENABLE);
  573. if (srst)
  574. buspirate_jtag_set_feature(buspirate_fd,
  575. FEATURE_SRST, ACTION_DISABLE);
  576. else
  577. buspirate_jtag_set_feature(buspirate_fd,
  578. FEATURE_SRST, ACTION_ENABLE);
  579. }
  580. /*************** jtag lowlevel functions ********************/
  581. static void buspirate_jtag_enable(int fd)
  582. {
  583. int ret;
  584. char tmp[21] = { [0 ... 20] = 0x00 };
  585. int done = 0;
  586. int cmd_sent = 0;
  587. LOG_DEBUG("Entering binary mode");
  588. buspirate_serial_write(fd, tmp, 20);
  589. usleep(10000);
  590. /* reads 1 to n "BBIO1"s and one "OCD1" */
  591. while (!done) {
  592. ret = buspirate_serial_read(fd, tmp, 4);
  593. if (ret != 4) {
  594. LOG_ERROR("Buspirate error. Is binary"
  595. "/OpenOCD support enabled?");
  596. exit(-1);
  597. }
  598. if (strncmp(tmp, "BBIO", 4) == 0) {
  599. ret = buspirate_serial_read(fd, tmp, 1);
  600. if (ret != 1) {
  601. LOG_ERROR("Buspirate did not answer correctly! "
  602. "Do you have correct firmware?");
  603. exit(-1);
  604. }
  605. if (tmp[0] != '1') {
  606. LOG_ERROR("Unsupported binary protocol");
  607. exit(-1);
  608. }
  609. if (cmd_sent == 0) {
  610. cmd_sent = 1;
  611. tmp[0] = CMD_ENTER_OOCD;
  612. ret = buspirate_serial_write(fd, tmp, 1);
  613. if (ret != 1) {
  614. LOG_ERROR("error reading");
  615. exit(-1);
  616. }
  617. }
  618. } else if (strncmp(tmp, "OCD1", 4) == 0)
  619. done = 1;
  620. else {
  621. LOG_ERROR("Buspirate did not answer correctly! "
  622. "Do you have correct firmware?");
  623. exit(-1);
  624. }
  625. }
  626. }
  627. static void buspirate_jtag_reset(int fd)
  628. {
  629. char tmp[5];
  630. tmp[0] = 0x00; /* exit OCD1 mode */
  631. buspirate_serial_write(fd, tmp, 1);
  632. usleep(10000);
  633. /* We ignore the return value here purposly, nothing we can do */
  634. buspirate_serial_read(fd, tmp, 5);
  635. if (strncmp(tmp, "BBIO1", 5) == 0) {
  636. tmp[0] = 0x0F; /* reset BP */
  637. buspirate_serial_write(fd, tmp, 1);
  638. } else
  639. LOG_ERROR("Unable to restart buspirate!");
  640. }
  641. static void buspirate_jtag_set_speed(int fd, char speed)
  642. {
  643. int ret;
  644. char tmp[2];
  645. char ack[2];
  646. ack[0] = 0xAA;
  647. ack[1] = 0x55;
  648. tmp[0] = CMD_UART_SPEED;
  649. tmp[1] = speed;
  650. buspirate_jtag_command(fd, tmp, 2);
  651. /* here the adapter changes speed, we need follow */
  652. buspirate_serial_setspeed(fd, speed);
  653. buspirate_serial_write(fd, ack, 2);
  654. ret = buspirate_serial_read(fd, tmp, 2);
  655. if (ret != 2) {
  656. LOG_ERROR("Buspirate did not ack speed change");
  657. exit(-1);
  658. }
  659. if ((tmp[0] != CMD_UART_SPEED) || (tmp[1] != speed)) {
  660. LOG_ERROR("Buspirate did not reply as expected");
  661. exit(-1);
  662. }
  663. LOG_INFO("Buspirate switched to %s mode",
  664. (speed == SERIAL_NORMAL) ? "normal" : "FAST");
  665. }
  666. static void buspirate_jtag_set_mode(int fd, char mode)
  667. {
  668. char tmp[2];
  669. tmp[0] = CMD_PORT_MODE;
  670. tmp[1] = mode;
  671. buspirate_jtag_command(fd, tmp, 2);
  672. }
  673. static void buspirate_jtag_set_feature(int fd, char feat, char action)
  674. {
  675. char tmp[3];
  676. tmp[0] = CMD_FEATURE;
  677. tmp[1] = feat; /* what */
  678. tmp[2] = action; /* action */
  679. buspirate_jtag_command(fd, tmp, 3);
  680. }
  681. static void buspirate_jtag_get_adcs(int fd)
  682. {
  683. uint8_t tmp[10];
  684. uint16_t a, b, c, d;
  685. tmp[0] = CMD_READ_ADCS;
  686. buspirate_jtag_command(fd, (char *)tmp, 1);
  687. a = tmp[2] << 8 | tmp[3];
  688. b = tmp[4] << 8 | tmp[5];
  689. c = tmp[6] << 8 | tmp[7];
  690. d = tmp[8] << 8 | tmp[9];
  691. LOG_INFO("ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
  692. "V50 = %.02f",
  693. ((float)a)/155.1515, ((float)b)/155.1515,
  694. ((float)c)/155.1515, ((float)d)/155.1515);
  695. }
  696. static unsigned char buspirate_jtag_command(int fd,
  697. char *cmd, int cmdlen)
  698. {
  699. int res;
  700. int len = 0;
  701. res = buspirate_serial_write(fd, cmd, cmdlen);
  702. if ((cmd[0] == CMD_UART_SPEED)
  703. || (cmd[0] == CMD_PORT_MODE)
  704. || (cmd[0] == CMD_FEATURE)
  705. || (cmd[0] == CMD_JTAG_SPEED))
  706. return 1;
  707. if (res == cmdlen) {
  708. switch (cmd[0]) {
  709. case CMD_READ_ADCS:
  710. len = 10; /* 2*sizeof(char)+4*sizeof(uint16_t) */
  711. break;
  712. case CMD_TAP_SHIFT:
  713. len = cmdlen;
  714. break;
  715. default:
  716. LOG_INFO("Wrong !");
  717. }
  718. res = buspirate_serial_read(fd, cmd, len);
  719. if (res > 0)
  720. return (unsigned char)cmd[1];
  721. else
  722. return -1;
  723. } else
  724. return -1;
  725. return 0;
  726. }
  727. /* low level serial port */
  728. /* TODO add support for WIN32 and others ! */
  729. static int buspirate_serial_open(char *port)
  730. {
  731. int fd;
  732. fd = open(buspirate_port, O_RDWR | O_NOCTTY | O_NDELAY);
  733. return fd;
  734. }
  735. static int buspirate_serial_setspeed(int fd, char speed)
  736. {
  737. struct termios t_opt;
  738. speed_t baud = (speed == SERIAL_FAST) ? B1000000 : B115200;
  739. /* set the serial port parameters */
  740. fcntl(fd, F_SETFL, 0);
  741. tcgetattr(fd, &t_opt);
  742. cfsetispeed(&t_opt, baud);
  743. cfsetospeed(&t_opt, baud);
  744. t_opt.c_cflag |= (CLOCAL | CREAD);
  745. t_opt.c_cflag &= ~PARENB;
  746. t_opt.c_cflag &= ~CSTOPB;
  747. t_opt.c_cflag &= ~CSIZE;
  748. t_opt.c_cflag |= CS8;
  749. t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  750. t_opt.c_iflag &= ~(IXON | IXOFF | IXANY);
  751. t_opt.c_oflag &= ~OPOST;
  752. t_opt.c_cc[VMIN] = 0;
  753. t_opt.c_cc[VTIME] = 10;
  754. tcflush(fd, TCIFLUSH);
  755. tcsetattr(fd, TCSANOW, &t_opt);
  756. return 0;
  757. }
  758. static int buspirate_serial_write(int fd, char *buf, int size)
  759. {
  760. int ret = 0;
  761. ret = write(fd, buf, size);
  762. LOG_DEBUG("size = %d ret = %d", size, ret);
  763. buspirate_print_buffer(buf, size);
  764. if (ret != size)
  765. LOG_ERROR("Error sending data");
  766. return ret;
  767. }
  768. static int buspirate_serial_read(int fd, char *buf, int size)
  769. {
  770. int len = 0;
  771. int ret = 0;
  772. int timeout = 0;
  773. while (len < size) {
  774. ret = read(fd, buf+len, size-len);
  775. if (ret == -1)
  776. return -1;
  777. if (ret == 0) {
  778. timeout++;
  779. if (timeout >= 10)
  780. break;
  781. continue;
  782. }
  783. len += ret;
  784. }
  785. LOG_DEBUG("should have read = %d actual size = %d", size, len);
  786. buspirate_print_buffer(buf, len);
  787. if (len != size)
  788. LOG_ERROR("Error reading data");
  789. return len;
  790. }
  791. static void buspirate_serial_close(int fd)
  792. {
  793. close(fd);
  794. }
  795. #define LINE_SIZE 81
  796. #define BYTES_PER_LINE 16
  797. static void buspirate_print_buffer(char *buf, int size)
  798. {
  799. char line[LINE_SIZE];
  800. char tmp[10];
  801. int offset = 0;
  802. line[0] = 0;
  803. while (offset < size) {
  804. snprintf(tmp, 5, "%02x ", (uint8_t)buf[offset]);
  805. offset++;
  806. strcat(line, tmp);
  807. if (offset % BYTES_PER_LINE == 0) {
  808. LOG_DEBUG("%s", line);
  809. line[0] = 0;
  810. }
  811. }
  812. if (line[0] != 0)
  813. LOG_DEBUG("%s", line);
  814. }