From c9f9f1d47dc9a7141ebaf75d10f3f2e7e4fe50b2 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Mon, 22 Jul 2024 22:26:00 +0800 Subject: [PATCH] Added nrf52xx support --- platformio.ini | 5 +++ src/PowersSY6970.tpp | 2 +- src/XPowersCommon.tpp | 71 ++++++++++++++++++++++++++++++++----------- 3 files changed, 59 insertions(+), 19 deletions(-) diff --git a/platformio.ini b/platformio.ini index 4a6275d..2ed85a9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -29,6 +29,7 @@ src_dir = examples/BQ25896_Shutdown_Example default_envs=esp32s3 ; default_envs=esp32dev ; default_envs=nucleo_f411re +; default_envs = nrf52840 [env] lib_extra_dirs = . @@ -73,3 +74,7 @@ board = nucleo_f411re upload_protocol = stlink +[env:nrf52840] +platform = nordicnrf52 +board = adafruit_feather_nrf52840 +framework = arduino \ No newline at end of file diff --git a/src/PowersSY6970.tpp b/src/PowersSY6970.tpp index b4efa69..465c7e3 100644 --- a/src/PowersSY6970.tpp +++ b/src/PowersSY6970.tpp @@ -904,7 +904,7 @@ public: }; Serial.println(); Serial.println("-------------------------"); - for (int i = 0; i < sizeof(regis) / sizeof(regis[0]); ++i) { + for (uint32_t i = 0; i < sizeof(regis) / sizeof(regis[0]); ++i) { int val = readRegister(regis[i]); if (val == -1) { continue; diff --git a/src/XPowersCommon.tpp b/src/XPowersCommon.tpp index 5532f03..c27010e 100644 --- a/src/XPowersCommon.tpp +++ b/src/XPowersCommon.tpp @@ -52,7 +52,7 @@ #endif //ESP_PLATFORM -#define XPOWERSLIB_I2C_MASTER_SEEED 400000 +#define XPOWERSLIB_I2C_MASTER_SPEED 400000 #ifdef _BV @@ -100,17 +100,33 @@ #endif #ifndef ESP32 +#ifdef LOG_FILE_LINE_INFO +#undef LOG_FILE_LINE_INFO +#endif +#define LOG_FILE_LINE_INFO __FILE__, __LINE__ #ifndef log_e -#define log_e(...) Serial.printf(__VA_ARGS__) +#define log_e(fmt, ...) Serial.printf("[E][%s:%d] " fmt "\n", LOG_FILE_LINE_INFO, ##__VA_ARGS__) #endif #ifndef log_i -#define log_i(...) Serial.printf(__VA_ARGS__) +#define log_i(fmt, ...) Serial.printf("[I][%s:%d] " fmt "\n", LOG_FILE_LINE_INFO, ##__VA_ARGS__) #endif #ifndef log_d -#define log_d(...) Serial.printf(__VA_ARGS__) +#define log_d(fmt, ...) Serial.printf("[D][%s:%d] " fmt "\n", LOG_FILE_LINE_INFO, ##__VA_ARGS__) +#endif +#endif + + +#if defined(NRF52840_XXAA) || defined(NRF52832_XXAA) +#ifndef SDA +#define SDA (0xFF) +#endif + +#ifndef SCL +#define SCL (0xFF) #endif #endif + typedef int (*iic_fptr_t)(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len); template @@ -127,13 +143,23 @@ public: __sda = sda; __scl = scl; __wire = &w; -#if defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) - __wire->end(); - __wire->setSDA(__sda); - __wire->setSCL(__scl); + +#if defined(NRF52840_XXAA) || defined(NRF52832_XXAA) + if (__sda != 0xFF && __scl != 0xFF) { + __wire->setPins(__sda, __scl); + } __wire->begin(); +#elif defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) + if (__sda != 0xFF && __scl != 0xFF) { + __wire->end(); + __wire->setSDA(__sda); + __wire->setSCL(__scl); + } + __wire->begin(); +#elif defined(ARDUINO_ARCH_ESP32) + __wire->begin(__sda, __scl); #else - __wire->begin(sda, scl); + __wire->begin(); #endif __addr = addr; return thisChip().initImpl(); @@ -147,7 +173,7 @@ public: // * which is useful when the bus shares multiple devices. bool begin(i2c_master_bus_handle_t i2c_dev_bus_handle, uint8_t addr) { - log_i("Using ESP-IDF Driver interface.\n"); + log_i("Using ESP-IDF Driver interface."); if (i2c_dev_bus_handle == NULL) return false; if (__has_init)return thisChip().initImpl(); @@ -171,7 +197,7 @@ public: i2c_device_config_t i2c_dev_conf = { .dev_addr_length = I2C_ADDR_BIT_LEN_7, .device_address = addr, - .scl_speed_hz = XPOWERSLIB_I2C_MASTER_SEEED, + .scl_speed_hz = XPOWERSLIB_I2C_MASTER_SPEED, #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,3,0)) #if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,4,0)) // New fields since esp-idf-v5.3-beta1 @@ -205,7 +231,7 @@ public: bool begin(i2c_port_t port_num, uint8_t addr, int sda, int scl) { __i2c_num = port_num; - log_i("Using ESP-IDF Driver interface.\n"); + log_i("Using ESP-IDF Driver interface."); if (__has_init)return thisChip().initImpl(); __sda = sda; __scl = scl; @@ -220,7 +246,7 @@ public: i2c_conf.scl_io_num = scl; i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE; i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE; - i2c_conf.master.clk_speed = XPOWERSLIB_I2C_MASTER_SEEED; + i2c_conf.master.clk_speed = XPOWERSLIB_I2C_MASTER_SPEED; /** * @brief Without checking whether the initialization is successful, @@ -419,13 +445,22 @@ protected: __has_init = true; 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); +#if defined(NRF52840_XXAA) || defined(NRF52832_XXAA) + if (__sda != 0xFF && __scl != 0xFF) { + __wire->setPins(__sda, __scl); + } __wire->begin(); -#else +#elif defined(ARDUINO_ARCH_RP2040) || defined(ARDUINO_ARCH_STM32) + if (__sda != 0xFF && __scl != 0xFF) { + __wire->end(); + __wire->setSDA(__sda); + __wire->setSCL(__scl); + } + __wire->begin(); +#elif defined(ARDUINO_ARCH_ESP32) __wire->begin(__sda, __scl); +#else + __wire->begin(); #endif } #endif /*ARDUINO*/