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.
 
 
 
 
 
 

3669 lines
92 KiB

  1. /***************************************************************************
  2. * SWIM contributions by Ake Rehnman *
  3. * Copyright (C) 2017 Ake Rehnman *
  4. * ake.rehnman(at)gmail.com *
  5. * *
  6. * Copyright (C) 2011-2012 by Mathias Kuester *
  7. * Mathias Kuester <kesmtp@freenet.de> *
  8. * *
  9. * Copyright (C) 2012 by Spencer Oliver *
  10. * spen@spen-soft.co.uk *
  11. * *
  12. * This code is based on https://github.com/texane/stlink *
  13. * *
  14. * This program is free software; you can redistribute it and/or modify *
  15. * it under the terms of the GNU General Public License as published by *
  16. * the Free Software Foundation; either version 2 of the License, or *
  17. * (at your option) any later version. *
  18. * *
  19. * This program is distributed in the hope that it will be useful, *
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  22. * GNU General Public License for more details. *
  23. * *
  24. * You should have received a copy of the GNU General Public License *
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>. *
  26. ***************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. #include "config.h"
  29. #endif
  30. /* project specific includes */
  31. #include <helper/binarybuffer.h>
  32. #include <helper/bits.h>
  33. #include <jtag/interface.h>
  34. #include <jtag/hla/hla_layout.h>
  35. #include <jtag/hla/hla_transport.h>
  36. #include <jtag/hla/hla_interface.h>
  37. #include <target/target.h>
  38. #include <transport/transport.h>
  39. #include <target/cortex_m.h>
  40. #include "libusb_common.h"
  41. #ifdef HAVE_LIBUSB1
  42. #define USE_LIBUSB_ASYNCIO
  43. #endif
  44. #define ENDPOINT_IN 0x80
  45. #define ENDPOINT_OUT 0x00
  46. #define STLINK_WRITE_TIMEOUT 1000
  47. #define STLINK_READ_TIMEOUT 1000
  48. #define STLINK_NULL_EP 0
  49. #define STLINK_RX_EP (1|ENDPOINT_IN)
  50. #define STLINK_TX_EP (2|ENDPOINT_OUT)
  51. #define STLINK_TRACE_EP (3|ENDPOINT_IN)
  52. #define STLINK_V2_1_TX_EP (1|ENDPOINT_OUT)
  53. #define STLINK_V2_1_TRACE_EP (2|ENDPOINT_IN)
  54. #define STLINK_SG_SIZE (31)
  55. #define STLINK_DATA_SIZE (4096)
  56. #define STLINK_CMD_SIZE_V2 (16)
  57. #define STLINK_CMD_SIZE_V1 (10)
  58. #define STLINK_V1_PID (0x3744)
  59. #define STLINK_V2_PID (0x3748)
  60. #define STLINK_V2_1_PID (0x374B)
  61. #define STLINK_V2_1_NO_MSD_PID (0x3752)
  62. #define STLINK_V3_USBLOADER_PID (0x374D)
  63. #define STLINK_V3E_PID (0x374E)
  64. #define STLINK_V3S_PID (0x374F)
  65. #define STLINK_V3_2VCP_PID (0x3753)
  66. /*
  67. * ST-Link/V1, ST-Link/V2 and ST-Link/V2.1 are full-speed USB devices and
  68. * this limits the bulk packet size and the 8bit read/writes to max 64 bytes.
  69. * STLINK-V3 is a high speed USB 2.0 and the limit is 512 bytes from FW V3J6.
  70. */
  71. #define STLINK_MAX_RW8 (64)
  72. #define STLINKV3_MAX_RW8 (512)
  73. /* "WAIT" responses will be retried (with exponential backoff) at
  74. * most this many times before failing to caller.
  75. */
  76. #define MAX_WAIT_RETRIES 8
  77. enum stlink_jtag_api_version {
  78. STLINK_JTAG_API_V1 = 1,
  79. STLINK_JTAG_API_V2,
  80. STLINK_JTAG_API_V3,
  81. };
  82. /** */
  83. struct stlink_usb_version {
  84. /** */
  85. int stlink;
  86. /** */
  87. int jtag;
  88. /** */
  89. int swim;
  90. /** jtag api version supported */
  91. enum stlink_jtag_api_version jtag_api;
  92. /** one bit for each feature supported. See macros STLINK_F_* */
  93. uint32_t flags;
  94. };
  95. /** */
  96. struct stlink_usb_handle_s {
  97. /** */
  98. struct jtag_libusb_device_handle *fd;
  99. /** */
  100. struct libusb_transfer *trans;
  101. /** */
  102. uint8_t rx_ep;
  103. /** */
  104. uint8_t tx_ep;
  105. /** */
  106. uint8_t trace_ep;
  107. /** */
  108. uint8_t cmdbuf[STLINK_SG_SIZE];
  109. /** */
  110. uint8_t cmdidx;
  111. /** */
  112. uint8_t direction;
  113. /** */
  114. uint8_t databuf[STLINK_DATA_SIZE];
  115. /** */
  116. uint32_t max_mem_packet;
  117. /** */
  118. enum hl_transports transport;
  119. /** */
  120. struct stlink_usb_version version;
  121. /** */
  122. uint16_t vid;
  123. /** */
  124. uint16_t pid;
  125. /** */
  126. struct {
  127. /** whether SWO tracing is enabled or not */
  128. bool enabled;
  129. /** trace module source clock */
  130. uint32_t source_hz;
  131. } trace;
  132. /** reconnect is needed next time we try to query the
  133. * status */
  134. bool reconnect_pending;
  135. };
  136. #define STLINK_SWIM_ERR_OK 0x00
  137. #define STLINK_SWIM_BUSY 0x01
  138. #define STLINK_DEBUG_ERR_OK 0x80
  139. #define STLINK_DEBUG_ERR_FAULT 0x81
  140. #define STLINK_SWD_AP_WAIT 0x10
  141. #define STLINK_SWD_AP_FAULT 0x11
  142. #define STLINK_SWD_AP_ERROR 0x12
  143. #define STLINK_SWD_AP_PARITY_ERROR 0x13
  144. #define STLINK_JTAG_GET_IDCODE_ERROR 0x09
  145. #define STLINK_JTAG_WRITE_ERROR 0x0c
  146. #define STLINK_JTAG_WRITE_VERIF_ERROR 0x0d
  147. #define STLINK_SWD_DP_WAIT 0x14
  148. #define STLINK_SWD_DP_FAULT 0x15
  149. #define STLINK_SWD_DP_ERROR 0x16
  150. #define STLINK_SWD_DP_PARITY_ERROR 0x17
  151. #define STLINK_SWD_AP_WDATA_ERROR 0x18
  152. #define STLINK_SWD_AP_STICKY_ERROR 0x19
  153. #define STLINK_SWD_AP_STICKYORUN_ERROR 0x1a
  154. #define STLINK_BAD_AP_ERROR 0x1d
  155. #define STLINK_CORE_RUNNING 0x80
  156. #define STLINK_CORE_HALTED 0x81
  157. #define STLINK_CORE_STAT_UNKNOWN -1
  158. #define STLINK_GET_VERSION 0xF1
  159. #define STLINK_DEBUG_COMMAND 0xF2
  160. #define STLINK_DFU_COMMAND 0xF3
  161. #define STLINK_SWIM_COMMAND 0xF4
  162. #define STLINK_GET_CURRENT_MODE 0xF5
  163. #define STLINK_GET_TARGET_VOLTAGE 0xF7
  164. #define STLINK_DEV_DFU_MODE 0x00
  165. #define STLINK_DEV_MASS_MODE 0x01
  166. #define STLINK_DEV_DEBUG_MODE 0x02
  167. #define STLINK_DEV_SWIM_MODE 0x03
  168. #define STLINK_DEV_BOOTLOADER_MODE 0x04
  169. #define STLINK_DEV_UNKNOWN_MODE -1
  170. #define STLINK_DFU_EXIT 0x07
  171. /*
  172. STLINK_SWIM_ENTER_SEQ
  173. 1.3ms low then 750Hz then 1.5kHz
  174. STLINK_SWIM_GEN_RST
  175. STM8 DM pulls reset pin low 50us
  176. STLINK_SWIM_SPEED
  177. uint8_t (0=low|1=high)
  178. STLINK_SWIM_WRITEMEM
  179. uint16_t length
  180. uint32_t address
  181. STLINK_SWIM_RESET
  182. send syncronization seq (16us low, response 64 clocks low)
  183. */
  184. #define STLINK_SWIM_ENTER 0x00
  185. #define STLINK_SWIM_EXIT 0x01
  186. #define STLINK_SWIM_READ_CAP 0x02
  187. #define STLINK_SWIM_SPEED 0x03
  188. #define STLINK_SWIM_ENTER_SEQ 0x04
  189. #define STLINK_SWIM_GEN_RST 0x05
  190. #define STLINK_SWIM_RESET 0x06
  191. #define STLINK_SWIM_ASSERT_RESET 0x07
  192. #define STLINK_SWIM_DEASSERT_RESET 0x08
  193. #define STLINK_SWIM_READSTATUS 0x09
  194. #define STLINK_SWIM_WRITEMEM 0x0a
  195. #define STLINK_SWIM_READMEM 0x0b
  196. #define STLINK_SWIM_READBUF 0x0c
  197. #define STLINK_DEBUG_GETSTATUS 0x01
  198. #define STLINK_DEBUG_FORCEDEBUG 0x02
  199. #define STLINK_DEBUG_APIV1_RESETSYS 0x03
  200. #define STLINK_DEBUG_APIV1_READALLREGS 0x04
  201. #define STLINK_DEBUG_APIV1_READREG 0x05
  202. #define STLINK_DEBUG_APIV1_WRITEREG 0x06
  203. #define STLINK_DEBUG_READMEM_32BIT 0x07
  204. #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
  205. #define STLINK_DEBUG_RUNCORE 0x09
  206. #define STLINK_DEBUG_STEPCORE 0x0a
  207. #define STLINK_DEBUG_APIV1_SETFP 0x0b
  208. #define STLINK_DEBUG_READMEM_8BIT 0x0c
  209. #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
  210. #define STLINK_DEBUG_APIV1_CLEARFP 0x0e
  211. #define STLINK_DEBUG_APIV1_WRITEDEBUGREG 0x0f
  212. #define STLINK_DEBUG_APIV1_SETWATCHPOINT 0x10
  213. #define STLINK_DEBUG_ENTER_JTAG_RESET 0x00
  214. #define STLINK_DEBUG_ENTER_SWD_NO_RESET 0xa3
  215. #define STLINK_DEBUG_ENTER_JTAG_NO_RESET 0xa4
  216. #define STLINK_DEBUG_APIV1_ENTER 0x20
  217. #define STLINK_DEBUG_EXIT 0x21
  218. #define STLINK_DEBUG_READCOREID 0x22
  219. #define STLINK_DEBUG_APIV2_ENTER 0x30
  220. #define STLINK_DEBUG_APIV2_READ_IDCODES 0x31
  221. #define STLINK_DEBUG_APIV2_RESETSYS 0x32
  222. #define STLINK_DEBUG_APIV2_READREG 0x33
  223. #define STLINK_DEBUG_APIV2_WRITEREG 0x34
  224. #define STLINK_DEBUG_APIV2_WRITEDEBUGREG 0x35
  225. #define STLINK_DEBUG_APIV2_READDEBUGREG 0x36
  226. #define STLINK_DEBUG_APIV2_READALLREGS 0x3A
  227. #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS 0x3B
  228. #define STLINK_DEBUG_APIV2_DRIVE_NRST 0x3C
  229. #define STLINK_DEBUG_APIV2_GETLASTRWSTATUS2 0x3E
  230. #define STLINK_DEBUG_APIV2_START_TRACE_RX 0x40
  231. #define STLINK_DEBUG_APIV2_STOP_TRACE_RX 0x41
  232. #define STLINK_DEBUG_APIV2_GET_TRACE_NB 0x42
  233. #define STLINK_DEBUG_APIV2_SWD_SET_FREQ 0x43
  234. #define STLINK_DEBUG_APIV2_JTAG_SET_FREQ 0x44
  235. #define STLINK_DEBUG_APIV2_READ_DAP_REG 0x45
  236. #define STLINK_DEBUG_APIV2_WRITE_DAP_REG 0x46
  237. #define STLINK_DEBUG_APIV2_READMEM_16BIT 0x47
  238. #define STLINK_DEBUG_APIV2_WRITEMEM_16BIT 0x48
  239. #define STLINK_DEBUG_APIV2_INIT_AP 0x4B
  240. #define STLINK_DEBUG_APIV2_CLOSE_AP_DBG 0x4C
  241. #define STLINK_APIV3_SET_COM_FREQ 0x61
  242. #define STLINK_APIV3_GET_COM_FREQ 0x62
  243. #define STLINK_APIV3_GET_VERSION_EX 0xFB
  244. #define STLINK_DEBUG_APIV2_DRIVE_NRST_LOW 0x00
  245. #define STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH 0x01
  246. #define STLINK_DEBUG_APIV2_DRIVE_NRST_PULSE 0x02
  247. #define STLINK_DEBUG_PORT_ACCESS 0xffff
  248. #define STLINK_TRACE_SIZE 4096
  249. #define STLINK_TRACE_MAX_HZ 2000000
  250. #define STLINK_V3_MAX_FREQ_NB 10
  251. /** */
  252. enum stlink_mode {
  253. STLINK_MODE_UNKNOWN = 0,
  254. STLINK_MODE_DFU,
  255. STLINK_MODE_MASS,
  256. STLINK_MODE_DEBUG_JTAG,
  257. STLINK_MODE_DEBUG_SWD,
  258. STLINK_MODE_DEBUG_SWIM
  259. };
  260. #define REQUEST_SENSE 0x03
  261. #define REQUEST_SENSE_LENGTH 18
  262. /*
  263. * Map the relevant features, quirks and workaround for specific firmware
  264. * version of stlink
  265. */
  266. #define STLINK_F_HAS_TRACE BIT(0)
  267. #define STLINK_F_HAS_SWD_SET_FREQ BIT(1)
  268. #define STLINK_F_HAS_JTAG_SET_FREQ BIT(2)
  269. #define STLINK_F_HAS_MEM_16BIT BIT(3)
  270. #define STLINK_F_HAS_GETLASTRWSTATUS2 BIT(4)
  271. #define STLINK_F_HAS_DAP_REG BIT(5)
  272. #define STLINK_F_QUIRK_JTAG_DP_READ BIT(6)
  273. #define STLINK_F_HAS_AP_INIT BIT(7)
  274. #define STLINK_F_HAS_DPBANKSEL BIT(8)
  275. #define STLINK_F_HAS_RW8_512BYTES BIT(9)
  276. /* aliases */
  277. #define STLINK_F_HAS_TARGET_VOLT STLINK_F_HAS_TRACE
  278. struct speed_map {
  279. int speed;
  280. int speed_divisor;
  281. };
  282. /* SWD clock speed */
  283. static const struct speed_map stlink_khz_to_speed_map_swd[] = {
  284. {4000, 0},
  285. {1800, 1}, /* default */
  286. {1200, 2},
  287. {950, 3},
  288. {480, 7},
  289. {240, 15},
  290. {125, 31},
  291. {100, 40},
  292. {50, 79},
  293. {25, 158},
  294. {15, 265},
  295. {5, 798}
  296. };
  297. /* JTAG clock speed */
  298. static const struct speed_map stlink_khz_to_speed_map_jtag[] = {
  299. {18000, 2},
  300. {9000, 4},
  301. {4500, 8},
  302. {2250, 16},
  303. {1125, 32}, /* default */
  304. {562, 64},
  305. {281, 128},
  306. {140, 256}
  307. };
  308. static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size);
  309. static int stlink_swim_status(void *handle);
  310. void stlink_dump_speed_map(const struct speed_map *map, unsigned int map_size);
  311. static int stlink_get_com_freq(void *handle, bool is_jtag, struct speed_map *map);
  312. static int stlink_speed(void *handle, int khz, bool query);
  313. /** */
  314. static unsigned int stlink_usb_block(void *handle)
  315. {
  316. struct stlink_usb_handle_s *h = handle;
  317. assert(handle != NULL);
  318. if (h->version.flags & STLINK_F_HAS_RW8_512BYTES)
  319. return STLINKV3_MAX_RW8;
  320. else
  321. return STLINK_MAX_RW8;
  322. }
  323. #ifdef USE_LIBUSB_ASYNCIO
  324. static LIBUSB_CALL void sync_transfer_cb(struct libusb_transfer *transfer)
  325. {
  326. int *completed = transfer->user_data;
  327. *completed = 1;
  328. /* caller interprets result and frees transfer */
  329. }
  330. static void sync_transfer_wait_for_completion(struct libusb_transfer *transfer)
  331. {
  332. int r, *completed = transfer->user_data;
  333. /* Assuming a single libusb context exists. There no existing interface into this
  334. * module to pass a libusb context.
  335. */
  336. struct libusb_context *ctx = NULL;
  337. while (!*completed) {
  338. r = libusb_handle_events_completed(ctx, completed);
  339. if (r < 0) {
  340. if (r == LIBUSB_ERROR_INTERRUPTED)
  341. continue;
  342. libusb_cancel_transfer(transfer);
  343. continue;
  344. }
  345. }
  346. }
  347. static int transfer_error_status(const struct libusb_transfer *transfer)
  348. {
  349. int r = 0;
  350. switch (transfer->status) {
  351. case LIBUSB_TRANSFER_COMPLETED:
  352. r = 0;
  353. break;
  354. case LIBUSB_TRANSFER_TIMED_OUT:
  355. r = LIBUSB_ERROR_TIMEOUT;
  356. break;
  357. case LIBUSB_TRANSFER_STALL:
  358. r = LIBUSB_ERROR_PIPE;
  359. break;
  360. case LIBUSB_TRANSFER_OVERFLOW:
  361. r = LIBUSB_ERROR_OVERFLOW;
  362. break;
  363. case LIBUSB_TRANSFER_NO_DEVICE:
  364. r = LIBUSB_ERROR_NO_DEVICE;
  365. break;
  366. case LIBUSB_TRANSFER_ERROR:
  367. case LIBUSB_TRANSFER_CANCELLED:
  368. r = LIBUSB_ERROR_IO;
  369. break;
  370. default:
  371. r = LIBUSB_ERROR_OTHER;
  372. break;
  373. }
  374. return r;
  375. }
  376. struct jtag_xfer {
  377. int ep;
  378. uint8_t *buf;
  379. size_t size;
  380. /* Internal */
  381. int retval;
  382. int completed;
  383. size_t transfer_size;
  384. struct libusb_transfer *transfer;
  385. };
  386. static int jtag_libusb_bulk_transfer_n(
  387. jtag_libusb_device_handle * dev_handle,
  388. struct jtag_xfer *transfers,
  389. size_t n_transfers,
  390. int timeout)
  391. {
  392. int retval = 0;
  393. int returnval = ERROR_OK;
  394. for (size_t i = 0; i < n_transfers; ++i) {
  395. transfers[i].retval = 0;
  396. transfers[i].completed = 0;
  397. transfers[i].transfer_size = 0;
  398. transfers[i].transfer = libusb_alloc_transfer(0);
  399. if (transfers[i].transfer == NULL) {
  400. for (size_t j = 0; j < i; ++j)
  401. libusb_free_transfer(transfers[j].transfer);
  402. LOG_DEBUG("ERROR, failed to alloc usb transfers");
  403. for (size_t k = 0; k < n_transfers; ++k)
  404. transfers[k].retval = LIBUSB_ERROR_NO_MEM;
  405. return ERROR_FAIL;
  406. }
  407. }
  408. for (size_t i = 0; i < n_transfers; ++i) {
  409. libusb_fill_bulk_transfer(
  410. transfers[i].transfer,
  411. dev_handle,
  412. transfers[i].ep, transfers[i].buf, transfers[i].size,
  413. sync_transfer_cb, &transfers[i].completed, timeout);
  414. transfers[i].transfer->type = LIBUSB_TRANSFER_TYPE_BULK;
  415. retval = libusb_submit_transfer(transfers[i].transfer);
  416. if (retval < 0) {
  417. LOG_DEBUG("ERROR, failed to submit transfer %zu, error %d", i, retval);
  418. /* Probably no point continuing to submit transfers once a submission fails.
  419. * As a result, tag all remaining transfers as errors.
  420. */
  421. for (size_t j = i; j < n_transfers; ++j)
  422. transfers[j].retval = retval;
  423. returnval = ERROR_FAIL;
  424. break;
  425. }
  426. }
  427. /* Wait for every submitted USB transfer to complete.
  428. */
  429. for (size_t i = 0; i < n_transfers; ++i) {
  430. if (transfers[i].retval == 0) {
  431. sync_transfer_wait_for_completion(transfers[i].transfer);
  432. retval = transfer_error_status(transfers[i].transfer);
  433. if (retval) {
  434. returnval = ERROR_FAIL;
  435. transfers[i].retval = retval;
  436. LOG_DEBUG("ERROR, transfer %zu failed, error %d", i, retval);
  437. } else {
  438. /* Assuming actual_length is only valid if there is no transfer error.
  439. */
  440. transfers[i].transfer_size = transfers[i].transfer->actual_length;
  441. }
  442. }
  443. libusb_free_transfer(transfers[i].transfer);
  444. transfers[i].transfer = NULL;
  445. }
  446. return returnval;
  447. }
  448. #endif
  449. /** */
  450. static int stlink_usb_xfer_v1_get_status(void *handle)
  451. {
  452. struct stlink_usb_handle_s *h = handle;
  453. int tr, ret;
  454. assert(handle != NULL);
  455. /* read status */
  456. memset(h->cmdbuf, 0, STLINK_SG_SIZE);
  457. ret = jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)h->cmdbuf, 13,
  458. STLINK_READ_TIMEOUT, &tr);
  459. if (ret || tr != 13)
  460. return ERROR_FAIL;
  461. uint32_t t1;
  462. t1 = buf_get_u32(h->cmdbuf, 0, 32);
  463. /* check for USBS */
  464. if (t1 != 0x53425355)
  465. return ERROR_FAIL;
  466. /*
  467. * CSW status:
  468. * 0 success
  469. * 1 command failure
  470. * 2 phase error
  471. */
  472. if (h->cmdbuf[12] != 0)
  473. return ERROR_FAIL;
  474. return ERROR_OK;
  475. }
  476. #ifdef USE_LIBUSB_ASYNCIO
  477. static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
  478. {
  479. struct stlink_usb_handle_s *h = handle;
  480. assert(handle != NULL);
  481. size_t n_transfers = 0;
  482. struct jtag_xfer transfers[2];
  483. memset(transfers, 0, sizeof(transfers));
  484. transfers[0].ep = h->tx_ep;
  485. transfers[0].buf = h->cmdbuf;
  486. transfers[0].size = cmdsize;
  487. ++n_transfers;
  488. if (h->direction == h->tx_ep && size) {
  489. transfers[1].ep = h->tx_ep;
  490. transfers[1].buf = (uint8_t *)buf;
  491. transfers[1].size = size;
  492. ++n_transfers;
  493. } else if (h->direction == h->rx_ep && size) {
  494. transfers[1].ep = h->rx_ep;
  495. transfers[1].buf = (uint8_t *)buf;
  496. transfers[1].size = size;
  497. ++n_transfers;
  498. }
  499. return jtag_libusb_bulk_transfer_n(
  500. h->fd,
  501. transfers,
  502. n_transfers,
  503. STLINK_WRITE_TIMEOUT);
  504. }
  505. #else
  506. static int stlink_usb_xfer_rw(void *handle, int cmdsize, const uint8_t *buf, int size)
  507. {
  508. struct stlink_usb_handle_s *h = handle;
  509. int tr, ret;
  510. assert(handle != NULL);
  511. ret = jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)h->cmdbuf,
  512. cmdsize, STLINK_WRITE_TIMEOUT, &tr);
  513. if (ret || tr != cmdsize)
  514. return ERROR_FAIL;
  515. if (h->direction == h->tx_ep && size) {
  516. ret = jtag_libusb_bulk_write(h->fd, h->tx_ep, (char *)buf,
  517. size, STLINK_WRITE_TIMEOUT, &tr);
  518. if (ret || tr != size) {
  519. LOG_DEBUG("bulk write failed");
  520. return ERROR_FAIL;
  521. }
  522. } else if (h->direction == h->rx_ep && size) {
  523. ret = jtag_libusb_bulk_read(h->fd, h->rx_ep, (char *)buf,
  524. size, STLINK_READ_TIMEOUT, &tr);
  525. if (ret || tr != size) {
  526. LOG_DEBUG("bulk read failed");
  527. return ERROR_FAIL;
  528. }
  529. }
  530. return ERROR_OK;
  531. }
  532. #endif
  533. /** */
  534. static int stlink_usb_xfer_v1_get_sense(void *handle)
  535. {
  536. int res;
  537. struct stlink_usb_handle_s *h = handle;
  538. assert(handle != NULL);
  539. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  540. h->cmdbuf[h->cmdidx++] = REQUEST_SENSE;
  541. h->cmdbuf[h->cmdidx++] = 0;
  542. h->cmdbuf[h->cmdidx++] = 0;
  543. h->cmdbuf[h->cmdidx++] = 0;
  544. h->cmdbuf[h->cmdidx++] = REQUEST_SENSE_LENGTH;
  545. res = stlink_usb_xfer_rw(handle, REQUEST_SENSE_LENGTH, h->databuf, 16);
  546. if (res != ERROR_OK)
  547. return res;
  548. if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK)
  549. return ERROR_FAIL;
  550. return ERROR_OK;
  551. }
  552. /*
  553. transfers block in cmdbuf
  554. <size> indicates number of bytes in the following
  555. data phase.
  556. Ignore the (eventual) error code in the received packet.
  557. */
  558. static int stlink_usb_xfer_noerrcheck(void *handle, const uint8_t *buf, int size)
  559. {
  560. int err, cmdsize = STLINK_CMD_SIZE_V2;
  561. struct stlink_usb_handle_s *h = handle;
  562. assert(handle != NULL);
  563. if (h->version.stlink == 1) {
  564. cmdsize = STLINK_SG_SIZE;
  565. /* put length in bCBWCBLength */
  566. h->cmdbuf[14] = h->cmdidx-15;
  567. }
  568. err = stlink_usb_xfer_rw(handle, cmdsize, buf, size);
  569. if (err != ERROR_OK)
  570. return err;
  571. if (h->version.stlink == 1) {
  572. if (stlink_usb_xfer_v1_get_status(handle) != ERROR_OK) {
  573. /* check csw status */
  574. if (h->cmdbuf[12] == 1) {
  575. LOG_DEBUG("get sense");
  576. if (stlink_usb_xfer_v1_get_sense(handle) != ERROR_OK)
  577. return ERROR_FAIL;
  578. }
  579. return ERROR_FAIL;
  580. }
  581. }
  582. return ERROR_OK;
  583. }
  584. /**
  585. Converts an STLINK status code held in the first byte of a response
  586. to an openocd error, logs any error/wait status as debug output.
  587. */
  588. static int stlink_usb_error_check(void *handle)
  589. {
  590. struct stlink_usb_handle_s *h = handle;
  591. assert(handle != NULL);
  592. if (h->transport == HL_TRANSPORT_SWIM) {
  593. switch (h->databuf[0]) {
  594. case STLINK_SWIM_ERR_OK:
  595. return ERROR_OK;
  596. case STLINK_SWIM_BUSY:
  597. return ERROR_WAIT;
  598. default:
  599. LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
  600. return ERROR_FAIL;
  601. }
  602. }
  603. /* TODO: no error checking yet on api V1 */
  604. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  605. h->databuf[0] = STLINK_DEBUG_ERR_OK;
  606. switch (h->databuf[0]) {
  607. case STLINK_DEBUG_ERR_OK:
  608. return ERROR_OK;
  609. case STLINK_DEBUG_ERR_FAULT:
  610. LOG_DEBUG("SWD fault response (0x%x)", STLINK_DEBUG_ERR_FAULT);
  611. return ERROR_FAIL;
  612. case STLINK_SWD_AP_WAIT:
  613. LOG_DEBUG("wait status SWD_AP_WAIT (0x%x)", STLINK_SWD_AP_WAIT);
  614. return ERROR_WAIT;
  615. case STLINK_SWD_DP_WAIT:
  616. LOG_DEBUG("wait status SWD_DP_WAIT (0x%x)", STLINK_SWD_DP_WAIT);
  617. return ERROR_WAIT;
  618. case STLINK_JTAG_GET_IDCODE_ERROR:
  619. LOG_DEBUG("STLINK_JTAG_GET_IDCODE_ERROR");
  620. return ERROR_FAIL;
  621. case STLINK_JTAG_WRITE_ERROR:
  622. LOG_DEBUG("Write error");
  623. return ERROR_FAIL;
  624. case STLINK_JTAG_WRITE_VERIF_ERROR:
  625. LOG_DEBUG("Write verify error, ignoring");
  626. return ERROR_OK;
  627. case STLINK_SWD_AP_FAULT:
  628. /* git://git.ac6.fr/openocd commit 657e3e885b9ee10
  629. * returns ERROR_OK with the comment:
  630. * Change in error status when reading outside RAM.
  631. * This fix allows CDT plugin to visualize memory.
  632. */
  633. LOG_DEBUG("STLINK_SWD_AP_FAULT");
  634. return ERROR_FAIL;
  635. case STLINK_SWD_AP_ERROR:
  636. LOG_DEBUG("STLINK_SWD_AP_ERROR");
  637. return ERROR_FAIL;
  638. case STLINK_SWD_AP_PARITY_ERROR:
  639. LOG_DEBUG("STLINK_SWD_AP_PARITY_ERROR");
  640. return ERROR_FAIL;
  641. case STLINK_SWD_DP_FAULT:
  642. LOG_DEBUG("STLINK_SWD_DP_FAULT");
  643. return ERROR_FAIL;
  644. case STLINK_SWD_DP_ERROR:
  645. LOG_DEBUG("STLINK_SWD_DP_ERROR");
  646. return ERROR_FAIL;
  647. case STLINK_SWD_DP_PARITY_ERROR:
  648. LOG_DEBUG("STLINK_SWD_DP_PARITY_ERROR");
  649. return ERROR_FAIL;
  650. case STLINK_SWD_AP_WDATA_ERROR:
  651. LOG_DEBUG("STLINK_SWD_AP_WDATA_ERROR");
  652. return ERROR_FAIL;
  653. case STLINK_SWD_AP_STICKY_ERROR:
  654. LOG_DEBUG("STLINK_SWD_AP_STICKY_ERROR");
  655. return ERROR_FAIL;
  656. case STLINK_SWD_AP_STICKYORUN_ERROR:
  657. LOG_DEBUG("STLINK_SWD_AP_STICKYORUN_ERROR");
  658. return ERROR_FAIL;
  659. case STLINK_BAD_AP_ERROR:
  660. LOG_DEBUG("STLINK_BAD_AP_ERROR");
  661. return ERROR_FAIL;
  662. default:
  663. LOG_DEBUG("unknown/unexpected STLINK status code 0x%x", h->databuf[0]);
  664. return ERROR_FAIL;
  665. }
  666. }
  667. /*
  668. * Wrapper around stlink_usb_xfer_noerrcheck()
  669. * to check the error code in the received packet
  670. */
  671. static int stlink_usb_xfer_errcheck(void *handle, const uint8_t *buf, int size)
  672. {
  673. int retval;
  674. assert(size > 0);
  675. retval = stlink_usb_xfer_noerrcheck(handle, buf, size);
  676. if (retval != ERROR_OK)
  677. return retval;
  678. return stlink_usb_error_check(handle);
  679. }
  680. /** Issue an STLINK command via USB transfer, with retries on any wait status responses.
  681. Works for commands where the STLINK_DEBUG status is returned in the first
  682. byte of the response packet. For SWIM a SWIM_READSTATUS is requested instead.
  683. Returns an openocd result code.
  684. */
  685. static int stlink_cmd_allow_retry(void *handle, const uint8_t *buf, int size)
  686. {
  687. int retries = 0;
  688. int res;
  689. struct stlink_usb_handle_s *h = handle;
  690. while (1) {
  691. if ((h->transport != HL_TRANSPORT_SWIM) || !retries) {
  692. res = stlink_usb_xfer_noerrcheck(handle, buf, size);
  693. if (res != ERROR_OK)
  694. return res;
  695. }
  696. if (h->transport == HL_TRANSPORT_SWIM) {
  697. res = stlink_swim_status(handle);
  698. if (res != ERROR_OK)
  699. return res;
  700. }
  701. res = stlink_usb_error_check(handle);
  702. if (res == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  703. useconds_t delay_us = (1<<retries++) * 1000;
  704. LOG_DEBUG("stlink_cmd_allow_retry ERROR_WAIT, retry %d, delaying %u microseconds", retries, delay_us);
  705. usleep(delay_us);
  706. continue;
  707. }
  708. return res;
  709. }
  710. }
  711. /** */
  712. static int stlink_usb_read_trace(void *handle, const uint8_t *buf, int size)
  713. {
  714. struct stlink_usb_handle_s *h = handle;
  715. int tr, ret;
  716. assert(handle != NULL);
  717. assert(h->version.flags & STLINK_F_HAS_TRACE);
  718. ret = jtag_libusb_bulk_read(h->fd, h->trace_ep, (char *)buf, size,
  719. STLINK_READ_TIMEOUT, &tr);
  720. if (ret || tr != size) {
  721. LOG_ERROR("bulk trace read failed");
  722. return ERROR_FAIL;
  723. }
  724. return ERROR_OK;
  725. }
  726. /*
  727. this function writes transfer length in
  728. the right place in the cb
  729. */
  730. static void stlink_usb_set_cbw_transfer_datalength(void *handle, uint32_t size)
  731. {
  732. struct stlink_usb_handle_s *h = handle;
  733. buf_set_u32(h->cmdbuf+8, 0, 32, size);
  734. }
  735. static void stlink_usb_xfer_v1_create_cmd(void *handle, uint8_t direction, uint32_t size)
  736. {
  737. struct stlink_usb_handle_s *h = handle;
  738. /* fill the send buffer */
  739. strcpy((char *)h->cmdbuf, "USBC");
  740. h->cmdidx += 4;
  741. /* csw tag not used */
  742. buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, 0);
  743. h->cmdidx += 4;
  744. /* cbw data transfer length (in the following data phase in or out) */
  745. buf_set_u32(h->cmdbuf+h->cmdidx, 0, 32, size);
  746. h->cmdidx += 4;
  747. /* cbw flags */
  748. h->cmdbuf[h->cmdidx++] = (direction == h->rx_ep ? ENDPOINT_IN : ENDPOINT_OUT);
  749. h->cmdbuf[h->cmdidx++] = 0; /* lun */
  750. /* cdb clength (is filled in at xfer) */
  751. h->cmdbuf[h->cmdidx++] = 0;
  752. }
  753. /** */
  754. static void stlink_usb_init_buffer(void *handle, uint8_t direction, uint32_t size)
  755. {
  756. struct stlink_usb_handle_s *h = handle;
  757. h->direction = direction;
  758. h->cmdidx = 0;
  759. memset(h->cmdbuf, 0, STLINK_SG_SIZE);
  760. memset(h->databuf, 0, STLINK_DATA_SIZE);
  761. if (h->version.stlink == 1)
  762. stlink_usb_xfer_v1_create_cmd(handle, direction, size);
  763. }
  764. /** */
  765. static int stlink_usb_version(void *handle)
  766. {
  767. int res;
  768. uint32_t flags;
  769. uint16_t version;
  770. uint8_t v, x, y, jtag, swim, msd, bridge = 0;
  771. char v_str[5 * (1 + 3) + 1]; /* VvJjMmBbSs */
  772. char *p;
  773. struct stlink_usb_handle_s *h = handle;
  774. assert(handle != NULL);
  775. stlink_usb_init_buffer(handle, h->rx_ep, 6);
  776. h->cmdbuf[h->cmdidx++] = STLINK_GET_VERSION;
  777. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 6);
  778. if (res != ERROR_OK)
  779. return res;
  780. version = be_to_h_u16(h->databuf);
  781. v = (version >> 12) & 0x0f;
  782. x = (version >> 6) & 0x3f;
  783. y = version & 0x3f;
  784. h->vid = le_to_h_u16(h->databuf + 2);
  785. h->pid = le_to_h_u16(h->databuf + 4);
  786. switch (h->pid) {
  787. case STLINK_V2_1_PID:
  788. case STLINK_V2_1_NO_MSD_PID:
  789. if ((x <= 22 && y == 7) || (x >= 25 && y >= 7 && y <= 12)) {
  790. /* MxSy : STM8 V2.1 - SWIM only */
  791. msd = x;
  792. swim = y;
  793. jtag = 0;
  794. } else {
  795. /* JxMy : STM32 V2.1 - JTAG/SWD only */
  796. jtag = x;
  797. msd = y;
  798. swim = 0;
  799. }
  800. break;
  801. default:
  802. jtag = x;
  803. swim = y;
  804. msd = 0;
  805. break;
  806. }
  807. /* STLINK-V3 requires a specific command */
  808. if (v == 3 && x == 0 && y == 0) {
  809. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  810. h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_VERSION_EX;
  811. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 12);
  812. if (res != ERROR_OK)
  813. return res;
  814. v = h->databuf[0];
  815. swim = h->databuf[1];
  816. jtag = h->databuf[2];
  817. msd = h->databuf[3];
  818. bridge = h->databuf[4];
  819. h->vid = le_to_h_u16(h->databuf + 8);
  820. h->pid = le_to_h_u16(h->databuf + 10);
  821. }
  822. h->version.stlink = v;
  823. h->version.jtag = jtag;
  824. h->version.swim = swim;
  825. flags = 0;
  826. switch (h->version.stlink) {
  827. case 1:
  828. /* ST-LINK/V1 from J11 switch to api-v2 (and support SWD) */
  829. if (h->version.jtag >= 11)
  830. h->version.jtag_api = STLINK_JTAG_API_V2;
  831. else
  832. h->version.jtag_api = STLINK_JTAG_API_V1;
  833. break;
  834. case 2:
  835. /* all ST-LINK/V2 and ST-Link/V2.1 use api-v2 */
  836. h->version.jtag_api = STLINK_JTAG_API_V2;
  837. /* API for trace from J13 */
  838. /* API for target voltage from J13 */
  839. if (h->version.jtag >= 13)
  840. flags |= STLINK_F_HAS_TRACE;
  841. /* preferred API to get last R/W status from J15 */
  842. if (h->version.jtag >= 15)
  843. flags |= STLINK_F_HAS_GETLASTRWSTATUS2;
  844. /* API to set SWD frequency from J22 */
  845. if (h->version.jtag >= 22)
  846. flags |= STLINK_F_HAS_SWD_SET_FREQ;
  847. /* API to set JTAG frequency from J24 */
  848. /* API to access DAP registers from J24 */
  849. if (h->version.jtag >= 24) {
  850. flags |= STLINK_F_HAS_JTAG_SET_FREQ;
  851. flags |= STLINK_F_HAS_DAP_REG;
  852. }
  853. /* Quirk for read DP in JTAG mode (V2 only) from J24, fixed in J32 */
  854. if (h->version.jtag >= 24 && h->version.jtag < 32)
  855. flags |= STLINK_F_QUIRK_JTAG_DP_READ;
  856. /* API to read/write memory at 16 bit from J26 */
  857. if (h->version.jtag >= 26)
  858. flags |= STLINK_F_HAS_MEM_16BIT;
  859. /* API required to init AP before any AP access from J28 */
  860. if (h->version.jtag >= 28)
  861. flags |= STLINK_F_HAS_AP_INIT;
  862. /* Banked regs (DPv1 & DPv2) support from V2J32 */
  863. if (h->version.jtag >= 32)
  864. flags |= STLINK_F_HAS_DPBANKSEL;
  865. break;
  866. case 3:
  867. /* all STLINK-V3 use api-v3 */
  868. h->version.jtag_api = STLINK_JTAG_API_V3;
  869. /* STLINK-V3 is a superset of ST-LINK/V2 */
  870. /* API for trace */
  871. /* API for target voltage */
  872. flags |= STLINK_F_HAS_TRACE;
  873. /* preferred API to get last R/W status */
  874. flags |= STLINK_F_HAS_GETLASTRWSTATUS2;
  875. /* API to access DAP registers */
  876. flags |= STLINK_F_HAS_DAP_REG;
  877. /* API to read/write memory at 16 bit */
  878. flags |= STLINK_F_HAS_MEM_16BIT;
  879. /* API required to init AP before any AP access */
  880. flags |= STLINK_F_HAS_AP_INIT;
  881. /* Banked regs (DPv1 & DPv2) support from V3J2 */
  882. if (h->version.jtag >= 2)
  883. flags |= STLINK_F_HAS_DPBANKSEL;
  884. /* 8bit read/write max packet size 512 bytes from V3J6 */
  885. if (h->version.jtag >= 6)
  886. flags |= STLINK_F_HAS_RW8_512BYTES;
  887. break;
  888. default:
  889. break;
  890. }
  891. h->version.flags = flags;
  892. p = v_str;
  893. p += sprintf(p, "V%d", v);
  894. if (jtag || !msd)
  895. p += sprintf(p, "J%d", jtag);
  896. if (msd)
  897. p += sprintf(p, "M%d", msd);
  898. if (bridge)
  899. p += sprintf(p, "B%d", bridge);
  900. if (swim || !msd)
  901. sprintf(p, "S%d", swim);
  902. LOG_INFO("STLINK %s (API v%d) VID:PID %04X:%04X",
  903. v_str,
  904. h->version.jtag_api,
  905. h->vid,
  906. h->pid);
  907. return ERROR_OK;
  908. }
  909. static int stlink_usb_check_voltage(void *handle, float *target_voltage)
  910. {
  911. struct stlink_usb_handle_s *h = handle;
  912. uint32_t adc_results[2];
  913. /* no error message, simply quit with error */
  914. if (!(h->version.flags & STLINK_F_HAS_TARGET_VOLT))
  915. return ERROR_COMMAND_NOTFOUND;
  916. stlink_usb_init_buffer(handle, h->rx_ep, 8);
  917. h->cmdbuf[h->cmdidx++] = STLINK_GET_TARGET_VOLTAGE;
  918. int result = stlink_usb_xfer_noerrcheck(handle, h->databuf, 8);
  919. if (result != ERROR_OK)
  920. return result;
  921. /* convert result */
  922. adc_results[0] = le_to_h_u32(h->databuf);
  923. adc_results[1] = le_to_h_u32(h->databuf + 4);
  924. *target_voltage = 0;
  925. if (adc_results[0])
  926. *target_voltage = 2 * ((float)adc_results[1]) * (float)(1.2 / adc_results[0]);
  927. LOG_INFO("Target voltage: %f", (double)*target_voltage);
  928. return ERROR_OK;
  929. }
  930. static int stlink_usb_set_swdclk(void *handle, uint16_t clk_divisor)
  931. {
  932. struct stlink_usb_handle_s *h = handle;
  933. assert(handle != NULL);
  934. if (!(h->version.flags & STLINK_F_HAS_SWD_SET_FREQ))
  935. return ERROR_COMMAND_NOTFOUND;
  936. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  937. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  938. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_SWD_SET_FREQ;
  939. h_u16_to_le(h->cmdbuf+h->cmdidx, clk_divisor);
  940. h->cmdidx += 2;
  941. int result = stlink_cmd_allow_retry(handle, h->databuf, 2);
  942. if (result != ERROR_OK)
  943. return result;
  944. return ERROR_OK;
  945. }
  946. static int stlink_usb_set_jtagclk(void *handle, uint16_t clk_divisor)
  947. {
  948. struct stlink_usb_handle_s *h = handle;
  949. assert(handle != NULL);
  950. if (!(h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ))
  951. return ERROR_COMMAND_NOTFOUND;
  952. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  953. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  954. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_JTAG_SET_FREQ;
  955. h_u16_to_le(h->cmdbuf+h->cmdidx, clk_divisor);
  956. h->cmdidx += 2;
  957. int result = stlink_cmd_allow_retry(handle, h->databuf, 2);
  958. if (result != ERROR_OK)
  959. return result;
  960. return ERROR_OK;
  961. }
  962. /** */
  963. static int stlink_usb_current_mode(void *handle, uint8_t *mode)
  964. {
  965. int res;
  966. struct stlink_usb_handle_s *h = handle;
  967. assert(handle != NULL);
  968. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  969. h->cmdbuf[h->cmdidx++] = STLINK_GET_CURRENT_MODE;
  970. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
  971. if (res != ERROR_OK)
  972. return res;
  973. *mode = h->databuf[0];
  974. return ERROR_OK;
  975. }
  976. /** */
  977. static int stlink_usb_mode_enter(void *handle, enum stlink_mode type)
  978. {
  979. int rx_size = 0;
  980. struct stlink_usb_handle_s *h = handle;
  981. assert(handle != NULL);
  982. /* on api V2 we are able the read the latest command
  983. * status
  984. * TODO: we need the test on api V1 too
  985. */
  986. if (h->version.jtag_api != STLINK_JTAG_API_V1)
  987. rx_size = 2;
  988. stlink_usb_init_buffer(handle, h->rx_ep, rx_size);
  989. switch (type) {
  990. case STLINK_MODE_DEBUG_JTAG:
  991. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  992. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  993. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
  994. else
  995. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
  996. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_JTAG_NO_RESET;
  997. break;
  998. case STLINK_MODE_DEBUG_SWD:
  999. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1000. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  1001. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_ENTER;
  1002. else
  1003. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_ENTER;
  1004. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_ENTER_SWD_NO_RESET;
  1005. break;
  1006. case STLINK_MODE_DEBUG_SWIM:
  1007. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1008. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER;
  1009. /* no answer for this function... */
  1010. rx_size = 0;
  1011. break;
  1012. case STLINK_MODE_DFU:
  1013. case STLINK_MODE_MASS:
  1014. default:
  1015. return ERROR_FAIL;
  1016. }
  1017. return stlink_cmd_allow_retry(handle, h->databuf, rx_size);
  1018. }
  1019. /** */
  1020. static int stlink_usb_mode_leave(void *handle, enum stlink_mode type)
  1021. {
  1022. int res;
  1023. struct stlink_usb_handle_s *h = handle;
  1024. assert(handle != NULL);
  1025. stlink_usb_init_buffer(handle, STLINK_NULL_EP, 0);
  1026. switch (type) {
  1027. case STLINK_MODE_DEBUG_JTAG:
  1028. case STLINK_MODE_DEBUG_SWD:
  1029. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1030. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_EXIT;
  1031. break;
  1032. case STLINK_MODE_DEBUG_SWIM:
  1033. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1034. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_EXIT;
  1035. break;
  1036. case STLINK_MODE_DFU:
  1037. h->cmdbuf[h->cmdidx++] = STLINK_DFU_COMMAND;
  1038. h->cmdbuf[h->cmdidx++] = STLINK_DFU_EXIT;
  1039. break;
  1040. case STLINK_MODE_MASS:
  1041. default:
  1042. return ERROR_FAIL;
  1043. }
  1044. res = stlink_usb_xfer_noerrcheck(handle, 0, 0);
  1045. if (res != ERROR_OK)
  1046. return res;
  1047. return ERROR_OK;
  1048. }
  1049. static int stlink_usb_assert_srst(void *handle, int srst);
  1050. static enum stlink_mode stlink_get_mode(enum hl_transports t)
  1051. {
  1052. switch (t) {
  1053. case HL_TRANSPORT_SWD:
  1054. return STLINK_MODE_DEBUG_SWD;
  1055. case HL_TRANSPORT_JTAG:
  1056. return STLINK_MODE_DEBUG_JTAG;
  1057. case HL_TRANSPORT_SWIM:
  1058. return STLINK_MODE_DEBUG_SWIM;
  1059. default:
  1060. return STLINK_MODE_UNKNOWN;
  1061. }
  1062. }
  1063. /** */
  1064. static int stlink_usb_init_mode(void *handle, bool connect_under_reset, int initial_interface_speed)
  1065. {
  1066. int res;
  1067. uint8_t mode;
  1068. enum stlink_mode emode;
  1069. struct stlink_usb_handle_s *h = handle;
  1070. assert(handle != NULL);
  1071. res = stlink_usb_current_mode(handle, &mode);
  1072. if (res != ERROR_OK)
  1073. return res;
  1074. LOG_DEBUG("MODE: 0x%02X", mode);
  1075. /* try to exit current mode */
  1076. switch (mode) {
  1077. case STLINK_DEV_DFU_MODE:
  1078. emode = STLINK_MODE_DFU;
  1079. break;
  1080. case STLINK_DEV_DEBUG_MODE:
  1081. emode = STLINK_MODE_DEBUG_SWD;
  1082. break;
  1083. case STLINK_DEV_SWIM_MODE:
  1084. emode = STLINK_MODE_DEBUG_SWIM;
  1085. break;
  1086. case STLINK_DEV_BOOTLOADER_MODE:
  1087. case STLINK_DEV_MASS_MODE:
  1088. default:
  1089. emode = STLINK_MODE_UNKNOWN;
  1090. break;
  1091. }
  1092. if (emode != STLINK_MODE_UNKNOWN) {
  1093. res = stlink_usb_mode_leave(handle, emode);
  1094. if (res != ERROR_OK)
  1095. return res;
  1096. }
  1097. res = stlink_usb_current_mode(handle, &mode);
  1098. if (res != ERROR_OK)
  1099. return res;
  1100. /* we check the target voltage here as an aid to debugging connection problems.
  1101. * the stlink requires the target Vdd to be connected for reliable debugging.
  1102. * this cmd is supported in all modes except DFU
  1103. */
  1104. if (mode != STLINK_DEV_DFU_MODE) {
  1105. float target_voltage;
  1106. /* check target voltage (if supported) */
  1107. res = stlink_usb_check_voltage(h, &target_voltage);
  1108. if (res != ERROR_OK) {
  1109. if (res != ERROR_COMMAND_NOTFOUND)
  1110. LOG_ERROR("voltage check failed");
  1111. /* attempt to continue as it is not a catastrophic failure */
  1112. } else {
  1113. /* check for a sensible target voltage, operating range is 1.65-5.5v
  1114. * according to datasheet */
  1115. if (target_voltage < 1.5)
  1116. LOG_ERROR("target voltage may be too low for reliable debugging");
  1117. }
  1118. }
  1119. LOG_DEBUG("MODE: 0x%02X", mode);
  1120. /* set selected mode */
  1121. emode = stlink_get_mode(h->transport);
  1122. if (emode == STLINK_MODE_UNKNOWN) {
  1123. LOG_ERROR("selected mode (transport) not supported");
  1124. return ERROR_FAIL;
  1125. }
  1126. /* set the speed before entering the mode, as the chip discovery phase should be done at this speed too */
  1127. if (h->transport == HL_TRANSPORT_JTAG) {
  1128. if (h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ) {
  1129. stlink_dump_speed_map(stlink_khz_to_speed_map_jtag, ARRAY_SIZE(stlink_khz_to_speed_map_jtag));
  1130. stlink_speed(h, initial_interface_speed, false);
  1131. }
  1132. } else if (h->transport == HL_TRANSPORT_SWD) {
  1133. if (h->version.flags & STLINK_F_HAS_SWD_SET_FREQ) {
  1134. stlink_dump_speed_map(stlink_khz_to_speed_map_swd, ARRAY_SIZE(stlink_khz_to_speed_map_swd));
  1135. stlink_speed(h, initial_interface_speed, false);
  1136. }
  1137. }
  1138. if (h->version.jtag_api == STLINK_JTAG_API_V3) {
  1139. struct speed_map map[STLINK_V3_MAX_FREQ_NB];
  1140. stlink_get_com_freq(h, (h->transport == HL_TRANSPORT_JTAG), map);
  1141. stlink_dump_speed_map(map, ARRAY_SIZE(map));
  1142. stlink_speed(h, initial_interface_speed, false);
  1143. }
  1144. /* preliminary SRST assert:
  1145. * We want SRST is asserted before activating debug signals (mode_enter).
  1146. * As the required mode has not been set, the adapter may not know what pin to use.
  1147. * Tested firmware STLINK v2 JTAG v29 API v2 SWIM v0 uses T_NRST pin by default
  1148. * Tested firmware STLINK v2 JTAG v27 API v2 SWIM v6 uses T_NRST pin by default
  1149. * after power on, SWIM_RST stays unchanged */
  1150. if (connect_under_reset && emode != STLINK_MODE_DEBUG_SWIM)
  1151. stlink_usb_assert_srst(handle, 0);
  1152. /* do not check the return status here, we will
  1153. proceed and enter the desired mode below
  1154. and try asserting srst again. */
  1155. res = stlink_usb_mode_enter(handle, emode);
  1156. if (res != ERROR_OK)
  1157. return res;
  1158. /* assert SRST again: a little bit late but now the adapter knows for sure what pin to use */
  1159. if (connect_under_reset) {
  1160. res = stlink_usb_assert_srst(handle, 0);
  1161. if (res != ERROR_OK)
  1162. return res;
  1163. }
  1164. res = stlink_usb_current_mode(handle, &mode);
  1165. if (res != ERROR_OK)
  1166. return res;
  1167. LOG_DEBUG("MODE: 0x%02X", mode);
  1168. return ERROR_OK;
  1169. }
  1170. /* request status from last swim request */
  1171. static int stlink_swim_status(void *handle)
  1172. {
  1173. struct stlink_usb_handle_s *h = handle;
  1174. int res;
  1175. stlink_usb_init_buffer(handle, h->rx_ep, 4);
  1176. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1177. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READSTATUS;
  1178. /* error is checked by the caller */
  1179. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
  1180. if (res != ERROR_OK)
  1181. return res;
  1182. return ERROR_OK;
  1183. }
  1184. /*
  1185. the purpose of this function is unknown...
  1186. capabilites? anyway for swim v6 it returns
  1187. 0001020600000000
  1188. */
  1189. __attribute__((unused))
  1190. static int stlink_swim_cap(void *handle, uint8_t *cap)
  1191. {
  1192. struct stlink_usb_handle_s *h = handle;
  1193. int res;
  1194. stlink_usb_init_buffer(handle, h->rx_ep, 8);
  1195. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1196. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READ_CAP;
  1197. h->cmdbuf[h->cmdidx++] = 0x01;
  1198. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 8);
  1199. if (res != ERROR_OK)
  1200. return res;
  1201. memcpy(cap, h->databuf, 8);
  1202. return ERROR_OK;
  1203. }
  1204. /* debug dongle assert/deassert sreset line */
  1205. static int stlink_swim_assert_reset(void *handle, int reset)
  1206. {
  1207. struct stlink_usb_handle_s *h = handle;
  1208. int res;
  1209. stlink_usb_init_buffer(handle, h->rx_ep, 0);
  1210. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1211. if (!reset)
  1212. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ASSERT_RESET;
  1213. else
  1214. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_DEASSERT_RESET;
  1215. res = stlink_cmd_allow_retry(handle, h->databuf, 0);
  1216. if (res != ERROR_OK)
  1217. return res;
  1218. return ERROR_OK;
  1219. }
  1220. /*
  1221. send swim enter seq
  1222. 1.3ms low then 750Hz then 1.5kHz
  1223. */
  1224. static int stlink_swim_enter(void *handle)
  1225. {
  1226. struct stlink_usb_handle_s *h = handle;
  1227. int res;
  1228. stlink_usb_init_buffer(handle, h->rx_ep, 0);
  1229. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1230. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_ENTER_SEQ;
  1231. res = stlink_cmd_allow_retry(handle, h->databuf, 0);
  1232. if (res != ERROR_OK)
  1233. return res;
  1234. return ERROR_OK;
  1235. }
  1236. /* switch high/low speed swim */
  1237. static int stlink_swim_speed(void *handle, int speed)
  1238. {
  1239. struct stlink_usb_handle_s *h = handle;
  1240. int res;
  1241. stlink_usb_init_buffer(handle, h->rx_ep, 0);
  1242. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1243. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_SPEED;
  1244. if (speed)
  1245. h->cmdbuf[h->cmdidx++] = 1;
  1246. else
  1247. h->cmdbuf[h->cmdidx++] = 0;
  1248. res = stlink_cmd_allow_retry(handle, h->databuf, 0);
  1249. if (res != ERROR_OK)
  1250. return res;
  1251. return ERROR_OK;
  1252. }
  1253. /*
  1254. initiate srst from swim.
  1255. nrst is pulled low for 50us.
  1256. */
  1257. static int stlink_swim_generate_rst(void *handle)
  1258. {
  1259. struct stlink_usb_handle_s *h = handle;
  1260. int res;
  1261. stlink_usb_init_buffer(handle, h->rx_ep, 0);
  1262. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1263. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_GEN_RST;
  1264. res = stlink_cmd_allow_retry(handle, h->databuf, 0);
  1265. if (res != ERROR_OK)
  1266. return res;
  1267. return ERROR_OK;
  1268. }
  1269. /*
  1270. send resyncronize sequence
  1271. swim is pulled low for 16us
  1272. reply is 64 clks low
  1273. */
  1274. static int stlink_swim_resync(void *handle)
  1275. {
  1276. struct stlink_usb_handle_s *h = handle;
  1277. int res;
  1278. stlink_usb_init_buffer(handle, h->rx_ep, 0);
  1279. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1280. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_RESET;
  1281. res = stlink_cmd_allow_retry(handle, h->databuf, 0);
  1282. if (res != ERROR_OK)
  1283. return res;
  1284. return ERROR_OK;
  1285. }
  1286. static int stlink_swim_writebytes(void *handle, uint32_t addr, uint32_t len, const uint8_t *data)
  1287. {
  1288. struct stlink_usb_handle_s *h = handle;
  1289. int res;
  1290. unsigned int i;
  1291. unsigned int datalen = 0;
  1292. int cmdsize = STLINK_CMD_SIZE_V2;
  1293. if (len > STLINK_DATA_SIZE)
  1294. return ERROR_FAIL;
  1295. if (h->version.stlink == 1)
  1296. cmdsize = STLINK_SG_SIZE;
  1297. stlink_usb_init_buffer(handle, h->tx_ep, 0);
  1298. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1299. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_WRITEMEM;
  1300. h_u16_to_be(h->cmdbuf+h->cmdidx, len);
  1301. h->cmdidx += 2;
  1302. h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
  1303. h->cmdidx += 4;
  1304. for (i = 0; i < len; i++) {
  1305. if (h->cmdidx == cmdsize)
  1306. h->databuf[datalen++] = *(data++);
  1307. else
  1308. h->cmdbuf[h->cmdidx++] = *(data++);
  1309. }
  1310. if (h->version.stlink == 1)
  1311. stlink_usb_set_cbw_transfer_datalength(handle, datalen);
  1312. res = stlink_cmd_allow_retry(handle, h->databuf, datalen);
  1313. if (res != ERROR_OK)
  1314. return res;
  1315. return ERROR_OK;
  1316. }
  1317. static int stlink_swim_readbytes(void *handle, uint32_t addr, uint32_t len, uint8_t *data)
  1318. {
  1319. struct stlink_usb_handle_s *h = handle;
  1320. int res;
  1321. if (len > STLINK_DATA_SIZE)
  1322. return ERROR_FAIL;
  1323. stlink_usb_init_buffer(handle, h->rx_ep, 0);
  1324. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1325. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READMEM;
  1326. h_u16_to_be(h->cmdbuf+h->cmdidx, len);
  1327. h->cmdidx += 2;
  1328. h_u32_to_be(h->cmdbuf+h->cmdidx, addr);
  1329. h->cmdidx += 4;
  1330. res = stlink_cmd_allow_retry(handle, h->databuf, 0);
  1331. if (res != ERROR_OK)
  1332. return res;
  1333. stlink_usb_init_buffer(handle, h->rx_ep, len);
  1334. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_COMMAND;
  1335. h->cmdbuf[h->cmdidx++] = STLINK_SWIM_READBUF;
  1336. res = stlink_usb_xfer_noerrcheck(handle, data, len);
  1337. if (res != ERROR_OK)
  1338. return res;
  1339. return ERROR_OK;
  1340. }
  1341. /** */
  1342. static int stlink_usb_idcode(void *handle, uint32_t *idcode)
  1343. {
  1344. int res, offset;
  1345. struct stlink_usb_handle_s *h = handle;
  1346. assert(handle != NULL);
  1347. /* there is no swim read core id cmd */
  1348. if (h->transport == HL_TRANSPORT_SWIM) {
  1349. *idcode = 0;
  1350. return ERROR_OK;
  1351. }
  1352. stlink_usb_init_buffer(handle, h->rx_ep, 12);
  1353. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1354. if (h->version.jtag_api == STLINK_JTAG_API_V1) {
  1355. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READCOREID;
  1356. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
  1357. offset = 0;
  1358. } else {
  1359. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READ_IDCODES;
  1360. res = stlink_usb_xfer_errcheck(handle, h->databuf, 12);
  1361. offset = 4;
  1362. }
  1363. if (res != ERROR_OK)
  1364. return res;
  1365. *idcode = le_to_h_u32(h->databuf + offset);
  1366. LOG_DEBUG("IDCODE: 0x%08" PRIX32, *idcode);
  1367. return ERROR_OK;
  1368. }
  1369. static int stlink_usb_v2_read_debug_reg(void *handle, uint32_t addr, uint32_t *val)
  1370. {
  1371. struct stlink_usb_handle_s *h = handle;
  1372. int res;
  1373. assert(handle != NULL);
  1374. stlink_usb_init_buffer(handle, h->rx_ep, 8);
  1375. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1376. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READDEBUGREG;
  1377. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1378. h->cmdidx += 4;
  1379. res = stlink_cmd_allow_retry(handle, h->databuf, 8);
  1380. if (res != ERROR_OK)
  1381. return res;
  1382. *val = le_to_h_u32(h->databuf + 4);
  1383. return ERROR_OK;
  1384. }
  1385. static int stlink_usb_write_debug_reg(void *handle, uint32_t addr, uint32_t val)
  1386. {
  1387. struct stlink_usb_handle_s *h = handle;
  1388. assert(handle != NULL);
  1389. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1390. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1391. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  1392. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEDEBUGREG;
  1393. else
  1394. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEDEBUGREG;
  1395. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1396. h->cmdidx += 4;
  1397. h_u32_to_le(h->cmdbuf+h->cmdidx, val);
  1398. h->cmdidx += 4;
  1399. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  1400. }
  1401. /** */
  1402. static int stlink_usb_trace_read(void *handle, uint8_t *buf, size_t *size)
  1403. {
  1404. struct stlink_usb_handle_s *h = handle;
  1405. assert(handle != NULL);
  1406. if (h->trace.enabled && (h->version.flags & STLINK_F_HAS_TRACE)) {
  1407. int res;
  1408. stlink_usb_init_buffer(handle, h->rx_ep, 10);
  1409. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1410. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GET_TRACE_NB;
  1411. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
  1412. if (res != ERROR_OK)
  1413. return res;
  1414. size_t bytes_avail = le_to_h_u16(h->databuf);
  1415. *size = bytes_avail < *size ? bytes_avail : *size - 1;
  1416. if (*size > 0) {
  1417. res = stlink_usb_read_trace(handle, buf, *size);
  1418. if (res != ERROR_OK)
  1419. return res;
  1420. return ERROR_OK;
  1421. }
  1422. }
  1423. *size = 0;
  1424. return ERROR_OK;
  1425. }
  1426. static enum target_state stlink_usb_v2_get_status(void *handle)
  1427. {
  1428. int result;
  1429. uint32_t status;
  1430. result = stlink_usb_v2_read_debug_reg(handle, DCB_DHCSR, &status);
  1431. if (result != ERROR_OK)
  1432. return TARGET_UNKNOWN;
  1433. if (status & S_HALT)
  1434. return TARGET_HALTED;
  1435. else if (status & S_RESET_ST)
  1436. return TARGET_RESET;
  1437. return TARGET_RUNNING;
  1438. }
  1439. /** */
  1440. static enum target_state stlink_usb_state(void *handle)
  1441. {
  1442. int res;
  1443. struct stlink_usb_handle_s *h = handle;
  1444. assert(handle != NULL);
  1445. if (h->transport == HL_TRANSPORT_SWIM) {
  1446. res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
  1447. if (res != ERROR_OK)
  1448. return TARGET_UNKNOWN;
  1449. res = stlink_swim_resync(handle);
  1450. if (res != ERROR_OK)
  1451. return TARGET_UNKNOWN;
  1452. return ERROR_OK;
  1453. }
  1454. if (h->reconnect_pending) {
  1455. LOG_INFO("Previous state query failed, trying to reconnect");
  1456. res = stlink_usb_mode_enter(handle, stlink_get_mode(h->transport));
  1457. if (res != ERROR_OK)
  1458. return TARGET_UNKNOWN;
  1459. h->reconnect_pending = false;
  1460. }
  1461. if (h->version.jtag_api != STLINK_JTAG_API_V1) {
  1462. res = stlink_usb_v2_get_status(handle);
  1463. if (res == TARGET_UNKNOWN)
  1464. h->reconnect_pending = true;
  1465. return res;
  1466. }
  1467. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1468. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1469. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_GETSTATUS;
  1470. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 2);
  1471. if (res != ERROR_OK)
  1472. return TARGET_UNKNOWN;
  1473. if (h->databuf[0] == STLINK_CORE_RUNNING)
  1474. return TARGET_RUNNING;
  1475. if (h->databuf[0] == STLINK_CORE_HALTED)
  1476. return TARGET_HALTED;
  1477. h->reconnect_pending = true;
  1478. return TARGET_UNKNOWN;
  1479. }
  1480. static int stlink_usb_assert_srst(void *handle, int srst)
  1481. {
  1482. struct stlink_usb_handle_s *h = handle;
  1483. assert(handle != NULL);
  1484. if (h->transport == HL_TRANSPORT_SWIM)
  1485. return stlink_swim_assert_reset(handle, srst);
  1486. if (h->version.stlink == 1)
  1487. return ERROR_COMMAND_NOTFOUND;
  1488. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1489. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1490. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_DRIVE_NRST;
  1491. h->cmdbuf[h->cmdidx++] = srst;
  1492. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  1493. }
  1494. /** */
  1495. static void stlink_usb_trace_disable(void *handle)
  1496. {
  1497. int res = ERROR_OK;
  1498. struct stlink_usb_handle_s *h = handle;
  1499. assert(handle != NULL);
  1500. assert(h->version.flags & STLINK_F_HAS_TRACE);
  1501. LOG_DEBUG("Tracing: disable");
  1502. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1503. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1504. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_STOP_TRACE_RX;
  1505. res = stlink_usb_xfer_errcheck(handle, h->databuf, 2);
  1506. if (res == ERROR_OK)
  1507. h->trace.enabled = false;
  1508. }
  1509. /** */
  1510. static int stlink_usb_trace_enable(void *handle)
  1511. {
  1512. int res;
  1513. struct stlink_usb_handle_s *h = handle;
  1514. assert(handle != NULL);
  1515. if (h->version.flags & STLINK_F_HAS_TRACE) {
  1516. stlink_usb_init_buffer(handle, h->rx_ep, 10);
  1517. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1518. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_START_TRACE_RX;
  1519. h_u16_to_le(h->cmdbuf+h->cmdidx, (uint16_t)STLINK_TRACE_SIZE);
  1520. h->cmdidx += 2;
  1521. h_u32_to_le(h->cmdbuf+h->cmdidx, h->trace.source_hz);
  1522. h->cmdidx += 4;
  1523. res = stlink_usb_xfer_errcheck(handle, h->databuf, 2);
  1524. if (res == ERROR_OK) {
  1525. h->trace.enabled = true;
  1526. LOG_DEBUG("Tracing: recording at %" PRIu32 "Hz", h->trace.source_hz);
  1527. }
  1528. } else {
  1529. LOG_ERROR("Tracing is not supported by this version.");
  1530. res = ERROR_FAIL;
  1531. }
  1532. return res;
  1533. }
  1534. /** */
  1535. static int stlink_usb_reset(void *handle)
  1536. {
  1537. struct stlink_usb_handle_s *h = handle;
  1538. int retval;
  1539. assert(handle != NULL);
  1540. if (h->transport == HL_TRANSPORT_SWIM)
  1541. return stlink_swim_generate_rst(handle);
  1542. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1543. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1544. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  1545. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_RESETSYS;
  1546. else
  1547. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_RESETSYS;
  1548. retval = stlink_cmd_allow_retry(handle, h->databuf, 2);
  1549. if (retval != ERROR_OK)
  1550. return retval;
  1551. if (h->trace.enabled) {
  1552. stlink_usb_trace_disable(h);
  1553. return stlink_usb_trace_enable(h);
  1554. }
  1555. return ERROR_OK;
  1556. }
  1557. /** */
  1558. static int stlink_usb_run(void *handle)
  1559. {
  1560. int res;
  1561. struct stlink_usb_handle_s *h = handle;
  1562. assert(handle != NULL);
  1563. if (h->version.jtag_api != STLINK_JTAG_API_V1) {
  1564. res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_DEBUGEN);
  1565. return res;
  1566. }
  1567. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1568. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1569. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_RUNCORE;
  1570. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  1571. }
  1572. /** */
  1573. static int stlink_usb_halt(void *handle)
  1574. {
  1575. int res;
  1576. struct stlink_usb_handle_s *h = handle;
  1577. assert(handle != NULL);
  1578. if (h->version.jtag_api != STLINK_JTAG_API_V1) {
  1579. res = stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
  1580. return res;
  1581. }
  1582. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1583. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1584. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_FORCEDEBUG;
  1585. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  1586. }
  1587. /** */
  1588. static int stlink_usb_step(void *handle)
  1589. {
  1590. struct stlink_usb_handle_s *h = handle;
  1591. assert(handle != NULL);
  1592. if (h->version.jtag_api != STLINK_JTAG_API_V1) {
  1593. /* TODO: this emulates the v1 api, it should really use a similar auto mask isr
  1594. * that the Cortex-M3 currently does. */
  1595. stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_MASKINTS|C_DEBUGEN);
  1596. stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_STEP|C_MASKINTS|C_DEBUGEN);
  1597. return stlink_usb_write_debug_reg(handle, DCB_DHCSR, DBGKEY|C_HALT|C_DEBUGEN);
  1598. }
  1599. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1600. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1601. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_STEPCORE;
  1602. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  1603. }
  1604. /** */
  1605. static int stlink_usb_read_regs(void *handle)
  1606. {
  1607. int res;
  1608. struct stlink_usb_handle_s *h = handle;
  1609. assert(handle != NULL);
  1610. stlink_usb_init_buffer(handle, h->rx_ep, 88);
  1611. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1612. if (h->version.jtag_api == STLINK_JTAG_API_V1) {
  1613. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READALLREGS;
  1614. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 84);
  1615. /* regs data from offset 0 */
  1616. } else {
  1617. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READALLREGS;
  1618. res = stlink_usb_xfer_errcheck(handle, h->databuf, 88);
  1619. /* status at offset 0, regs data from offset 4 */
  1620. }
  1621. return res;
  1622. }
  1623. /** */
  1624. static int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
  1625. {
  1626. int res;
  1627. struct stlink_usb_handle_s *h = handle;
  1628. assert(handle != NULL);
  1629. stlink_usb_init_buffer(handle, h->rx_ep, h->version.jtag_api == STLINK_JTAG_API_V1 ? 4 : 8);
  1630. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1631. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  1632. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_READREG;
  1633. else
  1634. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READREG;
  1635. h->cmdbuf[h->cmdidx++] = num;
  1636. if (h->version.jtag_api == STLINK_JTAG_API_V1) {
  1637. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, 4);
  1638. if (res != ERROR_OK)
  1639. return res;
  1640. *val = le_to_h_u32(h->databuf);
  1641. return ERROR_OK;
  1642. } else {
  1643. res = stlink_cmd_allow_retry(handle, h->databuf, 8);
  1644. if (res != ERROR_OK)
  1645. return res;
  1646. *val = le_to_h_u32(h->databuf + 4);
  1647. return ERROR_OK;
  1648. }
  1649. }
  1650. /** */
  1651. static int stlink_usb_write_reg(void *handle, int num, uint32_t val)
  1652. {
  1653. struct stlink_usb_handle_s *h = handle;
  1654. assert(handle != NULL);
  1655. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1656. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1657. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  1658. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV1_WRITEREG;
  1659. else
  1660. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEREG;
  1661. h->cmdbuf[h->cmdidx++] = num;
  1662. h_u32_to_le(h->cmdbuf+h->cmdidx, val);
  1663. h->cmdidx += 4;
  1664. return stlink_cmd_allow_retry(handle, h->databuf, 2);
  1665. }
  1666. static int stlink_usb_get_rw_status(void *handle)
  1667. {
  1668. struct stlink_usb_handle_s *h = handle;
  1669. assert(handle != NULL);
  1670. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  1671. return ERROR_OK;
  1672. stlink_usb_init_buffer(handle, h->rx_ep, 2);
  1673. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1674. if (h->version.flags & STLINK_F_HAS_GETLASTRWSTATUS2) {
  1675. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS2;
  1676. return stlink_usb_xfer_errcheck(handle, h->databuf, 12);
  1677. } else {
  1678. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_GETLASTRWSTATUS;
  1679. return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
  1680. }
  1681. }
  1682. /** */
  1683. static int stlink_usb_read_mem8(void *handle, uint32_t addr, uint16_t len,
  1684. uint8_t *buffer)
  1685. {
  1686. int res;
  1687. uint16_t read_len = len;
  1688. struct stlink_usb_handle_s *h = handle;
  1689. assert(handle != NULL);
  1690. /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
  1691. if (len > stlink_usb_block(h)) {
  1692. LOG_DEBUG("max buffer (%d) length exceeded", stlink_usb_block(h));
  1693. return ERROR_FAIL;
  1694. }
  1695. stlink_usb_init_buffer(handle, h->rx_ep, read_len);
  1696. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1697. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_8BIT;
  1698. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1699. h->cmdidx += 4;
  1700. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1701. h->cmdidx += 2;
  1702. /* we need to fix read length for single bytes */
  1703. if (read_len == 1)
  1704. read_len++;
  1705. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, read_len);
  1706. if (res != ERROR_OK)
  1707. return res;
  1708. memcpy(buffer, h->databuf, len);
  1709. return stlink_usb_get_rw_status(handle);
  1710. }
  1711. /** */
  1712. static int stlink_usb_write_mem8(void *handle, uint32_t addr, uint16_t len,
  1713. const uint8_t *buffer)
  1714. {
  1715. int res;
  1716. struct stlink_usb_handle_s *h = handle;
  1717. assert(handle != NULL);
  1718. /* max 8 bit read/write is 64 bytes or 512 bytes for v3 */
  1719. if (len > stlink_usb_block(h)) {
  1720. LOG_DEBUG("max buffer length (%d) exceeded", stlink_usb_block(h));
  1721. return ERROR_FAIL;
  1722. }
  1723. stlink_usb_init_buffer(handle, h->tx_ep, len);
  1724. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1725. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_8BIT;
  1726. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1727. h->cmdidx += 4;
  1728. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1729. h->cmdidx += 2;
  1730. res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
  1731. if (res != ERROR_OK)
  1732. return res;
  1733. return stlink_usb_get_rw_status(handle);
  1734. }
  1735. /** */
  1736. static int stlink_usb_read_mem16(void *handle, uint32_t addr, uint16_t len,
  1737. uint8_t *buffer)
  1738. {
  1739. int res;
  1740. struct stlink_usb_handle_s *h = handle;
  1741. assert(handle != NULL);
  1742. if (!(h->version.flags & STLINK_F_HAS_MEM_16BIT))
  1743. return ERROR_COMMAND_NOTFOUND;
  1744. /* data must be a multiple of 2 and half-word aligned */
  1745. if (len % 2 || addr % 2) {
  1746. LOG_DEBUG("Invalid data alignment");
  1747. return ERROR_TARGET_UNALIGNED_ACCESS;
  1748. }
  1749. stlink_usb_init_buffer(handle, h->rx_ep, len);
  1750. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1751. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READMEM_16BIT;
  1752. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1753. h->cmdidx += 4;
  1754. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1755. h->cmdidx += 2;
  1756. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, len);
  1757. if (res != ERROR_OK)
  1758. return res;
  1759. memcpy(buffer, h->databuf, len);
  1760. return stlink_usb_get_rw_status(handle);
  1761. }
  1762. /** */
  1763. static int stlink_usb_write_mem16(void *handle, uint32_t addr, uint16_t len,
  1764. const uint8_t *buffer)
  1765. {
  1766. int res;
  1767. struct stlink_usb_handle_s *h = handle;
  1768. assert(handle != NULL);
  1769. if (!(h->version.flags & STLINK_F_HAS_MEM_16BIT))
  1770. return ERROR_COMMAND_NOTFOUND;
  1771. /* data must be a multiple of 2 and half-word aligned */
  1772. if (len % 2 || addr % 2) {
  1773. LOG_DEBUG("Invalid data alignment");
  1774. return ERROR_TARGET_UNALIGNED_ACCESS;
  1775. }
  1776. stlink_usb_init_buffer(handle, h->tx_ep, len);
  1777. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1778. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITEMEM_16BIT;
  1779. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1780. h->cmdidx += 4;
  1781. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1782. h->cmdidx += 2;
  1783. res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
  1784. if (res != ERROR_OK)
  1785. return res;
  1786. return stlink_usb_get_rw_status(handle);
  1787. }
  1788. /** */
  1789. static int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
  1790. uint8_t *buffer)
  1791. {
  1792. int res;
  1793. struct stlink_usb_handle_s *h = handle;
  1794. assert(handle != NULL);
  1795. /* data must be a multiple of 4 and word aligned */
  1796. if (len % 4 || addr % 4) {
  1797. LOG_DEBUG("Invalid data alignment");
  1798. return ERROR_TARGET_UNALIGNED_ACCESS;
  1799. }
  1800. stlink_usb_init_buffer(handle, h->rx_ep, len);
  1801. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1802. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_READMEM_32BIT;
  1803. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1804. h->cmdidx += 4;
  1805. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1806. h->cmdidx += 2;
  1807. res = stlink_usb_xfer_noerrcheck(handle, h->databuf, len);
  1808. if (res != ERROR_OK)
  1809. return res;
  1810. memcpy(buffer, h->databuf, len);
  1811. return stlink_usb_get_rw_status(handle);
  1812. }
  1813. /** */
  1814. static int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
  1815. const uint8_t *buffer)
  1816. {
  1817. int res;
  1818. struct stlink_usb_handle_s *h = handle;
  1819. assert(handle != NULL);
  1820. /* data must be a multiple of 4 and word aligned */
  1821. if (len % 4 || addr % 4) {
  1822. LOG_DEBUG("Invalid data alignment");
  1823. return ERROR_TARGET_UNALIGNED_ACCESS;
  1824. }
  1825. stlink_usb_init_buffer(handle, h->tx_ep, len);
  1826. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  1827. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_WRITEMEM_32BIT;
  1828. h_u32_to_le(h->cmdbuf+h->cmdidx, addr);
  1829. h->cmdidx += 4;
  1830. h_u16_to_le(h->cmdbuf+h->cmdidx, len);
  1831. h->cmdidx += 2;
  1832. res = stlink_usb_xfer_noerrcheck(handle, buffer, len);
  1833. if (res != ERROR_OK)
  1834. return res;
  1835. return stlink_usb_get_rw_status(handle);
  1836. }
  1837. static uint32_t stlink_max_block_size(uint32_t tar_autoincr_block, uint32_t address)
  1838. {
  1839. uint32_t max_tar_block = (tar_autoincr_block - ((tar_autoincr_block - 1) & address));
  1840. if (max_tar_block == 0)
  1841. max_tar_block = 4;
  1842. return max_tar_block;
  1843. }
  1844. static int stlink_usb_read_mem(void *handle, uint32_t addr, uint32_t size,
  1845. uint32_t count, uint8_t *buffer)
  1846. {
  1847. int retval = ERROR_OK;
  1848. uint32_t bytes_remaining;
  1849. int retries = 0;
  1850. struct stlink_usb_handle_s *h = handle;
  1851. /* calculate byte count */
  1852. count *= size;
  1853. /* switch to 8 bit if stlink does not support 16 bit memory read */
  1854. if (size == 2 && !(h->version.flags & STLINK_F_HAS_MEM_16BIT))
  1855. size = 1;
  1856. while (count) {
  1857. bytes_remaining = (size != 1) ? \
  1858. stlink_max_block_size(h->max_mem_packet, addr) : stlink_usb_block(h);
  1859. if (count < bytes_remaining)
  1860. bytes_remaining = count;
  1861. if (h->transport == HL_TRANSPORT_SWIM) {
  1862. retval = stlink_swim_readbytes(handle, addr, bytes_remaining, buffer);
  1863. if (retval != ERROR_OK)
  1864. return retval;
  1865. } else
  1866. /*
  1867. * all stlink support 8/32bit memory read/writes and only from
  1868. * stlink V2J26 there is support for 16 bit memory read/write.
  1869. * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
  1870. * as 8bit access.
  1871. */
  1872. if (size != 1) {
  1873. /* When in jtag mode the stlink uses the auto-increment functionality.
  1874. * However it expects us to pass the data correctly, this includes
  1875. * alignment and any page boundaries. We already do this as part of the
  1876. * adi_v5 implementation, but the stlink is a hla adapter and so this
  1877. * needs implementing manually.
  1878. * currently this only affects jtag mode, according to ST they do single
  1879. * access in SWD mode - but this may change and so we do it for both modes */
  1880. /* we first need to check for any unaligned bytes */
  1881. if (addr & (size - 1)) {
  1882. uint32_t head_bytes = size - (addr & (size - 1));
  1883. retval = stlink_usb_read_mem8(handle, addr, head_bytes, buffer);
  1884. if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  1885. usleep((1<<retries++) * 1000);
  1886. continue;
  1887. }
  1888. if (retval != ERROR_OK)
  1889. return retval;
  1890. buffer += head_bytes;
  1891. addr += head_bytes;
  1892. count -= head_bytes;
  1893. bytes_remaining -= head_bytes;
  1894. }
  1895. if (bytes_remaining & (size - 1))
  1896. retval = stlink_usb_read_mem(handle, addr, 1, bytes_remaining, buffer);
  1897. else if (size == 2)
  1898. retval = stlink_usb_read_mem16(handle, addr, bytes_remaining, buffer);
  1899. else
  1900. retval = stlink_usb_read_mem32(handle, addr, bytes_remaining, buffer);
  1901. } else
  1902. retval = stlink_usb_read_mem8(handle, addr, bytes_remaining, buffer);
  1903. if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  1904. usleep((1<<retries++) * 1000);
  1905. continue;
  1906. }
  1907. if (retval != ERROR_OK)
  1908. return retval;
  1909. buffer += bytes_remaining;
  1910. addr += bytes_remaining;
  1911. count -= bytes_remaining;
  1912. }
  1913. return retval;
  1914. }
  1915. static int stlink_usb_write_mem(void *handle, uint32_t addr, uint32_t size,
  1916. uint32_t count, const uint8_t *buffer)
  1917. {
  1918. int retval = ERROR_OK;
  1919. uint32_t bytes_remaining;
  1920. int retries = 0;
  1921. struct stlink_usb_handle_s *h = handle;
  1922. /* calculate byte count */
  1923. count *= size;
  1924. /* switch to 8 bit if stlink does not support 16 bit memory read */
  1925. if (size == 2 && !(h->version.flags & STLINK_F_HAS_MEM_16BIT))
  1926. size = 1;
  1927. while (count) {
  1928. bytes_remaining = (size != 1) ? \
  1929. stlink_max_block_size(h->max_mem_packet, addr) : stlink_usb_block(h);
  1930. if (count < bytes_remaining)
  1931. bytes_remaining = count;
  1932. if (h->transport == HL_TRANSPORT_SWIM) {
  1933. retval = stlink_swim_writebytes(handle, addr, bytes_remaining, buffer);
  1934. if (retval != ERROR_OK)
  1935. return retval;
  1936. } else
  1937. /*
  1938. * all stlink support 8/32bit memory read/writes and only from
  1939. * stlink V2J26 there is support for 16 bit memory read/write.
  1940. * Honour 32 bit and, if possible, 16 bit too. Otherwise, handle
  1941. * as 8bit access.
  1942. */
  1943. if (size != 1) {
  1944. /* When in jtag mode the stlink uses the auto-increment functionality.
  1945. * However it expects us to pass the data correctly, this includes
  1946. * alignment and any page boundaries. We already do this as part of the
  1947. * adi_v5 implementation, but the stlink is a hla adapter and so this
  1948. * needs implementing manually.
  1949. * currently this only affects jtag mode, according to ST they do single
  1950. * access in SWD mode - but this may change and so we do it for both modes */
  1951. /* we first need to check for any unaligned bytes */
  1952. if (addr & (size - 1)) {
  1953. uint32_t head_bytes = size - (addr & (size - 1));
  1954. retval = stlink_usb_write_mem8(handle, addr, head_bytes, buffer);
  1955. if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  1956. usleep((1<<retries++) * 1000);
  1957. continue;
  1958. }
  1959. if (retval != ERROR_OK)
  1960. return retval;
  1961. buffer += head_bytes;
  1962. addr += head_bytes;
  1963. count -= head_bytes;
  1964. bytes_remaining -= head_bytes;
  1965. }
  1966. if (bytes_remaining & (size - 1))
  1967. retval = stlink_usb_write_mem(handle, addr, 1, bytes_remaining, buffer);
  1968. else if (size == 2)
  1969. retval = stlink_usb_write_mem16(handle, addr, bytes_remaining, buffer);
  1970. else
  1971. retval = stlink_usb_write_mem32(handle, addr, bytes_remaining, buffer);
  1972. } else
  1973. retval = stlink_usb_write_mem8(handle, addr, bytes_remaining, buffer);
  1974. if (retval == ERROR_WAIT && retries < MAX_WAIT_RETRIES) {
  1975. usleep((1<<retries++) * 1000);
  1976. continue;
  1977. }
  1978. if (retval != ERROR_OK)
  1979. return retval;
  1980. buffer += bytes_remaining;
  1981. addr += bytes_remaining;
  1982. count -= bytes_remaining;
  1983. }
  1984. return retval;
  1985. }
  1986. /** */
  1987. static int stlink_usb_override_target(const char *targetname)
  1988. {
  1989. return !strcmp(targetname, "cortex_m");
  1990. }
  1991. static int stlink_speed_swim(void *handle, int khz, bool query)
  1992. {
  1993. /*
  1994. we dont care what the khz rate is
  1995. we only have low and high speed...
  1996. before changing speed the SWIM_CSR HS bit
  1997. must be updated
  1998. */
  1999. if (khz == 0)
  2000. stlink_swim_speed(handle, 0);
  2001. else
  2002. stlink_swim_speed(handle, 1);
  2003. return khz;
  2004. }
  2005. static int stlink_match_speed_map(const struct speed_map *map, unsigned int map_size, int khz, bool query)
  2006. {
  2007. unsigned int i;
  2008. int speed_index = -1;
  2009. int speed_diff = INT_MAX;
  2010. int last_valid_speed = -1;
  2011. bool match = true;
  2012. for (i = 0; i < map_size; i++) {
  2013. if (!map[i].speed)
  2014. continue;
  2015. last_valid_speed = i;
  2016. if (khz == map[i].speed) {
  2017. speed_index = i;
  2018. break;
  2019. } else {
  2020. int current_diff = khz - map[i].speed;
  2021. /* get abs value for comparison */
  2022. current_diff = (current_diff > 0) ? current_diff : -current_diff;
  2023. if ((current_diff < speed_diff) && khz >= map[i].speed) {
  2024. speed_diff = current_diff;
  2025. speed_index = i;
  2026. }
  2027. }
  2028. }
  2029. if (speed_index == -1) {
  2030. /* this will only be here if we cannot match the slow speed.
  2031. * use the slowest speed we support.*/
  2032. speed_index = last_valid_speed;
  2033. match = false;
  2034. } else if (i == map_size)
  2035. match = false;
  2036. if (!match && query) {
  2037. LOG_INFO("Unable to match requested speed %d kHz, using %d kHz", \
  2038. khz, map[speed_index].speed);
  2039. }
  2040. return speed_index;
  2041. }
  2042. static int stlink_speed_swd(void *handle, int khz, bool query)
  2043. {
  2044. int speed_index;
  2045. struct stlink_usb_handle_s *h = handle;
  2046. /* old firmware cannot change it */
  2047. if (!(h->version.flags & STLINK_F_HAS_SWD_SET_FREQ))
  2048. return khz;
  2049. speed_index = stlink_match_speed_map(stlink_khz_to_speed_map_swd,
  2050. ARRAY_SIZE(stlink_khz_to_speed_map_swd), khz, query);
  2051. if (!query) {
  2052. int result = stlink_usb_set_swdclk(h, stlink_khz_to_speed_map_swd[speed_index].speed_divisor);
  2053. if (result != ERROR_OK) {
  2054. LOG_ERROR("Unable to set adapter speed");
  2055. return khz;
  2056. }
  2057. }
  2058. return stlink_khz_to_speed_map_swd[speed_index].speed;
  2059. }
  2060. static int stlink_speed_jtag(void *handle, int khz, bool query)
  2061. {
  2062. int speed_index;
  2063. struct stlink_usb_handle_s *h = handle;
  2064. /* old firmware cannot change it */
  2065. if (!(h->version.flags & STLINK_F_HAS_JTAG_SET_FREQ))
  2066. return khz;
  2067. speed_index = stlink_match_speed_map(stlink_khz_to_speed_map_jtag,
  2068. ARRAY_SIZE(stlink_khz_to_speed_map_jtag), khz, query);
  2069. if (!query) {
  2070. int result = stlink_usb_set_jtagclk(h, stlink_khz_to_speed_map_jtag[speed_index].speed_divisor);
  2071. if (result != ERROR_OK) {
  2072. LOG_ERROR("Unable to set adapter speed");
  2073. return khz;
  2074. }
  2075. }
  2076. return stlink_khz_to_speed_map_jtag[speed_index].speed;
  2077. }
  2078. void stlink_dump_speed_map(const struct speed_map *map, unsigned int map_size)
  2079. {
  2080. unsigned int i;
  2081. LOG_DEBUG("Supported clock speeds are:");
  2082. for (i = 0; i < map_size; i++)
  2083. if (map[i].speed)
  2084. LOG_DEBUG("%d kHz", map[i].speed);
  2085. }
  2086. static int stlink_get_com_freq(void *handle, bool is_jtag, struct speed_map *map)
  2087. {
  2088. struct stlink_usb_handle_s *h = handle;
  2089. int i;
  2090. if (h->version.jtag_api != STLINK_JTAG_API_V3) {
  2091. LOG_ERROR("Unknown command");
  2092. return 0;
  2093. }
  2094. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  2095. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  2096. h->cmdbuf[h->cmdidx++] = STLINK_APIV3_GET_COM_FREQ;
  2097. h->cmdbuf[h->cmdidx++] = is_jtag ? 1 : 0;
  2098. int res = stlink_usb_xfer_errcheck(handle, h->databuf, 52);
  2099. int size = h->databuf[8];
  2100. if (size > STLINK_V3_MAX_FREQ_NB)
  2101. size = STLINK_V3_MAX_FREQ_NB;
  2102. for (i = 0; i < size; i++) {
  2103. map[i].speed = le_to_h_u32(&h->databuf[12 + 4 * i]);
  2104. map[i].speed_divisor = i;
  2105. }
  2106. /* set to zero all the next entries */
  2107. for (i = size; i < STLINK_V3_MAX_FREQ_NB; i++)
  2108. map[i].speed = 0;
  2109. return res;
  2110. }
  2111. static int stlink_set_com_freq(void *handle, bool is_jtag, unsigned int frequency)
  2112. {
  2113. struct stlink_usb_handle_s *h = handle;
  2114. if (h->version.jtag_api != STLINK_JTAG_API_V3) {
  2115. LOG_ERROR("Unknown command");
  2116. return 0;
  2117. }
  2118. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  2119. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  2120. h->cmdbuf[h->cmdidx++] = STLINK_APIV3_SET_COM_FREQ;
  2121. h->cmdbuf[h->cmdidx++] = is_jtag ? 1 : 0;
  2122. h->cmdbuf[h->cmdidx++] = 0;
  2123. h_u32_to_le(&h->cmdbuf[4], frequency);
  2124. return stlink_usb_xfer_errcheck(handle, h->databuf, 8);
  2125. }
  2126. static int stlink_speed_v3(void *handle, bool is_jtag, int khz, bool query)
  2127. {
  2128. struct stlink_usb_handle_s *h = handle;
  2129. int speed_index;
  2130. struct speed_map map[STLINK_V3_MAX_FREQ_NB];
  2131. stlink_get_com_freq(h, is_jtag, map);
  2132. speed_index = stlink_match_speed_map(map, ARRAY_SIZE(map), khz, query);
  2133. if (!query) {
  2134. int result = stlink_set_com_freq(h, is_jtag, map[speed_index].speed);
  2135. if (result != ERROR_OK) {
  2136. LOG_ERROR("Unable to set adapter speed");
  2137. return khz;
  2138. }
  2139. }
  2140. return map[speed_index].speed;
  2141. }
  2142. static int stlink_speed(void *handle, int khz, bool query)
  2143. {
  2144. struct stlink_usb_handle_s *h = handle;
  2145. if (!handle)
  2146. return khz;
  2147. switch (h->transport) {
  2148. case HL_TRANSPORT_SWIM:
  2149. return stlink_speed_swim(handle, khz, query);
  2150. break;
  2151. case HL_TRANSPORT_SWD:
  2152. if (h->version.jtag_api == STLINK_JTAG_API_V3)
  2153. return stlink_speed_v3(handle, false, khz, query);
  2154. else
  2155. return stlink_speed_swd(handle, khz, query);
  2156. break;
  2157. case HL_TRANSPORT_JTAG:
  2158. if (h->version.jtag_api == STLINK_JTAG_API_V3)
  2159. return stlink_speed_v3(handle, true, khz, query);
  2160. else
  2161. return stlink_speed_jtag(handle, khz, query);
  2162. break;
  2163. default:
  2164. break;
  2165. }
  2166. return khz;
  2167. }
  2168. /** */
  2169. static int stlink_usb_close(void *handle)
  2170. {
  2171. int res;
  2172. uint8_t mode;
  2173. enum stlink_mode emode;
  2174. struct stlink_usb_handle_s *h = handle;
  2175. if (h && h->fd)
  2176. res = stlink_usb_current_mode(handle, &mode);
  2177. else
  2178. res = ERROR_FAIL;
  2179. /* do not exit if return code != ERROR_OK,
  2180. it prevents us from closing jtag_libusb */
  2181. if (res == ERROR_OK) {
  2182. /* try to exit current mode */
  2183. switch (mode) {
  2184. case STLINK_DEV_DFU_MODE:
  2185. emode = STLINK_MODE_DFU;
  2186. break;
  2187. case STLINK_DEV_DEBUG_MODE:
  2188. emode = STLINK_MODE_DEBUG_SWD;
  2189. break;
  2190. case STLINK_DEV_SWIM_MODE:
  2191. emode = STLINK_MODE_DEBUG_SWIM;
  2192. break;
  2193. case STLINK_DEV_BOOTLOADER_MODE:
  2194. case STLINK_DEV_MASS_MODE:
  2195. default:
  2196. emode = STLINK_MODE_UNKNOWN;
  2197. break;
  2198. }
  2199. if (emode != STLINK_MODE_UNKNOWN)
  2200. stlink_usb_mode_leave(handle, emode);
  2201. /* do not check return code, it prevent
  2202. us from closing jtag_libusb */
  2203. }
  2204. if (h && h->fd)
  2205. jtag_libusb_close(h->fd);
  2206. free(h);
  2207. return ERROR_OK;
  2208. }
  2209. /** */
  2210. static int stlink_usb_open(struct hl_interface_param_s *param, void **fd)
  2211. {
  2212. int err, retry_count = 1;
  2213. struct stlink_usb_handle_s *h;
  2214. LOG_DEBUG("stlink_usb_open");
  2215. h = calloc(1, sizeof(struct stlink_usb_handle_s));
  2216. if (h == 0) {
  2217. LOG_DEBUG("malloc failed");
  2218. return ERROR_FAIL;
  2219. }
  2220. h->transport = param->transport;
  2221. for (unsigned i = 0; param->vid[i]; i++) {
  2222. LOG_DEBUG("transport: %d vid: 0x%04x pid: 0x%04x serial: %s",
  2223. param->transport, param->vid[i], param->pid[i],
  2224. param->serial ? param->serial : "");
  2225. }
  2226. /*
  2227. On certain host USB configurations(e.g. MacBook Air)
  2228. STLINKv2 dongle seems to have its FW in a funky state if,
  2229. after plugging it in, you try to use openocd with it more
  2230. then once (by launching and closing openocd). In cases like
  2231. that initial attempt to read the FW info via
  2232. stlink_usb_version will fail and the device has to be reset
  2233. in order to become operational.
  2234. */
  2235. do {
  2236. if (jtag_libusb_open(param->vid, param->pid, param->serial, &h->fd) != ERROR_OK) {
  2237. LOG_ERROR("open failed");
  2238. goto error_open;
  2239. }
  2240. jtag_libusb_set_configuration(h->fd, 0);
  2241. if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
  2242. LOG_DEBUG("claim interface failed");
  2243. goto error_open;
  2244. }
  2245. /* RX EP is common for all versions */
  2246. h->rx_ep = STLINK_RX_EP;
  2247. uint16_t pid;
  2248. if (jtag_libusb_get_pid(jtag_libusb_get_device(h->fd), &pid) != ERROR_OK) {
  2249. LOG_DEBUG("libusb_get_pid failed");
  2250. goto error_open;
  2251. }
  2252. /* wrap version for first read */
  2253. switch (pid) {
  2254. case STLINK_V1_PID:
  2255. h->version.stlink = 1;
  2256. h->tx_ep = STLINK_TX_EP;
  2257. break;
  2258. case STLINK_V3_USBLOADER_PID:
  2259. case STLINK_V3E_PID:
  2260. case STLINK_V3S_PID:
  2261. case STLINK_V3_2VCP_PID:
  2262. h->version.stlink = 3;
  2263. h->tx_ep = STLINK_V2_1_TX_EP;
  2264. h->trace_ep = STLINK_V2_1_TRACE_EP;
  2265. break;
  2266. case STLINK_V2_1_PID:
  2267. case STLINK_V2_1_NO_MSD_PID:
  2268. h->version.stlink = 2;
  2269. h->tx_ep = STLINK_V2_1_TX_EP;
  2270. h->trace_ep = STLINK_V2_1_TRACE_EP;
  2271. break;
  2272. default:
  2273. /* fall through - we assume V2 to be the default version*/
  2274. case STLINK_V2_PID:
  2275. h->version.stlink = 2;
  2276. h->tx_ep = STLINK_TX_EP;
  2277. h->trace_ep = STLINK_TRACE_EP;
  2278. break;
  2279. }
  2280. /* get the device version */
  2281. err = stlink_usb_version(h);
  2282. if (err == ERROR_OK) {
  2283. break;
  2284. } else if (h->version.stlink == 1 ||
  2285. retry_count == 0) {
  2286. LOG_ERROR("read version failed");
  2287. goto error_open;
  2288. } else {
  2289. err = jtag_libusb_release_interface(h->fd, 0);
  2290. if (err != ERROR_OK) {
  2291. LOG_ERROR("release interface failed");
  2292. goto error_open;
  2293. }
  2294. err = jtag_libusb_reset_device(h->fd);
  2295. if (err != ERROR_OK) {
  2296. LOG_ERROR("reset device failed");
  2297. goto error_open;
  2298. }
  2299. jtag_libusb_close(h->fd);
  2300. /*
  2301. Give the device one second to settle down and
  2302. reenumerate.
  2303. */
  2304. usleep(1 * 1000 * 1000);
  2305. retry_count--;
  2306. }
  2307. } while (1);
  2308. /* check if mode is supported */
  2309. err = ERROR_OK;
  2310. switch (h->transport) {
  2311. case HL_TRANSPORT_SWD:
  2312. if (h->version.jtag_api == STLINK_JTAG_API_V1)
  2313. err = ERROR_FAIL;
  2314. /* fall-through */
  2315. case HL_TRANSPORT_JTAG:
  2316. if (h->version.jtag == 0)
  2317. err = ERROR_FAIL;
  2318. break;
  2319. case HL_TRANSPORT_SWIM:
  2320. if (h->version.swim == 0)
  2321. err = ERROR_FAIL;
  2322. break;
  2323. default:
  2324. err = ERROR_FAIL;
  2325. break;
  2326. }
  2327. if (err != ERROR_OK) {
  2328. LOG_ERROR("mode (transport) not supported by device");
  2329. goto error_open;
  2330. }
  2331. /* initialize the debug hardware */
  2332. err = stlink_usb_init_mode(h, param->connect_under_reset, param->initial_interface_speed);
  2333. if (err != ERROR_OK) {
  2334. LOG_ERROR("init mode failed (unable to connect to the target)");
  2335. goto error_open;
  2336. }
  2337. if (h->transport == HL_TRANSPORT_SWIM) {
  2338. err = stlink_swim_enter(h);
  2339. if (err != ERROR_OK) {
  2340. LOG_ERROR("stlink_swim_enter_failed (unable to connect to the target)");
  2341. goto error_open;
  2342. }
  2343. *fd = h;
  2344. h->max_mem_packet = STLINK_DATA_SIZE;
  2345. return ERROR_OK;
  2346. }
  2347. /* get cpuid, so we can determine the max page size
  2348. * start with a safe default */
  2349. h->max_mem_packet = (1 << 10);
  2350. uint8_t buffer[4];
  2351. err = stlink_usb_read_mem32(h, CPUID, 4, buffer);
  2352. if (err == ERROR_OK) {
  2353. uint32_t cpuid = le_to_h_u32(buffer);
  2354. int i = (cpuid >> 4) & 0xf;
  2355. if (i == 4 || i == 3) {
  2356. /* Cortex-M3/M4 has 4096 bytes autoincrement range */
  2357. h->max_mem_packet = (1 << 12);
  2358. }
  2359. }
  2360. LOG_DEBUG("Using TAR autoincrement: %" PRIu32, h->max_mem_packet);
  2361. *fd = h;
  2362. return ERROR_OK;
  2363. error_open:
  2364. stlink_usb_close(h);
  2365. return ERROR_FAIL;
  2366. }
  2367. int stlink_config_trace(void *handle, bool enabled,
  2368. enum tpiu_pin_protocol pin_protocol, uint32_t port_size,
  2369. unsigned int *trace_freq, unsigned int traceclkin_freq,
  2370. uint16_t *prescaler)
  2371. {
  2372. struct stlink_usb_handle_s *h = handle;
  2373. uint16_t presc;
  2374. if (enabled && (!(h->version.flags & STLINK_F_HAS_TRACE) ||
  2375. pin_protocol != TPIU_PIN_PROTOCOL_ASYNC_UART)) {
  2376. LOG_ERROR("The attached ST-LINK version doesn't support this trace mode");
  2377. return ERROR_FAIL;
  2378. }
  2379. if (!enabled) {
  2380. stlink_usb_trace_disable(h);
  2381. return ERROR_OK;
  2382. }
  2383. if (*trace_freq > STLINK_TRACE_MAX_HZ) {
  2384. LOG_ERROR("ST-LINK doesn't support SWO frequency higher than %u",
  2385. STLINK_TRACE_MAX_HZ);
  2386. return ERROR_FAIL;
  2387. }
  2388. stlink_usb_trace_disable(h);
  2389. if (!*trace_freq)
  2390. *trace_freq = STLINK_TRACE_MAX_HZ;
  2391. presc = traceclkin_freq / *trace_freq;
  2392. if (traceclkin_freq % *trace_freq > 0)
  2393. presc++;
  2394. if (presc > TPIU_ACPR_MAX_SWOSCALER) {
  2395. LOG_ERROR("SWO frequency is not suitable. Please choose a different "
  2396. "frequency.");
  2397. return ERROR_FAIL;
  2398. }
  2399. *prescaler = presc;
  2400. h->trace.source_hz = *trace_freq;
  2401. return stlink_usb_trace_enable(h);
  2402. }
  2403. /** */
  2404. static int stlink_usb_init_access_port(void *handle, unsigned char ap_num)
  2405. {
  2406. struct stlink_usb_handle_s *h = handle;
  2407. assert(handle != NULL);
  2408. if (!(h->version.flags & STLINK_F_HAS_AP_INIT))
  2409. return ERROR_COMMAND_NOTFOUND;
  2410. LOG_DEBUG_IO("init ap_num = %d", ap_num);
  2411. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  2412. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  2413. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_INIT_AP;
  2414. h->cmdbuf[h->cmdidx++] = ap_num;
  2415. return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
  2416. }
  2417. /** */
  2418. static int stlink_usb_close_access_port(void *handle, unsigned char ap_num)
  2419. {
  2420. struct stlink_usb_handle_s *h = handle;
  2421. assert(handle != NULL);
  2422. if (!(h->version.flags & STLINK_F_HAS_AP_INIT))
  2423. return ERROR_COMMAND_NOTFOUND;
  2424. LOG_DEBUG_IO("close ap_num = %d", ap_num);
  2425. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  2426. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  2427. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_CLOSE_AP_DBG;
  2428. h->cmdbuf[h->cmdidx++] = ap_num;
  2429. return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
  2430. }
  2431. /** */
  2432. static int stlink_read_dap_register(void *handle, unsigned short dap_port,
  2433. unsigned short addr, uint32_t *val)
  2434. {
  2435. struct stlink_usb_handle_s *h = handle;
  2436. int retval;
  2437. assert(handle != NULL);
  2438. if (!(h->version.flags & STLINK_F_HAS_DAP_REG))
  2439. return ERROR_COMMAND_NOTFOUND;
  2440. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  2441. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  2442. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_READ_DAP_REG;
  2443. h_u16_to_le(&h->cmdbuf[2], dap_port);
  2444. h_u16_to_le(&h->cmdbuf[4], addr);
  2445. retval = stlink_usb_xfer_errcheck(handle, h->databuf, 8);
  2446. *val = le_to_h_u32(h->databuf + 4);
  2447. LOG_DEBUG_IO("dap_port_read = %d, addr = 0x%x, value = 0x%x", dap_port, addr, *val);
  2448. return retval;
  2449. }
  2450. /** */
  2451. static int stlink_write_dap_register(void *handle, unsigned short dap_port,
  2452. unsigned short addr, uint32_t val)
  2453. {
  2454. struct stlink_usb_handle_s *h = handle;
  2455. assert(handle != NULL);
  2456. if (!(h->version.flags & STLINK_F_HAS_DAP_REG))
  2457. return ERROR_COMMAND_NOTFOUND;
  2458. LOG_DEBUG_IO("dap_write port = %d, addr = 0x%x, value = 0x%x", dap_port, addr, val);
  2459. stlink_usb_init_buffer(handle, h->rx_ep, 16);
  2460. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_COMMAND;
  2461. h->cmdbuf[h->cmdidx++] = STLINK_DEBUG_APIV2_WRITE_DAP_REG;
  2462. h_u16_to_le(&h->cmdbuf[2], dap_port);
  2463. h_u16_to_le(&h->cmdbuf[4], addr);
  2464. h_u32_to_le(&h->cmdbuf[6], val);
  2465. return stlink_usb_xfer_errcheck(handle, h->databuf, 2);
  2466. }
  2467. /** */
  2468. struct hl_layout_api_s stlink_usb_layout_api = {
  2469. /** */
  2470. .open = stlink_usb_open,
  2471. /** */
  2472. .close = stlink_usb_close,
  2473. /** */
  2474. .idcode = stlink_usb_idcode,
  2475. /** */
  2476. .state = stlink_usb_state,
  2477. /** */
  2478. .reset = stlink_usb_reset,
  2479. /** */
  2480. .assert_srst = stlink_usb_assert_srst,
  2481. /** */
  2482. .run = stlink_usb_run,
  2483. /** */
  2484. .halt = stlink_usb_halt,
  2485. /** */
  2486. .step = stlink_usb_step,
  2487. /** */
  2488. .read_regs = stlink_usb_read_regs,
  2489. /** */
  2490. .read_reg = stlink_usb_read_reg,
  2491. /** */
  2492. .write_reg = stlink_usb_write_reg,
  2493. /** */
  2494. .read_mem = stlink_usb_read_mem,
  2495. /** */
  2496. .write_mem = stlink_usb_write_mem,
  2497. /** */
  2498. .write_debug_reg = stlink_usb_write_debug_reg,
  2499. /** */
  2500. .override_target = stlink_usb_override_target,
  2501. /** */
  2502. .speed = stlink_speed,
  2503. /** */
  2504. .config_trace = stlink_config_trace,
  2505. /** */
  2506. .poll_trace = stlink_usb_trace_read,
  2507. };
  2508. /*****************************************************************************
  2509. * DAP direct interface
  2510. */
  2511. static struct stlink_usb_handle_s *stlink_dap_handle;
  2512. static struct hl_interface_param_s stlink_dap_param;
  2513. static DECLARE_BITMAP(opened_ap, DP_APSEL_MAX + 1);
  2514. static int stlink_dap_error = ERROR_OK;
  2515. static int stlink_dap_op_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
  2516. uint32_t *data);
  2517. /** */
  2518. static int stlink_dap_record_error(int error)
  2519. {
  2520. if (stlink_dap_error == ERROR_OK)
  2521. stlink_dap_error = error;
  2522. return ERROR_OK;
  2523. }
  2524. /** */
  2525. static int stlink_dap_get_and_clear_error(void)
  2526. {
  2527. int retval = stlink_dap_error;
  2528. stlink_dap_error = ERROR_OK;
  2529. return retval;
  2530. }
  2531. /** */
  2532. static int stlink_dap_open_ap(unsigned short apsel)
  2533. {
  2534. int retval;
  2535. /* nothing to do on old versions */
  2536. if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_AP_INIT))
  2537. return ERROR_OK;
  2538. if (apsel > DP_APSEL_MAX)
  2539. return ERROR_FAIL;
  2540. if (test_bit(apsel, opened_ap))
  2541. return ERROR_OK;
  2542. retval = stlink_usb_init_access_port(stlink_dap_handle, apsel);
  2543. if (retval != ERROR_OK)
  2544. return retval;
  2545. LOG_DEBUG("AP %d enabled", apsel);
  2546. set_bit(apsel, opened_ap);
  2547. return ERROR_OK;
  2548. }
  2549. /** */
  2550. static int stlink_dap_closeall_ap(void)
  2551. {
  2552. int retval, apsel;
  2553. /* nothing to do on old versions */
  2554. if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_AP_INIT))
  2555. return ERROR_OK;
  2556. for (apsel = 0; apsel <= DP_APSEL_MAX; apsel++) {
  2557. if (!test_bit(apsel, opened_ap))
  2558. continue;
  2559. retval = stlink_usb_close_access_port(stlink_dap_handle, apsel);
  2560. if (retval != ERROR_OK)
  2561. return retval;
  2562. clear_bit(apsel, opened_ap);
  2563. }
  2564. return ERROR_OK;
  2565. }
  2566. /** */
  2567. static int stlink_dap_reinit_interface(void)
  2568. {
  2569. int retval;
  2570. enum stlink_mode mode;
  2571. /*
  2572. * On JTAG only, it should be enough to call stlink_usb_reset(). But on
  2573. * some firmware version it does not work as expected, and there is no
  2574. * equivalent for SWD.
  2575. * At least for now, to reset the interface quit from JTAG/SWD mode then
  2576. * select the mode again.
  2577. */
  2578. mode = stlink_get_mode(stlink_dap_param.transport);
  2579. if (!stlink_dap_handle->reconnect_pending) {
  2580. stlink_dap_handle->reconnect_pending = true;
  2581. stlink_usb_mode_leave(stlink_dap_handle, mode);
  2582. }
  2583. retval = stlink_usb_mode_enter(stlink_dap_handle, mode);
  2584. if (retval != ERROR_OK)
  2585. return retval;
  2586. stlink_dap_handle->reconnect_pending = false;
  2587. /* on new FW, calling mode-leave closes all the opened AP; reopen them! */
  2588. if (stlink_dap_handle->version.flags & STLINK_F_HAS_AP_INIT)
  2589. for (int apsel = 0; apsel <= DP_APSEL_MAX; apsel++)
  2590. if (test_bit(apsel, opened_ap)) {
  2591. clear_bit(apsel, opened_ap);
  2592. stlink_dap_open_ap(apsel);
  2593. }
  2594. return ERROR_OK;
  2595. }
  2596. /** */
  2597. static int stlink_dap_op_connect(struct adiv5_dap *dap)
  2598. {
  2599. uint32_t idcode;
  2600. int retval;
  2601. LOG_INFO("stlink_dap_op_connect(%sconnect)", dap->do_reconnect ? "re" : "");
  2602. /* Check if we should reset srst already when connecting, but not if reconnecting. */
  2603. if (!dap->do_reconnect) {
  2604. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2605. if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
  2606. if (jtag_reset_config & RESET_SRST_NO_GATING)
  2607. adapter_assert_reset();
  2608. else
  2609. LOG_WARNING("\'srst_nogate\' reset_config option is required");
  2610. }
  2611. }
  2612. dap->do_reconnect = false;
  2613. dap_invalidate_cache(dap);
  2614. retval = dap_dp_init(dap);
  2615. if (retval != ERROR_OK) {
  2616. dap->do_reconnect = true;
  2617. return retval;
  2618. }
  2619. retval = stlink_usb_idcode(stlink_dap_handle, &idcode);
  2620. if (retval == ERROR_OK)
  2621. LOG_INFO("%s %#8.8" PRIx32,
  2622. (stlink_dap_handle->transport == HL_TRANSPORT_JTAG) ? "JTAG IDCODE" : "SWD DPIDR",
  2623. idcode);
  2624. else
  2625. dap->do_reconnect = true;
  2626. return retval;
  2627. }
  2628. /** */
  2629. static int stlink_dap_check_reconnect(struct adiv5_dap *dap)
  2630. {
  2631. int retval;
  2632. if (!dap->do_reconnect)
  2633. return ERROR_OK;
  2634. retval = stlink_dap_reinit_interface();
  2635. if (retval != ERROR_OK)
  2636. return retval;
  2637. return stlink_dap_op_connect(dap);
  2638. }
  2639. /** */
  2640. static int stlink_dap_op_send_sequence(struct adiv5_dap *dap, enum swd_special_seq seq)
  2641. {
  2642. /* Ignore the request */
  2643. return ERROR_OK;
  2644. }
  2645. /** */
  2646. static int stlink_dap_op_queue_dp_read(struct adiv5_dap *dap, unsigned reg,
  2647. uint32_t *data)
  2648. {
  2649. uint32_t dummy;
  2650. int retval;
  2651. if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_DPBANKSEL))
  2652. if (reg & 0x000000F0) {
  2653. LOG_ERROR("Banked DP registers not supported in current STLink FW");
  2654. return ERROR_COMMAND_NOTFOUND;
  2655. }
  2656. retval = stlink_dap_check_reconnect(dap);
  2657. if (retval != ERROR_OK)
  2658. return retval;
  2659. data = data ? : &dummy;
  2660. if (stlink_dap_handle->version.flags & STLINK_F_QUIRK_JTAG_DP_READ
  2661. && stlink_dap_handle->transport == HL_TRANSPORT_JTAG) {
  2662. /* Quirk required in JTAG. Read RDBUFF to get the data */
  2663. retval = stlink_read_dap_register(stlink_dap_handle,
  2664. STLINK_DEBUG_PORT_ACCESS, reg, &dummy);
  2665. if (retval == ERROR_OK)
  2666. retval = stlink_read_dap_register(stlink_dap_handle,
  2667. STLINK_DEBUG_PORT_ACCESS, DP_RDBUFF, data);
  2668. } else {
  2669. retval = stlink_read_dap_register(stlink_dap_handle,
  2670. STLINK_DEBUG_PORT_ACCESS, reg, data);
  2671. }
  2672. return stlink_dap_record_error(retval);
  2673. }
  2674. /** */
  2675. static int stlink_dap_op_queue_dp_write(struct adiv5_dap *dap, unsigned reg,
  2676. uint32_t data)
  2677. {
  2678. int retval;
  2679. if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_DPBANKSEL))
  2680. if (reg & 0x000000F0) {
  2681. LOG_ERROR("Banked DP registers not supported in current STLink FW");
  2682. return ERROR_COMMAND_NOTFOUND;
  2683. }
  2684. if (reg == DP_SELECT && (data & DP_SELECT_DPBANK) != 0) {
  2685. /* ignored if STLINK_F_HAS_DPBANKSEL, not properly managed otherwise */
  2686. LOG_DEBUG("Ignoring DPBANKSEL while write SELECT");
  2687. data &= ~DP_SELECT_DPBANK;
  2688. }
  2689. retval = stlink_dap_check_reconnect(dap);
  2690. if (retval != ERROR_OK)
  2691. return retval;
  2692. /* ST-Link does not like that we set CORUNDETECT */
  2693. if (reg == DP_CTRL_STAT)
  2694. data &= ~CORUNDETECT;
  2695. retval = stlink_write_dap_register(stlink_dap_handle,
  2696. STLINK_DEBUG_PORT_ACCESS, reg, data);
  2697. return stlink_dap_record_error(retval);
  2698. }
  2699. /** */
  2700. static int stlink_dap_op_queue_ap_read(struct adiv5_ap *ap, unsigned reg,
  2701. uint32_t *data)
  2702. {
  2703. struct adiv5_dap *dap = ap->dap;
  2704. uint32_t dummy;
  2705. int retval;
  2706. retval = stlink_dap_check_reconnect(dap);
  2707. if (retval != ERROR_OK)
  2708. return retval;
  2709. if (reg != AP_REG_IDR) {
  2710. retval = stlink_dap_open_ap(ap->ap_num);
  2711. if (retval != ERROR_OK)
  2712. return retval;
  2713. }
  2714. data = data ? : &dummy;
  2715. retval = stlink_read_dap_register(stlink_dap_handle, ap->ap_num, reg,
  2716. data);
  2717. dap->stlink_flush_ap_write = false;
  2718. return stlink_dap_record_error(retval);
  2719. }
  2720. /** */
  2721. static int stlink_dap_op_queue_ap_write(struct adiv5_ap *ap, unsigned reg,
  2722. uint32_t data)
  2723. {
  2724. struct adiv5_dap *dap = ap->dap;
  2725. int retval;
  2726. retval = stlink_dap_check_reconnect(dap);
  2727. if (retval != ERROR_OK)
  2728. return retval;
  2729. retval = stlink_dap_open_ap(ap->ap_num);
  2730. if (retval != ERROR_OK)
  2731. return retval;
  2732. retval = stlink_write_dap_register(stlink_dap_handle, ap->ap_num, reg,
  2733. data);
  2734. dap->stlink_flush_ap_write = true;
  2735. return stlink_dap_record_error(retval);
  2736. }
  2737. /** */
  2738. static int stlink_dap_op_queue_ap_abort(struct adiv5_dap *dap, uint8_t *ack)
  2739. {
  2740. LOG_WARNING("stlink_dap_op_queue_ap_abort()");
  2741. return ERROR_OK;
  2742. }
  2743. /** */
  2744. static int stlink_dap_op_run(struct adiv5_dap *dap)
  2745. {
  2746. uint32_t ctrlstat, pwrmask;
  2747. int retval, saved_retval;
  2748. /* Here no LOG_DEBUG. This is called continuously! */
  2749. /*
  2750. * ST-Link returns immediately after a DAP write, without waiting for it
  2751. * to complete.
  2752. * Run a dummy read to DP_RDBUFF, as suggested in
  2753. * http://infocenter.arm.com/help/topic/com.arm.doc.faqs/ka16363.html
  2754. */
  2755. if (dap->stlink_flush_ap_write) {
  2756. dap->stlink_flush_ap_write = false;
  2757. retval = stlink_dap_op_queue_dp_read(dap, DP_RDBUFF, NULL);
  2758. if (retval != ERROR_OK) {
  2759. dap->do_reconnect = true;
  2760. return retval;
  2761. }
  2762. }
  2763. saved_retval = stlink_dap_get_and_clear_error();
  2764. retval = stlink_dap_op_queue_dp_read(dap, DP_CTRL_STAT, &ctrlstat);
  2765. if (retval != ERROR_OK) {
  2766. dap->do_reconnect = true;
  2767. return retval;
  2768. }
  2769. retval = stlink_dap_get_and_clear_error();
  2770. if (retval != ERROR_OK) {
  2771. LOG_ERROR("Fail reading CTRL/STAT register. Force reconnect");
  2772. dap->do_reconnect = true;
  2773. return retval;
  2774. }
  2775. if (ctrlstat & SSTICKYERR) {
  2776. if (stlink_dap_param.transport == HL_TRANSPORT_JTAG)
  2777. retval = stlink_dap_op_queue_dp_write(dap, DP_CTRL_STAT,
  2778. ctrlstat & (dap->dp_ctrl_stat | SSTICKYERR));
  2779. else
  2780. retval = stlink_dap_op_queue_dp_write(dap, DP_ABORT, STKERRCLR);
  2781. if (retval != ERROR_OK) {
  2782. dap->do_reconnect = true;
  2783. return retval;
  2784. }
  2785. retval = stlink_dap_get_and_clear_error();
  2786. if (retval != ERROR_OK) {
  2787. dap->do_reconnect = true;
  2788. return retval;
  2789. }
  2790. }
  2791. /* check for power lost */
  2792. pwrmask = dap->dp_ctrl_stat & (CDBGPWRUPREQ | CSYSPWRUPREQ);
  2793. if ((ctrlstat & pwrmask) != pwrmask)
  2794. dap->do_reconnect = true;
  2795. return saved_retval;
  2796. }
  2797. /** */
  2798. static void stlink_dap_op_quit(struct adiv5_dap *dap)
  2799. {
  2800. int retval;
  2801. retval = stlink_dap_closeall_ap();
  2802. if (retval != ERROR_OK)
  2803. LOG_ERROR("Error closing APs");
  2804. }
  2805. static int stlink_dap_config_trace(bool enabled,
  2806. enum tpiu_pin_protocol pin_protocol, uint32_t port_size,
  2807. unsigned int *trace_freq, unsigned int traceclkin_freq,
  2808. uint16_t *prescaler)
  2809. {
  2810. return stlink_config_trace(stlink_dap_handle, enabled, pin_protocol,
  2811. port_size, trace_freq, traceclkin_freq,
  2812. prescaler);
  2813. }
  2814. static int stlink_dap_trace_read(uint8_t *buf, size_t *size)
  2815. {
  2816. return stlink_usb_trace_read(stlink_dap_handle, buf, size);
  2817. }
  2818. /** */
  2819. COMMAND_HANDLER(stlink_dap_serial_command)
  2820. {
  2821. LOG_DEBUG("stlink_dap_serial_command");
  2822. if (CMD_ARGC != 1) {
  2823. LOG_ERROR("Expected exactly one argument for \"st-link serial <serial-number>\".");
  2824. return ERROR_COMMAND_SYNTAX_ERROR;
  2825. }
  2826. if (stlink_dap_param.serial) {
  2827. LOG_WARNING("Command \"st-link serial\" already used. Replacing previous value");
  2828. free((void *)stlink_dap_param.serial);
  2829. }
  2830. stlink_dap_param.serial = strdup(CMD_ARGV[0]);
  2831. return ERROR_OK;
  2832. }
  2833. /** */
  2834. COMMAND_HANDLER(stlink_dap_vid_pid)
  2835. {
  2836. unsigned int i, max_usb_ids = HLA_MAX_USB_IDS;
  2837. if (CMD_ARGC > max_usb_ids * 2) {
  2838. LOG_WARNING("ignoring extra IDs in vid_pid "
  2839. "(maximum is %d pairs)", max_usb_ids);
  2840. CMD_ARGC = max_usb_ids * 2;
  2841. }
  2842. if (CMD_ARGC < 2 || (CMD_ARGC & 1)) {
  2843. LOG_WARNING("incomplete vid_pid configuration directive");
  2844. return ERROR_COMMAND_SYNTAX_ERROR;
  2845. }
  2846. for (i = 0; i < CMD_ARGC; i += 2) {
  2847. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i], stlink_dap_param.vid[i / 2]);
  2848. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[i + 1], stlink_dap_param.pid[i / 2]);
  2849. }
  2850. /* null termination */
  2851. stlink_dap_param.vid[i / 2] = stlink_dap_param.pid[i / 2] = 0;
  2852. return ERROR_OK;
  2853. }
  2854. /** */
  2855. static const struct command_registration stlink_dap_subcommand_handlers[] = {
  2856. {
  2857. .name = "serial",
  2858. .handler = stlink_dap_serial_command,
  2859. .mode = COMMAND_CONFIG,
  2860. .help = "set the serial number of the adapter",
  2861. .usage = "<serial_number>",
  2862. },
  2863. {
  2864. .name = "vid_pid",
  2865. .handler = stlink_dap_vid_pid,
  2866. .mode = COMMAND_CONFIG,
  2867. .help = "USB VID and PID of the adapter",
  2868. .usage = "(vid pid)+",
  2869. },
  2870. COMMAND_REGISTRATION_DONE
  2871. };
  2872. /** */
  2873. static const struct command_registration stlink_dap_command_handlers[] = {
  2874. {
  2875. .name = "st-link",
  2876. .mode = COMMAND_ANY,
  2877. .help = "perform st-link management",
  2878. .chain = stlink_dap_subcommand_handlers,
  2879. .usage = "",
  2880. },
  2881. COMMAND_REGISTRATION_DONE
  2882. };
  2883. /** */
  2884. static int stlink_dap_init(void)
  2885. {
  2886. enum reset_types jtag_reset_config = jtag_get_reset_config();
  2887. int retval;
  2888. LOG_DEBUG("stlink_dap_init()");
  2889. if (jtag_reset_config & RESET_CNCT_UNDER_SRST) {
  2890. if (jtag_reset_config & RESET_SRST_NO_GATING)
  2891. stlink_dap_param.connect_under_reset = true;
  2892. else
  2893. LOG_WARNING("\'srst_nogate\' reset_config option is required");
  2894. }
  2895. if (transport_is_dapdirect_swd())
  2896. stlink_dap_param.transport = HL_TRANSPORT_SWD;
  2897. else if (transport_is_dapdirect_jtag())
  2898. stlink_dap_param.transport = HL_TRANSPORT_JTAG;
  2899. else {
  2900. LOG_ERROR("Unsupported transport");
  2901. return ERROR_FAIL;
  2902. }
  2903. retval = stlink_usb_open(&stlink_dap_param, (void **)&stlink_dap_handle);
  2904. if (retval != ERROR_OK)
  2905. return retval;
  2906. if (!(stlink_dap_handle->version.flags & STLINK_F_HAS_DAP_REG)) {
  2907. LOG_ERROR("ST-Link version does not support DAP direct transport");
  2908. return ERROR_FAIL;
  2909. }
  2910. return ERROR_OK;
  2911. }
  2912. /** */
  2913. static int stlink_dap_quit(void)
  2914. {
  2915. LOG_DEBUG("stlink_dap_quit()");
  2916. free((void *)stlink_dap_param.serial);
  2917. stlink_dap_param.serial = NULL;
  2918. return stlink_usb_close(stlink_dap_handle);
  2919. }
  2920. /** */
  2921. static int stlink_dap_reset(int req_trst, int req_srst)
  2922. {
  2923. LOG_DEBUG("stlink_dap_reset(%d)", req_srst);
  2924. return stlink_usb_assert_srst(stlink_dap_handle,
  2925. req_srst ? STLINK_DEBUG_APIV2_DRIVE_NRST_LOW
  2926. : STLINK_DEBUG_APIV2_DRIVE_NRST_HIGH);
  2927. }
  2928. /** */
  2929. static int stlink_dap_speed(int speed)
  2930. {
  2931. if (speed == 0) {
  2932. LOG_ERROR("RTCK not supported. Set nonzero adapter_khz.");
  2933. return ERROR_JTAG_NOT_IMPLEMENTED;
  2934. }
  2935. stlink_dap_param.initial_interface_speed = speed;
  2936. stlink_speed(stlink_dap_handle, speed, false);
  2937. return ERROR_OK;
  2938. }
  2939. /** */
  2940. static int stlink_dap_khz(int khz, int *jtag_speed)
  2941. {
  2942. *jtag_speed = khz;
  2943. return ERROR_OK;
  2944. }
  2945. /** */
  2946. static int stlink_dap_speed_div(int speed, int *khz)
  2947. {
  2948. *khz = speed;
  2949. return ERROR_OK;
  2950. }
  2951. static const struct dap_ops stlink_dap_ops = {
  2952. .connect = stlink_dap_op_connect,
  2953. .send_sequence = stlink_dap_op_send_sequence,
  2954. .queue_dp_read = stlink_dap_op_queue_dp_read,
  2955. .queue_dp_write = stlink_dap_op_queue_dp_write,
  2956. .queue_ap_read = stlink_dap_op_queue_ap_read,
  2957. .queue_ap_write = stlink_dap_op_queue_ap_write,
  2958. .queue_ap_abort = stlink_dap_op_queue_ap_abort,
  2959. .run = stlink_dap_op_run,
  2960. .sync = NULL, /* optional */
  2961. .quit = stlink_dap_op_quit, /* optional */
  2962. };
  2963. static const char *const stlink_dap_transport[] = { "dapdirect_jtag", "dapdirect_swd", NULL };
  2964. struct adapter_driver stlink_dap_adapter_driver = {
  2965. .name = "st-link",
  2966. .transports = stlink_dap_transport,
  2967. .commands = stlink_dap_command_handlers,
  2968. .init = stlink_dap_init,
  2969. .quit = stlink_dap_quit,
  2970. .reset = stlink_dap_reset,
  2971. .speed = stlink_dap_speed,
  2972. .khz = stlink_dap_khz,
  2973. .speed_div = stlink_dap_speed_div,
  2974. .config_trace = stlink_dap_config_trace,
  2975. .poll_trace = stlink_dap_trace_read,
  2976. .dap_jtag_ops = &stlink_dap_ops,
  2977. .dap_swd_ops = &stlink_dap_ops,
  2978. };