|
@@ -1,6 +1,9 @@
|
|
|
-use stm32l4xx_hal::{prelude::_embedded_hal_blocking_i2c_Write};
|
|
|
+use field_offset::offset_of;
|
|
|
+use stm32l4xx_hal::prelude::_embedded_hal_blocking_i2c_Write;
|
|
|
use tock_registers::{
|
|
|
- interfaces::ReadWriteable, register_bitfields, register_structs, registers::InMemoryRegister,
|
|
|
+ interfaces::{ReadWriteable, Readable},
|
|
|
+ register_bitfields, register_structs,
|
|
|
+ registers::InMemoryRegister,
|
|
|
};
|
|
|
|
|
|
register_bitfields![
|
|
@@ -102,18 +105,31 @@ register_structs! {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-impl Default for Tusb322Registers {
|
|
|
- fn default() -> Self {
|
|
|
+impl Tusb322Registers {
|
|
|
+ const fn default() -> Self {
|
|
|
Self {
|
|
|
id: [0; 8],
|
|
|
- stat1: InMemoryRegister::new(0),
|
|
|
- stat2: InMemoryRegister::new(0),
|
|
|
- ctrl: InMemoryRegister::new(0),
|
|
|
+ stat1: InMemoryRegister::new(0x00),
|
|
|
+ stat2: InMemoryRegister::new(0x00),
|
|
|
+ ctrl: InMemoryRegister::new(0x00),
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // Program control registers
|
|
|
+ fn i2c_write_regs(&self, address: u8, i2c: &mut impl _embedded_hal_blocking_i2c_Write) {
|
|
|
+ let mut buffer: [u8; 4] = [0; 4];
|
|
|
+ buffer[0] = offset_of!(Tusb322Registers => stat1).get_byte_offset() as u8;
|
|
|
+ buffer[1] = self.stat1.get();
|
|
|
+ buffer[2] = self.stat2.get();
|
|
|
+ buffer[3] = self.ctrl.get();
|
|
|
+
|
|
|
+ match i2c.write(address, &buffer) {
|
|
|
+ Ok(_) => (),
|
|
|
+ Err(_) => panic!(),
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-pub fn init(i2c: &mut impl _embedded_hal_blocking_i2c_Write) {
|
|
|
+pub fn init(address: u8, i2c: &mut impl _embedded_hal_blocking_i2c_Write) {
|
|
|
let reg: Tusb322Registers = Tusb322Registers::default();
|
|
|
|
|
|
// Disable UFP accessory support (otherwise IC stays in DRP mode)
|
|
@@ -125,17 +141,10 @@ pub fn init(i2c: &mut impl _embedded_hal_blocking_i2c_Write) {
|
|
|
// For operation in UFP mode
|
|
|
reg.ctrl.modify(Control::MODE_SELECT::Ufp);
|
|
|
|
|
|
- const TUSB322_ADDR: u8 = 0x47;
|
|
|
+ reg.i2c_write_regs(address, i2c);
|
|
|
|
|
|
- // For some reason i2c.write().unwrap() doesn't work here as it complains
|
|
|
- // about an unsatisfied trait bound where the returned error value
|
|
|
- // (_embedded_hal_blocking_i2c_Write::Error) doesnt implement trait Debug.
|
|
|
- // Calling .unwrap() does work though if it's the caller that is doing so.
|
|
|
-
|
|
|
- match i2c.write(TUSB322_ADDR, &[0x00]) {
|
|
|
- Ok(_) => (),
|
|
|
- Err(_) => panic!()
|
|
|
- }
|
|
|
+ // Re-enable CC termination
|
|
|
+ reg.ctrl.modify(Control::DISABLE_TERM::CLEAR);
|
|
|
|
|
|
+ reg.i2c_write_regs(address, i2c);
|
|
|
}
|
|
|
-
|