bare_metal_example/
bare_metal_example.rs1use ehatrom::{Eeprom, EepromHeader, GpioMapAtom, VendorInfoAtom};
15
16fn main() {
17 let vendor_info = VendorInfoAtom::new(
22 0x0001, 0x0002, 0x0001, "Acme Corp",
26 "Test HAT",
27 [
28 0x12, 0x34, 0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
29 0x77, 0x88,
30 ],
31 );
32
33 let gpio_map = GpioMapAtom {
34 flags: 0x0001,
35 pins: [0; 28], };
37
38 static CUSTOM_DATA: &[u8] = b"Hello, Bare Metal!";
40
41 #[cfg(not(feature = "alloc"))]
43 static CUSTOM_ATOMS: &[(u8, &[u8])] = &[(0x80, CUSTOM_DATA)];
44
45 #[cfg(feature = "alloc")]
46 let custom_atoms = vec![(0x80u8, CUSTOM_DATA.to_vec())];
47
48 let mut eeprom = Eeprom {
49 header: EepromHeader::new(),
50 vendor_info,
51 gpio_map_bank0: gpio_map,
52 dt_blob: None,
53 gpio_map_bank1: None,
54 #[cfg(feature = "alloc")]
55 custom_atoms,
56 #[cfg(not(feature = "alloc"))]
57 custom_atoms: CUSTOM_ATOMS,
58 };
59
60 eeprom.update_header();
61
62 let buffer_size = eeprom.calculate_serialized_size();
64 println!("Required buffer size: {buffer_size} bytes");
65
66 let mut buffer = vec![0u8; 512]; #[cfg(feature = "alloc")]
70 {
71 let serialized = eeprom.serialize();
73 println!("Serialized {} bytes using Vec", serialized.len());
74 }
75
76 let mut offset = 0;
78 match eeprom.serialize_to_buffer(&mut buffer, &mut offset) {
79 Ok(()) => {
80 println!("Successfully serialized {offset} bytes to buffer");
81 println!("First 16 bytes: {:02X?}", &buffer[..16.min(offset)]);
82 }
83 Err(e) => {
84 println!("Serialization failed: {e:?}");
85 }
86 }
87
88 println!("EEPROM structure:\n{eeprom}");
89}