Files
fmini-1/firmware/src/main.c
T
Claude 232264d571 FMini-1 v1.7.1: fmug is now fmini everywhere (files, identifiers, engine name, editor, docs)
The project's first name, FMUG, is gone. Nothing about the sound or the stored data changes.

- Files: eng_fmini.c, fmini_app.c, ui_fmini.c, editor_fmini.c, eng_fmini_filter.h, eng_fmini_presets.h, the tests, the
  tools (fmini_bank.py, fmini_filter.py, fmini_retune.py, fmini_levels.json, gen_fmini_presets.py) and
  web/make_fmini_installer.py.
- Identifiers: fmug_ / FMUG_ / FELUCCA_FMUG_ONLY became fmini_ / FMINI_ / FELUCCA_FMINI_ONLY; the editor commands are
  FMINI_GET / FMINI_PUT / FMINI_PRESET (numbers 82-84 unchanged). The engine reports the name "FMINI" (was "FMUG") on the
  device and in the editor's engine list; the editor still accepts "FMUG" from firmware up to 1.7.0.
- Short forms fmg_ / GR_FMG0..6 and FG_* are left as they are.
- Tables: tests/golden.txt and tests/target_budget.txt only had their keys renamed.
- Docs: CLAUDE.md and DEVELOPING.md say there is one branch (main) and record the rename; the test-suite notes now describe
  the second regress section (FELUCCA_CPU2=0: 41 FMINI patches differ from the golden, as on 1.7.0).
- Site: docs/emu and docs/index.html rebuilt for v1.7.1; the splash pictures show the new version.

Checked: the firmware's instructions are identical to 1.7.0 (only the addresses of string literals move); the 100 FMINI golden
hashes are unchanged; every screen of the README shots is pixel-identical except the splash; the preset lab's metrics for the
100 patches are identical; the host suite fails in the same seven sections as before (fm4, ui_render, three regress, union_test,
web pages) and nowhere else. Not tested on a real FM-1.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01QUF6pQ1NkTwmBC8SR8AK94
2026-10-20 19:35:44 -03:00

393 lines
16 KiB
C

