Skip to content

Commit 76b40a2

Browse files
authored
Add Retention in power mode (Ameba-AIoT#293)
* Add Retention in power mode *Update DeepsleepMode example *Update StandbyMode example *Update Power Mode API +Add DeepSleepMode with retention example +Add StandbyMode with retention * Fix coding style Fix coding style for DeepSleepMode.ino and StandbyMode.ino
1 parent 3906180 commit 76b40a2

File tree

6 files changed

+507
-179
lines changed

6 files changed

+507
-179
lines changed

Arduino_package/hardware/libraries/PowerMode/examples/DeepSleepMode/DeepSleepMode.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This sketch shows how to use power mode deepsleep
33
44
Example guide:
5-
https://www.amebaiot.com/en/amebapro2-arduino-deepsleep/
5+
https://ameba-arduino-doc.readthedocs.io/en/latest/amebapro2/Example_Guides/PowerMode/Deep%20Sleep%20Mode.html
66
*/
77

88
#include "PowerMode.h"
@@ -11,6 +11,7 @@
1111
// wake up by AON GPIO : 1
1212
// wake up by RTC : 2
1313
#define WAKEUP_SOURCE 0
14+
#define RETENTION 0
1415

1516
#if (WAKEUP_SOURCE == 0)
1617
// set AON timer Clock, 1: 4MHz, 0: 100kHz
@@ -38,7 +39,7 @@ void setup()
3839
{
3940
Serial.begin(115200);
4041
Serial.println("DeepSleep Mode Demo!");
41-
PowerMode.begin(DEEPSLEEP_MODE, WAKEUP_SOURCE, WAKUPE_SETTING);
42+
PowerMode.begin(DEEPSLEEP_MODE, WAKEUP_SOURCE, RETENTION, WAKUPE_SETTING);
4243

4344
for (int i = 5; i > 0; i--) {
4445
Serial.print("Enter DeepSleep Mode by ");
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
This sketch shows how to use power mode deepsleep with retention
3+
4+
Example guide:
5+
tbd
6+
*/
7+
8+
#include "PowerMode.h"
9+
10+
// wake up retention by AON timer : 0
11+
// wake up retention by AON GPIO : 1
12+
#define WAKEUP_SOURCE 0
13+
#define RETENTION 1
14+
15+
#if (WAKEUP_SOURCE == 0)
16+
// set AON timer Clock, 1: 4MHz, 0: 100kHz
17+
#define CLOCK 0
18+
// set AON timer sleep duration, 5s
19+
#define SLEEP_DURATION 5
20+
uint32_t PM_AONtimer_setting[2] = {CLOCK, SLEEP_DURATION};
21+
#define WAKUPE_SETTING (uint32_t)(PM_AONtimer_setting)
22+
#elif (WAKEUP_SOURCE == 1)
23+
// set wake up AON GPIO pin : 21 / 22
24+
#define WAKUPE_SETTING 21
25+
#else
26+
#define WAKUPE_SETTING 0
27+
#endif
28+
// Retained variable in SRAM
29+
__attribute__((section(".retention.data"))) char retention_string[64] __attribute__((aligned(32)));
30+
__attribute__((section(".retention.data"))) uint32_t sum __attribute__((aligned(32)));
31+
void setup()
32+
{
33+
Serial.begin(115200);
34+
Serial.println(" Arduino Retention DeepSleep Mode Demo!");
35+
36+
// Check if the retention string indicates a previous retention state
37+
if (strcmp(retention_string, "Retention_string: Ameba Pro2") == 0) {
38+
// Print the retention string after waking up
39+
Serial.println("Waking up after standby:");
40+
Serial.println("System running normally after deepsleep and read back retention...");
41+
Serial.println(retention_string);
42+
Serial.print("Total = ");
43+
Serial.println(sum);
44+
delay(5000);
45+
46+
} else if (strcmp(retention_string, "Ameba Pro2") != 0) {
47+
// Initialize retention string
48+
strncpy(retention_string, "Retention_string: Ameba Pro2", sizeof(retention_string) - 1);
49+
PowerMode.cleanInvalidateCache(retention_string, sizeof(retention_string));
50+
51+
// Initialize retention sum
52+
sum = 1;
53+
PowerMode.cleanInvalidateCache((uint32_t *)&sum, sizeof(sum));
54+
55+
PowerMode.begin(DEEPSLEEP_MODE, WAKEUP_SOURCE, RETENTION, WAKUPE_SETTING);
56+
57+
for (int i = 5; i > 0; i--) {
58+
Serial.print("Enter DeepSleep Mode by ");
59+
Serial.print(i, DEC);
60+
Serial.println(" seconds.");
61+
delay(1000);
62+
}
63+
PowerMode.start();
64+
}
65+
}
66+
67+
void loop()
68+
{
69+
// do nothing
70+
delay(5000);
71+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
This sketch shows how to use power mode standby with retention
3+
4+
Example guide:
5+
tbd
6+
*/
7+
8+
#include "PowerMode.h"
9+
10+
// wake up by AON timer : 0
11+
// wake up by AON GPIO : 1
12+
// wake up by PON GPIO : 2
13+
// wake up by Gtimer0 : 3 //Only support 4MHz clock source
14+
#define WAKEUP_SOURCE 0
15+
#define RETENTION 1
16+
17+
#if (WAKEUP_SOURCE == 0)
18+
// set AON timer Clock, 1: 4MHz, 0: 100kHz
19+
#define CLOCK 0
20+
// set AON timer sleep duration, 5s
21+
#define SLEEP_DURATION 5
22+
uint32_t PM_AONtimer_setting[2] = {CLOCK, SLEEP_DURATION};
23+
#define WAKUPE_SETTING (uint32_t)(PM_AONtimer_setting)
24+
#elif (WAKEUP_SOURCE == 1)
25+
// set wake up AON GPIO pin : 21 / 22
26+
#define WAKUPE_SETTING 21
27+
#elif (WAKEUP_SOURCE == 2)
28+
// set wake up PON GPIO pin : 0 to 11
29+
#define WAKUPE_SETTING 0
30+
#elif (WAKEUP_SOURCE == 3)
31+
// set wake up Gtimer0 sleep duration, 5s
32+
#define SLEEP_DURATION 5
33+
#define WAKUPE_SETTING SLEEP_DURATION
34+
#else
35+
#define WAKUPE_SETTING 0
36+
#endif
37+
38+
// Retained variable in SRAM
39+
__attribute__((section(".retention.data"))) char retention_string[64] __attribute__((aligned(32)));
40+
__attribute__((section(".retention.data"))) uint32_t sum __attribute__((aligned(32)));
41+
void setup()
42+
{
43+
Serial.begin(115200);
44+
Serial.println(" Arduino Retention Standby Mode Demo!");
45+
46+
// Check if the retention string indicates a previous retention state
47+
if (strcmp(retention_string, "Retention_string: Ameba Pro2") == 0) {
48+
// Print the retention string after waking up
49+
Serial.println("Waking up after standby:");
50+
Serial.println("System running normally after standby and read back retention...");
51+
Serial.println(retention_string);
52+
Serial.print("Total = ");
53+
Serial.println(sum);
54+
delay(5000);
55+
} else if (strcmp(retention_string, "Ameba Pro2") != 0) {
56+
// Initialize retention string
57+
strncpy(retention_string, "Retention_string: Ameba Pro2", sizeof(retention_string) - 1);
58+
PowerMode.cleanInvalidateCache(retention_string, sizeof(retention_string));
59+
60+
// Initialize retention_sum
61+
sum = 1;
62+
PowerMode.cleanInvalidateCache((uint32_t *)&sum, sizeof(sum));
63+
64+
PowerMode.begin(STANDBY_MODE, WAKEUP_SOURCE, RETENTION, WAKUPE_SETTING);
65+
66+
for (int i = 5; i > 0; i--) {
67+
Serial.print("Enter Standby Mode by ");
68+
Serial.print(i, DEC);
69+
Serial.println(" seconds.");
70+
delay(1000);
71+
}
72+
PowerMode.start();
73+
}
74+
}
75+
76+
void loop()
77+
{
78+
// do nothing
79+
delay(5000);
80+
}

Arduino_package/hardware/libraries/PowerMode/examples/StandbyMode/StandbyMode.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
This sketch shows how to use power mode standby
33
44
Example guide:
5-
https://www.amebaiot.com/en/amebapro2-arduino-standby/
5+
https://ameba-arduino-doc.readthedocs.io/en/latest/amebapro2/Example_Guides/PowerMode/Standby%20Mode.html
66
*/
77

88
#include "PowerMode.h"
@@ -14,6 +14,7 @@
1414
// wake up by UART/Serial1 : 4 //Only support 4MHz clock source
1515
// wake up by Gtimer0 : 5 //Only support 4MHz clock source
1616
#define WAKEUP_SOURCE 0
17+
#define RETENTION 0
1718

1819
#if (WAKEUP_SOURCE == 0)
1920
// set AON timer Clock, 1: 4MHz, 0: 100kHz
@@ -48,7 +49,7 @@ void setup()
4849
{
4950
Serial.begin(115200);
5051
Serial.println("Standby Mode Demo!");
51-
PowerMode.begin(STANDBY_MODE, WAKEUP_SOURCE, WAKUPE_SETTING);
52+
PowerMode.begin(STANDBY_MODE, WAKEUP_SOURCE, RETENTION, WAKUPE_SETTING);
5253

5354
for (int i = 5; i > 0; i--) {
5455
Serial.print("Enter Standby Mode by ");

0 commit comments

Comments
 (0)