initial commit
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
// Adafruit Watchdog Library Basic Usage Example
|
||||
//
|
||||
// Simple example of how to use the watchdog library.
|
||||
//
|
||||
// Author: Tony DiCola
|
||||
|
||||
#ifdef USE_TINYUSB
|
||||
#include <Adafruit_TinyUSB.h> // Include TinyUSB Serial port
|
||||
#endif
|
||||
|
||||
#include <Adafruit_SleepyDog.h>
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
while (!Serial)
|
||||
delay(10);
|
||||
// wait for Arduino Serial Monitor (native USB boards)
|
||||
|
||||
Serial.println("Adafruit Watchdog Library Demo!");
|
||||
Serial.println();
|
||||
|
||||
// First a normal example of using the watchdog timer.
|
||||
// Enable the watchdog by calling Watchdog.enable() as below.
|
||||
// This will turn on the watchdog timer with a ~4 second timeout
|
||||
// before reseting the Arduino. The estimated actual milliseconds
|
||||
// before reset (in milliseconds) is returned.
|
||||
// Make sure to reset the watchdog before the countdown expires or
|
||||
// the Arduino will reset!
|
||||
int countdownMS = Watchdog.enable(4000);
|
||||
Serial.print("Enabled the watchdog with max countdown of ");
|
||||
Serial.print(countdownMS, DEC);
|
||||
Serial.println(" milliseconds!");
|
||||
Serial.println();
|
||||
|
||||
// Now loop a few times and periodically reset the watchdog.
|
||||
Serial.println("Looping ten times while resetting the watchdog...");
|
||||
for (int i = 1; i <= 10; ++i) {
|
||||
Serial.print("Loop #");
|
||||
Serial.println(i, DEC);
|
||||
delay(1000);
|
||||
// Reset watchdog with every loop to make sure the sketch keeps running.
|
||||
// If you comment out this call watch what happens in about 4 iterations!
|
||||
Watchdog.reset();
|
||||
}
|
||||
Serial.println();
|
||||
|
||||
// can not disable NRF or RP2040 wdt once enabled
|
||||
#if !defined(NRF52_SERIES) && !defined(ARDUINO_ARCH_RP2040)
|
||||
// Disable the watchdog entirely by calling Watchdog.disable();
|
||||
Watchdog.disable();
|
||||
#endif
|
||||
|
||||
// Finally demonstrate the watchdog resetting by enabling it for a shorter
|
||||
// period of time and waiting a long time without a reset. Notice you can
|
||||
// pass a _maximum_ countdown time (in milliseconds) to the enable call.
|
||||
// The library will try to use that value as the countdown, but it might
|
||||
// pick a smaller value if the hardware doesn't support it. The actual
|
||||
// countdown value will be returned so you can see what it is.
|
||||
countdownMS = Watchdog.enable(4000);
|
||||
Serial.print("Get ready, the watchdog will reset in ");
|
||||
Serial.print(countdownMS, DEC);
|
||||
Serial.println(" milliseconds!");
|
||||
Serial.println();
|
||||
#ifndef ARDUINO_ARCH_ESP8266
|
||||
delay(countdownMS + 1000);
|
||||
#else
|
||||
// Calls to delay() and yield() feed the ESP8266's
|
||||
// hardware and software watchdog timers, delayMicroseconds does not.
|
||||
delayMicroseconds(countdownMS * 1000);
|
||||
#endif
|
||||
|
||||
// Execution will never get here because the watchdog resets the Arduino!
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// We'll never actually get to the loop because the watchdog will reset in
|
||||
// the setup function.
|
||||
Serial.println("You shouldn't see this message.");
|
||||
delay(1000);
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
// Adafruit Watchdog Library Sleep Example
|
||||
//
|
||||
// Simple example of how to do low power sleep with the watchdog timer.
|
||||
//
|
||||
// Author: Tony DiCola
|
||||
|
||||
#ifdef USE_TINYUSB
|
||||
#include <Adafruit_TinyUSB.h> // Include TinyUSB Serial port
|
||||
#endif
|
||||
|
||||
#include <Adafruit_SleepyDog.h>
|
||||
|
||||
void setup() {
|
||||
// For boards with "native" USB support (e.g. not using an FTDI chip or
|
||||
// similar serial bridge), Serial connection may be lost on sleep/wake,
|
||||
// and you might not see the "I'm awake" messages. Use the onboard LED
|
||||
// as an alternate indicator -- the code turns it on when awake, off
|
||||
// before going to sleep.
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, HIGH); // Show we're awake
|
||||
|
||||
Serial.begin(115200);
|
||||
while(!Serial); // wait for Arduino Serial Monitor (native USB boards)
|
||||
Serial.println("Adafruit Watchdog Library Sleep Demo!");
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("Going to sleep in one second...");
|
||||
delay(1000);
|
||||
|
||||
// To enter low power sleep mode call Watchdog.sleep() like below
|
||||
// and the watchdog will allow low power sleep for as long as possible.
|
||||
// The actual amount of time spent in sleep will be returned (in
|
||||
// milliseconds).
|
||||
digitalWrite(LED_BUILTIN, LOW); // Show we're asleep
|
||||
int sleepMS = Watchdog.sleep();
|
||||
|
||||
// Alternatively you can provide a millisecond value to specify
|
||||
// how long you'd like the chip to sleep, but the hardware only
|
||||
// supports a limited range of values so the actual sleep time might
|
||||
// be smaller. The time spent in sleep will be returned (in
|
||||
// milliseconds).
|
||||
// int sleepMS = Watchdog.sleep(1000); // Sleep for up to 1 second.
|
||||
|
||||
// Code resumes here on wake.
|
||||
|
||||
digitalWrite(LED_BUILTIN, HIGH); // Show we're awake again
|
||||
|
||||
// Try to reattach USB connection on "native USB" boards (connection is
|
||||
// lost on sleep). Host will also need to reattach to the Serial monitor.
|
||||
// Seems not entirely reliable, hence the LED indicator fallback.
|
||||
#if defined(USBCON) && !defined(USE_TINYUSB)
|
||||
USBDevice.attach();
|
||||
#endif
|
||||
|
||||
Serial.print("I'm awake now! I slept for ");
|
||||
Serial.print(sleepMS, DEC);
|
||||
Serial.println(" milliseconds.");
|
||||
Serial.println();
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
// Adafruit Watchdog Library - RP2350 Pin/GPIO Sleep Example
|
||||
//
|
||||
// Simple example for using the Adafruit Watchdog Library to put the RP2350 to
|
||||
// sleep until a GPIO pin changes state.
|
||||
//
|
||||
// Brent Rubell for Adafruit Industries
|
||||
//
|
||||
// NOTE: After uploading, physically unplug and replug the USB cable to
|
||||
// reset the AON timer correctly before running.
|
||||
|
||||
#include <Adafruit_SleepyDog.h>
|
||||
#ifdef USE_TINYUSB
|
||||
#include <Adafruit_TinyUSB.h>
|
||||
#else
|
||||
#include "USB.h"
|
||||
#endif
|
||||
#define WAKE_PIN 2 // GPIO pin to wake on, change as needed for your use case
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
Serial.begin(115200);
|
||||
// while (!Serial);
|
||||
Serial.println("Adafruit Watchdog Library - RP2350 Sleep Pin/GPIO Demo!");
|
||||
Serial.println();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("Going to sleep in 5 seconds...");
|
||||
Serial.println("To wake up, connect GPIO pin 2 to 3.3V (for HIGH wake).");
|
||||
delay(5000);
|
||||
|
||||
// Enter Sleep mode for 5 seconds
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
|
||||
// Sleep until GPIO WAKE_PIN goes HIGH on a rising edge
|
||||
Watchdog.goToSleepUntilPin(WAKE_PIN, true, true);
|
||||
|
||||
// Sleep until GPIO WAKE_PIN goes HIGH on a falling edge
|
||||
// Watchdog.goToSleepUntilPin(WAKE_PIN, false, true);
|
||||
|
||||
// Sleep until GPIO WAKE_PIN goes LOW on a rising edge
|
||||
// Watchdog.goToSleepUntilPin(WAKE_PIN, true, false);
|
||||
|
||||
// Sleep until GPIO WAKE_PIN goes LOW on a falling edge
|
||||
// Watchdog.goToSleepUntilPin(WAKE_PIN, false, false);
|
||||
|
||||
// Re-enable clocks, generators, USB and resume execution
|
||||
// NOTE: This MUST be called to properly resume from sleep!
|
||||
Watchdog.resumeFromSleep();
|
||||
#ifndef USE_TINYUSB
|
||||
// Re-enable USB after sleep. Required for Windows where USB
|
||||
// does not re-enumerate automatically. On macOS and Linux, USB is
|
||||
// maintained across sleep cycles. If you use an external serial terminal
|
||||
// (e.g. screen, minicom), comment out USB.disconnect() and USB.connect()
|
||||
// to prevent the port from disconnecting on each wake cycle.
|
||||
USB.disconnect();
|
||||
delay(500); // Give host time to register disconnect before reconnecting
|
||||
USB.connect();
|
||||
#endif
|
||||
|
||||
// NOTE: We can not track sleep duration when waking from a pin because we use
|
||||
// the crystal oscillator as the dormant clock source, so the AON timer we'd
|
||||
// use to track sleep duration is turned off. If you need to track sleep
|
||||
// duration, see the SleepRP2350Timer example which uses the AON timer to wake
|
||||
// from sleep.
|
||||
Serial.println("I'm awake now!");
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// Adafruit Watchdog Library - RP2350 Timer Sleep Example
|
||||
//
|
||||
// Simple example for using the Adafruit Watchdog Library to put the RP2350 to
|
||||
// sleep until the AON timer expires. NOTE: This example exposes both the Sleep
|
||||
// State (6.5.2 in RP2350 Datasheet) and the Dormant State (6.5.3 in RP2350
|
||||
// Datasheet) APIs.
|
||||
//
|
||||
// Brent Rubell for Adafruit Industries
|
||||
//
|
||||
// NOTE: After uploading, physically unplug and replug the USB cable to
|
||||
// reset the AON timer correctly before running.
|
||||
|
||||
#include <Adafruit_SleepyDog.h>
|
||||
#ifdef USE_TINYUSB
|
||||
#include <Adafruit_TinyUSB.h>
|
||||
#else
|
||||
#include "USB.h"
|
||||
#endif
|
||||
|
||||
static bool awake;
|
||||
|
||||
// This function will be called when the device wakes from sleep
|
||||
// You can add any custom behavior you want here
|
||||
void cbWake() {
|
||||
// Show we're awake again
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
awake = true;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
pinMode(LED_BUILTIN, OUTPUT);
|
||||
digitalWrite(LED_BUILTIN, HIGH);
|
||||
|
||||
Serial.begin(115200);
|
||||
// while(!Serial);
|
||||
Serial.println("Adafruit Watchdog Library - RP2350 Sleep Timer Demo!");
|
||||
Serial.println();
|
||||
|
||||
// When the device wakes back up, we'll call this callback function
|
||||
// to indicate that we've resumed from sleep.
|
||||
Watchdog.setWakeCb(cbWake);
|
||||
}
|
||||
|
||||
void loop() {
|
||||
Serial.println("Going to sleep in 5 seconds...");
|
||||
delay(5000);
|
||||
|
||||
// Enter Sleep mode for 5 seconds
|
||||
awake = false;
|
||||
digitalWrite(LED_BUILTIN, LOW);
|
||||
// Enter sleep state (6.5.2 in RP2350 Datasheet)
|
||||
Watchdog.goToSleepUntil(5000);
|
||||
// Uncomment the line below (and comment the line above) to enter Dormant
|
||||
// State (6.5.3 in RP2350 Datasheet) instead of the Sleep State above
|
||||
// Watchdog.goToSleepUntil(5000, true);
|
||||
|
||||
// Make sure we don't wake
|
||||
while (!awake) {
|
||||
Serial.println("Should be sleeping here...");
|
||||
}
|
||||
|
||||
// Re-enable clocks, generators, USB and resume execution
|
||||
// NOTE: This MUST be called to properly resume from sleep!
|
||||
Watchdog.resumeFromSleep();
|
||||
#ifndef USE_TINYUSB
|
||||
// Re-enable USB after sleep. Required for Windows where USB
|
||||
// does not re-enumerate automatically. On macOS and Linux, USB is
|
||||
// maintained across sleep cycles. If you use an external serial terminal
|
||||
// (e.g. screen, minicom), comment out USB.disconnect() and USB.connect()
|
||||
// to prevent the port from disconnecting on each wake cycle.
|
||||
USB.disconnect();
|
||||
delay(500); // Give host time to register disconnect before reconnecting
|
||||
USB.connect();
|
||||
#endif
|
||||
|
||||
Serial.println("I'm awake now!");
|
||||
Serial.print("Slept for approximately ");
|
||||
long sleep_duration = Watchdog.getSleepDuration();
|
||||
Serial.print(sleep_duration);
|
||||
Serial.println(" milliseconds.");
|
||||
}
|
||||
Reference in New Issue
Block a user