Skip to content

Commit 63b86a0

Browse files
committed
Refactorings
1 parent 38b6cf4 commit 63b86a0

File tree

5 files changed

+36
-37
lines changed

5 files changed

+36
-37
lines changed

examples/DeepSleep_WakeFromPin/DeepSleep_WakeFromPin.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ void loop() {
4545
sleepFlag = false;
4646

4747
//board.setAllPeripheralsPower(false);
48-
board.deepSleepUntilWakeupEvent();
48+
board.standByUntilWakeupEvent();
4949
} else {
5050

5151
digitalWrite(LEDB, HIGH);

examples/DeepSleep_WakeFromRTC_C33/DeepSleep_WakeFromRTC_C33.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ void setup() {
3737

3838
void loop(){
3939
//board.setAllPeripheralsPower(false);
40-
board.deepSleepUntilWakeupEvent();
40+
board.standByUntilWakeupEvent();
4141
}
4242

examples/DeepSleep_WakeFromRTC_H7/DeepSleep_WakeFromRTC_H7.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void setup() {
1616
pinMode(LEDB, OUTPUT);
1717
digitalWrite(LEDB, LOW);
1818
board.setAllPeripheralsPower(false);
19-
board.deepSleepUntilWakeupEvent();
19+
board.standByUntilWakeupEvent();
2020
}
2121

2222
void loop() {

src/Board.cpp

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#include "Board.h"
22
#include <map>
33

4+
constexpr int UNKNOWN_VALUE = 0xFF;
5+
46
std::map<float, Ldo2Voltage> ldo2VoltageMap = {
57
{1.80f, Ldo2Voltage::V_1_80},
68
{1.90f, Ldo2Voltage::V_1_90},
@@ -43,20 +45,20 @@ std::map<float, Sw2Voltage> sw2VoltageMap = {
4345
};
4446

4547
Board::Board() {
46-
#if defined(ARDUINO_PORTENTA_C33)
47-
this -> lowPower = new LowPower();
48-
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION)
49-
if (CM7_CPUID == HAL_GetCurrentCPUID())
50-
{
48+
#if defined(ARDUINO_PORTENTA_C33)
49+
this->lowPower = new LowPower();
50+
#endif
51+
}
52+
53+
bool Board::begin() {
54+
#if defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_NICLA_VISION)
55+
if (CM7_CPUID == HAL_GetCurrentCPUID()){
5156
if (LowPowerReturnCode::success != LowPower.checkOptionBytes()){
5257
LowPower.prepareOptionBytes();
5358
}
5459
bootM4();
5560
}
5661
#endif
57-
}
58-
59-
bool Board::begin() {
6062
return PMIC.begin() == 0;
6163
}
6264

@@ -80,19 +82,19 @@ void Board::setExternalPowerEnabled(bool on) {
8082

8183
bool Board::setExternalVoltage(float voltage) {
8284
this -> setExternalPowerEnabled(false);
83-
uint8_t voltageRegisterValue = getRailVoltage(voltage, 4);
85+
uint8_t targetVoltage = getRailVoltageEnum(voltage, CONTEXT_SW2);
8486

85-
if (voltageRegisterValue == EMPTY_REGISTER){
87+
if (targetVoltage == UNKNOWN_VALUE){
8688
return false;
8789
}
8890

89-
PMIC.writePMICreg(Register::PMIC_SW2_VOLT, voltageRegisterValue);
90-
if(PMIC.readPMICreg(Register::PMIC_SW2_VOLT) == voltageRegisterValue){
91+
PMIC.writePMICreg(Register::PMIC_SW2_VOLT, targetVoltage);
92+
if(PMIC.readPMICreg(Register::PMIC_SW2_VOLT) == targetVoltage){
9193
this -> setExternalPowerEnabled(true);
9294
return true;
93-
} else {
94-
return false;
9595
}
96+
97+
return false;
9698
}
9799

98100
void Board::setCameraPowerEnabled(bool on) {
@@ -106,8 +108,6 @@ void Board::setCameraPowerEnabled(bool on) {
106108
PMIC.getControl()->turnLDO2Off(Ldo2Mode::Normal);
107109
PMIC.getControl()->turnLDO3Off(Ldo3Mode::Normal);
108110
}
109-
#else
110-
#warning "This feature is currently only supported on the Nicla Vision Board"
111111
#endif
112112
}
113113

@@ -192,7 +192,7 @@ void Board::sleepUntilWakeupEvent(){
192192
}
193193
#endif
194194

195-
void Board::deepSleepUntilWakeupEvent(){
195+
void Board::standByUntilWakeupEvent(){
196196
#if defined(ARDUINO_PORTENTA_C33)
197197
lowPower -> deepSleep();
198198
#elif defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) || defined(ARDUINO_NICLA_VISION)
@@ -263,6 +263,7 @@ void Board::setAnalogDigitalConverterPower(bool on){
263263
#endif
264264

265265
void Board::setCommunicationPeripheralsPower(bool on){
266+
// TODO: Why do we only use the normal mode here?
266267
if(on)
267268
PMIC.getControl()->turnSw1On(Sw1Mode::Normal);
268269
else
@@ -271,19 +272,19 @@ void Board::setCommunicationPeripheralsPower(bool on){
271272

272273

273274
bool Board::setReferenceVoltage(float voltage) {
274-
uint8_t voltageRegisterValue = getRailVoltage(voltage, CONTEXT_LDO2);
275+
uint8_t voltageRegisterValue = getRailVoltageEnum(voltage, CONTEXT_LDO2);
275276

276277
// If voltageRegisterValue is not empty, write it to the PMIC register
277278
// and return the result of the comparison directly.
278-
if (voltageRegisterValue != EMPTY_REGISTER) {
279+
if (voltageRegisterValue != UNKNOWN_VALUE) {
279280
PMIC.writePMICreg(Register::PMIC_LDO2_VOLT, voltageRegisterValue);
280281
return (PMIC.readPMICreg(Register::PMIC_LDO2_VOLT) == voltageRegisterValue);
281282
}
282283

283284
return false;
284285
}
285286

286-
uint8_t Board::getRailVoltage(float voltage, int context) {
287+
uint8_t Board::getRailVoltageEnum(float voltage, int context) {
287288
switch (context) {
288289
case CONTEXT_LDO2:
289290
if (ldo2VoltageMap.find(voltage) != ldo2VoltageMap.end()) {
@@ -304,10 +305,9 @@ bool Board::setReferenceVoltage(float voltage) {
304305
break;
305306

306307
default:
307-
return EMPTY_REGISTER;
308-
break;
308+
return UNKNOWN_VALUE;
309309
}
310310

311311

312-
return EMPTY_REGISTER;
312+
return UNKNOWN_VALUE;
313313
}

src/Board.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313
#include "Arduino_LowPowerPortentaH7.h"
1414
#endif
1515

16-
#define CONTEXT_LDO2 2
17-
#define CONTEXT_SW1 3
18-
#define CONTEXT_SW2 4
16+
#define CONTEXT_LDO2 2 // LDO regulator: 1.8 V to 3.3 V, 400 mA
17+
#define CONTEXT_SW1 3 // Buck converter: 1.0 A; 0.6 V to 1.3875 V
18+
#define CONTEXT_SW2 4 // Buck converter: 1.0 A; 0.6 V to 1.3875 V
1919

2020
enum class StandbyType : uint8_t {
2121
none = 0,
2222
untilPinActivity = 1,
2323
untilTimeElapsed = 2,
2424
untilEither = 3
25-
2625
};
2726

2827
inline constexpr StandbyType operator|(StandbyType x, StandbyType y){
@@ -33,8 +32,6 @@ inline constexpr StandbyType operator|=(StandbyType& x, StandbyType y){
3332
return x = x | y;
3433
}
3534

36-
constexpr int EMPTY_REGISTER = 0xFF;
37-
3835
/**
3936
* @brief Represents a board with power management capabilities.
4037
*
@@ -185,7 +182,7 @@ class Board {
185182
* A wakeup event can be an interrupt on a pin or the RTC,
186183
* depending on what you set with enableWakeupFromPin() and enableWakeupFromRTC().
187184
*/
188-
void sleepUntilWakeupEvent();
185+
void standByUntilWakeupEvent();
189186
#endif
190187

191188
// TODO Same as above
@@ -197,15 +194,14 @@ class Board {
197194
* A wakeup event can be an interrupt on a pin or the RTC, depending on what
198195
* you set with enableWakeupFromPin() and enableWakeupFromRTC().
199196
*/
200-
void deepSleepUntilWakeupEvent();
197+
void standByUntilWakeupEvent();
201198

202199
/**
203200
* @brief Toggle the peripherals' power on Portenta C33 (ADC, RGB LED, Secure Element, Wifi and Bluetooth).
204201
* @param on True to turn on the power, false to turn it off.
205202
*/
206203
void setAllPeripheralsPower(bool on);
207204

208-
209205
/**
210206
* @brief Toggles the communication peripherials' power on Portenta C33 (Wifi, Bluetooth and Secure Element)
211207
* @param on True to turn on the power, false to turn it off.
@@ -221,7 +217,7 @@ class Board {
221217

222218
#endif
223219
/**
224-
* @brief Set the reference voltage on Portenta C33. This value is used by the ADC to convert analog values to digital values.
220+
* @brief Set the reference voltage. This value is used by the ADC to convert analog values to digital values.
225221
* This can be particularly useful to increase the accuracy of the ADC when working with low voltages
226222
* @param voltage Reference voltage value in volts. It can be anything between 1.80V and 3.30V in steps of 0.10V.
227223
* Any value outside this range or with different steps will not be accepted by the library.
@@ -232,7 +228,10 @@ class Board {
232228
// TODO add function to shut down the fuel gauge / and hibernate mode
233229

234230
private:
235-
static uint8_t getRailVoltage(float voltage, int context);
231+
/**
232+
* Convert a numeric voltage value to the corresponding enum value for the PMIC library.
233+
*/
234+
static uint8_t getRailVoltageEnum(float voltage, int context);
236235

237236
#if defined(ARDUINO_PORTENTA_C33)
238237
LowPower * lowPower;

0 commit comments

Comments
 (0)