bare_metal_example/
bare_metal_example.rs

1//  _  _       _             _  _
2// | || |  ___| |_ _ __ ___ | || |
3// | || |_/ __| __| '_ ` _ \| || |_
4// |__   _\__ | |_| | | | | |__   _|
5//   |_| |___/\__|_|_|_| |_|  |_|
6//! # ehatrom — EEPROM HAT library for Raspberry Pi HATs
7//! - [Documentation (docs.rs)](https://docs.rs/ehatrom)
8//! - [GitHub](https://github.com/4stm4/ehatrom)
9//!
10// Example of using ehatrom in no_std environment
11// Note: This example can be compiled with std for demonstration,
12// but shows how to use the library in no_std context
13
14use ehatrom::{Eeprom, EepromHeader, GpioMapAtom, VendorInfoAtom};
15
16fn main() {
17    // This example demonstrates how to use ehatrom in a bare-metal environment
18    // without heap allocation
19
20    // Create EEPROM structure with static data
21    let vendor_info = VendorInfoAtom::new(
22        0x0001, // vendor_id
23        0x0002, // product_id
24        0x0001, // product_ver
25        "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], // All pins disabled
36    };
37
38    // Custom atoms with static data
39    static CUSTOM_DATA: &[u8] = b"Hello, Bare Metal!";
40
41    // In no_std environment, custom_atoms would be a static slice
42    #[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    // Calculate required buffer size
63    let buffer_size = eeprom.calculate_serialized_size();
64    println!("Required buffer size: {buffer_size} bytes");
65
66    // Serialize to fixed buffer (demonstrates no heap allocation approach)
67    let mut buffer = vec![0u8; 512]; // In real no_std, this would be a static array
68
69    #[cfg(feature = "alloc")]
70    {
71        // Standard allocation-based serialization
72        let serialized = eeprom.serialize();
73        println!("Serialized {} bytes using Vec", serialized.len());
74    }
75
76    // Demonstrate no-allocation serialization (available in both std and no_std)
77    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}