DeepSleep on MG24 may cause it to stop accepting new sketch upload. Currently, there is no way to recover from this, as there is no published method on the Wiki to put the MG24 into “boot mode”. Caution is required.
A possible interim solution is to detect the user switch in the setup() so that it enters an infinite loop, and upload the sketch while it is looping.
I have experimented with the following sketch and it works.
We are waiting for a formal countermeasure as soon as possible.
//----------------------------------------------------------------------------------------------
// BoardBoard Library : Silicon Labs 2.2.0
// Board Select : Seeed Studio XIAO MG24 (Sense)
// Plotocol stack : None
//----------------------------------------------------------------------------------------------
// 2024/12/29 @msfujino
// IMPORTANT NOTE:Cannot upload new sketches during DeepSleep
#include <Arduino.h>
#include "ArduinoLowPower.h"
// User SW for upload
#define USER_SW PC3 // (3), D3 for example
// on board Flush SPI_1 pins
#define CS1 PA6 // (21)
#define CLK1 PA0 // (17), D17
#define MOSI1 PB0 // (15), D15
#define MISO1 PB1 // (16), D16
// on board peripherals pins
#define IMU_EN PD5 // (19)
#define MIC_EN PC8 // (22)
#define VBAT_EN PD3 // (25)
#define RFSW_EN PB5 // (27)
// Flash commands
#define READ_DATA 0x03
#define WRITE_ENABLE 0x06
#define PAGE_PROGRAM 0x02
#define SECTOR_ERASE 0x20
// Flash functions
void sendSPI(byte data) {
for (int i = 0; i < 8; i++) {
digitalWrite(MOSI1, data & 0x80);
data <<= 1;
digitalWrite(CLK1, HIGH);
delayMicroseconds(1);
digitalWrite(CLK1, LOW);
delayMicroseconds(1);
}
}
void writeEnable() {
digitalWrite(CS1, LOW);
sendSPI(WRITE_ENABLE);
digitalWrite(CS1, HIGH);
}
// ******************************************************************************
void setup()
{
Serial.begin(115200);
while(!Serial);
delay(2000);
Serial.println("Deep sleep / upload test");
// builtin LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
// on board flash
pinMode(CS1, OUTPUT);
pinMode(CLK1, OUTPUT);
pinMode(MOSI1, OUTPUT);
pinMode(MISO1, INPUT);
digitalWrite(CS1, HIGH); // CS1 HIGH
// on board peripherals
pinMode(IMU_EN, OUTPUT);
pinMode(MIC_EN, OUTPUT);
pinMode(VBAT_EN, OUTPUT);
pinMode(RFSW_EN, OUTPUT);
digitalWrite(IMU_EN, LOW); // IMU Power OFF
digitalWrite(MIC_EN, LOW); // MIC Power OFF
digitalWrite(VBAT_EN, LOW); // VBAT Power OFF
digitalWrite(RFSW_EN, LOW); // RFSW Power OFF
// Flash Deep Power Down
writeEnable();
digitalWrite(CS1, LOW);
sendSPI(0xB9);
digitalWrite(CS1, HIGH);
Serial.println("Need to stay awake to upload new sketches");
// if user sw is on, blink and enable to upload new sketch
pinMode(USER_SW, INPUT_PULLUP);
if(digitalRead(USER_SW) == LOW) {
Serial.println("enable to upload new sketch");
while(true) {
digitalWrite(LED_BUILTIN, LOW);
delay(50);
digitalWrite(LED_BUILTIN, HIGH);
delay(50);
}
}
Serial.println("end of setup");
}
// *******************************************************************************************
void loop()
{
// blink LED
digitalWrite(LED_BUILTIN, LOW);
delay(500);
digitalWrite(LED_BUILTIN, HIGH);
Serial.printf("Going to deep sleep");
// IMPORTANT NOTE:Cannot upload new sketches during DeepSleep
LowPower.deepSleep(10000);
}