1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Copyright (C) 2019-2020 by Marc Schink <dev@zapb.de>
11 #include <helper/log.h>
12 #include <target/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
;
27 selected_id
= DEFAULT_ID
;
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
;
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
)
53 COMMAND_HANDLER(handle_rtt_start_command
)
56 return ERROR_COMMAND_SYNTAX_ERROR
;
58 if (!rtt_configured()) {
59 command_print(CMD
, "RTT is not configured");
66 COMMAND_HANDLER(handle_rtt_stop_command
)
69 return ERROR_COMMAND_SYNTAX_ERROR
;
74 COMMAND_HANDLER(handle_rtt_polling_interval_command
)
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");
87 command_print(CMD
, "%u ms", interval
);
88 } else if (CMD_ARGC
== 1) {
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");
100 return ERROR_COMMAND_SYNTAX_ERROR
;
106 COMMAND_HANDLER(handle_rtt_channels_command
)
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");
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
);
137 command_print(CMD
, "%u: %s %u %u", i
, info
.name
, info
.size
,
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
);
152 command_print(CMD
, "%u: %s %u %u", i
, info
.name
, info
.size
,
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
;
166 return ERROR_COMMAND_SYNTAX_ERROR
;
168 if (!rtt_found_cb()) {
169 command_print(CMD
, "rtt: Control block not available");
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
);
191 " size 0x%" PRIx32
"\n"
192 " flags 0x%" PRIx32
"\n"
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
);
210 " size 0x%" PRIx32
"\n"
211 " flags 0x%" PRIx32
"\n"
213 info
.name
, info
.size
, info
.flags
);
216 command_print(CMD
, "}");
221 static const struct command_registration rtt_subcommand_handlers
[] = {
224 .handler
= handle_rtt_setup_command
,
227 .usage
= "<address> <size> [ID]"
231 .handler
= handle_rtt_start_command
,
232 .mode
= COMMAND_EXEC
,
238 .handler
= handle_rtt_stop_command
,
239 .mode
= COMMAND_EXEC
,
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]"
252 .handler
= handle_rtt_channels_command
,
253 .mode
= COMMAND_EXEC
,
254 .help
= "list available channels",
258 .name
= "channellist",
259 .handler
= handle_channel_list
,
260 .mode
= COMMAND_EXEC
,
261 .help
= "list available channels",
264 COMMAND_REGISTRATION_DONE
267 const struct command_registration rtt_target_command_handlers
[] = {
270 .mode
= COMMAND_EXEC
,
271 .help
= "RTT target commands",
273 .chain
= rtt_subcommand_handlers
275 COMMAND_REGISTRATION_DONE