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.
 
 
 
 
 
 

308 lines
8.3 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2009 by Simon Qian *
  3. * SimonQian@SimonQian.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  19. ***************************************************************************/
  20. #ifdef HAVE_CONFIG_H
  21. #include "config.h"
  22. #endif
  23. #include "avrt.h"
  24. #include "target.h"
  25. #include "target_type.h"
  26. #define AVR_JTAG_INS_LEN 4
  27. /* forward declarations */
  28. int avr_target_create(struct target *target, Jim_Interp *interp);
  29. int avr_init_target(struct command_context *cmd_ctx, struct target *target);
  30. int avr_arch_state(struct target *target);
  31. int avr_poll(struct target *target);
  32. int avr_halt(struct target *target);
  33. int avr_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution);
  34. int avr_step(struct target *target, int current, uint32_t address, int handle_breakpoints);
  35. int avr_assert_reset(struct target *target);
  36. int avr_deassert_reset(struct target *target);
  37. int avr_soft_reset_halt(struct target *target);
  38. /* IR and DR functions */
  39. int avr_jtag_sendinstr(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out);
  40. int avr_jtag_senddat(struct jtag_tap *tap, uint32_t *dr_in, uint32_t dr_out, int len);
  41. int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti);
  42. int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti);
  43. int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti);
  44. int mcu_write_dr_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int dr_len, int rti);
  45. int mcu_write_ir_u16(struct jtag_tap *tap, uint16_t *ir_in, uint16_t ir_out, int ir_len, int rti);
  46. int mcu_write_dr_u16(struct jtag_tap *tap, uint16_t *ir_in, uint16_t ir_out, int dr_len, int rti);
  47. int mcu_write_ir_u32(struct jtag_tap *tap, uint32_t *ir_in, uint32_t ir_out, int ir_len, int rti);
  48. int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *ir_in, uint32_t ir_out, int dr_len, int rti);
  49. int mcu_execute_queue(void);
  50. struct target_type avr_target =
  51. {
  52. .name = "avr",
  53. .poll = avr_poll,
  54. .arch_state = avr_arch_state,
  55. .target_request_data = NULL,
  56. .halt = avr_halt,
  57. .resume = avr_resume,
  58. .step = avr_step,
  59. .assert_reset = avr_assert_reset,
  60. .deassert_reset = avr_deassert_reset,
  61. .soft_reset_halt = avr_soft_reset_halt,
  62. /*
  63. .get_gdb_reg_list = avr_get_gdb_reg_list,
  64. .read_memory = avr_read_memory,
  65. .write_memory = avr_write_memory,
  66. .bulk_write_memory = avr_bulk_write_memory,
  67. .checksum_memory = avr_checksum_memory,
  68. .blank_check_memory = avr_blank_check_memory,
  69. .run_algorithm = avr_run_algorithm,
  70. .add_breakpoint = avr_add_breakpoint,
  71. .remove_breakpoint = avr_remove_breakpoint,
  72. .add_watchpoint = avr_add_watchpoint,
  73. .remove_watchpoint = avr_remove_watchpoint,
  74. */
  75. .target_create = avr_target_create,
  76. .init_target = avr_init_target,
  77. };
  78. int avr_target_create(struct target *target, Jim_Interp *interp)
  79. {
  80. struct avr_common *avr = calloc(1, sizeof(struct avr_common));
  81. avr->jtag_info.tap = target->tap;
  82. target->arch_info = avr;
  83. return ERROR_OK;
  84. }
  85. int avr_init_target(struct command_context *cmd_ctx, struct target *target)
  86. {
  87. LOG_DEBUG("%s", __FUNCTION__);
  88. return ERROR_OK;
  89. }
  90. int avr_arch_state(struct target *target)
  91. {
  92. LOG_DEBUG("%s", __FUNCTION__);
  93. return ERROR_OK;
  94. }
  95. int avr_poll(struct target *target)
  96. {
  97. if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
  98. {
  99. target->state = TARGET_HALTED;
  100. }
  101. LOG_DEBUG("%s", __FUNCTION__);
  102. return ERROR_OK;
  103. }
  104. int avr_halt(struct target *target)
  105. {
  106. LOG_DEBUG("%s", __FUNCTION__);
  107. return ERROR_OK;
  108. }
  109. int avr_resume(struct target *target, int current, uint32_t address, int handle_breakpoints, int debug_execution)
  110. {
  111. LOG_DEBUG("%s", __FUNCTION__);
  112. return ERROR_OK;
  113. }
  114. int avr_step(struct target *target, int current, uint32_t address, int handle_breakpoints)
  115. {
  116. LOG_DEBUG("%s", __FUNCTION__);
  117. return ERROR_OK;
  118. }
  119. int avr_assert_reset(struct target *target)
  120. {
  121. target->state = TARGET_RESET;
  122. LOG_DEBUG("%s", __FUNCTION__);
  123. return ERROR_OK;
  124. }
  125. int avr_deassert_reset(struct target *target)
  126. {
  127. target->state = TARGET_RUNNING;
  128. LOG_DEBUG("%s", __FUNCTION__);
  129. return ERROR_OK;
  130. }
  131. int avr_soft_reset_halt(struct target *target)
  132. {
  133. LOG_DEBUG("%s", __FUNCTION__);
  134. return ERROR_OK;
  135. }
  136. int avr_jtag_senddat(struct jtag_tap *tap, uint32_t* dr_in, uint32_t dr_out, int len)
  137. {
  138. return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
  139. }
  140. int avr_jtag_sendinstr(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out)
  141. {
  142. return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
  143. }
  144. /* IR and DR functions */
  145. int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti)
  146. {
  147. if (NULL == tap)
  148. {
  149. LOG_ERROR("invalid tap");
  150. return ERROR_FAIL;
  151. }
  152. if (ir_len != tap->ir_length)
  153. {
  154. LOG_ERROR("invalid ir_len");
  155. return ERROR_FAIL;
  156. }
  157. {
  158. struct scan_field field[1];
  159. field[0].tap = tap;
  160. field[0].num_bits = tap->ir_length;
  161. field[0].out_value = ir_out;
  162. field[0].in_value = ir_in;
  163. jtag_add_plain_ir_scan(ARRAY_SIZE(field), field, jtag_set_end_state(TAP_IDLE));
  164. }
  165. return ERROR_OK;
  166. }
  167. int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti)
  168. {
  169. if (NULL == tap)
  170. {
  171. LOG_ERROR("invalid tap");
  172. return ERROR_FAIL;
  173. }
  174. {
  175. struct scan_field field[1];
  176. field[0].tap = tap;
  177. field[0].num_bits = dr_len;
  178. field[0].out_value = dr_out;
  179. field[0].in_value = dr_in;
  180. jtag_add_plain_dr_scan(ARRAY_SIZE(field), field, jtag_set_end_state(TAP_IDLE));
  181. }
  182. return ERROR_OK;
  183. }
  184. int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti)
  185. {
  186. if (ir_len > 8)
  187. {
  188. LOG_ERROR("ir_len overflow, maxium is 8");
  189. return ERROR_FAIL;
  190. }
  191. mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
  192. return ERROR_OK;
  193. }
  194. int mcu_write_dr_u8(struct jtag_tap *tap, uint8_t *dr_in, uint8_t dr_out, int dr_len, int rti)
  195. {
  196. if (dr_len > 8)
  197. {
  198. LOG_ERROR("dr_len overflow, maxium is 8");
  199. return ERROR_FAIL;
  200. }
  201. mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);
  202. return ERROR_OK;
  203. }
  204. int mcu_write_ir_u16(struct jtag_tap *tap, uint16_t *ir_in, uint16_t ir_out, int ir_len, int rti)
  205. {
  206. if (ir_len > 16)
  207. {
  208. LOG_ERROR("ir_len overflow, maxium is 16");
  209. return ERROR_FAIL;
  210. }
  211. mcu_write_ir(tap, (uint8_t*)ir_in, (uint8_t*)&ir_out, ir_len, rti);
  212. return ERROR_OK;
  213. }
  214. int mcu_write_dr_u16(struct jtag_tap *tap, uint16_t *dr_in, uint16_t dr_out, int dr_len, int rti)
  215. {
  216. if (dr_len > 16)
  217. {
  218. LOG_ERROR("dr_len overflow, maxium is 16");
  219. return ERROR_FAIL;
  220. }
  221. mcu_write_dr(tap, (uint8_t*)dr_in, (uint8_t*)&dr_out, dr_len, rti);
  222. return ERROR_OK;
  223. }
  224. int mcu_write_ir_u32(struct jtag_tap *tap, uint32_t *ir_in, uint32_t ir_out, int ir_len, int rti)
  225. {
  226. if (ir_len > 32)
  227. {
  228. LOG_ERROR("ir_len overflow, maxium is 32");
  229. return ERROR_FAIL;
  230. }
  231. mcu_write_ir(tap, (uint8_t*)ir_in, (uint8_t*)&ir_out, ir_len, rti);
  232. return ERROR_OK;
  233. }
  234. int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *dr_in, uint32_t dr_out, int dr_len, int rti)
  235. {
  236. if (dr_len > 32)
  237. {
  238. LOG_ERROR("dr_len overflow, maxium is 32");
  239. return ERROR_FAIL;
  240. }
  241. mcu_write_dr(tap, (uint8_t*)dr_in, (uint8_t*)&dr_out, dr_len, rti);
  242. return ERROR_OK;
  243. }
  244. int mcu_execute_queue(void)
  245. {
  246. return jtag_execute_queue();
  247. }