/* SPDX-License-Identifier: GPL-3.0-only
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
/* FELUCCA boot and main loop. Boot order: WDT first, boot-loop guard, fatal vectors,
* guards; then LCD, input (TIMER5 IRQ, 10 kHz), audio (ALNK0 IRQ). */
extern uint32_t _data_start[], _data_end[], _data_load[], _bss_start[], _bss_end[];
extern uint32_t _pool_start[], _pool_end[], _rt_start[], _rt_end[], _rt_load[];
#if FELUCCA_CPU2
extern uint32_t _c1_start[], _c1_end[], _c1_load[];
#endif
/* TIMER5 outranks ALNK0, so the scan keeps its 100 us pace while a half buffer renders: before,
* the ticks stopped for the whole render (0.7 ms idle, several ms loaded), the column lit when it
* began stayed lit that long (a ~16 Hz flicker over all LEDs, beating with the scan) and the
* encoders lost frames. Nested in ALNK0 it only scans (GPIO + fm1_in, nothing the audio ISR touches)
* and counts ms; USB and UART polls wait for the first tick after the render, as they always did,
* and the time spent nested is handed to the audio ISR so its load figures stay render-only.
* The USB audio stream cannot wait for the render (one packet per 1 ms frame, a render takes up to
* ~5 ms): uac_service also runs nested. It touches only EP4 (INDEX is set on every access) and the
* consumer side of the audio ring, and usb_poll never runs nested, so the two never interleave. */
void fm1_timer5_irq(void)
{
static uint32_t sub, owed;
uint32_t t0 = fm1_ticks(), usb_due = sub % 5u == 0u;
fm1_timer5_ack();
felucca_dbg.timer_irqs++;
fm1_input_tick();
{ /* milliseconds from the 24 MHz TIMER4 (robust to a late tick) */
static uint32_t last, acc;
acc += t0 - last;
last = t0;
while (acc >= 1000u * FM1_TICKS_PER_US) {
acc -= 1000u * FM1_TICKS_PER_US;
fm1_ms++;
}
}
if (usb_due)
owed |= 1u; /* 2 kHz: all USB SIE traffic lives here */
#if FELUCCA_UART
if (sub % 5u == 2u)
owed |= 2u; /* 2 kHz: <= ~7 bytes per call at 31250 baud */
#endif
if (++sub == 10u)
sub = 0;
if (felucca_dbg.in_audio) {
felucca_dbg.nested++;
#if FELUCCA_UAC
if (usb_due)
uac_service();
#endif
t5_nested_ticks += fm1_ticks() - t0;
return;
}
if (owed & 1u)
usb_poll();
#if FELUCCA_UART
if (owed & 2u)
uart_midi_poll();
#endif
owed = 0;
}
extern void isr_timer5(void);
static void timer5_start(void) /* OSC /4 = 6 MHz, PRD 600 -> 10 kHz */
{
fm1_timer5_start(isr_timer5, 4); /* above ALNK0 (3): the scan nests into the render (see fm1_timer5_irq) */
}
static void hexs(char *b, uint32_t v)
{
uint32_t i;
for (i = 0; i < 8u; i++)
b[i] = "0123456789ABCDEF"[(v >> (28u - 4u * i)) & 15u];
b[8] = 0;
}
static void fm1_fault(const fm1_crash_t *c)
{
char b[12];
uint32_t t0;
fm1_audio_stop();
scr_wake_now(); /* (MENU > SCREEN OFF: the panel on first) */
lcd_fill(0, 0, 240, 240, UI_CRASH_BG); /* fixed, outside the palettes */
draw_text_line(0, 8, 240, &AF_M, "FELUCCA CRASH", UI_CRASH_INK, UI_CRASH_BG, 1);
hexs(b, c->vec);
draw_text_line(10, 40, 220, &AF_M, b, UI_CRASH_INK, UI_CRASH_BG, 0);
hexs(b, c->pc);
draw_text_line(10, 60, 220, &AF_M, b, UI_CRASH_INK, UI_CRASH_BG, 0);
hexs(b, c->emu);
draw_text_line(10, 84, 220, &AF_M, b, UI_CRASH_INK, UI_CRASH_BG, 0);
hexs(b, c->dbg);
draw_text_line(10, 102, 220, &AF_M, b, UI_CRASH_INK, UI_CRASH_BG, 0);
hexs(b, c->rets);
draw_text_line(10, 120, 220, &AF_M, b, UI_CRASH_INK, UI_CRASH_BG, 0);
t0 = fm1_ticks();
while ((uint32_t)(fm1_ticks() - t0) < 4000u * 1000u * FM1_TICKS_PER_US)
;
fm1_reboot();
}
/* power-on: the parts with their default sounds (TRK_DEF); the sequencers empty */
#if FELUCCA_FMINI_ONLY
#define UI_INPUT fa_input
#define UI_LEDS fa_leds
#define UI_DRAW fa_draw
#else
#define UI_INPUT ui_input
#define UI_LEDS ui_leds
#define UI_DRAW ui_draw
#endif
static void felucca_init(void)
{
uint32_t i;
chain_defaults(&chain_config);
for (i = 0; i < G_COUNT; i++)
song.g[i] = GP[i].def;
undo_depth++; /* (no undo copy of the power-on loads) */
fm6_init(); /* every track's FM6 patch: the init voice */
fmini_init(); /* every track's FMINI patch: F1 */
for (i = 0; i < NTRK; i++) {
track_t *t = &trk[i];
track_defaults(t);
set_engine_of(t, TRK_DEF[i][0]);
apply_preset_to(t, TRK_DEF[i][1]); /* with its sends */
t->engine = t->eng_req;
eng_mem_claim(t); /* (zeros already: no note can come before) */
track_defaults_steps(t); /* (a sound load never touches them) */
if (TRK_DEF[i][2])
load_pat16(t, PATTERNS[TRK_DEF[i][2] - 1u].note, PATTERNS[TRK_DEF[i][2] - 1u].flags);
pat_sig[i] = steps_sig(t); /* a default pattern, not the user's */
pat_last[i] = TRK_DEF[i][2];
}
undo_depth--;
song.sel = 0;
song.master_q12 = 2048; /* (the pot's level replaces it before the audio starts: master_boot) */
ui.home = 1;
ui.force = 1;
}
/* The power-on LED sweep (1.1, hal/fm1_led_anim.h: a soft light over the keys left to right, then the buttons swell
* and settle on the glow; ~0.70 s), started before the scan and run by it: nothing waits for it, the splash's 430 ms
* and the first ~0.27 s of the UI go by under it (the UI's LEDs from its end, or at once on a key or a button down).
* Every LEDS setting: it is no idle glow; with DIM HI / DIM LO it ends on that glow (and is drawn over that glow
* level), with OFF / INV dark. Not with MENU > ANIM OFF (no motion: the LEDs as the UI has them at once), not after a crash (a crash record newer than the last boot saw: the crash screen's
* reset), a hang (a WDT reset) or a failed boot (bootguard: the last one died within its first 30 s): straight to the
* UI. The UBOOT paths never get here. */
static uint32_t crash_seen __attribute__((section(".noinit"))); /* fm1_crash.count at the last boot */
static uint8_t boot_clean; /* no crash, hang (WDT) or failed boot before this one (boot_leds):
* the sweep runs and the autosave is restored (project.c autosave_boot) */
static void boot_leds(void)
{
uint32_t crashed = fm1_crash.magic == FM1_CRASH_MAGIC && fm1_crash.count != crash_seen
#if FELUCCA_CPU2
&& fm1_crash.core != 1u /* (the second core's own fault left core 0 playing) */
#endif
;
crash_seen = fm1_crash.count;
boot_clean = !crashed && !bootguard.failed && !(fm1_boot.p3_rst & 4u);
fm1_led_dim_level(settings_leds == LEDS_DIM_LO);
if (boot_clean && !(ui_prefs & PREF_ANIM_OFF))
fm1_led_anim_start(settings_leds == LEDS_DIM || settings_leds == LEDS_DIM_LO);
}
static void fm1_main(void)
{
uint32_t ms;
#if FELUCCA_CPU2
uint8_t c1_told = 0; /* 2ND CORE MISREAD shown (and ON cleared) */
#endif
persist_boot();
#if FELUCCA_OTA
if (flash_ok)
ota_boot_cleanup(); /* staging area left by an update */
#endif
settings_init();
usb_serial_apply(); /* (#67: the saved USB SERIAL before usb_start) */
lcd_init();
#if FELUCCA_FMINI_ONLY
fa_splash();
#else
draw_splash(); /* ui_draw.c (1.1.5) */
#endif
if (felucca_dbg.magic != DBG_MAGIC) {
memset(&felucca_dbg, 0, sizeof felucca_dbg);
felucca_dbg.magic = DBG_MAGIC;
}
felucca_dbg.boots++;
felucca_dbg.max_us = 0;
felucca_dbg.in_audio = 0; /* .noinit: a reset inside the audio ISR left it set, and
TIMER5 would treat every tick as nested (no USB poll) */
felucca_dbg.prev_stage = felucca_dbg.stage; /* a WDT reset leaves the last breadcrumb here */
felucca_dbg.prev_page = felucca_dbg.page;
felucca_dbg.prev_home = felucca_dbg.home;
felucca_dbg.prev_frames = felucca_dbg.ui_frames;
felucca_dbg.prev_rst = fm1_boot.p3_rst;
fm1_input_init();
fm1_adc_init();
panel_init();
felucca_init();
master_boot();
kb_boot_hold = 1; /* #137: no notes from the keys before the UI (seq.c keyboard_block) */
#if FELUCCA_CPU2
if (!bootguard.failed && (ui_prefs2 & (PREF_CPU2_ON >> 16))) {
/* the second core (hal/fm1_cpu1.h; fx.c mix_block hands it parts), at power-on, before the audio and
* the timers (started later it never reaches its entry). It starts in the chip's ROM: the PC limits open
* meanwhile. MENU > 2ND CORE OFF (the default), a crash in the last boot's first 30 s, or no answer: it is
* never started, one core, as 1.6 */
fm1_guard_pc_open();
fm1_cpu1_start();
fm1_guard_enable(FM1_GUARD_PC);
}
#endif
audio_init();
usb_start();
#if FELUCCA_UART
uart_midi_init();
#endif
boot_leds();
timer5_start();
fm1_guard_lock_top();
fm1_irq_enable_all();
fm1_delay_ms(30);
if ((fm1_in.buttons & 3u) == 3u) {
panel_setup(); /* OCT- + OCT+ held at power-on */
settings_save();
}
autosave_boot(boot_clean); /* (1.2) the last session's music, under the splash */
#if FELUCCA_FMINI_ONLY
fa_init(); /* FMini-1: track 1 = the instrument, whatever the autosave held */
#endif
#if FELUCCA_FMINI_ONLY
for (ms = 0; ms < 3000u; ms += 10u) { /* the splash: 3 s (since power-on's work above ran under it, the rest of it) */
#else
for (ms = 0; ms < 400u; ms += 10u) { /* the splash; the pot followed (MIDI IN plays under it) */
#endif
fm1_delay_ms(10);
master_poll();
}
kb_boot_hold = 0;
lcd_fill(0, 0, 240, 240, T_BG);
scrn.idle = fm1_ms; /* (MENU > SCREEN OFF: from here) */
for (;;) {
uint32_t m = fm1_ms;
fm1_wdt_feed();
usb_retry(fm1_ms);
if (fm1_ms > 30000u && bootguard.pending) { /* a crash or hang in the first 30 s counts */
bootguard.pending = 0;
bootguard.failed = 0;
}
#if FELUCCA_CPU2
if (*(volatile uint8_t *)&fm1_c1_gave_up && !c1_told) { /* (the ISR's) */
c1_told = 1; /* (once: ON set again afterwards stays, for the next power-on) */
if (ui_prefs2 & (PREF_CPU2_ON >> 16)) {
ui_prefs2 &= (uint8_t)~(PREF_CPU2_ON >> 16); /* core 1 misread or stopped answering on this FM-1: one
* core from now on (MENU > 2ND CORE ON tries again) */
settings_later();
}
ui_message("2ND CORE MISREAD: OFF"); /* (ui.msg: 23 characters at most) */
}
#endif
{
int32_t b = fm1_adc_read(FM1_ADC_BATT); /* battery: slow IIR */
if (b > 0)
song.batt_raw = song.batt_raw ? song.batt_raw + (b - song.batt_raw) / 32 : b;
}
master_poll();
{ /* OCT- + OCT+ held 5 s: enter UBOOT with RAM intact (debug / update); a countdown
* shows from 2 s over the whole screen (the menu and the dialogs too: ui_draw), letting
* go cancels it */
static uint32_t t0;
uint32_t both = (1u << panel.btn[B_OCTDN]) | (1u << panel.btn[B_OCTUP]);
if ((fm1_in.buttons & both) != both) {
if (ui.uboot) {
ui.uboot = 0;
ui.force = 1; /* what the countdown covered */
ui_message("UPDATE CANCELLED");
}
t0 = fm1_ms;
} else if (fm1_ms - t0 > 2000u && fm1_ms - t0 <= 5000u) {
uint32_t left = (5000u - (fm1_ms - t0) + 999u) / 1000u;
if (left != ui.uboot) {
ui.uboot = (uint8_t)left;
ui.force = 1;
}
} else if (fm1_ms - t0 > 5000u) {
fm1_audio_stop();
scr_wake_now();
lcd_fill(0, 0, 240, 240, T_BG);
draw_text_box(0, 110, 240, &AF_M, "UBOOT", T_THEME, 1);
usb_detach();
fm1_delay_ms(30);
bootguard.pending = 0; /* intentional reset: not a failed boot */
fm1_enter_uboot();
}
}
#if FELUCCA_OTA
ed_service(); /* web editor SysEx */
ota_service(); /* M-UPGRADE handshake */
if (usb.ota_req) { /* M-UPGRADE upgrade command */
usb.ota_req = 0;
panic_req = (1u << NTRK) - 1u; /* every track (bit per track) */
scr_wake_now(); /* (its progress on the screen) */
if (flash_ok)
ota_session(); /* returns only if nothing was committed */
lcd_fill(0, 0, 240, 240, T_BG);
ui.force = 1;
}
#endif
if (usb.uboot_req) { /* SysEx F0 22 24 35 7D F7 from the host */
fm1_audio_stop();
scr_wake_now();
lcd_fill(0, 0, 240, 240, T_BG);
draw_text_box(0, 110, 240, &AF_M, "UBOOT (USB)", T_THEME, 1);
fm1_delay_ms(20);
usb_detach();
fm1_delay_ms(30);
bootguard.pending = 0;
fm1_enter_uboot();
}
#if FELUCCA_CDC
cdc_task();
#endif
felucca_dbg.ui_frames++;
felucca_dbg.page = ui.page;
felucca_dbg.home = ui.home;
felucca_dbg.stage = 1;
UI_INPUT();
settings_poll(); /* queued settings save: only while stopped */
autosave_poll(); /* (1.2) the music, stopped and idle: project.c */
felucca_dbg.stage = 2;
UI_LEDS();
UI_DRAW();
felucca_dbg.stage = 9;
while (fm1_ms - m < 15u) { /* ~60 UI frames/s at most */
UI_INPUT();
#if FELUCCA_OTA
ed_service(); /* editor replies without waiting for the next frame */
#endif
}
}
}
void fm1_cstart(void)
{
uint32_t *s, *d, p3, src, wdt;
#if FELUCCA_CPU2
fm1_cpu1_halt(); /* (after Jangada, X0X): a second core still running from before
* this reset (a watchdog, a fault, another firmware) would read the RAM
* cleared below */
#endif
fm1_time_init();
fm1_reset_reason();
p3 = fm1_boot.p3_rst;
src = fm1_boot.rst_src;
wdt = fm1_boot.wdt_con;
fm1_wdt_arm(0x0D);
if (bootguard.magic != BOOTGUARD_MAGIC) {
bootguard.magic = BOOTGUARD_MAGIC;
bootguard.failed = 0;
bootguard.pending = 0;
}
if (bootguard.pending)
bootguard.failed++;
bootguard.pending = 1;
if (bootguard.failed >= 2u) {
bootguard.failed = 0;
bootguard.pending = 0;
fm1_enter_uboot();
}
fm1_irq_init();
for (d = _bss_start; d < _bss_end; d++)
*d = 0;
for (d = _pool_start; d < _pool_end; d++)
*d = 0;
for (s = _data_load, d = _data_start; d < _data_end; s++, d++)
*d = *s;
for (s = _rt_load, d = _rt_start; d < _rt_end; s++, d++)
*d = *s; /* flash driver code that must run from RAM */
#if FELUCCA_CPU2
for (s = _c1_load, d = _c1_start; d < _c1_end; s++, d++)
*d = *s; /* the second core's waiting loop (hal/fm1_cpu1.h) */
#endif
fm1_mailbox_clear();
fm1_guard_enable(FM1_GUARD_STACK | FM1_GUARD_WRITE | FM1_GUARD_BUS | FM1_GUARD_PC);
fm1_boot.p3_rst = (uint8_t)p3;
fm1_boot.rst_src = src;
fm1_boot.wdt_con = (uint8_t)wdt;
fm1_main();
for (;;)
;
}