diff --git a/software/firmware/source/SoftRF/src/driver/Battery.h b/software/firmware/source/SoftRF/src/driver/Battery.h index b4ab3e297..2aa533ead 100644 --- a/software/firmware/source/SoftRF/src/driver/Battery.h +++ b/software/firmware/source/SoftRF/src/driver/Battery.h @@ -56,6 +56,7 @@ enum PMU_AXP192, PMU_AXP202, PMU_AXP2101, + PMU_SY6970, }; void Battery_setup(void); diff --git a/software/firmware/source/SoftRF/src/platform/nRF52.cpp b/software/firmware/source/SoftRF/src/platform/nRF52.cpp index 8fd8bcc2d..004a57609 100644 --- a/software/firmware/source/SoftRF/src/platform/nRF52.cpp +++ b/software/firmware/source/SoftRF/src/platform/nRF52.cpp @@ -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 + +PowersSY6970 sy6970; + +static bool nRF52_has_pmu = false; +#endif /* EXCLUDE_PMU */ + static void nRF52_setup() { ui = &ui_settings; @@ -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) diff --git a/software/firmware/source/SoftRF/src/platform/nRF52.h b/software/firmware/source/SoftRF/src/platform/nRF52.h index bb2e4b5c4..edf26e1ea 100644 --- a/software/firmware/source/SoftRF/src/platform/nRF52.h +++ b/software/firmware/source/SoftRF/src/platform/nRF52.h @@ -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 @@ -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" diff --git a/software/firmware/source/libraries/XPowersLib/src/XPowersCommon.tpp b/software/firmware/source/libraries/XPowersLib/src/XPowersCommon.tpp index 5e2aad62c..e98c8d96b 100644 --- a/software/firmware/source/libraries/XPowersLib/src/XPowersCommon.tpp +++ b/software/firmware/source/libraries/XPowersLib/src/XPowersCommon.tpp @@ -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 XPowersCommon { - typedef int (*iic_fptr_t)(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len); public: @@ -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(); } @@ -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(); }