diff --git a/.github/workflows/compile-examples.yml b/.github/workflows/compile-examples.yml index cc19b1f..88aba7e 100644 --- a/.github/workflows/compile-examples.yml +++ b/.github/workflows/compile-examples.yml @@ -29,6 +29,8 @@ jobs: runs-on: ubuntu-latest env: + UNIVERSAL_SKETCH_PATHS: | + - examples/BasicUsage SKETCHES_REPORTS_PATH: sketches-reports strategy: @@ -40,12 +42,21 @@ jobs: # See: https://github.com/arduino/compile-sketches#platforms platforms: | - name: arduino:mbed_nicla - - fqbn: arduino:mbed_portenta:envie_m4 + sketch-paths: + - fqbn: arduino:mbed_portenta:envie_m7:target_core=cm4 platforms: | - name: arduino:mbed_portenta + sketch-paths: - fqbn: arduino:mbed_portenta:envie_m7 platforms: | - name: arduino:mbed_portenta + sketch-paths: + - fqbn: arduino:renesas:portenta_h33 + platforms: | + - name: arduino:renesas + source-url: https://downloads.arduino.cc/packages/package_renesas_index.json + sketch-paths: | + - examples/C33-Low-Power steps: - name: Checkout repository @@ -63,7 +74,8 @@ jobs: # Additional library dependencies can be listed here. # See: https://github.com/arduino/compile-sketches#libraries sketch-paths: | - - examples + ${{ env.UNIVERSAL_SKETCH_PATHS }} + ${{ matrix.sketch-paths }} enable-deltas-report: true sketches-report-path: ${{ env.SKETCHES_REPORTS_PATH }} diff --git a/examples/C33-Low-Power/C33-Low-Power.ino b/examples/C33-Low-Power/C33-Low-Power.ino new file mode 100644 index 0000000..298c5ee --- /dev/null +++ b/examples/C33-Low-Power/C33-Low-Power.ino @@ -0,0 +1,112 @@ +#include + +#include + +#include "RTC.h" +#include "r_lpm.h" + +void periodic_cbk() +{ + digitalWrite(LEDR, !digitalRead(LEDR)); +} + +void alarm_cbk() +{ + digitalWrite(LED_BUILTIN, LOW); + delay(500); + digitalWrite(LED_BUILTIN, HIGH); +} + +lpm_instance_ctrl_t p_api_ctrl; +lpm_cfg_t p_cfg; + +void setup() +{ + Serial.begin(9600); + while (!Serial) { } + + //PMIC.debug(Serial); + PMIC.begin(); + PMIC.configLDO1(Ldo1Voltage::V_3_30, false, false, false); + PMIC.configLDO2(Ldo2Voltage::V_3_30, false, false, false); + PMIC.configLDO3(Ldo3Voltage::V_1_20, false, false, false); + PMIC.configSw2(Sw2Voltage::V_3_30, Sw2Voltage::V_3_30, Sw2Voltage::V_3_30, Sw2CurrentLimit::I_1_5_A, false, false, false); + + p_cfg.low_power_mode = LPM_MODE_DEEP; // LPM_MODE_SLEEP LPM_MODE_STANDBY LPM_MODE_STANDBY_SNOOZE LPM_MODE_DEEP + p_cfg.standby_wake_sources = LPM_STANDBY_WAKE_SOURCE_IRQ0 | LPM_STANDBY_WAKE_SOURCE_RTCALM; + p_cfg.dtc_state_in_snooze = LPM_SNOOZE_DTC_DISABLE; // LPM_SNOOZE_DTC_ENABLE LPM_SNOOZE_DTC_DISABLE + p_cfg.power_supply_state = LPM_POWER_SUPPLY_DEEPCUT0; // LPM_POWER_SUPPLY_DEEPCUT0, LPM_POWER_SUPPLY_DEEPCUT1, LPM_POWER_SUPPLY_DEEPCUT3 + p_cfg.output_port_enable = LPM_OUTPUT_PORT_ENABLE_RETAIN; // LPM_OUTPUT_PORT_ENABLE_RETAIN LPM_OUTPUT_PORT_ENABLE_HIGH_IMPEDANCE + p_cfg.io_port_state = LPM_IO_PORT_NO_CHANGE; // LPM_IO_PORT_NO_CHANGE LPM_IO_PORT_RESET + p_cfg.deep_standby_cancel_source = LPM_DEEP_STANDBY_CANCEL_SOURCE_RTC_ALARM; + + R_LPM_Open(&p_api_ctrl, &p_cfg); + + /* Configure LED_BUILTIN. */ + pinMode(LED_BUILTIN, OUTPUT); + digitalWrite(LED_BUILTIN, HIGH); + + /* Configure the red RGB LED. */ + pinMode(LEDR, OUTPUT); + digitalWrite(LEDR, LOW); + delay(200); + digitalWrite(LEDR, HIGH); + + /* Initialize the RTC. */ + RTC.begin(); + RTCTime initial_time(1, Month::JANUARY, 2000, 12, 10, 00, DayOfWeek::TUESDAY, SaveLight::SAVING_TIME_ACTIVE); + + if (!RTC.isRunning()) + RTC.setTime(initial_time); + + RTCTime alarm_time; + alarm_time.setSecond(35); + + AlarmMatch alarm_match; + alarm_match.addMatchSecond(); + + if (!RTC.setPeriodicCallback(periodic_cbk, Period::ONCE_EVERY_2_SEC)) + Serial.println("ERROR: periodic callback not set"); + + if (!RTC.setAlarmCallback(alarm_cbk, alarm_time, alarm_match)) + Serial.println("ERROR: alarm callback not set"); +} + +void loop() +{ + /* Enter low power mode. Note: The JLink looses connection here. */ + R_LPM_LowPowerModeEnter(&p_api_ctrl); + + if (RTC.isRunning()) + { + /* GET CURRENT TIME FROM RTC */ + RTCTime current_time; + RTC.getTime(current_time); + + /* PRINT CURRENT TIME on Serial */ + Serial.print("Current time: "); + /* DATE */ + Serial.print(current_time.getDayOfMonth()); + Serial.print("/"); + Serial.print(Month2int(current_time.getMonth())); + Serial.print("/"); + Serial.print(current_time.getYear()); + Serial.print(" - "); + Serial.print(current_time.getUnixTime()); + Serial.print(" - "); + + struct timeval tv; + gettimeofday(&tv, NULL); + Serial.print(tv.tv_sec); + Serial.print(" - "); + + /* HOUR:MINUTES:SECONDS */ + Serial.print(current_time.getHour()); + Serial.print(":"); + Serial.print(current_time.getMinutes()); + Serial.print(":"); + Serial.println(current_time.getSeconds()); + + delay(1000); + } +} diff --git a/library.properties b/library.properties index 2d8d843..39de148 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 +architectures=mbed,mbed_nicla,mbed_portenta,renesas diff --git a/src/PF1550.cpp b/src/PF1550.cpp index 72a46f0..a723346 100644 --- a/src/PF1550.cpp +++ b/src/PF1550.cpp @@ -22,6 +22,7 @@ #include "PF1550.h" +#include "PF1550/PF1550_Io_C33.h" #include "PF1550/PF1550_Io_EnvieH747.h" /****************************************************************************** @@ -157,6 +158,10 @@ void PF1550::configCharger(IFastCharge const i_fast_charge, EXTERN DEFINITION ******************************************************************************/ -static PF1550_Io_EnvieH747 io; +#ifdef ARDUINO_PORTENTA_H33 +static PF1550_Io_C33 io(interface::PF1550_I2C_DEFAULT_ADDR); +#else +static PF1550_Io_EnvieH747 io(interface::PF1550_I2C_DEFAULT_ADDR); +#endif PF1550 PMIC(io); diff --git a/src/PF1550.h b/src/PF1550.h index 0a66888..d06149e 100644 --- a/src/PF1550.h +++ b/src/PF1550.h @@ -19,7 +19,7 @@ #ifndef ARDUINO_PF1550_H_ #define ARDUINO_PF1550_H_ -#define PF1550_I2C_ADDR 0x08 +#define PF1550_I2C_ADDR (interface::PF1550_I2C_DEFAULT_ADDR) /****************************************************************************** INCLUDE diff --git a/src/PF1550/PF1550_Defines.h b/src/PF1550/PF1550_Defines.h index 0b2b333..67a536b 100644 --- a/src/PF1550/PF1550_Defines.h +++ b/src/PF1550/PF1550_Defines.h @@ -68,4 +68,10 @@ /* VBUS_INLIM_CNFG ************************************************************/ #define REG_VBUS_INLIM_CNFG_VBUS_LIN_INLIM_mask (0xF8) +/* LED_PWM ********************************************************************/ +#define REG_LED_PWM_LED_EN_bp (7) + +/* LED_CNFG *******************************************************************/ +#define REG_LED_CNFG_LEDOVRD_bp (5) + #endif /* PF1550_DEFINES_H_ */ diff --git a/src/PF1550/PF1550_Io_C33.cpp b/src/PF1550/PF1550_Io_C33.cpp new file mode 100644 index 0000000..6e5aedc --- /dev/null +++ b/src/PF1550/PF1550_Io_C33.cpp @@ -0,0 +1,128 @@ +/* + Copyright (c) 2019 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include "PF1550_Io_C33.h" + +#ifdef ARDUINO_PORTENTA_H33 + +#include "Wire.h" +#include "Arduino.h" + +#include "PF1550_Defines.h" + +/****************************************************************************** + CTOR/DTOR + ******************************************************************************/ + +PF1550_Io_C33::PF1550_Io_C33(uint8_t const i2c_addr) +: _i2c_addr(i2c_addr) +{ + +} + +/****************************************************************************** + PUBLIC MEMBER FUNCTIONS + ******************************************************************************/ + +int PF1550_Io_C33::begin() +{ + Wire3.begin(); + Wire3.setClock(100000); + + /* Enable LED. */ + 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); + + return 1; +} + +void PF1550_Io_C33::readRegister(Register const reg_addr, uint8_t * data) +{ + uint8_t i2c_data[2]; + + i2c_data[0] = (uint8_t)reg_addr; + + writeRegister(_i2c_addr, i2c_data, 1, 1); + + uint8_t retVal = Wire3.requestFrom(_i2c_addr, 1, true); + + if (_debug) { + _debug->print("requestFrom: "); + _debug->println(retVal); + } + + int i = 0; + while (Wire3.available() && i < 1) { + data[0] = Wire3.read(); + i++; + } + + if (_debug) { + _debug->println("Read:"); + for (i = 0; i < 1; i++) { + _debug->println(data[i], HEX); + } + } +} + +void PF1550_Io_C33::writeRegister(uint8_t slave_addr, uint8_t *data, uint8_t data_len, uint8_t restart) +{ + if (_debug) + { + _debug->print("Restart: "); + _debug->println(restart); + if (!restart) + { + _debug->print("PF1550_Io_C33::writeRegister at address="); + _debug->print(data[0], HEX); + _debug->print(" data="); + _debug->println(data[1], HEX); + _debug->print("i2c slave address: "); + _debug->println(slave_addr); + } + else + { + _debug->print("PF1550_Io_C33::Read from register at address="); + _debug->println(data[0], HEX); + } + } + + Wire3.beginTransmission(slave_addr); + Wire3.write(data, data_len); + uint8_t retVal = Wire3.endTransmission(restart == 0 ? true : false); + + if (_debug) { + _debug->print("End transmission: "); + _debug->println(retVal); + } + /* + if (_debug) { + _debug->println("Write:"); + for (int i = 0; i < data_len; i++) { + _debug->println(data[i], HEX); + } + } + */ +} + +#endif /* ARDUINO_PORTENTA_H33 */ diff --git a/src/PF1550/PF1550_Io_C33.h b/src/PF1550/PF1550_Io_C33.h new file mode 100644 index 0000000..f342c54 --- /dev/null +++ b/src/PF1550/PF1550_Io_C33.h @@ -0,0 +1,52 @@ +/* + Copyright (c) 2019 Arduino. All right reserved. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + See the GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#ifndef PF1550_IO_C33_H_ +#define PF1550_IO_C33_H_ + +/****************************************************************************** + INCLUDE + ******************************************************************************/ + +#include "Arduino.h" +#include "interface/PF1550_Io.h" + +/****************************************************************************** + CLASS DECLARATION + ******************************************************************************/ + +class PF1550_Io_C33 : public interface::PF1550_Io +{ +public: + PF1550_Io_C33(uint8_t const i2c_addr); + virtual ~PF1550_Io_C33() { } + + virtual int begin () override; + virtual void readRegister (Register const reg_addr, uint8_t * data) override; + virtual void writeRegister(uint8_t slave_addr, uint8_t * data, uint8_t data_len, uint8_t restart) override; + virtual void setSTANDBY () override { } + virtual void clrSTANDBY () override { } + + +private: + + uint8_t const _i2c_addr; + Stream * _debug; +}; + +#endif /* PF1550_IO_C33_H_ */ diff --git a/src/PF1550/PF1550_Io_EnvieH747.cpp b/src/PF1550/PF1550_Io_EnvieH747.cpp index 9039fde..793ebf2 100644 --- a/src/PF1550/PF1550_Io_EnvieH747.cpp +++ b/src/PF1550/PF1550_Io_EnvieH747.cpp @@ -46,11 +46,6 @@ int PF1550_Io_EnvieH747::begin() return 1; } -void PF1550_Io_EnvieH747::debug(Stream& stream) -{ - _debug = &stream; -} - void PF1550_Io_EnvieH747::readRegister(Register const reg_addr, uint8_t *data) { uint8_t i2c_data[2]; diff --git a/src/PF1550/PF1550_Io_EnvieH747.h b/src/PF1550/PF1550_Io_EnvieH747.h index bc546a6..718482d 100644 --- a/src/PF1550/PF1550_Io_EnvieH747.h +++ b/src/PF1550/PF1550_Io_EnvieH747.h @@ -35,11 +35,10 @@ class PF1550_Io_EnvieH747 : public interface::PF1550_Io public: - PF1550_Io_EnvieH747(uint8_t const i2c_addr = interface::PF1550_I2C_DEFAULT_ADDR); + PF1550_Io_EnvieH747(uint8_t const i2c_addr); virtual ~PF1550_Io_EnvieH747() { } virtual int begin () override; - virtual void debug (Stream& stream) override; virtual void readRegister (Register const reg_addr, uint8_t *data) override; virtual void writeRegister(uint8_t slave_addr, uint8_t *data, uint8_t data_len, uint8_t restart) override; virtual void setSTANDBY () override; diff --git a/src/PF1550/interface/PF1550_Io.h b/src/PF1550/interface/PF1550_Io.h index 2498c96..b89a9db 100644 --- a/src/PF1550/interface/PF1550_Io.h +++ b/src/PF1550/interface/PF1550_Io.h @@ -50,8 +50,11 @@ class PF1550_Io virtual ~PF1550_Io() { } + + void debug (Stream& stream) { _debug = &stream; } + + virtual int begin () = 0; - virtual void debug (Stream& stream) = 0; virtual void readRegister (Register const reg_addr, uint8_t *data) = 0; virtual void writeRegister(uint8_t slave_addr, uint8_t *data, uint8_t data_len, uint8_t restart) = 0; virtual void setSTANDBY () = 0; @@ -60,6 +63,9 @@ class PF1550_Io void setBit (Register const reg, uint8_t const bit_pos); void clrBit (Register const reg, uint8_t const bit_pos); + +protected: + Stream * _debug; }; /******************************************************************************