Skip to content

Commit

Permalink
Add get_port_status function and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertZ2011 committed Jan 7, 2025
1 parent af65c4d commit 64376b9
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 0 deletions.
97 changes: 97 additions & 0 deletions device.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -296,3 +296,100 @@ IntClearBus1:
reset_value: 0x0000000000000000000000
access: RW

Status:
type: register
address: 0x1A
access: RO
size_bits: 40
reset_value: 0x0000000000
description: Port status
fields:
PlugPresent:
base: bool
start: 0
description: Plug present
ConnectionState:
base: uint
start: 1
end: 4
description: Connection state
conversion:
name: PlugMode
NotConnected: 0x0
Disabled: 0x1
Audio: 0x2
Debug: 0x3
# No connection, but Ra detected
RaDetected: 0x4
Reserved: 0x5
# Connection, but no Ra detected
ConnectedNoRa: 0x6
Connected: 0x7
PlugOrientation:
base: bool
start: 4
description: Connector oreintation, 0 for normal, 1 for flipped
PortRole:
base: bool
start: 5
description: PD role, 0 for sink, 1 for source
DataRole:
base: bool
start: 6
description: Data role, 0 for UFP, 1 for DFP
ErpMode:
base: bool
start: 7
description: Is EPR mode active
VbusStatus:
base: uint
start: 20
end: 22
description: Vbus status
conversion:
name: VbusMode
AtVsafe0: 0x0
Atvsafe5: 0x1
Normal: 0x2
Other: 0x3
UsbHost:
base: uint
start: 22
end: 24
description: USB host mode
conversion:
name: UsbHostMode
NoHost: 0x0
AttachedNoData: 0x1
AttachedNoPd: 0x2
HostPresent: 0x3
Legacy:
base: uint
start: 24
end: 26
description: Legacy mode stotus
conversion:
name: LegacyMode
NoLegacy: 0x0
LegacySink: 0x01
LegacySource: 0x2
LegacySinkDeadBattery: 0x03
BistInProgress:
base: bool
start: 27
description: If a BIST is in progress
SocAckTimeout:
base: bool
start: 30
description: Set when the SOC acknolwedgement has timed out
AmStatus:
base: uint
start: 32
end: 34
description: Alternate mode entry status
conversion:
name: AmStatus
NoneAttempted: 0x0
EntrySuccessful: 0x1
EntryFailed: 0x2
PartialSuccess: 0x3
35 changes: 35 additions & 0 deletions src/asynchronous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ impl<B: I2c> Tps6699x<B> {

Ok(flags)
}

/// Get port status
pub async fn get_port_status(&mut self, port: PortId) -> Result<registers::field_sets::Status, Error<B::Error>> {
self.borrow_port(port)?.into_registers().status().read_async().await
}
}

#[cfg(test)]
Expand Down Expand Up @@ -325,4 +330,34 @@ mod test {
test_clear_interrupt_port(&mut tps6699x, PORT1, PORT1_ADDR1).await;
}

async fn test_read_status_ports(tps6699x: &mut Tps6699x<Mock>, port: PortId, expected_addr: u8) {
use registers::field_sets::Status;

let mut transactions = Vec::new();
// Read status register
transactions.extend(create_register_read(expected_addr, 0x1A, Status::new_zero()).into_iter());
tps6699x.bus.update_expectations(&transactions);

let status = tps6699x.get_port_status(port).await.unwrap();
assert_eq!(status, Status::new_zero());
tps6699x.bus.done();
}

#[tokio::test]
async fn test_read_status_ports_0() {
let mock = Mock::new(&[]);
let mut tps6699x: Tps6699x<Mock> = Tps6699x::new(mock, ADDR0);

test_read_status_ports(&mut tps6699x, PORT0, PORT0_ADDR0).await;
test_read_status_ports(&mut tps6699x, PORT1, PORT1_ADDR0).await;
}

#[tokio::test]
async fn test_read_status_ports_1() {
let mock = Mock::new(&[]);
let mut tps6699x: Tps6699x<Mock> = Tps6699x::new(mock, ADDR1);

test_read_status_ports(&mut tps6699x, PORT0, PORT0_ADDR1).await;
test_read_status_ports(&mut tps6699x, PORT1, PORT1_ADDR1).await;
}
}

0 comments on commit 64376b9

Please sign in to comment.