Numworks Epsilon  1.4.1
Graphing Calculator Operating System
power.cpp
Go to the documentation of this file.
1 #include <ion.h>
2 #include "battery.h"
3 #include "device.h"
4 #include "display.h"
5 #include "keyboard.h"
6 #include "led.h"
7 #include "usb.h"
8 #include "wakeup.h"
9 #include "regs/regs.h"
10 
11 void Ion::Power::suspend(bool checkIfPowerKeyReleased) {
12  if (checkIfPowerKeyReleased) {
13  /* Wait until power is released to avoid restarting just after suspending */
14  bool isPowerDown = true;
15  while (isPowerDown) {
17  isPowerDown = scan.keyDown(Keyboard::Key::B2);
18  }
19  }
21 
22  PWR.CR()->setLPDS(true); // Turn the regulator off. Takes longer to wake up.
23  PWR.CR()->setFPDS(true); // Put the flash to sleep. Takes longer to wake up.
24  CM4.SCR()->setSLEEPDEEP(true);
25 
27 #if LED_WHILE_CHARGING
29 #endif
30  while (1) {
31 #if LED_WHILE_CHARGING
32  /* Update LEDS
33  * if the standby mode was stopped due to a "stop charging" event, we wait
34  * a while to be sure that the plug state of the USB is up-to-date. */
35  msleep(200);
37 #endif
38 
40 
42 
43  /* To enter sleep, we need to issue a WFE instruction, which waits for the
44  * event flag to be set and then clears it. However, the event flag might
45  * already be on. So the safest way to make sure we actually wait for a new
46  * event is to force the event flag to on (SEV instruction), use a first WFE
47  * to clear it, and then a second WFE to wait for a _new_ event. */
48  asm("sev");
49  asm("wfe");
50  msleep(1);
51  asm("wfe");
52 
54 
58 
59  Ion::Keyboard::State OnlyPowerKeyDown = Keyboard::State(Keyboard::Key::B2);
60  if (scan == OnlyPowerKeyDown || USB::isPlugged()) {
61  // Wake up
62  break;
63  }
64  }
66 
68 }
bool isPlugged()
Definition: usb.cpp:12
void shutdownClocks()
Definition: device.cpp:276
void onUSBPlugging()
Definition: wakeup.cpp:27
Definition: pwr.h:6
bool isCharging()
Definition: battery.cpp:16
void enforceState(bool red, bool green, bool blue)
Definition: led.cpp:89
void onChargingEvent()
Definition: wakeup.cpp:11
void msleep(long ms)
Definition: ion.cpp:4
Definition: cm4.h:6
void initClocks()
Definition: device.cpp:186
bool keyDown(Key k) const
Definition: keyboard.h:47
void onPowerKeyDown()
Definition: wakeup.cpp:41
void shutdownPeripherals()
Definition: device.cpp:172
State scan()
Definition: keyboard.cpp:50
void initPeripherals()
Definition: device.cpp:158
Definition: pwr.h:8
Definition: cm4.h:38
void suspend(bool checkIfPowerKeyReleased=false)
Definition: power.cpp:11