From ac4f482649636f4622c6f15ae12e627d429e56ee Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Mon, 13 May 2024 15:06:52 +0200 Subject: [PATCH 1/6] Ensure single initialization --- src/PF1550.cpp | 11 ++++++++++- src/PF1550.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/PF1550.cpp b/src/PF1550.cpp index fe8800c..0174f06 100644 --- a/src/PF1550.cpp +++ b/src/PF1550.cpp @@ -45,10 +45,19 @@ PF1550::PF1550(PF1550_IO & io) int PF1550::begin() { + if(_initialized) { + return 0; + } + if (_debug) { _debug->println("PF1550 begin"); } - return _control.begin(); + int returnCode = _control.begin(); + + if (returnCode == 0) { + _initialized = true; + } + return returnCode; } void PF1550::debug(Stream& stream) diff --git a/src/PF1550.h b/src/PF1550.h index 2d41571..295546c 100644 --- a/src/PF1550.h +++ b/src/PF1550.h @@ -100,6 +100,7 @@ class PF1550 private: PF1550_Control _control; + volatile bool _initialized; Stream* _debug; }; From af337a7f1f7177683bc9c0175fad29488520f219 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Mon, 13 May 2024 15:07:25 +0200 Subject: [PATCH 2/6] Add proper architecture flag for C33 --- library.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library.properties b/library.properties index f76d986..933e646 100644 --- a/library.properties +++ b/library.properties @@ -6,4 +6,4 @@ sentence=Arduino library for the PF1550 Power Management IC paragraph=This library allows the control and configuration of the PF1550 used on various Arduino boards. category=Device Control url=https://github.com/arduino-libraries/Arduino_PF1550 -architectures=mbed,mbed_nicla,mbed_portenta,renesas +architectures=mbed,mbed_nicla,mbed_portenta,renesas,renesas_portenta From 4b312d619015b39242b3657f692888ce26ddae37 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Mon, 13 May 2024 15:08:55 +0200 Subject: [PATCH 3/6] Handle error of endTransmission --- src/PF1550/PF1550_IO.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/PF1550/PF1550_IO.cpp b/src/PF1550/PF1550_IO.cpp index 334334b..cdc3110 100644 --- a/src/PF1550/PF1550_IO.cpp +++ b/src/PF1550/PF1550_IO.cpp @@ -52,7 +52,14 @@ void PF1550_IO::readRegister(Register const reg_addr, uint8_t * data) { _wire->beginTransmission(_i2c_addr); _wire->write(static_cast(reg_addr)); - _wire->endTransmission(false); /* No Stop. */ + + /* No Stop. */ + if(_wire->endTransmission(false) != 0) { + if (_debug) { + _debug->println("PF1550_IO::readRegister: endTransmission failed"); + } + return; + } _wire->requestFrom(_i2c_addr, 1, true); while (_wire->available() < 1) { } From 67f91416df94a907ae6ae743123cb7881e632a7e Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Mon, 13 May 2024 15:09:58 +0200 Subject: [PATCH 4/6] Add getBit function --- src/PF1550/PF1550_IO.cpp | 8 ++++++++ src/PF1550/PF1550_IO.h | 1 + 2 files changed, 9 insertions(+) diff --git a/src/PF1550/PF1550_IO.cpp b/src/PF1550/PF1550_IO.cpp index cdc3110..4059436 100644 --- a/src/PF1550/PF1550_IO.cpp +++ b/src/PF1550/PF1550_IO.cpp @@ -84,6 +84,14 @@ void PF1550_IO::setBit(Register const reg, uint8_t const bit_pos) writeRegister(reg, reg_val); } +uint8_t PF1550_IO::getBit(Register const reg, uint8_t const bit_pos) +{ + assert(bit_pos < 8); + uint8_t reg_val; + readRegister(reg, ®_val); + return (reg_val >> bit_pos) & 1; +} + void PF1550_IO::clrBit(Register const reg, uint8_t const bit_pos) { assert(bit_pos < 8); diff --git a/src/PF1550/PF1550_IO.h b/src/PF1550/PF1550_IO.h index caa2323..89ffd90 100644 --- a/src/PF1550/PF1550_IO.h +++ b/src/PF1550/PF1550_IO.h @@ -56,6 +56,7 @@ class PF1550_IO void writeRegister(Register const reg_addr, uint8_t const data); void setBit(Register const reg, uint8_t const bit_pos); + uint8_t getBit(Register const reg, uint8_t const bit_pos); void clrBit(Register const reg, uint8_t const bit_pos); From b6d9dff207f7e1ab8c78b00b99fd7f0194cadf63 Mon Sep 17 00:00:00 2001 From: Sebastian Romero Date: Mon, 13 May 2024 15:11:26 +0200 Subject: [PATCH 5/6] Add missing implementations of derived_begin --- src/PF1550/PF1550_IO_C33.h | 9 +++++++++ src/PF1550/PF1550_IO_Nicla_Vision.h | 2 +- src/PF1550/PF1550_IO_Portenta_H7.h | 2 +- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/PF1550/PF1550_IO_C33.h b/src/PF1550/PF1550_IO_C33.h index 3aa564f..e08ec03 100644 --- a/src/PF1550/PF1550_IO_C33.h +++ b/src/PF1550/PF1550_IO_C33.h @@ -45,6 +45,15 @@ class PF1550_IO_C33 : public PF1550_IO setBit(Register::CHARGER_LED_PWM, REG_LED_PWM_LED_EN_bp); /* Allow LED control by software. */ setBit(Register::CHARGER_LED_CNFG, REG_LED_CNFG_LEDOVRD_bp); + + if(getBit(Register::CHARGER_LED_PWM, REG_LED_PWM_LED_EN_bp) == 0){ + return -1; + } + + if(getBit(Register::CHARGER_LED_CNFG, REG_LED_CNFG_LEDOVRD_bp) == 0){ + return -1; + } + return 0; } }; diff --git a/src/PF1550/PF1550_IO_Nicla_Vision.h b/src/PF1550/PF1550_IO_Nicla_Vision.h index 3bdce52..9f81b9c 100644 --- a/src/PF1550/PF1550_IO_Nicla_Vision.h +++ b/src/PF1550/PF1550_IO_Nicla_Vision.h @@ -37,7 +37,7 @@ class PF1550_IO_Nicla_Vision : public PF1550_IO protected: - virtual int derived_begin() override { } + virtual int derived_begin() override { return 0; } }; #endif /* PF1550_IO_NICLA_VISION_H_ */ diff --git a/src/PF1550/PF1550_IO_Portenta_H7.h b/src/PF1550/PF1550_IO_Portenta_H7.h index 7e1a6f7..2c69055 100644 --- a/src/PF1550/PF1550_IO_Portenta_H7.h +++ b/src/PF1550/PF1550_IO_Portenta_H7.h @@ -37,7 +37,7 @@ class PF1550_IO_Portenta_H7 : public PF1550_IO protected: - virtual int derived_begin() override { } + virtual int derived_begin() override { return 0;} }; #endif /* PF1550_IO_ENVIEH747_H_ */ From e95199c264dd4c9dcec350745ab835eaaf60c265 Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Wed, 15 May 2024 07:49:06 +0200 Subject: [PATCH 6/6] Missing initialisation of "_initialized" to "false" leads to russian roulette whether or not "_control.begin()" is called. --- src/PF1550.cpp | 5 +++-- src/PF1550.h | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/PF1550.cpp b/src/PF1550.cpp index 0174f06..fd1ca7c 100644 --- a/src/PF1550.cpp +++ b/src/PF1550.cpp @@ -33,8 +33,9 @@ ******************************************************************************/ PF1550::PF1550(PF1550_IO & io) -: _control(io), - _debug(NULL) +: _control(io) +, _initialized(false) +, _debug(NULL) { } diff --git a/src/PF1550.h b/src/PF1550.h index 295546c..0e6430e 100644 --- a/src/PF1550.h +++ b/src/PF1550.h @@ -100,7 +100,7 @@ class PF1550 private: PF1550_Control _control; - volatile bool _initialized; + bool _initialized; Stream* _debug; };