From f4f55d9e93a9eb40351b6ad5f6fa995ab31ef992 Mon Sep 17 00:00:00 2001 From: lewisxhe Date: Wed, 24 Jul 2024 17:28:34 +0800 Subject: [PATCH] Added HUSB238 examples --- .../PowerDeliveryHUSB238_BleUart.ino | 178 ++++++++++++++++++ .../PowerDeliveryHUSB238_Example.ino | 72 +++++++ platformio.ini | 43 +++-- 3 files changed, 276 insertions(+), 17 deletions(-) create mode 100644 examples/PowerDeliveryHUSB238_BleUart/PowerDeliveryHUSB238_BleUart.ino create mode 100644 examples/PowerDeliveryHUSB238_Example/PowerDeliveryHUSB238_Example.ino diff --git a/examples/PowerDeliveryHUSB238_BleUart/PowerDeliveryHUSB238_BleUart.ino b/examples/PowerDeliveryHUSB238_BleUart/PowerDeliveryHUSB238_BleUart.ino new file mode 100644 index 0000000..6281d5a --- /dev/null +++ b/examples/PowerDeliveryHUSB238_BleUart/PowerDeliveryHUSB238_BleUart.ino @@ -0,0 +1,178 @@ +/** + * @file PowerDeliveryHUSB238_BleUart.ino + * @author Lewis He (lewishe@outlook.com) + * @license MIT + * @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd + * @date 2024-07-24 + * + */ + +#include + +#ifdef ARDUINO_ARCH_NRF52 + +#include +#include + +PowerDeliveryHUSB238 pd; + + +#ifndef CONFIG_PMU_SDA +#define CONFIG_PMU_SDA 0 +#endif + +#ifndef CONFIG_PMU_SCL +#define CONFIG_PMU_SCL 1 +#endif + +const uint8_t i2c_sda = CONFIG_PMU_SDA; +const uint8_t i2c_scl = CONFIG_PMU_SCL; + + +BLEUart bleuart; // uart over ble + + +void connect_callback(uint16_t conn_handle) +{ + BLEConnection *conn = Bluefruit.Connection(conn_handle); + Serial.println("Connected"); + + // request PHY changed to 2MB + Serial.println("Request to change PHY"); + conn->requestPHY(); + + // request to update data length + Serial.println("Request to change Data Length"); + conn->requestDataLengthUpdate(); + + // request mtu exchange + Serial.println("Request to change MTU"); + conn->requestMtuExchange(247); + + // request connection interval of 7.5 ms + //conn->requestConnectionParameter(6); // in unit of 1.25 + + // delay a bit for all the request to complete + delay(1000); +} + +void startAdv(void) +{ + // Advertising packet + + Bluefruit.Advertising.addFlags(BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE); + Bluefruit.Advertising.addTxPower(); + + // Include bleuart 128-bit uuid + Bluefruit.Advertising.addService(bleuart); + + // Secondary Scan Response packet (optional) + // Since there is no room for 'Name' in Advertising packet + Bluefruit.ScanResponse.addName(); + + /* Start Advertising + * - Enable auto advertising if disconnected + * - Interval: fast mode = 20 ms, slow mode = 152.5 ms + * - Timeout for fast mode is 30 seconds + * - Start(timeout) with timeout = 0 will advertise forever (until connected) + * + * For recommended advertising interval + * https://developer.apple.com/library/content/qa/qa1931/_index.html + */ + Bluefruit.Advertising.restartOnDisconnect(true); + Bluefruit.Advertising.setInterval(32, 244); // in unit of 0.625 ms + Bluefruit.Advertising.setFastTimeout(30); // number of seconds in fast mode + Bluefruit.Advertising.start(0); // 0 = Don't stop advertising after n seconds +} + +void setup() +{ + Serial.begin(115200); + // while (!Serial); + + bool result = pd.init(Wire, i2c_sda, i2c_scl, HUSB238_SLAVE_ADDRESS); + if (result == false) { + while (1) { + Serial.println("USB Power Delivery controller not online..."); + delay(1000); + } + } + + + Bluefruit.begin(); + Bluefruit.setName("PowerDelivery"); + Bluefruit.setTxPower(4); // Check bluefruit.h for supported values + Bluefruit.Periph.setConnectCallback(connect_callback); + Bluefruit.Periph.setConnInterval(6, 12); // 7.5 - 15 ms + + // Configure and Start BLE Uart Service + bleuart.begin(); + + startAdv(); + + Serial.println("Please use Adafruit's Bluefruit LE app to connect in UART mode"); +} + + +String buffer; + +void loop() +{ + + Serial.print("PD Voltage:"); Serial.print(pd.getPdVoltage()); Serial.print(" V"); + Serial.print(" Current: "); Serial.print(pd.getPdCurrent()); Serial.println(" A"); + + buffer = "Vol:"; + buffer.concat(pd.getPdVoltage()); + buffer.concat("V "); + buffer.concat("Cur:"); + buffer.concat(pd.getPdCurrent()); + buffer.concat("A"); + buffer.concat("Status:"); + + + PowerDeliveryHUSB238::PD_Status status = pd.status(); + Serial.print("USB Power Delivery Status : "); + + switch (status) { + case PowerDeliveryHUSB238::NO_RESPONSE: + Serial.println("no response"); + buffer.concat("no response"); + break; + case PowerDeliveryHUSB238::SUCCESS: + Serial.println("success"); + buffer.concat("success"); + break; + case PowerDeliveryHUSB238::INVALID_CMD: + Serial.println("invalid command"); + buffer.concat("invalid command"); + break; + case PowerDeliveryHUSB238::NOT_SUPPORT: + Serial.println("not support"); + buffer.concat("not support"); + break; + case PowerDeliveryHUSB238::TRANSACTION_FAIL: + Serial.println("transaction failed"); + buffer.concat("transaction failed"); + break; + default: + break; + } + bleuart.write(buffer.c_str()); + + delay(5000); +} + +#else +void setup() +{ + Serial.begin(115200); +} +void loop() +{ + Serial.println("ble examples only support nrf platform"); delay(1000); +} +#endif + + + diff --git a/examples/PowerDeliveryHUSB238_Example/PowerDeliveryHUSB238_Example.ino b/examples/PowerDeliveryHUSB238_Example/PowerDeliveryHUSB238_Example.ino new file mode 100644 index 0000000..84ba00b --- /dev/null +++ b/examples/PowerDeliveryHUSB238_Example/PowerDeliveryHUSB238_Example.ino @@ -0,0 +1,72 @@ +/** + * @file PowerDeliveryHUSB238.ino + * @author Lewis He (lewishe@outlook.com) + * @license MIT + * @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd + * @date 2024-07-24 + * + */ +#include + +PowerDeliveryHUSB238 pd; + + +#ifndef CONFIG_PMU_SDA +#define CONFIG_PMU_SDA 0 +#endif + +#ifndef CONFIG_PMU_SCL +#define CONFIG_PMU_SCL 1 +#endif + +const uint8_t i2c_sda = CONFIG_PMU_SDA; +const uint8_t i2c_scl = CONFIG_PMU_SCL; + + +void setup() +{ + Serial.begin(115200); + while (!Serial); + bool result = pd.init(Wire, i2c_sda, i2c_scl, HUSB238_SLAVE_ADDRESS); + if (result == false) { + while (1) { + Serial.println("USB Power Delivery controller not online..."); + delay(1000); + } + } +} + + +void loop() +{ + Serial.print("PD Voltage:"); Serial.print(pd.getPdVoltage()); Serial.print(" V"); + Serial.print(" Current: "); Serial.print(pd.getPdCurrent()); Serial.println(" A"); + + PowerDeliveryHUSB238::PD_Status status = pd.status(); + Serial.print("USB Power Delivery Status : "); + switch (status) { + case PowerDeliveryHUSB238::NO_RESPONSE: + Serial.println("no response"); + break; + case PowerDeliveryHUSB238::SUCCESS: + Serial.println("success"); + break; + case PowerDeliveryHUSB238::INVALID_CMD: + Serial.println("invalid command"); + break; + case PowerDeliveryHUSB238::NOT_SUPPORT: + Serial.println("not support"); + break; + case PowerDeliveryHUSB238::TRANSACTION_FAIL: + Serial.println("transaction failed"); + break; + default: + break; + } + delay(1000); +} + + + + + diff --git a/platformio.ini b/platformio.ini index 2ed85a9..aa8dae5 100644 --- a/platformio.ini +++ b/platformio.ini @@ -22,14 +22,15 @@ ; src_dir = examples/SY6970_Example ; src_dir = examples/SY6970_Watchdog_Example ; src_dir = examples/SY6970_Shutdown_Example - ; src_dir = examples/BQ25896_Example -src_dir = examples/BQ25896_Shutdown_Example +; src_dir = examples/BQ25896_Shutdown_Example +; src_dir = examples/PowerDeliveryHUSB238_Example +src_dir = examples/PowerDeliveryHUSB238_BleUart -default_envs=esp32s3 -; default_envs=esp32dev -; default_envs=nucleo_f411re -; default_envs = nrf52840 +; default_envs = esp32s3 +; default_envs = esp32dev +; default_envs = nucleo_f411re +default_envs = nrf52840 [env] lib_extra_dirs = . @@ -43,14 +44,14 @@ build_flags = ; -DCONFIG_PMU_SCL=2 ; T-EPD47 S3 - -DCONFIG_PMU_SDA=6 - -DCONFIG_PMU_SCL=5 - -DCONFIG_PMU_IRQ=15 - - ; -DCONFIG_PMU_SDA=3 - ; -DCONFIG_PMU_SCL=2 + ; -DCONFIG_PMU_SDA=6 + ; -DCONFIG_PMU_SCL=5 ; -DCONFIG_PMU_IRQ=15 - -UARDUINO_USB_CDC_ON_BOOT + + -DCONFIG_PMU_SDA=33 + -DCONFIG_PMU_SCL=35 + -DCONFIG_PMU_IRQ=15 + ; -UARDUINO_USB_CDC_ON_BOOT ; -DARDUINO_USB_CDC_ON_BOOT=1 -Wtype-limits -Wall @@ -66,15 +67,23 @@ platform = espressif32 framework = arduino board = esp32-s3-devkitc-1 - [env:nucleo_f411re] platform = ststm32 framework = arduino board = nucleo_f411re - upload_protocol = stlink [env:nrf52840] platform = nordicnrf52 -board = adafruit_feather_nrf52840 -framework = arduino \ No newline at end of file +board = nrf52840_dk_adafruit +framework = arduino +upload_protocol = nrfutil +monitor_speed = 115200 +; upload_protocol = nrfjprog +; upload_protocol = jlink +lib_deps = + Adafruit TinyUSB Library + Wire + SPI + +