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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//! High level page buffer API

use crate::{spm, Address, DataPage};
use core::iter;

/// Representation of the spm page buffer.
///
/// The page buffer is a special area of memory which is write-only, and only writable using the `spm` instruction.
/// Setting a value in the buffer does not write to the program memory - that only happens when [`PageBuffer::store`] is called
///
/// # Example
/// ```no_run
/// use avr_boot::PageBuffer;
///
/// let address:u16 = 0x1000;
/// let buff = PageBuffer::new(address);
/// for w in buff.iter() {
///     w.set(0xabcd);
/// }
/// buff.store();
/// ```
///
/// A whole page is written in one go, so if you only want to change part of a page, you need to make sure you have
/// loaded the rest of the page into the buffer first.
///
/// There is only one physical buffer in the system, so you should make sure only one of these structs ever
/// exists at any time. This rule is not enforced.
///
/// The page address will be aligned downwards to the nearest starting page address
///
pub struct PageBuffer {
    address: Address,
}

impl PageBuffer {
    /// The buffer length in words, the value will change depending on the MCU compilation target
    pub const LENGTH: usize = crate::SPM_PAGESIZE_WORDS;

    /// Create a new PageBuffer with the given address.
    ///
    /// # Example
    /// ```rust
    /// use avr_boot::PageBuffer;
    ///
    /// let buff = PageBuffer::new(0x101fu16);
    /// assert_eq!(0x1000u16, buff.address().into());
    /// ```
    /// The page address will be aligned downwards to the nearest starting page address
    pub fn new(address: impl Into<Address>) -> PageBuffer {
        PageBuffer {
            address: address.into().into_page_aligned(),
        }
    }

    /// Get the base page address to be operated on
    ///
    /// # Example
    /// ```rust
    /// use avr_boot::{PageBuffer, Address};
    ///
    /// let buff = PageBuffer::new(0x1000u16);
    /// assert_eq!(Address::from(0x1000u16), buff.address());
    /// ```
    /// The page address will be aligned downwards to the nearest starting page address
    pub fn address(&self) -> Address {
        self.address
    }

    /// Fill the buffer from a slice
    ///
    /// # Example
    ///
    /// ```no_run
    /// use avr_boot::{DataPage, PageBuffer};
    ///
    /// let address: u16 = 0x1000;
    /// let data = DataPage(core::array::from_fn(|_| 0x69));
    /// let buff = PageBuffer::new(address);
    /// buff.copy_from(&data);
    /// buff.store();
    /// ```
    ///
    /// # Example
    ///
    /// ```no_run
    /// use avr_boot::PageBuffer;
    ///
    /// let address: u16 = 0x1000;
    /// let data = [0xff; avr_boot::SPM_PAGESIZE_BYTES];
    /// let buff = PageBuffer::new(address);
    /// buff.copy_from(&data);
    /// buff.store();
    /// ```
    pub fn copy_from<'a>(&self, data: impl Into<&'a DataPage>) {
        spm::copy_to_buffer(data);
    }

    /// Fill the buffer from a slice and store it immediately
    ///
    /// # Example
    ///
    /// ```no_run
    /// use avr_boot::{DataPage, PageBuffer};
    ///
    /// let address: u16 = 0x1000;
    /// let data = DataPage(core::array::from_fn(|_| 0x69));
    /// let buff = PageBuffer::new(address);
    /// buff.copy_from(&data);
    /// buff.store();
    /// ```
    ///
    pub fn store_from<'a>(self, data: impl Into<&'a DataPage>) {
        spm::erase_page(self.address);
        spm::copy_to_buffer(data);
        spm::write_page(self.address);
    }

    /// Fill the buffer by repeatedly calling the callback function
    ///
    /// # Example
    ///
    /// ```no_run
    /// use avr_boot::PageBuffer;
    ///
    /// let address:u16 = 0x1000;
    /// let buff = PageBuffer::new(address);
    /// buff.fill_from_fn(|| Some(0x1234));
    /// buff.store();
    /// ```
    pub fn fill_from_fn<F>(&self, f: F)
    where
        F: FnMut() -> Option<u16>,
    {
        self.fill_from_iter(iter::from_fn(f));
    }

    /// Fill the buffer by repeatedly polling an iterator.  
    ///
    /// # Example
    ///
    /// ```no_run
    /// use avr_boot::PageBuffer;
    /// use core::iter;
    ///
    /// let page_address:u16 = 0x1000;
    /// let buff = PageBuffer::new(page_address);
    /// buff.fill_from_iter(iter::repeat(0x69));
    /// buff.store();
    /// ```
    pub fn fill_from_iter(&self, i: impl IntoIterator<Item = u16>) {
        for (word, value) in self.iter().zip(i.into_iter()) {
            word.set(value);
        }
    }

    /// Erase the page from program memory, then write the contents of the buffer to it
    pub fn store(self) {
        spm::erase_page(self.address);
        spm::write_page(self.address);
    }

    /// Iterate the buffer as writable word cells
    ///
    /// # Example
    ///
    /// ```no_run
    /// use avr_boot::PageBuffer;
    ///
    /// let address: u16 = 0x1000;
    /// let buff = PageBuffer::new(address);
    /// for w in buff.iter() {
    ///     w.set(0x69);
    /// }
    /// buff.store();
    /// ```
    pub fn iter(&self) -> impl Iterator<Item = BufferCell> {
        CellIter { offset: 0 }
    }
}

impl Drop for PageBuffer {
    // Wait for any current spm operation to complete and
    // re-enable the rww section (if there is one)
    fn drop(&mut self) {
        spm::rww_enable();
    }
}

struct CellIter {
    offset: u8,
}

impl Iterator for CellIter {
    type Item = BufferCell;

    fn next(&mut self) -> Option<Self::Item> {
        let current = self.offset;
        if current >= crate::SPM_PAGESIZE_BYTES as u8 {
            None
        } else {
            self.offset += 2;
            Some(BufferCell { offset: current })
        }
    }
}

/// A single 16 bit word in the page buffer. Write only.
pub struct BufferCell {
    offset: u8,
}

impl BufferCell {
    /// Set the value of the word in the spm buffer
    pub fn set(&self, w: u16) {
        spm::fill_page(self.offset, w);
    }
}