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.
 
 
 
 
 
 

831 lines
21 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011 by Mathias Kuester *
  3. * Mathias Kuester <kesmtp@freenet.de> *
  4. * *
  5. * Copyright (C) 2011 by Spencer Oliver *
  6. * spen@spen-soft.co.uk *
  7. * *
  8. * This program is free software; you can redistribute it and/or modify *
  9. * it under the terms of the GNU General Public License as published by *
  10. * the Free Software Foundation; either version 2 of the License, or *
  11. * (at your option) any later version. *
  12. * *
  13. * This program is distributed in the hope that it will be useful, *
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  16. * GNU General Public License for more details. *
  17. * *
  18. * You should have received a copy of the GNU General Public License *
  19. * along with this program; if not, write to the *
  20. * Free Software Foundation, Inc., *
  21. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  22. ***************************************************************************/
  23. #ifdef HAVE_CONFIG_H
  24. #include "config.h"
  25. #endif
  26. #include "jtag/jtag.h"
  27. #include "jtag/hla/hla_transport.h"
  28. #include "jtag/hla/hla_interface.h"
  29. #include "jtag/hla/hla_layout.h"
  30. #include "register.h"
  31. #include "algorithm.h"
  32. #include "target.h"
  33. #include "breakpoints.h"
  34. #include "target_type.h"
  35. #include "armv7m.h"
  36. #include "cortex_m.h"
  37. #include "arm_semihosting.h"
  38. #define ARMV7M_SCS_DCRSR 0xe000edf4
  39. #define ARMV7M_SCS_DCRDR 0xe000edf8
  40. static inline struct hl_interface_s *target_to_adapter(struct target *target)
  41. {
  42. return target->tap->priv;
  43. }
  44. static int adapter_load_core_reg_u32(struct target *target,
  45. uint32_t num, uint32_t *value)
  46. {
  47. int retval;
  48. struct hl_interface_s *adapter = target_to_adapter(target);
  49. LOG_DEBUG("%s", __func__);
  50. /* NOTE: we "know" here that the register identifiers used
  51. * in the v7m header match the Cortex-M3 Debug Core Register
  52. * Selector values for R0..R15, xPSR, MSP, and PSP.
  53. */
  54. switch (num) {
  55. case 0 ... 18:
  56. /* read a normal core register */
  57. retval = adapter->layout->api->read_reg(adapter->fd, num, value);
  58. if (retval != ERROR_OK) {
  59. LOG_ERROR("JTAG failure %i", retval);
  60. return ERROR_JTAG_DEVICE_ERROR;
  61. }
  62. LOG_DEBUG("load from core reg %i value 0x%" PRIx32 "", (int)num, *value);
  63. break;
  64. case ARMV7M_FPSID:
  65. case ARMV7M_FPEXC:
  66. *value = 0;
  67. break;
  68. case ARMV7M_FPSCR:
  69. /* Floating-point Status and Registers */
  70. retval = target_write_u32(target, ARMV7M_SCS_DCRSR, 33);
  71. if (retval != ERROR_OK)
  72. return retval;
  73. retval = target_read_u32(target, ARMV7M_SCS_DCRDR, value);
  74. if (retval != ERROR_OK)
  75. return retval;
  76. LOG_DEBUG("load from core reg %i value 0x%" PRIx32 "", (int)num, *value);
  77. break;
  78. case ARMV7M_S0 ... ARMV7M_S31:
  79. /* Floating-point Status and Registers */
  80. retval = target_write_u32(target, ARMV7M_SCS_DCRSR, num-ARMV7M_S0+64);
  81. if (retval != ERROR_OK)
  82. return retval;
  83. retval = target_read_u32(target, ARMV7M_SCS_DCRDR, value);
  84. if (retval != ERROR_OK)
  85. return retval;
  86. LOG_DEBUG("load from core reg %i value 0x%" PRIx32 "", (int)num, *value);
  87. break;
  88. case ARMV7M_D0 ... ARMV7M_D15:
  89. value = 0;
  90. break;
  91. case ARMV7M_PRIMASK:
  92. case ARMV7M_BASEPRI:
  93. case ARMV7M_FAULTMASK:
  94. case ARMV7M_CONTROL:
  95. /* Cortex-M3 packages these four registers as bitfields
  96. * in one Debug Core register. So say r0 and r2 docs;
  97. * it was removed from r1 docs, but still works.
  98. */
  99. retval = adapter->layout->api->read_reg(adapter->fd, 20, value);
  100. if (retval != ERROR_OK)
  101. return retval;
  102. switch (num) {
  103. case ARMV7M_PRIMASK:
  104. *value = buf_get_u32((uint8_t *) value, 0, 1);
  105. break;
  106. case ARMV7M_BASEPRI:
  107. *value = buf_get_u32((uint8_t *) value, 8, 8);
  108. break;
  109. case ARMV7M_FAULTMASK:
  110. *value = buf_get_u32((uint8_t *) value, 16, 1);
  111. break;
  112. case ARMV7M_CONTROL:
  113. *value = buf_get_u32((uint8_t *) value, 24, 2);
  114. break;
  115. }
  116. LOG_DEBUG("load from special reg %i value 0x%" PRIx32 "",
  117. (int)num, *value);
  118. break;
  119. default:
  120. return ERROR_COMMAND_SYNTAX_ERROR;
  121. }
  122. return ERROR_OK;
  123. }
  124. static int adapter_store_core_reg_u32(struct target *target,
  125. uint32_t num, uint32_t value)
  126. {
  127. int retval;
  128. uint32_t reg;
  129. struct armv7m_common *armv7m = target_to_armv7m(target);
  130. struct hl_interface_s *adapter = target_to_adapter(target);
  131. LOG_DEBUG("%s", __func__);
  132. #ifdef ARMV7_GDB_HACKS
  133. /* If the LR register is being modified, make sure it will put us
  134. * in "thumb" mode, or an INVSTATE exception will occur. This is a
  135. * hack to deal with the fact that gdb will sometimes "forge"
  136. * return addresses, and doesn't set the LSB correctly (i.e., when
  137. * printing expressions containing function calls, it sets LR = 0.)
  138. * Valid exception return codes have bit 0 set too.
  139. */
  140. if (num == ARMV7M_R14)
  141. value |= 0x01;
  142. #endif
  143. /* NOTE: we "know" here that the register identifiers used
  144. * in the v7m header match the Cortex-M3 Debug Core Register
  145. * Selector values for R0..R15, xPSR, MSP, and PSP.
  146. */
  147. switch (num) {
  148. case 0 ... 18:
  149. retval = adapter->layout->api->write_reg(adapter->fd, num, value);
  150. if (retval != ERROR_OK) {
  151. struct reg *r;
  152. LOG_ERROR("JTAG failure");
  153. r = armv7m->arm.core_cache->reg_list + num;
  154. r->dirty = r->valid;
  155. return ERROR_JTAG_DEVICE_ERROR;
  156. }
  157. LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
  158. break;
  159. case ARMV7M_FPSID:
  160. case ARMV7M_FPEXC:
  161. break;
  162. case ARMV7M_FPSCR:
  163. /* Floating-point Status and Registers */
  164. retval = target_write_u32(target, ARMV7M_SCS_DCRDR, value);
  165. if (retval != ERROR_OK)
  166. return retval;
  167. retval = target_write_u32(target, ARMV7M_SCS_DCRSR, 33 | (1<<16));
  168. if (retval != ERROR_OK)
  169. return retval;
  170. LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
  171. break;
  172. case ARMV7M_S0 ... ARMV7M_S31:
  173. /* Floating-point Status and Registers */
  174. retval = target_write_u32(target, ARMV7M_SCS_DCRDR, value);
  175. if (retval != ERROR_OK)
  176. return retval;
  177. retval = target_write_u32(target, ARMV7M_SCS_DCRSR, (num-ARMV7M_S0+64) | (1<<16));
  178. if (retval != ERROR_OK)
  179. return retval;
  180. LOG_DEBUG("write core reg %i value 0x%" PRIx32 "", (int)num, value);
  181. break;
  182. case ARMV7M_D0 ... ARMV7M_D15:
  183. break;
  184. case ARMV7M_PRIMASK:
  185. case ARMV7M_BASEPRI:
  186. case ARMV7M_FAULTMASK:
  187. case ARMV7M_CONTROL:
  188. /* Cortex-M3 packages these four registers as bitfields
  189. * in one Debug Core register. So say r0 and r2 docs;
  190. * it was removed from r1 docs, but still works.
  191. */
  192. adapter->layout->api->read_reg(adapter->fd, 20, &reg);
  193. switch (num) {
  194. case ARMV7M_PRIMASK:
  195. buf_set_u32((uint8_t *) &reg, 0, 1, value);
  196. break;
  197. case ARMV7M_BASEPRI:
  198. buf_set_u32((uint8_t *) &reg, 8, 8, value);
  199. break;
  200. case ARMV7M_FAULTMASK:
  201. buf_set_u32((uint8_t *) &reg, 16, 1, value);
  202. break;
  203. case ARMV7M_CONTROL:
  204. buf_set_u32((uint8_t *) &reg, 24, 2, value);
  205. break;
  206. }
  207. adapter->layout->api->write_reg(adapter->fd, 20, reg);
  208. LOG_DEBUG("write special reg %i value 0x%" PRIx32 " ", (int)num, value);
  209. break;
  210. default:
  211. return ERROR_COMMAND_SYNTAX_ERROR;
  212. }
  213. return ERROR_OK;
  214. }
  215. static int adapter_examine_debug_reason(struct target *target)
  216. {
  217. if ((target->debug_reason != DBG_REASON_DBGRQ)
  218. && (target->debug_reason != DBG_REASON_SINGLESTEP)) {
  219. target->debug_reason = DBG_REASON_BREAKPOINT;
  220. }
  221. return ERROR_OK;
  222. }
  223. static int adapter_init_arch_info(struct target *target,
  224. struct cortex_m3_common *cortex_m3,
  225. struct jtag_tap *tap)
  226. {
  227. struct armv7m_common *armv7m;
  228. LOG_DEBUG("%s", __func__);
  229. armv7m = &cortex_m3->armv7m;
  230. armv7m_init_arch_info(target, armv7m);
  231. armv7m->load_core_reg_u32 = adapter_load_core_reg_u32;
  232. armv7m->store_core_reg_u32 = adapter_store_core_reg_u32;
  233. armv7m->examine_debug_reason = adapter_examine_debug_reason;
  234. armv7m->stlink = true;
  235. return ERROR_OK;
  236. }
  237. static int adapter_init_target(struct command_context *cmd_ctx,
  238. struct target *target)
  239. {
  240. LOG_DEBUG("%s", __func__);
  241. armv7m_build_reg_cache(target);
  242. return ERROR_OK;
  243. }
  244. static int adapter_target_create(struct target *target,
  245. Jim_Interp *interp)
  246. {
  247. LOG_DEBUG("%s", __func__);
  248. struct cortex_m3_common *cortex_m3 = calloc(1, sizeof(struct cortex_m3_common));
  249. if (!cortex_m3)
  250. return ERROR_COMMAND_SYNTAX_ERROR;
  251. adapter_init_arch_info(target, cortex_m3, target->tap);
  252. return ERROR_OK;
  253. }
  254. static int adapter_load_context(struct target *target)
  255. {
  256. struct armv7m_common *armv7m = target_to_armv7m(target);
  257. int num_regs = armv7m->arm.core_cache->num_regs;
  258. for (int i = 0; i < num_regs; i++) {
  259. struct reg *r = &armv7m->arm.core_cache->reg_list[i];
  260. if (!r->valid)
  261. armv7m->arm.read_core_reg(target, r, i, ARM_MODE_ANY);
  262. }
  263. return ERROR_OK;
  264. }
  265. static int adapter_debug_entry(struct target *target)
  266. {
  267. struct hl_interface_s *adapter = target_to_adapter(target);
  268. struct armv7m_common *armv7m = target_to_armv7m(target);
  269. struct arm *arm = &armv7m->arm;
  270. struct reg *r;
  271. uint32_t xPSR;
  272. int retval;
  273. retval = armv7m->examine_debug_reason(target);
  274. if (retval != ERROR_OK)
  275. return retval;
  276. adapter_load_context(target);
  277. /* make sure we clear the vector catch bit */
  278. adapter->layout->api->write_debug_reg(adapter->fd, DCB_DEMCR, TRCENA);
  279. r = arm->core_cache->reg_list + ARMV7M_xPSR;
  280. xPSR = buf_get_u32(r->value, 0, 32);
  281. /* Are we in an exception handler */
  282. if (xPSR & 0x1FF) {
  283. armv7m->exception_number = (xPSR & 0x1FF);
  284. arm->core_mode = ARM_MODE_HANDLER;
  285. arm->map = armv7m_msp_reg_map;
  286. } else {
  287. unsigned control = buf_get_u32(arm->core_cache
  288. ->reg_list[ARMV7M_CONTROL].value, 0, 2);
  289. /* is this thread privileged? */
  290. arm->core_mode = control & 1
  291. ? ARM_MODE_USER_THREAD
  292. : ARM_MODE_THREAD;
  293. /* which stack is it using? */
  294. if (control & 2)
  295. arm->map = armv7m_psp_reg_map;
  296. else
  297. arm->map = armv7m_msp_reg_map;
  298. armv7m->exception_number = 0;
  299. }
  300. LOG_DEBUG("entered debug state in core mode: %s at PC 0x%08" PRIx32 ", target->state: %s",
  301. arm_mode_name(arm->core_mode),
  302. *(uint32_t *)(arm->pc->value),
  303. target_state_name(target));
  304. return retval;
  305. }
  306. static int adapter_poll(struct target *target)
  307. {
  308. enum target_state state;
  309. struct hl_interface_s *adapter = target_to_adapter(target);
  310. struct armv7m_common *armv7m = target_to_armv7m(target);
  311. enum target_state prev_target_state = target->state;
  312. state = adapter->layout->api->state(adapter->fd);
  313. if (state == TARGET_UNKNOWN) {
  314. LOG_ERROR("jtag status contains invalid mode value - communication failure");
  315. return ERROR_TARGET_FAILURE;
  316. }
  317. if (target->state == state)
  318. return ERROR_OK;
  319. if (state == TARGET_HALTED) {
  320. target->state = state;
  321. int retval = adapter_debug_entry(target);
  322. if (retval != ERROR_OK)
  323. return retval;
  324. if (prev_target_state == TARGET_DEBUG_RUNNING) {
  325. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_HALTED);
  326. } else {
  327. if (arm_semihosting(target, &retval) != 0)
  328. return retval;
  329. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  330. }
  331. LOG_DEBUG("halted: PC: 0x%08x", buf_get_u32(armv7m->arm.pc->value, 0, 32));
  332. }
  333. return ERROR_OK;
  334. }
  335. static int adapter_assert_reset(struct target *target)
  336. {
  337. int res = ERROR_OK;
  338. struct hl_interface_s *adapter = target_to_adapter(target);
  339. struct armv7m_common *armv7m = target_to_armv7m(target);
  340. bool use_srst_fallback = true;
  341. LOG_DEBUG("%s", __func__);
  342. enum reset_types jtag_reset_config = jtag_get_reset_config();
  343. bool srst_asserted = false;
  344. if (jtag_reset_config & RESET_SRST_NO_GATING) {
  345. jtag_add_reset(0, 1);
  346. res = adapter->layout->api->assert_srst(adapter->fd, 0);
  347. srst_asserted = true;
  348. }
  349. adapter->layout->api->write_debug_reg(adapter->fd, DCB_DHCSR, DBGKEY|C_DEBUGEN);
  350. /* only set vector catch if halt is requested */
  351. if (target->reset_halt)
  352. adapter->layout->api->write_debug_reg(adapter->fd, DCB_DEMCR, TRCENA|VC_CORERESET);
  353. else
  354. adapter->layout->api->write_debug_reg(adapter->fd, DCB_DEMCR, TRCENA);
  355. if (jtag_reset_config & RESET_HAS_SRST) {
  356. if (!srst_asserted) {
  357. jtag_add_reset(0, 1);
  358. res = adapter->layout->api->assert_srst(adapter->fd, 0);
  359. }
  360. if (res == ERROR_COMMAND_NOTFOUND)
  361. LOG_ERROR("Hardware srst not supported, falling back to software reset");
  362. else if (res == ERROR_OK) {
  363. /* hardware srst supported */
  364. use_srst_fallback = false;
  365. }
  366. }
  367. if (use_srst_fallback) {
  368. /* stlink v1 api does not support hardware srst, so we use a software reset fallback */
  369. adapter->layout->api->write_debug_reg(adapter->fd, NVIC_AIRCR, AIRCR_VECTKEY | AIRCR_SYSRESETREQ);
  370. }
  371. res = adapter->layout->api->reset(adapter->fd);
  372. if (res != ERROR_OK)
  373. return res;
  374. /* registers are now invalid */
  375. register_cache_invalidate(armv7m->arm.core_cache);
  376. if (target->reset_halt) {
  377. target->state = TARGET_RESET;
  378. target->debug_reason = DBG_REASON_DBGRQ;
  379. } else {
  380. target->state = TARGET_HALTED;
  381. }
  382. return ERROR_OK;
  383. }
  384. static int adapter_deassert_reset(struct target *target)
  385. {
  386. int res;
  387. struct hl_interface_s *adapter = target_to_adapter(target);
  388. enum reset_types jtag_reset_config = jtag_get_reset_config();
  389. LOG_DEBUG("%s", __func__);
  390. if (jtag_reset_config & RESET_HAS_SRST)
  391. adapter->layout->api->assert_srst(adapter->fd, 1);
  392. /* virtual deassert reset, we need it for the internal
  393. * jtag state machine
  394. */
  395. jtag_add_reset(0, 0);
  396. if (!target->reset_halt) {
  397. res = target_resume(target, 1, 0, 0, 0);
  398. if (res != ERROR_OK)
  399. return res;
  400. }
  401. return ERROR_OK;
  402. }
  403. static int adapter_soft_reset_halt(struct target *target)
  404. {
  405. LOG_DEBUG("%s", __func__);
  406. return ERROR_OK;
  407. }
  408. static int adapter_halt(struct target *target)
  409. {
  410. int res;
  411. struct hl_interface_s *adapter = target_to_adapter(target);
  412. LOG_DEBUG("%s", __func__);
  413. if (target->state == TARGET_HALTED) {
  414. LOG_DEBUG("target was already halted");
  415. return ERROR_OK;
  416. }
  417. if (target->state == TARGET_UNKNOWN)
  418. LOG_WARNING("target was in unknown state when halt was requested");
  419. res = adapter->layout->api->halt(adapter->fd);
  420. if (res != ERROR_OK)
  421. return res;
  422. target->debug_reason = DBG_REASON_DBGRQ;
  423. return ERROR_OK;
  424. }
  425. static int adapter_resume(struct target *target, int current,
  426. uint32_t address, int handle_breakpoints,
  427. int debug_execution)
  428. {
  429. int res;
  430. struct hl_interface_s *adapter = target_to_adapter(target);
  431. struct armv7m_common *armv7m = target_to_armv7m(target);
  432. uint32_t resume_pc;
  433. struct breakpoint *breakpoint = NULL;
  434. struct reg *pc;
  435. LOG_DEBUG("%s %d 0x%08x %d %d", __func__, current, address,
  436. handle_breakpoints, debug_execution);
  437. if (target->state != TARGET_HALTED) {
  438. LOG_WARNING("target not halted");
  439. return ERROR_TARGET_NOT_HALTED;
  440. }
  441. if (!debug_execution) {
  442. target_free_all_working_areas(target);
  443. cortex_m3_enable_breakpoints(target);
  444. cortex_m3_enable_watchpoints(target);
  445. }
  446. pc = armv7m->arm.pc;
  447. if (!current) {
  448. buf_set_u32(pc->value, 0, 32, address);
  449. pc->dirty = true;
  450. pc->valid = true;
  451. }
  452. if (!breakpoint_find(target, buf_get_u32(pc->value, 0, 32))
  453. && !debug_execution) {
  454. armv7m_maybe_skip_bkpt_inst(target, NULL);
  455. }
  456. resume_pc = buf_get_u32(pc->value, 0, 32);
  457. /* write any user vector flags */
  458. res = target_write_u32(target, DCB_DEMCR, TRCENA | armv7m->demcr);
  459. if (res != ERROR_OK)
  460. return res;
  461. armv7m_restore_context(target);
  462. /* registers are now invalid */
  463. register_cache_invalidate(armv7m->arm.core_cache);
  464. /* the front-end may request us not to handle breakpoints */
  465. if (handle_breakpoints) {
  466. /* Single step past breakpoint at current address */
  467. breakpoint = breakpoint_find(target, resume_pc);
  468. if (breakpoint) {
  469. LOG_DEBUG("unset breakpoint at 0x%8.8" PRIx32 " (ID: %d)",
  470. breakpoint->address,
  471. breakpoint->unique_id);
  472. cortex_m3_unset_breakpoint(target, breakpoint);
  473. res = adapter->layout->api->step(adapter->fd);
  474. if (res != ERROR_OK)
  475. return res;
  476. cortex_m3_set_breakpoint(target, breakpoint);
  477. }
  478. }
  479. res = adapter->layout->api->run(adapter->fd);
  480. if (res != ERROR_OK)
  481. return res;
  482. target->debug_reason = DBG_REASON_NOTHALTED;
  483. if (!debug_execution) {
  484. target->state = TARGET_RUNNING;
  485. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  486. } else {
  487. target->state = TARGET_DEBUG_RUNNING;
  488. target_call_event_callbacks(target, TARGET_EVENT_DEBUG_RESUMED);
  489. }
  490. return ERROR_OK;
  491. }
  492. static int adapter_step(struct target *target, int current,
  493. uint32_t address, int handle_breakpoints)
  494. {
  495. int res;
  496. struct hl_interface_s *adapter = target_to_adapter(target);
  497. struct armv7m_common *armv7m = target_to_armv7m(target);
  498. struct breakpoint *breakpoint = NULL;
  499. struct reg *pc = armv7m->arm.pc;
  500. bool bkpt_inst_found = false;
  501. LOG_DEBUG("%s", __func__);
  502. if (target->state != TARGET_HALTED) {
  503. LOG_WARNING("target not halted");
  504. return ERROR_TARGET_NOT_HALTED;
  505. }
  506. if (!current) {
  507. buf_set_u32(pc->value, 0, 32, address);
  508. pc->dirty = true;
  509. pc->valid = true;
  510. }
  511. uint32_t pc_value = buf_get_u32(pc->value, 0, 32);
  512. /* the front-end may request us not to handle breakpoints */
  513. if (handle_breakpoints) {
  514. breakpoint = breakpoint_find(target, pc_value);
  515. if (breakpoint)
  516. cortex_m3_unset_breakpoint(target, breakpoint);
  517. }
  518. armv7m_maybe_skip_bkpt_inst(target, &bkpt_inst_found);
  519. target->debug_reason = DBG_REASON_SINGLESTEP;
  520. armv7m_restore_context(target);
  521. target_call_event_callbacks(target, TARGET_EVENT_RESUMED);
  522. res = adapter->layout->api->step(adapter->fd);
  523. if (res != ERROR_OK)
  524. return res;
  525. /* registers are now invalid */
  526. register_cache_invalidate(armv7m->arm.core_cache);
  527. if (breakpoint)
  528. cortex_m3_set_breakpoint(target, breakpoint);
  529. adapter_debug_entry(target);
  530. target_call_event_callbacks(target, TARGET_EVENT_HALTED);
  531. LOG_INFO("halted: PC: 0x%08x", buf_get_u32(armv7m->arm.pc->value, 0, 32));
  532. return ERROR_OK;
  533. }
  534. static int adapter_read_memory(struct target *target, uint32_t address,
  535. uint32_t size, uint32_t count,
  536. uint8_t *buffer)
  537. {
  538. struct hl_interface_s *adapter = target_to_adapter(target);
  539. int res;
  540. uint32_t buffer_threshold = (adapter->param.max_buffer / 4);
  541. uint32_t addr_increment = 4;
  542. uint32_t c;
  543. if (!count || !buffer)
  544. return ERROR_COMMAND_SYNTAX_ERROR;
  545. LOG_DEBUG("%s 0x%08x %d %d", __func__, address, size, count);
  546. /* prepare byte count, buffer threshold
  547. * and address increment for none 32bit access
  548. */
  549. if (size != 4) {
  550. count *= size;
  551. buffer_threshold = (adapter->param.max_buffer / 4) / 2;
  552. addr_increment = 1;
  553. }
  554. while (count) {
  555. if (count > buffer_threshold)
  556. c = buffer_threshold;
  557. else
  558. c = count;
  559. if (size != 4)
  560. res = adapter->layout->api->read_mem8(adapter->fd,
  561. address, c, buffer);
  562. else
  563. res = adapter->layout->api->read_mem32(adapter->fd,
  564. address, c, buffer);
  565. if (res != ERROR_OK)
  566. return res;
  567. address += (c * addr_increment);
  568. buffer += (c * addr_increment);
  569. count -= c;
  570. }
  571. return ERROR_OK;
  572. }
  573. static int adapter_write_memory(struct target *target, uint32_t address,
  574. uint32_t size, uint32_t count,
  575. const uint8_t *buffer)
  576. {
  577. struct hl_interface_s *adapter = target_to_adapter(target);
  578. int res;
  579. uint32_t buffer_threshold = (adapter->param.max_buffer / 4);
  580. uint32_t addr_increment = 4;
  581. uint32_t c;
  582. if (!count || !buffer)
  583. return ERROR_COMMAND_SYNTAX_ERROR;
  584. LOG_DEBUG("%s 0x%08x %d %d", __func__, address, size, count);
  585. /* prepare byte count, buffer threshold
  586. * and address increment for none 32bit access
  587. */
  588. if (size != 4) {
  589. count *= size;
  590. buffer_threshold = (adapter->param.max_buffer / 4) / 2;
  591. addr_increment = 1;
  592. }
  593. while (count) {
  594. if (count > buffer_threshold)
  595. c = buffer_threshold;
  596. else
  597. c = count;
  598. if (size != 4)
  599. res = adapter->layout->api->write_mem8(adapter->fd,
  600. address, c, buffer);
  601. else
  602. res = adapter->layout->api->write_mem32(adapter->fd,
  603. address, c, buffer);
  604. if (res != ERROR_OK)
  605. return res;
  606. address += (c * addr_increment);
  607. buffer += (c * addr_increment);
  608. count -= c;
  609. }
  610. return ERROR_OK;
  611. }
  612. static int adapter_bulk_write_memory(struct target *target,
  613. uint32_t address, uint32_t count,
  614. const uint8_t *buffer)
  615. {
  616. return adapter_write_memory(target, address, 4, count, buffer);
  617. }
  618. static const struct command_registration adapter_command_handlers[] = {
  619. {
  620. .chain = arm_command_handlers,
  621. },
  622. COMMAND_REGISTRATION_DONE
  623. };
  624. struct target_type hla_target = {
  625. .name = "hla_target",
  626. .deprecated_name = "stm32_stlink",
  627. .init_target = adapter_init_target,
  628. .target_create = adapter_target_create,
  629. .examine = cortex_m3_examine,
  630. .commands = adapter_command_handlers,
  631. .poll = adapter_poll,
  632. .arch_state = armv7m_arch_state,
  633. .assert_reset = adapter_assert_reset,
  634. .deassert_reset = adapter_deassert_reset,
  635. .soft_reset_halt = adapter_soft_reset_halt,
  636. .halt = adapter_halt,
  637. .resume = adapter_resume,
  638. .step = adapter_step,
  639. .get_gdb_reg_list = armv7m_get_gdb_reg_list,
  640. .read_memory = adapter_read_memory,
  641. .write_memory = adapter_write_memory,
  642. .bulk_write_memory = adapter_bulk_write_memory,
  643. .checksum_memory = armv7m_checksum_memory,
  644. .blank_check_memory = armv7m_blank_check_memory,
  645. .run_algorithm = armv7m_run_algorithm,
  646. .start_algorithm = armv7m_start_algorithm,
  647. .wait_algorithm = armv7m_wait_algorithm,
  648. .add_breakpoint = cortex_m3_add_breakpoint,
  649. .remove_breakpoint = cortex_m3_remove_breakpoint,
  650. .add_watchpoint = cortex_m3_add_watchpoint,
  651. .remove_watchpoint = cortex_m3_remove_watchpoint,
  652. };