Skip to content

Commit e21e3a4

Browse files
committed
STUSB4500: fix NVM programming
The last NVM sector was erased but not programmed. This caused malfunctions.
1 parent 165bc4f commit e21e3a4

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

openemc-firmware/src/main.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -546,18 +546,31 @@ mod app {
546546
// Initialize power supplies.
547547
let stusb4500 = match ThisBoard::STUSB4500_I2C_ADDR {
548548
Some(addr) => {
549-
// Verify and possibily reprogram NVM.
549+
// Verify and possibily reprogram NVM of STUSB4500.
550+
let mut reprogrammed = false;
550551
if let Some(data) = ThisBoard::STUSB4500_NVM {
551552
let i2c2 = defmt::unwrap!(i2c2.as_mut());
552553
match StUsb4500Nvm::new(addr, i2c2).and_then(|mut nvm| nvm.ensure_nvm(&data)) {
553-
Ok(true) => defmt::info!("STUSB4500 NVM has been reprogrammed"),
554+
Ok(true) => {
555+
defmt::info!("STUSB4500 NVM has been reprogrammed");
556+
reprogrammed = true;
557+
}
554558
Ok(false) => defmt::info!("STUSB4500 NVM has been verified"),
555559
Err(err) => defmt::warn!("STUSB4500 NVM programming failed: {:?}", err),
556560
}
557561
}
558562

563+
// Initialize STUSB4500.
564+
let mut stusb4500 =
565+
StUsb4500::new(addr, &ThisBoard::USB_INITIAL_PDO, ThisBoard::USB_MAXIMUM_VOLTAGE);
566+
567+
if reprogrammed {
568+
defmt::info!("Forcing STUSB4500 pin reset");
569+
stusb4500.reset();
570+
}
571+
559572
defmt::unwrap!(stusb4500_periodic::spawn_after(1u64.secs()));
560-
Some(StUsb4500::new(addr, &ThisBoard::USB_INITIAL_PDO, ThisBoard::USB_MAXIMUM_VOLTAGE))
573+
Some(stusb4500)
561574
}
562575
None => None,
563576
};

openemc-firmware/src/supply/stusb4500/nvm.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,11 @@ where
4040
/// Mask of programmable bits within NVM.
4141
#[rustfmt::skip]
4242
pub const NVM_PROGRAMABLE: [u8; 40] = [
43-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
44-
0x30, 0x20, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
45-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46-
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00,
47-
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
43+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC0 - 0xC7
44+
0x30, 0x20, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xC8 - 0xCF
45+
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0xD0 - 0xD7
46+
0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, // 0xD8 - 0xDF
47+
0xC0, 0xFF, 0xFF, 0xFF, 0x6F, 0x00, 0x18, 0x00, // 0xE0 - 0xE7
4848
];
4949

5050
/// Read I2C register(s).
@@ -158,7 +158,7 @@ where
158158
defmt::debug!("reading NVM");
159159

160160
let mut buf = [0; 40];
161-
for sector in 0..4 {
161+
for sector in 0..=4 {
162162
buf[(sector * 8)..((sector + 1) * 8)].copy_from_slice(&self.read_nvm_sector(sector)?);
163163
}
164164

@@ -214,7 +214,7 @@ where
214214

215215
self.erase_nvm()?;
216216

217-
for sector in 0..4 {
217+
for sector in 0..=4 {
218218
self.write_nvm_sector(sector, &defmt::unwrap!(data[(sector * 8)..((sector + 1) * 8)].try_into()))?;
219219
}
220220

0 commit comments

Comments
 (0)