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.
 
 
 
 
 
 

226 lines
6.1 KiB

  1. /***************************************************************************
  2. * Copyright (C) 2011 by Mathias Kuester *
  3. * Mathias Kuester <kesmtp@freenet.de> *
  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. /* project specific includes */
  24. #include <jtag/interface.h>
  25. #include <transport/transport.h>
  26. #include <helper/time_support.h>
  27. #include <jtag/stlink/stlink_tcl.h>
  28. #include <jtag/stlink/stlink_layout.h>
  29. #include <jtag/stlink/stlink_interface.h>
  30. #include <target/target.h>
  31. static struct stlink_interface_s stlink_if = { {0, 0, 0, 0}, 0, 0 };
  32. int stlink_interface_open(void)
  33. {
  34. LOG_DEBUG("stlink_interface_open");
  35. return stlink_if.layout->open(&stlink_if);
  36. }
  37. int stlink_interface_init_target(struct target *t)
  38. {
  39. int res;
  40. /* this is the interface for the current target and we
  41. * can setup the private pointer in the tap structure
  42. * if the interface match the tap idcode
  43. */
  44. res = stlink_if.layout->api->idcode(stlink_if.fd, &t->tap->idcode);
  45. if (res != ERROR_OK)
  46. return res;
  47. unsigned ii, limit = t->tap->expected_ids_cnt;
  48. int found = 0;
  49. for (ii = 0; ii < limit; ii++) {
  50. uint32_t expected = t->tap->expected_ids[ii];
  51. if (t->tap->idcode == expected) {
  52. found = 1;
  53. break;
  54. }
  55. }
  56. if (found == 0) {
  57. LOG_ERROR
  58. ("stlink_interface_init_target: target not found: idcode: %x ",
  59. t->tap->idcode);
  60. return ERROR_FAIL;
  61. }
  62. t->tap->priv = &stlink_if;
  63. t->tap->hasidcode = 1;
  64. return ERROR_OK;
  65. }
  66. static int stlink_interface_init(void)
  67. {
  68. LOG_DEBUG("stlink_interface_init");
  69. /* here we can initialize the layout */
  70. return stlink_layout_init(&stlink_if);
  71. }
  72. static int stlink_interface_quit(void)
  73. {
  74. LOG_DEBUG("stlink_interface_quit");
  75. return ERROR_OK;
  76. }
  77. static int stlink_interface_speed(int speed)
  78. {
  79. LOG_DEBUG("stlink_interface_speed: ignore speed %d", speed);
  80. return ERROR_OK;
  81. }
  82. static int stlink_interface_execute_queue(void)
  83. {
  84. LOG_DEBUG("stlink_interface_execute_queue: ignored");
  85. return ERROR_OK;
  86. }
  87. COMMAND_HANDLER(stlink_interface_handle_device_desc_command)
  88. {
  89. LOG_DEBUG("stlink_interface_handle_device_desc_command");
  90. if (CMD_ARGC == 1) {
  91. stlink_if.param.device_desc = strdup(CMD_ARGV[0]);
  92. } else {
  93. LOG_ERROR
  94. ("expected exactly one argument to stlink_device_desc <description>");
  95. }
  96. return ERROR_OK;
  97. }
  98. COMMAND_HANDLER(stlink_interface_handle_serial_command)
  99. {
  100. LOG_DEBUG("stlink_interface_handle_serial_command");
  101. if (CMD_ARGC == 1) {
  102. stlink_if.param.serial = strdup(CMD_ARGV[0]);
  103. } else {
  104. LOG_ERROR
  105. ("expected exactly one argument to stlink_serial <serial-number>");
  106. }
  107. return ERROR_OK;
  108. }
  109. COMMAND_HANDLER(stlink_interface_handle_layout_command)
  110. {
  111. LOG_DEBUG("stlink_interface_handle_layout_command");
  112. if (CMD_ARGC != 1) {
  113. LOG_ERROR("Need exactly one argument to stlink_layout");
  114. return ERROR_COMMAND_SYNTAX_ERROR;
  115. }
  116. if (stlink_if.layout) {
  117. LOG_ERROR("already specified stlink_layout %s",
  118. stlink_if.layout->name);
  119. return (strcmp(stlink_if.layout->name, CMD_ARGV[0]) != 0)
  120. ? ERROR_FAIL : ERROR_OK;
  121. }
  122. for (const struct stlink_layout *l = stlink_layout_get_list(); l->name;
  123. l++) {
  124. if (strcmp(l->name, CMD_ARGV[0]) == 0) {
  125. stlink_if.layout = l;
  126. return ERROR_OK;
  127. }
  128. }
  129. LOG_ERROR("No STLINK layout '%s' found", CMD_ARGV[0]);
  130. return ERROR_FAIL;
  131. }
  132. COMMAND_HANDLER(stlink_interface_handle_vid_pid_command)
  133. {
  134. LOG_DEBUG("stlink_interface_handle_vid_pid_command");
  135. if (CMD_ARGC != 2) {
  136. LOG_WARNING
  137. ("ignoring extra IDs in stlink_vid_pid (maximum is 1 pair)");
  138. return ERROR_COMMAND_SYNTAX_ERROR;
  139. }
  140. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[0], stlink_if.param.vid);
  141. COMMAND_PARSE_NUMBER(u16, CMD_ARGV[1], stlink_if.param.pid);
  142. return ERROR_OK;
  143. }
  144. static const struct command_registration stlink_interface_command_handlers[] = {
  145. {
  146. .name = "stlink_device_desc",
  147. .handler = &stlink_interface_handle_device_desc_command,
  148. .mode = COMMAND_CONFIG,
  149. .help = "set the stlink device description of the STLINK device",
  150. .usage = "description_string",
  151. },
  152. {
  153. .name = "stlink_serial",
  154. .handler = &stlink_interface_handle_serial_command,
  155. .mode = COMMAND_CONFIG,
  156. .help = "set the serial number of the STLINK device",
  157. .usage = "serial_string",
  158. },
  159. {
  160. .name = "stlink_layout",
  161. .handler = &stlink_interface_handle_layout_command,
  162. .mode = COMMAND_CONFIG,
  163. .help = "set the layout of the STLINK to usb or sg",
  164. .usage = "layout_name",
  165. },
  166. {
  167. .name = "stlink_vid_pid",
  168. .handler = &stlink_interface_handle_vid_pid_command,
  169. .mode = COMMAND_CONFIG,
  170. .help = "the vendor and product ID of the STLINK device",
  171. .usage = "(vid pid)* ",
  172. },
  173. COMMAND_REGISTRATION_DONE
  174. };
  175. struct jtag_interface stlink_interface = {
  176. .name = "stlink",
  177. .supported = 0,
  178. .commands = stlink_interface_command_handlers,
  179. .transports = stlink_transports,
  180. .init = stlink_interface_init,
  181. .quit = stlink_interface_quit,
  182. .speed = stlink_interface_speed,
  183. .execute_queue = stlink_interface_execute_queue,
  184. };