1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! EEPROM
//!
//! # Example
//!
//! Complete example source code can be found in the repository:
//! [`atmega2560-eeprom.rs`](https://github.com/Rahix/avr-hal/blob/main/examples/atmega2560/src/bin/atmega2560-eeprom.rs)
//!
//! ```
//! const BOOT_COUNT_OFFSET: u16 = 0;
//!
//! let dp = atmega_hal::Peripherals::take().unwrap();
//! let mut eeprom = Eeprom::new(dp.EEPROM);
//!
//! let mut boot_count = eeprom.read_byte(BOOT_COUNT_OFFSET);
//! boot_count = boot_count.wrapping_add(1);
//! eeprom.write_byte(BOOT_COUNT_OFFSET, boot_count);
//!
//! ufmt::uwriteln!(&mut serial, "Boot count: {}", boot_count).unwrap();
//! ```

pub use avr_hal_generic::eeprom::{EepromOps, OutOfBoundsError};

pub type Eeprom = avr_hal_generic::eeprom::Eeprom<crate::Atmega, crate::pac::EEPROM>;

///////////////////////////////////////////////////////////
#[cfg(feature = "atmega48p")]
avr_hal_generic::impl_eeprom_atmega! {
    hal: crate::Atmega,
    peripheral: crate::pac::EEPROM,
    capacity: 256,
    addr_width: u8,
    set_address: |peripheral, address| {
        peripheral.eearl.write(|w| w.bits(address));
    },
}

#[cfg(any(feature = "atmega168", feature = "atmega164pa"))]
avr_hal_generic::impl_eeprom_atmega! {
    hal: crate::Atmega,
    peripheral: crate::pac::EEPROM,
    capacity: 512,
    addr_width: u16,
    set_address: |peripheral, address| {
        peripheral.eear.write(|w| w.bits(address));
    },
}

#[cfg(any(
    feature = "atmega328pb",
    feature = "atmega328p",
    feature = "atmega32u4"
))]
avr_hal_generic::impl_eeprom_atmega! {
    hal: crate::Atmega,
    peripheral: crate::pac::EEPROM,
    capacity: 1024,
    addr_width: u16,
    set_address: |peripheral, address| {
        peripheral.eear.write(|w| w.bits(address));
    },
}

#[cfg(any(
    feature = "atmega2560",
    feature = "atmega1280",
    feature = "atmega1284p"
))]
avr_hal_generic::impl_eeprom_atmega! {
    hal: crate::Atmega,
    peripheral: crate::pac::EEPROM,
    capacity: 4096,
    addr_width: u16,
    set_address: |peripheral, address| {
        peripheral.eear.write(|w| w.bits(address));
    },
}

#[cfg(any(feature = "atmega8"))]
avr_hal_generic::impl_eeprom_atmega_old! {
    hal: crate::Atmega,
    peripheral: crate::pac::EEPROM,
    capacity: 512,
    addr_width: u16,
    set_address: |peripheral, address| {
        peripheral.eear.write(|w| w.bits(address));
    },
}

#[cfg(any(feature = "atmega32a"))]
avr_hal_generic::impl_eeprom_atmega_old! {
    hal: crate::Atmega,
    peripheral: crate::pac::EEPROM,
    capacity: 1024,
    addr_width: u16,
    set_address: |peripheral, address| {
        peripheral.eear.write(|w| w.bits(address));
    },
}

#[cfg(any(feature = "atmega128a",))]
avr_hal_generic::impl_eeprom_atmega_old! {
    hal: crate::Atmega,
    peripheral: crate::pac::EEPROM,
    capacity: 4096,
    addr_width: u16,
    set_address: |peripheral, address| {
        peripheral.eear.write(|w| w.bits(address));
    },
}