spi_flash/
id.rs

1/// Store the ID read off an SPI flash memory.
2///
3/// The manufacturer ID and (long, 16-bit) device ID are read using the 0x9F command,
4/// and the number of 0x7F continuation code bytes present before the manufacturer ID
5/// is stored as `manufacturer_bank`.
6///
7/// The 64-bit unique ID is read using the 0x4B command.
8#[derive(Copy, Clone, Debug)]
9pub struct FlashID {
10    pub manufacturer_bank: u8,
11    pub manufacturer_id: u8,
12    pub device_id_long: u16,
13    pub device_id_short: u8,
14    pub unique_id: u64,
15}
16
17impl FlashID {
18    /// Look up a manufacturer name from the JEDEC ID.
19    #[cfg(feature = "std")]
20    pub fn manufacturer_name(&self) -> Option<&'static str> {
21        let (bank, id) = (self.manufacturer_bank, self.manufacturer_id & 0x7F);
22        match jep106::JEP106Code::new(bank, id).get() {
23            // Winbond acquired NEXCOM and so the ID 0xEF is commonly used for Winbond memory.
24            Some(mfn) if mfn == "NEXCOM" => Some("Winbond/NEXCOM"),
25            // GigaDevice flash doesn't use a continuation code, so 0xC8 appears as Apple Computer.
26            Some(mfn) if mfn == "Apple Computer" => Some("Apple Computer/GigaDevice Semiconductor"),
27            Some(mfn) => Some(mfn),
28            None => None,
29        }
30    }
31}
32
33#[cfg(feature = "std")]
34impl core::fmt::Display for FlashID {
35    fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
36        let mfn = match self.manufacturer_name() {
37            Some(mfn) => alloc::format!(" ({})", mfn),
38            None => "".to_string(),
39        };
40        let unique_id = match self.unique_id {
41            0x0000_0000_0000_0000 | 0xFFFF_FFFF_FFFF_FFFF => "".to_string(),
42            id => alloc::format!(", Unique ID: {:016X}", id),
43        };
44        write!(
45            f,
46            "Manufacturer 0x{:02X}{}, Device 0x{:02X}/0x{:04X}{}",
47            self.manufacturer_id, mfn, self.device_id_short, self.device_id_long, unique_id
48        )
49    }
50}