I haven’t been able to find great answers as to what is going on with my nrf52840. Consider the following code:
#include <Adafruit_SPIFlash.h>
#include <Arduino.h>
#include <U8g2lib.h>
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif
#include <nrf52840.h>
Adafruit_FlashTransport_QSPI flashTransport;
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/U8X8_PIN_NONE);
#define SLEEP_TIMEOUT 7000
#define PIN_WAKEUP (28)
unsigned long startTimeoutTime = 0;
void QSPIF_sleep(void) {
flashTransport.begin();
flashTransport.runCommand(0xB9);
flashTransport.end();
}
void setup() {
u8g2.begin();
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
digitalWrite(LED_RED, HIGH);
digitalWrite(LED_GREEN, HIGH);
QSPIF_sleep();
digitalWrite(LED_GREEN, LOW);
delay(1000);
digitalWrite(LED_GREEN, HIGH);
NRF_POWER->DCDCEN = 1;
}
void loop() {
if (startTimeoutTime == 0) startTimeoutTime = millis();
if (startTimeoutTime > 0 && ((millis() - startTimeoutTime) > SLEEP_TIMEOUT)) {
startTimeoutTime = 0;
enterStandbySleep();
} else {
u8g2.setPowerSave(0);
u8g2.clearBuffer();
u8g2.setFont(u8g2_font_profont11_mf);
u8g2.setDisplayRotation(U8G2_R3);
u8g2.setCursor(10, 10);
u8g2.print(F("Hello!"));
u8g2.sendBuffer();
}
}
void enterStandbySleep() {
digitalWrite(LED_RED, LOW);
delay(1000);
digitalWrite(LED_RED, HIGH);
u8g2.setPowerSave(1);
NRF_GPIOTE->INTENCLR |= 0x80000000; // disable interrupt for event PORT
nrf_gpio_cfg_sense_input(PIN_WAKEUP, NRF_GPIO_PIN_PULLDOWN, NRF_GPIO_PIN_SENSE_HIGH);
NRF_GPIOTE->EVENTS_PORT = 0; // Event not generated
NRF_GPIOTE->INTENSET |= 0x80000000; // enable interrupt for event PORT
NRF_POWER->SYSTEMOFF = 1;
}
It seems to work fine when plugged in to my computer. Green LED blinks, display says “Hello!” for 7 seconds, red LED blinks, and it goes to deep sleep. Then clicking a button on pin 2 wakes it up.
If you power the XIAO with a battery though (in my case, and 9v passed through a buck converter to get it 3.7) it will not stay asleep. After the red LED blinks and it goes to sleep, it wakes right back up and the green LED blinks. It will occasionally actually stay asleep, but not often.
I am concerned this is because of the battery itself. I’ve read in a few posts that trying to power too much with a 9v can cause it to dip and reset the microcontroller. Kinda feel like just trying to put it to deep sleep shouldn’t be that power intensive though?
Anyway, been working on this days with nothing to show for it now.