Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,18 @@ clock and start / stop through the clock helper (`clock_follow(MIDI_BLE)`), and
`bt_status()` says whether it's advertising, connected or ready for MIDI. Notes held when the link drops get their
note-offs.

It can also connect out, to a Bluetooth MIDI controller or another FM-1: `ble_scan()` looks for 15 s, `ble_device(i)`
names the BLE MIDI devices found (with a rough signal), `ble_device_addr(i)` gives one's address (keep it to find the
device again later with `ble_find()`), `ble_connect(i)` connects, `ble_disconnect()` leaves it (a computer connected
to the FM-1 stays). A link not made in a few seconds is worth leaving and trying again. One link at a time, no
pairing. A clock from Bluetooth is placed by its senders' time stamps, so it comes out as evenly as it was sent.

On Felucca's platform it's Henri David's Bluetooth stack from [Optimist](https://github.com/w0ts/optimist) with the
radio drivers of [felucca-bsp](https://github.com/hugelton/felucca-bsp) (`platform/ble`, `platform/hal/fm1_ble*.h`,
`platform/bt.c`): written from the Bluetooth specifications and measurements of the FM-1's radio, no JieLi library.
It calibrates the radio when it starts, so nothing stored on the FM-1 is needed. It's a peripheral only: no pairing
(macOS and iOS connect without it), no connecting out to a controller (`ble_central` answers "not ready") and no
audio. Tested on an FM-1 with a Mac, both ways.
It starts the radio with the FM-1's own calibration, as the stock firmware left it in flash (the defaults when it's
gone), and measures the radio's VCO bands at every start (with another unit's, reception went bad seconds after the
start). No pairing (macOS and iOS connect without it) and no audio. Tested on FM-1s with a Mac and with each other.

To connect from a Mac: Audio MIDI Setup, Window > Show MIDI Studio, the Bluetooth button, then Connect.

