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.
 
 
 
 
 
 

659 lines
14 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011 by Mathias Kuester *
  3. * Mathias Kuester <kesmtp@freenet.de> *
  4. * *
  5. * This code is based on https://github.com/texane/stlink *
  6. * *
  7. * This program is free software; you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation; either version 2 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * You should have received a copy of the GNU General Public License *
  18. * along with this program; if not, write to the *
  19. * Free Software Foundation, Inc., *
  20. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  21. ***************************************************************************/
  22. #ifdef HAVE_CONFIG_H
  23. #include "config.h"
  24. #endif
  25. /* project specific includes */
  26. #include <helper/binarybuffer.h>
  27. #include <jtag/interface.h>
  28. #include <jtag/stlink/stlink_layout.h>
  29. #include <jtag/stlink/stlink_interface.h>
  30. #include <target/target.h>
  31. #include "libusb_common.h"
  32. #define ENDPOINT_IN 0x80
  33. #define ENDPOINT_OUT 0x00
  34. #define STLINK_RX_EP (1|ENDPOINT_IN)
  35. #define STLINK_TX_EP (2|ENDPOINT_OUT)
  36. #define STLINK_CMD_SIZE (16)
  37. #define STLINK_TX_SIZE (4*128)
  38. #define STLINK_RX_SIZE (4*128)
  39. /** */
  40. struct stlink_usb_handle_s {
  41. /** */
  42. struct jtag_libusb_device_handle *fd;
  43. /** */
  44. struct libusb_transfer *trans;
  45. /** */
  46. uint8_t txbuf[STLINK_TX_SIZE];
  47. /** */
  48. uint8_t rxbuf[STLINK_RX_SIZE];
  49. };
  50. #define STLINK_OK 0x80
  51. #define STLINK_FALSE 0x81
  52. #define STLINK_CORE_RUNNING 0x80
  53. #define STLINK_CORE_HALTED 0x81
  54. #define STLINK_CORE_STAT_UNKNOWN -1
  55. #define STLINK_GET_VERSION 0xf1
  56. #define STLINK_GET_CURRENT_MODE 0xf5
  57. #define STLINK_DEBUG_COMMAND 0xF2
  58. #define STLINK_DFU_COMMAND 0xF3
  59. #define STLINK_DFU_EXIT 0x07
  60. #define STLINK_DEV_DFU_MODE 0x00
  61. #define STLINK_DEV_MASS_MODE 0x01
  62. #define STLINK_DEV_DEBUG_MODE 0x02
  63. #define STLINK_DEV_UNKNOWN_MODE -1
  64. #define STLINK_DEBUG_ENTER 0x20
  65. #define STLINK_DEBUG_EXIT 0x21
  66. #define STLINK_DEBUG_READCOREID 0x22
  67. #define STLINK_DEBUG_GETSTATUS 0x01
  68. #define STLINK_DEBUG_FORCEDEBUG 0x02
  69. #define STLINK_DEBUG_RESETSYS 0x03
  70. #define STLINK_DEBUG_READALLREGS 0x04
  71. #define STLINK_DEBUG_READREG 0x05
  72. #define STLINK_DEBUG_WRITEREG 0x06
  73. #define STLINK_DEBUG_READMEM_32BIT 0x07
  74. #define STLINK_DEBUG_WRITEMEM_32BIT 0x08
  75. #define STLINK_DEBUG_RUNCORE 0x09
  76. #define STLINK_DEBUG_STEPCORE 0x0a
  77. #define STLINK_DEBUG_SETFP 0x0b
  78. #define STLINK_DEBUG_WRITEMEM_8BIT 0x0d
  79. #define STLINK_DEBUG_CLEARFP 0x0e
  80. #define STLINK_DEBUG_WRITEDEBUGREG 0x0f
  81. #define STLINK_DEBUG_ENTER_SWD 0xa3
  82. #define STLINK_DEBUG_ENTER_JTAG 0x00
  83. #define STLINK_SWD_ENTER 0x30
  84. #define STLINK_SWD_READCOREID 0x32
  85. /** */
  86. int stlink_usb_recv(void *handle, uint8_t *txbuf, int txsize, uint8_t *rxbuf,
  87. int rxsize)
  88. {
  89. struct stlink_usb_handle_s *h;
  90. assert(handle != NULL);
  91. h = (struct stlink_usb_handle_s *)handle;
  92. if (jtag_libusb_bulk_write(h->fd, STLINK_TX_EP, (char *)txbuf, txsize,
  93. 1000) != txsize) {
  94. return ERROR_FAIL;
  95. }
  96. if (rxsize && rxbuf) {
  97. if (jtag_libusb_bulk_read(h->fd, STLINK_RX_EP, (char *)rxbuf,
  98. rxsize, 1000) != rxsize) {
  99. return ERROR_FAIL;
  100. }
  101. }
  102. return ERROR_OK;
  103. }
  104. /** */
  105. void stlink_usb_init_buffer(void *handle)
  106. {
  107. struct stlink_usb_handle_s *h;
  108. assert(handle != NULL);
  109. h = (struct stlink_usb_handle_s *)handle;
  110. memset(h->txbuf, 0, STLINK_CMD_SIZE);
  111. }
  112. /** */
  113. int stlink_usb_version(void *handle)
  114. {
  115. int res;
  116. uint16_t v;
  117. struct stlink_usb_handle_s *h;
  118. assert(handle != NULL);
  119. h = (struct stlink_usb_handle_s *)handle;
  120. stlink_usb_init_buffer(handle);
  121. h->txbuf[0] = STLINK_GET_VERSION;
  122. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 6);
  123. if (res != ERROR_OK)
  124. return res;
  125. v = (h->rxbuf[0] << 8) | h->rxbuf[1];
  126. LOG_DEBUG("STLINK v%d", (v >> 12) & 0x0f);
  127. LOG_DEBUG("JTAG v%d", (v >> 6) & 0x3f);
  128. LOG_DEBUG("SWIM v%d", v & 0x3f);
  129. LOG_DEBUG("VID %04X", buf_get_u32(h->rxbuf, 16, 16));
  130. LOG_DEBUG("PID %04X", buf_get_u32(h->rxbuf, 32, 16));
  131. return ERROR_OK;
  132. }
  133. /** */
  134. int stlink_usb_current_mode(void *handle, uint8_t *mode)
  135. {
  136. int res;
  137. struct stlink_usb_handle_s *h;
  138. assert(handle != NULL);
  139. h = (struct stlink_usb_handle_s *)handle;
  140. stlink_usb_init_buffer(handle);
  141. h->txbuf[0] = STLINK_GET_CURRENT_MODE;
  142. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
  143. if (res != ERROR_OK)
  144. return res;
  145. *mode = h->rxbuf[0];
  146. return ERROR_OK;
  147. }
  148. /** */
  149. int stlink_usb_dfu_mode_leave(void *handle)
  150. {
  151. int res;
  152. struct stlink_usb_handle_s *h;
  153. assert(handle != NULL);
  154. h = (struct stlink_usb_handle_s *)handle;
  155. stlink_usb_init_buffer(handle);
  156. h->txbuf[0] = STLINK_DFU_COMMAND;
  157. h->txbuf[1] = STLINK_DFU_EXIT;
  158. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
  159. if (res != ERROR_OK)
  160. return res;
  161. return ERROR_OK;
  162. }
  163. /** */
  164. int stlink_usb_swd_mode_enter(void *handle)
  165. {
  166. int res;
  167. struct stlink_usb_handle_s *h;
  168. assert(handle != NULL);
  169. h = (struct stlink_usb_handle_s *)handle;
  170. stlink_usb_init_buffer(handle);
  171. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  172. h->txbuf[1] = STLINK_DEBUG_ENTER;
  173. h->txbuf[2] = STLINK_DEBUG_ENTER_SWD;
  174. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
  175. if (res != ERROR_OK)
  176. return res;
  177. return ERROR_OK;
  178. }
  179. /** */
  180. int stlink_usb_debug_mode_leave(void *handle)
  181. {
  182. int res;
  183. struct stlink_usb_handle_s *h;
  184. assert(handle != NULL);
  185. h = (struct stlink_usb_handle_s *)handle;
  186. stlink_usb_init_buffer(handle);
  187. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  188. h->txbuf[1] = STLINK_DEBUG_EXIT;
  189. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
  190. if (res != ERROR_OK)
  191. return res;
  192. return ERROR_OK;
  193. }
  194. /** */
  195. int stlink_usb_init_mode(void *handle)
  196. {
  197. int res;
  198. uint8_t mode;
  199. assert(handle != NULL);
  200. res = stlink_usb_current_mode(handle, &mode);
  201. if (res != ERROR_OK)
  202. return res;
  203. LOG_DEBUG("MODE: %02X", mode);
  204. if (mode == STLINK_DEV_DFU_MODE) {
  205. res = stlink_usb_dfu_mode_leave(handle);
  206. if (res != ERROR_OK)
  207. return res;
  208. }
  209. res = stlink_usb_current_mode(handle, &mode);
  210. if (res != ERROR_OK)
  211. return res;
  212. LOG_DEBUG("MODE: %02X", mode);
  213. if (mode != STLINK_DEV_DEBUG_MODE) {
  214. res = stlink_usb_swd_mode_enter(handle);
  215. if (res != ERROR_OK)
  216. return res;
  217. }
  218. res = stlink_usb_current_mode(handle, &mode);
  219. if (res != ERROR_OK)
  220. return res;
  221. LOG_DEBUG("MODE: %02X", mode);
  222. return ERROR_OK;
  223. }
  224. /** */
  225. int stlink_usb_idcode(void *handle, uint32_t *idcode)
  226. {
  227. int res;
  228. struct stlink_usb_handle_s *h;
  229. assert(handle != NULL);
  230. h = (struct stlink_usb_handle_s *)handle;
  231. stlink_usb_init_buffer(handle);
  232. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  233. h->txbuf[1] = STLINK_DEBUG_READCOREID;
  234. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
  235. if (res != ERROR_OK)
  236. return res;
  237. *idcode = le_to_h_u32(h->rxbuf);
  238. LOG_DEBUG("IDCODE: %08X", *idcode);
  239. return ERROR_OK;
  240. }
  241. /** */
  242. enum target_state stlink_usb_state(void *handle)
  243. {
  244. int res;
  245. struct stlink_usb_handle_s *h;
  246. assert(handle != NULL);
  247. h = (struct stlink_usb_handle_s *)handle;
  248. stlink_usb_init_buffer(handle);
  249. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  250. h->txbuf[1] = STLINK_DEBUG_GETSTATUS;
  251. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
  252. if (res != ERROR_OK)
  253. return TARGET_UNKNOWN;
  254. if (h->rxbuf[0] == STLINK_CORE_RUNNING)
  255. return TARGET_RUNNING;
  256. if (h->rxbuf[0] == STLINK_CORE_HALTED)
  257. return TARGET_HALTED;
  258. return TARGET_UNKNOWN;
  259. }
  260. /** */
  261. int stlink_usb_reset(void *handle)
  262. {
  263. int res;
  264. struct stlink_usb_handle_s *h;
  265. assert(handle != NULL);
  266. h = (struct stlink_usb_handle_s *)handle;
  267. stlink_usb_init_buffer(handle);
  268. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  269. h->txbuf[1] = STLINK_DEBUG_RESETSYS;
  270. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
  271. if (res != ERROR_OK)
  272. return res;
  273. LOG_DEBUG("RESET: %08X", h->rxbuf[0]);
  274. return ERROR_OK;
  275. }
  276. /** */
  277. int stlink_usb_run(void *handle)
  278. {
  279. int res;
  280. struct stlink_usb_handle_s *h;
  281. assert(handle != NULL);
  282. h = (struct stlink_usb_handle_s *)handle;
  283. stlink_usb_init_buffer(handle);
  284. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  285. h->txbuf[1] = STLINK_DEBUG_RUNCORE;
  286. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
  287. if (res != ERROR_OK)
  288. return res;
  289. return ERROR_OK;
  290. }
  291. /** */
  292. int stlink_usb_halt(void *handle)
  293. {
  294. int res;
  295. struct stlink_usb_handle_s *h;
  296. assert(handle != NULL);
  297. h = (struct stlink_usb_handle_s *)handle;
  298. stlink_usb_init_buffer(handle);
  299. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  300. h->txbuf[1] = STLINK_DEBUG_FORCEDEBUG;
  301. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
  302. if (res != ERROR_OK)
  303. return res;
  304. return ERROR_OK;
  305. }
  306. /** */
  307. int stlink_usb_step(void *handle)
  308. {
  309. int res;
  310. struct stlink_usb_handle_s *h;
  311. assert(handle != NULL);
  312. h = (struct stlink_usb_handle_s *)handle;
  313. stlink_usb_init_buffer(handle);
  314. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  315. h->txbuf[1] = STLINK_DEBUG_STEPCORE;
  316. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
  317. if (res != ERROR_OK)
  318. return res;
  319. return ERROR_OK;
  320. }
  321. /** */
  322. int stlink_usb_read_regs(void *handle)
  323. {
  324. int res;
  325. struct stlink_usb_handle_s *h;
  326. assert(handle != NULL);
  327. h = (struct stlink_usb_handle_s *)handle;
  328. stlink_usb_init_buffer(handle);
  329. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  330. h->txbuf[1] = STLINK_DEBUG_READALLREGS;
  331. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 84);
  332. if (res != ERROR_OK)
  333. return res;
  334. return ERROR_OK;
  335. }
  336. /** */
  337. int stlink_usb_read_reg(void *handle, int num, uint32_t *val)
  338. {
  339. int res;
  340. struct stlink_usb_handle_s *h;
  341. assert(handle != NULL);
  342. h = (struct stlink_usb_handle_s *)handle;
  343. stlink_usb_init_buffer(handle);
  344. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  345. h->txbuf[1] = STLINK_DEBUG_READREG;
  346. h->txbuf[2] = num;
  347. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 4);
  348. if (res != ERROR_OK)
  349. return res;
  350. *val = le_to_h_u32(h->rxbuf);
  351. return ERROR_OK;
  352. }
  353. /** */
  354. int stlink_usb_write_reg(void *handle, int num, uint32_t val)
  355. {
  356. int res;
  357. struct stlink_usb_handle_s *h;
  358. assert(handle != NULL);
  359. h = (struct stlink_usb_handle_s *)handle;
  360. stlink_usb_init_buffer(handle);
  361. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  362. h->txbuf[1] = STLINK_DEBUG_WRITEREG;
  363. h->txbuf[2] = num;
  364. h_u32_to_le(h->txbuf + 3, val);
  365. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, 2);
  366. if (res != ERROR_OK)
  367. return res;
  368. return ERROR_OK;
  369. }
  370. /** */
  371. int stlink_usb_read_mem32(void *handle, uint32_t addr, uint16_t len,
  372. uint32_t *buffer)
  373. {
  374. int res;
  375. struct stlink_usb_handle_s *h;
  376. assert(handle != NULL);
  377. h = (struct stlink_usb_handle_s *)handle;
  378. stlink_usb_init_buffer(handle);
  379. len *= 4;
  380. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  381. h->txbuf[1] = STLINK_DEBUG_READMEM_32BIT;
  382. h_u32_to_le(h->txbuf + 2, addr);
  383. h_u16_to_le(h->txbuf + 2 + 4, len);
  384. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, h->rxbuf, len);
  385. if (res != ERROR_OK)
  386. return res;
  387. memcpy(buffer, h->rxbuf, len);
  388. return ERROR_OK;
  389. }
  390. /** */
  391. int stlink_usb_write_mem32(void *handle, uint32_t addr, uint16_t len,
  392. uint32_t *buffer)
  393. {
  394. int res;
  395. struct stlink_usb_handle_s *h;
  396. assert(handle != NULL);
  397. h = (struct stlink_usb_handle_s *)handle;
  398. stlink_usb_init_buffer(handle);
  399. len *= 4;
  400. h->txbuf[0] = STLINK_DEBUG_COMMAND;
  401. h->txbuf[1] = STLINK_DEBUG_WRITEMEM_32BIT;
  402. h_u32_to_le(h->txbuf + 2, addr);
  403. h_u16_to_le(h->txbuf + 2 + 4, len);
  404. res = stlink_usb_recv(handle, h->txbuf, STLINK_CMD_SIZE, 0, 0);
  405. if (res != ERROR_OK)
  406. return res;
  407. res = stlink_usb_recv(handle, (uint8_t *) buffer, len, 0, 0);
  408. if (res != ERROR_OK)
  409. return res;
  410. memcpy(buffer, h->rxbuf, len);
  411. return ERROR_OK;
  412. }
  413. /** */
  414. int stlink_usb_open(struct stlink_interface_param_s *param, void **fd)
  415. {
  416. struct stlink_usb_handle_s *h;
  417. LOG_DEBUG("stlink_usb_open");
  418. h = malloc(sizeof(struct stlink_usb_handle_s));
  419. if (h == 0) {
  420. LOG_DEBUG("stlink_open_usb: malloc failed");
  421. return ERROR_FAIL;
  422. }
  423. const uint16_t vids[] = { param->vid, 0 };
  424. const uint16_t pids[] = { param->pid, 0 };
  425. LOG_DEBUG("stlink_open_usb: vid: %04x pid: %04x", param->vid,
  426. param->pid);
  427. if (jtag_libusb_open(vids, pids, &h->fd) != ERROR_OK) {
  428. LOG_DEBUG("stlink_open_usb: open failed");
  429. return ERROR_FAIL;
  430. }
  431. jtag_libusb_set_configuration(h->fd, 0);
  432. if (jtag_libusb_claim_interface(h->fd, 0) != ERROR_OK) {
  433. LOG_DEBUG("stlink_open_usb: claim failed");
  434. return ERROR_FAIL;
  435. }
  436. stlink_usb_init_mode(h);
  437. stlink_usb_version(h);
  438. *fd = h;
  439. return ERROR_OK;
  440. }
  441. /** */
  442. struct stlink_layout_api_s stlink_layout_api = {
  443. /** */
  444. .open = stlink_usb_open,
  445. /** */
  446. .idcode = stlink_usb_idcode,
  447. /** */
  448. .state = stlink_usb_state,
  449. /** */
  450. .reset = stlink_usb_reset,
  451. /** */
  452. .run = stlink_usb_run,
  453. /** */
  454. .halt = stlink_usb_halt,
  455. /** */
  456. .step = stlink_usb_step,
  457. /** */
  458. .read_regs = stlink_usb_read_regs,
  459. /** */
  460. .read_reg = stlink_usb_read_reg,
  461. /** */
  462. .write_reg = stlink_usb_write_reg,
  463. /** */
  464. .read_mem32 = stlink_usb_read_mem32,
  465. /** */
  466. .write_mem32 = stlink_usb_write_mem32,
  467. };