command: Cleaned up and refactored cmd_rtt

# Conflicts:
#	src/command.c
This commit is contained in:
dragonmux 2022-07-26 12:44:42 +01:00 committed by Jason Kotzin
parent c712a54cbc
commit ad1868f8d4

View File

@ -441,88 +441,77 @@ static bool cmd_target_power(target *t, int argc, const char **argv)
#endif #endif
#ifdef ENABLE_RTT #ifdef ENABLE_RTT
const char* onoroffstr[2] = {"off", "on"}; static const char *on_or_off(const bool value)
static const char* onoroff(bool bval) { {
return bval ? onoroffstr[1] : onoroffstr[0]; return value ? "on" : "off";
} }
static bool cmd_rtt(target *t, int argc, const char **argv) static bool cmd_rtt(target *t, int argc, const char **argv)
{ {
(void)t; (void)t;
if ((argc == 1) || ((argc == 2) && !strncmp(argv[1], "enabled", strlen(argv[1])))) { const size_t command_len = strlen(argv[1]);
if (argc == 1 || (argc == 2 && !strncmp(argv[1], "enabled", command_len))) {
rtt_enabled = true; rtt_enabled = true;
rtt_found = false; rtt_found = false;
} } else if ((argc == 2) && !strncmp(argv[1], "disabled", command_len)) {
else if ((argc == 2) && !strncmp(argv[1], "disabled", strlen(argv[1]))) {
rtt_enabled = false; rtt_enabled = false;
rtt_found = false; rtt_found = false;
} } else if ((argc == 2) && !strncmp(argv[1], "status", command_len)) {
else if ((argc == 2) && !strncmp(argv[1], "status", strlen(argv[1]))) { gdb_outf("rtt: %s found: %s ident: \"%s\"", on_or_off(rtt_enabled), rtt_found ? "yes" : "no",
gdb_outf("rtt: %s found: %s ident: ", rtt_ident[0] == '\0' ? "off" : rtt_ident);
onoroff(rtt_enabled), rtt_found ? "yes" : "no"); gdb_outf(" halt: %s", on_or_off(target_no_background_memory_access(t)));
if (rtt_ident[0] == '\0')
gdb_out("off");
else
gdb_outf("\"%s\"", rtt_ident);
gdb_outf(" halt: %s", onoroff(target_no_background_memory_access(t)));
gdb_out(" channels: "); gdb_out(" channels: ");
if (rtt_auto_channel) gdb_out("auto "); if (rtt_auto_channel)
for (uint32_t i = 0; i < MAX_RTT_CHAN; i++) gdb_out("auto ");
if (rtt_channel[i].is_enabled) gdb_outf("%d ", i); for (size_t i = 0; i < MAX_RTT_CHAN; i++) {
gdb_outf("\nmax poll ms: %u min poll ms: %u max errs: %u\n", if (rtt_channel[i].is_enabled)
rtt_max_poll_ms, rtt_min_poll_ms, rtt_max_poll_errs); gdb_outf("%d ", i);
} }
else if ((argc >= 2) && !strncmp(argv[1], "channel", strlen(argv[1]))) { gdb_outf(
"\nmax poll ms: %u min poll ms: %u max errs: %u\n", rtt_max_poll_ms, rtt_min_poll_ms, rtt_max_poll_errs);
} else if (argc >= 2 && !strncmp(argv[1], "channel", command_len)) {
/* mon rtt channel switches to auto rtt channel selection /* mon rtt channel switches to auto rtt channel selection
mon rtt channel number... selects channels given */ mon rtt channel number... selects channels given */
for (uint32_t i = 0; i < MAX_RTT_CHAN; i++) for (size_t i = 0; i < MAX_RTT_CHAN; i++)
rtt_channel[i].is_enabled = false; rtt_channel[i].is_enabled = false;
if (argc == 2) {
if (argc == 2)
rtt_auto_channel = true; rtt_auto_channel = true;
} else { else {
rtt_auto_channel = false; rtt_auto_channel = false;
for (int i = 2; i < argc; i++) { for (size_t i = 2; i < (size_t)argc; ++i) {
int chan = atoi(argv[i]); const uint32_t channel = strtoul(argv[i], NULL, 0);
if ((chan >= 0) && (chan < MAX_RTT_CHAN)) if (channel < MAX_RTT_CHAN)
rtt_channel[chan].is_enabled = true; rtt_channel[channel].is_enabled = true;
} }
} }
} } else if (argc == 2 && !strncmp(argv[1], "ident", command_len))
else if ((argc == 2) && !strncmp(argv[1], "ident", strlen(argv[1]))) {
rtt_ident[0] = '\0'; rtt_ident[0] = '\0';
} else if (argc == 2 && !strncmp(argv[1], "poll", command_len))
else if ((argc == 2) && !strncmp(argv[1], "poll", strlen(argv[1])))
gdb_outf("%u %u %u\n", rtt_max_poll_ms, rtt_min_poll_ms, rtt_max_poll_errs); gdb_outf("%u %u %u\n", rtt_max_poll_ms, rtt_min_poll_ms, rtt_max_poll_errs);
else if ((argc == 2) && !strncmp(argv[1], "cblock", strlen(argv[1]))) { else if (argc == 2 && !strncmp(argv[1], "cblock", command_len)) {
gdb_outf("cbaddr: 0x%x\n", rtt_cbaddr); gdb_outf("cbaddr: 0x%x\n", rtt_cbaddr);
gdb_out("ch ena cfg i/o buf@ size head@ tail@ flg\n"); gdb_out("ch ena cfg i/o buf@ size head@ tail@ flg\n");
for (uint32_t i = 0; i < MAX_RTT_CHAN; i++) { for (size_t i = 0; i < MAX_RTT_CHAN; ++i) {
gdb_outf("%2d %c %c %s 0x%08x %5d 0x%08x 0x%08x %d\n", gdb_outf("%2zu %c %c %s 0x%08x %5d 0x%08x 0x%08x %d\n", i, rtt_channel[i].is_enabled ? 'y' : 'n',
i, rtt_channel[i].is_enabled ? 'y' : 'n', rtt_channel[i].is_configured ? 'y' : 'n', rtt_channel[i].is_configured ? 'y' : 'n', rtt_channel[i].is_output ? "out" : "in ",
rtt_channel[i].is_output ? "out" : "in ", rtt_channel[i].buf_addr, rtt_channel[i].buf_size, rtt_channel[i].buf_addr, rtt_channel[i].buf_size, rtt_channel[i].head_addr, rtt_channel[i].tail_addr,
rtt_channel[i].head_addr, rtt_channel[i].tail_addr, rtt_channel[i].flag); rtt_channel[i].flag);
} }
} } else if (argc == 3 && !strncmp(argv[1], "ident", command_len)) {
else if ((argc == 3) && !strncmp(argv[1], "ident", strlen(argv[1]))) {
strncpy(rtt_ident, argv[2], sizeof(rtt_ident)); strncpy(rtt_ident, argv[2], sizeof(rtt_ident));
rtt_ident[sizeof(rtt_ident)-1] = '\0'; rtt_ident[sizeof(rtt_ident) - 1] = '\0';
for (uint32_t i = 0; i < sizeof(rtt_ident); i++) for (size_t i = 0; i < sizeof(rtt_ident); i++) {
if (rtt_ident[i] == '_') rtt_ident[i] = ' '; if (rtt_ident[i] == '_')
} rtt_ident[i] = ' ';
else if ((argc == 5) && !strncmp(argv[1], "poll", strlen(argv[1]))) {
/* set polling params */
int32_t new_max_poll_ms = atoi(argv[2]);
int32_t new_min_poll_ms = atoi(argv[3]);
int32_t new_max_poll_errs = atoi(argv[4]);
if ((new_max_poll_ms >= 0) && (new_min_poll_ms >= 0) && (new_max_poll_errs >= 0)
&& (new_max_poll_ms >= new_min_poll_ms)) {
rtt_max_poll_ms = new_max_poll_ms;
rtt_min_poll_ms = new_min_poll_ms;
rtt_max_poll_errs = new_max_poll_errs;
} }
else gdb_out("how?\n"); } else if (argc == 5 && !strncmp(argv[1], "poll", command_len)) {
} /* set polling params */
else gdb_out("what?\n"); rtt_max_poll_ms = strtoul(argv[2], NULL, 0);
rtt_min_poll_ms = strtoul(argv[3], NULL, 0);
rtt_max_poll_errs = strtoul(argv[4], NULL, 0);
} else
gdb_out("what?\n");
return true; return true;
} }
#endif #endif