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.
 
 
 
 
 
 

1851 lines
45 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011-2012 by Mathias Kuester *
  3. * Mathias Kuester <kesmtp@freenet.de> *
  4. * *
  5. * Copyright (C) 2012 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * This code is based on https://github.com/texane/stlink *
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. * This program is distributed in the hope that it will be useful, *
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  18. * GNU General Public License for more details. *
  19. * *
  20. * You should have received a copy of the GNU General Public License *
  21. * along with this program; if not, write to the *
  22. * Free Software Foundation, Inc., *
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
  24. ***************************************************************************/
  25. #ifdef HAVE_CONFIG_H
  26. #include "config.h"
  27. #endif
  28. /* project specific includes */
  29. #include <helper/binarybuffer.h>
  30. #include <jtag/interface.h>
  31. #include <jtag/hla/hla_layout.h>
  32. #include <jtag/hla/hla_transport.h>
  33. #include <jtag/hla/hla_interface.h>
  34. #include <target/target.h>
  35. #include <target/cortex_m.h>
  36. #include "libusb_common.h"
  37. #define ENDPOINT_IN 0x80
  38. #define ENDPOINT_OUT 0x00
  39. #define STLINK_WRITE_TIMEOUT 1000
  40. #define STLINK_READ_TIMEOUT 1000
  41. #define STLINK_NULL_EP 0
  42. #define STLINK_RX_EP (1|ENDPOINT_IN)
  43. #define STLINK_TX_EP (2|ENDPOINT_OUT)
  44. #define STLINK_TRACE_EP (3|ENDPOINT_IN)
  45. #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
  46. #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
  47. #define STLINK_SG_SIZE (31)
  48. #define STLINK_DATA_SIZE (4096)
  49. #define STLINK_CMD_SIZE_V2 (16)
  50. #define STLINK_CMD_SIZE_V1 (10)
  51. #define STLINK_V1_PID (0x3744)
  52. #define STLINK_V2_PID (0x3748)
  53. #define STLINK_V2_1_PID (0x374B)
  54. /* the current implementation of the stlink limits
  55. * 8bit read/writes to max 64 bytes. */
  56. #define STLINK_MAX_RW8 (64)
  57. /* "WAIT" responses will be retried (with exponential backoff) at
  58. * most this many times before failing to caller.
  59. */
  60. #define MAX_WAIT_RETRIES 8
  61. enum stlink_jtag_api_version {
  62. STLINK_JTAG_API_V1 = 1,
  63. STLINK_JTAG_API_V2,
  64. };
  65. /** */
  66. struct stlink_usb_version {
  67. /** */
  68. int stlink;
  69. /** */
  70. int jtag;
  71. /** */
  72. int swim;
  73. /** highest supported jtag api version */
  74. enum stlink_jtag_api_version jtag_api_max;
  75. };
  76. /** */
  77. struct stlink_usb_handle_s {
  78. /** */
  79. struct jtag_libusb_device_handle *fd;
  80. /** */
  81. struct libusb_transfer *trans;
  82. /** */
  83. uint8_t rx_ep;
  84. /** */
  85. uint8_t tx_ep;
  86. /** */
  87. uint8_t trace_ep;
  88. /** */
  89. uint8_t cmdbuf[STLINK_SG_SIZE];
  90. /** */
  91. uint8_t cmdidx;
  92. /** */
  93. uint8_t direction;
  94. /** */
  95. uint8_t databuf[STLINK_DATA_SIZE];
  96. /** */
  97. uint32_t max_mem_packet;
  98. /** */
  99. enum hl_transports transport;
  100. /** */
  101. struct stlink_usb_version version;
  102. /** */
  103. uint16_t vid;
  104. /** */
  105. uint16_t pid;
  106. /** this is the currently used jtag api */
  107. enum stlink_jtag_api_version jtag_api;
  108. /** */
  109. struct {
  110. /** whether SWO tracing is enabled or not */
  111. bool enabled;
  112. /** trace module source clock */
  113. uint32_t source_hz;
  114. } trace;
  115. /** reconnect is needed next time we try to query the
  116. * status */
  117. bool reconnect_pending;
  118. };
  119. #define STLINK_DEBUG_ERR_OK 0x80
  120. #define STLINK_DEBUG_ERR_FAULT 0x81
  121. #define STLINK_SWD_AP_WAIT 0x10
  122. #define STLINK_JTAG_WRITE_ERROR 0x0c
  123. #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
  124. #define STLINK_SWD_DP_WAIT 0x14
  125. #define STLINK_CORE_RUNNING 0x80
  126. #define STLINK_CORE_HALTED 0x81
  127. #define STLINK_CORE_STAT_UNKNOWN -1
  128. #define STLINK_GET_VERSION 0xF1
  129. #define STLINK_DEBUG_COMMAND 0xF2
  130. #define STLINK_DFU_COMMAND 0xF3
  131. #define STLINK_SWIM_COMMAND 0xF4
  132. #define STLINK_GET_CURRENT_MODE 0xF5
  133. #define STLINK_GET_TARGET_VOLTAGE 0xF7
  134. #define STLINK_DEV_DFU_MODE 0x00
  135. #define STLINK_DEV_MASS_MODE 0x01
  136. #define STLINK_DEV_DEBUG_MODE 0x02
  137. #define STLINK_DEV_SWIM_MODE 0x03
  138. #define STLINK_DEV_BOOTLOADER_MODE 0x04
  139. #define STLINK_DEV_UNKNOWN_MODE -1
  140. #define STLINK_DFU_EXIT 0x07
  141. #define STLINK_SWIM_ENTER 0x00
  142. #define STLINK_SWIM_EXIT 0x01
  143. #define STLINK_DEBUG_ENTER_JTAG 0x00
  144. #define STLINK_DEBUG_GETSTATUS 0x01
  145. #define STLINK_DEBUG_FORCEDEBUG 0x02
  146. #define STLINK_DEBUG_APIV1_RESETSYS 0x03
  147. #define STLINK_DEBUG_APIV1_READALLREGS 0x04
  148. #define STLINK_DEBUG_APIV1_READREG 0x05
  149. #define STLINK_DEBUG_APIV1_WRITEREG 0x06
  150. #define STLINK_DEBUG_READMEM_32BIT 0x07
  151. #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
  152. #define STLINK_DEBUG_RUNCORE 0x09
  153. #define STLINK_DEBUG_STEPCORE 0x0a
  154. #define STLINK_DEBUG_APIV1_SETFP 0x0b
  155. #define STLINK_DEBUG_READMEM_8BIT 0x0c
  156. #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
  157. #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
  158. #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
  159. #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
  160. #define STLINK_DEBUG_ENTER_JTAG 0x00
  161. #define STLINK_DEBUG_ENTER_SWD 0xa3
  162. #define STLINK_DEBUG_APIV1_ENTER 0x20
  163. #define STLINK_DEBUG_EXIT 0x21
  164. #define STLINK_DEBUG_READCOREID 0x22
  165. #define STLINK_DEBUG_APIV2_ENTER 0x30
  166. #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
  167. #define STLINK_DEBUG_APIV2_RESETSYS 0x32
  168. #define STLINK_DEBUG_APIV2_READREG 0x33
  169. #define STLINK_DEBUG_APIV2_WRITEREG 0x34
  170. #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
  171. #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
  172. #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
  173. #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
  174. #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
  175. #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
  176. #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
  177. #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
  178. #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
  179. #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
  180. #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
  181. #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
  182. #define STLINK_TRACE_SIZE 1024
  183. #define STLINK_TRACE_MAX_HZ 2000000
  184. #define STLINK_TRACE_MIN_VERSION 13
  185. /** */
  186. enum stlink_mode {
  187. STLINK_MODE_UNKNOWN = 0,
  188. STLINK_MODE_DFU,
  189. STLINK_MODE_MASS,
  190. STLINK_MODE_DEBUG_JTAG,
  191. STLINK_MODE_DEBUG_SWD,
  192. STLINK_MODE_DEBUG_SWIM
  193. };
  194. #define REQUEST_SENSE 0x03
  195. #define REQUEST_SENSE_LENGTH 18
  196. static const struct {
  197. int speed;
  198. int speed_divisor;
  199. } stlink_khz_to_speed_map[] = {
  200. {4000, 0},
  201. {1800, 1}, /* default */
  202. {1200, 2},
  203. {950, 3},
  204. {480, 7},
  205. {240, 15},
  206. {125, 31},
  207. {100, 40},
  208. {50, 79},
  209. {25, 158},
  210. {15, 265},
  211. {5, 798}
  212. };
  213. static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
  214. /** */
  215. static int stlink_usb_xfer_v1_get_status(void *handle)
  216. {
  217. struct stlink_usb_handle_s *h = handle;
  218. assert(handle != NULL);
  219. /* read status */
  220. memset(h->cmdbuf, 0, STLINK_SG_SIZE);
  221. if (jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)h->cmdbuf,
  222. 13, STLINK_READ_TIMEOUT) != 13)
  223. return ERROR_FAIL;
  224. uint32_t t1;
  225. t1 = buf_get_u32(h->cmdbuf, 0, 32);
  226. /* check for USBS */
  227. if (t1 != 0x53425355)
  228. return ERROR_FAIL;
  229. /*
  230. * CSW status:
  231. * 0 success
  232. * 1 command failure
  233. * 2 phase error
  234. */
  235. if (h->cmdbuf[12] != 0)
  236. return ERROR_FAIL;
  237. return ERROR_OK;
  238. }
  239. /** */
  240. static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
  241. {
  242. struct stlink_usb_handle_s *h = handle;
  243. assert(handle != NULL);
  244. if (jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)h->cmdbuf, cmdsize,
  245. STLINK_WRITE_TIMEOUT) != cmdsize) {
  246. return ERROR_FAIL;
  247. }
  248. if (h->direction == h->tx_ep && size) {
  249. if (jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)buf,
  250. size, STLINK_WRITE_TIMEOUT) != size) {
  251. LOG_DEBUG("bulk write failed");
  252. return ERROR_FAIL;
  253. }
  254. } else if (h->direction == h->rx_ep && size) {
  255. if (jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)buf,
  256. size, STLINK_READ_TIMEOUT) != size) {
  257. LOG_DEBUG("bulk read failed");
  258. return ERROR_FAIL;
  259. }
  260. }
  261. return ERROR_OK;
  262. }
  263. /** */
  264. static int stlink_usb_xfer_v1_get_sense(void *handle)
  265. {
  266. int res;
  267. struct stlink_usb_handle_s *h = handle;
  268. assert(handle != NULL);
  269. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  270. h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
  271. h->cmdbuf[h->cmdidx++] = 0;
  272. h->cmdbuf[h->cmdidx++] = 0;
  273. h->cmdbuf[h->cmdidx++] = 0;
  274. h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
  275. res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
  276. if (res != ERROR_OK)
  277. return res;
  278. if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
  279. return ERROR_FAIL;
  280. return ERROR_OK;
  281. }
  282. /** */
  283. static int stlink_usb_xfer(void *handle, const uint8_t *buf, int size)
  284. {
  285. int err, cmdsize = STLINK_CMD_SIZE_V2;
  286. struct stlink_usb_handle_s *h = handle;
  287. assert(handle != NULL);
  288. if (h->version.stlink == 1)
  289. cmdsize = STLINK_SG_SIZE;
  290. err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
  291. if (err != ERROR_OK)
  292. return err;
  293. if (h->version.stlink == 1) {
  294. if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
  295. /* check csw status */
  296. if (h->cmdbuf[12] == 1) {
  297. LOG_DEBUG("get sense");
  298. if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
  299. return ERROR_FAIL;
  300. }
  301. return ERROR_FAIL;
  302. }
  303. }
  304. return ERROR_OK;
  305. }
  306. /**
  307. Converts an STLINK status code held in the first byte of a response
  308. to an openocd error, logs any error/wait status as debug output.
  309. */
  310. static int stlink_usb_error_check(void *handle)
  311. {
  312. struct stlink_usb_handle_s *h = handle;
  313. assert(handle != NULL);
  314. /* TODO: no error checking yet on api V1 */
  315. if (h->jtag_api == STLINK_JTAG_API_V1)
  316. h->databuf[0] = STLINK_DEBUG_ERR_OK;
  317. switch (h->databuf[0]) {
  318. case STLINK_DEBUG_ERR_OK:
  319. return ERROR_OK;
  320. case STLINK_DEBUG_ERR_FAULT:
  321. LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT);
  322. return ERROR_FAIL;
  323. case STLINK_SWD_AP_WAIT:
  324. LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT);
  325. return ERROR_WAIT;
  326. case STLINK_SWD_DP_WAIT:
  327. LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_AP_WAIT);
  328. return ERROR_WAIT;
  329. case STLINK_JTAG_WRITE_ERROR:
  330. LOG_DEBUG("Write error");
  331. return ERROR_FAIL;
  332. case STLINK_JTAG_WRITE_VERIF_ERROR:
  333. LOG_DEBUG("Verify error");
  334. return ERROR_FAIL;
  335. default:
  336. LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
  337. return ERROR_FAIL;
  338. }
  339. }
  340. /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
  341. Works for commands where the STLINK_DEBUG status is returned in the first
  342. byte of the response packet.
  343. Returns an openocd result code.
  344. */
  345. static int stlink_cmd_allow_retry(void *handle, const uint8_t *buf, int size)
  346. {
  347. int retries = 0;
  348. int res;
  349. while (1) {
  350. res = stlink_usb_xfer(handle, buf, size);
  351. if (res != ERROR_OK)
  352. return res;
  353. res = stlink_usb_error_check(handle);
  354. if (res == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  355. usleep((1<<retries++) * 1000);
  356. continue;
  357. }
  358. return res;
  359. }
  360. }
  361. /** */
  362. static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
  363. {
  364. struct stlink_usb_handle_s *h = handle;
  365. assert(handle != NULL);
  366. assert(h->version.stlink >= 2);
  367. if (jtag_libusb_bulk_read(h->fd, h->trace_ep, (char *)buf,
  368. size, STLINK_READ_TIMEOUT) != size) {
  369. LOG_ERROR("bulk trace read failed");
  370. return ERROR_FAIL;
  371. }
  372. return ERROR_OK;
  373. }
  374. /** */
  375. static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
  376. {
  377. struct stlink_usb_handle_s *h = handle;
  378. /* fill the send buffer */
  379. strcpy((char *)h->cmdbuf, "USBC");
  380. h->cmdidx += 4;
  381. /* csw tag not used */
  382. h->cmdidx += 4;
  383. buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
  384. h->cmdidx += 4;
  385. h->cmdbuf[h->cmdidx++] = (direction == h->rx_ep ? ENDPOINT_IN : ENDPOINT_OUT);
  386. h->cmdbuf[h->cmdidx++] = 0; /* lun */
  387. h->cmdbuf[h->cmdidx++] = STLINK_CMD_SIZE_V1;
  388. }
  389. /** */
  390. static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
  391. {
  392. struct stlink_usb_handle_s *h = handle;
  393. h->direction = direction;
  394. h->cmdidx = 0;
  395. memset(h->cmdbuf, 0, STLINK_SG_SIZE);
  396. memset(h->databuf, 0, STLINK_DATA_SIZE);
  397. if (h->version.stlink == 1)
  398. stlink_usb_xfer_v1_create_cmd(handle, direction, size);
  399. }
  400. /** */
  401. static int stlink_usb_version(void *handle)
  402. {
  403. int res;
  404. uint16_t v;
  405. struct stlink_usb_handle_s *h = handle;
  406. assert(handle != NULL);
  407. stlink_usb_init_buffer(handle, h->rx_ep, 6);
  408. h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
  409. res = stlink_usb_xfer(handle, h->databuf, 6);
  410. if (res != ERROR_OK)
  411. return res;
  412. v = (h->databuf[0] << 8) | h->databuf[1];
  413. h->version.stlink = (v >> 12) & 0x0f;
  414. h->version.jtag = (v >> 6) & 0x3f;
  415. h->version.swim = v & 0x3f;
  416. h->vid = buf_get_u32(h->databuf, 16, 16);
  417. h->pid = buf_get_u32(h->databuf, 32, 16);
  418. /* set the supported jtag api version
  419. * API V2 is supported since JTAG V11
  420. */
  421. if (h->version.jtag >= 11)
  422. h->version.jtag_api_max = STLINK_JTAG_API_V2;
  423. else
  424. h->version.jtag_api_max = STLINK_JTAG_API_V1;
  425. LOG_INFO("STLINK v%d JTAG v%d API v%d SWIM v%d VID 0x%04X PID 0x%04X",
  426. h->version.stlink,
  427. h->version.jtag,
  428. (h->version.jtag_api_max == STLINK_JTAG_API_V1) ? 1 : 2,
  429. h->version.swim,
  430. h->vid,
  431. h->pid);
  432. return ERROR_OK;
  433. }
  434. static int stlink_usb_check_voltage(void *handle, float *target_voltage)
  435. {
  436. struct stlink_usb_handle_s *h = handle;
  437. uint32_t adc_results[2];
  438. /* only supported by stlink/v2 and for firmware >= 13 */
  439. if (h->version.stlink == 1 || h->version.jtag < 13)
  440. return ERROR_COMMAND_NOTFOUND;
  441. stlink_usb_init_buffer(handle, h->rx_ep, 8);
  442. h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
  443. int result = stlink_usb_xfer(handle, h->databuf, 8);
  444. if (result != ERROR_OK)
  445. return result;
  446. /* convert result */
  447. adc_results[0] = le_to_h_u32(h->databuf);
  448. adc_results[1] = le_to_h_u32(h->databuf + 4);
  449. *target_voltage = 0;
  450. if (adc_results[0])
  451. *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
  452. LOG_INFO("Target voltage: %f", (double)*target_voltage);
  453. return ERROR_OK;
  454. }
  455. static int stlink_usb_set_swdclk(void *handle, uint16_t clk_divisor)
  456. {
  457. struct stlink_usb_handle_s *h = handle;
  458. assert(handle != NULL);
  459. /* only supported by stlink/v2 and for firmware >= 22 */
  460. if (h->version.stlink == 1 || h->version.jtag < 22)
  461. return ERROR_COMMAND_NOTFOUND;
  462. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  463. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  464. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ;
  465. h_u16_to_le(h->cmdbuf+h->cmdidx, clk_divisor);
  466. h->cmdidx += 2;
  467. int result = stlink_cmd_allow_retry(handle, h->databuf, 2);
  468. if (result != ERROR_OK)
  469. return result;
  470. return ERROR_OK;
  471. }
  472. /** */
  473. static int stlink_usb_current_mode(void *handle, uint8_t *mode)
  474. {
  475. int res;
  476. struct stlink_usb_handle_s *h = handle;
  477. assert(handle != NULL);
  478. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  479. h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
  480. res = stlink_usb_xfer(handle, h->databuf, 2);
  481. if (res != ERROR_OK)
  482. return res;
  483. *mode = h->databuf[0];
  484. return ERROR_OK;
  485. }
  486. /** */
  487. static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
  488. {
  489. int rx_size = 0;
  490. struct stlink_usb_handle_s *h = handle;
  491. assert(handle != NULL);
  492. /* on api V2 we are able the read the latest command
  493. * status
  494. * TODO: we need the test on api V1 too
  495. */
  496. if (h->jtag_api == STLINK_JTAG_API_V2)
  497. rx_size = 2;
  498. stlink_usb_init_buffer(handle, h->rx_ep, rx_size);
  499. switch (type) {
  500. case STLINK_MODE_DEBUG_JTAG:
  501. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  502. if (h->jtag_api == STLINK_JTAG_API_V1)
  503. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
  504. else
  505. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
  506. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG;
  507. break;
  508. case STLINK_MODE_DEBUG_SWD:
  509. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  510. if (h->jtag_api == STLINK_JTAG_API_V1)
  511. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
  512. else
  513. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
  514. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD;
  515. break;
  516. case STLINK_MODE_DEBUG_SWIM:
  517. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  518. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
  519. break;
  520. case STLINK_MODE_DFU:
  521. case STLINK_MODE_MASS:
  522. default:
  523. return ERROR_FAIL;
  524. }
  525. return stlink_cmd_allow_retry(handle, h->databuf, rx_size);
  526. }
  527. /** */
  528. static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
  529. {
  530. int res;
  531. struct stlink_usb_handle_s *h = handle;
  532. assert(handle != NULL);
  533. stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
  534. switch (type) {
  535. case STLINK_MODE_DEBUG_JTAG:
  536. case STLINK_MODE_DEBUG_SWD:
  537. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  538. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
  539. break;
  540. case STLINK_MODE_DEBUG_SWIM:
  541. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  542. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
  543. break;
  544. case STLINK_MODE_DFU:
  545. h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
  546. h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
  547. break;
  548. case STLINK_MODE_MASS:
  549. default:
  550. return ERROR_FAIL;
  551. }
  552. res = stlink_usb_xfer(handle, 0, 0);
  553. if (res != ERROR_OK)
  554. return res;
  555. return ERROR_OK;
  556. }
  557. static int stlink_usb_assert_srst(void *handle, int srst);
  558. static enum stlink_mode stlink_get_mode(enum hl_transports t)
  559. {
  560. switch (t) {
  561. case HL_TRANSPORT_SWD:
  562. return STLINK_MODE_DEBUG_SWD;
  563. case HL_TRANSPORT_JTAG:
  564. return STLINK_MODE_DEBUG_JTAG;
  565. case HL_TRANSPORT_SWIM:
  566. return STLINK_MODE_DEBUG_SWIM;
  567. default:
  568. return STLINK_MODE_UNKNOWN;
  569. }
  570. }
  571. /** */
  572. static int stlink_usb_init_mode(void *handle, bool connect_under_reset)
  573. {
  574. int res;
  575. uint8_t mode;
  576. enum stlink_mode emode;
  577. struct stlink_usb_handle_s *h = handle;
  578. assert(handle != NULL);
  579. res = stlink_usb_current_mode(handle, &mode);
  580. if (res != ERROR_OK)
  581. return res;
  582. LOG_DEBUG("MODE: 0x%02X", mode);
  583. /* try to exit current mode */
  584. switch (mode) {
  585. case STLINK_DEV_DFU_MODE:
  586. emode = STLINK_MODE_DFU;
  587. break;
  588. case STLINK_DEV_DEBUG_MODE:
  589. emode = STLINK_MODE_DEBUG_SWD;
  590. break;
  591. case STLINK_DEV_SWIM_MODE:
  592. emode = STLINK_MODE_DEBUG_SWIM;
  593. break;
  594. case STLINK_DEV_BOOTLOADER_MODE:
  595. case STLINK_DEV_MASS_MODE:
  596. default:
  597. emode = STLINK_MODE_UNKNOWN;
  598. break;
  599. }
  600. if (emode != STLINK_MODE_UNKNOWN) {
  601. res = stlink_usb_mode_leave(handle, emode);
  602. if (res != ERROR_OK)
  603. return res;
  604. }
  605. res = stlink_usb_current_mode(handle, &mode);
  606. if (res != ERROR_OK)
  607. return res;
  608. /* we check the target voltage here as an aid to debugging connection problems.
  609. * the stlink requires the target Vdd to be connected for reliable debugging.
  610. * this cmd is supported in all modes except DFU
  611. */
  612. if (mode != STLINK_DEV_DFU_MODE) {
  613. float target_voltage;
  614. /* check target voltage (if supported) */
  615. res = stlink_usb_check_voltage(h, &target_voltage);
  616. if (res != ERROR_OK) {
  617. if (res != ERROR_COMMAND_NOTFOUND)
  618. LOG_ERROR("voltage check failed");
  619. /* attempt to continue as it is not a catastrophic failure */
  620. } else {
  621. /* check for a sensible target voltage, operating range is 1.65-5.5v
  622. * according to datasheet */
  623. if (target_voltage < 1.5)
  624. LOG_ERROR("target voltage may be too low for reliable debugging");
  625. }
  626. }
  627. LOG_DEBUG("MODE: 0x%02X", mode);
  628. /* set selected mode */
  629. emode = stlink_get_mode(h->transport);
  630. if (emode == STLINK_MODE_UNKNOWN) {
  631. LOG_ERROR("selected mode (transport) not supported");
  632. return ERROR_FAIL;
  633. }
  634. if (connect_under_reset) {
  635. res = stlink_usb_assert_srst(handle, 0);
  636. if (res != ERROR_OK)
  637. return res;
  638. }
  639. res = stlink_usb_mode_enter(handle, emode);
  640. if (res != ERROR_OK)
  641. return res;
  642. res = stlink_usb_current_mode(handle, &mode);
  643. if (res != ERROR_OK)
  644. return res;
  645. LOG_DEBUG("MODE: 0x%02X", mode);
  646. return ERROR_OK;
  647. }
  648. /** */
  649. static int stlink_usb_idcode(void *handle, uint32_t *idcode)
  650. {
  651. int res;
  652. struct stlink_usb_handle_s *h = handle;
  653. assert(handle != NULL);
  654. stlink_usb_init_buffer(handle, h->rx_ep, 4);
  655. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  656. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
  657. res = stlink_usb_xfer(handle, h->databuf, 4);
  658. if (res != ERROR_OK)
  659. return res;
  660. *idcode = le_to_h_u32(h->databuf);
  661. LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
  662. return ERROR_OK;
  663. }
  664. static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
  665. {
  666. struct stlink_usb_handle_s *h = handle;
  667. int res;
  668. assert(handle != NULL);
  669. stlink_usb_init_buffer(handle, h->rx_ep, 8);
  670. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  671. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
  672. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  673. h->cmdidx += 4;
  674. res = stlink_cmd_allow_retry(handle, h->databuf, 8);
  675. if (res != ERROR_OK)
  676. return res;
  677. *val = le_to_h_u32(h->databuf + 4);
  678. return ERROR_OK;
  679. }
  680. static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
  681. {
  682. struct stlink_usb_handle_s *h = handle;
  683. assert(handle != NULL);
  684. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  685. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  686. if (h->jtag_api == STLINK_JTAG_API_V1)
  687. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
  688. else
  689. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
  690. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  691. h->cmdidx += 4;
  692. h_u32_to_le(h->cmdbuf+h->cmdidx, val);
  693. h->cmdidx += 4;
  694. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  695. }
  696. /** */
  697. static int stlink_usb_trace_read(void *handle, uint8_t *buf, size_t *size)
  698. {
  699. struct stlink_usb_handle_s *h = handle;
  700. assert(handle != NULL);
  701. if (h->trace.enabled && h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
  702. int res;
  703. stlink_usb_init_buffer(handle, h->rx_ep, 10);
  704. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  705. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
  706. res = stlink_usb_xfer(handle, h->databuf, 2);
  707. if (res != ERROR_OK)
  708. return res;
  709. size_t bytes_avail = le_to_h_u16(h->databuf);
  710. *size = bytes_avail < *size ? bytes_avail : *size - 1;
  711. if (*size > 0) {
  712. res = stlink_usb_read_trace(handle, buf, *size);
  713. if (res != ERROR_OK)
  714. return res;
  715. return ERROR_OK;
  716. }
  717. }
  718. *size = 0;
  719. return ERROR_OK;
  720. }
  721. static enum target_state stlink_usb_v2_get_status(void *handle)
  722. {
  723. int result;
  724. uint32_t status;
  725. result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
  726. if (result != ERROR_OK)
  727. return TARGET_UNKNOWN;
  728. if (status & S_HALT)
  729. return TARGET_HALTED;
  730. else if (status & S_RESET_ST)
  731. return TARGET_RESET;
  732. return TARGET_RUNNING;
  733. }
  734. /** */
  735. static enum target_state stlink_usb_state(void *handle)
  736. {
  737. int res;
  738. struct stlink_usb_handle_s *h = handle;
  739. assert(handle != NULL);
  740. if (h->reconnect_pending) {
  741. LOG_INFO("Previous state query failed, trying to reconnect");
  742. res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
  743. if (res != ERROR_OK)
  744. return TARGET_UNKNOWN;
  745. h->reconnect_pending = false;
  746. }
  747. if (h->jtag_api == STLINK_JTAG_API_V2) {
  748. res = stlink_usb_v2_get_status(handle);
  749. if (res == TARGET_UNKNOWN)
  750. h->reconnect_pending = true;
  751. return res;
  752. }
  753. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  754. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  755. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
  756. res = stlink_usb_xfer(handle, h->databuf, 2);
  757. if (res != ERROR_OK)
  758. return TARGET_UNKNOWN;
  759. if (h->databuf[0] == STLINK_CORE_RUNNING)
  760. return TARGET_RUNNING;
  761. if (h->databuf[0] == STLINK_CORE_HALTED)
  762. return TARGET_HALTED;
  763. h->reconnect_pending = true;
  764. return TARGET_UNKNOWN;
  765. }
  766. static int stlink_usb_assert_srst(void *handle, int srst)
  767. {
  768. struct stlink_usb_handle_s *h = handle;
  769. assert(handle != NULL);
  770. if (h->version.stlink == 1)
  771. return ERROR_COMMAND_NOTFOUND;
  772. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  773. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  774. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
  775. h->cmdbuf[h->cmdidx++] = srst;
  776. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  777. }
  778. /** */
  779. static void stlink_usb_trace_disable(void *handle)
  780. {
  781. int res = ERROR_OK;
  782. struct stlink_usb_handle_s *h = handle;
  783. assert(handle != NULL);
  784. assert(h->version.jtag >= STLINK_TRACE_MIN_VERSION);
  785. LOG_DEBUG("Tracing: disable");
  786. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  787. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  788. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
  789. res = stlink_usb_xfer(handle, h->databuf, 2);
  790. if (res == ERROR_OK)
  791. h->trace.enabled = false;
  792. }
  793. /** */
  794. static int stlink_usb_trace_enable(void *handle)
  795. {
  796. int res;
  797. struct stlink_usb_handle_s *h = handle;
  798. assert(handle != NULL);
  799. if (h->version.jtag >= STLINK_TRACE_MIN_VERSION) {
  800. stlink_usb_init_buffer(handle, h->rx_ep, 10);
  801. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  802. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX;
  803. h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE);
  804. h->cmdidx += 2;
  805. h_u32_to_le(h->cmdbuf+h->cmdidx, h->trace.source_hz);
  806. h->cmdidx += 4;
  807. res = stlink_usb_xfer(handle, h->databuf, 2);
  808. if (res == ERROR_OK) {
  809. h->trace.enabled = true;
  810. LOG_DEBUG("Tracing: recording at %" PRIu32 "Hz", h->trace.source_hz);
  811. }
  812. } else {
  813. LOG_ERROR("Tracing is not supported by this version.");
  814. res = ERROR_FAIL;
  815. }
  816. return res;
  817. }
  818. /** */
  819. static int stlink_usb_reset(void *handle)
  820. {
  821. struct stlink_usb_handle_s *h = handle;
  822. int retval;
  823. assert(handle != NULL);
  824. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  825. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  826. if (h->jtag_api == STLINK_JTAG_API_V1)
  827. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
  828. else
  829. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
  830. retval = stlink_cmd_allow_retry(handle, h->databuf, 2);
  831. if (retval != ERROR_OK)
  832. return retval;
  833. if (h->trace.enabled) {
  834. stlink_usb_trace_disable(h);
  835. return stlink_usb_trace_enable(h);
  836. }
  837. return ERROR_OK;
  838. }
  839. /** */
  840. static int stlink_usb_run(void *handle)
  841. {
  842. int res;
  843. struct stlink_usb_handle_s *h = handle;
  844. assert(handle != NULL);
  845. if (h->jtag_api == STLINK_JTAG_API_V2) {
  846. res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
  847. return res;
  848. }
  849. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  850. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  851. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
  852. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  853. }
  854. /** */
  855. static int stlink_usb_halt(void *handle)
  856. {
  857. int res;
  858. struct stlink_usb_handle_s *h = handle;
  859. assert(handle != NULL);
  860. if (h->jtag_api == STLINK_JTAG_API_V2) {
  861. res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
  862. return res;
  863. }
  864. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  865. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  866. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
  867. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  868. }
  869. /** */
  870. static int stlink_usb_step(void *handle)
  871. {
  872. struct stlink_usb_handle_s *h = handle;
  873. assert(handle != NULL);
  874. if (h->jtag_api == STLINK_JTAG_API_V2) {
  875. /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
  876. * that the cortex-m3 currently does. */
  877. stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
  878. stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
  879. return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
  880. }
  881. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  882. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  883. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
  884. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  885. }
  886. /** */
  887. static int stlink_usb_read_regs(void *handle)
  888. {
  889. int res;
  890. struct stlink_usb_handle_s *h = handle;
  891. assert(handle != NULL);
  892. stlink_usb_init_buffer(handle, h->rx_ep, 84);
  893. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  894. if (h->jtag_api == STLINK_JTAG_API_V1)
  895. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
  896. else
  897. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
  898. res = stlink_usb_xfer(handle, h->databuf, 84);
  899. if (res != ERROR_OK)
  900. return res;
  901. return ERROR_OK;
  902. }
  903. /** */
  904. static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
  905. {
  906. int res;
  907. struct stlink_usb_handle_s *h = handle;
  908. assert(handle != NULL);
  909. stlink_usb_init_buffer(handle, h->rx_ep, h->jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
  910. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  911. if (h->jtag_api == STLINK_JTAG_API_V1)
  912. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
  913. else
  914. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
  915. h->cmdbuf[h->cmdidx++] = num;
  916. if (h->jtag_api == STLINK_JTAG_API_V1) {
  917. res = stlink_usb_xfer(handle, h->databuf, 4);
  918. if (res != ERROR_OK)
  919. return res;
  920. *val = le_to_h_u32(h->databuf);
  921. return ERROR_OK;
  922. } else {
  923. res = stlink_cmd_allow_retry(handle, h->databuf, 8);
  924. if (res != ERROR_OK)
  925. return res;
  926. *val = le_to_h_u32(h->databuf + 4);
  927. return ERROR_OK;
  928. }
  929. }
  930. /** */
  931. static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
  932. {
  933. struct stlink_usb_handle_s *h = handle;
  934. assert(handle != NULL);
  935. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  936. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  937. if (h->jtag_api == STLINK_JTAG_API_V1)
  938. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
  939. else
  940. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
  941. h->cmdbuf[h->cmdidx++] = num;
  942. h_u32_to_le(h->cmdbuf+h->cmdidx, val);
  943. h->cmdidx += 4;
  944. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  945. }
  946. static int stlink_usb_get_rw_status(void *handle)
  947. {
  948. int res;
  949. struct stlink_usb_handle_s *h = handle;
  950. assert(handle != NULL);
  951. if (h->jtag_api == STLINK_JTAG_API_V1)
  952. return ERROR_OK;
  953. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  954. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  955. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
  956. res = stlink_usb_xfer(handle, h->databuf, 2);
  957. if (res != ERROR_OK)
  958. return res;
  959. return stlink_usb_error_check(h);
  960. }
  961. /** */
  962. static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
  963. uint8_t *buffer)
  964. {
  965. int res;
  966. uint16_t read_len = len;
  967. struct stlink_usb_handle_s *h = handle;
  968. assert(handle != NULL);
  969. /* max 8bit read/write is 64bytes */
  970. if (len > STLINK_MAX_RW8) {
  971. LOG_DEBUG("max buffer length exceeded");
  972. return ERROR_FAIL;
  973. }
  974. stlink_usb_init_buffer(handle, h->rx_ep, read_len);
  975. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  976. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
  977. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  978. h->cmdidx += 4;
  979. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  980. h->cmdidx += 2;
  981. /* we need to fix read length for single bytes */
  982. if (read_len == 1)
  983. read_len++;
  984. res = stlink_usb_xfer(handle, h->databuf, read_len);
  985. if (res != ERROR_OK)
  986. return res;
  987. memcpy(buffer, h->databuf, len);
  988. return stlink_usb_get_rw_status(handle);
  989. }
  990. /** */
  991. static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
  992. const uint8_t *buffer)
  993. {
  994. int res;
  995. struct stlink_usb_handle_s *h = handle;
  996. assert(handle != NULL);
  997. /* max 8bit read/write is 64bytes */
  998. if (len > STLINK_MAX_RW8) {
  999. LOG_DEBUG("max buffer length exceeded");
  1000. return ERROR_FAIL;
  1001. }
  1002. stlink_usb_init_buffer(handle, h->tx_ep, len);
  1003. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1004. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
  1005. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1006. h->cmdidx += 4;
  1007. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1008. h->cmdidx += 2;
  1009. res = stlink_usb_xfer(handle, buffer, len);
  1010. if (res != ERROR_OK)
  1011. return res;
  1012. return stlink_usb_get_rw_status(handle);
  1013. }
  1014. /** */
  1015. static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
  1016. uint8_t *buffer)
  1017. {
  1018. int res;
  1019. struct stlink_usb_handle_s *h = handle;
  1020. assert(handle != NULL);
  1021. /* data must be a multiple of 4 and word aligned */
  1022. if (len % 4 || addr % 4) {
  1023. LOG_DEBUG("Invalid data alignment");
  1024. return ERROR_TARGET_UNALIGNED_ACCESS;
  1025. }
  1026. stlink_usb_init_buffer(handle, h->rx_ep, len);
  1027. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1028. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
  1029. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1030. h->cmdidx += 4;
  1031. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1032. h->cmdidx += 2;
  1033. res = stlink_usb_xfer(handle, h->databuf, len);
  1034. if (res != ERROR_OK)
  1035. return res;
  1036. memcpy(buffer, h->databuf, len);
  1037. return stlink_usb_get_rw_status(handle);
  1038. }
  1039. /** */
  1040. static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
  1041. const uint8_t *buffer)
  1042. {
  1043. int res;
  1044. struct stlink_usb_handle_s *h = handle;
  1045. assert(handle != NULL);
  1046. /* data must be a multiple of 4 and word aligned */
  1047. if (len % 4 || addr % 4) {
  1048. LOG_DEBUG("Invalid data alignment");
  1049. return ERROR_TARGET_UNALIGNED_ACCESS;
  1050. }
  1051. stlink_usb_init_buffer(handle, h->tx_ep, len);
  1052. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1053. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
  1054. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1055. h->cmdidx += 4;
  1056. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1057. h->cmdidx += 2;
  1058. res = stlink_usb_xfer(handle, buffer, len);
  1059. if (res != ERROR_OK)
  1060. return res;
  1061. return stlink_usb_get_rw_status(handle);
  1062. }
  1063. static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
  1064. {
  1065. uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
  1066. if (max_tar_block == 0)
  1067. max_tar_block = 4;
  1068. return max_tar_block;
  1069. }
  1070. static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
  1071. uint32_t count, uint8_t *buffer)
  1072. {
  1073. int retval = ERROR_OK;
  1074. uint32_t bytes_remaining;
  1075. int retries = 0;
  1076. struct stlink_usb_handle_s *h = handle;
  1077. /* calculate byte count */
  1078. count *= size;
  1079. while (count) {
  1080. bytes_remaining = (size == 4) ? \
  1081. stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
  1082. if (count < bytes_remaining)
  1083. bytes_remaining = count;
  1084. /* the stlink only supports 8/32bit memory read/writes
  1085. * honour 32bit, all others will be handled as 8bit access */
  1086. if (size == 4) {
  1087. /* When in jtag mode the stlink uses the auto-increment functinality.
  1088. * However it expects us to pass the data correctly, this includes
  1089. * alignment and any page boundaries. We already do this as part of the
  1090. * adi_v5 implementation, but the stlink is a hla adapter and so this
  1091. * needs implementiong manually.
  1092. * currently this only affects jtag mode, according to ST they do single
  1093. * access in SWD mode - but this may change and so we do it for both modes */
  1094. /* we first need to check for any unaligned bytes */
  1095. if (addr % 4) {
  1096. uint32_t head_bytes = 4 - (addr % 4);
  1097. retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
  1098. if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  1099. usleep((1<<retries++) * 1000);
  1100. continue;
  1101. }
  1102. if (retval != ERROR_OK)
  1103. return retval;
  1104. buffer += head_bytes;
  1105. addr += head_bytes;
  1106. count -= head_bytes;
  1107. bytes_remaining -= head_bytes;
  1108. }
  1109. if (bytes_remaining % 4)
  1110. retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
  1111. else
  1112. retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
  1113. } else
  1114. retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
  1115. if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  1116. usleep((1<<retries++) * 1000);
  1117. continue;
  1118. }
  1119. if (retval != ERROR_OK)
  1120. return retval;
  1121. buffer += bytes_remaining;
  1122. addr += bytes_remaining;
  1123. count -= bytes_remaining;
  1124. }
  1125. return retval;
  1126. }
  1127. static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
  1128. uint32_t count, const uint8_t *buffer)
  1129. {
  1130. int retval = ERROR_OK;
  1131. uint32_t bytes_remaining;
  1132. int retries = 0;
  1133. struct stlink_usb_handle_s *h = handle;
  1134. /* calculate byte count */
  1135. count *= size;
  1136. while (count) {
  1137. bytes_remaining = (size == 4) ? \
  1138. stlink_max_block_size(h->max_mem_packet, addr) : STLINK_MAX_RW8;
  1139. if (count < bytes_remaining)
  1140. bytes_remaining = count;
  1141. /* the stlink only supports 8/32bit memory read/writes
  1142. * honour 32bit, all others will be handled as 8bit access */
  1143. if (size == 4) {
  1144. /* When in jtag mode the stlink uses the auto-increment functinality.
  1145. * However it expects us to pass the data correctly, this includes
  1146. * alignment and any page boundaries. We already do this as part of the
  1147. * adi_v5 implementation, but the stlink is a hla adapter and so this
  1148. * needs implementiong manually.
  1149. * currently this only affects jtag mode, according to ST they do single
  1150. * access in SWD mode - but this may change and so we do it for both modes */
  1151. /* we first need to check for any unaligned bytes */
  1152. if (addr % 4) {
  1153. uint32_t head_bytes = 4 - (addr % 4);
  1154. retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
  1155. if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  1156. usleep((1<<retries++) * 1000);
  1157. continue;
  1158. }
  1159. if (retval != ERROR_OK)
  1160. return retval;
  1161. buffer += head_bytes;
  1162. addr += head_bytes;
  1163. count -= head_bytes;
  1164. bytes_remaining -= head_bytes;
  1165. }
  1166. if (bytes_remaining % 4)
  1167. retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
  1168. else
  1169. retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
  1170. } else
  1171. retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
  1172. if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  1173. usleep((1<<retries++) * 1000);
  1174. continue;
  1175. }
  1176. if (retval != ERROR_OK)
  1177. return retval;
  1178. buffer += bytes_remaining;
  1179. addr += bytes_remaining;
  1180. count -= bytes_remaining;
  1181. }
  1182. return retval;
  1183. }
  1184. /** */
  1185. static int stlink_usb_override_target(const char *targetname)
  1186. {
  1187. return !strcmp(targetname, "cortex_m");
  1188. }
  1189. static int stlink_speed(void *handle, int khz, bool query)
  1190. {
  1191. unsigned i;
  1192. int speed_index = -1;
  1193. int speed_diff = INT_MAX;
  1194. struct stlink_usb_handle_s *h = handle;
  1195. /* only supported by stlink/v2 and for firmware >= 22 */
  1196. if (h && (h->version.stlink == 1 || h->version.jtag < 22))
  1197. return khz;
  1198. for (i = 0; i < ARRAY_SIZE(stlink_khz_to_speed_map); i++) {
  1199. if (khz == stlink_khz_to_speed_map[i].speed) {
  1200. speed_index = i;
  1201. break;
  1202. } else {
  1203. int current_diff = khz - stlink_khz_to_speed_map[i].speed;
  1204. /* get abs value for comparison */
  1205. current_diff = (current_diff > 0) ? current_diff : -current_diff;
  1206. if ((current_diff < speed_diff) && khz >= stlink_khz_to_speed_map[i].speed) {
  1207. speed_diff = current_diff;
  1208. speed_index = i;
  1209. }
  1210. }
  1211. }
  1212. bool match = true;
  1213. if (speed_index == -1) {
  1214. /* this will only be here if we cannot match the slow speed.
  1215. * use the slowest speed we support.*/
  1216. speed_index = ARRAY_SIZE(stlink_khz_to_speed_map) - 1;
  1217. match = false;
  1218. } else if (i == ARRAY_SIZE(stlink_khz_to_speed_map))
  1219. match = false;
  1220. if (!match && query) {
  1221. LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
  1222. khz, stlink_khz_to_speed_map[speed_index].speed);
  1223. }
  1224. if (h && !query) {
  1225. int result = stlink_usb_set_swdclk(h, stlink_khz_to_speed_map[speed_index].speed_divisor);
  1226. if (result != ERROR_OK) {
  1227. LOG_ERROR("Unable to set adapter speed");
  1228. return khz;
  1229. }
  1230. }
  1231. return stlink_khz_to_speed_map[speed_index].speed;
  1232. }
  1233. /** */
  1234. static int stlink_usb_close(void *handle)
  1235. {
  1236. struct stlink_usb_handle_s *h = handle;
  1237. if (h && h->fd)
  1238. jtag_libusb_close(h->fd);
  1239. free(h);
  1240. return ERROR_OK;
  1241. }
  1242. /** */
  1243. static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
  1244. {
  1245. int err, retry_count = 1;
  1246. struct stlink_usb_handle_s *h;
  1247. enum stlink_jtag_api_version api;
  1248. LOG_DEBUG("stlink_usb_open");
  1249. h = calloc(1, sizeof(struct stlink_usb_handle_s));
  1250. if (h == 0) {
  1251. LOG_DEBUG("malloc failed");
  1252. return ERROR_FAIL;
  1253. }
  1254. h->transport = param->transport;
  1255. const uint16_t vids[] = { param->vid, 0 };
  1256. const uint16_t pids[] = { param->pid, 0 };
  1257. const char *serial = param->serial;
  1258. LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
  1259. param->transport, param->vid, param->pid,
  1260. param->serial ? param->serial : "");
  1261. /*
  1262. On certain host USB configurations(e.g. MacBook Air)
  1263. STLINKv2 dongle seems to have its FW in a funky state if,
  1264. after plugging it in, you try to use openocd with it more
  1265. then once (by launching and closing openocd). In cases like
  1266. that initial attempt to read the FW info via
  1267. stlink_usb_version will fail and the device has to be reset
  1268. in order to become operational.
  1269. */
  1270. do {
  1271. if (jtag_libusb_open(vids, pids, serial, &h->fd) != ERROR_OK) {
  1272. LOG_ERROR("open failed");
  1273. goto error_open;
  1274. }
  1275. jtag_libusb_set_configuration(h->fd, 0);
  1276. if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
  1277. LOG_DEBUG("claim interface failed");
  1278. goto error_open;
  1279. }
  1280. /* RX EP is common for all versions */
  1281. h->rx_ep = STLINK_RX_EP;
  1282. /* wrap version for first read */
  1283. switch (param->pid) {
  1284. case STLINK_V1_PID:
  1285. h->version.stlink = 1;
  1286. h->tx_ep = STLINK_TX_EP;
  1287. h->trace_ep = STLINK_TRACE_EP;
  1288. break;
  1289. case STLINK_V2_1_PID:
  1290. h->version.stlink = 2;
  1291. h->tx_ep = STLINK_V2_1_TX_EP;
  1292. h->trace_ep = STLINK_V2_1_TRACE_EP;
  1293. break;
  1294. default:
  1295. /* fall through - we assume V2 to be the default version*/
  1296. case STLINK_V2_PID:
  1297. h->version.stlink = 2;
  1298. h->tx_ep = STLINK_TX_EP;
  1299. h->trace_ep = STLINK_TRACE_EP;
  1300. break;
  1301. }
  1302. /* get the device version */
  1303. err = stlink_usb_version(h);
  1304. if (err == ERROR_OK) {
  1305. break;
  1306. } else if (h->version.stlink == 1 ||
  1307. retry_count == 0) {
  1308. LOG_ERROR("read version failed");
  1309. goto error_open;
  1310. } else {
  1311. err = jtag_libusb_release_interface(h->fd, 0);
  1312. if (err != ERROR_OK) {
  1313. LOG_ERROR("release interface failed");
  1314. goto error_open;
  1315. }
  1316. err = jtag_libusb_reset_device(h->fd);
  1317. if (err != ERROR_OK) {
  1318. LOG_ERROR("reset device failed");
  1319. goto error_open;
  1320. }
  1321. jtag_libusb_close(h->fd);
  1322. /*
  1323. Give the device one second to settle down and
  1324. reenumerate.
  1325. */
  1326. usleep(1 * 1000 * 1000);
  1327. retry_count--;
  1328. }
  1329. } while (1);
  1330. /* compare usb vid/pid */
  1331. if ((param->vid != h->vid) || (param->pid != h->pid))
  1332. LOG_INFO("vid/pid are not identical: 0x%04X/0x%04X 0x%04X/0x%04X",
  1333. param->vid, param->pid,
  1334. h->vid, h->pid);
  1335. /* check if mode is supported */
  1336. err = ERROR_OK;
  1337. switch (h->transport) {
  1338. case HL_TRANSPORT_SWD:
  1339. case HL_TRANSPORT_JTAG:
  1340. if (h->version.jtag == 0)
  1341. err = ERROR_FAIL;
  1342. break;
  1343. case HL_TRANSPORT_SWIM:
  1344. if (h->version.swim == 0)
  1345. err = ERROR_FAIL;
  1346. break;
  1347. default:
  1348. err = ERROR_FAIL;
  1349. break;
  1350. }
  1351. if (err != ERROR_OK) {
  1352. LOG_ERROR("mode (transport) not supported by device");
  1353. goto error_open;
  1354. }
  1355. api = h->version.jtag_api_max;
  1356. LOG_INFO("using stlink api v%d", api);
  1357. /* set the used jtag api, this will default to the newest supported version */
  1358. h->jtag_api = api;
  1359. /* initialize the debug hardware */
  1360. err = stlink_usb_init_mode(h, param->connect_under_reset);
  1361. if (err != ERROR_OK) {
  1362. LOG_ERROR("init mode failed (unable to connect to the target)");
  1363. goto error_open;
  1364. }
  1365. /* clock speed only supported by stlink/v2 and for firmware >= 22 */
  1366. if (h->version.stlink >= 2 && h->version.jtag >= 22) {
  1367. LOG_DEBUG("Supported clock speeds are:");
  1368. for (unsigned i = 0; i < ARRAY_SIZE(stlink_khz_to_speed_map); i++)
  1369. LOG_DEBUG("%d kHz", stlink_khz_to_speed_map[i].speed);
  1370. stlink_speed(h, param->initial_interface_speed, false);
  1371. }
  1372. /* get cpuid, so we can determine the max page size
  1373. * start with a safe default */
  1374. h->max_mem_packet = (1 << 10);
  1375. uint8_t buffer[4];
  1376. err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
  1377. if (err == ERROR_OK) {
  1378. uint32_t cpuid = le_to_h_u32(buffer);
  1379. int i = (cpuid >> 4) & 0xf;
  1380. if (i == 4 || i == 3) {
  1381. /* Cortex-M3/M4 has 4096 bytes autoincrement range */
  1382. h->max_mem_packet = (1 << 12);
  1383. }
  1384. }
  1385. LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
  1386. *fd = h;
  1387. return ERROR_OK;
  1388. error_open:
  1389. stlink_usb_close(h);
  1390. return ERROR_FAIL;
  1391. }
  1392. int stlink_config_trace(void *handle, bool enabled, enum tpio_pin_protocol pin_protocol,
  1393. uint32_t port_size, unsigned int *trace_freq)
  1394. {
  1395. struct stlink_usb_handle_s *h = handle;
  1396. if (enabled && (h->jtag_api < 2 || pin_protocol != ASYNC_UART)) {
  1397. LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
  1398. return ERROR_FAIL;
  1399. }
  1400. if (!enabled) {
  1401. stlink_usb_trace_disable(h);
  1402. return ERROR_OK;
  1403. }
  1404. if (*trace_freq > STLINK_TRACE_MAX_HZ) {
  1405. LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
  1406. STLINK_TRACE_MAX_HZ);
  1407. return ERROR_FAIL;
  1408. }
  1409. stlink_usb_trace_disable(h);
  1410. if (!*trace_freq)
  1411. *trace_freq = STLINK_TRACE_MAX_HZ;
  1412. h->trace.source_hz = *trace_freq;
  1413. return stlink_usb_trace_enable(h);
  1414. }
  1415. /** */
  1416. struct hl_layout_api_s stlink_usb_layout_api = {
  1417. /** */
  1418. .open = stlink_usb_open,
  1419. /** */
  1420. .close = stlink_usb_close,
  1421. /** */
  1422. .idcode = stlink_usb_idcode,
  1423. /** */
  1424. .state = stlink_usb_state,
  1425. /** */
  1426. .reset = stlink_usb_reset,
  1427. /** */
  1428. .assert_srst = stlink_usb_assert_srst,
  1429. /** */
  1430. .run = stlink_usb_run,
  1431. /** */
  1432. .halt = stlink_usb_halt,
  1433. /** */
  1434. .step = stlink_usb_step,
  1435. /** */
  1436. .read_regs = stlink_usb_read_regs,
  1437. /** */
  1438. .read_reg = stlink_usb_read_reg,
  1439. /** */
  1440. .write_reg = stlink_usb_write_reg,
  1441. /** */
  1442. .read_mem = stlink_usb_read_mem,
  1443. /** */
  1444. .write_mem = stlink_usb_write_mem,
  1445. /** */
  1446. .write_debug_reg = stlink_usb_write_debug_reg,
  1447. /** */
  1448. .override_target = stlink_usb_override_target,
  1449. /** */
  1450. .speed = stlink_speed,
  1451. /** */
  1452. .config_trace = stlink_config_trace,
  1453. /** */
  1454. .poll_trace = stlink_usb_trace_read,
  1455. };