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.
 
 
 
 
 
 

1054 lines
26 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2013 by mike brown *
  3. * mike@theshedworks.org.uk *
  4. * *
  5. * Copyright (C) 2013 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include <transport/transport.h>
  27. #include <jtag/swd.h>
  28. #include <jtag/interface.h>
  29. #include <jtag/commands.h>
  30. #include <jtag/tcl.h>
  31. #include <hidapi.h>
  32. #ifdef _DEBUG_JTAG_IO_
  33. #define DEBUG_IO(expr...) LOG_DEBUG(expr)
  34. #else
  35. #define DEBUG_IO(expr...) do {} while (0)
  36. #endif
  37. /*
  38. * See CMSIS-DAP documentation:
  39. * Version 0.01 - Beta.
  40. */
  41. /* USB Config */
  42. /* Known vid/pid pairs:
  43. * VID 0xc251: Keil Software
  44. * PID 0xf001: LPC-Link-II CMSIS_DAP
  45. * PID 0xf002: OPEN-SDA CMSIS_DAP (Freedom Board)
  46. * PID 0x2722: Keil ULINK2 CMSIS-DAP
  47. *
  48. * VID 0x0d28: mbed Software
  49. * PID 0x0204: MBED CMSIS-DAP
  50. */
  51. #define MAX_USB_IDS 8
  52. /* vid = pid = 0 marks the end of the list */
  53. static uint16_t cmsis_dap_vid[MAX_USB_IDS + 1] = { 0 };
  54. static uint16_t cmsis_dap_pid[MAX_USB_IDS + 1] = { 0 };
  55. static bool swd_mode;
  56. #define PACKET_SIZE (64 + 1) /* 64 bytes plus report id */
  57. #define USB_TIMEOUT 1000
  58. /* CMSIS-DAP General Commands */
  59. #define CMD_DAP_INFO 0x00
  60. #define CMD_DAP_LED 0x01
  61. #define CMD_DAP_CONNECT 0x02
  62. #define CMD_DAP_DISCONNECT 0x03
  63. #define CMD_DAP_WRITE_ABORT 0x08
  64. #define CMD_DAP_DELAY 0x09
  65. #define CMD_DAP_RESET_TARGET 0x0A
  66. /* CMD_INFO */
  67. #define INFO_ID_VID 0x00 /* string */
  68. #define INFO_ID_PID 0x02 /* string */
  69. #define INFO_ID_SERNUM 0x03 /* string */
  70. #define INFO_ID_FW_VER 0x04 /* string */
  71. #define INFO_ID_TD_VEND 0x05 /* string */
  72. #define INFO_ID_TD_NAME 0x06 /* string */
  73. #define INFO_ID_CAPS 0xf0 /* byte */
  74. #define INFO_ID_PKT_CNT 0xfe /* byte */
  75. #define INFO_ID_PKT_SZ 0xff /* short */
  76. #define INFO_CAPS_SWD 0x01
  77. #define INFO_CAPS_JTAG 0x02
  78. /* CMD_LED */
  79. #define LED_ID_CONNECT 0x00
  80. #define LED_ID_RUN 0x01
  81. #define LED_OFF 0x00
  82. #define LED_ON 0x01
  83. /* CMD_CONNECT */
  84. #define CONNECT_DEFAULT 0x00
  85. #define CONNECT_SWD 0x01
  86. #define CONNECT_JTAG 0x02
  87. /* CMSIS-DAP Common SWD/JTAG Commands */
  88. #define CMD_DAP_DELAY 0x09
  89. #define CMD_DAP_SWJ_PINS 0x10
  90. #define CMD_DAP_SWJ_CLOCK 0x11
  91. #define CMD_DAP_SWJ_SEQ 0x12
  92. /*
  93. * PINS
  94. * Bit 0: SWCLK/TCK
  95. * Bit 1: SWDIO/TMS
  96. * Bit 2: TDI
  97. * Bit 3: TDO
  98. * Bit 5: nTRST
  99. * Bit 7: nRESET
  100. */
  101. /* CMSIS-DAP SWD Commands */
  102. #define CMD_DAP_SWD_CONFIGURE 0x13
  103. /* CMSIS-DAP JTAG Commands */
  104. #define CMD_DAP_JTAG_SEQ 0x14
  105. #define CMD_DAP_JTAG_CONFIGURE 0x15
  106. #define CMD_DAP_JTAG_IDCODE 0x16
  107. /* CMSIS-DAP Transfer Commands */
  108. #define CMD_DAP_TFER_CONFIGURE 0x04
  109. #define CMD_DAP_TFER 0x05
  110. #define CMD_DAP_TFER_BLOCK 0x06
  111. #define CMD_DAP_TFER_ABORT 0x07
  112. /* DAP Status Code */
  113. #define DAP_OK 0
  114. #define DAP_ERROR 0xFF
  115. /* CMSIS-DAP Vendor Commands
  116. * None as yet... */
  117. static const char * const info_caps_str[] = {
  118. "SWD Supported",
  119. "JTAG Supported"
  120. };
  121. /* max clock speed (kHz) */
  122. #define DAP_MAX_CLOCK 5000
  123. struct cmsis_dap {
  124. hid_device *dev_handle;
  125. uint16_t packet_size;
  126. uint16_t packet_count;
  127. uint8_t *packet_buffer;
  128. uint8_t caps;
  129. uint8_t mode;
  130. };
  131. static struct cmsis_dap *cmsis_dap_handle;
  132. static int cmsis_dap_usb_open(void)
  133. {
  134. hid_device *dev = NULL;
  135. int i;
  136. struct hid_device_info *devs, *cur_dev;
  137. unsigned short target_vid, target_pid;
  138. target_vid = 0;
  139. target_pid = 0;
  140. /*
  141. The CMSIS-DAP specification stipulates:
  142. "The Product String must contain "CMSIS-DAP" somewhere in the string. This is used by the
  143. debuggers to idenify a CMSIS-DAP compliant Debug Unit that is connected to a host computer."
  144. */
  145. devs = hid_enumerate(0x0, 0x0);
  146. cur_dev = devs;
  147. while (NULL != cur_dev) {
  148. if (0 == cmsis_dap_vid[0]) {
  149. if (NULL == cur_dev->product_string) {
  150. LOG_DEBUG("Cannot read product string of device 0x%x:0x%x",
  151. cur_dev->vendor_id, cur_dev->product_id);
  152. } else {
  153. if (wcsstr(cur_dev->product_string, L"CMSIS-DAP"))
  154. /*
  155. if the user hasn't specified VID:PID *and*
  156. product string contains "CMSIS-DAP", pick it
  157. */
  158. break;
  159. }
  160. } else {
  161. /*
  162. otherwise, exhaustively compare against all VID:PID in list
  163. */
  164. for (i = 0; cmsis_dap_vid[i] || cmsis_dap_pid[i]; i++) {
  165. if ((cmsis_dap_vid[i] == cur_dev->vendor_id) && (cmsis_dap_pid[i] == cur_dev->product_id))
  166. break;
  167. }
  168. if (cmsis_dap_vid[i] || cmsis_dap_pid[i])
  169. break;
  170. }
  171. cur_dev = cur_dev->next;
  172. }
  173. if (NULL != cur_dev) {
  174. target_vid = cur_dev->vendor_id;
  175. target_pid = cur_dev->product_id;
  176. }
  177. hid_free_enumeration(devs);
  178. if (target_vid == 0 && target_pid == 0) {
  179. LOG_ERROR("unable to find CMSIS-DAP device");
  180. return ERROR_FAIL;
  181. }
  182. if (hid_init() != 0) {
  183. LOG_ERROR("unable to open HIDAPI");
  184. return ERROR_FAIL;
  185. }
  186. dev = hid_open(target_vid, target_pid, NULL);
  187. if (dev == NULL) {
  188. LOG_ERROR("unable to open CMSIS-DAP device");
  189. return ERROR_FAIL;
  190. }
  191. struct cmsis_dap *dap = malloc(sizeof(struct cmsis_dap));
  192. if (dap == NULL) {
  193. LOG_ERROR("unable to allocate memory");
  194. return ERROR_FAIL;
  195. }
  196. dap->dev_handle = dev;
  197. dap->caps = 0;
  198. dap->mode = 0;
  199. cmsis_dap_handle = dap;
  200. /* allocate default packet buffer, may be changed later.
  201. * currently with HIDAPI we have no way of getting the output report length
  202. * without this info we cannot communicate with the adapter.
  203. * For the moment we ahve to hard code the packet size */
  204. int packet_size = PACKET_SIZE;
  205. /* atmel cmsis-dap uses 512 byte reports */
  206. if (target_vid == 0x03eb)
  207. packet_size = 512 + 1;
  208. cmsis_dap_handle->packet_buffer = malloc(packet_size);
  209. cmsis_dap_handle->packet_size = packet_size;
  210. if (cmsis_dap_handle->packet_buffer == NULL) {
  211. LOG_ERROR("unable to allocate memory");
  212. return ERROR_FAIL;
  213. }
  214. return ERROR_OK;
  215. }
  216. static void cmsis_dap_usb_close(struct cmsis_dap *dap)
  217. {
  218. hid_close(dap->dev_handle);
  219. hid_exit();
  220. if (cmsis_dap_handle->packet_buffer)
  221. free(cmsis_dap_handle->packet_buffer);
  222. if (cmsis_dap_handle) {
  223. free(cmsis_dap_handle);
  224. cmsis_dap_handle = NULL;
  225. }
  226. return;
  227. }
  228. /* Send a message and receive the reply */
  229. static int cmsis_dap_usb_xfer(struct cmsis_dap *dap, int txlen)
  230. {
  231. /* Pad the rest of the TX buffer with 0's */
  232. memset(dap->packet_buffer + txlen, 0, dap->packet_size - 1 - txlen);
  233. /* write data to device */
  234. int retval = hid_write(dap->dev_handle, dap->packet_buffer, dap->packet_size);
  235. if (retval == -1) {
  236. LOG_ERROR("error writing data: %ls", hid_error(dap->dev_handle));
  237. return ERROR_FAIL;
  238. }
  239. /* get reply */
  240. retval = hid_read_timeout(dap->dev_handle, dap->packet_buffer, dap->packet_size, USB_TIMEOUT);
  241. if (retval == -1 || retval == 0) {
  242. LOG_DEBUG("error reading data: %ls", hid_error(dap->dev_handle));
  243. return ERROR_FAIL;
  244. }
  245. return ERROR_OK;
  246. }
  247. static int cmsis_dap_cmd_DAP_SWJ_Pins(uint8_t pins, uint8_t mask, uint32_t delay, uint8_t *input)
  248. {
  249. int retval;
  250. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  251. buffer[0] = 0; /* report number */
  252. buffer[1] = CMD_DAP_SWJ_PINS;
  253. buffer[2] = pins;
  254. buffer[3] = mask;
  255. buffer[4] = delay & 0xff;
  256. buffer[5] = (delay >> 8) & 0xff;
  257. buffer[6] = (delay >> 16) & 0xff;
  258. buffer[7] = (delay >> 24) & 0xff;
  259. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 8);
  260. if (retval != ERROR_OK) {
  261. LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_PINS failed.");
  262. return ERROR_JTAG_DEVICE_ERROR;
  263. }
  264. if (input)
  265. *input = buffer[1];
  266. return ERROR_OK;
  267. }
  268. static int cmsis_dap_cmd_DAP_SWJ_Clock(uint32_t swj_clock)
  269. {
  270. int retval;
  271. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  272. /* set clock in Hz */
  273. swj_clock *= 1000;
  274. buffer[0] = 0; /* report number */
  275. buffer[1] = CMD_DAP_SWJ_CLOCK;
  276. buffer[2] = swj_clock & 0xff;
  277. buffer[3] = (swj_clock >> 8) & 0xff;
  278. buffer[4] = (swj_clock >> 16) & 0xff;
  279. buffer[5] = (swj_clock >> 24) & 0xff;
  280. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 6);
  281. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  282. LOG_ERROR("CMSIS-DAP command CMD_DAP_SWJ_CLOCK failed.");
  283. return ERROR_JTAG_DEVICE_ERROR;
  284. }
  285. return ERROR_OK;
  286. }
  287. static int cmsis_dap_cmd_DAP_Info(uint8_t info, uint8_t **data)
  288. {
  289. int retval;
  290. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  291. buffer[0] = 0; /* report number */
  292. buffer[1] = CMD_DAP_INFO;
  293. buffer[2] = info;
  294. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
  295. if (retval != ERROR_OK) {
  296. LOG_ERROR("CMSIS-DAP command CMD_INFO failed.");
  297. return ERROR_JTAG_DEVICE_ERROR;
  298. }
  299. *data = &(buffer[1]);
  300. return ERROR_OK;
  301. }
  302. static int cmsis_dap_cmd_DAP_LED(uint8_t leds)
  303. {
  304. int retval;
  305. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  306. buffer[0] = 0; /* report number */
  307. buffer[1] = CMD_DAP_LED;
  308. buffer[2] = 0x00;
  309. buffer[3] = leds;
  310. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
  311. if (retval != ERROR_OK || buffer[1] != 0x00) {
  312. LOG_ERROR("CMSIS-DAP command CMD_LED failed.");
  313. return ERROR_JTAG_DEVICE_ERROR;
  314. }
  315. return ERROR_OK;
  316. }
  317. static int cmsis_dap_cmd_DAP_Connect(uint8_t mode)
  318. {
  319. int retval;
  320. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  321. buffer[0] = 0; /* report number */
  322. buffer[1] = CMD_DAP_CONNECT;
  323. buffer[2] = mode;
  324. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
  325. if (retval != ERROR_OK) {
  326. LOG_ERROR("CMSIS-DAP command CMD_CONNECT failed.");
  327. return ERROR_JTAG_DEVICE_ERROR;
  328. }
  329. if (buffer[1] != mode) {
  330. LOG_ERROR("CMSIS-DAP failed to connect in mode (%d)", mode);
  331. return ERROR_JTAG_DEVICE_ERROR;
  332. }
  333. return ERROR_OK;
  334. }
  335. static int cmsis_dap_cmd_DAP_Disconnect(void)
  336. {
  337. int retval;
  338. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  339. buffer[0] = 0; /* report number */
  340. buffer[1] = CMD_DAP_DISCONNECT;
  341. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
  342. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  343. LOG_ERROR("CMSIS-DAP command CMD_DISCONNECT failed.");
  344. return ERROR_JTAG_DEVICE_ERROR;
  345. }
  346. return ERROR_OK;
  347. }
  348. static int cmsis_dap_cmd_DAP_TFER_Configure(uint8_t idle, uint16_t delay, uint16_t retry)
  349. {
  350. int retval;
  351. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  352. buffer[0] = 0; /* report number */
  353. buffer[1] = CMD_DAP_TFER_CONFIGURE;
  354. buffer[2] = idle;
  355. buffer[3] = delay & 0xff;
  356. buffer[4] = (delay >> 8) & 0xff;
  357. buffer[5] = retry & 0xff;
  358. buffer[6] = (retry >> 8) & 0xff;
  359. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
  360. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  361. LOG_ERROR("CMSIS-DAP command CMD_TFER_Configure failed.");
  362. return ERROR_JTAG_DEVICE_ERROR;
  363. }
  364. return ERROR_OK;
  365. }
  366. static int cmsis_dap_cmd_DAP_SWD_Configure(uint8_t cfg)
  367. {
  368. int retval;
  369. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  370. buffer[0] = 0; /* report number */
  371. buffer[1] = CMD_DAP_SWD_CONFIGURE;
  372. buffer[2] = cfg;
  373. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 3);
  374. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  375. LOG_ERROR("CMSIS-DAP command CMD_SWD_Configure failed.");
  376. return ERROR_JTAG_DEVICE_ERROR;
  377. }
  378. return ERROR_OK;
  379. }
  380. #if 0
  381. static int cmsis_dap_cmd_DAP_Delay(uint16_t delay_us)
  382. {
  383. int retval;
  384. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  385. buffer[0] = 0; /* report number */
  386. buffer[1] = CMD_DAP_DELAY;
  387. buffer[2] = delay_us & 0xff;
  388. buffer[3] = (delay_us >> 8) & 0xff;
  389. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 4);
  390. if (retval != ERROR_OK || buffer[1] != DAP_OK) {
  391. LOG_ERROR("CMSIS-DAP command CMD_Delay failed.");
  392. return ERROR_JTAG_DEVICE_ERROR;
  393. }
  394. return ERROR_OK;
  395. }
  396. #endif
  397. static int queued_retval;
  398. static void cmsis_dap_swd_read_reg(struct adiv5_dap *dap, uint8_t cmd, uint32_t *value)
  399. {
  400. if (queued_retval != ERROR_OK)
  401. return;
  402. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  403. int retval;
  404. uint32_t val;
  405. DEBUG_IO("CMSIS-DAP: Read Reg 0x%02" PRIx8, cmd);
  406. buffer[0] = 0; /* report number */
  407. buffer[1] = CMD_DAP_TFER;
  408. buffer[2] = 0x00;
  409. buffer[3] = 0x01;
  410. buffer[4] = cmd;
  411. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
  412. /* TODO - need better response checking */
  413. if (retval != ERROR_OK || buffer[1] != 0x01) {
  414. LOG_ERROR("CMSIS-DAP: Read Error (0x%02" PRIx8 ")", buffer[2]);
  415. queued_retval = buffer[2];
  416. return;
  417. }
  418. val = le_to_h_u32(&buffer[3]);
  419. DEBUG_IO("0x%08" PRIx32, val);
  420. if (value)
  421. *value = val;
  422. queued_retval = retval;
  423. }
  424. static void cmsis_dap_swd_write_reg(struct adiv5_dap *dap, uint8_t cmd, uint32_t value)
  425. {
  426. if (queued_retval != ERROR_OK)
  427. return;
  428. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  429. DEBUG_IO("CMSIS-DAP: Write Reg 0x%02" PRIx8 " 0x%08" PRIx32, cmd, value);
  430. buffer[0] = 0; /* report number */
  431. buffer[1] = CMD_DAP_TFER;
  432. buffer[2] = 0x00;
  433. buffer[3] = 0x01;
  434. buffer[4] = cmd;
  435. buffer[5] = (value) & 0xff;
  436. buffer[6] = (value >> 8) & 0xff;
  437. buffer[7] = (value >> 16) & 0xff;
  438. buffer[8] = (value >> 24) & 0xff;
  439. int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 9);
  440. if (buffer[1] != 0x01) {
  441. LOG_ERROR("CMSIS-DAP: Write Error (0x%02" PRIx8 ")", buffer[2]);
  442. retval = buffer[2];
  443. }
  444. queued_retval = retval;
  445. }
  446. static int cmsis_dap_swd_run(struct adiv5_dap *dap)
  447. {
  448. int retval = queued_retval;
  449. queued_retval = ERROR_OK;
  450. return retval;
  451. }
  452. static int cmsis_dap_get_version_info(void)
  453. {
  454. uint8_t *data;
  455. /* INFO_ID_FW_VER - string */
  456. int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_FW_VER, &data);
  457. if (retval != ERROR_OK)
  458. return retval;
  459. if (data[0]) /* strlen */
  460. LOG_INFO("CMSIS-DAP: FW Version = %s", &data[1]);
  461. return ERROR_OK;
  462. }
  463. static int cmsis_dap_get_caps_info(void)
  464. {
  465. uint8_t *data;
  466. /* INFO_ID_CAPS - byte */
  467. int retval = cmsis_dap_cmd_DAP_Info(INFO_ID_CAPS, &data);
  468. if (retval != ERROR_OK)
  469. return retval;
  470. if (data[0] == 1) {
  471. uint8_t caps = data[1];
  472. cmsis_dap_handle->caps = caps;
  473. if (caps & INFO_CAPS_SWD)
  474. LOG_INFO("CMSIS-DAP: %s", info_caps_str[0]);
  475. if (caps & INFO_CAPS_JTAG)
  476. LOG_INFO("CMSIS-DAP: %s", info_caps_str[1]);
  477. }
  478. return ERROR_OK;
  479. }
  480. static int cmsis_dap_get_status(void)
  481. {
  482. uint8_t d;
  483. int retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, 0, 0, &d);
  484. if (retval == ERROR_OK) {
  485. LOG_INFO("SWCLK/TCK = %d SWDIO/TMS = %d TDI = %d TDO = %d nTRST = %d nRESET = %d",
  486. (d & (0x01 << 0)) ? 1 : 0, /* Bit 0: SWCLK/TCK */
  487. (d & (0x01 << 1)) ? 1 : 0, /* Bit 1: SWDIO/TMS */
  488. (d & (0x01 << 2)) ? 1 : 0, /* Bit 2: TDI */
  489. (d & (0x01 << 3)) ? 1 : 0, /* Bit 3: TDO */
  490. (d & (0x01 << 5)) ? 1 : 0, /* Bit 5: nTRST */
  491. (d & (0x01 << 7)) ? 1 : 0); /* Bit 7: nRESET */
  492. }
  493. return retval;
  494. }
  495. static int cmsis_dap_reset_link(void)
  496. {
  497. uint8_t *buffer = cmsis_dap_handle->packet_buffer;
  498. LOG_DEBUG("CMSIS-DAP: cmsis_dap_reset_link");
  499. LOG_INFO("DAP_SWJ Sequence (reset: 50+ '1' followed by 0)");
  500. /* reset line with SWDIO high for >50 cycles */
  501. buffer[0] = 0; /* report number */
  502. buffer[1] = CMD_DAP_SWJ_SEQ;
  503. buffer[2] = 7 * 8;
  504. buffer[3] = 0xff;
  505. buffer[4] = 0xff;
  506. buffer[5] = 0xff;
  507. buffer[6] = 0xff;
  508. buffer[7] = 0xff;
  509. buffer[8] = 0xff;
  510. buffer[9] = 0xff;
  511. int retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 10);
  512. if (retval != ERROR_OK || buffer[1] != DAP_OK)
  513. return ERROR_FAIL;
  514. /* 16bit JTAG-SWD sequence */
  515. buffer[0] = 0; /* report number */
  516. buffer[1] = CMD_DAP_SWJ_SEQ;
  517. buffer[2] = 2 * 8;
  518. buffer[3] = 0x9e;
  519. buffer[4] = 0xe7;
  520. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
  521. if (retval != ERROR_OK || buffer[1] != DAP_OK)
  522. return ERROR_FAIL;
  523. /* another reset just incase */
  524. buffer[0] = 0; /* report number */
  525. buffer[1] = CMD_DAP_SWJ_SEQ;
  526. buffer[2] = 7 * 8;
  527. buffer[3] = 0xff;
  528. buffer[4] = 0xff;
  529. buffer[5] = 0xff;
  530. buffer[6] = 0xff;
  531. buffer[7] = 0xff;
  532. buffer[8] = 0xff;
  533. buffer[9] = 0xff;
  534. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 10);
  535. if (retval != ERROR_OK || buffer[1] != DAP_OK)
  536. return ERROR_FAIL;
  537. /* 16 cycle idle period */
  538. buffer[0] = 0; /* report number */
  539. buffer[1] = CMD_DAP_SWJ_SEQ;
  540. buffer[2] = 2 * 8;
  541. buffer[3] = 0x00;
  542. buffer[4] = 0x00;
  543. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
  544. if (retval != ERROR_OK || buffer[1] != DAP_OK)
  545. return ERROR_FAIL;
  546. DEBUG_IO("DAP Read IDCODE");
  547. /* read the id code is always the next sequence */
  548. buffer[0] = 0; /* report number */
  549. buffer[1] = CMD_DAP_TFER;
  550. buffer[2] = 0x00;
  551. buffer[3] = 0x01;
  552. buffer[4] = 0x02;
  553. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 5);
  554. if (retval != ERROR_OK)
  555. return retval;
  556. if (buffer[1] == 0) {
  557. LOG_DEBUG("Result 0x%02" PRIx8 " 0x%02" PRIx8, buffer[1], buffer[2]);
  558. LOG_DEBUG("DAP Reset Target");
  559. buffer[0] = 0; /* report number */
  560. buffer[1] = CMD_DAP_RESET_TARGET;
  561. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 2);
  562. LOG_DEBUG("Result 0x%02" PRIx8 " 0x%02" PRIx8, buffer[1], buffer[2]);
  563. LOG_DEBUG("DAP Write Abort");
  564. buffer[0] = 0; /* report number */
  565. buffer[1] = CMD_DAP_WRITE_ABORT;
  566. buffer[2] = 0x00;
  567. buffer[3] = 0x1e/*0x1f*/;
  568. buffer[4] = 0x00;
  569. buffer[5] = 0x00;
  570. buffer[6] = 0x00;
  571. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
  572. LOG_DEBUG("Result 0x%02" PRIx8, buffer[1]);
  573. return 0x80 + buffer[1];
  574. }
  575. LOG_DEBUG("DAP Write Abort");
  576. buffer[0] = 0; /* report number */
  577. buffer[1] = CMD_DAP_WRITE_ABORT;
  578. buffer[2] = 0x00;
  579. buffer[3] = 0x1e;
  580. buffer[4] = 0x00;
  581. buffer[5] = 0x00;
  582. buffer[6] = 0x00;
  583. retval = cmsis_dap_usb_xfer(cmsis_dap_handle, 7);
  584. LOG_DEBUG("Result 0x%02" PRIx8, buffer[1]);
  585. return retval;
  586. }
  587. static int cmsis_dap_swd_open(void)
  588. {
  589. int retval;
  590. DEBUG_IO("CMSIS-DAP: cmsis_dap_swd_open");
  591. if (cmsis_dap_handle == NULL) {
  592. /* SWD init */
  593. retval = cmsis_dap_usb_open();
  594. if (retval != ERROR_OK)
  595. return retval;
  596. retval = cmsis_dap_get_caps_info();
  597. if (retval != ERROR_OK)
  598. return retval;
  599. }
  600. if (!(cmsis_dap_handle->caps & INFO_CAPS_SWD)) {
  601. LOG_ERROR("CMSIS-DAP: SWD not supported");
  602. return ERROR_JTAG_DEVICE_ERROR;
  603. }
  604. retval = cmsis_dap_cmd_DAP_Connect(CONNECT_SWD);
  605. if (retval != ERROR_OK)
  606. return retval;
  607. /* Add more setup here.??... */
  608. LOG_INFO("CMSIS-DAP: Interface Initialised (SWD)");
  609. return ERROR_OK;
  610. }
  611. static int cmsis_dap_init(void)
  612. {
  613. int retval;
  614. uint8_t *data;
  615. if (swd_mode) {
  616. retval = cmsis_dap_swd_open();
  617. if (retval != ERROR_OK)
  618. return retval;
  619. }
  620. if (cmsis_dap_handle == NULL) {
  621. /* JTAG init */
  622. retval = cmsis_dap_usb_open();
  623. if (retval != ERROR_OK)
  624. return retval;
  625. retval = cmsis_dap_get_caps_info();
  626. if (retval != ERROR_OK)
  627. return retval;
  628. /* Connect in JTAG mode */
  629. if (!(cmsis_dap_handle->caps & INFO_CAPS_JTAG)) {
  630. LOG_ERROR("CMSIS-DAP: JTAG not supported");
  631. return ERROR_JTAG_DEVICE_ERROR;
  632. }
  633. retval = cmsis_dap_cmd_DAP_Connect(CONNECT_JTAG);
  634. if (retval != ERROR_OK)
  635. return retval;
  636. LOG_INFO("CMSIS-DAP: Interface Initialised (JTAG)");
  637. }
  638. retval = cmsis_dap_get_version_info();
  639. if (retval != ERROR_OK)
  640. return retval;
  641. /* INFO_ID_PKT_SZ - short */
  642. retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_SZ, &data);
  643. if (retval != ERROR_OK)
  644. return retval;
  645. if (data[0] == 2) { /* short */
  646. uint16_t pkt_sz = data[1] + (data[2] << 8);
  647. if (cmsis_dap_handle->packet_size != pkt_sz + 1) {
  648. /* reallocate buffer */
  649. cmsis_dap_handle->packet_size = pkt_sz + 1;
  650. cmsis_dap_handle->packet_buffer = realloc(cmsis_dap_handle->packet_buffer,
  651. cmsis_dap_handle->packet_size);
  652. if (cmsis_dap_handle->packet_buffer == NULL) {
  653. LOG_ERROR("unable to reallocate memory");
  654. return ERROR_FAIL;
  655. }
  656. }
  657. LOG_DEBUG("CMSIS-DAP: Packet Size = %" PRId16, pkt_sz);
  658. }
  659. /* INFO_ID_PKT_CNT - byte */
  660. retval = cmsis_dap_cmd_DAP_Info(INFO_ID_PKT_CNT, &data);
  661. if (retval != ERROR_OK)
  662. return retval;
  663. if (data[0] == 1) { /* byte */
  664. uint16_t pkt_cnt = data[1];
  665. cmsis_dap_handle->packet_count = pkt_cnt;
  666. LOG_DEBUG("CMSIS-DAP: Packet Count = %" PRId16, pkt_cnt);
  667. }
  668. retval = cmsis_dap_get_status();
  669. if (retval != ERROR_OK)
  670. return ERROR_FAIL;
  671. /* Now try to connect to the target
  672. * TODO: This is all SWD only @ present */
  673. retval = cmsis_dap_cmd_DAP_SWJ_Clock(100); /* 100kHz */
  674. if (retval != ERROR_OK)
  675. return ERROR_FAIL;
  676. retval = cmsis_dap_cmd_DAP_TFER_Configure(0, 64, 0);
  677. if (retval != ERROR_OK)
  678. return ERROR_FAIL;
  679. retval = cmsis_dap_cmd_DAP_SWD_Configure(0x00);
  680. if (retval != ERROR_OK)
  681. return ERROR_FAIL;
  682. retval = cmsis_dap_cmd_DAP_LED(0x03); /* Both LEDs on */
  683. if (retval != ERROR_OK)
  684. return ERROR_FAIL;
  685. /* support connecting with srst asserted */
  686. enum reset_types jtag_reset_config = jtag_get_reset_config();
  687. if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
  688. if (jtag_reset_config & RESET_SRST_NO_GATING) {
  689. retval = cmsis_dap_cmd_DAP_SWJ_Pins(0, (1 << 7), 0, NULL);
  690. if (retval != ERROR_OK)
  691. return ERROR_FAIL;
  692. LOG_INFO("Connecting under reset");
  693. }
  694. }
  695. retval = cmsis_dap_reset_link();
  696. if (retval != ERROR_OK)
  697. return ERROR_FAIL;
  698. cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
  699. LOG_INFO("CMSIS-DAP: Interface ready");
  700. return ERROR_OK;
  701. }
  702. static int cmsis_dap_swd_init(void)
  703. {
  704. swd_mode = true;
  705. return ERROR_OK;
  706. }
  707. static int cmsis_dap_quit(void)
  708. {
  709. cmsis_dap_cmd_DAP_Disconnect();
  710. cmsis_dap_cmd_DAP_LED(0x00); /* Both LEDs off */
  711. cmsis_dap_usb_close(cmsis_dap_handle);
  712. return ERROR_OK;
  713. }
  714. static void cmsis_dap_execute_reset(struct jtag_command *cmd)
  715. {
  716. int retval = cmsis_dap_cmd_DAP_SWJ_Pins(cmd->cmd.reset->srst ? 0 : (1 << 7), \
  717. (1 << 7), 0, NULL);
  718. if (retval != ERROR_OK)
  719. LOG_ERROR("CMSIS-DAP: Interface reset failed");
  720. }
  721. static void cmsis_dap_execute_sleep(struct jtag_command *cmd)
  722. {
  723. #if 0
  724. int retval = cmsis_dap_cmd_DAP_Delay(cmd->cmd.sleep->us);
  725. if (retval != ERROR_OK)
  726. #endif
  727. jtag_sleep(cmd->cmd.sleep->us);
  728. }
  729. static void cmsis_dap_execute_command(struct jtag_command *cmd)
  730. {
  731. switch (cmd->type) {
  732. case JTAG_RESET:
  733. cmsis_dap_execute_reset(cmd);
  734. break;
  735. case JTAG_SLEEP:
  736. cmsis_dap_execute_sleep(cmd);
  737. break;
  738. default:
  739. LOG_ERROR("BUG: unknown JTAG command type encountered");
  740. exit(-1);
  741. }
  742. }
  743. static int cmsis_dap_execute_queue(void)
  744. {
  745. struct jtag_command *cmd = jtag_command_queue;
  746. while (cmd != NULL) {
  747. cmsis_dap_execute_command(cmd);
  748. cmd = cmd->next;
  749. }
  750. return ERROR_OK;
  751. }
  752. static int cmsis_dap_speed(int speed)
  753. {
  754. if (speed > DAP_MAX_CLOCK) {
  755. LOG_INFO("reduce speed request: %dkHz to %dkHz maximum", speed, DAP_MAX_CLOCK);
  756. speed = DAP_MAX_CLOCK;
  757. }
  758. if (speed == 0) {
  759. LOG_INFO("RTCK not supported");
  760. return ERROR_JTAG_NOT_IMPLEMENTED;
  761. }
  762. return cmsis_dap_cmd_DAP_SWJ_Clock(speed);
  763. }
  764. static int cmsis_dap_speed_div(int speed, int *khz)
  765. {
  766. *khz = speed;
  767. return ERROR_OK;
  768. }
  769. static int cmsis_dap_khz(int khz, int *jtag_speed)
  770. {
  771. *jtag_speed = khz;
  772. return ERROR_OK;
  773. }
  774. COMMAND_HANDLER(cmsis_dap_handle_info_command)
  775. {
  776. if (cmsis_dap_get_version_info() == ERROR_OK)
  777. cmsis_dap_get_status();
  778. return ERROR_OK;
  779. }
  780. COMMAND_HANDLER(cmsis_dap_handle_vid_pid_command)
  781. {
  782. if (CMD_ARGC > MAX_USB_IDS * 2) {
  783. LOG_WARNING("ignoring extra IDs in cmsis_dap_vid_pid "
  784. "(maximum is %d pairs)", MAX_USB_IDS);
  785. CMD_ARGC = MAX_USB_IDS * 2;
  786. }
  787. if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
  788. LOG_WARNING("incomplete cmsis_dap_vid_pid configuration directive");
  789. if (CMD_ARGC < 2)
  790. return ERROR_COMMAND_SYNTAX_ERROR;
  791. /* remove the incomplete trailing id */
  792. CMD_ARGC -= 1;
  793. }
  794. unsigned i;
  795. for (i = 0; i < CMD_ARGC; i += 2) {
  796. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], cmsis_dap_vid[i >> 1]);
  797. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], cmsis_dap_pid[i >> 1]);
  798. }
  799. /*
  800. * Explicitly terminate, in case there are multiples instances of
  801. * cmsis_dap_vid_pid.
  802. */
  803. cmsis_dap_vid[i >> 1] = cmsis_dap_pid[i >> 1] = 0;
  804. return ERROR_OK;
  805. }
  806. static const struct command_registration cmsis_dap_subcommand_handlers[] = {
  807. {
  808. .name = "info",
  809. .handler = &cmsis_dap_handle_info_command,
  810. .mode = COMMAND_EXEC,
  811. .usage = "",
  812. .help = "show cmsis-dap info",
  813. },
  814. COMMAND_REGISTRATION_DONE
  815. };
  816. static const struct command_registration cmsis_dap_command_handlers[] = {
  817. {
  818. .name = "cmsis-dap",
  819. .mode = COMMAND_ANY,
  820. .help = "perform CMSIS-DAP management",
  821. .usage = "<cmd>",
  822. .chain = cmsis_dap_subcommand_handlers,
  823. },
  824. {
  825. .name = "cmsis_dap_vid_pid",
  826. .handler = &cmsis_dap_handle_vid_pid_command,
  827. .mode = COMMAND_CONFIG,
  828. .help = "the vendor ID and product ID of the CMSIS-DAP device",
  829. .usage = "(vid pid)* ",
  830. },
  831. COMMAND_REGISTRATION_DONE
  832. };
  833. static const struct swd_driver cmsis_dap_swd_driver = {
  834. .init = cmsis_dap_swd_init,
  835. .read_reg = cmsis_dap_swd_read_reg,
  836. .write_reg = cmsis_dap_swd_write_reg,
  837. .run = cmsis_dap_swd_run,
  838. };
  839. const char *cmsis_dap_transport[] = {"cmsis-dap", NULL};
  840. struct jtag_interface cmsis_dap_interface = {
  841. .name = "cmsis-dap",
  842. .commands = cmsis_dap_command_handlers,
  843. .swd = &cmsis_dap_swd_driver,
  844. .transports = cmsis_dap_transport,
  845. .execute_queue = cmsis_dap_execute_queue,
  846. .speed = cmsis_dap_speed,
  847. .speed_div = cmsis_dap_speed_div,
  848. .khz = cmsis_dap_khz,
  849. .init = cmsis_dap_init,
  850. .quit = cmsis_dap_quit,
  851. };