// SPDX-License-Identifier: Apache-2.0 // Copyright (c) 2012-2024 Scott Penrose and WII5 Buoy contributors // // This file is part of WII5 Buoy firmware. // See LICENSE for full terms. /** * @file WII5Sh3dUtil.cpp * @brief Sh3d utility helpers: sleep, reset, and platform glue. */ /* Sh3dNode - Utils / Utilities: TODO 2024 - Check this is the main code for sleep * Date Time - Sync and format * Sleep - Management * Software reset * WatchDog Timer (internal and external) */ #include #include #include "WII5Sh3dUtil.h" #include "WII5Sh3dIO.h" #include #ifdef KINETISK // TODO See Snooze - https://github.com/duff2013/Snooze #else #ifdef WII5_LOWPOWERSLEEP #include #endif #endif #include #include // Just in case void WII5Sh3dUtil::begin() { } /* sleep - Put the whole device to sleep for "n" seconds. TODO: Wake on Radio option */ uint8_t WII5Sh3dUtil::sleep(uint32_t seconds) { // TODO - Make sure the LEDs are actually off // digitalWrite(LED1, HIGH); // digitalWrite(LED2, HIGH); // Deep sleep mode ! (NOTE: NMEA out?) console.printf(F("# SLEEP %lu sec\r\n"), seconds); console.flush(); // TODO these should be registered? and not used if not required // sh3dNodeRadio.sleep(); // sh3dNodeIO.sleep(); // sh3dNodeStorage.sleep(); sleepLastTime = now(); volatile uint32_t sleepCount = 0; pinMode(LED_1, OUTPUT); while ( (sleepCount * 8) < seconds) { digitalWrite(LED_1, LOW); #ifdef WDT_RESET pinMode(WDT_RESET, OUTPUT); digitalWrite(WDT_RESET, (sleepCount % 2) == 0 ? HIGH : LOW); #endif // Compare LowPower with work on Talk2 #ifdef KINETISK // TODO look up snooze #endif // List boards supported #ifdef WII5_LOWPOWERSLEEP LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF); #endif sleepCount++; uint8_t btn = sh3dNodeIO.buttonSleepCheck(); if (btn > 0) { console.flush(); console.printf(F("# !SLEEP Button %d %lu sec, %lu c\r\n"), btn, seconds, sleepCount); sleepLastSeconds = sleepCount * 8; sleepLastReason = 10 + btn; return 10 + btn; } digitalWrite(LED_1,HIGH); } // TODO Fix time // cli(); // timer0_millis += 64000; //TODO should use multiplier // sei() // TODO these should be registered? // sh3dNodeRadio.wake(); // sh3dNodeIO.wake(); // sh3dNodeStorage.wake(); // Flsuh devices again console.flush(); console.printf(F("# SLEEP END %lu sec, %lu c\r\n"), seconds, sleepCount); sleepLastSeconds = sleepCount * 8; sleepLastReason = 1; return 1; } void WII5Sh3dUtil::reset() { wdt_enable(WDTO_15MS); // TODO 2024 - This may need - WDTCSR |= (1 << WDE); // Watchdog System Reset Enable // TODO or maybe not while(1); /* #ifdef WATCHDOG wdt_enable(WDTO_15MS); #else // Teensy 3 #ifndef SCB_AIRCR_SYSRESETREQ_MASK #define SCB_AIRCR_SYSRESETREQ_MASK ((unsigned int) 0x00000004) #endif cli(); delay(100); SCB_AIRCR = 0x05FA0000 | SCB_AIRCR_SYSRESETREQ_MASK; while(1); #endif delay(1000); */ } WII5Sh3dUtil sh3dNodeUtil;