Skip to content

Commit 562190f

Browse files
committed
Add APIs for boards with companion chip (eg. Tian)
1 parent 5175ae9 commit 562190f

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed

examples/TianStandby/TianStandby.ino

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
TianStandby
3+
4+
This sketch demonstrates the usage of SAMD chip to furtherly reduce the power usage of Tian
5+
board. This method can be applied to any board with companion chips which expose a method
6+
(via direct pin interrupt or via a command) to enter and exit standby.
7+
Sleep modes allow a significant drop in the power usage of a board while it does nothing waiting for an event to happen. Battery powered application can take advantage of these modes to enhance battery life significantly.
8+
9+
In this sketch, the internal RTC of SAMD chip will wake up the processor every 20 seconds.
10+
Before going to sleep, the SAMD chip tells the MIPS CPU to standby too.
11+
Please note that, if the processor is sleeping, a new sketch can't be uploaded. To overcome this, manually reset the board (usually with a single or double tap to the RESET button)
12+
13+
This example code is in the public domain.
14+
*/
15+
16+
#include "ArduinoLowPower.h"
17+
18+
#define MIPS_PIN 32
19+
20+
void MIPS_PM(bool sleep) {
21+
pinMode(MIPS_PIN, OUTPUT);
22+
digitalWrite(MIPS_PIN, sleep ? LOW: HIGH);
23+
}
24+
25+
void setup() {
26+
pinMode(LED_BUILTIN, OUTPUT);
27+
LowPower.companionLowPowerCallback(MIPS_PM);
28+
// Uncomment this function if you wish to attach function dummy when RTC wakes up the chip
29+
LowPower.attachInterruptWakeup(RTC_ALARM_WAKEUP, onWakeup, CHANGE);
30+
}
31+
32+
void loop() {
33+
digitalWrite(LED_BUILTIN, HIGH);
34+
delay(500);
35+
digitalWrite(LED_BUILTIN, LOW);
36+
delay(500);
37+
// Triggers a 2000 ms sleep (the device will be woken up only by the registered wakeup sources and by internal RTC)
38+
// The power consumption of the chip will drop consistently
39+
// Send sleep command to MIPS CPU and then go to sleep
40+
LowPower.companionSleep();
41+
LowPower.sleep(20000);
42+
}
43+
44+
void onWakeup() {
45+
// This function will be called once on device wakeup
46+
// You can do some little operations here (like changing variables which will be used in the loop)
47+
// Remember to avoid calling delay() and long running functions since this functions executes in interrupt context
48+
49+
// Wakeup the companion chip, too
50+
LowPower.companionWakeup();
51+
}

src/ArduinoLowPower.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,13 @@
1515
#include "RTCZero.h"
1616
#endif
1717

18+
#if defined(ARDUINO_SAMD_TIAN)
19+
// add here any board with companion chip which can be woken up
20+
#define BOARD_HAS_COMPANION_CHIP
21+
#endif
22+
1823
//typedef void (*voidFuncPtr)( void ) ;
24+
typedef void (*onOffFuncPtr)( bool ) ;
1925

2026
class ArduinoLowPowerClass {
2127
public:
@@ -39,6 +45,18 @@ class ArduinoLowPowerClass {
3945

4046
void attachInterruptWakeup(uint32_t pin, voidFuncPtr callback, uint32_t mode);
4147

48+
#ifdef BOARD_HAS_COMPANION_CHIP
49+
void companionLowPowerCallback(onOffFuncPtr callback) {
50+
companionSleepCB = callback;
51+
}
52+
void companionSleep() {
53+
companionSleepCB(true);
54+
}
55+
void companionWakeup() {
56+
companionSleepCB(false);
57+
}
58+
#endif
59+
4260
#ifdef __ARDUINO_ARC__
4361
void wakeFromSleepCallback(void);
4462
void wakeFromDoze(void);
@@ -74,6 +92,7 @@ class ArduinoLowPowerClass {
7492
#define RTC_ALARM_WAKEUP 0xFF
7593
#define RESET_BUTTON_WAKEUP 0xFE
7694
#endif
95+
void (*companionSleepCB)(bool);
7796
};
7897

7998
extern ArduinoLowPowerClass LowPower;

0 commit comments

Comments
 (0)