Skip to content

Commit 1f4c1ca

Browse files
authored
Merge pull request #31 from arduino-libraries/idempotence
Add begin() guard
2 parents 7e80f6e + e95199c commit 1f4c1ca

8 files changed

+43
-7
lines changed

library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ sentence=Arduino library for the PF1550 Power Management IC
66
paragraph=This library allows the control and configuration of the PF1550 used on various Arduino boards.
77
category=Device Control
88
url=https://github.com/arduino-libraries/Arduino_PF1550
9-
architectures=mbed,mbed_nicla,mbed_portenta,renesas
9+
architectures=mbed,mbed_nicla,mbed_portenta,renesas,renesas_portenta

src/PF1550.cpp

+13-3
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@
3333
******************************************************************************/
3434

3535
PF1550::PF1550(PF1550_IO & io)
36-
: _control(io),
37-
_debug(NULL)
36+
: _control(io)
37+
, _initialized(false)
38+
, _debug(NULL)
3839
{
3940

4041
}
@@ -45,10 +46,19 @@ PF1550::PF1550(PF1550_IO & io)
4546

4647
int PF1550::begin()
4748
{
49+
if(_initialized) {
50+
return 0;
51+
}
52+
4853
if (_debug) {
4954
_debug->println("PF1550 begin");
5055
}
51-
return _control.begin();
56+
int returnCode = _control.begin();
57+
58+
if (returnCode == 0) {
59+
_initialized = true;
60+
}
61+
return returnCode;
5262
}
5363

5464
void PF1550::debug(Stream& stream)

src/PF1550.h

+1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ class PF1550
100100

101101
private:
102102
PF1550_Control _control;
103+
bool _initialized;
103104
Stream* _debug;
104105
};
105106

src/PF1550/PF1550_IO.cpp

+16-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,14 @@ void PF1550_IO::readRegister(Register const reg_addr, uint8_t * data)
5252
{
5353
_wire->beginTransmission(_i2c_addr);
5454
_wire->write(static_cast<uint8_t>(reg_addr));
55-
_wire->endTransmission(false); /* No Stop. */
55+
56+
/* No Stop. */
57+
if(_wire->endTransmission(false) != 0) {
58+
if (_debug) {
59+
_debug->println("PF1550_IO::readRegister: endTransmission failed");
60+
}
61+
return;
62+
}
5663

5764
_wire->requestFrom(_i2c_addr, 1, true);
5865
while (_wire->available() < 1) { }
@@ -77,6 +84,14 @@ void PF1550_IO::setBit(Register const reg, uint8_t const bit_pos)
7784
writeRegister(reg, reg_val);
7885
}
7986

87+
uint8_t PF1550_IO::getBit(Register const reg, uint8_t const bit_pos)
88+
{
89+
assert(bit_pos < 8);
90+
uint8_t reg_val;
91+
readRegister(reg, &reg_val);
92+
return (reg_val >> bit_pos) & 1;
93+
}
94+
8095
void PF1550_IO::clrBit(Register const reg, uint8_t const bit_pos)
8196
{
8297
assert(bit_pos < 8);

src/PF1550/PF1550_IO.h

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class PF1550_IO
5656
void writeRegister(Register const reg_addr, uint8_t const data);
5757

5858
void setBit(Register const reg, uint8_t const bit_pos);
59+
uint8_t getBit(Register const reg, uint8_t const bit_pos);
5960
void clrBit(Register const reg, uint8_t const bit_pos);
6061

6162

src/PF1550/PF1550_IO_C33.h

+9
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,15 @@ class PF1550_IO_C33 : public PF1550_IO
4545
setBit(Register::CHARGER_LED_PWM, REG_LED_PWM_LED_EN_bp);
4646
/* Allow LED control by software. */
4747
setBit(Register::CHARGER_LED_CNFG, REG_LED_CNFG_LEDOVRD_bp);
48+
49+
if(getBit(Register::CHARGER_LED_PWM, REG_LED_PWM_LED_EN_bp) == 0){
50+
return -1;
51+
}
52+
53+
if(getBit(Register::CHARGER_LED_CNFG, REG_LED_CNFG_LEDOVRD_bp) == 0){
54+
return -1;
55+
}
56+
return 0;
4857
}
4958

5059
};

src/PF1550/PF1550_IO_Nicla_Vision.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class PF1550_IO_Nicla_Vision : public PF1550_IO
3737

3838

3939
protected:
40-
virtual int derived_begin() override { }
40+
virtual int derived_begin() override { return 0; }
4141
};
4242

4343
#endif /* PF1550_IO_NICLA_VISION_H_ */

src/PF1550/PF1550_IO_Portenta_H7.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class PF1550_IO_Portenta_H7 : public PF1550_IO
3737

3838

3939
protected:
40-
virtual int derived_begin() override { }
40+
virtual int derived_begin() override { return 0;}
4141
};
4242

4343
#endif /* PF1550_IO_ENVIEH747_H_ */

0 commit comments

Comments
 (0)