|
| 1 | +/* |
| 2 | +******************************************************************************** |
| 3 | +* WakeFromRTC.ino |
| 4 | +* |
| 5 | +* This example demonstrates how to use the RTC to wake up the |
| 6 | +* Portenta C33 from deep sleep. The device will go to sleep for 1 second and |
| 7 | +* then wake up. The built-in LED will blink every second. |
| 8 | +* |
| 9 | +* The example also demonstrates how to use the PF1550 PMIC to turn off the peripherals |
| 10 | +* before going to sleep and turn them back on after waking up. |
| 11 | +* uncomment #define TURN_PERIPHERALS_OFF on line 24 to enable this feature. |
| 12 | +* |
| 13 | +* INSTRUCTIONS: |
| 14 | +* - Make sure you are running the latest version of the Renesas Core |
| 15 | +* - Select the Portenta C33 board from the Tools menu |
| 16 | +* - Select the Portenta C33 USB port from the Tools menu |
| 17 | +* - Upload the code to your Portenta C33 |
| 18 | +* |
| 19 | +* Original author: C. Dragomir (http://arduino.cc) |
1 | 20 |
|
| 21 | +*/ |
| 22 | + |
| 23 | +// #define TURN_PERIPHERALS_OFF |
2 | 24 |
|
3 | 25 | #include "RTC.h"
|
4 | 26 | #include "Arduino_LowPowerPortentaC33.h"
|
5 | 27 |
|
| 28 | +#ifdef TURN_PERIPHERALS_OFF |
| 29 | + #include "Arduino_PMIC.h" |
| 30 | +#endif |
6 | 31 |
|
7 | 32 | LowPower lowPower;
|
8 | 33 | RTCTime initialTime(1, Month::JANUARY, 2000, 12, 10, 00, DayOfWeek::TUESDAY, SaveLight::SAVING_TIME_ACTIVE);
|
9 | 34 |
|
10 |
| -void alarmCallback(){ |
11 |
| - digitalWrite(LED_BUILTIN, HIGH); |
12 |
| - delay(1000); |
13 |
| - digitalWrite(LED_BUILTIN, LOW); |
14 |
| - lowPower.deepSleep(); |
15 |
| -} |
| 35 | +#ifdef TURN_PERIPHERALS_OFF |
| 36 | + void turnPeripheralsOff(){ |
| 37 | + PMIC.getControl() -> turnLDO1Off(Ldo1Mode::Normal); // LDO1 is used for AVCC (Analog Power) |
| 38 | + PMIC.getControl() -> turnLDO2Off(Ldo2Mode::Normal); // LDO2 is used for the analog voltage reference |
| 39 | + PMIC.getControl() -> turnLDO3Off(Ldo3Mode::Normal); // LDO3 is a 1.2V regulator used by the Ethernet chip |
| 40 | + PMIC.getControl() -> turnSw1Off(Sw1Mode::Normal); // SW1 is a 3.3V regulator used by RGB Led, WIFI Chip, Secure element |
| 41 | + PMIC.getControl() -> turnSw2Off(Sw2Mode::Normal); // SW2 is the power regulator used for powering external circuits (Labeled 3V3 on the board) |
| 42 | + } |
16 | 43 |
|
| 44 | + void turnPeripheralsOn(){ |
| 45 | + PMIC.getControl() -> turnLDO1On(Ldo1Mode::Normal); // LDO1 is used for AVCC (Analog Power) |
| 46 | + PMIC.getControl() -> turnLDO2On(Ldo2Mode::Normal); // LDO2 is used for the analog voltage reference |
| 47 | + PMIC.getControl() -> turnLDO3On(Ldo3Mode::Normal); // LDO3 is a 1.2V regulator used by the Ethernet chip |
| 48 | + PMIC.getControl() -> turnSw1On(Sw1Mode::Normal); // SW1 is a 3.3V regulator used by RGB Led, WIFI Chip, Secure element |
| 49 | + PMIC.getControl() -> turnSw2On(Sw2Mode::Normal); // SW2 is the power regulator used for powering external circuits (Labeled 3V3 on the board) |
| 50 | + } |
| 51 | +#endif |
17 | 52 |
|
18 |
| -bool sleepFor(int hours, int minutes, int seconds){ |
| 53 | +bool sleepFor(int hours, int minutes, int seconds) |
19 | 54 | {
|
20 |
| - |
| 55 | + if(RTC.isRunning()){ |
| 56 | + // Get the current time from the RTC |
21 | 57 | RTCTime currentTime;
|
22 | 58 | if (!RTC.getTime(currentTime)) {
|
23 |
| - Serial.println("Failed to get current time"); |
24 | 59 | return false; // Failed to get current time
|
25 | 60 | }
|
26 |
| - Serial.println(currentTime.toString()); |
27 | 61 |
|
28 | 62 | // Convert current time to UNIX timestamp and add the desired interval
|
29 | 63 | time_t currentTimestamp = currentTime.getUnixTime();
|
30 | 64 | currentTimestamp += hours * 3600 + minutes * 60 + seconds;
|
| 65 | + |
31 | 66 | // Convert back to RTCTime
|
32 | 67 | RTCTime alarmTime(currentTimestamp);
|
| 68 | + |
| 69 | + |
33 | 70 | // Configure the alarm match criteria
|
34 | 71 | AlarmMatch match;
|
35 | 72 | match.addMatchSecond(); // Trigger the alarm when the seconds match
|
36 | 73 | match.addMatchMinute(); // Trigger the alarm when the minutes match
|
37 | 74 | match.addMatchHour(); // Trigger the alarm when the hours match
|
38 |
| - // Set the alarm callback (assuming you have a callback function named alarmCallback) |
39 |
| - if (!RTC.setAlarmCallback(alarmCallback, alarmTime, match)) { |
40 |
| - Serial.println("Failed to set the alarm"); |
| 75 | + |
| 76 | + // Set the alarm |
| 77 | + if (!RTC.setAlarm(alarmTime, match)) { |
41 | 78 | return false; // Failed to set the alarm
|
42 | 79 | }
|
43 | 80 |
|
44 | 81 | // Enable the alarm
|
45 |
| - Serial.println("Enabling the alarm"); |
46 | 82 | return true;
|
47 | 83 | }
|
48 | 84 | }
|
49 | 85 |
|
50 | 86 | void setup(){
|
51 |
| - Serial.begin(9600); |
52 |
| - |
53 | 87 | lowPower = LowPower();
|
54 | 88 | lowPower.enableWakeupFromRTC();
|
55 | 89 |
|
56 |
| - pinMode(LED_BUILTIN, OUTPUT); |
57 |
| - digitalWrite(LED_BUILTIN, LOW); |
| 90 | + #ifdef TURN_PERIPHERALS_OFF |
| 91 | + PMIC.begin(); |
| 92 | + turnPeripheralsOn(); |
| 93 | + #endif |
58 | 94 |
|
| 95 | + // Initialize the RTC |
59 | 96 | RTC.begin();
|
60 | 97 |
|
| 98 | + // Set the initial time if the RTC is not running |
61 | 99 | if (!RTC.isRunning()) {
|
62 | 100 | RTC.setTime(initialTime);
|
63 |
| - sleepFor(0, 0, 1); |
64 | 101 | }
|
| 102 | + |
| 103 | + pinMode(LED_BUILTIN, OUTPUT); // Set the LED pin as an output |
| 104 | + digitalWrite(LED_BUILTIN, LOW); // Turn on the LED |
| 105 | + delay(1000); // lets the user see the led for 1 second |
65 | 106 |
|
66 |
| - lowPower.deepSleep(); |
| 107 | + // The device will wake up every second and blink the LED |
| 108 | + // effectivelly creating the same efect as the blink sketch |
| 109 | + if(sleepFor(0, 0, 1)){ |
| 110 | + // turn peripherals off before going to sleep |
| 111 | + #ifdef TURN_PERIPHERALS_OFF |
| 112 | + turnPeripheralsOff(); |
| 113 | + #endif |
| 114 | + lowPower.deepSleep(); |
| 115 | + } |
67 | 116 | }
|
68 | 117 |
|
69 | 118 | void loop(){
|
70 |
| - |
71 | 119 | }
|
0 commit comments