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.
 
 
 
 
 
 

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