Skip to content

Commit

Permalink
Added HUSB238 examples
Browse files Browse the repository at this point in the history
  • Loading branch information
lewisxhe committed Jul 24, 2024
1 parent 8b796b3 commit f4f55d9
Show file tree
Hide file tree
Showing 3 changed files with 276 additions and 17 deletions.
178 changes: 178 additions & 0 deletions examples/PowerDeliveryHUSB238_BleUart/PowerDeliveryHUSB238_BleUart.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* @file PowerDeliveryHUSB238_BleUart.ino
* @author Lewis He ([email protected])
* @license MIT
* @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2024-07-24
*
*/

#include <XPowersLib.h>

#ifdef ARDUINO_ARCH_NRF52

#include <bluefruit.h>
#include <Adafruit_LittleFS.h>

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



Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/**
* @file PowerDeliveryHUSB238.ino
* @author Lewis He ([email protected])
* @license MIT
* @copyright Copyright (c) 2024 Shenzhen Xin Yuan Electronic Technology Co., Ltd
* @date 2024-07-24
*
*/
#include <XPowersLib.h>

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);
}





43 changes: 26 additions & 17 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 = .
Expand All @@ -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
Expand All @@ -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
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


0 comments on commit f4f55d9

Please sign in to comment.