Expand Down Expand Up @@ -158,7 +164,10 @@ The FM-1 runs a JieLi AC791N. The emulator is forgiving, the chip is not; the bu
`./fm1 flash` runs on your computer, not in Docker (Docker on a Mac can't reach USB), and needs only `python3`
(CoreMIDI on macOS, ALSA on Linux; not on Windows yet: use the page). `./fm1 flash` installs `build/web/firmware.fwsc`,
`./fm1 flash FILE.fwsc` any package, `./fm1 flash --id` says what the FM-1 runs (`FM-1_000`: the USB rescue). Quit
other apps that use the FM-1's MIDI port first. If an install is cut off, run it again: it finishes it.
other apps that use the FM-1's MIDI port first. If an install is cut off, run it again: it finishes it. With more than
one FM-1 on USB, `./fm1 flash --list` numbers them and `--port N` first picks one (`./fm1 flash --port 2`).
`./fm1 flash --crash` shows the last crash the FM-1 recorded (the USB rescue answers it too); with Bluetooth,
`--bt` shows the link and what the last scan found, and `--radio` which radio calibration it started with.

Windows: `fm1.cmd dev` (Docker Desktop). Without the script:
`docker run --rm -it --platform linux/amd64 -p 8000:8000 -v "$PWD:/work" konsumer/foma-toolchain python3 tools/build.py --dev 8000`.
Expand Down
3 changes: 2 additions & 1 deletion platform/ble/ble_hw_wl82_central.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ BLE_API void ble_hw_scan_start(const struct ble_hw_scan *s)
CB->wincntl2 = HW_SCAN_WIN2;
fm1_ble_col_wr(HW_LINK, 1, iv & 0xFFFFu);
fm1_ble_col_wr(HW_LINK, 15, iv >> 16); /* bit15 = 0 for scanning (its meaning: C8) */
CB->filtercntl &= (uint16_t)~8u; /* 5: filter policy 0 (no whitelist) */
CB->filtercntl &= (uint16_t)~(8u | 0x10u | 1u); /* 5: filter policy 0 (no whitelist; initiating's target
* entry and its CONNECT_IND-only bit off too) */
drv.scan_active = s->active ? 1u : 0u;
CB->format = (uint16_t)(drv.scan_active ? 0u : 0x100u); /* 6: bit8 0 = SCAN_REQ allowed */
fm1_ble_col_wr(HW_LINK, 6, 0x2100u | 37u); /* 7: channel 37 first */
Expand Down
276 changes: 261 additions & 15 deletions platform/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,30 @@
* (IRQ 45 and 29, priority 2: below the audio and TIMER5) and talks to the rest through single-producer rings:
* in: the BLE interrupts write, the TIMER5 tick moves it to the MIDI ring (and the clock) as cable 2;
* out: midi_send writes (interrupts off), the BLE interrupts send it.
* The FM-1 is a BLE MIDI peripheral: a computer or phone connects to it. No pairing (macOS and iOS connect anyway),
* no central role, no audio. */
* The FM-1 is a BLE MIDI peripheral (a computer or phone connects to it), and it can connect to one itself: ble_scan,
* ble_device, ble_connect (the central role, at the stack's minimal level: no pairing, so a controller or another
* FM-1, not a device that insists on pairing). One link at a time. No pairing as a peripheral either (macOS and
* iOS connect anyway), no audio. */
#ifndef BLE_DEVICE_NAME
#define BLE_DEVICE_NAME APP_NAME /* advertised as "NAME XXXX", XXXX from this FM-1's address */
#endif
#define FELUCCA_BLE_DIAG 0
#define BLE_CENTRAL 0
#ifndef BLE_CENTRAL
#define BLE_CENTRAL 3 /* the central role without pairing (ble_cfg.h) */
#endif
#include "ble/ble_stack.c"
#include "ble/ble_vm.c" /* the stock firmware's stored radio records (read only) */

/* the SDK board's constant records (crystal load, PA bias; the Wi-Fi rates' gains left as they are): as fm1-base's
* bt_own.c. The radio's own record (187) is measured at every start (fm1_rf_cal.h) */
static const struct ble_rf_trims BT_TRIMS = {{0x0B, 0x0B}, {1, 7, 4, 7, 11, 1, 7}, {0}, {0}};
/* the radio's four records (106, 107, 108, 187): this FM-1's own from the stock firmware's VM when it's still there
* (ble_vm.c: the factory's, read only), else the SDK board's constant ones with the captured start-up's 187 fields
* (as fm1-base's bt_own.c, without its measuring). Our own calibration (hal/fm1_rf_cal.h) is not run: the radio's
* writes after it, in group 10 of the start-up, left the chip hanging or faulting in most builds (fomajam with or
* without the central role, the code in flash or in RAM, 2026-10-31), and ran only in some, by their layout */
static struct ble_rf_trims bt_trims = {{0x0B, 0x0B}, {1, 7, 4, 7, 11, 1, 7}, {0}, {0}};
static volatile struct { /* which records the last start used, kept over a reset (fm1.c platform_frame 0x14) */
uint32_t magic, src, area, nrec;
uint8_t x187[68], pad[44];
} bt_rf_kept __attribute__((section(".noinit.ble")));

static void bt_audio_hold(int hold); /* fm1.c: the audio stopped (1) or going again (0) */

Expand All @@ -34,7 +46,18 @@ static struct {
static volatile uint32_t bo_w, bo_r;
static uint8_t bt_addr[6];
static volatile uint8_t bt_up, bt_on, bt_live; /* the radio started; switched on; a central listens for MIDI */
static uint8_t bt_held[16][16]; /* notes a central holds on: ended when the link goes */
static uint8_t bt_held[16][16];
/* notes a central holds on: ended when the link goes */
#if BLE_CENTRAL
static struct ble_scan_tab bt_scan; /* the BLE MIDI devices around (ble_scan) */
static uint32_t bt_scan_end, bt_aged;
static uint32_t bt_adv_at; /* when our scan's end or a cancel started advertising again (fm1_ms) */
/* advertising stopped right after it started leaves the engine deaf in the next mode (nothing heard while scanning or
* initiating, 2026-10-31): a scan or a connect waits until advertising has run BT_SETTLE_MS */
#define BT_SETTLE_MS 250u
static int bt_settled(void) { return fm1_ms - bt_adv_at >= BT_SETTLE_MS; }
static uint8_t bt_role; /* ble_central */
#endif

/* ---- the stack's side: the BLE interrupts */
BLE_API int ble_app_midi_peek(uint32_t *pkt, uint32_t *t_ms) {
Expand All @@ -50,11 +73,29 @@ BLE_API void ble_app_midi_pop(void) {
bo_r++;
}
}
/* when a message was meant, from its BLE-MIDI time stamp (the sender's ms, 13 bits): our ms minus its stamp is the
* two clocks' offset plus the trip; its smallest (the quickest trip) is kept, creeping up 1 ms every 2 s to follow the
* clocks' drift. A message's time is its arrival less what it was late past that, plus BT_CLOCK_DELAY_MS, so a clock
* comes out as evenly as it was sent (clock.c holds a tick that isn't due yet) */
#define BT_CLOCK_DELAY_MS 6u
static uint32_t bt_when(uint16_t ts) {
static uint32_t off, off_at;
static uint8_t have;
uint32_t now = fm1_ticks(), ms = fm1_ms, d = (ms - ts) & 8191u, late = (d - off) & 8191u;
if (!have || late > 4096u) { /* the quickest trip yet (or the first) */
off = d, have = 1, late = 0, off_at = ms;
} else if (ms - off_at > 2000u) {
off = (off + 1u) & 8191u, off_at = ms, late = late ? late - 1u : 0u;
}
late = late > 50u ? 50u : late; /* (a stamp far off: as it came) */
return now - late * 1000u * FM1_TICKS_PER_US + BT_CLOCK_DELAY_MS * 1000u * FM1_TICKS_PER_US;
}

BLE_API void ble_app_midi_in(uint32_t pkt, uint16_t ts, uint16_t last_ts) {
(void)ts, (void)last_ts;
(void)last_ts;
if (bi_w - bi_r < BQ) {
bt_in[bi_w % BQ].pkt = pkt & ~0xF0u;
bt_in[bi_w % BQ].t = fm1_ticks();
bt_in[bi_w % BQ].t = bt_when(ts);
RING_PUBLISH();
bi_w++;
}
Expand Down Expand Up @@ -151,6 +192,11 @@ static void bt_addr_make(void) {
bt_addr[5] = (uint8_t)((g >> 24) | 0xC0u); /* static random: the top two bits set */
}

static int bt_vm_read(void *ctx, uint32_t off, uint8_t *dst, uint32_t n) { /* ble_vm_scan's flash reads */
(void)ctx;
return st_read(off, dst, n);
}

/* the first bt_enable: the two BLE vectors (masked), the radio and its calibration, then advertising. Nothing of
* this runs at boot: a firmware that never turns Bluetooth on never touches the radio (attaching the vectors at
* boot crashed the FM-1) */
Expand All @@ -163,10 +209,26 @@ static void bt_start(void) {
fm1_guard_lock_top();
irq_restore(f);
ble_init(bt_addr, 1);
fm1_rf_cal_on = 1;
fm1_wdt_arm(0x0F); /* (the radio's start-up and calibration take a while) */
{
struct ble_vm_info vi;
struct ble_rf_trims vm;
uint32_t i, src = flash_ok && ble_vm_scan(bt_vm_read, 0, &vi, &vm, 0, 0) ? 1u : 0u;
if (src) {
bt_trims = vm;
}
bt_rf_kept.magic = 0x52464B54u; /* "TKFR" */
bt_rf_kept.src = src;
bt_rf_kept.area = src ? vi.area : 0u;
bt_rf_kept.nrec = src ? vi.nrec : 0u;
for (i = 0; i < 68u; i++) {
bt_rf_kept.x187[i] = bt_trims.x187[i];
}
}
fm1_rf_cal_on = 2; /* this unit's own VCO bands (the replay's are another FM-1's: reception went bad seconds
* after the start), the stored 187 for the rest (our trim hung the start-up: hal/fm1_ble_rf.h) */
fm1_wdt_arm(0x0F); /* (the radio's start-up takes a while) */
bt_audio_hold(1); /* the audio interrupt paused: a long audio() in the start-up's timed sequences hangs it */
ble_hw_wl82_start(&BT_TRIMS);
ble_hw_wl82_start(&bt_trims);
bt_audio_hold(0);
fm1_wdt_arm(0x0D); /* (as fm1_cstart arms it) */
bt_up = 1;
Expand All @@ -187,19 +249,203 @@ int bt_enable(int features) {
return 0;
}

#if BLE_CENTRAL
void bt_status(bt_status_t *s) {
uint8_t idx[BLE_SCAN_N], cs = bt_up ? ble_central_state() : BLE_CS_IDLE;
*s = (bt_status_t){0};
s->ready = bt_up;
s->ble_state = !bt_up || !bt_on ? BLE_OFF : bt_live ? BLE_READY
s->ble_role = bt_role;
s->ble_count = (uint8_t)(bt_up ? ble_scan_list(&bt_scan, idx) : 0u);
s->ble_error = bt_up ? ble_central_fail() : 0;
s->ble_state = !bt_up || !bt_on ? BLE_OFF
: bt_live ? BLE_READY
: cs == BLE_CS_CONNECTING ? BLE_CONNECTING
: cs == BLE_CS_SETUP ? BLE_DISCOVERING
: ble_connected() ? BLE_CONNECTED
: bt_scan_end && ble_ll_scanning() ? BLE_SCANNING
: BLE_ADVERTISING;
}

/* the main loop (platform_poll): the scan's reports into the list, and its end */
static void bt_loop(void) {
uint8_t pdu[40];
uint16_t rssi;
uint8_t n;
if (!bt_up) {
return;
}
while ((n = ble_ll_scan_take(pdu, &rssi)) != 0) {
ble_scan_add(&bt_scan, pdu, n, rssi, fm1_ms);
}
if (fm1_ms - bt_aged > 1000u) {
bt_aged = fm1_ms;
ble_scan_age(&bt_scan, fm1_ms);
}
if (bt_scan_end && (int32_t)(fm1_ms - bt_scan_end) >= 0) {
bt_scan_end = 0;
fm1_ble_irqs_hold(1);
ble_ll_scan(0);
fm1_ble_irqs_hold(0);
bt_adv_at = fm1_ms;
}
}

/* ---- the central role: the FM-1 connects to a BLE MIDI device (a controller, another FM-1) */
int ble_central(int on) {
bt_role = (uint8_t)(on != 0);
if (!on) {
ble_disconnect();
}
return 0;
}
int ble_scan(void) {
if (!bt_up || !bt_on) {
return -3;
}
if (!bt_settled()) {
return -2; /* (busy: ask again) */
}
ble_scan_clear(&bt_scan);
bt_scan_end = (fm1_ms + 15000u) | 1u;
fm1_ble_irqs_hold(1);
ble_ll_scan(1);
fm1_ble_irqs_hold(0);
return 0;
}
int ble_device(int i, char name[25], int *rssi) {
uint8_t idx[BLE_SCAN_N];
uint32_t n = bt_up ? ble_scan_list(&bt_scan, idx) : 0u, k;
if (i < 0 || (uint32_t)i >= n) {
return -1;
}
const struct ble_found *e = &bt_scan.e[idx[i]];
for (k = 0; k < 24u && e->name[k]; k++) {
name[k] = e->name[k];
}
name[k] = 0;
if (rssi) { /* (no gain table: the list's three bars as a rough dBm) */
*rssi = -100 + 20 * (int)ble_scan_bars(&bt_scan, idx[i]);
}
return 0;
}
int ble_device_addr(int i, uint8_t addr[6], int *random) {
uint8_t idx[BLE_SCAN_N];
uint32_t n = bt_up ? ble_scan_list(&bt_scan, idx) : 0u;
if (i < 0 || (uint32_t)i >= n) {
return -1;
}
ble_cpy(addr, bt_scan.e[idx[i]].addr, 6);
if (random) {
*random = bt_scan.e[idx[i]].addr_rand;
}
return 0;
}
int ble_find(const uint8_t addr[6], int random, const char *name) {
uint8_t idx[BLE_SCAN_N];
uint32_t n = bt_up ? ble_scan_list(&bt_scan, idx) : 0u, i, k;
for (i = 0; i < n; i++) { /* its address */
const struct ble_found *e = &bt_scan.e[idx[i]];
if (e->addr_rand == (uint8_t)(random != 0) && ble_eq(e->addr, addr, 6)) {
return (int)i;
}
}
for (i = 0; name && name[0] && i < n; i++) { /* else its name (an address that changes) */
const struct ble_found *e = &bt_scan.e[idx[i]];
for (k = 0; name[k] && e->name[k] == name[k]; k++) {
}
if (!name[k] && !e->name[k]) {
return (int)i;
}
}
return -1;
}
int ble_connect(int i) {
uint8_t idx[BLE_SCAN_N];
uint32_t n = bt_up && bt_on ? ble_scan_list(&bt_scan, idx) : 0u;
struct ble_peer p;
int ok;
if (i < 0 || (uint32_t)i >= n) {
return -1;
}
if (!bt_settled()) {
return -2;
}
ble_zero((uint8_t *)&p, sizeof p);
ble_cpy(p.addr, bt_scan.e[idx[i]].addr, 6);
p.addr_rand = bt_scan.e[idx[i]].addr_rand;
bt_scan_end = 0;
fm1_ble_irqs_hold(1);
ok = ble_central_connect(&p); /* (from the scan: it stops it. A scan stopped first starts advertising again, and
* initiating right after that left the engine deaf: nothing heard, no link) */
ble_ll_scan(0); /* (initiating: only the wish to scan goes, so the scan doesn't come back after) */
fm1_ble_irqs_hold(0);
return ok ? 0 : -2;
}
int ble_disconnect(void) {
if (!bt_up) {
return -3;
}
fm1_ble_irqs_hold(1);
ble_central_cancel(); /* (a computer connected to us stays) */
fm1_ble_irqs_hold(0);
bt_adv_at = fm1_ms;
return 0;
}
int ble_forget(void) { return 0; } /* (no pairings are kept) */

#else
/* (BLE_CENTRAL 0: a peripheral only) */
void bt_status(bt_status_t *s) {
*s = (bt_status_t){0};
s->ready = bt_up;
s->ble_state = !bt_up || !bt_on ? BLE_OFF : bt_live ? BLE_READY
: ble_connected() ? BLE_CONNECTED
: BLE_ADVERTISING;
}

/* not on this platform: the central role (connecting to a controller) and audio from Bluetooth */
static void bt_loop(void) {}
int ble_central(int on) { return on ? -3 : 0; }
int ble_scan(void) { return -3; }
int ble_device(int i, char name[25], int *rssi) { return (void)i, (void)name, (void)rssi, -1; }
int ble_device_addr(int i, uint8_t addr[6], int *random) { return (void)i, (void)addr, (void)random, -1; }
int ble_find(const uint8_t addr[6], int random, const char *name) { return (void)addr, (void)random, (void)name, -1; }
int ble_connect(int i) { return (void)i, -3; }
int ble_disconnect(void) { return -3; }
int ble_forget(void) { return -3; }
#endif

#if BLE_CENTRAL
/* for ./fm1 flash --bt (fm1.c platform_frame): 0x15 the state, 0x16 scan entry i. 32 bytes each */
static void bt_report(uint32_t cmd, uint32_t i, uint8_t o[32]) {
uint8_t idx[BLE_SCAN_N];
uint32_t n = bt_up ? ble_scan_list(&bt_scan, idx) : 0u, k;
bt_status_t b;
for (k = 0; k < 32u; k++) {
o[k] = 0;
}
if (cmd == 0x15u) {
bt_status(&b);
o[0] = bt_up, o[1] = bt_on, o[2] = bt_live, o[3] = bt_role, o[4] = b.ble_state;
o[5] = bt_up ? ble_central_state() : 0u, o[6] = bt_up ? ble_central_fail() : 0u, o[7] = bt_up ? ble_central_code() : 0u;
o[8] = (uint8_t)(bt_up && ble_connected()), o[9] = (uint8_t)n, o[10] = (uint8_t)(bt_up && ble_ll_scanning()), o[11] = drv.state;
ble_cpy(o + 12, bcen.peer.addr, 6);
o[18] = bcen.peer.addr_rand, o[19] = drv.master;
for (k = 0; k < 4u; k++) {
o[20 + k] = (uint8_t)(ble_hw_stat.connects >> 8 * k), o[24 + k] = (uint8_t)(ble_hw_stat.rx >> 8 * k);
o[28 + k] = (uint8_t)(bt_scan.reports >> 8 * k);
}
} else if (cmd == 0x19u) { /* our own address */
ble_cpy(o, bt_addr, 6);
} else if (i < n) {
const struct ble_found *e = &bt_scan.e[idx[i]];
o[0] = 1, o[1] = e->midi, o[2] = e->addr_rand, o[3] = e->hits;
ble_cpy(o + 4, e->addr, 6);
o[10] = (uint8_t)e->rssi, o[11] = (uint8_t)(e->rssi >> 8);
for (k = 0; k < 20u && e->name[k]; k++) {
o[12 + k] = (uint8_t)e->name[k];
}
}
}
#endif

/* not on this platform: audio from Bluetooth */
const int32_t *bt_audio_in(void) { return 0; }
Loading
Loading