|
1 | 1 | /*
|
2 | 2 | ********************************************************************************
|
3 |
| -* WakeFromRTC.ino |
4 | 3 | *
|
5 | 4 | * This example demonstrates how to use the RTC to wake up the
|
6 | 5 | * Portenta C33 from deep sleep. The device will go to sleep for 1 second and
|
7 | 6 | * then wake up. The built-in LED will blink every second.
|
8 | 7 | *
|
9 | 8 | * The example also demonstrates how to use the PF1550 PMIC to turn off the peripherals
|
10 | 9 | * 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. |
| 10 | +* uncomment #define TURN_PERIPHERALS_OFF on line 23 to enable this feature. |
12 | 11 | *
|
13 | 12 | * INSTRUCTIONS:
|
14 | 13 | * - Make sure you are running the latest version of the Renesas Core
|
@@ -50,70 +49,54 @@ RTCTime initialTime(1, Month::JANUARY, 2000, 12, 10, 00, DayOfWeek::TUESDAY, Sav
|
50 | 49 | }
|
51 | 50 | #endif
|
52 | 51 |
|
53 |
| -bool sleepFor(int hours, int minutes, int seconds) |
54 |
| -{ |
55 |
| - if(RTC.isRunning()){ |
56 |
| - // Get the current time from the RTC |
57 |
| - RTCTime currentTime; |
58 |
| - if (!RTC.getTime(currentTime)) { |
59 |
| - return false; // Failed to get current time |
60 |
| - } |
61 |
| - |
62 |
| - // Convert current time to UNIX timestamp and add the desired interval |
63 |
| - time_t currentTimestamp = currentTime.getUnixTime(); |
64 |
| - currentTimestamp += hours * 3600 + minutes * 60 + seconds; |
65 |
| - |
66 |
| - // Convert back to RTCTime |
67 |
| - RTCTime alarmTime(currentTimestamp); |
68 |
| - |
69 |
| - |
70 |
| - // Configure the alarm match criteria |
71 |
| - AlarmMatch match; |
72 |
| - match.addMatchSecond(); // Trigger the alarm when the seconds match |
73 |
| - match.addMatchMinute(); // Trigger the alarm when the minutes match |
74 |
| - match.addMatchHour(); // Trigger the alarm when the hours match |
75 |
| - |
76 |
| - // Set the alarm |
77 |
| - if (!RTC.setAlarm(alarmTime, match)) { |
78 |
| - return false; // Failed to set the alarm |
79 |
| - } |
| 52 | +/** |
| 53 | + * Initializes the Real-Time Clock (RTC) and sets the initial time if the RTC is not running. |
| 54 | + * This initial time is a dummy time. You can obtain the current time from an NTP server and set it here. |
| 55 | + */ |
| 56 | +void initializeRealTimeClock(){ |
| 57 | + RTC.begin(); |
80 | 58 |
|
81 |
| - // Enable the alarm |
82 |
| - return true; |
| 59 | + if (!RTC.isRunning()) { |
| 60 | + RTC.setTime(initialTime); |
83 | 61 | }
|
84 | 62 | }
|
85 | 63 |
|
86 | 64 | void setup(){
|
87 | 65 | lowPower = LowPower();
|
88 |
| - lowPower.enableWakeupFromRTC(); |
89 | 66 |
|
90 | 67 | #ifdef TURN_PERIPHERALS_OFF
|
91 | 68 | PMIC.begin();
|
92 | 69 | turnPeripheralsOn();
|
93 | 70 | #endif
|
94 | 71 |
|
95 |
| - // Initialize the RTC |
96 |
| - RTC.begin(); |
97 |
| - |
98 |
| - // Set the initial time if the RTC is not running |
99 |
| - if (!RTC.isRunning()) { |
100 |
| - RTC.setTime(initialTime); |
101 |
| - } |
| 72 | + initializeRealTimeClock(); |
102 | 73 |
|
103 |
| - pinMode(LED_BUILTIN, OUTPUT); // Set the LED pin as an output |
| 74 | + pinMode(LED_BUILTIN, OUTPUT); // Set the LED pin as an output |
| 75 | + pinMode(LEDR, OUTPUT); // Use the red LED to indicate errors |
| 76 | + digitalWrite(LEDR, HIGH); // Turn off the red LED |
104 | 77 | digitalWrite(LED_BUILTIN, LOW); // Turn on the LED
|
105 |
| - delay(1000); // lets the user see the led for 1 second |
| 78 | + delay(5000); // lets the user see the led for 5 seconds |
106 | 79 |
|
107 |
| - // The device will wake up every second and blink the LED |
| 80 | + // The device will go to sleep every 5 seconds and wake up after 5 seconds to blink the LED |
108 | 81 | // 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(); |
| 82 | + if(!lowPower.setWakeUpAlarm(0, 0, 5)){ |
| 83 | + // Blink the red LED indefinitely to indicate an error |
| 84 | + while(true){ |
| 85 | + digitalWrite(LEDR, HIGH); |
| 86 | + delay(500); |
| 87 | + digitalWrite(LEDR, LOW); |
| 88 | + delay(500); |
| 89 | + } |
115 | 90 | }
|
116 |
| -} |
117 | 91 |
|
118 |
| -void loop(){ |
| 92 | + #ifdef TURN_PERIPHERALS_OFF |
| 93 | + // turn peripherals off before going to sleep |
| 94 | + turnPeripheralsOff(); |
| 95 | + #else |
| 96 | + // Turn off the LED to indicate the device is going to sleep |
| 97 | + digitalWrite(LED_BUILTIN, HIGH); |
| 98 | + #endif |
| 99 | + lowPower.deepSleep(); |
119 | 100 | }
|
| 101 | + |
| 102 | +void loop(){} |
0 commit comments