Skip to content

Add support for Portenta C33. #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Mar 28, 2023
Merged
16 changes: 14 additions & 2 deletions .github/workflows/compile-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ jobs:
runs-on: ubuntu-latest

env:
UNIVERSAL_SKETCH_PATHS: |
- examples/BasicUsage
SKETCHES_REPORTS_PATH: sketches-reports

strategy:
Expand All @@ -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
Expand All @@ -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 }}

Expand Down
112 changes: 112 additions & 0 deletions examples/C33-Low-Power/C33-Low-Power.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include <Arduino_PMIC.h>

#include <Wire.h>

#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);
}
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 6 additions & 1 deletion src/PF1550.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "PF1550.h"

#include "PF1550/PF1550_Io_C33.h"
#include "PF1550/PF1550_Io_EnvieH747.h"

/******************************************************************************
Expand Down Expand Up @@ -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);
2 changes: 1 addition & 1 deletion src/PF1550.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/PF1550/PF1550_Defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_ */
128 changes: 128 additions & 0 deletions src/PF1550/PF1550_Io_C33.cpp
Original file line number Diff line number Diff line change
@@ -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 */
52 changes: 52 additions & 0 deletions src/PF1550/PF1550_Io_C33.h
Original file line number Diff line number Diff line change
@@ -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_ */
5 changes: 0 additions & 5 deletions src/PF1550/PF1550_Io_EnvieH747.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down
Loading