Skip to content

Commit

Permalink
[nRF52] build with SY6970 PMU driver
Browse files Browse the repository at this point in the history
  • Loading branch information
lyusupov committed Jun 17, 2024
1 parent 8248131 commit 006d12f
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 3 deletions.
1 change: 1 addition & 0 deletions software/firmware/source/SoftRF/src/driver/Battery.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ enum
PMU_AXP192,
PMU_AXP202,
PMU_AXP2101,
PMU_SY6970,
};

void Battery_setup(void);
Expand Down
70 changes: 70 additions & 0 deletions software/firmware/source/SoftRF/src/platform/nRF52.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,17 @@ static bool play_file(char *filename)
}
#endif /* USE_EXT_I2S_DAC */

#if !defined(EXCLUDE_PMU)
#define POWERS_CHIP_SY6970
#define SDA SOC_GPIO_PIN_SDA
#define SCL SOC_GPIO_PIN_SCL
#include <XPowersLib.h>

PowersSY6970 sy6970;

static bool nRF52_has_pmu = false;
#endif /* EXCLUDE_PMU */

static void nRF52_setup()
{
ui = &ui_settings;
Expand Down Expand Up @@ -641,6 +652,65 @@ static void nRF52_setup()
USBDevice.setDeviceVersion(nRF52_Device_Version);
#endif /* ARDUINO_ARCH_MBED */

#if !defined(EXCLUDE_PMU)
nRF52_has_pmu = sy6970.init(Wire, SOC_GPIO_PMU_SDA, SOC_GPIO_PMU_SCL, SY6970_SLAVE_ADDRESS);

if (nRF52_has_pmu) {
nRF52_board = NRF52_LILYGO_TULTIMA;
hw_info.model = SOFTRF_MODEL_NEO;
hw_info.pmu = PMU_SY6970;

// Set the minimum operating voltage. Below this voltage, the PMU will protect
sy6970.setSysPowerDownVoltage(3300);

// Set input current limit, default is 500mA
sy6970.setInputCurrentLimit(3250);

//Serial.printf("getInputCurrentLimit: %d mA\n",sy6970.getInputCurrentLimit());

// Disable current limit pin
sy6970.disableCurrentLimitPin();

// Set the charging target voltage, Range:3840 ~ 4608mV ,step:16 mV
sy6970.setChargeTargetVoltage(4208);

// Set the precharge current , Range: 64mA ~ 1024mA ,step:64mA
sy6970.setPrechargeCurr(64);

// The premise is that Limit Pin is disabled, or it will only follow the maximum charging current set by Limi tPin.
// Set the charging current , Range:0~5056mA ,step:64mA
sy6970.setChargerConstantCurr(832);

// Get the set charging current
sy6970.getChargerConstantCurr();
//Serial.printf("getChargerConstantCurr: %d mA\n",sy6970.getChargerConstantCurr());


// To obtain voltage data, the ADC must be enabled first
sy6970.enableADCMeasure();

// Turn on charging function
// If there is no battery connected, do not turn on the charging function
sy6970.enableCharge();

// Turn off charging function
// If USB is used as the only power input, it is best to turn off the charging function,
// otherwise the VSYS power supply will have a sawtooth wave, affecting the discharge output capability.
// sy6970.disableCharge();


// The OTG function needs to enable OTG, and set the OTG control pin to HIGH
// After OTG is enabled, if an external power supply is plugged in, OTG will be turned off

// sy6970.enableOTG();
// sy6970.disableOTG();
// pinMode(OTG_ENABLE_PIN, OUTPUT);
// digitalWrite(OTG_ENABLE_PIN, HIGH);
} else {
Wire.end();
}
#endif /* EXCLUDE_PMU */

#if defined(USE_TINYUSB)
Serial1.setPins(SOC_GPIO_PIN_CONS_RX, SOC_GPIO_PIN_CONS_TX);
#if defined(EXCLUDE_WIFI)
Expand Down
4 changes: 4 additions & 0 deletions software/firmware/source/SoftRF/src/platform/nRF52.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,9 @@ struct rst_info {
/* RTC */
#define SOC_GPIO_PIN_R_INT _PINNUM(0, 16) // P0.16

#define SOC_GPIO_PMU_SDA _PINNUM(1, 0) // P1.00
#define SOC_GPIO_PMU_SCL _PINNUM(0, 24) // P0.24

#define EXCLUDE_WIFI
//#define EXCLUDE_OTA
//#define USE_ARDUINO_WIFI
Expand Down Expand Up @@ -329,6 +332,7 @@ struct rst_info {
#define EXCLUDE_IMU
#endif /* ARDUINO_ARCH_MBED */
//#define USE_EXT_I2S_DAC
#define EXCLUDE_PMU

/* FTD-012 data port protocol version 8 and 9 */
#define PFLAA_EXT1_FMT ",%d,%d,%d"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,23 @@

#endif

#ifndef ESP32
#ifndef log_e
#define log_e(...) Serial.printf(__VA_ARGS__)
#endif
#ifndef log_i
#define log_i(...) Serial.printf(__VA_ARGS__)
#endif
#ifndef log_d
#define log_d(...) Serial.printf(__VA_ARGS__)
#endif
#endif

typedef int (*iic_fptr_t)(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len);

template <class chipType>
class XPowersCommon
{
typedef int (*iic_fptr_t)(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len);

public:

Expand All @@ -85,7 +98,18 @@ public:
if (__has_init)return thisChip().initImpl();
__has_init = true;
__wire = &w;
#if defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32)
__wire->end();
__wire->setSDA(__sda);
__wire->setSCL(__scl);
__wire->begin();
#elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF52840)
__wire->end();
__wire->setPins(__sda, __scl);
__wire->begin();
#else
__wire->begin(sda, scl);
#endif
__addr = addr;
return thisChip().initImpl();
}
Expand Down Expand Up @@ -245,8 +269,21 @@ protected:
#if defined(ARDUINO)
if (__has_init) return thisChip().initImpl();
__has_init = true;
log_i("SDA:%d SCL:%d", __sda, __scl);
__wire->begin(__sda, __scl);
if (__wire) {
log_i("SDA:%d SCL:%d", __sda, __scl);
#if defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32)
__wire->end();
__wire->setSDA(__sda);
__wire->setSCL(__scl);
__wire->begin();
#elif defined(ARDUINO_ARCH_NRF52) || defined(ARDUINO_ARCH_NRF52840)
__wire->end();
__wire->setPins(__sda, __scl);
__wire->begin();
#else
__wire->begin(__sda, __scl);
#endif
}
#endif /*ARDUINO*/
return thisChip().initImpl();
}
Expand Down

0 comments on commit 006d12f

Please sign in to comment.