pub const CMD_SPI_SET_CFG: u8 = 0xC0; pub const CMD_SPI_CONTROL: u8 = 0xC1; pub const CMD_SPI_RD_WR: u8 = 0xC2; pub const CMD_SPI_BLCK_RD: u8 = 0xC3; pub const CMD_SPI_BLCK_WR: u8 = 0xC4; pub const CMD_JTAG_INIT: u8 = 0xD0;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SpiSpeed {
Speed60M = 0,
Speed30M = 1,
Speed15M = 2,
Speed7_5M = 3,
Speed3_75M = 4,
#[default]
Medium = 5,
Speed937K = 6,
Speed468K = 7,
}
impl SpiSpeed {
pub fn from_u8(value: u8) -> Self {
match value {
0 => Self::Speed60M,
1 => Self::Speed30M,
2 => Self::Speed15M,
3 => Self::Speed7_5M,
4 => Self::Speed3_75M,
5 => Self::Medium,
6 => Self::Speed937K,
7 => Self::Speed468K,
_ => Self::default(),
}
}
pub fn description(&self) -> &'static str {
match self {
Self::Speed60M => "60 MHz",
Self::Speed30M => "30 MHz",
Self::Speed15M => "15 MHz",
Self::Speed7_5M => "7.5 MHz",
Self::Speed3_75M => "3.75 MHz",
Self::Medium => "1.875 MHz",
Self::Speed937K => "937.5 KHz",
Self::Speed468K => "468.75 KHz",
}
}
}
pub fn build_set_cfg_cmd(speed: SpiSpeed) -> Vec<u8> {
let mut cfg = vec![0u8; 26];
cfg[6] = 0x00;
cfg[8] = 0x00;
cfg[12] = (speed as u8) << 3;
cfg[14] = 0x00;
let len = cfg.len();
let mut packet = Vec::with_capacity(3 + len);
packet.push(CMD_SPI_SET_CFG);
packet.push((len & 0xFF) as u8);
packet.push(((len >> 8) & 0xFF) as u8);
packet.extend_from_slice(&cfg);
packet
}
pub fn build_cs_cmd(active: bool) -> Vec<u8> {
let mut payload = vec![0u8; 10];
let val_bit = if active { 0 } else { 1 };
payload[0] = 0x80 | (val_bit << 6);
let len = payload.len();
let mut packet = Vec::with_capacity(3 + len);
packet.push(CMD_SPI_CONTROL);
packet.push((len & 0xFF) as u8);
packet.push(((len >> 8) & 0xFF) as u8);
packet.extend_from_slice(&payload);
packet
}
pub fn build_spi_transfer_cmd(tx: &[u8]) -> Vec<u8> {
let len = tx.len();
let mut packet = Vec::with_capacity(3 + len);
packet.push(CMD_SPI_RD_WR);
packet.push((len & 0xFF) as u8);
packet.push(((len >> 8) & 0xFF) as u8);
packet.extend_from_slice(tx);
packet
}
pub fn build_handshake_cmd() -> Vec<u8> {
vec![
CMD_JTAG_INIT,
0x06,
0x00,
0x09,
0x00,
0x00,
0x00,
0x00,
0x00,
]
}
pub const MAX_PACKET_SIZE_STANDARD: usize = 510;
pub const MAX_PACKET_SIZE_LARGER: usize = 51184;