OpenOCD
rtt/tcl.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
3 /*
4  * Copyright (C) 2019-2020 by Marc Schink <dev@zapb.de>
5  */
6 
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10 
11 #include <helper/log.h>
12 #include <target/rtt.h>
13 
14 #include "rtt.h"
15 
16 #define CHANNEL_NAME_SIZE 128
17 
18 COMMAND_HANDLER(handle_rtt_setup_command)
19 {
20  struct rtt_source source;
21 
22  const char *DEFAULT_ID = "SEGGER RTT";
23  const char *selected_id;
24  if (CMD_ARGC < 2 || CMD_ARGC > 3)
26  if (CMD_ARGC == 2)
27  selected_id = DEFAULT_ID;
28  else
29  selected_id = CMD_ARGV[2];
30 
38 
39  target_addr_t address;
40  uint32_t size;
41 
42  COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[0], address);
44 
46 
47  if (rtt_setup(address, size, selected_id) != ERROR_OK)
48  return ERROR_FAIL;
49 
50  return ERROR_OK;
51 }
52 
53 COMMAND_HANDLER(handle_rtt_start_command)
54 {
55  if (CMD_ARGC > 0)
57 
58  if (!rtt_configured()) {
59  command_print(CMD, "RTT is not configured");
60  return ERROR_FAIL;
61  }
62 
63  return rtt_start();
64 }
65 
66 COMMAND_HANDLER(handle_rtt_stop_command)
67 {
68  if (CMD_ARGC > 0)
70 
71  return rtt_stop();
72 }
73 
74 COMMAND_HANDLER(handle_rtt_polling_interval_command)
75 {
76  if (CMD_ARGC == 0) {
77  int ret;
78  unsigned int interval;
79 
80  ret = rtt_get_polling_interval(&interval);
81 
82  if (ret != ERROR_OK) {
83  command_print(CMD, "Failed to get polling interval");
84  return ret;
85  }
86 
87  command_print(CMD, "%u ms", interval);
88  } else if (CMD_ARGC == 1) {
89  int ret;
90  unsigned int interval;
91 
92  COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], interval);
93  ret = rtt_set_polling_interval(interval);
94 
95  if (ret != ERROR_OK) {
96  command_print(CMD, "Failed to set polling interval");
97  return ret;
98  }
99  } else {
101  }
102 
103  return ERROR_OK;
104 }
105 
106 COMMAND_HANDLER(handle_rtt_channels_command)
107 {
108  int ret;
109  char channel_name[CHANNEL_NAME_SIZE];
110  const struct rtt_control *ctrl;
111  struct rtt_channel_info info;
112 
113  if (!rtt_found_cb()) {
114  command_print(CMD, "rtt: Control block not available");
115  return ERROR_FAIL;
116  }
117 
118  ctrl = rtt_get_control();
119 
120  command_print(CMD, "Channels: up=%u, down=%u", ctrl->num_up_channels,
122 
123  command_print(CMD, "Up-channels:");
124 
125  info.name = channel_name;
126  info.name_length = sizeof(channel_name);
127 
128  for (unsigned int i = 0; i < ctrl->num_up_channels; i++) {
130 
131  if (ret != ERROR_OK)
132  return ret;
133 
134  if (!info.size)
135  continue;
136 
137  command_print(CMD, "%u: %s %u %u", i, info.name, info.size,
138  info.flags);
139  }
140 
141  command_print(CMD, "Down-channels:");
142 
143  for (unsigned int i = 0; i < ctrl->num_down_channels; i++) {
145 
146  if (ret != ERROR_OK)
147  return ret;
148 
149  if (!info.size)
150  continue;
151 
152  command_print(CMD, "%u: %s %u %u", i, info.name, info.size,
153  info.flags);
154  }
155 
156  return ERROR_OK;
157 }
158 
159 COMMAND_HANDLER(handle_channel_list)
160 {
161  char channel_name[CHANNEL_NAME_SIZE];
162  const struct rtt_control *ctrl;
163  struct rtt_channel_info info;
164 
165  if (CMD_ARGC != 0)
167 
168  if (!rtt_found_cb()) {
169  command_print(CMD, "rtt: Control block not available");
170  return ERROR_FAIL;
171  }
172 
173  ctrl = rtt_get_control();
174 
175  info.name = channel_name;
176  info.name_length = sizeof(channel_name);
177 
178  command_print(CMD, "{");
179 
180  for (unsigned int i = 0; i < ctrl->num_up_channels; i++) {
182  if (ret != ERROR_OK)
183  return ret;
184 
185  if (!info.size)
186  continue;
187 
189  " {\n"
190  " name %s\n"
191  " size 0x%" PRIx32 "\n"
192  " flags 0x%" PRIx32 "\n"
193  " }",
194  info.name, info.size, info.flags);
195  }
196 
197  command_print(CMD, "}\n{");
198 
199  for (unsigned int i = 0; i < ctrl->num_down_channels; i++) {
201  if (ret != ERROR_OK)
202  return ret;
203 
204  if (!info.size)
205  continue;
206 
208  " {\n"
209  " name %s\n"
210  " size 0x%" PRIx32 "\n"
211  " flags 0x%" PRIx32 "\n"
212  " }",
213  info.name, info.size, info.flags);
214  }
215 
216  command_print(CMD, "}");
217 
218  return ERROR_OK;
219 }
220 
221 static const struct command_registration rtt_subcommand_handlers[] = {
222  {
223  .name = "setup",
224  .handler = handle_rtt_setup_command,
225  .mode = COMMAND_ANY,
226  .help = "setup RTT",
227  .usage = "<address> <size> [ID]"
228  },
229  {
230  .name = "start",
231  .handler = handle_rtt_start_command,
232  .mode = COMMAND_EXEC,
233  .help = "start RTT",
234  .usage = ""
235  },
236  {
237  .name = "stop",
238  .handler = handle_rtt_stop_command,
239  .mode = COMMAND_EXEC,
240  .help = "stop RTT",
241  .usage = ""
242  },
243  {
244  .name = "polling_interval",
245  .handler = handle_rtt_polling_interval_command,
246  .mode = COMMAND_EXEC,
247  .help = "show or set polling interval in ms",
248  .usage = "[interval]"
249  },
250  {
251  .name = "channels",
252  .handler = handle_rtt_channels_command,
253  .mode = COMMAND_EXEC,
254  .help = "list available channels",
255  .usage = ""
256  },
257  {
258  .name = "channellist",
259  .handler = handle_channel_list,
260  .mode = COMMAND_EXEC,
261  .help = "list available channels",
262  .usage = ""
263  },
265 };
266 
268  {
269  .name = "rtt",
270  .mode = COMMAND_EXEC,
271  .help = "RTT target commands",
272  .usage = "",
273  .chain = rtt_subcommand_handlers
274  },
276 };
void command_print(struct command_invocation *cmd, const char *format,...)
Definition: command.c:443
#define CMD
Use this macro to access the command being handled, rather than accessing the variable directly.
Definition: command.h:141
#define CMD_ARGV
Use this macro to access the arguments for the command being handled, rather than accessing the varia...
Definition: command.h:156
#define ERROR_COMMAND_SYNTAX_ERROR
Definition: command.h:402
#define CMD_ARGC
Use this macro to access the number of arguments for the command being handled, rather than accessing...
Definition: command.h:151
#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:442
#define CMD_CTX
Use this macro to access the context of the command being handled, rather than accessing the variable...
Definition: command.h:146
#define COMMAND_REGISTRATION_DONE
Use this as the last entry in an array of command_registration records.
Definition: command.h:253
@ COMMAND_ANY
Definition: command.h:42
@ COMMAND_EXEC
Definition: command.h:40
#define ERROR_FAIL
Definition: log.h:170
#define ERROR_OK
Definition: log.h:164
int rtt_stop(void)
Stop Real-Time Transfer (RTT).
Definition: rtt/rtt.c:165
int rtt_start(void)
Start Real-Time Transfer (RTT).
Definition: rtt/rtt.c:124
struct rtt_control ctrl
Control block.
Definition: rtt/rtt.c:25
bool rtt_configured(void)
Get whether RTT is configured.
Definition: rtt/rtt.c:305
bool rtt_found_cb(void)
Get whether RTT control block was found.
Definition: rtt/rtt.c:310
int rtt_set_polling_interval(unsigned int interval)
Set the polling interval.
Definition: rtt/rtt.c:272
size_t size
Size of the control block search area.
Definition: rtt/rtt.c:30
int rtt_register_source(const struct rtt_source source, struct target *target)
Register an RTT source for a target.
Definition: rtt/rtt.c:106
const struct rtt_control * rtt_get_control(void)
Get the RTT control block.
Definition: rtt/rtt.c:315
int rtt_get_polling_interval(unsigned int *interval)
Get the polling interval.
Definition: rtt/rtt.c:262
int rtt_setup(target_addr_t address, size_t size, const char *id)
Setup RTT.
Definition: rtt/rtt.c:88
int rtt_read_channel_info(unsigned int channel_index, enum rtt_channel_type type, struct rtt_channel_info *info)
Read channel information.
Definition: rtt/rtt.c:320
struct rtt_source source
Definition: rtt/rtt.c:23
@ RTT_CHANNEL_TYPE_UP
Up channel (target to host).
Definition: rtt/rtt.h:93
@ RTT_CHANNEL_TYPE_DOWN
Down channel (host to target).
Definition: rtt/rtt.h:95
static const struct command_registration rtt_subcommand_handlers[]
Definition: rtt/tcl.c:221
COMMAND_HANDLER(handle_rtt_setup_command)
Definition: rtt/tcl.c:18
#define CHANNEL_NAME_SIZE
Definition: rtt/tcl.c:16
const struct command_registration rtt_target_command_handlers[]
Definition: rtt/tcl.c:267
const char * name
Definition: command.h:235
RTT channel information.
Definition: rtt/rtt.h:65
RTT control block.
Definition: rtt/rtt.h:31
uint32_t num_up_channels
Maximum number of up-channels.
Definition: rtt/rtt.h:37
uint32_t num_down_channels
Maximum number of down-channels.
Definition: rtt/rtt.h:39
RTT source.
Definition: rtt/rtt.h:119
rtt_source_find_ctrl_block find_cb
Definition: rtt/rtt.h:120
rtt_source_start start
Definition: rtt/rtt.h:123
rtt_source_stop stop
Definition: rtt/rtt.h:124
rtt_source_write write
Definition: rtt/rtt.h:126
rtt_source_read_ctrl_block read_cb
Definition: rtt/rtt.h:121
rtt_source_read_channel_info read_channel_info
Definition: rtt/rtt.h:122
rtt_source_read read
Definition: rtt/rtt.h:125
int target_rtt_read_control_block(struct target *target, target_addr_t address, struct rtt_control *ctrl, void *user_data)
Definition: target/rtt.c:220
int target_rtt_read_callback(struct target *target, const struct rtt_control *ctrl, struct rtt_sink_list **sinks, size_t num_channels, void *user_data)
Definition: target/rtt.c:366
int target_rtt_find_control_block(struct target *target, target_addr_t *address, size_t size, const char *id, bool *found, void *user_data)
Definition: target/rtt.c:241
int target_rtt_stop(struct target *target, void *user_data)
Definition: target/rtt.c:56
int target_rtt_start(struct target *target, const struct rtt_control *ctrl, void *user_data)
Definition: target/rtt.c:50
int target_rtt_write_callback(struct target *target, struct rtt_control *ctrl, unsigned int channel_index, const uint8_t *buffer, size_t *length, void *user_data)
Definition: target/rtt.c:182
int target_rtt_read_channel_info(struct target *target, const struct rtt_control *ctrl, unsigned int channel_index, enum rtt_channel_type type, struct rtt_channel_info *info, void *user_data)
Definition: target/rtt.c:285
struct target * get_current_target(struct command_context *cmd_ctx)
Definition: target.c:458
uint64_t target_addr_t
Definition: types.h:335
static struct ublast_lowlevel_priv info