2020-09-17 11:21:24 +08:00
|
|
|
use embedded_hal::{
|
2020-09-25 14:24:33 +08:00
|
|
|
digital::v2::{OutputPin, InputPin},
|
2020-09-17 11:21:24 +08:00
|
|
|
blocking::spi::Transfer,
|
2020-09-18 12:23:28 +08:00
|
|
|
blocking::delay::DelayUs,
|
2020-09-17 11:21:24 +08:00
|
|
|
};
|
|
|
|
|
2020-09-17 11:48:32 +08:00
|
|
|
#[derive(Debug)]
|
2020-09-17 11:21:24 +08:00
|
|
|
pub enum FPGAFlashError {
|
|
|
|
SPICommunicationError,
|
|
|
|
NegotiationError,
|
|
|
|
ResetStatusError,
|
|
|
|
}
|
|
|
|
|
2020-09-24 17:15:07 +08:00
|
|
|
const DATA: &'static [u8] = include_bytes!("../build/top.bin");
|
|
|
|
|
2020-09-17 11:21:24 +08:00
|
|
|
// A public method to flash iCE40 FPGA on Humpback
|
|
|
|
pub fn flash_ice40_fpga<SPI: Transfer<u8>,
|
|
|
|
SS: OutputPin,
|
|
|
|
RST: OutputPin,
|
|
|
|
DELAY: DelayUs<u32>,
|
|
|
|
DONE: InputPin>
|
2020-09-24 17:15:07 +08:00
|
|
|
(mut spi: SPI, mut ss: SS, mut creset: RST, cdone: DONE, mut delay: DELAY) -> Result<(), FPGAFlashError>
|
2020-09-17 11:21:24 +08:00
|
|
|
{
|
2020-09-25 14:24:33 +08:00
|
|
|
// Data buffer setup
|
|
|
|
let mut dummy_byte :[u8; 1] = [0x00];
|
|
|
|
let mut dummy_13_bytes :[u8; 13] = [0x00; 13];
|
2020-09-17 11:21:24 +08:00
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Drive CRESET_B low
|
2020-09-17 11:21:24 +08:00
|
|
|
creset.set_low()
|
|
|
|
.map_err(|_| FPGAFlashError::NegotiationError)?;
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Drive SPI_SS_B low
|
2020-09-17 11:21:24 +08:00
|
|
|
ss.set_low()
|
|
|
|
.map_err(|_| FPGAFlashError::NegotiationError)?;
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Wait at least 200ns
|
|
|
|
delay.delay_us(1_u32);
|
2020-09-17 11:21:24 +08:00
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Drive CRESET_B high
|
2020-09-17 11:21:24 +08:00
|
|
|
creset.set_high()
|
|
|
|
.map_err(|_| FPGAFlashError::NegotiationError)?;
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Wait at least another 1200us to clear internal config memory
|
|
|
|
delay.delay_us(1200_u32);
|
2020-09-17 11:21:24 +08:00
|
|
|
|
|
|
|
// Before data transmission starts, check if C_DONE is truly low
|
|
|
|
// If C_DONE is high, the FPGA reset procedure is unsuccessful
|
2020-09-25 14:24:33 +08:00
|
|
|
match cdone.is_low() {
|
2020-09-17 11:21:24 +08:00
|
|
|
Ok(true) => {},
|
|
|
|
_ => return Err(FPGAFlashError::ResetStatusError),
|
|
|
|
};
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Set SPI_SS_B high
|
2020-09-17 11:21:24 +08:00
|
|
|
ss.set_high()
|
|
|
|
.map_err(|_| FPGAFlashError::NegotiationError)?;
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Send 8 dummy clock, effectively 1 byte of 0x00
|
2020-09-17 11:21:24 +08:00
|
|
|
spi.transfer(&mut dummy_byte)
|
|
|
|
.map_err(|_| FPGAFlashError::SPICommunicationError)?;
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Drive SPI_SS_B low
|
2020-09-17 11:21:24 +08:00
|
|
|
ss.set_low()
|
|
|
|
.map_err(|_| FPGAFlashError::NegotiationError)?;
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Send the whole image without interruption
|
|
|
|
for byte in DATA.into_iter() {
|
2020-09-17 11:21:24 +08:00
|
|
|
let mut single_byte_slice = [*byte];
|
|
|
|
spi.transfer(&mut single_byte_slice)
|
|
|
|
.map_err(|_| FPGAFlashError::SPICommunicationError)?;
|
2020-09-25 14:24:33 +08:00
|
|
|
}
|
2020-09-17 11:21:24 +08:00
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Drive SPI_SS_B high
|
2020-09-17 11:21:24 +08:00
|
|
|
ss.set_high()
|
|
|
|
.map_err(|_| FPGAFlashError::NegotiationError)?;
|
|
|
|
|
2020-09-25 14:24:33 +08:00
|
|
|
// Send at another 100 dummy clocks (choosing 13 bytes)
|
2020-09-17 11:21:24 +08:00
|
|
|
spi.transfer(&mut dummy_13_bytes)
|
|
|
|
.map_err(|_| FPGAFlashError::SPICommunicationError)?;
|
|
|
|
|
|
|
|
// Check the CDONE output from FPGA
|
|
|
|
// CDONE needs to be high
|
2020-09-25 14:24:33 +08:00
|
|
|
match cdone.is_high() {
|
2020-09-17 11:21:24 +08:00
|
|
|
Ok(true) => {},
|
|
|
|
_ => return Err(FPGAFlashError::ResetStatusError),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Send at least another 49 clock cycles to activate IO pins (choosing same 13 bytes)
|
|
|
|
spi.transfer(&mut dummy_13_bytes).map_err(|_| FPGAFlashError::SPICommunicationError)?;
|
|
|
|
Ok(())
|
|
|
|
|
|
|
|
}
|