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.
 
 
 
 
 
 

1647 lines
44 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2016 by Maksym Hilliaka *
  3. * oter@frozen-team.com *
  4. * *
  5. * Copyright (C) 2016 by Phillip Pearson *
  6. * pp@myelin.co.nz *
  7. * *
  8. * Copyright (C) 2014 by Paul Fertser *
  9. * fercerpav@gmail.com *
  10. * *
  11. * Copyright (C) 2013 by mike brown *
  12. * mike@theshedworks.org.uk *
  13. * *
  14. * Copyright (C) 2013 by Spencer Oliver *
  15. * spen@spen-soft.co.uk *
  16. * *
  17. * This program is free software; you can redistribute it and/or modify *
  18. * it under the terms of the GNU General Public License as published by *
  19. * the Free Software Foundation; either version 2 of the License, or *
  20. * (at your option) any later version. *
  21. * *
  22. * This program is distributed in the hope that it will be useful, *
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  25. * GNU General Public License for more details. *
  26. * *
  27. * You should have received a copy of the GNU General Public License *
  28. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  29. ***************************************************************************/
  30. #ifdef HAVE_CONFIG_H
  31. #include "config.h"
  32. #endif
  33. #include <transport/transport.h>
  34. #include <jtag/swd.h>
  35. #include <jtag/interface.h>
  36. #include <jtag/commands.h>
  37. #include <jtag/tcl.h>
  38. #include <hidapi.h>
  39. /*
  40. * See CMSIS-DAP documentation:
  41. * Version 0.01 - Beta.
  42. */
  43. /* USB Config */
  44. /* Known vid/pid pairs:
  45. * VID 0xc251: Keil Software
  46. * PID 0xf001: LPC-Link-II CMSIS_DAP
  47. * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
  48. * PID 0x2722: Keil ULINK2 CMSIS-DAP
  49. *
  50. * VID 0x0d28: mbed Software
  51. * PID 0x0204: MBED CMSIS-DAP
  52. */
  53. #define MAX_USB_IDS 8
  54. /* vid = pid = 0 marks the end of the list */
  55. static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
  56. static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
  57. static wchar_t *cmsis_dap_serial;
  58. static bool swd_mode;
  59. #define PACKET_SIZE (64 + 1) /* 64 bytes plus report id */
  60. #define USB_TIMEOUT 1000
  61. /* CMSIS-DAP General Commands */
  62. #define CMD_DAP_INFO 0x00
  63. #define CMD_DAP_LED 0x01
  64. #define CMD_DAP_CONNECT 0x02
  65. #define CMD_DAP_DISCONNECT 0x03
  66. #define CMD_DAP_WRITE_ABORT 0x08
  67. #define CMD_DAP_DELAY 0x09
  68. #define CMD_DAP_RESET_TARGET 0x0A
  69. /* CMD_INFO */
  70. #define INFO_ID_VID 0x00 /* string */
  71. #define INFO_ID_PID 0x02 /* string */
  72. #define INFO_ID_SERNUM 0x03 /* string */
  73. #define INFO_ID_FW_VER 0x04 /* string */
  74. #define INFO_ID_TD_VEND 0x05 /* string */
  75. #define INFO_ID_TD_NAME 0x06 /* string */
  76. #define INFO_ID_CAPS 0xf0 /* byte */
  77. #define INFO_ID_PKT_CNT 0xfe /* byte */
  78. #define INFO_ID_PKT_SZ 0xff /* short */
  79. #define INFO_CAPS_SWD 0x01
  80. #define INFO_CAPS_JTAG 0x02
  81. /* CMD_LED */
  82. #define LED_ID_CONNECT 0x00
  83. #define LED_ID_RUN 0x01
  84. #define LED_OFF 0x00
  85. #define LED_ON 0x01
  86. /* CMD_CONNECT */
  87. #define CONNECT_DEFAULT 0x00
  88. #define CONNECT_SWD 0x01
  89. #define CONNECT_JTAG 0x02
  90. /* CMSIS-DAP Common SWD/JTAG Commands */
  91. #define CMD_DAP_DELAY 0x09
  92. #define CMD_DAP_SWJ_PINS 0x10
  93. #define CMD_DAP_SWJ_CLOCK 0x11
  94. #define CMD_DAP_SWJ_SEQ 0x12
  95. /*
  96. * PINS
  97. * Bit 0: SWCLK/TCK
  98. * Bit 1: SWDIO/TMS
  99. * Bit 2: TDI
  100. * Bit 3: TDO
  101. * Bit 5: nTRST
  102. * Bit 7: nRESET
  103. */
  104. #define SWJ_PIN_TCK (1<<0)
  105. #define SWJ_PIN_TMS (1<<1)
  106. #define SWJ_PIN_TDI (1<<2)
  107. #define SWJ_PIN_TDO (1<<3)
  108. #define SWJ_PIN_TRST (1<<5)
  109. #define SWJ_PIN_SRST (1<<7)
  110. /* CMSIS-DAP SWD Commands */
  111. #define CMD_DAP_SWD_CONFIGURE 0x13
  112. /* CMSIS-DAP JTAG Commands */
  113. #define CMD_DAP_JTAG_SEQ 0x14
  114. #define CMD_DAP_JTAG_CONFIGURE 0x15
  115. #define CMD_DAP_JTAG_IDCODE 0x16
  116. /* CMSIS-DAP JTAG sequence info masks */
  117. /* Number of bits to clock through (0 means 64) */
  118. #define DAP_JTAG_SEQ_TCK 0x3F
  119. /* TMS will be set during the sequence if this bit is set */
  120. #define DAP_JTAG_SEQ_TMS 0x40
  121. /* TDO output will be captured if this bit is set */
  122. #define DAP_JTAG_SEQ_TDO 0x80
  123. /* CMSIS-DAP Transfer Commands */
  124. #define CMD_DAP_TFER_CONFIGURE 0x04
  125. #define CMD_DAP_TFER 0x05
  126. #define CMD_DAP_TFER_BLOCK 0x06
  127. #define CMD_DAP_TFER_ABORT 0x07
  128. /* DAP Status Code */
  129. #define DAP_OK 0
  130. #define DAP_ERROR 0xFF
  131. /* CMSIS-DAP Vendor Commands
  132. * None as yet... */
  133. static const char * const info_caps_str[] = {
  134. "SWD Supported",
  135. "JTAG Supported"
  136. };
  137. /* max clock speed (kHz) */
  138. #define DAP_MAX_CLOCK 5000
  139. struct cmsis_dap {
  140. hid_device *dev_handle;
  141. uint16_t packet_size;
  142. uint16_t packet_count;
  143. uint8_t *packet_buffer;
  144. uint8_t caps;
  145. uint8_t mode;
  146. };
  147. struct pending_transfer_result {
  148. uint8_t cmd;
  149. uint32_t data;
  150. void *buffer;
  151. };
  152. struct pending_scan_result {
  153. /** Offset in bytes in the CMD_DAP_JTAG_SEQ response buffer. */
  154. unsigned first;
  155. /** Number of bits to read. */
  156. unsigned length;
  157. /** Location to store the result */
  158. uint8_t *buffer;
  159. /** Offset in the destination buffer */
  160. unsigned buffer_offset;
  161. };
  162. static int pending_transfer_count, pending_queue_len;
  163. static struct pending_transfer_result *pending_transfers;
  164. /* pointers to buffers that will receive jtag scan results on the next flush */
  165. #define MAX_PENDING_SCAN_RESULTS 256
  166. static int pending_scan_result_count;
  167. static struct pending_scan_result pending_scan_results[MAX_PENDING_SCAN_RESULTS];
  168. /* queued JTAG sequences that will be executed on the next flush */
  169. #define QUEUED_SEQ_BUF_LEN (cmsis_dap_handle->packet_size - 3)
  170. static int queued_seq_count;
  171. static int queued_seq_buf_end;
  172. static int queued_seq_tdo_ptr;
  173. static uint8_t queued_seq_buf[1024]; /* TODO: make dynamic / move into cmsis object */
  174. static int queued_retval;
  175. static struct cmsis_dap *cmsis_dap_handle;
  176. static int cmsis_dap_usb_open(void)
  177. {
  178. hid_device *dev = NULL;
  179. int i;
  180. struct hid_device_info *devs, *cur_dev;
  181. unsigned short target_vid, target_pid;
  182. wchar_t *target_serial = NULL;
  183. bool found = false;
  184. bool serial_found = false;
  185. target_vid = 0;
  186. target_pid = 0;
  187. /*
  188. * The CMSIS-DAP specification stipulates:
  189. * "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
  190. * debuggers to identify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
  191. */
  192. devs = hid_enumerate(0x0, 0x0);
  193. cur_dev = devs;
  194. while (NULL != cur_dev) {
  195. if (0 == cmsis_dap_vid[0]) {
  196. if (NULL == cur_dev->product_string) {
  197. LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
  198. cur_dev->vendor_id, cur_dev->product_id);
  199. } else {
  200. if (wcsstr(cur_dev->product_string, L"CMSIS-DAP")) {
  201. /* if the user hasn't specified VID:PID *and*
  202. * product string contains "CMSIS-DAP", pick it
  203. */
  204. found = true;
  205. }
  206. }
  207. } else {
  208. /* otherwise, exhaustively compare against all VID:PID in list */
  209. for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
  210. if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
  211. found = true;
  212. }
  213. if (cmsis_dap_vid[i] || cmsis_dap_pid[i])
  214. found = true;
  215. }
  216. if (found) {
  217. /* we have found an adapter, so exit further checks */
  218. /* check serial number matches if given */
  219. if (cmsis_dap_serial != NULL) {
  220. if ((cur_dev->serial_number != NULL) && wcscmp(cmsis_dap_serial, cur_dev->serial_number) == 0) {
  221. serial_found = true;
  222. break;
  223. }
  224. } else
  225. break;
  226. found = false;
  227. }
  228. cur_dev = cur_dev->next;
  229. }
  230. if (NULL != cur_dev) {
  231. target_vid = cur_dev->vendor_id;
  232. target_pid = cur_dev->product_id;
  233. if (serial_found)
  234. target_serial = cmsis_dap_serial;
  235. }
  236. hid_free_enumeration(devs);
  237. if (target_vid == 0 && target_pid == 0) {
  238. LOG_ERROR("unable to find CMSIS-DAP device");
  239. return ERROR_FAIL;
  240. }
  241. if (hid_init() != 0) {
  242. LOG_ERROR("unable to open HIDAPI");
  243. return ERROR_FAIL;
  244. }
  245. dev = hid_open(target_vid, target_pid, target_serial);
  246. if (dev == NULL) {
  247. LOG_ERROR("unable to open CMSIS-DAP device 0x%x:0x%x", target_vid, target_pid);
  248. return ERROR_FAIL;
  249. }
  250. struct cmsis_dap *dap = malloc(sizeof(struct cmsis_dap));
  251. if (dap == NULL) {
  252. LOG_ERROR("unable to allocate memory");
  253. return ERROR_FAIL;
  254. }
  255. dap->dev_handle = dev;
  256. dap->caps = 0;
  257. dap->mode = 0;
  258. cmsis_dap_handle = dap;
  259. /* allocate default packet buffer, may be changed later.
  260. * currently with HIDAPI we have no way of getting the output report length
  261. * without this info we cannot communicate with the adapter.
  262. * For the moment we ahve to hard code the packet size */
  263. int packet_size = PACKET_SIZE;
  264. /* atmel cmsis-dap uses 512 byte reports */
  265. /* TODO: HID report descriptor should be parsed instead of
  266. * hardcoding a match by VID */
  267. if (target_vid == 0x03eb)
  268. packet_size = 512 + 1;
  269. cmsis_dap_handle->packet_buffer = malloc(packet_size);
  270. cmsis_dap_handle->packet_size = packet_size;
  271. if (cmsis_dap_handle->packet_buffer == NULL) {
  272. LOG_ERROR("unable to allocate memory");
  273. return ERROR_FAIL;
  274. }
  275. return ERROR_OK;
  276. }
  277. static void cmsis_dap_usb_close(struct cmsis_dap *dap)
  278. {
  279. hid_close(dap->dev_handle);
  280. hid_exit();
  281. free(cmsis_dap_handle->packet_buffer);
  282. free(cmsis_dap_handle);
  283. cmsis_dap_handle = NULL;
  284. free(cmsis_dap_serial);
  285. cmsis_dap_serial = NULL;
  286. free(pending_transfers);
  287. pending_transfers = NULL;
  288. return;
  289. }
  290. /* Send a message and receive the reply */
  291. static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
  292. {
  293. #ifdef CMSIS_DAP_JTAG_DEBUG
  294. LOG_DEBUG("cmsis-dap usb xfer cmd=%02X", dap->packet_buffer[1]);
  295. #endif
  296. /* Pad the rest of the TX buffer with 0's */
  297. memset(dap->packet_buffer + txlen, 0, dap->packet_size - txlen);
  298. /* write data to device */
  299. int retval = hid_write(dap->dev_handle, dap->packet_buffer, dap->packet_size);
  300. if (retval == -1) {
  301. LOG_ERROR("error writing data: %ls", hid_error(dap->dev_handle));
  302. return ERROR_FAIL;
  303. }
  304. /* get reply */
  305. retval = hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, USB_TIMEOUT);
  306. if (retval == -1 || retval == 0) {
  307. LOG_DEBUG("error reading data: %ls", hid_error(dap->dev_handle));
  308. return ERROR_FAIL;
  309. }
  310. return ERROR_OK;
  311. }
  312. static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
  313. {
  314. int retval;
  315. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  316. buffer[0] = 0; /* report number */
  317. buffer[1] = CMD_DAP_SWJ_PINS;
  318. buffer[2] = pins;
  319. buffer[3] = mask;
  320. buffer[4] = delay & 0xff;
  321. buffer[5] = (delay >> 8) & 0xff;
  322. buffer[6] = (delay >> 16) & 0xff;
  323. buffer[7] = (delay >> 24) & 0xff;
  324. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 8);
  325. if (retval != ERROR_OK) {
  326. LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
  327. return ERROR_JTAG_DEVICE_ERROR;
  328. }
  329. if (input)
  330. *input = buffer[1];
  331. return ERROR_OK;
  332. }
  333. static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
  334. {
  335. int retval;
  336. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  337. /* set clock in Hz */
  338. swj_clock *= 1000;
  339. buffer[0] = 0; /* report number */
  340. buffer[1] = CMD_DAP_SWJ_CLOCK;
  341. buffer[2] = swj_clock & 0xff;
  342. buffer[3] = (swj_clock >> 8) & 0xff;
  343. buffer[4] = (swj_clock >> 16) & 0xff;
  344. buffer[5] = (swj_clock >> 24) & 0xff;
  345. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 6);
  346. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  347. LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
  348. return ERROR_JTAG_DEVICE_ERROR;
  349. }
  350. return ERROR_OK;
  351. }
  352. /* clock a sequence of bits out on TMS, to change JTAG states */
  353. static int cmsis_dap_cmd_DAP_SWJ_Sequence(uint8_t s_len, const uint8_t *sequence)
  354. {
  355. int retval;
  356. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  357. #ifdef CMSIS_DAP_JTAG_DEBUG
  358. LOG_DEBUG("cmsis-dap TMS sequence: len=%d", s_len);
  359. for (int i = 0; i < DIV_ROUND_UP(s_len, 8); ++i)
  360. printf("%02X ", sequence[i]);
  361. printf("\n");
  362. #endif
  363. buffer[0] = 0; /* report number */
  364. buffer[1] = CMD_DAP_SWJ_SEQ;
  365. buffer[2] = s_len;
  366. bit_copy(&buffer[3], 0, sequence, 0, s_len);
  367. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, DIV_ROUND_UP(s_len, 8) + 3);
  368. if (retval != ERROR_OK || buffer[1] != DAP_OK)
  369. return ERROR_FAIL;
  370. return ERROR_OK;
  371. }
  372. static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
  373. {
  374. int retval;
  375. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  376. buffer[0] = 0; /* report number */
  377. buffer[1] = CMD_DAP_INFO;
  378. buffer[2] = info;
  379. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
  380. if (retval != ERROR_OK) {
  381. LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
  382. return ERROR_JTAG_DEVICE_ERROR;
  383. }
  384. *data = &(buffer[1]);
  385. return ERROR_OK;
  386. }
  387. static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
  388. {
  389. int retval;
  390. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  391. buffer[0] = 0; /* report number */
  392. buffer[1] = CMD_DAP_LED;
  393. buffer[2] = 0x00;
  394. buffer[3] = leds;
  395. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
  396. if (retval != ERROR_OK || buffer[1] != 0x00) {
  397. LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
  398. return ERROR_JTAG_DEVICE_ERROR;
  399. }
  400. return ERROR_OK;
  401. }
  402. static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
  403. {
  404. int retval;
  405. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  406. buffer[0] = 0; /* report number */
  407. buffer[1] = CMD_DAP_CONNECT;
  408. buffer[2] = mode;
  409. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
  410. if (retval != ERROR_OK) {
  411. LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
  412. return ERROR_JTAG_DEVICE_ERROR;
  413. }
  414. if (buffer[1] != mode) {
  415. LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
  416. return ERROR_JTAG_DEVICE_ERROR;
  417. }
  418. return ERROR_OK;
  419. }
  420. static int cmsis_dap_cmd_DAP_Disconnect(void)
  421. {
  422. int retval;
  423. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  424. buffer[0] = 0; /* report number */
  425. buffer[1] = CMD_DAP_DISCONNECT;
  426. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
  427. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  428. LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
  429. return ERROR_JTAG_DEVICE_ERROR;
  430. }
  431. return ERROR_OK;
  432. }
  433. static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t retry_count, uint16_t match_retry)
  434. {
  435. int retval;
  436. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  437. buffer[0] = 0; /* report number */
  438. buffer[1] = CMD_DAP_TFER_CONFIGURE;
  439. buffer[2] = idle;
  440. buffer[3] = retry_count & 0xff;
  441. buffer[4] = (retry_count >> 8) & 0xff;
  442. buffer[5] = match_retry & 0xff;
  443. buffer[6] = (match_retry >> 8) & 0xff;
  444. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
  445. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  446. LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
  447. return ERROR_JTAG_DEVICE_ERROR;
  448. }
  449. return ERROR_OK;
  450. }
  451. static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
  452. {
  453. int retval;
  454. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  455. buffer[0] = 0; /* report number */
  456. buffer[1] = CMD_DAP_SWD_CONFIGURE;
  457. buffer[2] = cfg;
  458. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
  459. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  460. LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
  461. return ERROR_JTAG_DEVICE_ERROR;
  462. }
  463. return ERROR_OK;
  464. }
  465. #if 0
  466. static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
  467. {
  468. int retval;
  469. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  470. buffer[0] = 0; /* report number */
  471. buffer[1] = CMD_DAP_DELAY;
  472. buffer[2] = delay_us & 0xff;
  473. buffer[3] = (delay_us >> 8) & 0xff;
  474. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
  475. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  476. LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
  477. return ERROR_JTAG_DEVICE_ERROR;
  478. }
  479. return ERROR_OK;
  480. }
  481. #endif
  482. static int cmsis_dap_swd_run_queue(void)
  483. {
  484. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  485. LOG_DEBUG("Executing %d queued transactions", pending_transfer_count);
  486. if (queued_retval != ERROR_OK) {
  487. LOG_DEBUG("Skipping due to previous errors: %d", queued_retval);
  488. goto skip;
  489. }
  490. if (!pending_transfer_count)
  491. goto skip;
  492. size_t idx = 0;
  493. buffer[idx++] = 0; /* report number */
  494. buffer[idx++] = CMD_DAP_TFER;
  495. buffer[idx++] = 0x00; /* DAP Index */
  496. buffer[idx++] = pending_transfer_count;
  497. for (int i = 0; i < pending_transfer_count; i++) {
  498. uint8_t cmd = pending_transfers[i].cmd;
  499. uint32_t data = pending_transfers[i].data;
  500. LOG_DEBUG("%s %s reg %x %"PRIx32,
  501. cmd & SWD_CMD_APnDP ? "AP" : "DP",
  502. cmd & SWD_CMD_RnW ? "read" : "write",
  503. (cmd & SWD_CMD_A32) >> 1, data);
  504. /* When proper WAIT handling is implemented in the
  505. * common SWD framework, this kludge can be
  506. * removed. However, this might lead to minor
  507. * performance degradation as the adapter wouldn't be
  508. * able to automatically retry anything (because ARM
  509. * has forgotten to implement sticky error flags
  510. * clearing). See also comments regarding
  511. * cmsis_dap_cmd_DAP_TFER_Configure() and
  512. * cmsis_dap_cmd_DAP_SWD_Configure() in
  513. * cmsis_dap_init().
  514. */
  515. if (!(cmd & SWD_CMD_RnW) &&
  516. !(cmd & SWD_CMD_APnDP) &&
  517. (cmd & SWD_CMD_A32) >> 1 == DP_CTRL_STAT &&
  518. (data & CORUNDETECT)) {
  519. LOG_DEBUG("refusing to enable sticky overrun detection");
  520. data &= ~CORUNDETECT;
  521. }
  522. buffer[idx++] = (cmd >> 1) & 0x0f;
  523. if (!(cmd & SWD_CMD_RnW)) {
  524. buffer[idx++] = (data) & 0xff;
  525. buffer[idx++] = (data >> 8) & 0xff;
  526. buffer[idx++] = (data >> 16) & 0xff;
  527. buffer[idx++] = (data >> 24) & 0xff;
  528. }
  529. }
  530. queued_retval = cmsis_dap_usb_xfer(cmsis_dap_handle, idx);
  531. if (queued_retval != ERROR_OK)
  532. goto skip;
  533. idx = 2;
  534. uint8_t ack = buffer[idx] & 0x07;
  535. if (ack != SWD_ACK_OK || (buffer[idx] & 0x08)) {
  536. LOG_DEBUG("SWD ack not OK: %d %s", buffer[idx-1],
  537. ack == SWD_ACK_WAIT ? "WAIT" : ack == SWD_ACK_FAULT ? "FAULT" : "JUNK");
  538. queued_retval = ack == SWD_ACK_WAIT ? ERROR_WAIT : ERROR_FAIL;
  539. goto skip;
  540. }
  541. idx++;
  542. if (pending_transfer_count != buffer[1])
  543. LOG_ERROR("CMSIS-DAP transfer count mismatch: expected %d, got %d",
  544. pending_transfer_count, buffer[1]);
  545. for (int i = 0; i < buffer[1]; i++) {
  546. if (pending_transfers[i].cmd & SWD_CMD_RnW) {
  547. static uint32_t last_read;
  548. uint32_t data = le_to_h_u32(&buffer[idx]);
  549. uint32_t tmp = data;
  550. idx += 4;
  551. LOG_DEBUG("Read result: %"PRIx32, data);
  552. /* Imitate posted AP reads */
  553. if ((pending_transfers[i].cmd & SWD_CMD_APnDP) ||
  554. ((pending_transfers[i].cmd & SWD_CMD_A32) >> 1 == DP_RDBUFF)) {
  555. tmp = last_read;
  556. last_read = data;
  557. }
  558. if (pending_transfers[i].buffer)
  559. *(uint32_t *)pending_transfers[i].buffer = tmp;
  560. }
  561. }
  562. skip:
  563. pending_transfer_count = 0;
  564. int retval = queued_retval;
  565. queued_retval = ERROR_OK;
  566. return retval;
  567. }
  568. static void cmsis_dap_swd_queue_cmd(uint8_t cmd, uint32_t *dst, uint32_t data)
  569. {
  570. if (pending_transfer_count == pending_queue_len) {
  571. /* Not enough room in the queue. Run the queue. */
  572. queued_retval = cmsis_dap_swd_run_queue();
  573. }
  574. if (queued_retval != ERROR_OK)
  575. return;
  576. pending_transfers[pending_transfer_count].data = data;
  577. pending_transfers[pending_transfer_count].cmd = cmd;
  578. if (cmd & SWD_CMD_RnW) {
  579. /* Queue a read transaction */
  580. pending_transfers[pending_transfer_count].buffer = dst;
  581. }
  582. pending_transfer_count++;
  583. }
  584. static void cmsis_dap_swd_write_reg(uint8_t cmd, uint32_t value, uint32_t ap_delay_clk)
  585. {
  586. assert(!(cmd & SWD_CMD_RnW));
  587. cmsis_dap_swd_queue_cmd(cmd, NULL, value);
  588. }
  589. static void cmsis_dap_swd_read_reg(uint8_t cmd, uint32_t *value, uint32_t ap_delay_clk)
  590. {
  591. assert(cmd & SWD_CMD_RnW);
  592. cmsis_dap_swd_queue_cmd(cmd, value, 0);
  593. }
  594. static int cmsis_dap_get_version_info(void)
  595. {
  596. uint8_t *data;
  597. /* INFO_ID_FW_VER - string */
  598. int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER, &data);
  599. if (retval != ERROR_OK)
  600. return retval;
  601. if (data[0]) /* strlen */
  602. LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
  603. return ERROR_OK;
  604. }
  605. static int cmsis_dap_get_caps_info(void)
  606. {
  607. uint8_t *data;
  608. /* INFO_ID_CAPS - byte */
  609. int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS, &data);
  610. if (retval != ERROR_OK)
  611. return retval;
  612. if (data[0] == 1) {
  613. uint8_t caps = data[1];
  614. cmsis_dap_handle->caps = caps;
  615. if (caps & INFO_CAPS_SWD)
  616. LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
  617. if (caps & INFO_CAPS_JTAG)
  618. LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
  619. }
  620. return ERROR_OK;
  621. }
  622. static int cmsis_dap_get_status(void)
  623. {
  624. uint8_t d;
  625. int retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d);
  626. if (retval == ERROR_OK) {
  627. LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
  628. (d & SWJ_PIN_TCK) ? 1 : 0,
  629. (d & SWJ_PIN_TMS) ? 1 : 0,
  630. (d & SWJ_PIN_TDI) ? 1 : 0,
  631. (d & SWJ_PIN_TDO) ? 1 : 0,
  632. (d & SWJ_PIN_TRST) ? 1 : 0,
  633. (d & SWJ_PIN_SRST) ? 1 : 0);
  634. }
  635. return retval;
  636. }
  637. static int cmsis_dap_swd_switch_seq(enum swd_special_seq seq)
  638. {
  639. const uint8_t *s;
  640. unsigned int s_len;
  641. int retval;
  642. /* First disconnect before connecting, Atmel EDBG needs it for SAMD/R/L/C */
  643. cmsis_dap_cmd_DAP_Disconnect();
  644. /* When we are reconnecting, DAP_Connect needs to be rerun, at
  645. * least on Keil ULINK-ME */
  646. retval = cmsis_dap_cmd_DAP_Connect(seq == LINE_RESET || seq == JTAG_TO_SWD ?
  647. CONNECT_SWD : CONNECT_JTAG);
  648. if (retval != ERROR_OK)
  649. return retval;
  650. switch (seq) {
  651. case LINE_RESET:
  652. LOG_DEBUG("SWD line reset");
  653. s = swd_seq_line_reset;
  654. s_len = swd_seq_line_reset_len;
  655. break;
  656. case JTAG_TO_SWD:
  657. LOG_DEBUG("JTAG-to-SWD");
  658. s = swd_seq_jtag_to_swd;
  659. s_len = swd_seq_jtag_to_swd_len;
  660. break;
  661. case SWD_TO_JTAG:
  662. LOG_DEBUG("SWD-to-JTAG");
  663. s = swd_seq_swd_to_jtag;
  664. s_len = swd_seq_swd_to_jtag_len;
  665. break;
  666. default:
  667. LOG_ERROR("Sequence %d not supported", seq);
  668. return ERROR_FAIL;
  669. }
  670. return cmsis_dap_cmd_DAP_SWJ_Sequence(s_len, s);
  671. }
  672. static int cmsis_dap_swd_open(void)
  673. {
  674. int retval;
  675. if (cmsis_dap_handle == NULL) {
  676. /* SWD init */
  677. retval = cmsis_dap_usb_open();
  678. if (retval != ERROR_OK)
  679. return retval;
  680. retval = cmsis_dap_get_caps_info();
  681. if (retval != ERROR_OK)
  682. return retval;
  683. }
  684. if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
  685. LOG_ERROR("CMSIS-DAP: SWD not supported");
  686. return ERROR_JTAG_DEVICE_ERROR;
  687. }
  688. retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
  689. if (retval != ERROR_OK)
  690. return retval;
  691. /* Add more setup here.??... */
  692. LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
  693. return ERROR_OK;
  694. }
  695. static int cmsis_dap_init(void)
  696. {
  697. int retval;
  698. uint8_t *data;
  699. if (swd_mode) {
  700. retval = cmsis_dap_swd_open();
  701. if (retval != ERROR_OK)
  702. return retval;
  703. }
  704. if (cmsis_dap_handle == NULL) {
  705. /* JTAG init */
  706. retval = cmsis_dap_usb_open();
  707. if (retval != ERROR_OK)
  708. return retval;
  709. retval = cmsis_dap_get_caps_info();
  710. if (retval != ERROR_OK)
  711. return retval;
  712. /* Connect in JTAG mode */
  713. if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
  714. LOG_ERROR("CMSIS-DAP: JTAG not supported");
  715. return ERROR_JTAG_DEVICE_ERROR;
  716. }
  717. retval = cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG);
  718. if (retval != ERROR_OK)
  719. return retval;
  720. LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
  721. }
  722. retval = cmsis_dap_get_version_info();
  723. if (retval != ERROR_OK)
  724. return retval;
  725. /* INFO_ID_PKT_SZ - short */
  726. retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ, &data);
  727. if (retval != ERROR_OK)
  728. return retval;
  729. if (data[0] == 2) { /* short */
  730. uint16_t pkt_sz = data[1] + (data[2] << 8);
  731. /* 4 bytes of command header + 5 bytes per register
  732. * write. For bulk read sequences just 4 bytes are
  733. * needed per transfer, so this is suboptimal. */
  734. pending_queue_len = (pkt_sz - 4) / 5;
  735. pending_transfers = malloc(pending_queue_len * sizeof(*pending_transfers));
  736. if (!pending_transfers) {
  737. LOG_ERROR("Unable to allocate memory for CMSIS-DAP queue");
  738. return ERROR_FAIL;
  739. }
  740. if (cmsis_dap_handle->packet_size != pkt_sz + 1) {
  741. /* reallocate buffer */
  742. cmsis_dap_handle->packet_size = pkt_sz + 1;
  743. cmsis_dap_handle->packet_buffer = realloc(cmsis_dap_handle->packet_buffer,
  744. cmsis_dap_handle->packet_size);
  745. if (cmsis_dap_handle->packet_buffer == NULL) {
  746. LOG_ERROR("unable to reallocate memory");
  747. return ERROR_FAIL;
  748. }
  749. }
  750. LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16, pkt_sz);
  751. }
  752. /* INFO_ID_PKT_CNT - byte */
  753. retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT, &data);
  754. if (retval != ERROR_OK)
  755. return retval;
  756. if (data[0] == 1) { /* byte */
  757. uint16_t pkt_cnt = data[1];
  758. cmsis_dap_handle->packet_count = pkt_cnt;
  759. LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16, pkt_cnt);
  760. }
  761. retval = cmsis_dap_get_status();
  762. if (retval != ERROR_OK)
  763. return ERROR_FAIL;
  764. /* Now try to connect to the target
  765. * TODO: This is all SWD only @ present */
  766. retval = cmsis_dap_cmd_DAP_SWJ_Clock(jtag_get_speed_khz());
  767. if (retval != ERROR_OK)
  768. return ERROR_FAIL;
  769. /* Ask CMSIS-DAP to automatically retry on receiving WAIT for
  770. * up to 64 times. This must be changed to 0 if sticky
  771. * overrun detection is enabled. */
  772. retval = cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
  773. if (retval != ERROR_OK)
  774. return ERROR_FAIL;
  775. /* Data Phase (bit 2) must be set to 1 if sticky overrun
  776. * detection is enabled */
  777. retval = cmsis_dap_cmd_DAP_SWD_Configure(0); /* 1 TRN, no Data Phase */
  778. if (retval != ERROR_OK)
  779. return ERROR_FAIL;
  780. retval = cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
  781. if (retval != ERROR_OK)
  782. return ERROR_FAIL;
  783. /* support connecting with srst asserted */
  784. enum reset_types jtag_reset_config = jtag_get_reset_config();
  785. if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
  786. if (jtag_reset_config & RESET_SRST_NO_GATING) {
  787. retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL);
  788. if (retval != ERROR_OK)
  789. return ERROR_FAIL;
  790. LOG_INFO("Connecting under reset");
  791. }
  792. }
  793. cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
  794. LOG_INFO("CMSIS-DAP: Interface ready");
  795. return ERROR_OK;
  796. }
  797. static int cmsis_dap_swd_init(void)
  798. {
  799. swd_mode = true;
  800. return ERROR_OK;
  801. }
  802. static int cmsis_dap_quit(void)
  803. {
  804. cmsis_dap_cmd_DAP_Disconnect();
  805. cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
  806. cmsis_dap_usb_close(cmsis_dap_handle);
  807. return ERROR_OK;
  808. }
  809. static void cmsis_dap_execute_reset(struct jtag_command *cmd)
  810. {
  811. /* Set both TRST and SRST even if they're not enabled as
  812. * there's no way to tristate them */
  813. uint8_t pins = 0;
  814. if (!cmd->cmd.reset->srst)
  815. pins |= SWJ_PIN_SRST;
  816. if (!cmd->cmd.reset->trst)
  817. pins |= SWJ_PIN_TRST;
  818. int retval = cmsis_dap_cmd_DAP_SWJ_Pins(pins,
  819. SWJ_PIN_TRST | SWJ_PIN_SRST, 0, NULL);
  820. if (retval != ERROR_OK)
  821. LOG_ERROR("CMSIS-DAP: Interface reset failed");
  822. }
  823. static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
  824. {
  825. #if 0
  826. int retval = cmsis_dap_cmd_DAP_Delay(cmd->cmd.sleep->us);
  827. if (retval != ERROR_OK)
  828. #endif
  829. jtag_sleep(cmd->cmd.sleep->us);
  830. }
  831. /* Set TMS high for five TCK clocks, to move the TAP to the Test-Logic-Reset state */
  832. static int cmsis_dap_execute_tlr_reset(struct jtag_command *cmd)
  833. {
  834. LOG_INFO("cmsis-dap JTAG TLR_RESET");
  835. uint8_t seq = 0xff;
  836. int ret = cmsis_dap_cmd_DAP_SWJ_Sequence(8, &seq);
  837. if (ret == ERROR_OK)
  838. tap_set_state(TAP_RESET);
  839. return ret;
  840. }
  841. /* Set new end state */
  842. static void cmsis_dap_end_state(tap_state_t state)
  843. {
  844. if (tap_is_state_stable(state))
  845. tap_set_end_state(state);
  846. else {
  847. LOG_ERROR("BUG: %i is not a valid end state", state);
  848. exit(-1);
  849. }
  850. }
  851. #ifdef SPRINT_BINARY
  852. static void sprint_binary(char *s, const uint8_t *buf, int offset, int len)
  853. {
  854. if (!len)
  855. return;
  856. /*
  857. buf = { 0x18 } len=5 should result in: 11000
  858. buf = { 0xff 0x18 } len=13 should result in: 11111111 11000
  859. buf = { 0xc0 0x18 } offset=3 len=10 should result in: 11000 11000
  860. i=3 there means i/8 = 0 so c = 0xFF, and
  861. */
  862. for (int i = offset; i < offset + len; ++i) {
  863. uint8_t c = buf[i / 8], mask = 1 << (i % 8);
  864. if ((i != offset) && !(i % 8))
  865. putchar(' ');
  866. *s++ = (c & mask) ? '1' : '0';
  867. }
  868. *s = 0;
  869. }
  870. #endif
  871. #ifdef CMSIS_DAP_JTAG_DEBUG
  872. static void debug_parse_cmsis_buf(const uint8_t *cmd, int cmdlen)
  873. {
  874. /* cmd is a usb packet to go to the cmsis-dap interface */
  875. printf("cmsis-dap buffer (%d b): ", cmdlen);
  876. for (int i = 0; i < cmdlen; ++i)
  877. printf(" %02x", cmd[i]);
  878. printf("\n");
  879. switch (cmd[1]) {
  880. case CMD_DAP_JTAG_SEQ: {
  881. printf("cmsis-dap jtag sequence command %02x (n=%d)\n", cmd[1], cmd[2]);
  882. /*
  883. * #2 = number of sequences
  884. * #3 = sequence info 1
  885. * #4...4+n_bytes-1 = sequence 1
  886. * #4+n_bytes = sequence info 2
  887. * #5+n_bytes = sequence 2 (single bit)
  888. */
  889. int pos = 3;
  890. for (int seq = 0; seq < cmd[2]; ++seq) {
  891. uint8_t info = cmd[pos++];
  892. int len = info & DAP_JTAG_SEQ_TCK;
  893. if (len == 0)
  894. len = 64;
  895. printf(" sequence %d starting %d: info %02x (len=%d tms=%d read_tdo=%d): ",
  896. seq, pos, info, len, info & DAP_JTAG_SEQ_TMS, info & DAP_JTAG_SEQ_TDO);
  897. for (int i = 0; i < DIV_ROUND_UP(len, 8); ++i)
  898. printf(" %02x", cmd[pos+i]);
  899. pos += DIV_ROUND_UP(len, 8);
  900. printf("\n");
  901. }
  902. if (pos != cmdlen) {
  903. printf("BUFFER LENGTH MISMATCH looks like %d but %d specified", pos, cmdlen);
  904. exit(-1);
  905. }
  906. break;
  907. }
  908. default:
  909. LOG_DEBUG("unknown cmsis-dap command %02x", cmd[1]);
  910. break;
  911. }
  912. }
  913. #endif
  914. static void cmsis_dap_flush(void)
  915. {
  916. if (!queued_seq_count)
  917. return;
  918. DEBUG_JTAG_IO("Flushing %d queued sequences (%d bytes) with %d pending scan results to capture",
  919. queued_seq_count, queued_seq_buf_end, pending_scan_result_count);
  920. /* prep CMSIS-DAP packet */
  921. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  922. buffer[0] = 0; /* report number */
  923. buffer[1] = CMD_DAP_JTAG_SEQ;
  924. buffer[2] = queued_seq_count;
  925. memcpy(buffer + 3, queued_seq_buf, queued_seq_buf_end);
  926. #ifdef CMSIS_DAP_JTAG_DEBUG
  927. debug_parse_cmsis_buf(buffer, queued_seq_buf_end + 3);
  928. #endif
  929. /* send command to USB device */
  930. int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, queued_seq_buf_end + 3);
  931. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  932. LOG_ERROR("CMSIS-DAP command CMD_DAP_JTAG_SEQ failed.");
  933. exit(-1);
  934. }
  935. #ifdef CMSIS_DAP_JTAG_DEBUG
  936. DEBUG_JTAG_IO("USB response buf:");
  937. for (int c = 0; c < queued_seq_buf_end + 3; ++c)
  938. printf("%02X ", buffer[c]);
  939. printf("\n");
  940. #endif
  941. /* copy scan results into client buffers */
  942. for (int i = 0; i < pending_scan_result_count; ++i) {
  943. struct pending_scan_result *scan = &pending_scan_results[i];
  944. DEBUG_JTAG_IO("Copying pending_scan_result %d/%d: %d bits from byte %d -> buffer + %d bits",
  945. i, pending_scan_result_count, scan->length, scan->first + 2, scan->buffer_offset);
  946. #ifdef CMSIS_DAP_JTAG_DEBUG
  947. for (uint32_t b = 0; b < DIV_ROUND_UP(scan->length, 8); ++b)
  948. printf("%02X ", buffer[2+scan->first+b]);
  949. printf("\n");
  950. #endif
  951. bit_copy(scan->buffer, scan->buffer_offset, buffer + 2 + scan->first, 0, scan->length);
  952. }
  953. /* reset */
  954. queued_seq_count = 0;
  955. queued_seq_buf_end = 0;
  956. queued_seq_tdo_ptr = 0;
  957. pending_scan_result_count = 0;
  958. }
  959. /* queue a sequence of bits to clock out TDI / in TDO, executing if the buffer is full.
  960. *
  961. * sequence=NULL means clock out zeros on TDI
  962. * tdo_buffer=NULL means don't capture TDO
  963. */
  964. static void cmsis_dap_add_jtag_sequence(int s_len, const uint8_t *sequence, int s_offset,
  965. bool tms, uint8_t *tdo_buffer, int tdo_buffer_offset)
  966. {
  967. DEBUG_JTAG_IO("[at %d] %d bits, tms %s, seq offset %d, tdo buf %p, tdo offset %d",
  968. queued_seq_buf_end,
  969. s_len, tms ? "HIGH" : "LOW", s_offset, tdo_buffer, tdo_buffer_offset);
  970. if (s_len == 0)
  971. return;
  972. if (s_len > 64) {
  973. DEBUG_JTAG_IO("START JTAG SEQ SPLIT");
  974. for (int offset = 0; offset < s_len; offset += 64) {
  975. int len = s_len - offset;
  976. if (len > 64)
  977. len = 64;
  978. DEBUG_JTAG_IO("Splitting long jtag sequence: %d-bit chunk starting at offset %d", len, offset);
  979. cmsis_dap_add_jtag_sequence(
  980. len,
  981. sequence,
  982. s_offset + offset,
  983. tms,
  984. tdo_buffer,
  985. tdo_buffer == NULL ? 0 : (tdo_buffer_offset + offset)
  986. );
  987. }
  988. DEBUG_JTAG_IO("END JTAG SEQ SPLIT");
  989. return;
  990. }
  991. int cmd_len = 1 + DIV_ROUND_UP(s_len, 8);
  992. if (queued_seq_count >= 255 || queued_seq_buf_end + cmd_len > QUEUED_SEQ_BUF_LEN)
  993. /* empty out the buffer */
  994. cmsis_dap_flush();
  995. ++queued_seq_count;
  996. /* control byte */
  997. queued_seq_buf[queued_seq_buf_end] =
  998. (tms ? DAP_JTAG_SEQ_TMS : 0) |
  999. (tdo_buffer != NULL ? DAP_JTAG_SEQ_TDO : 0) |
  1000. (s_len == 64 ? 0 : s_len);
  1001. if (sequence != NULL)
  1002. bit_copy(&queued_seq_buf[queued_seq_buf_end + 1], 0, sequence, s_offset, s_len);
  1003. else
  1004. memset(&queued_seq_buf[queued_seq_buf_end + 1], 0, DIV_ROUND_UP(s_len, 8));
  1005. queued_seq_buf_end += cmd_len;
  1006. if (tdo_buffer != NULL) {
  1007. struct pending_scan_result *scan = &pending_scan_results[pending_scan_result_count++];
  1008. scan->first = queued_seq_tdo_ptr;
  1009. queued_seq_tdo_ptr += DIV_ROUND_UP(s_len, 8);
  1010. scan->length = s_len;
  1011. scan->buffer = tdo_buffer;
  1012. scan->buffer_offset = tdo_buffer_offset;
  1013. }
  1014. }
  1015. /* queue a sequence of bits to clock out TMS, executing if the buffer is full */
  1016. static void cmsis_dap_add_tms_sequence(const uint8_t *sequence, int s_len)
  1017. {
  1018. DEBUG_JTAG_IO("%d bits: %02X", s_len, *sequence);
  1019. /* we use a series of CMD_DAP_JTAG_SEQ commands to toggle TMS,
  1020. because even though it seems ridiculously inefficient, it
  1021. allows us to combine TMS and scan sequences into the same
  1022. USB packet. */
  1023. /* TODO: combine runs of the same tms value */
  1024. for (int i = 0; i < s_len; ++i) {
  1025. bool bit = (sequence[i / 8] & (1 << (i % 8))) != 0;
  1026. cmsis_dap_add_jtag_sequence(1, NULL, 0, bit, NULL, 0);
  1027. }
  1028. }
  1029. /* Move to the end state by queuing a sequence to clock into TMS */
  1030. static void cmsis_dap_state_move(void)
  1031. {
  1032. uint8_t tms_scan;
  1033. uint8_t tms_scan_bits;
  1034. tms_scan = tap_get_tms_path(tap_get_state(), tap_get_end_state());
  1035. tms_scan_bits = tap_get_tms_path_len(tap_get_state(), tap_get_end_state());
  1036. DEBUG_JTAG_IO("state move from %s to %s: %d clocks, %02X on tms",
  1037. tap_state_name(tap_get_state()), tap_state_name(tap_get_end_state()),
  1038. tms_scan_bits, tms_scan);
  1039. cmsis_dap_add_tms_sequence(&tms_scan, tms_scan_bits);
  1040. tap_set_state(tap_get_end_state());
  1041. }
  1042. /* Execute a JTAG scan operation by queueing TMS and TDI/TDO sequences */
  1043. static void cmsis_dap_execute_scan(struct jtag_command *cmd)
  1044. {
  1045. DEBUG_JTAG_IO("%s type:%d", cmd->cmd.scan->ir_scan ? "IRSCAN" : "DRSCAN",
  1046. jtag_scan_type(cmd->cmd.scan));
  1047. /* Make sure there are no trailing fields with num_bits == 0, or the logic below will fail. */
  1048. while (cmd->cmd.scan->num_fields > 0
  1049. && cmd->cmd.scan->fields[cmd->cmd.scan->num_fields - 1].num_bits == 0) {
  1050. cmd->cmd.scan->num_fields--;
  1051. LOG_DEBUG("discarding trailing empty field");
  1052. }
  1053. if (cmd->cmd.scan->num_fields == 0) {
  1054. LOG_DEBUG("empty scan, doing nothing");
  1055. return;
  1056. }
  1057. if (cmd->cmd.scan->ir_scan) {
  1058. if (tap_get_state() != TAP_IRSHIFT) {
  1059. cmsis_dap_end_state(TAP_IRSHIFT);
  1060. cmsis_dap_state_move();
  1061. }
  1062. } else {
  1063. if (tap_get_state() != TAP_DRSHIFT) {
  1064. cmsis_dap_end_state(TAP_DRSHIFT);
  1065. cmsis_dap_state_move();
  1066. }
  1067. }
  1068. cmsis_dap_end_state(cmd->cmd.scan->end_state);
  1069. struct scan_field *field = cmd->cmd.scan->fields;
  1070. unsigned scan_size = 0;
  1071. for (int i = 0; i < cmd->cmd.scan->num_fields; i++, field++) {
  1072. scan_size += field->num_bits;
  1073. DEBUG_JTAG_IO("%s%s field %d/%d %d bits",
  1074. field->in_value ? "in" : "",
  1075. field->out_value ? "out" : "",
  1076. i,
  1077. cmd->cmd.scan->num_fields,
  1078. field->num_bits);
  1079. if (i == cmd->cmd.scan->num_fields - 1 && tap_get_state() != tap_get_end_state()) {
  1080. DEBUG_JTAG_IO("Last field and have to move out of SHIFT state");
  1081. /* Last field, and we're leaving IRSHIFT/DRSHIFT. Clock last bit during tap
  1082. * movement. This last field can't have length zero, it was checked above. */
  1083. cmsis_dap_add_jtag_sequence(
  1084. field->num_bits - 1, /* number of bits to clock */
  1085. field->out_value, /* output sequence */
  1086. 0, /* output offset */
  1087. false, /* TMS low */
  1088. field->in_value,
  1089. 0);
  1090. /* Clock the last bit out, with TMS high */
  1091. uint8_t last_bit = 0;
  1092. if (field->out_value)
  1093. bit_copy(&last_bit, 0, field->out_value, field->num_bits - 1, 1);
  1094. cmsis_dap_add_jtag_sequence(
  1095. 1,
  1096. &last_bit,
  1097. 0,
  1098. true,
  1099. field->in_value,
  1100. field->num_bits - 1);
  1101. tap_set_state(tap_state_transition(tap_get_state(), 1));
  1102. /* Now clock one more cycle, with TMS low, to get us into a PAUSE state */
  1103. cmsis_dap_add_jtag_sequence(
  1104. 1,
  1105. &last_bit,
  1106. 0,
  1107. false,
  1108. NULL,
  1109. 0);
  1110. tap_set_state(tap_state_transition(tap_get_state(), 0));
  1111. } else {
  1112. DEBUG_JTAG_IO("Internal field, staying in SHIFT state afterwards");
  1113. /* Clocking part of a sequence into DR or IR with TMS=0,
  1114. leaving TMS=0 at the end so we can continue later */
  1115. cmsis_dap_add_jtag_sequence(
  1116. field->num_bits,
  1117. field->out_value,
  1118. 0,
  1119. false,
  1120. field->in_value,
  1121. 0);
  1122. }
  1123. }
  1124. if (tap_get_state() != tap_get_end_state()) {
  1125. cmsis_dap_end_state(tap_get_end_state());
  1126. cmsis_dap_state_move();
  1127. }
  1128. DEBUG_JTAG_IO("%s scan, %i bits, end in %s",
  1129. (cmd->cmd.scan->ir_scan) ? "IR" : "DR", scan_size,
  1130. tap_state_name(tap_get_end_state()));
  1131. }
  1132. static void cmsis_dap_pathmove(int num_states, tap_state_t *path)
  1133. {
  1134. int i;
  1135. uint8_t tms0 = 0x00;
  1136. uint8_t tms1 = 0xff;
  1137. for (i = 0; i < num_states; i++) {
  1138. if (path[i] == tap_state_transition(tap_get_state(), false))
  1139. cmsis_dap_add_tms_sequence(&tms0, 1);
  1140. else if (path[i] == tap_state_transition(tap_get_state(), true))
  1141. cmsis_dap_add_tms_sequence(&tms1, 1);
  1142. else {
  1143. LOG_ERROR("BUG: %s -> %s isn't a valid TAP transition.",
  1144. tap_state_name(tap_get_state()), tap_state_name(path[i]));
  1145. exit(-1);
  1146. }
  1147. tap_set_state(path[i]);
  1148. }
  1149. cmsis_dap_end_state(tap_get_state());
  1150. }
  1151. static void cmsis_dap_execute_pathmove(struct jtag_command *cmd)
  1152. {
  1153. DEBUG_JTAG_IO("pathmove: %i states, end in %i",
  1154. cmd->cmd.pathmove->num_states,
  1155. cmd->cmd.pathmove->path[cmd->cmd.pathmove->num_states - 1]);
  1156. cmsis_dap_pathmove(cmd->cmd.pathmove->num_states, cmd->cmd.pathmove->path);
  1157. }
  1158. static void cmsis_dap_stableclocks(int num_cycles)
  1159. {
  1160. int i;
  1161. uint8_t tms = tap_get_state() == TAP_RESET;
  1162. /* TODO: Perform optimizations? */
  1163. /* Execute num_cycles. */
  1164. for (i = 0; i < num_cycles; i++)
  1165. cmsis_dap_add_tms_sequence(&tms, 1);
  1166. }
  1167. static void cmsis_dap_runtest(int num_cycles)
  1168. {
  1169. tap_state_t saved_end_state = tap_get_end_state();
  1170. /* Only do a state_move when we're not already in IDLE. */
  1171. if (tap_get_state() != TAP_IDLE) {
  1172. cmsis_dap_end_state(TAP_IDLE);
  1173. cmsis_dap_state_move();
  1174. }
  1175. cmsis_dap_stableclocks(num_cycles);
  1176. /* Finish in end_state. */
  1177. cmsis_dap_end_state(saved_end_state);
  1178. if (tap_get_state() != tap_get_end_state())
  1179. cmsis_dap_state_move();
  1180. }
  1181. static void cmsis_dap_execute_runtest(struct jtag_command *cmd)
  1182. {
  1183. DEBUG_JTAG_IO("runtest %i cycles, end in %i", cmd->cmd.runtest->num_cycles,
  1184. cmd->cmd.runtest->end_state);
  1185. cmsis_dap_end_state(cmd->cmd.runtest->end_state);
  1186. cmsis_dap_runtest(cmd->cmd.runtest->num_cycles);
  1187. }
  1188. static void cmsis_dap_execute_stableclocks(struct jtag_command *cmd)
  1189. {
  1190. DEBUG_JTAG_IO("stableclocks %i cycles", cmd->cmd.runtest->num_cycles);
  1191. cmsis_dap_stableclocks(cmd->cmd.runtest->num_cycles);
  1192. }
  1193. /* TODO: Is there need to call cmsis_dap_flush() for the JTAG_PATHMOVE,
  1194. * JTAG_RUNTEST, JTAG_STABLECLOCKS? */
  1195. static void cmsis_dap_execute_command(struct jtag_command *cmd)
  1196. {
  1197. switch (cmd->type) {
  1198. case JTAG_RESET:
  1199. cmsis_dap_flush();
  1200. cmsis_dap_execute_reset(cmd);
  1201. break;
  1202. case JTAG_SLEEP:
  1203. cmsis_dap_flush();
  1204. cmsis_dap_execute_sleep(cmd);
  1205. break;
  1206. case JTAG_TLR_RESET:
  1207. cmsis_dap_flush();
  1208. cmsis_dap_execute_tlr_reset(cmd);
  1209. break;
  1210. case JTAG_SCAN:
  1211. cmsis_dap_execute_scan(cmd);
  1212. break;
  1213. case JTAG_PATHMOVE:
  1214. cmsis_dap_execute_pathmove(cmd);
  1215. break;
  1216. case JTAG_RUNTEST:
  1217. cmsis_dap_execute_runtest(cmd);
  1218. break;
  1219. case JTAG_STABLECLOCKS:
  1220. cmsis_dap_execute_stableclocks(cmd);
  1221. break;
  1222. case JTAG_TMS:
  1223. default:
  1224. LOG_ERROR("BUG: unknown JTAG command type 0x%X encountered", cmd->type);
  1225. exit(-1);
  1226. }
  1227. }
  1228. static int cmsis_dap_execute_queue(void)
  1229. {
  1230. struct jtag_command *cmd = jtag_command_queue;
  1231. while (cmd != NULL) {
  1232. cmsis_dap_execute_command(cmd);
  1233. cmd = cmd->next;
  1234. }
  1235. cmsis_dap_flush();
  1236. return ERROR_OK;
  1237. }
  1238. static int cmsis_dap_speed(int speed)
  1239. {
  1240. if (speed > DAP_MAX_CLOCK) {
  1241. LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed, DAP_MAX_CLOCK);
  1242. speed = DAP_MAX_CLOCK;
  1243. }
  1244. if (speed == 0) {
  1245. LOG_INFO("RTCK not supported");
  1246. return ERROR_JTAG_NOT_IMPLEMENTED;
  1247. }
  1248. return cmsis_dap_cmd_DAP_SWJ_Clock(speed);
  1249. }
  1250. static int cmsis_dap_speed_div(int speed, int *khz)
  1251. {
  1252. *khz = speed;
  1253. return ERROR_OK;
  1254. }
  1255. static int cmsis_dap_khz(int khz, int *jtag_speed)
  1256. {
  1257. *jtag_speed = khz;
  1258. return ERROR_OK;
  1259. }
  1260. static int_least32_t cmsis_dap_swd_frequency(int_least32_t hz)
  1261. {
  1262. if (hz > 0)
  1263. cmsis_dap_speed(hz / 1000);
  1264. return hz;
  1265. }
  1266. COMMAND_HANDLER(cmsis_dap_handle_info_command)
  1267. {
  1268. if (cmsis_dap_get_version_info() == ERROR_OK)
  1269. cmsis_dap_get_status();
  1270. return ERROR_OK;
  1271. }
  1272. COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
  1273. {
  1274. if (CMD_ARGC > MAX_USB_IDS * 2) {
  1275. LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
  1276. "(maximum is %d pairs)", MAX_USB_IDS);
  1277. CMD_ARGC = MAX_USB_IDS * 2;
  1278. }
  1279. if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
  1280. LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
  1281. if (CMD_ARGC < 2)
  1282. return ERROR_COMMAND_SYNTAX_ERROR;
  1283. /* remove the incomplete trailing id */
  1284. CMD_ARGC -= 1;
  1285. }
  1286. unsigned i;
  1287. for (i = 0; i < CMD_ARGC; i += 2) {
  1288. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
  1289. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
  1290. }
  1291. /*
  1292. * Explicitly terminate, in case there are multiples instances of
  1293. * cmsis_dap_vid_pid.
  1294. */
  1295. cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
  1296. return ERROR_OK;
  1297. }
  1298. COMMAND_HANDLER(cmsis_dap_handle_serial_command)
  1299. {
  1300. if (CMD_ARGC == 1) {
  1301. size_t len = mbstowcs(NULL, CMD_ARGV[0], 0);
  1302. cmsis_dap_serial = calloc(len + 1, sizeof(wchar_t));
  1303. if (cmsis_dap_serial == NULL) {
  1304. LOG_ERROR("unable to allocate memory");
  1305. return ERROR_OK;
  1306. }
  1307. if (mbstowcs(cmsis_dap_serial, CMD_ARGV[0], len + 1) == (size_t)-1) {
  1308. free(cmsis_dap_serial);
  1309. cmsis_dap_serial = NULL;
  1310. LOG_ERROR("unable to convert serial");
  1311. }
  1312. } else {
  1313. LOG_ERROR("expected exactly one argument to cmsis_dap_serial <serial-number>");
  1314. }
  1315. return ERROR_OK;
  1316. }
  1317. static const struct command_registration cmsis_dap_subcommand_handlers[] = {
  1318. {
  1319. .name = "info",
  1320. .handler = &cmsis_dap_handle_info_command,
  1321. .mode = COMMAND_EXEC,
  1322. .usage = "",
  1323. .help = "show cmsis-dap info",
  1324. },
  1325. COMMAND_REGISTRATION_DONE
  1326. };
  1327. static const struct command_registration cmsis_dap_command_handlers[] = {
  1328. {
  1329. .name = "cmsis-dap",
  1330. .mode = COMMAND_ANY,
  1331. .help = "perform CMSIS-DAP management",
  1332. .usage = "<cmd>",
  1333. .chain = cmsis_dap_subcommand_handlers,
  1334. },
  1335. {
  1336. .name = "cmsis_dap_vid_pid",
  1337. .handler = &cmsis_dap_handle_vid_pid_command,
  1338. .mode = COMMAND_CONFIG,
  1339. .help = "the vendor ID and product ID of the CMSIS-DAP device",
  1340. .usage = "(vid pid)* ",
  1341. },
  1342. {
  1343. .name = "cmsis_dap_serial",
  1344. .handler = &cmsis_dap_handle_serial_command,
  1345. .mode = COMMAND_CONFIG,
  1346. .help = "set the serial number of the adapter",
  1347. .usage = "serial_string",
  1348. },
  1349. COMMAND_REGISTRATION_DONE
  1350. };
  1351. static const struct swd_driver cmsis_dap_swd_driver = {
  1352. .init = cmsis_dap_swd_init,
  1353. .frequency = cmsis_dap_swd_frequency,
  1354. .switch_seq = cmsis_dap_swd_switch_seq,
  1355. .read_reg = cmsis_dap_swd_read_reg,
  1356. .write_reg = cmsis_dap_swd_write_reg,
  1357. .run = cmsis_dap_swd_run_queue,
  1358. };
  1359. static const char * const cmsis_dap_transport[] = { "swd", "jtag", NULL };
  1360. struct jtag_interface cmsis_dap_interface = {
  1361. .name = "cmsis-dap",
  1362. .commands = cmsis_dap_command_handlers,
  1363. .swd = &cmsis_dap_swd_driver,
  1364. .transports = cmsis_dap_transport,
  1365. .execute_queue = cmsis_dap_execute_queue,
  1366. .speed = cmsis_dap_speed,
  1367. .speed_div = cmsis_dap_speed_div,
  1368. .khz = cmsis_dap_khz,
  1369. .init = cmsis_dap_init,
  1370. .quit = cmsis_dap_quit,
  1371. };