Fix BLE upload: write-with-response on CMD+DATA, on-demand advertising, wake-play-random, remove startup blink

This commit is contained in:
zyphlar
2026-07-04 03:28:14 -07:00
parent d3bd7b71b9
commit 96d0c44d11
5 changed files with 68 additions and 39 deletions
+52 -23
View File
@@ -49,6 +49,11 @@
#include <Adafruit_TinyUSB.h>
#include <bluefruit.h>
// ============================================================
// When defined, a wake from deep sleep (button press) plays a random track
// immediately instead of waiting for user input.
//#define WAKE_PLAY_RANDOM
// ============================================================
// POC MODE: stream audio from internal LittleFS instead of SPI flash.
// Upload tracks via upload_track.py (serial) or BLE (CMD 0x02).
@@ -247,6 +252,10 @@ unsigned long g_lastActivity = 0;
// USB connected flag
volatile bool g_usbConnected = false;
// Wake-from-sleep detection. Read before SoftDevice starts (setupBLE), stored here.
static bool g_wakeFromSleep = false;
static uint8_t g_wakeTrack = 0;
#ifdef POC_INTERNAL_FLASH
static void pocFilename(uint8_t n, char *buf) { // buf must be >=16 bytes
snprintf(buf, 16, "/track%d.wav", n);
@@ -746,8 +755,10 @@ void audioCmd_write_cb(uint16_t conn_handle, BLECharacteristic* chr,
loadTrackTable();
Serial.println("BLE: Upload complete");
#else
// Signal loop() to finalize after ring buffer drains.
// Don't call loadTrackTable() here — we're in a BLE callback.
// Signal loop() to finalize once the ring buffer drains.
// Because the DATA characteristic uses write-with-response, every data
// packet has been ATT-acknowledged before the client sends this command,
// so all bytes are already in the ring buffer by the time we get here.
if (g_bleUploading) {
g_bleFinalizing = true;
Serial.println("BLE: Finalizing upload...");
@@ -1003,17 +1014,20 @@ void setupBLE() {
// Audio service
audioSvc.begin();
// Command characteristic (write without response — avoids "GATT already in progress" errors)
audioCmd.setProperties(CHR_PROPS_WRITE_WO_RESP);
// Command characteristic: write-with-response so JS can await full OS-level
// completion before issuing the next operation on any characteristic.
audioCmd.setProperties(CHR_PROPS_WRITE);
audioCmd.setPermission(SECMODE_OPEN, SECMODE_OPEN);
audioCmd.setMaxLen(240);
audioCmd.setWriteCallback(audioCmd_write_cb);
audioCmd.begin();
// Data characteristic (write without response for speed)
audioData.setProperties(CHR_PROPS_WRITE_WO_RESP);
// Data characteristic (write with response — ATT flow control ensures the
// SoftDevice has acknowledged every packet before the client sends the next,
// so CMD_UPLOAD_END can never race ahead of the data stream)
audioData.setProperties(CHR_PROPS_WRITE);
audioData.setPermission(SECMODE_OPEN, SECMODE_OPEN);
audioData.setMaxLen(240);
audioData.setMaxLen(244); // must match DATA_CHUNK in ble.js (MTU 247 - 3 = 244)
audioData.setWriteCallback(audioData_write_cb);
audioData.begin();
@@ -1324,6 +1338,17 @@ void waitButtonRelease(uint8_t btn) {
// SLEEP / WAKE
// ============================================================
// One byte from the nRF52840 hardware true random number generator.
static uint8_t hwRandom() {
NRF_RNG->CONFIG = RNG_CONFIG_DERCEN_Enabled << RNG_CONFIG_DERCEN_Pos; // bias correction on
NRF_RNG->EVENTS_VALRDY = 0;
NRF_RNG->TASKS_START = 1;
while (!NRF_RNG->EVENTS_VALRDY) {}
uint8_t v = (uint8_t)NRF_RNG->VALUE;
NRF_RNG->TASKS_STOP = 1;
return v;
}
void enterDeepSleep() {
// Don't sleep if any button is currently LOW — would wake instantly.
// Also catches floating pins that the internal pull-up isn't winning against.
@@ -1372,22 +1397,6 @@ void setup() {
digitalWrite(PIN_LED, HIGH);
Serial.begin(115200);
Serial.println("...");
int c=0;
while(c<10) {
// pinMode(c, OUTPUT);
delay(250);
digitalWrite(PIN_LED, HIGH);
// digitalWrite(c, HIGH);
// Serial.println("0");
delay(250);
digitalWrite(PIN_LED, LOW);
// digitalWrite(c, LOW);
// Serial.println(c);
c++;
}
Serial.println("=== Baby Mobile v2 ===");
Serial.println("nRF52840 + IS25LP128F + PAM8302A");
@@ -1444,11 +1453,31 @@ void setup() {
usb_msc.begin();
#endif
// Read RESETREAS and collect a random byte BEFORE starting the SoftDevice.
// Once Bluefruit.begin() enables S140, the SoftDevice owns NRF_RNG and
// NRF_POWER — direct register access after that point causes a hard fault.
g_wakeFromSleep = (NRF_POWER->RESETREAS & POWER_RESETREAS_OFF_Msk) != 0;
NRF_POWER->RESETREAS = 0xFFFFFFFFUL; // write-1-to-clear
if (g_wakeFromSleep) Serial.println("Woke from deep sleep.");
#ifdef WAKE_PLAY_RANDOM
if (g_wakeFromSleep && g_numTracks > 0) {
// hwRandom() is safe here — SoftDevice not yet started.
g_wakeTrack = hwRandom() % g_numTracks;
}
#endif
// BLE
setupBLE();
g_lastActivity = millis();
#ifdef WAKE_PLAY_RANDOM
if (g_wakeFromSleep && g_numTracks > 0) {
Serial.print("Auto-play random track "); Serial.println(g_wakeTrack);
audioStart(g_wakeTrack);
}
#endif
digitalWrite(PIN_LED, HIGH);
Serial.println("Ready! Plug in USB to upload audio, or press a button.");
}