Skip to content

Commit da46ecb

Browse files
BMC SPI now runs at 2 MHz and is a bit more robust.
1 parent 4f78d0a commit da46ecb

1 file changed

Lines changed: 16 additions & 17 deletions

File tree

src/main.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,9 @@ impl Hardware {
370370
/// Give the device 2us (2 clocks @ 1 MHz) before we take away CS.
371371
const CS_BUS_HOLD_CPU_CLOCKS: u32 = 2000 / Self::NS_PER_CLOCK_CYCLE;
372372

373+
/// Give the device 10us when we do a retry.
374+
const SPI_RETRY_CPU_CLOCKS: u32 = 10_000 / Self::NS_PER_CLOCK_CYCLE;
375+
373376
/// Data Direction Register A on the MCP23S17
374377
const MCP23S17_DDRA: u8 = 0x00;
375378

@@ -534,7 +537,7 @@ impl Hardware {
534537
spi_bus: hal::Spi::new(spi).init(
535538
resets,
536539
clocks.peripheral_clock.freq(),
537-
1_000_000.Hz(),
540+
2_000_000.Hz(),
538541
&embedded_hal::spi::MODE_0,
539542
),
540543
delay,
@@ -566,7 +569,7 @@ impl Hardware {
566569
self.pins.nspi_cs_io.set_high().unwrap();
567570
}
568571

569-
/// Write to a registers on the MCP23S17 I/O chip.
572+
/// Write to a register on the MCP23S17 I/O chip.
570573
///
571574
/// * `register` - the address of the register to write to
572575
/// * `data` - the value to write
@@ -580,9 +583,9 @@ impl Hardware {
580583
});
581584
}
582585

583-
/// Write to a registers on the MCP23S17 I/O chip.
586+
/// Read from a register on the MCP23S17 I/O chip.
584587
///
585-
/// * `register` - the address of the register to write to
588+
/// * `register` - the address of the register to read from
586589
fn io_chip_read(&mut self, register: u8) -> u8 {
587590
// Inter-packet delay
588591
cortex_m::asm::delay(Self::CS_IO_DISABLE_CPU_CLOCKS);
@@ -682,16 +685,6 @@ impl Hardware {
682685
self.io_chip_write(Self::MCP23S17_GPPUB, 0xFF);
683686
}
684687

685-
fn dump_io_chip(&mut self) {
686-
// Inter-packet delay
687-
cortex_m::asm::delay(Self::CS_IO_DISABLE_CPU_CLOCKS);
688-
689-
// Dump registers
690-
for reg in 0x00..=0x15 {
691-
let data = self.io_chip_read(reg);
692-
}
693-
}
694-
695688
/// Read the BMC firmware version string.
696689
///
697690
/// You get 32 bytes of probably UTF-8 data.
@@ -705,7 +698,7 @@ impl Hardware {
705698
defmt::info!("buffer: {=[u8]:x}", buffer);
706699
let mut result = &buffer[..];
707700
let mut latency = 0;
708-
while result.len() > 0 && result[0] == 0xFF {
701+
while result.len() > 0 && ((result[0] == 0xFF) || (result[0] == 0x00)) {
709702
latency += 1;
710703
result = &result[1..];
711704
}
@@ -748,7 +741,7 @@ impl Hardware {
748741
fn bmc_read_ps2_keyboard_fifo(&mut self, out_buffer: &mut [u8; 8]) -> Result<usize, ()> {
749742
let req = neotron_bmc_protocol::Request::new_read(USE_ALT.get(), 0x40, 8);
750743
for _retry in 0..4 {
751-
let mut buffer = [0xFF; 42];
744+
let mut buffer = [0xFF; 32];
752745
buffer[0..=3].copy_from_slice(&req.as_bytes());
753746
self.with_bus_cs(0, |spi| {
754747
spi.transfer(&mut buffer).unwrap();
@@ -793,8 +786,14 @@ impl Hardware {
793786
);
794787
}
795788
}
789+
} else {
790+
defmt::warn!("Short packet!?");
796791
}
792+
793+
// Wait a bit before we try again
794+
cortex_m::asm::delay(Self::SPI_RETRY_CPU_CLOCKS);
797795
}
796+
// Ran out of retries
798797
panic!("KB retry timeout");
799798
}
800799
}
@@ -1521,7 +1520,7 @@ extern "C" fn block_dev_eject(dev_id: u8) -> common::Result<()> {
15211520
/// Sleep the CPU until the next interrupt.
15221521
extern "C" fn power_idle() {
15231522
// cortex_m::asm::wfe();
1524-
cortex_m::asm::delay(10_000_000);
1523+
cortex_m::asm::delay(1_000_000);
15251524
}
15261525

15271526
/// TODO: Get the monotonic run-time of the system from SysTick.

0 commit comments

Comments
 (0)