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.
 
 
 
 
 
 

355 lines
8.8 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 "replacements.h"
  24. #include "avrt.h"
  25. #include "register.h"
  26. #include "target.h"
  27. #include "log.h"
  28. #include "jtag.h"
  29. #include "binarybuffer.h"
  30. #include "time_support.h"
  31. #include "breakpoints.h"
  32. #include "fileio.h"
  33. #include <stdlib.h>
  34. #include <string.h>
  35. #include <sys/types.h>
  36. #include <unistd.h>
  37. #include <errno.h>
  38. #define AVR_JTAG_INS_LEN 4
  39. /* cli handling */
  40. int avr_register_commands(struct command_context_s *cmd_ctx);
  41. /* forward declarations */
  42. int avr_target_create(struct target_s *target, Jim_Interp *interp);
  43. int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target);
  44. int avr_quit(void);
  45. int avr_arch_state(struct target_s *target);
  46. int avr_poll(target_t *target);
  47. int avr_halt(target_t *target);
  48. int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution);
  49. int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints);
  50. int avr_assert_reset(target_t *target);
  51. int avr_deassert_reset(target_t *target);
  52. int avr_soft_reset_halt(struct target_s *target);
  53. /* IR and DR functions */
  54. int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out);
  55. int avr_jtag_senddat(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int len);
  56. int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti);
  57. int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti);
  58. int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti);
  59. int mcu_write_dr_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int dr_len, int rti);
  60. int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti);
  61. int mcu_write_dr_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int dr_len, int rti);
  62. int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti);
  63. int mcu_write_dr_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int dr_len, int rti);
  64. int mcu_execute_queue(void);
  65. target_type_t avr_target =
  66. {
  67. .name = "avr",
  68. .poll = avr_poll,
  69. .arch_state = avr_arch_state,
  70. .target_request_data = NULL,
  71. .halt = avr_halt,
  72. .resume = avr_resume,
  73. .step = avr_step,
  74. .assert_reset = avr_assert_reset,
  75. .deassert_reset = avr_deassert_reset,
  76. .soft_reset_halt = avr_soft_reset_halt,
  77. /*
  78. .get_gdb_reg_list = avr_get_gdb_reg_list,
  79. .read_memory = avr_read_memory,
  80. .write_memory = avr_write_memory,
  81. .bulk_write_memory = avr_bulk_write_memory,
  82. .checksum_memory = avr_checksum_memory,
  83. .blank_check_memory = avr_blank_check_memory,
  84. .run_algorithm = avr_run_algorithm,
  85. .add_breakpoint = avr_add_breakpoint,
  86. .remove_breakpoint = avr_remove_breakpoint,
  87. .add_watchpoint = avr_add_watchpoint,
  88. .remove_watchpoint = avr_remove_watchpoint,
  89. */
  90. .register_commands = avr_register_commands,
  91. .target_create = avr_target_create,
  92. .init_target = avr_init_target,
  93. .quit = avr_quit,
  94. /*
  95. .virt2phys = avr_virt2phys,
  96. .mmu = avr_mmu
  97. */
  98. };
  99. int avr_register_commands(struct command_context_s *cmd_ctx)
  100. {
  101. LOG_DEBUG(__FUNCTION__);
  102. return ERROR_OK;
  103. }
  104. int avr_target_create(struct target_s *target, Jim_Interp *interp)
  105. {
  106. avr_common_t *avr = calloc(1, sizeof(avr_common_t));
  107. avr->jtag_info.tap = target->tap;
  108. target->arch_info = avr;
  109. return ERROR_OK;
  110. }
  111. int avr_init_target(struct command_context_s *cmd_ctx, struct target_s *target)
  112. {
  113. LOG_DEBUG(__FUNCTION__);
  114. return ERROR_OK;
  115. }
  116. int avr_quit(void)
  117. {
  118. LOG_DEBUG(__FUNCTION__);
  119. return ERROR_OK;
  120. }
  121. int avr_arch_state(struct target_s *target)
  122. {
  123. LOG_DEBUG(__FUNCTION__);
  124. return ERROR_OK;
  125. }
  126. int avr_poll(target_t *target)
  127. {
  128. if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
  129. {
  130. target->state = TARGET_HALTED;
  131. }
  132. LOG_DEBUG(__FUNCTION__);
  133. return ERROR_OK;
  134. }
  135. int avr_halt(target_t *target)
  136. {
  137. LOG_DEBUG(__FUNCTION__);
  138. return ERROR_OK;
  139. }
  140. int avr_resume(struct target_s *target, int current, u32 address, int handle_breakpoints, int debug_execution)
  141. {
  142. LOG_DEBUG(__FUNCTION__);
  143. return ERROR_OK;
  144. }
  145. int avr_step(struct target_s *target, int current, u32 address, int handle_breakpoints)
  146. {
  147. LOG_DEBUG(__FUNCTION__);
  148. return ERROR_OK;
  149. }
  150. int avr_assert_reset(target_t *target)
  151. {
  152. target->state = TARGET_RESET;
  153. LOG_DEBUG(__FUNCTION__);
  154. return ERROR_OK;
  155. }
  156. int avr_deassert_reset(target_t *target)
  157. {
  158. target->state = TARGET_RUNNING;
  159. LOG_DEBUG(__FUNCTION__);
  160. return ERROR_OK;
  161. }
  162. int avr_soft_reset_halt(struct target_s *target)
  163. {
  164. LOG_DEBUG(__FUNCTION__);
  165. return ERROR_OK;
  166. }
  167. int avr_jtag_senddat(jtag_tap_t *tap, u32* dr_in, u32 dr_out, int len)
  168. {
  169. return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
  170. }
  171. int avr_jtag_sendinstr(jtag_tap_t *tap, u8 *ir_in, u8 ir_out)
  172. {
  173. return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
  174. }
  175. /* IR and DR functions */
  176. int mcu_write_ir(jtag_tap_t *tap, u8 *ir_in, u8 *ir_out, int ir_len, int rti)
  177. {
  178. if (NULL == tap)
  179. {
  180. LOG_ERROR("invalid tap");
  181. return ERROR_FAIL;
  182. }
  183. if (ir_len != tap->ir_length)
  184. {
  185. LOG_ERROR("invalid ir_len");
  186. return ERROR_FAIL;
  187. }
  188. {
  189. scan_field_t field[1];
  190. field[0].tap = tap;
  191. field[0].num_bits = tap->ir_length;
  192. field[0].out_value = ir_out;
  193. field[0].out_mask = NULL;
  194. field[0].in_value = ir_in;
  195. field[0].in_check_value = NULL;
  196. field[0].in_check_mask = NULL;
  197. field[0].in_handler = NULL;
  198. field[0].in_handler_priv = NULL;
  199. jtag_add_plain_ir_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
  200. }
  201. return ERROR_OK;
  202. }
  203. int mcu_write_dr(jtag_tap_t *tap, u8 *dr_in, u8 *dr_out, int dr_len, int rti)
  204. {
  205. if (NULL == tap)
  206. {
  207. LOG_ERROR("invalid tap");
  208. return ERROR_FAIL;
  209. }
  210. {
  211. scan_field_t field[1];
  212. field[0].tap = tap;
  213. field[0].num_bits = dr_len;
  214. field[0].out_value = dr_out;
  215. field[0].out_mask = NULL;
  216. field[0].in_value = dr_in;
  217. field[0].in_check_value = NULL;
  218. field[0].in_check_mask = NULL;
  219. field[0].in_handler = NULL;
  220. field[0].in_handler_priv = NULL;
  221. jtag_add_plain_dr_scan(sizeof(field) / sizeof(field[0]), field, TAP_IDLE);
  222. }
  223. return ERROR_OK;
  224. }
  225. int mcu_write_ir_u8(jtag_tap_t *tap, u8 *ir_in, u8 ir_out, int ir_len, int rti)
  226. {
  227. if (ir_len > 8)
  228. {
  229. LOG_ERROR("ir_len overflow, maxium is 8");
  230. return ERROR_FAIL;
  231. }
  232. mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
  233. return ERROR_OK;
  234. }
  235. int mcu_write_dr_u8(jtag_tap_t *tap, u8 *dr_in, u8 dr_out, int dr_len, int rti)
  236. {
  237. if (dr_len > 8)
  238. {
  239. LOG_ERROR("dr_len overflow, maxium is 8");
  240. return ERROR_FAIL;
  241. }
  242. mcu_write_dr(tap, dr_in, &dr_out, dr_len, rti);
  243. return ERROR_OK;
  244. }
  245. int mcu_write_ir_u16(jtag_tap_t *tap, u16 *ir_in, u16 ir_out, int ir_len, int rti)
  246. {
  247. if (ir_len > 16)
  248. {
  249. LOG_ERROR("ir_len overflow, maxium is 16");
  250. return ERROR_FAIL;
  251. }
  252. mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
  253. return ERROR_OK;
  254. }
  255. int mcu_write_dr_u16(jtag_tap_t *tap, u16 *dr_in, u16 dr_out, int dr_len, int rti)
  256. {
  257. if (dr_len > 16)
  258. {
  259. LOG_ERROR("dr_len overflow, maxium is 16");
  260. return ERROR_FAIL;
  261. }
  262. mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
  263. return ERROR_OK;
  264. }
  265. int mcu_write_ir_u32(jtag_tap_t *tap, u32 *ir_in, u32 ir_out, int ir_len, int rti)
  266. {
  267. if (ir_len > 32)
  268. {
  269. LOG_ERROR("ir_len overflow, maxium is 32");
  270. return ERROR_FAIL;
  271. }
  272. mcu_write_ir(tap, (u8*)ir_in, (u8*)&ir_out, ir_len, rti);
  273. return ERROR_OK;
  274. }
  275. int mcu_write_dr_u32(jtag_tap_t *tap, u32 *dr_in, u32 dr_out, int dr_len, int rti)
  276. {
  277. if (dr_len > 32)
  278. {
  279. LOG_ERROR("dr_len overflow, maxium is 32");
  280. return ERROR_FAIL;
  281. }
  282. mcu_write_dr(tap, (u8*)dr_in, (u8*)&dr_out, dr_len, rti);
  283. return ERROR_OK;
  284. }
  285. int mcu_execute_queue(void)
  286. {
  287. return jtag_execute_queue();
  288. }