mirror of
https://github.com/isod89/sloop-fm1.git
synced 2026-12-06 04:44:22 +01:00
345 lines
11 KiB
C
345 lines
11 KiB
C
/* SPDX-License-Identifier: GPL-3.0-only
|
|
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
|
|
/* Serial console on the CDC-ACM function (FELUCCA_CDC=1): read-only
|
|
* diagnostics. Runs in the main loop (cdc_task); usb_poll moves the bytes.
|
|
* The baud rate is ignored. Nothing here writes memory or flash; `uboot`
|
|
* does what the SysEx soft key does. */
|
|
#define CON_LINE 64u
|
|
|
|
static struct {
|
|
char line[CON_LINE];
|
|
uint32_t len;
|
|
uint8_t dtr_seen, stalled; /* stalled: the host stopped reading, drop the rest of this reply */
|
|
} con;
|
|
static uint32_t lowram_state(void); /* main.c: the SRAM below the RAM region, proved at boot */
|
|
static uint32_t stack_free_main(void), stack_free_sup(void), stack_sp(void); /* main.c: core 0's stacks, painted at boot */
|
|
|
|
static void con_putc(char c)
|
|
{
|
|
uint32_t t0 = fm1_ms;
|
|
while (co_w - co_r >= CO_N) { /* full: wait for usb_poll, briefly */
|
|
if (!cdc.dtr || con.stalled || fm1_ms - t0 > 20u) {
|
|
con.stalled = 1;
|
|
return;
|
|
}
|
|
fm1_wdt_feed();
|
|
}
|
|
cdc_out[co_w % CO_N] = (uint8_t)c;
|
|
RING_PUBLISH();
|
|
co_w++;
|
|
}
|
|
|
|
static void con_puts(const char *s)
|
|
{
|
|
while (*s)
|
|
con_putc(*s++);
|
|
}
|
|
|
|
static void con_hex(uint32_t v, uint32_t digits)
|
|
{
|
|
while (digits--)
|
|
con_putc("0123456789ABCDEF"[(v >> (digits * 4u)) & 15u]);
|
|
}
|
|
|
|
static void con_dec(int32_t v)
|
|
{
|
|
char b[12];
|
|
uint32_t n = 0, u = v < 0 ? (uint32_t)-v : (uint32_t)v;
|
|
if (v < 0)
|
|
con_putc('-');
|
|
do
|
|
b[n++] = (char)('0' + u % 10u);
|
|
while ((u /= 10u) != 0 && n < sizeof b);
|
|
while (n)
|
|
con_putc(b[--n]);
|
|
}
|
|
|
|
static void con_kv(const char *k, int32_t v) /* "key value\r\n" */
|
|
{
|
|
con_puts(k);
|
|
con_putc(' ');
|
|
con_dec(v);
|
|
con_puts("\r\n");
|
|
}
|
|
|
|
static void con_kx(const char *k, uint32_t v)
|
|
{
|
|
con_puts(k);
|
|
con_puts(" 0x");
|
|
con_hex(v, 8);
|
|
con_puts("\r\n");
|
|
}
|
|
|
|
static uint32_t con_num(const char **p, int *ok) /* decimal or 0x hex */
|
|
{
|
|
const char *s = *p;
|
|
uint32_t v = 0, base = 10, d, n = 0;
|
|
while (*s == ' ')
|
|
s++;
|
|
if (s[0] == '0' && (s[1] == 'x' || s[1] == 'X'))
|
|
s += 2, base = 16;
|
|
for (;; s++, n++) {
|
|
char c = *s;
|
|
if (c >= '0' && c <= '9')
|
|
d = (uint32_t)(c - '0');
|
|
else if (base == 16 && c >= 'a' && c <= 'f')
|
|
d = (uint32_t)(c - 'a' + 10);
|
|
else if (base == 16 && c >= 'A' && c <= 'F')
|
|
d = (uint32_t)(c - 'A' + 10);
|
|
else
|
|
break;
|
|
v = v * base + d;
|
|
}
|
|
*ok = n > 0;
|
|
*p = s;
|
|
return v;
|
|
}
|
|
|
|
static int con_word(const char **p, const char *w) /* match a whole word */
|
|
{
|
|
const char *s = *p;
|
|
while (*s == ' ')
|
|
s++;
|
|
while (*w && *s == *w)
|
|
s++, w++;
|
|
if (*w || (*s && *s != ' '))
|
|
return 0;
|
|
*p = s;
|
|
return 1;
|
|
}
|
|
|
|
static void con_memr(const char *p)
|
|
{
|
|
int ok, ok2;
|
|
uint32_t a = con_num(&p, &ok), n = con_num(&p, &ok2), i;
|
|
if (!ok) {
|
|
con_puts("usage: memr ADDR [LEN<=256]\r\n");
|
|
return;
|
|
}
|
|
if (!ok2 || !n)
|
|
n = 64;
|
|
if (n > 256u)
|
|
n = 256;
|
|
if (!fm1_mem_readable(a, n)) {
|
|
con_puts("only RAM 01C00000-01C80000 and XIP 02000000-02100000\r\n");
|
|
return;
|
|
}
|
|
for (i = 0; i < n; i++) {
|
|
if (i % 16u == 0) {
|
|
con_hex(a + i, 8);
|
|
con_putc(':');
|
|
}
|
|
con_putc(' ');
|
|
con_hex(fm1_peek8(a + i), 2);
|
|
if (i % 16u == 15u || i + 1u == n)
|
|
con_puts("\r\n");
|
|
}
|
|
}
|
|
|
|
#if FELUCCA_FLASH
|
|
static void con_flr(const char *p) /* flash read over SPI (no XIP decryption) */
|
|
{
|
|
static uint8_t b[256];
|
|
int ok, ok2;
|
|
uint32_t a = con_num(&p, &ok), n = con_num(&p, &ok2), i;
|
|
if (!ok || a < 0x93000u || a >= 0x100000u) {
|
|
con_puts("usage: flr OFFSET [LEN<=256] (0x93000..0xFFFFF)\r\n");
|
|
return;
|
|
}
|
|
if (!ok2 || !n)
|
|
n = 64;
|
|
if (n > 256u)
|
|
n = 256;
|
|
if (a + n > 0x100000u)
|
|
n = 0x100000u - a;
|
|
if (!flash_ok || st_read(a, b, n)) {
|
|
con_puts("flash not available\r\n");
|
|
return;
|
|
}
|
|
for (i = 0; i < n; i++) {
|
|
if (i % 16u == 0) {
|
|
con_hex(a + i, 6);
|
|
con_putc(':');
|
|
}
|
|
con_putc(' ');
|
|
con_hex(b[i], 2);
|
|
if (i % 16u == 15u || i + 1u == n)
|
|
con_puts("\r\n");
|
|
}
|
|
}
|
|
#endif
|
|
|
|
static void con_status(void)
|
|
{
|
|
const engine_t *e = ENGINES[TSEL->eng_req % NENGINES];
|
|
con_puts("felucca ");
|
|
con_puts(FELUCCA_VERSION);
|
|
con_puts("\r\n");
|
|
con_kv("uptime_ms", (int32_t)fm1_ms);
|
|
con_kv("cpu_pct", (int32_t)(song.cpu_q8 * 100u / 256u));
|
|
#if FELUCCA_CPU1
|
|
con_kv("cpu1", (int32_t)cpu1_state); /* 1 did not start, 2 self-test failed, 3 running (cpu1.c) */
|
|
con_kx("cpu1_trace", fm1_c1_trace);
|
|
con_kv("cpu1_timeouts", (int32_t)cpu1_timeouts);
|
|
con_kv("cpu1_gave_up", (int32_t)fm1_c1_gave_up); /* (2.8.3) 1 a wrong job read, 2 one late, 3 barred till power-off */
|
|
con_kv("cpu1_bad", (int32_t)fm1_c1_mb.bad); /* jobs core 1 read wrong and refused (fm1_cpu1.h) */
|
|
con_kx("cpu1_bad_fn", fm1_c1_mb.bad_fn);
|
|
con_kv("cpu1_units", cpu1_units); /* the render units it gets: -1 its share, else a mask (fx.c) */
|
|
con_kv("cpu1_busy_pct", (int32_t)(cpu1_busy_q8 * 100u / 256u)); /* its time in the render, of a half (fx.c) */
|
|
con_kv("cpu1_wait_pct", (int32_t)(cpu1_wait_q8 * 100u / 256u)); /* core 0's time waiting for it */
|
|
con_kv("cpu1_stack_free", (int32_t)cpu1_stack_free()); /* bytes of its stack the render never reached */
|
|
#endif
|
|
con_kv("lowram", (int32_t)lowram_state()); /* the SRAM below the RAM region: 1 proved at boot, 2 not (main.c) */
|
|
con_kv("stack_free", (int32_t)stack_free_main()); /* core 0's main stack (24 KB): bytes never reached since boot */
|
|
con_kv("sstack_free", (int32_t)stack_free_sup()); /* its supervisor stack (8 KB) */
|
|
con_kx("sp", stack_sp()); /* where the main loop runs now: which of the two regions */
|
|
con_kv("audio_max_us", (int32_t)felucca_dbg.max_us);
|
|
con_kv("voices_shed", (int32_t)shed_count);
|
|
con_kv("voices_given_up", (int32_t)voice_kills);
|
|
con_kv("track", (int32_t)song.sel + 1);
|
|
con_kv("batt_raw", song.batt_raw);
|
|
con_puts("engine ");
|
|
con_puts(e->name);
|
|
con_puts("\r\n");
|
|
con_puts("preset ");
|
|
con_puts(TSEL->preset < e->npresets ? e->presets[TSEL->preset].name : "-");
|
|
con_puts("\r\n");
|
|
con_kv("bpm", song.g[G_BPM]);
|
|
con_kv("playing", song.playing);
|
|
con_kv("boots", (int32_t)felucca_dbg.boots);
|
|
con_kv("usb_resets", (int32_t)usb.resets);
|
|
con_kv("usb_sof", (int32_t)usb.sof_seen);
|
|
con_kv("usb_suspends", (int32_t)usb.suspends);
|
|
con_kv("usb_retries", (int32_t)usb.retries);
|
|
con_kv("usb_frame", (int32_t)usb.frame);
|
|
con_kv("usb_frame_stalls", (int32_t)usb.frame_stalls);
|
|
con_kv("usb_max_gap_polls", (int32_t)usb.max_gap);
|
|
con_kv("midi_rx_pkts", (int32_t)usb.rx_pkts);
|
|
con_kv("midi_tx_pkts", (int32_t)usb.tx_pkts);
|
|
#if FELUCCA_FLASH
|
|
con_kv("flash", flash_ok);
|
|
#endif
|
|
}
|
|
|
|
static void con_dbg(void)
|
|
{
|
|
const uint32_t *w = (const uint32_t *)&felucca_dbg;
|
|
static const char *const NAMES[] = {"magic", "halves", "max_us", "nested", "in_audio", "late",
|
|
"timer_irqs", "ui_frames", "last_us", "cpu_q8", "boots",
|
|
"stage", "page", "home", "prev_stage", "prev_page",
|
|
"prev_home", "prev_rst", "prev_frames"};
|
|
uint32_t i;
|
|
for (i = 0; i < sizeof NAMES / sizeof NAMES[0] && i < sizeof felucca_dbg / 4u; i++)
|
|
con_kx(NAMES[i], w[i]);
|
|
}
|
|
|
|
static void con_crash(void)
|
|
{
|
|
if (fm1_crash.magic != FM1_CRASH_MAGIC) {
|
|
con_puts("no crash record\r\n");
|
|
return;
|
|
}
|
|
con_kv("count", (int32_t)fm1_crash.count);
|
|
con_kx("vec", fm1_crash.vec);
|
|
con_kx("pc", fm1_crash.pc);
|
|
con_kx("rets", fm1_crash.rets);
|
|
con_kx("emu", fm1_crash.emu);
|
|
con_kx("sp", fm1_crash.sp);
|
|
con_kx("psr", fm1_crash.psr);
|
|
con_kv("uptime_ms", (int32_t)fm1_crash.uptime_ms);
|
|
con_kv("early", (int32_t)fm1_crash.early);
|
|
}
|
|
|
|
static void con_params(void)
|
|
{
|
|
uint32_t i;
|
|
for (i = 0; i < P_COUNT; i++) {
|
|
con_dec((int32_t)i);
|
|
con_putc('=');
|
|
con_dec(TSEL->p[i]);
|
|
con_puts(i % 8u == 7u || i + 1u == P_COUNT ? "\r\n" : " ");
|
|
}
|
|
}
|
|
|
|
static void con_exec(const char *p)
|
|
{
|
|
if (con_word(&p, "help") || con_word(&p, "?"))
|
|
con_puts("status dbg crash params memr ADDR [LEN] flr OFF [LEN] cpu1 units MASK|auto uboot yes\r\n");
|
|
else if (con_word(&p, "status"))
|
|
con_status();
|
|
else if (con_word(&p, "dbg"))
|
|
con_dbg();
|
|
else if (con_word(&p, "crash"))
|
|
con_crash();
|
|
else if (con_word(&p, "params"))
|
|
con_params();
|
|
else if (con_word(&p, "memr"))
|
|
con_memr(p);
|
|
#if FELUCCA_CPU1
|
|
else if (con_word(&p, "cpu1")) { /* cpu1 units MASK | auto: the render units core 1 gets (fx.c
|
|
* units_split: bit k = synth part k, bit 3 = the drums), live */
|
|
if (con_word(&p, "units")) {
|
|
int ok;
|
|
uint32_t v;
|
|
if (con_word(&p, "auto"))
|
|
cpu1_units = -1;
|
|
else if ((v = con_num(&p, &ok)), ok && v <= 15u)
|
|
cpu1_units = (int32_t)v;
|
|
else {
|
|
con_puts("usage: cpu1 units MASK(0-15)|auto\r\n");
|
|
return;
|
|
}
|
|
}
|
|
con_kv("cpu1_units", cpu1_units);
|
|
}
|
|
#endif
|
|
#if FELUCCA_FLASH
|
|
else if (con_word(&p, "flr"))
|
|
con_flr(p);
|
|
#endif
|
|
else if (con_word(&p, "uboot")) {
|
|
if (con_word(&p, "yes")) {
|
|
con_puts("entering UBOOT\r\n");
|
|
usb.uboot_req = 1; /* main loop: same path as the SysEx key */
|
|
} else {
|
|
con_puts("type 'uboot yes'\r\n");
|
|
}
|
|
} else if (*p)
|
|
con_puts("? (help)\r\n");
|
|
}
|
|
|
|
static void cdc_task(void) /* main loop */
|
|
{
|
|
if (cdc.dtr && !con.dtr_seen) {
|
|
con_puts("\r\nFelucca ");
|
|
con_puts(FELUCCA_VERSION);
|
|
con_puts(" console - 'help'\r\n> ");
|
|
}
|
|
con.dtr_seen = cdc.dtr;
|
|
while (ci_r != ci_w) {
|
|
char c;
|
|
RING_PUBLISH(); /* usb_poll (producer) can preempt us */
|
|
c = (char)cdc_in[ci_r % CI_N];
|
|
RING_PUBLISH();
|
|
ci_r++;
|
|
if (c == '\r' || c == '\n') {
|
|
if (c == '\n' && con.len == 0)
|
|
continue; /* the LF of a CR LF */
|
|
con_puts("\r\n");
|
|
con.line[con.len] = 0;
|
|
con.stalled = 0;
|
|
con_exec(con.line);
|
|
con.len = 0;
|
|
con_puts("> ");
|
|
} else if (c == 0x7F || c == 0x08) {
|
|
if (con.len) {
|
|
con.len--;
|
|
con_puts("\b \b");
|
|
}
|
|
} else if (c >= ' ' && c < 0x7F && con.len + 1u < CON_LINE) {
|
|
con.line[con.len++] = c;
|
|
con_putc(c);
|
|
}
|
|
}
|
|
}
|