OpenOCD
pld.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /***************************************************************************
4  * Copyright (C) 2006 by Dominic Rath *
5  * Dominic.Rath@gmx.de *
6  ***************************************************************************/
7 
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 
12 #include "pld.h"
13 #include <helper/log.h>
14 #include <helper/replacements.h>
15 #include <helper/time_support.h>
16 
17 
18 /* pld drivers
19  */
20 extern struct pld_driver virtex2_pld;
21 
22 static struct pld_driver *pld_drivers[] = {
23  &virtex2_pld,
24  NULL,
25 };
26 
27 static struct pld_device *pld_devices;
28 
30 {
31  struct pld_device *p;
32  int i = 0;
33 
34  for (p = pld_devices; p; p = p->next) {
35  if (i++ == num)
36  return p;
37  }
38 
39  return NULL;
40 }
41 
42 /* pld device <driver> [driver_options ...]
43  */
44 COMMAND_HANDLER(handle_pld_device_command)
45 {
46  int i;
47  int found = 0;
48 
49  if (CMD_ARGC < 1)
51 
52  for (i = 0; pld_drivers[i]; i++) {
53  if (strcmp(CMD_ARGV[0], pld_drivers[i]->name) == 0) {
54  struct pld_device *p, *c;
55 
56  /* register pld specific commands */
57  int retval;
58  if (pld_drivers[i]->commands) {
59  retval = register_commands(CMD_CTX, NULL, pld_drivers[i]->commands);
60  if (retval != ERROR_OK) {
61  LOG_ERROR("couldn't register '%s' commands", CMD_ARGV[0]);
62  return ERROR_FAIL;
63  }
64  }
65 
66  c = malloc(sizeof(struct pld_device));
67  c->driver = pld_drivers[i];
68  c->next = NULL;
69 
70  retval = CALL_COMMAND_HANDLER(
71  pld_drivers[i]->pld_device_command, c);
72  if (retval != ERROR_OK) {
73  LOG_ERROR("'%s' driver rejected pld device",
74  CMD_ARGV[0]);
75  free(c);
76  return ERROR_OK;
77  }
78 
79  /* put pld device in linked list */
80  if (pld_devices) {
81  /* find last pld device */
82  for (p = pld_devices; p && p->next; p = p->next)
83  ;
84  if (p)
85  p->next = c;
86  } else
87  pld_devices = c;
88 
89  found = 1;
90  }
91  }
92 
93  /* no matching pld driver found */
94  if (!found) {
95  LOG_ERROR("pld driver '%s' not found", CMD_ARGV[0]);
96  exit(-1);
97  }
98 
99  return ERROR_OK;
100 }
101 
102 COMMAND_HANDLER(handle_pld_devices_command)
103 {
104  struct pld_device *p;
105  int i = 0;
106 
107  if (!pld_devices) {
108  command_print(CMD, "no pld devices configured");
109  return ERROR_OK;
110  }
111 
112  for (p = pld_devices; p; p = p->next)
113  command_print(CMD, "#%i: %s", i++, p->driver->name);
114 
115  return ERROR_OK;
116 }
117 
118 COMMAND_HANDLER(handle_pld_load_command)
119 {
120  int retval;
121  struct timeval start, end, duration;
122  struct pld_device *p;
123 
125 
126  if (CMD_ARGC < 2)
128 
129  unsigned dev_id;
130  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], dev_id);
131  p = get_pld_device_by_num(dev_id);
132  if (!p) {
133  command_print(CMD, "pld device '#%s' is out of bounds", CMD_ARGV[0]);
134  return ERROR_OK;
135  }
136 
137  retval = p->driver->load(p, CMD_ARGV[1]);
138  if (retval != ERROR_OK) {
139  command_print(CMD, "failed loading file %s to pld device %u",
140  CMD_ARGV[1], dev_id);
141  switch (retval) {
142  }
143  return retval;
144  } else {
145  gettimeofday(&end, NULL);
146  timeval_subtract(&duration, &end, &start);
147 
148  command_print(CMD, "loaded file %s to pld device %u in %jis %jius",
149  CMD_ARGV[1], dev_id,
150  (intmax_t)duration.tv_sec, (intmax_t)duration.tv_usec);
151  }
152 
153  return ERROR_OK;
154 }
155 
156 static const struct command_registration pld_exec_command_handlers[] = {
157  {
158  .name = "devices",
159  .handler = handle_pld_devices_command,
160  .mode = COMMAND_EXEC,
161  .help = "list configured pld devices",
162  .usage = "",
163  },
164  {
165  .name = "load",
166  .handler = handle_pld_load_command,
167  .mode = COMMAND_EXEC,
168  .help = "load configuration file into PLD",
169  .usage = "pld_num filename",
170  },
172 };
173 
174 static int pld_init(struct command_context *cmd_ctx)
175 {
176  if (!pld_devices)
177  return ERROR_OK;
178 
179  return register_commands(cmd_ctx, "pld", pld_exec_command_handlers);
180 }
181 
182 COMMAND_HANDLER(handle_pld_init_command)
183 {
184  if (CMD_ARGC != 0)
186 
187  static bool pld_initialized;
188  if (pld_initialized) {
189  LOG_INFO("'pld init' has already been called");
190  return ERROR_OK;
191  }
192  pld_initialized = true;
193 
194  LOG_DEBUG("Initializing PLDs...");
195  return pld_init(CMD_CTX);
196 }
197 
198 static const struct command_registration pld_config_command_handlers[] = {
199  {
200  .name = "device",
201  .mode = COMMAND_CONFIG,
202  .handler = handle_pld_device_command,
203  .help = "configure a PLD device",
204  .usage = "driver_name [driver_args ... ]",
205  },
206  {
207  .name = "init",
208  .mode = COMMAND_CONFIG,
209  .handler = handle_pld_init_command,
210  .help = "initialize PLD devices",
211  .usage = ""
212  },
214 };
215 static const struct command_registration pld_command_handler[] = {
216  {
217  .name = "pld",
218  .mode = COMMAND_ANY,
219  .help = "programmable logic device commands",
220  .usage = "",
222  },
224 };
226 {
227  return register_commands(cmd_ctx, NULL, pld_command_handler);
228 }
const char * name
Definition: armv4_5.c:76
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:473
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:140
#define CALL_COMMAND_HANDLER(name, extra ...)
Use this to macro to call a command helper (or a nested handler).
Definition: command.h:117
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:155
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:385
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:150
#define COMMAND_PARSE_NUMBER(type, in, out)
parses the string in into out as a type, or prints a command error and passes the error code to the c...
Definition: command.h:425
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:145
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:247
static int register_commands(struct command_context *cmd_ctx, const char *cmd_prefix, const struct command_registration *cmds)
Register one or more commands in the specified context, as children of parent (or top-level commends,...
Definition: command.h:268
@ COMMAND_CONFIG
Definition: command.h:41
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
static int64_t start
Definition: log.c:41
#define ERROR_FAIL
Definition: log.h:161
#define LOG_ERROR(expr ...)
Definition: log.h:123
#define LOG_INFO(expr ...)
Definition: log.h:117
#define LOG_DEBUG(expr ...)
Definition: log.h:109
#define ERROR_OK
Definition: log.h:155
struct pld_device * get_pld_device_by_num(int num)
Definition: pld.c:29
static int pld_init(struct command_context *cmd_ctx)
Definition: pld.c:174
static const struct command_registration pld_config_command_handlers[]
Definition: pld.c:198
COMMAND_HANDLER(handle_pld_device_command)
Definition: pld.c:44
static const struct command_registration pld_exec_command_handlers[]
Definition: pld.c:156
static const struct command_registration pld_command_handler[]
Definition: pld.c:215
struct pld_driver virtex2_pld
Definition: virtex2.c:236
static struct pld_driver * pld_drivers[]
Definition: pld.c:22
static struct pld_device * pld_devices
Definition: pld.c:27
int pld_register_commands(struct command_context *cmd_ctx)
Definition: pld.c:225
int gettimeofday(struct timeval *tv, struct timezone *tz)
const char * name
Definition: command.h:229
Definition: pld.h:28
struct pld_device * next
Definition: pld.h:31
struct pld_driver * driver
Definition: pld.h:29
Definition: pld.h:18
int(* load)(struct pld_device *pld_device, const char *filename)
Definition: pld.h:22
const char * name
Definition: pld.h:19
int timeval_subtract(struct timeval *result, struct timeval *x, struct timeval *y)
Definition: time_support.c:21
#define NULL
Definition: usb.h:16