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.
 
 
 
 
 
 

224 lines
6.1 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, see <http://www.gnu.org/licenses/>. *
  17. ***************************************************************************/
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21. #include "avrt.h"
  22. #include "target.h"
  23. #include "target_type.h"
  24. #define AVR_JTAG_INS_LEN 4
  25. /* forward declarations */
  26. static int avr_target_create(struct target *target, Jim_Interp *interp);
  27. static int avr_init_target(struct command_context *cmd_ctx, struct target *target);
  28. static int avr_arch_state(struct target *target);
  29. static int avr_poll(struct target *target);
  30. static int avr_halt(struct target *target);
  31. static int avr_resume(struct target *target, int current, target_addr_t address,
  32. int handle_breakpoints, int debug_execution);
  33. static int avr_step(struct target *target, int current, target_addr_t address,
  34. int handle_breakpoints);
  35. static int avr_assert_reset(struct target *target);
  36. static int avr_deassert_reset(struct target *target);
  37. /* IR and DR functions */
  38. static int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out, int ir_len, int rti);
  39. static int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out, int dr_len, int rti);
  40. static int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out, int ir_len, int rti);
  41. static int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *ir_in, uint32_t ir_out, int dr_len, int rti);
  42. struct target_type avr_target = {
  43. .name = "avr",
  44. .poll = avr_poll,
  45. .arch_state = avr_arch_state,
  46. .halt = avr_halt,
  47. .resume = avr_resume,
  48. .step = avr_step,
  49. .assert_reset = avr_assert_reset,
  50. .deassert_reset = avr_deassert_reset,
  51. /*
  52. .get_gdb_reg_list = avr_get_gdb_reg_list,
  53. .read_memory = avr_read_memory,
  54. .write_memory = avr_write_memory,
  55. .bulk_write_memory = avr_bulk_write_memory,
  56. .checksum_memory = avr_checksum_memory,
  57. .blank_check_memory = avr_blank_check_memory,
  58. .run_algorithm = avr_run_algorithm,
  59. .add_breakpoint = avr_add_breakpoint,
  60. .remove_breakpoint = avr_remove_breakpoint,
  61. .add_watchpoint = avr_add_watchpoint,
  62. .remove_watchpoint = avr_remove_watchpoint,
  63. */
  64. .target_create = avr_target_create,
  65. .init_target = avr_init_target,
  66. };
  67. static int avr_target_create(struct target *target, Jim_Interp *interp)
  68. {
  69. struct avr_common *avr = calloc(1, sizeof(struct avr_common));
  70. avr->jtag_info.tap = target->tap;
  71. target->arch_info = avr;
  72. return ERROR_OK;
  73. }
  74. static int avr_init_target(struct command_context *cmd_ctx, struct target *target)
  75. {
  76. LOG_DEBUG("%s", __func__);
  77. return ERROR_OK;
  78. }
  79. static int avr_arch_state(struct target *target)
  80. {
  81. LOG_DEBUG("%s", __func__);
  82. return ERROR_OK;
  83. }
  84. static int avr_poll(struct target *target)
  85. {
  86. if ((target->state == TARGET_RUNNING) || (target->state == TARGET_DEBUG_RUNNING))
  87. target->state = TARGET_HALTED;
  88. LOG_DEBUG("%s", __func__);
  89. return ERROR_OK;
  90. }
  91. static int avr_halt(struct target *target)
  92. {
  93. LOG_DEBUG("%s", __func__);
  94. return ERROR_OK;
  95. }
  96. static int avr_resume(struct target *target, int current, target_addr_t address,
  97. int handle_breakpoints, int debug_execution)
  98. {
  99. LOG_DEBUG("%s", __func__);
  100. return ERROR_OK;
  101. }
  102. static int avr_step(struct target *target, int current, target_addr_t address, int handle_breakpoints)
  103. {
  104. LOG_DEBUG("%s", __func__);
  105. return ERROR_OK;
  106. }
  107. static int avr_assert_reset(struct target *target)
  108. {
  109. target->state = TARGET_RESET;
  110. LOG_DEBUG("%s", __func__);
  111. return ERROR_OK;
  112. }
  113. static int avr_deassert_reset(struct target *target)
  114. {
  115. target->state = TARGET_RUNNING;
  116. LOG_DEBUG("%s", __func__);
  117. return ERROR_OK;
  118. }
  119. int avr_jtag_senddat(struct jtag_tap *tap, uint32_t *dr_in, uint32_t dr_out,
  120. int len)
  121. {
  122. return mcu_write_dr_u32(tap, dr_in, dr_out, len, 1);
  123. }
  124. int avr_jtag_sendinstr(struct jtag_tap *tap, uint8_t *ir_in, uint8_t ir_out)
  125. {
  126. return mcu_write_ir_u8(tap, ir_in, ir_out, AVR_JTAG_INS_LEN, 1);
  127. }
  128. /* IR and DR functions */
  129. static int mcu_write_ir(struct jtag_tap *tap, uint8_t *ir_in, uint8_t *ir_out,
  130. int ir_len, int rti)
  131. {
  132. if (!tap) {
  133. LOG_ERROR("invalid tap");
  134. return ERROR_FAIL;
  135. }
  136. if (ir_len != tap->ir_length) {
  137. LOG_ERROR("invalid ir_len");
  138. return ERROR_FAIL;
  139. }
  140. {
  141. jtag_add_plain_ir_scan(tap->ir_length, ir_out, ir_in, TAP_IDLE);
  142. }
  143. return ERROR_OK;
  144. }
  145. static int mcu_write_dr(struct jtag_tap *tap, uint8_t *dr_in, uint8_t *dr_out,
  146. int dr_len, int rti)
  147. {
  148. if (!tap) {
  149. LOG_ERROR("invalid tap");
  150. return ERROR_FAIL;
  151. }
  152. {
  153. jtag_add_plain_dr_scan(dr_len, dr_out, dr_in, TAP_IDLE);
  154. }
  155. return ERROR_OK;
  156. }
  157. static int mcu_write_ir_u8(struct jtag_tap *tap, uint8_t *ir_in,
  158. uint8_t ir_out, int ir_len, int rti)
  159. {
  160. if (ir_len > 8) {
  161. LOG_ERROR("ir_len overflow, maximum is 8");
  162. return ERROR_FAIL;
  163. }
  164. mcu_write_ir(tap, ir_in, &ir_out, ir_len, rti);
  165. return ERROR_OK;
  166. }
  167. static int mcu_write_dr_u32(struct jtag_tap *tap, uint32_t *dr_in,
  168. uint32_t dr_out, int dr_len, int rti)
  169. {
  170. if (dr_len > 32) {
  171. LOG_ERROR("dr_len overflow, maximum is 32");
  172. return ERROR_FAIL;
  173. }
  174. mcu_write_dr(tap, (uint8_t *)dr_in, (uint8_t *)&dr_out, dr_len, rti);
  175. return ERROR_OK;
  176. }
  177. int mcu_execute_queue(void)
  178. {
  179. return jtag_execute_queue();
  180. }