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.
 
 
 
 
 
 

793 lines
21 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 by Dimitar Dimitrov <dinuxbg@gmail.com> *
  3. * based on Dominic Rath's and Benedikt Sauter's usbprog.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 <usb.h>
  26. #include "usb_common.h"
  27. #define USB_VID 0x15ba
  28. #define USB_PID 0x001e
  29. #define ARMJTAGEW_EPT_BULK_OUT 0x01u
  30. #define ARMJTAGEW_EPT_BULK_IN 0x82u
  31. #define ARMJTAGEW_USB_TIMEOUT 2000
  32. #define ARMJTAGEW_IN_BUFFER_SIZE (4*1024)
  33. #define ARMJTAGEW_OUT_BUFFER_SIZE (4*1024)
  34. /* USB command request codes. */
  35. #define CMD_GET_VERSION 0x00
  36. #define CMD_SELECT_DPIMPL 0x10
  37. #define CMD_SET_TCK_FREQUENCY 0x11
  38. #define CMD_GET_TCK_FREQUENCY 0x12
  39. #define CMD_MEASURE_MAX_TCK_FREQ 0x15
  40. #define CMD_MEASURE_RTCK_RESPONSE 0x16
  41. #define CMD_TAP_SHIFT 0x17
  42. #define CMD_SET_TAPHW_STATE 0x20
  43. #define CMD_GET_TAPHW_STATE 0x21
  44. #define CMD_TGPWR_SETUP 0x22
  45. /* Global USB buffers */
  46. static uint8_t usb_in_buffer[ARMJTAGEW_IN_BUFFER_SIZE];
  47. static uint8_t usb_out_buffer[ARMJTAGEW_OUT_BUFFER_SIZE];
  48. /* Queue command functions */
  49. static void armjtagew_end_state(tap_state_t state);
  50. static void armjtagew_state_move(void);
  51. static void armjtagew_path_move(int num_states, tap_state_t *path);
  52. static void armjtagew_runtest(int num_cycles);
  53. static void armjtagew_scan(bool ir_scan,
  54. enum scan_type type,
  55. uint8_t *buffer,
  56. int scan_size,
  57. struct scan_command *command);
  58. static void armjtagew_reset(int trst, int srst);
  59. /* static void armjtagew_simple_command(uint8_t command); */
  60. static int armjtagew_get_status(void);
  61. /* tap buffer functions */
  62. static void armjtagew_tap_init(void);
  63. static int armjtagew_tap_execute(void);
  64. static void armjtagew_tap_ensure_space(int scans, int bits);
  65. static void armjtagew_tap_append_step(int tms, int tdi);
  66. static void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command);
  67. /* ARM-JTAG-EW lowlevel functions */
  68. struct armjtagew {
  69. struct usb_dev_handle *usb_handle;
  70. };
  71. static struct armjtagew *armjtagew_usb_open(void);
  72. static void armjtagew_usb_close(struct armjtagew *armjtagew);
  73. static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, int in_length);
  74. static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length);
  75. static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length);
  76. /* helper functions */
  77. static int armjtagew_get_version_info(void);
  78. #ifdef _DEBUG_USB_COMMS_
  79. static void armjtagew_debug_buffer(uint8_t *buffer, int length);
  80. #endif
  81. static struct armjtagew *armjtagew_handle;
  82. /**************************************************************************
  83. * External interface implementation */
  84. static int armjtagew_execute_queue(void)
  85. {
  86. struct jtag_command *cmd = jtag_command_queue;
  87. int scan_size;
  88. enum scan_type type;
  89. uint8_t *buffer;
  90. while (cmd != NULL) {
  91. switch (cmd->type) {
  92. case JTAG_RUNTEST:
  93. DEBUG_JTAG_IO("runtest %i cycles, end in %i",
  94. cmd->cmd.runtest->num_cycles, \
  95. cmd->cmd.runtest->end_state);
  96. armjtagew_end_state(cmd->cmd.runtest->end_state);
  97. armjtagew_runtest(cmd->cmd.runtest->num_cycles);
  98. break;
  99. case JTAG_TLR_RESET:
  100. DEBUG_JTAG_IO("statemove end in %i", cmd->cmd.statemove->end_state);
  101. armjtagew_end_state(cmd->cmd.statemove->end_state);
  102. armjtagew_state_move();
  103. break;
  104. case JTAG_PATHMOVE:
  105. DEBUG_JTAG_IO("pathmove: %i states, end in %i", \
  106. cmd->cmd.pathmove->num_states, \
  107. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  108. armjtagew_path_move(cmd->cmd.pathmove->num_states,
  109. cmd->cmd.pathmove->path);
  110. break;
  111. case JTAG_SCAN:
  112. DEBUG_JTAG_IO("scan end in %i", cmd->cmd.scan->end_state);
  113. armjtagew_end_state(cmd->cmd.scan->end_state);
  114. scan_size = jtag_build_buffer(cmd->cmd.scan, &buffer);
  115. DEBUG_JTAG_IO("scan input, length = %d", scan_size);
  116. #ifdef _DEBUG_USB_COMMS_
  117. armjtagew_debug_buffer(buffer, (scan_size + 7) / 8);
  118. #endif
  119. type = jtag_scan_type(cmd->cmd.scan);
  120. armjtagew_scan(cmd->cmd.scan->ir_scan,
  121. type, buffer,
  122. scan_size, cmd->cmd.scan);
  123. break;
  124. case JTAG_RESET:
  125. DEBUG_JTAG_IO("reset trst: %i srst %i",
  126. cmd->cmd.reset->trst,
  127. cmd->cmd.reset->srst);
  128. armjtagew_tap_execute();
  129. if (cmd->cmd.reset->trst == 1)
  130. tap_set_state(TAP_RESET);
  131. armjtagew_reset(cmd->cmd.reset->trst, cmd->cmd.reset->srst);
  132. break;
  133. case JTAG_SLEEP:
  134. DEBUG_JTAG_IO("sleep %i", cmd->cmd.sleep->us);
  135. armjtagew_tap_execute();
  136. jtag_sleep(cmd->cmd.sleep->us);
  137. break;
  138. default:
  139. LOG_ERROR("BUG: unknown JTAG command type encountered");
  140. exit(-1);
  141. }
  142. cmd = cmd->next;
  143. }
  144. return armjtagew_tap_execute();
  145. }
  146. /* Sets speed in kHz. */
  147. static int armjtagew_speed(int speed)
  148. {
  149. int result;
  150. int speed_real;
  151. usb_out_buffer[0] = CMD_SET_TCK_FREQUENCY;
  152. buf_set_u32(usb_out_buffer + 1, 0, 32, speed*1000);
  153. result = armjtagew_usb_message(armjtagew_handle, 5, 4);
  154. if (result < 0) {
  155. LOG_ERROR("ARM-JTAG-EW setting speed failed (%d)", result);
  156. return ERROR_JTAG_DEVICE_ERROR;
  157. }
  158. usb_out_buffer[0] = CMD_GET_TCK_FREQUENCY;
  159. result = armjtagew_usb_message(armjtagew_handle, 1, 4);
  160. speed_real = (int)buf_get_u32(usb_in_buffer, 0, 32) / 1000;
  161. if (result < 0) {
  162. LOG_ERROR("ARM-JTAG-EW getting speed failed (%d)", result);
  163. return ERROR_JTAG_DEVICE_ERROR;
  164. } else
  165. LOG_INFO("Requested speed %dkHz, emulator reported %dkHz.", speed, speed_real);
  166. return ERROR_OK;
  167. }
  168. static int armjtagew_khz(int khz, int *jtag_speed)
  169. {
  170. *jtag_speed = khz;
  171. return ERROR_OK;
  172. }
  173. static int armjtagew_speed_div(int speed, int *khz)
  174. {
  175. *khz = speed;
  176. return ERROR_OK;
  177. }
  178. static int armjtagew_init(void)
  179. {
  180. int check_cnt;
  181. armjtagew_handle = armjtagew_usb_open();
  182. if (armjtagew_handle == 0) {
  183. LOG_ERROR(
  184. "Cannot find ARM-JTAG-EW Interface! Please check connection and permissions.");
  185. return ERROR_JTAG_INIT_FAILED;
  186. }
  187. check_cnt = 0;
  188. while (check_cnt < 3) {
  189. if (armjtagew_get_version_info() == ERROR_OK) {
  190. /* attempt to get status */
  191. armjtagew_get_status();
  192. break;
  193. }
  194. check_cnt++;
  195. }
  196. if (check_cnt == 3)
  197. LOG_INFO("ARM-JTAG-EW initial read failed, don't worry");
  198. /* Initial JTAG speed (for reset and initialization): 32 kHz */
  199. armjtagew_speed(32);
  200. LOG_INFO("ARM-JTAG-EW JTAG Interface ready");
  201. armjtagew_reset(0, 0);
  202. armjtagew_tap_init();
  203. return ERROR_OK;
  204. }
  205. static int armjtagew_quit(void)
  206. {
  207. armjtagew_usb_close(armjtagew_handle);
  208. return ERROR_OK;
  209. }
  210. /**************************************************************************
  211. * Queue command implementations */
  212. static void armjtagew_end_state(tap_state_t state)
  213. {
  214. if (tap_is_state_stable(state))
  215. tap_set_end_state(state);
  216. else {
  217. LOG_ERROR("BUG: %i is not a valid end state", state);
  218. exit(-1);
  219. }
  220. }
  221. /* Goes to the end state. */
  222. static void armjtagew_state_move(void)
  223. {
  224. int i;
  225. int tms = 0;
  226. uint8_t tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  227. int tms_count = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  228. for (i = 0; i < tms_count; i++) {
  229. tms = (tms_scan >> i) & 1;
  230. armjtagew_tap_append_step(tms, 0);
  231. }
  232. tap_set_state(tap_get_end_state());
  233. }
  234. static void armjtagew_path_move(int num_states, tap_state_t *path)
  235. {
  236. int i;
  237. for (i = 0; i < num_states; i++) {
  238. /*
  239. * TODO: The ARM-JTAG-EW hardware delays TDI with 3 TCK cycles when in RTCK mode.
  240. * Either handle that here, or update the documentation with examples
  241. * how to fix that in the configuration files.
  242. */
  243. if (path[i] == tap_state_transition(tap_get_state(), false))
  244. armjtagew_tap_append_step(0, 0);
  245. else if (path[i] == tap_state_transition(tap_get_state(), true))
  246. armjtagew_tap_append_step(1, 0);
  247. else {
  248. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition",
  249. tap_state_name(tap_get_state()), tap_state_name(path[i]));
  250. exit(-1);
  251. }
  252. tap_set_state(path[i]);
  253. }
  254. tap_set_end_state(tap_get_state());
  255. }
  256. static void armjtagew_runtest(int num_cycles)
  257. {
  258. int i;
  259. tap_state_t saved_end_state = tap_get_end_state();
  260. /* only do a state_move when we're not already in IDLE */
  261. if (tap_get_state() != TAP_IDLE) {
  262. armjtagew_end_state(TAP_IDLE);
  263. armjtagew_state_move();
  264. }
  265. /* execute num_cycles */
  266. for (i = 0; i < num_cycles; i++)
  267. armjtagew_tap_append_step(0, 0);
  268. /* finish in end_state */
  269. armjtagew_end_state(saved_end_state);
  270. if (tap_get_state() != tap_get_end_state())
  271. armjtagew_state_move();
  272. }
  273. static void armjtagew_scan(bool ir_scan,
  274. enum scan_type type,
  275. uint8_t *buffer,
  276. int scan_size,
  277. struct scan_command *command)
  278. {
  279. tap_state_t saved_end_state;
  280. armjtagew_tap_ensure_space(1, scan_size + 8);
  281. saved_end_state = tap_get_end_state();
  282. /* Move to appropriate scan state */
  283. armjtagew_end_state(ir_scan ? TAP_IRSHIFT : TAP_DRSHIFT);
  284. armjtagew_state_move();
  285. armjtagew_end_state(saved_end_state);
  286. /* Scan */
  287. armjtagew_tap_append_scan(scan_size, buffer, command);
  288. /* We are in Exit1, go to Pause */
  289. armjtagew_tap_append_step(0, 0);
  290. tap_set_state(ir_scan ? TAP_IRPAUSE : TAP_DRPAUSE);
  291. if (tap_get_state() != tap_get_end_state())
  292. armjtagew_state_move();
  293. }
  294. static void armjtagew_reset(int trst, int srst)
  295. {
  296. const uint8_t trst_mask = (1u << 5);
  297. const uint8_t srst_mask = (1u << 6);
  298. uint8_t val = 0;
  299. uint8_t outp_en = 0;
  300. uint8_t change_mask = 0;
  301. int result;
  302. LOG_DEBUG("trst: %i, srst: %i", trst, srst);
  303. if (srst == 0) {
  304. val |= srst_mask;
  305. outp_en &= ~srst_mask; /* tristate */
  306. change_mask |= srst_mask;
  307. } else if (srst == 1) {
  308. val &= ~srst_mask;
  309. outp_en |= srst_mask;
  310. change_mask |= srst_mask;
  311. }
  312. if (trst == 0) {
  313. val |= trst_mask;
  314. outp_en &= ~trst_mask; /* tristate */
  315. change_mask |= trst_mask;
  316. } else if (trst == 1) {
  317. val &= ~trst_mask;
  318. outp_en |= trst_mask;
  319. change_mask |= trst_mask;
  320. }
  321. usb_out_buffer[0] = CMD_SET_TAPHW_STATE;
  322. usb_out_buffer[1] = val;
  323. usb_out_buffer[2] = outp_en;
  324. usb_out_buffer[3] = change_mask;
  325. result = armjtagew_usb_write(armjtagew_handle, 4);
  326. if (result != 4)
  327. LOG_ERROR("ARM-JTAG-EW TRST/SRST pin set failed failed (%d)", result);
  328. }
  329. static int armjtagew_get_status(void)
  330. {
  331. int result;
  332. usb_out_buffer[0] = CMD_GET_TAPHW_STATE;
  333. result = armjtagew_usb_message(armjtagew_handle, 1, 12);
  334. if (result == 0) {
  335. unsigned int u_tg = buf_get_u32(usb_in_buffer, 0, 16);
  336. LOG_INFO(
  337. "U_tg = %d mV, U_aux = %d mV, U_tgpwr = %d mV, I_tgpwr = %d mA, D1 = %d, Target power %s %s",
  338. (int)(buf_get_u32(usb_in_buffer + 0, 0, 16)),
  339. (int)(buf_get_u32(usb_in_buffer + 2, 0, 16)),
  340. (int)(buf_get_u32(usb_in_buffer + 4, 0, 16)),
  341. (int)(buf_get_u32(usb_in_buffer + 6, 0, 16)),
  342. usb_in_buffer[9],
  343. usb_in_buffer[11] ? "OVERCURRENT" : "OK",
  344. usb_in_buffer[10] ? "enabled" : "disabled");
  345. if (u_tg < 1500)
  346. LOG_ERROR("Vref too low. Check Target Power");
  347. } else
  348. LOG_ERROR("ARM-JTAG-EW command CMD_GET_TAPHW_STATE failed (%d)", result);
  349. return ERROR_OK;
  350. }
  351. static int armjtagew_get_version_info(void)
  352. {
  353. int result;
  354. char sn[16];
  355. char auxinfo[257];
  356. /* query hardware version */
  357. usb_out_buffer[0] = CMD_GET_VERSION;
  358. result = armjtagew_usb_message(armjtagew_handle, 1, 4 + 15 + 256);
  359. if (result != 0) {
  360. LOG_ERROR("ARM-JTAG-EW command CMD_GET_VERSION failed (%d)", result);
  361. return ERROR_JTAG_DEVICE_ERROR;
  362. }
  363. memcpy(sn, usb_in_buffer + 4, 15);
  364. sn[15] = '\0';
  365. memcpy(auxinfo, usb_in_buffer + 4+15, 256);
  366. auxinfo[256] = '\0';
  367. LOG_INFO(
  368. "ARM-JTAG-EW firmware version %d.%d, hardware revision %c, SN=%s, Additional info: %s", \
  369. usb_in_buffer[1],
  370. usb_in_buffer[0], \
  371. isgraph(usb_in_buffer[2]) ? usb_in_buffer[2] : 'X', \
  372. sn,
  373. auxinfo);
  374. if (1 != usb_in_buffer[1] || 6 != usb_in_buffer[0])
  375. LOG_WARNING(
  376. "ARM-JTAG-EW firmware version %d.%d is untested with this version of OpenOCD. You might experience unexpected behavior.",
  377. usb_in_buffer[1],
  378. usb_in_buffer[0]);
  379. return ERROR_OK;
  380. }
  381. COMMAND_HANDLER(armjtagew_handle_armjtagew_info_command)
  382. {
  383. if (armjtagew_get_version_info() == ERROR_OK) {
  384. /* attempt to get status */
  385. armjtagew_get_status();
  386. }
  387. return ERROR_OK;
  388. }
  389. static const struct command_registration armjtagew_command_handlers[] = {
  390. {
  391. .name = "armjtagew_info",
  392. .handler = &armjtagew_handle_armjtagew_info_command,
  393. .mode = COMMAND_EXEC,
  394. .help = "query armjtagew info",
  395. },
  396. COMMAND_REGISTRATION_DONE
  397. };
  398. struct jtag_interface armjtagew_interface = {
  399. .name = "arm-jtag-ew",
  400. .commands = armjtagew_command_handlers,
  401. .transports = jtag_only,
  402. .execute_queue = armjtagew_execute_queue,
  403. .speed = armjtagew_speed,
  404. .speed_div = armjtagew_speed_div,
  405. .khz = armjtagew_khz,
  406. .init = armjtagew_init,
  407. .quit = armjtagew_quit,
  408. };
  409. /**************************************************************************
  410. * ARM-JTAG-EW tap functions */
  411. /* 2048 is the max value we can use here */
  412. #define ARMJTAGEW_TAP_BUFFER_SIZE 2048
  413. static int tap_length;
  414. static uint8_t tms_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
  415. static uint8_t tdi_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
  416. static uint8_t tdo_buffer[ARMJTAGEW_TAP_BUFFER_SIZE];
  417. struct pending_scan_result {
  418. int first; /* First bit position in tdo_buffer to read */
  419. int length; /* Number of bits to read */
  420. struct scan_command *command; /* Corresponding scan command */
  421. uint8_t *buffer;
  422. };
  423. #define MAX_PENDING_SCAN_RESULTS 256
  424. static int pending_scan_results_length;
  425. static struct pending_scan_result pending_scan_results_buffer[MAX_PENDING_SCAN_RESULTS];
  426. static int last_tms;
  427. static void armjtagew_tap_init(void)
  428. {
  429. tap_length = 0;
  430. pending_scan_results_length = 0;
  431. }
  432. static void armjtagew_tap_ensure_space(int scans, int bits)
  433. {
  434. int available_scans = MAX_PENDING_SCAN_RESULTS - pending_scan_results_length;
  435. int available_bits = ARMJTAGEW_TAP_BUFFER_SIZE * 8 - tap_length;
  436. if (scans > available_scans || bits > available_bits)
  437. armjtagew_tap_execute();
  438. }
  439. static void armjtagew_tap_append_step(int tms, int tdi)
  440. {
  441. last_tms = tms;
  442. int index_local = tap_length / 8;
  443. if (index_local < ARMJTAGEW_TAP_BUFFER_SIZE) {
  444. int bit_index = tap_length % 8;
  445. uint8_t bit = 1 << bit_index;
  446. if (tms)
  447. tms_buffer[index_local] |= bit;
  448. else
  449. tms_buffer[index_local] &= ~bit;
  450. if (tdi)
  451. tdi_buffer[index_local] |= bit;
  452. else
  453. tdi_buffer[index_local] &= ~bit;
  454. tap_length++;
  455. } else
  456. LOG_ERROR("armjtagew_tap_append_step, overflow");
  457. }
  458. void armjtagew_tap_append_scan(int length, uint8_t *buffer, struct scan_command *command)
  459. {
  460. struct pending_scan_result *pending_scan_result =
  461. &pending_scan_results_buffer[pending_scan_results_length];
  462. int i;
  463. pending_scan_result->first = tap_length;
  464. pending_scan_result->length = length;
  465. pending_scan_result->command = command;
  466. pending_scan_result->buffer = buffer;
  467. for (i = 0; i < length; i++)
  468. armjtagew_tap_append_step((i < length-1 ? 0 : 1), (buffer[i/8] >> (i%8)) & 1);
  469. pending_scan_results_length++;
  470. }
  471. /* Pad and send a tap sequence to the device, and receive the answer.
  472. * For the purpose of padding we assume that we are in idle or pause state. */
  473. static int armjtagew_tap_execute(void)
  474. {
  475. int byte_length;
  476. int tms_offset;
  477. int tdi_offset;
  478. int i;
  479. int result;
  480. if (tap_length > 0) {
  481. /* Pad last byte so that tap_length is divisible by 8 */
  482. while (tap_length % 8 != 0) {
  483. /* More of the last TMS value keeps us in the same state,
  484. * analogous to free-running JTAG interfaces. */
  485. armjtagew_tap_append_step(last_tms, 0);
  486. }
  487. byte_length = tap_length / 8;
  488. usb_out_buffer[0] = CMD_TAP_SHIFT;
  489. buf_set_u32(usb_out_buffer + 1, 0, 16, byte_length);
  490. tms_offset = 3;
  491. for (i = 0; i < byte_length; i++)
  492. usb_out_buffer[tms_offset + i] = flip_u32(tms_buffer[i], 8);
  493. tdi_offset = tms_offset + byte_length;
  494. for (i = 0; i < byte_length; i++)
  495. usb_out_buffer[tdi_offset + i] = flip_u32(tdi_buffer[i], 8);
  496. result = armjtagew_usb_message(armjtagew_handle,
  497. 3 + 2 * byte_length,
  498. byte_length + 4);
  499. if (result == 0) {
  500. int stat_local;
  501. stat_local = (int)buf_get_u32(usb_in_buffer + byte_length, 0, 32);
  502. if (stat_local) {
  503. LOG_ERROR(
  504. "armjtagew_tap_execute, emulator returned error code %d for a CMD_TAP_SHIFT command",
  505. stat_local);
  506. return ERROR_JTAG_QUEUE_FAILED;
  507. }
  508. for (i = 0; i < byte_length; i++)
  509. tdo_buffer[i] = flip_u32(usb_in_buffer[i], 8);
  510. for (i = 0; i < pending_scan_results_length; i++) {
  511. struct pending_scan_result *pending_scan_result =
  512. &pending_scan_results_buffer[i];
  513. uint8_t *buffer = pending_scan_result->buffer;
  514. int length = pending_scan_result->length;
  515. int first = pending_scan_result->first;
  516. struct scan_command *command = pending_scan_result->command;
  517. /* Copy to buffer */
  518. buf_set_buf(tdo_buffer, first, buffer, 0, length);
  519. DEBUG_JTAG_IO("pending scan result, length = %d", length);
  520. #ifdef _DEBUG_USB_COMMS_
  521. armjtagew_debug_buffer(buffer, byte_length);
  522. #endif
  523. if (jtag_read_buffer(buffer, command) != ERROR_OK) {
  524. armjtagew_tap_init();
  525. return ERROR_JTAG_QUEUE_FAILED;
  526. }
  527. if (pending_scan_result->buffer != NULL)
  528. free(pending_scan_result->buffer);
  529. }
  530. } else {
  531. LOG_ERROR("armjtagew_tap_execute, wrong result %d, expected %d",
  532. result,
  533. byte_length);
  534. return ERROR_JTAG_QUEUE_FAILED;
  535. }
  536. armjtagew_tap_init();
  537. }
  538. return ERROR_OK;
  539. }
  540. /****************************************************************************
  541. * JLink USB low-level functions */
  542. static struct armjtagew *armjtagew_usb_open()
  543. {
  544. usb_init();
  545. const uint16_t vids[] = { USB_VID, 0 };
  546. const uint16_t pids[] = { USB_PID, 0 };
  547. struct usb_dev_handle *dev;
  548. if (jtag_usb_open(vids, pids, &dev) != ERROR_OK)
  549. return NULL;
  550. struct armjtagew *result = malloc(sizeof(struct armjtagew));
  551. result->usb_handle = dev;
  552. #if 0
  553. /* usb_set_configuration required under win32 */
  554. usb_set_configuration(dev, dev->config[0].bConfigurationValue);
  555. #endif
  556. usb_claim_interface(dev, 0);
  557. #if 0
  558. /*
  559. * This makes problems under Mac OS X. And is not needed
  560. * under Windows. Hopefully this will not break a linux build
  561. */
  562. usb_set_altinterface(dev, 0);
  563. #endif
  564. return result;
  565. }
  566. static void armjtagew_usb_close(struct armjtagew *armjtagew)
  567. {
  568. usb_close(armjtagew->usb_handle);
  569. free(armjtagew);
  570. }
  571. /* Send a message and receive the reply. */
  572. static int armjtagew_usb_message(struct armjtagew *armjtagew, int out_length, int in_length)
  573. {
  574. int result;
  575. result = armjtagew_usb_write(armjtagew, out_length);
  576. if (result == out_length) {
  577. result = armjtagew_usb_read(armjtagew, in_length);
  578. if (result != in_length) {
  579. LOG_ERROR("usb_bulk_read failed (requested=%d, result=%d)",
  580. in_length,
  581. result);
  582. return -1;
  583. }
  584. } else {
  585. LOG_ERROR("usb_bulk_write failed (requested=%d, result=%d)", out_length, result);
  586. return -1;
  587. }
  588. return 0;
  589. }
  590. /* Write data from out_buffer to USB. */
  591. static int armjtagew_usb_write(struct armjtagew *armjtagew, int out_length)
  592. {
  593. int result;
  594. if (out_length > ARMJTAGEW_OUT_BUFFER_SIZE) {
  595. LOG_ERROR("armjtagew_write illegal out_length=%d (max=%d)",
  596. out_length,
  597. ARMJTAGEW_OUT_BUFFER_SIZE);
  598. return -1;
  599. }
  600. result = usb_bulk_write(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_OUT, \
  601. (char *)usb_out_buffer, out_length, ARMJTAGEW_USB_TIMEOUT);
  602. DEBUG_JTAG_IO("armjtagew_usb_write, out_length = %d, result = %d", out_length, result);
  603. #ifdef _DEBUG_USB_COMMS_
  604. armjtagew_debug_buffer(usb_out_buffer, out_length);
  605. #endif
  606. return result;
  607. }
  608. /* Read data from USB into in_buffer. */
  609. static int armjtagew_usb_read(struct armjtagew *armjtagew, int exp_in_length)
  610. {
  611. int result = usb_bulk_read(armjtagew->usb_handle, ARMJTAGEW_EPT_BULK_IN, \
  612. (char *)usb_in_buffer, exp_in_length, ARMJTAGEW_USB_TIMEOUT);
  613. DEBUG_JTAG_IO("armjtagew_usb_read, result = %d", result);
  614. #ifdef _DEBUG_USB_COMMS_
  615. armjtagew_debug_buffer(usb_in_buffer, result);
  616. #endif
  617. return result;
  618. }
  619. #ifdef _DEBUG_USB_COMMS_
  620. #define BYTES_PER_LINE 16
  621. static void armjtagew_debug_buffer(uint8_t *buffer, int length)
  622. {
  623. char line[81];
  624. char s[4];
  625. int i;
  626. int j;
  627. for (i = 0; i < length; i += BYTES_PER_LINE) {
  628. snprintf(line, 5, "%04x", i);
  629. for (j = i; j < i + BYTES_PER_LINE && j < length; j++) {
  630. snprintf(s, 4, " %02x", buffer[j]);
  631. strcat(line, s);
  632. }
  633. LOG_DEBUG("%s", line);
  634. /* Prevent GDB timeout (writing to log might take some time) */
  635. keep_alive();
  636. }
  637. }
  638. #endif