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.
 
 
 
 
 
 

1550 lines
40 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2010 by Michal Demin *
  3. * based on usbprog.c and arm-jtag-ew.c *
  4. * Several fixes by R. Diez in 2013. *
  5. * *
  6. * This program is free software; you can redistribute it and/or modify *
  7. * it under the terms of the GNU General Public License as published by *
  8. * the Free Software Foundation; either version 2 of the License, or *
  9. * (at your option) any later version. *
  10. * *
  11. * This program is distributed in the hope that it will be useful, *
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  14. * GNU General Public License for more details. *
  15. * *
  16. * You should have received a copy of the GNU General Public License *
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  18. ***************************************************************************/
  19. #ifdef HAVE_CONFIG_H
  20. #include "config.h"
  21. #endif
  22. #include <jtag/interface.h>
  23. #include <jtag/swd.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_init(void);
  32. static int buspirate_quit(void);
  33. static int buspirate_reset(int trst, int srst);
  34. static void buspirate_end_state(tap_state_t state);
  35. static void buspirate_state_move(void);
  36. static void buspirate_path_move(int num_states, tap_state_t *path);
  37. static void buspirate_runtest(int num_cycles);
  38. static void buspirate_scan(bool ir_scan, enum scan_type type,
  39. uint8_t *buffer, int scan_size, struct scan_command *command);
  40. static void buspirate_stableclocks(int num_cycles);
  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_RWIRE 0x05
  48. #define CMD_ENTER_OOCD 0x06
  49. #define CMD_UART_SPEED 0x07
  50. #define CMD_JTAG_SPEED 0x08
  51. #define CMD_RAW_PERIPH 0x40
  52. #define CMD_RAW_SPEED 0x60
  53. #define CMD_RAW_MODE 0x80
  54. #define CMD_TAP_SHIFT_HEADER_LEN 3
  55. /* raw-wire mode configuration */
  56. #define CMD_RAW_CONFIG_HIZ 0x00
  57. #define CMD_RAW_CONFIG_3V3 0x08
  58. #define CMD_RAW_CONFIG_2W 0x00
  59. #define CMD_RAW_CONFIG_3W 0x04
  60. #define CMD_RAW_CONFIG_MSB 0x00
  61. #define CMD_RAW_CONFIG_LSB 0x02
  62. /* Not all OSes have this speed defined */
  63. #if !defined(B1000000)
  64. #define B1000000 0010010
  65. #endif
  66. #define SHORT_TIMEOUT 1 /* Must be at least 1. */
  67. #define NORMAL_TIMEOUT 10
  68. enum {
  69. MODE_HIZ = 0,
  70. MODE_JTAG = 1, /* push-pull outputs */
  71. MODE_JTAG_OD = 2, /* open-drain outputs */
  72. };
  73. enum {
  74. FEATURE_LED = 0x01,
  75. FEATURE_VREG = 0x02,
  76. FEATURE_TRST = 0x04,
  77. FEATURE_SRST = 0x08,
  78. FEATURE_PULLUP = 0x10
  79. };
  80. enum {
  81. ACTION_DISABLE = 0,
  82. ACTION_ENABLE = 1
  83. };
  84. enum {
  85. SERIAL_NORMAL = 0,
  86. SERIAL_FAST = 1
  87. };
  88. enum {
  89. SPEED_RAW_5_KHZ = 0x0,
  90. SPEED_RAW_50_KHZ = 0x1,
  91. SPEED_RAW_100_KHZ = 0x2,
  92. SPEED_RAW_400_KHZ = 0x3
  93. };
  94. /* SWD mode specific */
  95. static bool swd_mode;
  96. static int queued_retval;
  97. static char swd_features;
  98. static int buspirate_fd = -1;
  99. static int buspirate_pinmode = MODE_JTAG_OD;
  100. static int buspirate_baudrate = SERIAL_NORMAL;
  101. static int buspirate_vreg;
  102. static int buspirate_pullup;
  103. static char *buspirate_port;
  104. static enum tap_state last_tap_state = TAP_RESET;
  105. /* SWD interface */
  106. static int buspirate_swd_init(void);
  107. static void buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk);
  108. static void buspirate_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk);
  109. static int buspirate_swd_switch_seq(enum swd_special_seq seq);
  110. static int buspirate_swd_run_queue(void);
  111. /* TAP interface */
  112. static void buspirate_tap_init(void);
  113. static int buspirate_tap_execute(void);
  114. static void buspirate_tap_append(int tms, int tdi);
  115. static void buspirate_tap_append_scan(int length, uint8_t *buffer,
  116. struct scan_command *command);
  117. static void buspirate_tap_make_space(int scan, int bits);
  118. static void buspirate_set_feature(int, char, char);
  119. static void buspirate_set_mode(int, char);
  120. static void buspirate_set_speed(int, char);
  121. /* low level interface */
  122. static void buspirate_bbio_enable(int);
  123. static void buspirate_jtag_reset(int);
  124. static unsigned char buspirate_jtag_command(int, uint8_t *, int);
  125. static void buspirate_jtag_set_speed(int, char);
  126. static void buspirate_jtag_set_mode(int, char);
  127. static void buspirate_jtag_set_feature(int, char, char);
  128. static void buspirate_jtag_get_adcs(int);
  129. /* low level two-wire interface */
  130. static void buspirate_swd_set_speed(int, char);
  131. static void buspirate_swd_set_feature(int, char, char);
  132. static void buspirate_swd_set_mode(int, char);
  133. /* low level HW communication interface */
  134. static int buspirate_serial_open(char *port);
  135. static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout);
  136. static int buspirate_serial_write(int fd, uint8_t *buf, int size);
  137. static int buspirate_serial_read(int fd, uint8_t *buf, int size);
  138. static void buspirate_serial_close(int fd);
  139. static void buspirate_print_buffer(uint8_t *buf, int size);
  140. static int buspirate_execute_queue(void)
  141. {
  142. /* currently processed command */
  143. struct jtag_command *cmd = jtag_command_queue;
  144. int scan_size;
  145. enum scan_type type;
  146. uint8_t *buffer;
  147. while (cmd) {
  148. switch (cmd->type) {
  149. case JTAG_RUNTEST:
  150. LOG_DEBUG_IO("runtest %i cycles, end in %s",
  151. cmd->cmd.runtest->num_cycles,
  152. tap_state_name(cmd->cmd.runtest
  153. ->end_state));
  154. buspirate_end_state(cmd->cmd.runtest
  155. ->end_state);
  156. buspirate_runtest(cmd->cmd.runtest
  157. ->num_cycles);
  158. break;
  159. case JTAG_TLR_RESET:
  160. LOG_DEBUG_IO("statemove end in %s",
  161. tap_state_name(cmd->cmd.statemove
  162. ->end_state));
  163. buspirate_end_state(cmd->cmd.statemove
  164. ->end_state);
  165. buspirate_state_move();
  166. break;
  167. case JTAG_PATHMOVE:
  168. LOG_DEBUG_IO("pathmove: %i states, end in %s",
  169. cmd->cmd.pathmove->num_states,
  170. tap_state_name(cmd->cmd.pathmove
  171. ->path[cmd->cmd.pathmove
  172. ->num_states - 1]));
  173. buspirate_path_move(cmd->cmd.pathmove
  174. ->num_states,
  175. cmd->cmd.pathmove->path);
  176. break;
  177. case JTAG_SCAN:
  178. LOG_DEBUG_IO("scan end in %s",
  179. tap_state_name(cmd->cmd.scan
  180. ->end_state));
  181. buspirate_end_state(cmd->cmd.scan
  182. ->end_state);
  183. scan_size = jtag_build_buffer(cmd->cmd.scan,
  184. &buffer);
  185. type = jtag_scan_type(cmd->cmd.scan);
  186. buspirate_scan(cmd->cmd.scan->ir_scan, type,
  187. buffer, scan_size, cmd->cmd.scan);
  188. break;
  189. case JTAG_SLEEP:
  190. LOG_DEBUG_IO("sleep %" PRIu32, cmd->cmd.sleep->us);
  191. buspirate_tap_execute();
  192. jtag_sleep(cmd->cmd.sleep->us);
  193. break;
  194. case JTAG_STABLECLOCKS:
  195. LOG_DEBUG_IO("stable clock %i cycles", cmd->cmd.stableclocks->num_cycles);
  196. buspirate_stableclocks(cmd->cmd.stableclocks->num_cycles);
  197. break;
  198. default:
  199. LOG_ERROR("BUG: unknown JTAG command type encountered");
  200. exit(-1);
  201. }
  202. cmd = cmd->next;
  203. }
  204. return buspirate_tap_execute();
  205. }
  206. /* Returns true if successful, false if error. */
  207. static bool read_and_discard_all_data(const int fd)
  208. {
  209. /* LOG_INFO("Discarding any stale data from a previous connection..."); */
  210. bool was_msg_already_printed = false;
  211. for ( ; ; ) {
  212. uint8_t buffer[1024]; /* Any size will do, it's a trade-off between stack size and performance. */
  213. const ssize_t read_count = read(fd, buffer, sizeof(buffer));
  214. if (read_count == 0) {
  215. /* This is the "end of file" or "connection closed at the other end" condition. */
  216. return true;
  217. }
  218. if (read_count > 0) {
  219. if (!was_msg_already_printed) {
  220. LOG_INFO("Some stale data from a previous connection was discarded.");
  221. was_msg_already_printed = true;
  222. }
  223. continue;
  224. }
  225. assert(read_count == -1); /* According to the specification. */
  226. const int errno_code = errno;
  227. if (errno_code == EINTR)
  228. continue;
  229. if (errno_code == EAGAIN ||
  230. errno_code == EWOULDBLOCK) {
  231. /* We know that the file descriptor has been opened with O_NONBLOCK or O_NDELAY,
  232. and these codes mean that there is no data to read at present. */
  233. return true;
  234. }
  235. /* Some other error has occurred. */
  236. return false;
  237. }
  238. }
  239. static int buspirate_init(void)
  240. {
  241. if (!buspirate_port) {
  242. LOG_ERROR("You need to specify the serial port!");
  243. return ERROR_JTAG_INIT_FAILED;
  244. }
  245. buspirate_fd = buspirate_serial_open(buspirate_port);
  246. if (buspirate_fd == -1) {
  247. LOG_ERROR("Could not open serial port");
  248. return ERROR_JTAG_INIT_FAILED;
  249. }
  250. /* The Operating System or the device itself may deliver stale data from the last connection,
  251. so discard all available bytes right after the new connection has been established.
  252. After all, we are implementing here a master/slave protocol, so the slave should have nothing
  253. to say until the master sends the first command.
  254. In the past, there was a tcflush() call in buspirate_serial_setspeed(), but that
  255. was not enough. I guess you must actively read from the serial port to trigger any
  256. data collection from the device and/or lower USB layers. If you disable the serial port
  257. read timeout (if you set SHORT_TIMEOUT to 0), then the discarding does not work any more.
  258. Note that we are lowering the serial port timeout for this first read operation,
  259. otherwise the normal initialisation would be delayed for too long. */
  260. if (-1 == buspirate_serial_setspeed(buspirate_fd, SERIAL_NORMAL, SHORT_TIMEOUT)) {
  261. LOG_ERROR("Error configuring the serial port.");
  262. return ERROR_JTAG_INIT_FAILED;
  263. }
  264. if (!read_and_discard_all_data(buspirate_fd)) {
  265. LOG_ERROR("Error while attempting to discard any stale data right after establishing the connection.");
  266. return ERROR_JTAG_INIT_FAILED;
  267. }
  268. if (-1 == buspirate_serial_setspeed(buspirate_fd, SERIAL_NORMAL, NORMAL_TIMEOUT)) {
  269. LOG_ERROR("Error configuring the serial port.");
  270. return ERROR_JTAG_INIT_FAILED;
  271. }
  272. buspirate_bbio_enable(buspirate_fd);
  273. if (swd_mode || buspirate_baudrate != SERIAL_NORMAL)
  274. buspirate_set_speed(buspirate_fd, SERIAL_FAST);
  275. LOG_INFO("Buspirate %s Interface ready!", swd_mode ? "SWD" : "JTAG");
  276. if (!swd_mode)
  277. buspirate_tap_init();
  278. buspirate_set_mode(buspirate_fd, buspirate_pinmode);
  279. buspirate_set_feature(buspirate_fd, FEATURE_VREG,
  280. (buspirate_vreg == 1) ? ACTION_ENABLE : ACTION_DISABLE);
  281. buspirate_set_feature(buspirate_fd, FEATURE_PULLUP,
  282. (buspirate_pullup == 1) ? ACTION_ENABLE : ACTION_DISABLE);
  283. buspirate_reset(0, 0);
  284. return ERROR_OK;
  285. }
  286. static int buspirate_quit(void)
  287. {
  288. LOG_INFO("Shutting down buspirate.");
  289. buspirate_set_mode(buspirate_fd, MODE_HIZ);
  290. buspirate_set_speed(buspirate_fd, SERIAL_NORMAL);
  291. buspirate_jtag_reset(buspirate_fd);
  292. buspirate_serial_close(buspirate_fd);
  293. free(buspirate_port);
  294. buspirate_port = NULL;
  295. return ERROR_OK;
  296. }
  297. /* openocd command interface */
  298. COMMAND_HANDLER(buspirate_handle_adc_command)
  299. {
  300. if (buspirate_fd == -1)
  301. return ERROR_OK;
  302. /* unavailable in SWD mode */
  303. if (swd_mode)
  304. return ERROR_OK;
  305. /* send the command */
  306. buspirate_jtag_get_adcs(buspirate_fd);
  307. return ERROR_OK;
  308. }
  309. COMMAND_HANDLER(buspirate_handle_vreg_command)
  310. {
  311. if (CMD_ARGC < 1)
  312. return ERROR_COMMAND_SYNTAX_ERROR;
  313. if (atoi(CMD_ARGV[0]) == 1)
  314. buspirate_vreg = 1;
  315. else if (atoi(CMD_ARGV[0]) == 0)
  316. buspirate_vreg = 0;
  317. else
  318. LOG_ERROR("usage: buspirate_vreg <1|0>");
  319. return ERROR_OK;
  320. }
  321. COMMAND_HANDLER(buspirate_handle_pullup_command)
  322. {
  323. if (CMD_ARGC < 1)
  324. return ERROR_COMMAND_SYNTAX_ERROR;
  325. if (atoi(CMD_ARGV[0]) == 1)
  326. buspirate_pullup = 1;
  327. else if (atoi(CMD_ARGV[0]) == 0)
  328. buspirate_pullup = 0;
  329. else
  330. LOG_ERROR("usage: buspirate_pullup <1|0>");
  331. return ERROR_OK;
  332. }
  333. COMMAND_HANDLER(buspirate_handle_led_command)
  334. {
  335. if (CMD_ARGC < 1)
  336. return ERROR_COMMAND_SYNTAX_ERROR;
  337. if (atoi(CMD_ARGV[0]) == 1) {
  338. /* enable led */
  339. buspirate_set_feature(buspirate_fd, FEATURE_LED,
  340. ACTION_ENABLE);
  341. } else if (atoi(CMD_ARGV[0]) == 0) {
  342. /* disable led */
  343. buspirate_set_feature(buspirate_fd, FEATURE_LED,
  344. ACTION_DISABLE);
  345. } else {
  346. LOG_ERROR("usage: buspirate_led <1|0>");
  347. }
  348. return ERROR_OK;
  349. }
  350. COMMAND_HANDLER(buspirate_handle_mode_command)
  351. {
  352. if (CMD_ARGC < 1)
  353. return ERROR_COMMAND_SYNTAX_ERROR;
  354. if (CMD_ARGV[0][0] == 'n')
  355. buspirate_pinmode = MODE_JTAG;
  356. else if (CMD_ARGV[0][0] == 'o')
  357. buspirate_pinmode = MODE_JTAG_OD;
  358. else
  359. LOG_ERROR("usage: buspirate_mode <normal|open-drain>");
  360. return ERROR_OK;
  361. }
  362. COMMAND_HANDLER(buspirate_handle_speed_command)
  363. {
  364. if (CMD_ARGC < 1)
  365. return ERROR_COMMAND_SYNTAX_ERROR;
  366. if (CMD_ARGV[0][0] == 'n')
  367. buspirate_baudrate = SERIAL_NORMAL;
  368. else if (CMD_ARGV[0][0] == 'f')
  369. buspirate_baudrate = SERIAL_FAST;
  370. else
  371. LOG_ERROR("usage: buspirate_speed <normal|fast>");
  372. return ERROR_OK;
  373. }
  374. COMMAND_HANDLER(buspirate_handle_port_command)
  375. {
  376. if (CMD_ARGC < 1)
  377. return ERROR_COMMAND_SYNTAX_ERROR;
  378. if (!buspirate_port)
  379. buspirate_port = strdup(CMD_ARGV[0]);
  380. return ERROR_OK;
  381. }
  382. static const struct command_registration buspirate_subcommand_handlers[] = {
  383. {
  384. .name = "adc",
  385. .handler = &buspirate_handle_adc_command,
  386. .mode = COMMAND_EXEC,
  387. .help = "reads voltages on adc pins",
  388. .usage = "",
  389. },
  390. {
  391. .name = "vreg",
  392. .usage = "<1|0>",
  393. .handler = &buspirate_handle_vreg_command,
  394. .mode = COMMAND_CONFIG,
  395. .help = "changes the state of voltage regulators",
  396. },
  397. {
  398. .name = "pullup",
  399. .usage = "<1|0>",
  400. .handler = &buspirate_handle_pullup_command,
  401. .mode = COMMAND_CONFIG,
  402. .help = "changes the state of pullup",
  403. },
  404. {
  405. .name = "led",
  406. .usage = "<1|0>",
  407. .handler = &buspirate_handle_led_command,
  408. .mode = COMMAND_EXEC,
  409. .help = "changes the state of led",
  410. },
  411. {
  412. .name = "speed",
  413. .usage = "<normal|fast>",
  414. .handler = &buspirate_handle_speed_command,
  415. .mode = COMMAND_CONFIG,
  416. .help = "speed of the interface",
  417. },
  418. {
  419. .name = "mode",
  420. .usage = "<normal|open-drain>",
  421. .handler = &buspirate_handle_mode_command,
  422. .mode = COMMAND_CONFIG,
  423. .help = "pin mode of the interface",
  424. },
  425. {
  426. .name = "port",
  427. .usage = "/dev/ttyUSB0",
  428. .handler = &buspirate_handle_port_command,
  429. .mode = COMMAND_CONFIG,
  430. .help = "name of the serial port to open",
  431. },
  432. COMMAND_REGISTRATION_DONE
  433. };
  434. static const struct command_registration buspirate_command_handlers[] = {
  435. {
  436. .name = "buspirate",
  437. .mode = COMMAND_ANY,
  438. .help = "perform buspirate management",
  439. .chain = buspirate_subcommand_handlers,
  440. .usage = "",
  441. },
  442. COMMAND_REGISTRATION_DONE
  443. };
  444. static const struct swd_driver buspirate_swd = {
  445. .init = buspirate_swd_init,
  446. .switch_seq = buspirate_swd_switch_seq,
  447. .read_reg = buspirate_swd_read_reg,
  448. .write_reg = buspirate_swd_write_reg,
  449. .run = buspirate_swd_run_queue,
  450. };
  451. static const char * const buspirate_transports[] = { "jtag", "swd", NULL };
  452. static struct jtag_interface buspirate_interface = {
  453. .execute_queue = buspirate_execute_queue,
  454. };
  455. struct adapter_driver buspirate_adapter_driver = {
  456. .name = "buspirate",
  457. .transports = buspirate_transports,
  458. .commands = buspirate_command_handlers,
  459. .init = buspirate_init,
  460. .quit = buspirate_quit,
  461. .reset = buspirate_reset,
  462. .jtag_ops = &buspirate_interface,
  463. .swd_ops = &buspirate_swd,
  464. };
  465. /*************** jtag execute commands **********************/
  466. static void buspirate_end_state(tap_state_t state)
  467. {
  468. if (tap_is_state_stable(state))
  469. tap_set_end_state(state);
  470. else {
  471. LOG_ERROR("BUG: %i is not a valid end state", state);
  472. exit(-1);
  473. }
  474. }
  475. static void buspirate_state_move(void)
  476. {
  477. int i = 0, tms = 0;
  478. uint8_t tms_scan = tap_get_tms_path(tap_get_state(),
  479. tap_get_end_state());
  480. int tms_count = tap_get_tms_path_len(tap_get_state(),
  481. tap_get_end_state());
  482. for (i = 0; i < tms_count; i++) {
  483. tms = (tms_scan >> i) & 1;
  484. buspirate_tap_append(tms, 0);
  485. }
  486. tap_set_state(tap_get_end_state());
  487. }
  488. static void buspirate_path_move(int num_states, tap_state_t *path)
  489. {
  490. int i;
  491. for (i = 0; i < num_states; i++) {
  492. if (tap_state_transition(tap_get_state(), false) == path[i]) {
  493. buspirate_tap_append(0, 0);
  494. } else if (tap_state_transition(tap_get_state(), true)
  495. == path[i]) {
  496. buspirate_tap_append(1, 0);
  497. } else {
  498. LOG_ERROR("BUG: %s -> %s isn't a valid "
  499. "TAP transition",
  500. tap_state_name(tap_get_state()),
  501. tap_state_name(path[i]));
  502. exit(-1);
  503. }
  504. tap_set_state(path[i]);
  505. }
  506. tap_set_end_state(tap_get_state());
  507. }
  508. static void buspirate_runtest(int num_cycles)
  509. {
  510. int i;
  511. tap_state_t saved_end_state = tap_get_end_state();
  512. /* only do a state_move when we're not already in IDLE */
  513. if (tap_get_state() != TAP_IDLE) {
  514. buspirate_end_state(TAP_IDLE);
  515. buspirate_state_move();
  516. }
  517. for (i = 0; i < num_cycles; i++)
  518. buspirate_tap_append(0, 0);
  519. LOG_DEBUG_IO("runtest: cur_state %s end_state %s",
  520. tap_state_name(tap_get_state()),
  521. tap_state_name(tap_get_end_state()));
  522. /* finish in end_state */
  523. buspirate_end_state(saved_end_state);
  524. if (tap_get_state() != tap_get_end_state())
  525. buspirate_state_move();
  526. }
  527. static void buspirate_scan(bool ir_scan, enum scan_type type,
  528. uint8_t *buffer, int scan_size, struct scan_command *command)
  529. {
  530. tap_state_t saved_end_state;
  531. buspirate_tap_make_space(1, scan_size+8);
  532. /* is 8 correct ? (2 moves = 16) */
  533. saved_end_state = tap_get_end_state();
  534. buspirate_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  535. /* Only move if we're not already there */
  536. if (tap_get_state() != tap_get_end_state())
  537. buspirate_state_move();
  538. buspirate_tap_append_scan(scan_size, buffer, command);
  539. /* move to PAUSE */
  540. buspirate_tap_append(0, 0);
  541. /* restore the saved state */
  542. buspirate_end_state(saved_end_state);
  543. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  544. if (tap_get_state() != tap_get_end_state())
  545. buspirate_state_move();
  546. }
  547. static void buspirate_stableclocks(int num_cycles)
  548. {
  549. int i;
  550. int tms = (tap_get_state() == TAP_RESET ? 1 : 0);
  551. buspirate_tap_make_space(0, num_cycles);
  552. for (i = 0; i < num_cycles; i++)
  553. buspirate_tap_append(tms, 0);
  554. }
  555. /************************* TAP related stuff **********/
  556. /* This buffer size matches the maximum CMD_TAP_SHIFT bit length in the Bus Pirate firmware,
  557. look for constant 0x2000 in OpenOCD.c . */
  558. #define BUSPIRATE_BUFFER_SIZE 1024
  559. /* The old value of 32 scans was not enough to achieve near 100% utilisation ratio
  560. for the current BUSPIRATE_BUFFER_SIZE value of 1024.
  561. With 128 scans I am getting full USB 2.0 high speed packets (512 bytes long) when
  562. using the JtagDue firmware on the Arduino Due instead of the Bus Pirate, which
  563. amounts approximately to a 10% overall speed gain. Bigger packets should also
  564. benefit the Bus Pirate, but the speed difference is much smaller.
  565. Unfortunately, each 512-byte packet is followed by a 329-byte one, which is not ideal.
  566. However, increasing BUSPIRATE_BUFFER_SIZE for the benefit of the JtagDue would
  567. make it incompatible with the Bus Pirate firmware. */
  568. #define BUSPIRATE_MAX_PENDING_SCANS 128
  569. static uint8_t tms_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
  570. static uint8_t tdi_chain[BUSPIRATE_BUFFER_SIZE]; /* send */
  571. static int tap_chain_index;
  572. struct pending_scan_result /* this was stolen from arm-jtag-ew */
  573. {
  574. int first; /* First bit position in tdo_buffer to read */
  575. int length; /* Number of bits to read */
  576. struct scan_command *command; /* Corresponding scan command */
  577. uint8_t *buffer;
  578. };
  579. static struct pending_scan_result
  580. tap_pending_scans[BUSPIRATE_MAX_PENDING_SCANS];
  581. static int tap_pending_scans_num;
  582. static void buspirate_tap_init(void)
  583. {
  584. tap_chain_index = 0;
  585. tap_pending_scans_num = 0;
  586. }
  587. static int buspirate_tap_execute(void)
  588. {
  589. uint8_t tmp[4096];
  590. uint8_t *in_buf;
  591. int i;
  592. int fill_index = 0;
  593. int ret;
  594. int bytes_to_send;
  595. if (tap_chain_index <= 0)
  596. return ERROR_OK;
  597. LOG_DEBUG("executing tap num bits = %i scans = %i",
  598. tap_chain_index, tap_pending_scans_num);
  599. bytes_to_send = DIV_ROUND_UP(tap_chain_index, 8);
  600. tmp[0] = CMD_TAP_SHIFT; /* this command expects number of bits */
  601. tmp[1] = tap_chain_index >> 8; /* high */
  602. tmp[2] = tap_chain_index; /* low */
  603. fill_index = CMD_TAP_SHIFT_HEADER_LEN;
  604. for (i = 0; i < bytes_to_send; i++) {
  605. tmp[fill_index] = tdi_chain[i];
  606. fill_index++;
  607. tmp[fill_index] = tms_chain[i];
  608. fill_index++;
  609. }
  610. /* jlink.c calls the routine below, which may be useful for debugging purposes.
  611. For example, enabling this allows you to compare the log outputs from jlink.c
  612. and from this module for JTAG development or troubleshooting purposes. */
  613. if (false) {
  614. last_tap_state = jtag_debug_state_machine(tms_chain, tdi_chain,
  615. tap_chain_index, last_tap_state);
  616. }
  617. ret = buspirate_serial_write(buspirate_fd, tmp, CMD_TAP_SHIFT_HEADER_LEN + bytes_to_send*2);
  618. if (ret != bytes_to_send*2+CMD_TAP_SHIFT_HEADER_LEN) {
  619. LOG_ERROR("error writing :(");
  620. return ERROR_JTAG_DEVICE_ERROR;
  621. }
  622. ret = buspirate_serial_read(buspirate_fd, tmp, bytes_to_send + CMD_TAP_SHIFT_HEADER_LEN);
  623. if (ret != bytes_to_send + CMD_TAP_SHIFT_HEADER_LEN) {
  624. LOG_ERROR("error reading");
  625. return ERROR_FAIL;
  626. }
  627. in_buf = (uint8_t *)(&tmp[CMD_TAP_SHIFT_HEADER_LEN]);
  628. /* parse the scans */
  629. for (i = 0; i < tap_pending_scans_num; i++) {
  630. uint8_t *buffer = tap_pending_scans[i].buffer;
  631. int length = tap_pending_scans[i].length;
  632. int first = tap_pending_scans[i].first;
  633. struct scan_command *command = tap_pending_scans[i].command;
  634. /* copy bits from buffer */
  635. buf_set_buf(in_buf, first, buffer, 0, length);
  636. /* return buffer to higher level */
  637. if (jtag_read_buffer(buffer, command) != ERROR_OK) {
  638. buspirate_tap_init();
  639. return ERROR_JTAG_QUEUE_FAILED;
  640. }
  641. free(buffer);
  642. }
  643. buspirate_tap_init();
  644. return ERROR_OK;
  645. }
  646. static void buspirate_tap_make_space(int scans, int bits)
  647. {
  648. int have_scans = BUSPIRATE_MAX_PENDING_SCANS - tap_pending_scans_num;
  649. int have_bits = BUSPIRATE_BUFFER_SIZE * 8 - tap_chain_index;
  650. if ((have_scans < scans) || (have_bits < bits))
  651. buspirate_tap_execute();
  652. }
  653. static void buspirate_tap_append(int tms, int tdi)
  654. {
  655. int chain_index;
  656. buspirate_tap_make_space(0, 1);
  657. chain_index = tap_chain_index / 8;
  658. if (chain_index < BUSPIRATE_BUFFER_SIZE) {
  659. int bit_index = tap_chain_index % 8;
  660. uint8_t bit = 1 << bit_index;
  661. if (bit_index == 0) {
  662. /* Let's say that the TAP shift operation wants to shift 9 bits,
  663. so we will be sending to the Bus Pirate a bit count of 9 but still
  664. full 16 bits (2 bytes) of shift data.
  665. If we don't clear all bits at this point, the last 7 bits will contain
  666. random data from the last buffer contents, which is not pleasant to the eye.
  667. Besides, the Bus Pirate (or some clone) may want to assert in debug builds
  668. that, after consuming all significant data bits, the rest of them are zero.
  669. Therefore, for aesthetic and for assert purposes, we clear all bits below. */
  670. tms_chain[chain_index] = 0;
  671. tdi_chain[chain_index] = 0;
  672. }
  673. if (tms)
  674. tms_chain[chain_index] |= bit;
  675. else
  676. tms_chain[chain_index] &= ~bit;
  677. if (tdi)
  678. tdi_chain[chain_index] |= bit;
  679. else
  680. tdi_chain[chain_index] &= ~bit;
  681. tap_chain_index++;
  682. } else {
  683. LOG_ERROR("tap_chain overflow, bad things will happen");
  684. /* Exit abruptly, like jlink.c does. After a buffer overflow we don't want
  685. to carry on, as data will be corrupt. Another option would be to return
  686. some error code at this point. */
  687. exit(-1);
  688. }
  689. }
  690. static void buspirate_tap_append_scan(int length, uint8_t *buffer,
  691. struct scan_command *command)
  692. {
  693. int i;
  694. tap_pending_scans[tap_pending_scans_num].length = length;
  695. tap_pending_scans[tap_pending_scans_num].buffer = buffer;
  696. tap_pending_scans[tap_pending_scans_num].command = command;
  697. tap_pending_scans[tap_pending_scans_num].first = tap_chain_index;
  698. for (i = 0; i < length; i++) {
  699. int tms = (i < length-1 ? 0 : 1);
  700. int tdi = (buffer[i/8] >> (i%8)) & 1;
  701. buspirate_tap_append(tms, tdi);
  702. }
  703. tap_pending_scans_num++;
  704. }
  705. /*************** wrapper functions *********************/
  706. /* (1) assert or (0) deassert reset lines */
  707. static int buspirate_reset(int trst, int srst)
  708. {
  709. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  710. if (trst)
  711. buspirate_set_feature(buspirate_fd, FEATURE_TRST, ACTION_DISABLE);
  712. else
  713. buspirate_set_feature(buspirate_fd, FEATURE_TRST, ACTION_ENABLE);
  714. if (srst)
  715. buspirate_set_feature(buspirate_fd, FEATURE_SRST, ACTION_DISABLE);
  716. else
  717. buspirate_set_feature(buspirate_fd, FEATURE_SRST, ACTION_ENABLE);
  718. return ERROR_OK;
  719. }
  720. static void buspirate_set_feature(int fd, char feat, char action)
  721. {
  722. if (swd_mode)
  723. buspirate_swd_set_feature(fd, feat, action);
  724. else
  725. buspirate_jtag_set_feature(fd, feat, action);
  726. }
  727. static void buspirate_set_mode(int fd, char mode)
  728. {
  729. if (swd_mode)
  730. buspirate_swd_set_mode(fd, mode);
  731. else
  732. buspirate_jtag_set_mode(fd, mode);
  733. }
  734. static void buspirate_set_speed(int fd, char speed)
  735. {
  736. if (swd_mode)
  737. buspirate_swd_set_speed(fd, speed);
  738. else
  739. buspirate_jtag_set_speed(fd, speed);
  740. }
  741. /*************** swd lowlevel functions ********************/
  742. static void buspirate_swd_set_speed(int fd, char speed)
  743. {
  744. int ret;
  745. uint8_t tmp[1];
  746. LOG_DEBUG("Buspirate speed setting in SWD mode defaults to 400 kHz");
  747. /* speed settings */
  748. tmp[0] = CMD_RAW_SPEED | SPEED_RAW_400_KHZ;
  749. buspirate_serial_write(fd, tmp, 1);
  750. ret = buspirate_serial_read(fd, tmp, 1);
  751. if (ret != 1) {
  752. LOG_ERROR("Buspirate did not answer correctly");
  753. exit(-1);
  754. }
  755. if (tmp[0] != 1) {
  756. LOG_ERROR("Buspirate did not reply as expected to the speed change command");
  757. exit(-1);
  758. }
  759. }
  760. static void buspirate_swd_set_mode(int fd, char mode)
  761. {
  762. int ret;
  763. uint8_t tmp[1];
  764. /* raw-wire mode configuration */
  765. if (mode == MODE_HIZ)
  766. tmp[0] = CMD_RAW_MODE | CMD_RAW_CONFIG_LSB;
  767. else
  768. tmp[0] = CMD_RAW_MODE | CMD_RAW_CONFIG_LSB | CMD_RAW_CONFIG_3V3;
  769. buspirate_serial_write(fd, tmp, 1);
  770. ret = buspirate_serial_read(fd, tmp, 1);
  771. if (ret != 1) {
  772. LOG_ERROR("Buspirate did not answer correctly");
  773. exit(-1);
  774. }
  775. if (tmp[0] != 1) {
  776. LOG_ERROR("Buspirate did not reply as expected to the configure command");
  777. exit(-1);
  778. }
  779. }
  780. static void buspirate_swd_set_feature(int fd, char feat, char action)
  781. {
  782. int ret;
  783. uint8_t tmp[1];
  784. switch (feat) {
  785. case FEATURE_TRST:
  786. LOG_DEBUG("Buspirate TRST feature not available in SWD mode");
  787. return;
  788. case FEATURE_LED:
  789. LOG_ERROR("Buspirate LED feature not available in SWD mode");
  790. return;
  791. case FEATURE_SRST:
  792. swd_features = (action == ACTION_ENABLE) ? swd_features | 0x02 : swd_features & 0x0D;
  793. break;
  794. case FEATURE_PULLUP:
  795. swd_features = (action == ACTION_ENABLE) ? swd_features | 0x04 : swd_features & 0x0B;
  796. break;
  797. case FEATURE_VREG:
  798. swd_features = (action == ACTION_ENABLE) ? swd_features | 0x08 : swd_features & 0x07;
  799. break;
  800. default:
  801. LOG_DEBUG("Buspirate unknown feature %d", feat);
  802. return;
  803. }
  804. tmp[0] = CMD_RAW_PERIPH | swd_features;
  805. buspirate_serial_write(fd, tmp, 1);
  806. ret = buspirate_serial_read(fd, tmp, 1);
  807. if (ret != 1) {
  808. LOG_DEBUG("Buspirate feature %d not supported in SWD mode", feat);
  809. } else if (tmp[0] != 1) {
  810. LOG_ERROR("Buspirate did not reply as expected to the configure command");
  811. exit(-1);
  812. }
  813. }
  814. /*************** jtag lowlevel functions ********************/
  815. static void buspirate_bbio_enable(int fd)
  816. {
  817. int ret;
  818. char command;
  819. const char *mode_answers[2] = { "OCD1", "RAW1" };
  820. const char *correct_ans = NULL;
  821. uint8_t tmp[21] = { [0 ... 20] = 0x00 };
  822. int done = 0;
  823. int cmd_sent = 0;
  824. if (swd_mode) {
  825. command = CMD_ENTER_RWIRE;
  826. correct_ans = mode_answers[1];
  827. } else {
  828. command = CMD_ENTER_OOCD;
  829. correct_ans = mode_answers[0];
  830. }
  831. LOG_DEBUG("Entering binary mode, that is %s", correct_ans);
  832. buspirate_serial_write(fd, tmp, 20);
  833. usleep(10000);
  834. /* reads 1 to n "BBIO1"s and one "OCD1" or "RAW1" */
  835. while (!done) {
  836. ret = buspirate_serial_read(fd, tmp, 4);
  837. if (ret != 4) {
  838. LOG_ERROR("Buspirate error. Is binary"
  839. "/OpenOCD support enabled?");
  840. exit(-1);
  841. }
  842. if (strncmp((char *)tmp, "BBIO", 4) == 0) {
  843. ret = buspirate_serial_read(fd, tmp, 1);
  844. if (ret != 1) {
  845. LOG_ERROR("Buspirate did not answer correctly! "
  846. "Do you have correct firmware?");
  847. exit(-1);
  848. }
  849. if (tmp[0] != '1') {
  850. LOG_ERROR("Unsupported binary protocol");
  851. exit(-1);
  852. }
  853. if (cmd_sent == 0) {
  854. cmd_sent = 1;
  855. tmp[0] = command;
  856. ret = buspirate_serial_write(fd, tmp, 1);
  857. if (ret != 1) {
  858. LOG_ERROR("error reading");
  859. exit(-1);
  860. }
  861. }
  862. } else if (strncmp((char *)tmp, correct_ans, 4) == 0)
  863. done = 1;
  864. else {
  865. LOG_ERROR("Buspirate did not answer correctly! "
  866. "Do you have correct firmware?");
  867. exit(-1);
  868. }
  869. }
  870. }
  871. static void buspirate_jtag_reset(int fd)
  872. {
  873. uint8_t tmp[5];
  874. tmp[0] = 0x00; /* exit OCD1 mode */
  875. buspirate_serial_write(fd, tmp, 1);
  876. usleep(10000);
  877. /* We ignore the return value here on purpose, nothing we can do */
  878. buspirate_serial_read(fd, tmp, 5);
  879. if (strncmp((char *)tmp, "BBIO1", 5) == 0) {
  880. tmp[0] = 0x0F; /* reset BP */
  881. buspirate_serial_write(fd, tmp, 1);
  882. } else
  883. LOG_ERROR("Unable to restart buspirate!");
  884. }
  885. static void buspirate_jtag_set_speed(int fd, char speed)
  886. {
  887. int ret;
  888. uint8_t tmp[2];
  889. uint8_t ack[2];
  890. ack[0] = 0xAA;
  891. ack[1] = 0x55;
  892. tmp[0] = CMD_UART_SPEED;
  893. tmp[1] = speed;
  894. buspirate_jtag_command(fd, tmp, 2);
  895. /* here the adapter changes speed, we need follow */
  896. if (-1 == buspirate_serial_setspeed(fd, speed, NORMAL_TIMEOUT)) {
  897. LOG_ERROR("Error configuring the serial port.");
  898. exit(-1);
  899. }
  900. buspirate_serial_write(fd, ack, 2);
  901. ret = buspirate_serial_read(fd, tmp, 2);
  902. if (ret != 2) {
  903. LOG_ERROR("Buspirate did not ack speed change");
  904. exit(-1);
  905. }
  906. if ((tmp[0] != CMD_UART_SPEED) || (tmp[1] != speed)) {
  907. LOG_ERROR("Buspirate did not reply as expected to the speed change command");
  908. exit(-1);
  909. }
  910. LOG_INFO("Buspirate switched to %s mode",
  911. (speed == SERIAL_NORMAL) ? "normal" : "FAST");
  912. }
  913. static void buspirate_jtag_set_mode(int fd, char mode)
  914. {
  915. uint8_t tmp[2];
  916. tmp[0] = CMD_PORT_MODE;
  917. tmp[1] = mode;
  918. buspirate_jtag_command(fd, tmp, 2);
  919. }
  920. static void buspirate_jtag_set_feature(int fd, char feat, char action)
  921. {
  922. uint8_t tmp[3];
  923. tmp[0] = CMD_FEATURE;
  924. tmp[1] = feat; /* what */
  925. tmp[2] = action; /* action */
  926. buspirate_jtag_command(fd, tmp, 3);
  927. }
  928. static void buspirate_jtag_get_adcs(int fd)
  929. {
  930. uint8_t tmp[10];
  931. uint16_t a, b, c, d;
  932. tmp[0] = CMD_READ_ADCS;
  933. buspirate_jtag_command(fd, tmp, 1);
  934. a = tmp[2] << 8 | tmp[3];
  935. b = tmp[4] << 8 | tmp[5];
  936. c = tmp[6] << 8 | tmp[7];
  937. d = tmp[8] << 8 | tmp[9];
  938. LOG_INFO("ADC: ADC_Pin = %.02f VPullup = %.02f V33 = %.02f "
  939. "V50 = %.02f",
  940. ((float)a)/155.1515, ((float)b)/155.1515,
  941. ((float)c)/155.1515, ((float)d)/155.1515);
  942. }
  943. static unsigned char buspirate_jtag_command(int fd,
  944. uint8_t *cmd, int cmdlen)
  945. {
  946. int res;
  947. int len = 0;
  948. res = buspirate_serial_write(fd, cmd, cmdlen);
  949. if ((cmd[0] == CMD_UART_SPEED)
  950. || (cmd[0] == CMD_PORT_MODE)
  951. || (cmd[0] == CMD_FEATURE)
  952. || (cmd[0] == CMD_JTAG_SPEED))
  953. return 1;
  954. if (res == cmdlen) {
  955. switch (cmd[0]) {
  956. case CMD_READ_ADCS:
  957. len = 10; /* 2*sizeof(char)+4*sizeof(uint16_t) */
  958. break;
  959. case CMD_TAP_SHIFT:
  960. len = cmdlen;
  961. break;
  962. default:
  963. LOG_INFO("Wrong !");
  964. }
  965. res = buspirate_serial_read(fd, cmd, len);
  966. if (res > 0)
  967. return (unsigned char)cmd[1];
  968. else
  969. return -1;
  970. } else
  971. return -1;
  972. return 0;
  973. }
  974. /* low level serial port */
  975. /* TODO add support for WIN32 and others ! */
  976. static int buspirate_serial_open(char *port)
  977. {
  978. int fd;
  979. fd = open(buspirate_port, O_RDWR | O_NOCTTY | O_NDELAY);
  980. return fd;
  981. }
  982. /* Returns -1 on error. */
  983. static int buspirate_serial_setspeed(int fd, char speed, cc_t timeout)
  984. {
  985. struct termios t_opt;
  986. speed_t baud = (speed == SERIAL_FAST) ? B1000000 : B115200;
  987. /* set the serial port parameters */
  988. fcntl(fd, F_SETFL, 0);
  989. if (tcgetattr(fd, &t_opt) != 0)
  990. return -1;
  991. if (cfsetispeed(&t_opt, baud) != 0)
  992. return -1;
  993. if (cfsetospeed(&t_opt, baud) != 0)
  994. return -1;
  995. t_opt.c_cflag |= (CLOCAL | CREAD);
  996. t_opt.c_cflag &= ~PARENB;
  997. t_opt.c_cflag &= ~CSTOPB;
  998. t_opt.c_cflag &= ~CSIZE;
  999. t_opt.c_cflag |= CS8;
  1000. t_opt.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  1001. /* The serial port may have been configured for human interaction with
  1002. the Bus Pirate console, but OpenOCD is going to use a binary protocol,
  1003. so make sure to turn off any CR/LF translation and the like. */
  1004. t_opt.c_iflag &= ~(IXON | IXOFF | IXANY | INLCR | ICRNL);
  1005. t_opt.c_oflag &= ~OPOST;
  1006. t_opt.c_cc[VMIN] = 0;
  1007. t_opt.c_cc[VTIME] = timeout;
  1008. /* Note that, in the past, TCSANOW was used below instead of TCSADRAIN,
  1009. and CMD_UART_SPEED did not work properly then, at least with
  1010. the Bus Pirate v3.5 (USB). */
  1011. if (tcsetattr(fd, TCSADRAIN, &t_opt) != 0) {
  1012. /* According to the Linux documentation, this is actually not enough
  1013. to detect errors, you need to call tcgetattr() and check that
  1014. all changes have been performed successfully. */
  1015. return -1;
  1016. }
  1017. return 0;
  1018. }
  1019. static int buspirate_serial_write(int fd, uint8_t *buf, int size)
  1020. {
  1021. int ret = 0;
  1022. ret = write(fd, buf, size);
  1023. LOG_DEBUG("size = %d ret = %d", size, ret);
  1024. buspirate_print_buffer(buf, size);
  1025. if (ret != size)
  1026. LOG_ERROR("Error sending data");
  1027. return ret;
  1028. }
  1029. static int buspirate_serial_read(int fd, uint8_t *buf, int size)
  1030. {
  1031. int len = 0;
  1032. int ret = 0;
  1033. int timeout = 0;
  1034. while (len < size) {
  1035. ret = read(fd, buf+len, size-len);
  1036. if (ret == -1)
  1037. return -1;
  1038. if (ret == 0) {
  1039. timeout++;
  1040. if (timeout >= 10)
  1041. break;
  1042. continue;
  1043. }
  1044. len += ret;
  1045. }
  1046. LOG_DEBUG("should have read = %d actual size = %d", size, len);
  1047. buspirate_print_buffer(buf, len);
  1048. if (len != size)
  1049. LOG_ERROR("Error reading data");
  1050. return len;
  1051. }
  1052. static void buspirate_serial_close(int fd)
  1053. {
  1054. close(fd);
  1055. }
  1056. #define LINE_SIZE 81
  1057. #define BYTES_PER_LINE 16
  1058. static void buspirate_print_buffer(uint8_t *buf, int size)
  1059. {
  1060. char line[LINE_SIZE];
  1061. char tmp[10];
  1062. int offset = 0;
  1063. line[0] = 0;
  1064. while (offset < size) {
  1065. snprintf(tmp, 5, "%02x ", (uint8_t)buf[offset]);
  1066. offset++;
  1067. strcat(line, tmp);
  1068. if (offset % BYTES_PER_LINE == 0) {
  1069. LOG_DEBUG("%s", line);
  1070. line[0] = 0;
  1071. }
  1072. }
  1073. if (line[0] != 0)
  1074. LOG_DEBUG("%s", line);
  1075. }
  1076. /************************* SWD related stuff **********/
  1077. static int buspirate_swd_init(void)
  1078. {
  1079. LOG_INFO("Buspirate SWD mode enabled");
  1080. swd_mode = true;
  1081. return ERROR_OK;
  1082. }
  1083. static int buspirate_swd_switch_seq(enum swd_special_seq seq)
  1084. {
  1085. const uint8_t *sequence;
  1086. int sequence_len;
  1087. uint32_t no_bytes, sequence_offset;
  1088. switch (seq) {
  1089. case LINE_RESET:
  1090. LOG_DEBUG("SWD line reset");
  1091. sequence = swd_seq_line_reset;
  1092. sequence_len = DIV_ROUND_UP(swd_seq_line_reset_len, 8);
  1093. break;
  1094. case JTAG_TO_SWD:
  1095. LOG_DEBUG("JTAG-to-SWD");
  1096. sequence = swd_seq_jtag_to_swd;
  1097. sequence_len = DIV_ROUND_UP(swd_seq_jtag_to_swd_len, 8);
  1098. break;
  1099. case SWD_TO_JTAG:
  1100. LOG_DEBUG("SWD-to-JTAG");
  1101. sequence = swd_seq_swd_to_jtag;
  1102. sequence_len = DIV_ROUND_UP(swd_seq_swd_to_jtag_len, 8);
  1103. break;
  1104. default:
  1105. LOG_ERROR("Sequence %d not supported", seq);
  1106. return ERROR_FAIL;
  1107. }
  1108. no_bytes = sequence_len;
  1109. sequence_offset = 0;
  1110. while (no_bytes) {
  1111. uint8_t tmp[17];
  1112. uint32_t to_send;
  1113. to_send = no_bytes > 16 ? 16 : no_bytes;
  1114. tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
  1115. memcpy(tmp + 1, &sequence[sequence_offset], to_send);
  1116. buspirate_serial_write(buspirate_fd, tmp, to_send + 1);
  1117. buspirate_serial_read(buspirate_fd, tmp, to_send + 1);
  1118. no_bytes -= to_send;
  1119. sequence_offset += to_send;
  1120. }
  1121. return ERROR_OK;
  1122. }
  1123. static uint8_t buspirate_swd_write_header(uint8_t cmd)
  1124. {
  1125. uint8_t tmp[8];
  1126. int to_send;
  1127. tmp[0] = 0x10; /* bus pirate: send 1 byte */
  1128. tmp[1] = cmd; /* swd cmd */
  1129. tmp[2] = 0x07; /* ack __x */
  1130. tmp[3] = 0x07; /* ack _x_ */
  1131. tmp[4] = 0x07; /* ack x__ */
  1132. tmp[5] = 0x07; /* write mode trn_1 */
  1133. tmp[6] = 0x07; /* write mode trn_2 */
  1134. to_send = ((cmd & SWD_CMD_RNW) == 0) ? 7 : 5;
  1135. buspirate_serial_write(buspirate_fd, tmp, to_send);
  1136. /* read ack */
  1137. buspirate_serial_read(buspirate_fd, tmp, 2); /* drop pirate command ret vals */
  1138. buspirate_serial_read(buspirate_fd, tmp, to_send - 2); /* ack bits */
  1139. return tmp[2] << 2 | tmp[1] << 1 | tmp[0];
  1140. }
  1141. static void buspirate_swd_idle_clocks(uint32_t no_bits)
  1142. {
  1143. uint32_t no_bytes;
  1144. uint8_t tmp[20];
  1145. no_bytes = (no_bits + 7) / 8;
  1146. memset(tmp + 1, 0x00, sizeof(tmp) - 1);
  1147. /* unfortunately bus pirate misbehaves when clocks are sent in parts
  1148. * so we need to limit at 128 clock cycles
  1149. */
  1150. if (no_bytes > 16)
  1151. no_bytes = 16;
  1152. while (no_bytes) {
  1153. uint8_t to_send = no_bytes > 16 ? 16 : no_bytes;
  1154. tmp[0] = 0x10 + ((to_send - 1) & 0x0F);
  1155. buspirate_serial_write(buspirate_fd, tmp, to_send + 1);
  1156. buspirate_serial_read(buspirate_fd, tmp, to_send + 1);
  1157. no_bytes -= to_send;
  1158. }
  1159. }
  1160. static void buspirate_swd_clear_sticky_errors(void)
  1161. {
  1162. buspirate_swd_write_reg(swd_cmd(false, false, DP_ABORT),
  1163. STKCMPCLR | STKERRCLR | WDERRCLR | ORUNERRCLR, 0);
  1164. }
  1165. static void buspirate_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
  1166. {
  1167. uint8_t tmp[16];
  1168. LOG_DEBUG("buspirate_swd_read_reg");
  1169. assert(cmd & SWD_CMD_RNW);
  1170. if (queued_retval != ERROR_OK) {
  1171. LOG_DEBUG("Skip buspirate_swd_read_reg because queued_retval=%d", queued_retval);
  1172. return;
  1173. }
  1174. cmd |= SWD_CMD_START | SWD_CMD_PARK;
  1175. uint8_t ack = buspirate_swd_write_header(cmd);
  1176. /* do a read transaction */
  1177. tmp[0] = 0x06; /* 4 data bytes */
  1178. tmp[1] = 0x06;
  1179. tmp[2] = 0x06;
  1180. tmp[3] = 0x06;
  1181. tmp[4] = 0x07; /* parity bit */
  1182. tmp[5] = 0x21; /* 2 turnaround clocks */
  1183. buspirate_serial_write(buspirate_fd, tmp, 6);
  1184. buspirate_serial_read(buspirate_fd, tmp, 6);
  1185. /* store the data and parity */
  1186. uint32_t data = (uint8_t) tmp[0];
  1187. data |= (uint8_t) tmp[1] << 8;
  1188. data |= (uint8_t) tmp[2] << 16;
  1189. data |= (uint8_t) tmp[3] << 24;
  1190. int parity = tmp[4] ? 0x01 : 0x00;
  1191. LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
  1192. ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
  1193. cmd & SWD_CMD_APNDP ? "AP" : "DP",
  1194. cmd & SWD_CMD_RNW ? "read" : "write",
  1195. (cmd & SWD_CMD_A32) >> 1,
  1196. data);
  1197. switch (ack) {
  1198. case SWD_ACK_OK:
  1199. if (parity != parity_u32(data)) {
  1200. LOG_DEBUG("Read data parity mismatch %x %x", parity, parity_u32(data));
  1201. queued_retval = ERROR_FAIL;
  1202. return;
  1203. }
  1204. if (value)
  1205. *value = data;
  1206. if (cmd & SWD_CMD_APNDP)
  1207. buspirate_swd_idle_clocks(ap_delay_clk);
  1208. return;
  1209. case SWD_ACK_WAIT:
  1210. LOG_DEBUG("SWD_ACK_WAIT");
  1211. buspirate_swd_clear_sticky_errors();
  1212. return;
  1213. case SWD_ACK_FAULT:
  1214. LOG_DEBUG("SWD_ACK_FAULT");
  1215. queued_retval = ack;
  1216. return;
  1217. default:
  1218. LOG_DEBUG("No valid acknowledge: ack=%d", ack);
  1219. queued_retval = ack;
  1220. return;
  1221. }
  1222. }
  1223. static void buspirate_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
  1224. {
  1225. uint8_t tmp[16];
  1226. LOG_DEBUG("buspirate_swd_write_reg");
  1227. assert(!(cmd & SWD_CMD_RNW));
  1228. if (queued_retval != ERROR_OK) {
  1229. LOG_DEBUG("Skip buspirate_swd_write_reg because queued_retval=%d", queued_retval);
  1230. return;
  1231. }
  1232. cmd |= SWD_CMD_START | SWD_CMD_PARK;
  1233. uint8_t ack = buspirate_swd_write_header(cmd);
  1234. /* do a write transaction */
  1235. tmp[0] = 0x10 + ((4 + 1 - 1) & 0xF); /* bus pirate: send 4+1 bytes */
  1236. buf_set_u32((uint8_t *) tmp + 1, 0, 32, value);
  1237. /* write sequence ends with parity bit and 7 idle ticks */
  1238. tmp[5] = parity_u32(value) ? 0x01 : 0x00;
  1239. buspirate_serial_write(buspirate_fd, tmp, 6);
  1240. buspirate_serial_read(buspirate_fd, tmp, 6);
  1241. LOG_DEBUG("%s %s %s reg %X = %08"PRIx32,
  1242. ack == SWD_ACK_OK ? "OK" : ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK",
  1243. cmd & SWD_CMD_APNDP ? "AP" : "DP",
  1244. cmd & SWD_CMD_RNW ? "read" : "write",
  1245. (cmd & SWD_CMD_A32) >> 1,
  1246. value);
  1247. switch (ack) {
  1248. case SWD_ACK_OK:
  1249. if (cmd & SWD_CMD_APNDP)
  1250. buspirate_swd_idle_clocks(ap_delay_clk);
  1251. return;
  1252. case SWD_ACK_WAIT:
  1253. LOG_DEBUG("SWD_ACK_WAIT");
  1254. buspirate_swd_clear_sticky_errors();
  1255. return;
  1256. case SWD_ACK_FAULT:
  1257. LOG_DEBUG("SWD_ACK_FAULT");
  1258. queued_retval = ack;
  1259. return;
  1260. default:
  1261. LOG_DEBUG("No valid acknowledge: ack=%d", ack);
  1262. queued_retval = ack;
  1263. return;
  1264. }
  1265. }
  1266. static int buspirate_swd_run_queue(void)
  1267. {
  1268. LOG_DEBUG("buspirate_swd_run_queue");
  1269. /* A transaction must be followed by another transaction or at least 8 idle cycles to
  1270. * ensure that data is clocked through the AP. */
  1271. buspirate_swd_idle_clocks(8);
  1272. int retval = queued_retval;
  1273. queued_retval = ERROR_OK;
  1274. LOG_DEBUG("SWD queue return value: %02x", retval);
  1275. return retval;
  1276. }