Skip to content

Commit

Permalink
STUSB4500: read vbus_en_sink status
Browse files Browse the repository at this point in the history
  • Loading branch information
surban committed Mar 15, 2024
1 parent 5c86042 commit ce74fb5
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
50 changes: 50 additions & 0 deletions misc/Alternative-STUSB4500-NVMs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Alternative STUSB4500 NVMs:

// More tolerant.
#[rustfmt::skip]
const STUSB4500_NVM: Option<[u8; 40]> = Some([
0x00, 0x00, 0xB0, 0xAB, 0x00, 0x45, 0x00, 0x00,
0x20, 0x40, 0x9C, 0x1C, 0xFF, 0x01, 0x3C, 0xDF,
0x02, 0x40, 0x0F, 0x00, 0x32, 0x00, 0xFC, 0xF1,
0x00, 0x19, 0x57, 0xFF, 0xF5, 0x5F, 0xFF, 0x00,
0x00, 0x5A, 0x7C, 0x21, 0x43, 0x00, 0x40, 0xFB,
]);

#[rustfmt::skip]
const STUSB4500_NVM: Option<[u8; 40]> = Some([
0x00, 0x00, 0xB0, 0xAB, 0x00, 0x45, 0x00, 0x00,
0x10, 0x40, 0x9C, 0x1C, 0xFF, 0x01, 0x3C, 0xDF,
0x02, 0x40, 0x0F, 0x00, 0x32, 0x00, 0xFC, 0xF1,
0x00, 0x19, 0x56, 0xAF, 0xF5, 0x35, 0x5F, 0x00,
0x00, 0x4B, 0x90, 0x21, 0x43, 0x00, 0x40, 0xFB,
]);

// Only over 5V.
#[rustfmt::skip]
const STUSB4500_NVM: Option<[u8; 40]> = Some([
0x00, 0x00, 0xB0, 0xAB, 0x00, 0x45, 0x00, 0x00,
0x20, 0x40, 0x9C, 0x1C, 0xFF, 0x01, 0x3C, 0xDF,
0x02, 0x40, 0x0F, 0x00, 0x32, 0x00, 0xFC, 0xF1,
0x00, 0x19, 0x57, 0xFF, 0xF5, 0x5F, 0xFF, 0x00,
0x00, 0x5A, 0x7C, 0x21, 0x43, 0x00, 0x48, 0xFB,
]);

// Tolerant and no discharge.
#[rustfmt::skip]
const STUSB4500_NVM: Option<[u8; 40]> = Some([
0x00, 0x00, 0xB0, 0xAB, 0x00, 0x45, 0x00, 0x00,
0x20, 0x60, 0x9C, 0x1C, 0xFF, 0x01, 0x3C, 0xDF,
0x02, 0x40, 0x0F, 0x00, 0x32, 0x00, 0xFC, 0xF1,
0x00, 0x19, 0x57, 0xFF, 0xF5, 0x5F, 0xFF, 0x00,
0x00, 0x5A, 0x7C, 0x21, 0x43, 0x00, 0x40, 0xFB,
]);

// long discharge
#[rustfmt::skip]
const STUSB4500_NVM: Option<[u8; 40]> = Some([
0x00, 0x00, 0xB0, 0xAB, 0x00, 0x45, 0x00, 0x00,
0x20, 0x40, 0xFF, 0x1C, 0xFF, 0x01, 0x3C, 0xDF,
0x02, 0x40, 0x0F, 0x00, 0x32, 0x00, 0xFC, 0xF1,
0x00, 0x19, 0x57, 0xFF, 0xF5, 0x5F, 0xFF, 0x00,
0x00, 0x5A, 0x90, 0x21, 0x03, 0x00, 0x40, 0xFB,
]);
8 changes: 6 additions & 2 deletions openemc-firmware/src/supply/stusb4500/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,8 @@ where
/// Reads the monitoring status.
fn update_monitoring_status(&mut self, i2c: &mut I2C) -> Result<()> {
let buf = self.read(i2c, REG_MONITORING_STATUS_0, 2)?;
let monitoring_status = MonitoringStatus::parse(buf[0], buf[1]);
let buf2 = self.read(i2c, REG_VBUS_CTRL, 1)?;
let monitoring_status = MonitoringStatus::parse(buf[0], buf[1], buf2[0]);
if self.monitoring_status != monitoring_status {
defmt::info!("Monitoring status: {:?}", monitoring_status);
self.monitoring_status = monitoring_status;
Expand Down Expand Up @@ -804,6 +805,7 @@ const REG_CC_STATUS: u8 = 0x11;
const REG_CC_HW_FAULT_STATUS_0: u8 = 0x12;
const REG_TYPE_C_STATUS: u8 = 0x15;
const REG_PRT_STATUS: u8 = 0x16;
const REG_VBUS_CTRL: u8 = 0x27;
const REG_RX_BYTE_CNT: u8 = 0x30;
const REG_RX_HEADER: u8 = 0x31;
const REG_RX_DATA_OBJ: u8 = 0x33;
Expand Down Expand Up @@ -962,17 +964,19 @@ struct MonitoringStatus {
vbus_valid_sink: bool,
vbus_vsafe_0v: bool,
vbus_ready: bool,
vbus_en_sink: bool,
}

impl MonitoringStatus {
fn parse(monitoring_status_0: u8, monitoring_status_1: u8) -> Self {
fn parse(monitoring_status_0: u8, monitoring_status_1: u8, vbus_ctrl: u8) -> Self {
Self {
vbus_low: monitoring_status_0 & (1 << 4) != 0,
vbus_high: monitoring_status_0 & (1 << 5) != 0,
vconn_valid: monitoring_status_1 & (1 << 0) != 0,
vbus_valid_sink: monitoring_status_1 & (1 << 1) != 0,
vbus_vsafe_0v: monitoring_status_1 & (1 << 2) != 0,
vbus_ready: monitoring_status_1 & (1 << 3) != 0,
vbus_en_sink: vbus_ctrl & (1 << 1) != 0,
}
}
}
Expand Down

0 comments on commit ce74fb5

Please sign in to comment.