checkpatch: exclude gerrit's Change-Id line from commit description
[openocd.git] / src / rtt / tcl.c
blobbae71b6ce57eac10860f8f6f2c353cf5fa52602f
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 /*
4 * Copyright (C) 2019-2020 by Marc Schink <dev@zapb.de>
5 */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
11 #include <helper/log.h>
12 #include <target/rtt.h>
14 #include "rtt.h"
16 #define CHANNEL_NAME_SIZE 128
18 COMMAND_HANDLER(handle_rtt_setup_command)
20 struct rtt_source source;
22 const char *DEFAULT_ID = "SEGGER RTT";
23 const char *selected_id;
24 if (CMD_ARGC < 2 || CMD_ARGC > 3)
25 return ERROR_COMMAND_SYNTAX_ERROR;
26 if (CMD_ARGC == 2)
27 selected_id = DEFAULT_ID;
28 else
29 selected_id = CMD_ARGV[2];
31 source.find_cb = &target_rtt_find_control_block;
32 source.read_cb = &target_rtt_read_control_block;
33 source.start = &target_rtt_start;
34 source.stop = &target_rtt_stop;
35 source.read = &target_rtt_read_callback;
36 source.write = &target_rtt_write_callback;
37 source.read_channel_info = &target_rtt_read_channel_info;
39 target_addr_t address;
40 uint32_t size;
42 COMMAND_PARSE_NUMBER(target_addr, CMD_ARGV[0], address);
43 COMMAND_PARSE_NUMBER(u32, CMD_ARGV[1], size);
45 rtt_register_source(source, get_current_target(CMD_CTX));
47 if (rtt_setup(address, size, selected_id) != ERROR_OK)
48 return ERROR_FAIL;
50 return ERROR_OK;
53 COMMAND_HANDLER(handle_rtt_start_command)
55 if (CMD_ARGC > 0)
56 return ERROR_COMMAND_SYNTAX_ERROR;
58 if (!rtt_configured()) {
59 command_print(CMD, "RTT is not configured");
60 return ERROR_FAIL;
63 return rtt_start();
66 COMMAND_HANDLER(handle_rtt_stop_command)
68 if (CMD_ARGC > 0)
69 return ERROR_COMMAND_SYNTAX_ERROR;
71 return rtt_stop();
74 COMMAND_HANDLER(handle_rtt_polling_interval_command)
76 if (CMD_ARGC == 0) {
77 int ret;
78 unsigned int interval;
80 ret = rtt_get_polling_interval(&interval);
82 if (ret != ERROR_OK) {
83 command_print(CMD, "Failed to get polling interval");
84 return ret;
87 command_print(CMD, "%u ms", interval);
88 } else if (CMD_ARGC == 1) {
89 int ret;
90 unsigned int interval;
92 COMMAND_PARSE_NUMBER(uint, CMD_ARGV[0], interval);
93 ret = rtt_set_polling_interval(interval);
95 if (ret != ERROR_OK) {
96 command_print(CMD, "Failed to set polling interval");
97 return ret;
99 } else {
100 return ERROR_COMMAND_SYNTAX_ERROR;
103 return ERROR_OK;
106 COMMAND_HANDLER(handle_rtt_channels_command)
108 int ret;
109 char channel_name[CHANNEL_NAME_SIZE];
110 const struct rtt_control *ctrl;
111 struct rtt_channel_info info;
113 if (!rtt_found_cb()) {
114 command_print(CMD, "rtt: Control block not available");
115 return ERROR_FAIL;
118 ctrl = rtt_get_control();
120 command_print(CMD, "Channels: up=%u, down=%u", ctrl->num_up_channels,
121 ctrl->num_down_channels);
123 command_print(CMD, "Up-channels:");
125 info.name = channel_name;
126 info.name_length = sizeof(channel_name);
128 for (unsigned int i = 0; i < ctrl->num_up_channels; i++) {
129 ret = rtt_read_channel_info(i, RTT_CHANNEL_TYPE_UP, &info);
131 if (ret != ERROR_OK)
132 return ret;
134 if (!info.size)
135 continue;
137 command_print(CMD, "%u: %s %u %u", i, info.name, info.size,
138 info.flags);
141 command_print(CMD, "Down-channels:");
143 for (unsigned int i = 0; i < ctrl->num_down_channels; i++) {
144 ret = rtt_read_channel_info(i, RTT_CHANNEL_TYPE_DOWN, &info);
146 if (ret != ERROR_OK)
147 return ret;
149 if (!info.size)
150 continue;
152 command_print(CMD, "%u: %s %u %u", i, info.name, info.size,
153 info.flags);
156 return ERROR_OK;
159 COMMAND_HANDLER(handle_channel_list)
161 char channel_name[CHANNEL_NAME_SIZE];
162 const struct rtt_control *ctrl;
163 struct rtt_channel_info info;
165 if (CMD_ARGC != 0)
166 return ERROR_COMMAND_SYNTAX_ERROR;
168 if (!rtt_found_cb()) {
169 command_print(CMD, "rtt: Control block not available");
170 return ERROR_FAIL;
173 ctrl = rtt_get_control();
175 info.name = channel_name;
176 info.name_length = sizeof(channel_name);
178 command_print(CMD, "{");
180 for (unsigned int i = 0; i < ctrl->num_up_channels; i++) {
181 int ret = rtt_read_channel_info(i, RTT_CHANNEL_TYPE_UP, &info);
182 if (ret != ERROR_OK)
183 return ret;
185 if (!info.size)
186 continue;
188 command_print(CMD,
189 " {\n"
190 " name %s\n"
191 " size 0x%" PRIx32 "\n"
192 " flags 0x%" PRIx32 "\n"
193 " }",
194 info.name, info.size, info.flags);
197 command_print(CMD, "}\n{");
199 for (unsigned int i = 0; i < ctrl->num_down_channels; i++) {
200 int ret = rtt_read_channel_info(i, RTT_CHANNEL_TYPE_DOWN, &info);
201 if (ret != ERROR_OK)
202 return ret;
204 if (!info.size)
205 continue;
207 command_print(CMD,
208 " {\n"
209 " name %s\n"
210 " size 0x%" PRIx32 "\n"
211 " flags 0x%" PRIx32 "\n"
212 " }",
213 info.name, info.size, info.flags);
216 command_print(CMD, "}");
218 return ERROR_OK;
221 static const struct command_registration rtt_subcommand_handlers[] = {
223 .name = "setup",
224 .handler = handle_rtt_setup_command,
225 .mode = COMMAND_ANY,
226 .help = "setup RTT",
227 .usage = "<address> <size> [ID]"
230 .name = "start",
231 .handler = handle_rtt_start_command,
232 .mode = COMMAND_EXEC,
233 .help = "start RTT",
234 .usage = ""
237 .name = "stop",
238 .handler = handle_rtt_stop_command,
239 .mode = COMMAND_EXEC,
240 .help = "stop RTT",
241 .usage = ""
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]"
251 .name = "channels",
252 .handler = handle_rtt_channels_command,
253 .mode = COMMAND_EXEC,
254 .help = "list available channels",
255 .usage = ""
258 .name = "channellist",
259 .handler = handle_channel_list,
260 .mode = COMMAND_EXEC,
261 .help = "list available channels",
262 .usage = ""
264 COMMAND_REGISTRATION_DONE
267 const struct command_registration rtt_target_command_handlers[] = {
269 .name = "rtt",
270 .mode = COMMAND_EXEC,
271 .help = "RTT target commands",
272 .usage = "",
273 .chain = rtt_subcommand_handlers
275 COMMAND_REGISTRATION_DONE