forked from alexforencich/xboot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
eeprom_driver.c
346 lines (270 loc) · 9.82 KB
/
eeprom_driver.c
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/************************************************************************/
/* XMEGA EEPROM Driver */
/* */
/* eeprom.c */
/* */
/* Alex Forencich <alex@alexforencich.com> */
/* */
/* Copyright (c) 2011 Alex Forencich */
/* */
/* Permission is hereby granted, free of charge, to any person */
/* obtaining a copy of this software and associated documentation */
/* files(the "Software"), to deal in the Software without restriction, */
/* including without limitation the rights to use, copy, modify, merge, */
/* publish, distribute, sublicense, and/or sell copies of the Software, */
/* and to permit persons to whom the Software is furnished to do so, */
/* subject to the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND */
/* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS */
/* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN */
/* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN */
/* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
/* SOFTWARE. */
/* */
/************************************************************************/
#include "eeprom_driver.h"
#include "string.h"
#ifdef __AVR_XMEGA__
#ifdef USE_AVR1008_EEPROM
#include <avr/sleep.h>
#endif // USE_AVR1008_EEPROM
#else
#include <avr/boot.h>
#include <avr/io.h>
#endif // __AVR_XMEGA__
#ifdef __AVR_XMEGA__
// NVM call
static inline void NVM_EXEC(void)
{
void *z = (void *)&NVM_CTRLA;
__asm__ volatile("out %[ccp], %[ioreg]" "\n\t"
"st z, %[cmdex]"
:
: [ccp] "I" (_SFR_IO_ADDR(CCP)),
[ioreg] "d" (CCP_IOREG_gc),
[cmdex] "r" (NVM_CMDEX_bm),
[z] "z" (z)
);
}
#ifdef USE_AVR1008_EEPROM
// Interrupt handler for the EEPROM write "done" interrupt
ISR(NVM_EE_vect)
{
// Disable the EEPROM interrupt
NVM.INTCTRL = (NVM.INTCTRL & ~NVM_EELVL_gm);
}
// AVR1008 fix
static inline void NVM_EXEC_WRAPPER(void)
{
// Save the Sleep register
uint8_t sleepCtr = SLEEP.CTRL;
// Set sleep mode to IDLE
SLEEP.CTRL = (SLEEP.CTRL & ~SLEEP.CTRL) | SLEEP_SMODE_IDLE_gc;
// Save the PMIC Status and control registers
uint8_t statusStore = PMIC.STATUS;
uint8_t pmicStore = PMIC.CTRL;
// Enable only the highest level of interrupts
PMIC.CTRL = (PMIC.CTRL & ~(PMIC_MEDLVLEN_bm | PMIC_LOLVLEN_bm)) | PMIC_HILVLEN_bm;
// Save SREG for later use
uint8_t globalInt = SREG;
// Enable global interrupts
sei();
// Set sleep enabled
SLEEP.CTRL |= SLEEP_SEN_bm;
// Save eeprom interrupt settings for later
uint8_t eepromintStore = NVM.INTCTRL;
NVM_EXEC();
// Enable EEPROM interrupt
NVM.INTCTRL = NVM_EELVL0_bm | NVM_EELVL1_bm;
// Sleep before 2.5uS has passed
sleep_cpu();
// Restore sleep settings
SLEEP.CTRL = sleepCtr;
// Restore PMIC status and control registers
PMIC.STATUS = statusStore;
PMIC.CTRL = pmicStore;
// Restore EEPROM interruptsettings
NVM.INTCTRL = eepromintStore;
// Restore global interrupt settings
SREG = globalInt;
}
#else
#define NVM_EXEC_WRAPPER NVM_EXEC
#endif // USE_AVR1008_EEPROM
void wait_for_nvm(void)
{
while (NVM.STATUS & NVM_NVMBUSY_bm) { };
}
void flush_buffer(void)
{
wait_for_nvm();
if ((NVM.STATUS & NVM_EELOAD_bm) != 0) {
NVM.CMD = NVM_CMD_ERASE_EEPROM_BUFFER_gc;
NVM_EXEC();
}
}
uint8_t EEPROM_read_byte(uint16_t addr)
{
wait_for_nvm();
NVM.ADDR0 = addr & 0xFF;
NVM.ADDR1 = (addr >> 8) & 0x1F;
NVM.ADDR2 = 0;
NVM.CMD = NVM_CMD_READ_EEPROM_gc;
NVM_EXEC();
return NVM.DATA0;
}
void EEPROM_write_byte(uint16_t addr, uint8_t byte)
{
flush_buffer();
NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc;
NVM.ADDR0 = addr & 0xFF;
NVM.ADDR1 = (addr >> 8) & 0x1F;
NVM.ADDR2 = 0;
NVM.DATA0 = byte;
NVM.CMD = NVM_CMD_ERASE_WRITE_EEPROM_PAGE_gc;
NVM_EXEC_WRAPPER();
}
uint16_t EEPROM_read_block(uint16_t addr, uint8_t *dest, uint16_t len)
{
uint16_t cnt = 0;
NVM.ADDR2 = 0;
wait_for_nvm();
while (len > 0)
{
NVM.ADDR0 = addr & 0xFF;
NVM.ADDR1 = (addr >> 8) & 0x1F;
NVM.CMD = NVM_CMD_READ_EEPROM_gc;
NVM_EXEC();
*(dest++) = NVM.DATA0; addr++;
len--; cnt++;
}
return cnt;
}
uint16_t EEPROM_write_block(uint16_t addr, const uint8_t *src, uint16_t len)
{
uint8_t byte_addr = addr % EEPROM_PAGE_SIZE;
uint16_t page_addr = addr - byte_addr;
uint16_t cnt = 0;
flush_buffer();
wait_for_nvm();
NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc;
NVM.ADDR1 = 0;
NVM.ADDR2 = 0;
while (len > 0)
{
NVM.ADDR0 = byte_addr;
NVM.DATA0 = *(src++);
byte_addr++;
len--;
if (len == 0 || byte_addr >= EEPROM_PAGE_SIZE)
{
NVM.ADDR0 = page_addr & 0xFF;
NVM.ADDR1 = (page_addr >> 8) & 0x1F;
NVM.CMD = NVM_CMD_ERASE_WRITE_EEPROM_PAGE_gc;
NVM_EXEC();
page_addr += EEPROM_PAGE_SIZE;
byte_addr = 0;
wait_for_nvm();
NVM.CMD = NVM_CMD_LOAD_EEPROM_BUFFER_gc;
}
cnt++;
}
return cnt;
}
void EEPROM_erase_page(uint16_t addr)
{
NVM.ADDR0 = addr & 0xFF;
NVM.ADDR1 = (addr >> 8) & 0x1F;
NVM.ADDR2 = 0;
wait_for_nvm();
NVM.CMD = NVM_CMD_ERASE_EEPROM_PAGE_gc;
NVM_EXEC_WRAPPER();
}
void EEPROM_erase_all(void)
{
wait_for_nvm();
NVM.CMD = NVM_CMD_ERASE_EEPROM_gc;
NVM_EXEC_WRAPPER();
}
#else // __AVR_XMEGA__
void EEPROM_erase_all(void)
{
uint8_t hfuse = boot_lock_fuse_bits_get(GET_HIGH_FUSE_BITS);
if ((hfuse & (1 << 3)) != 0) {
for (uint16_t i = 0; i < E2END; i++)
{
eeprom_update_byte((uint8_t *)i, 0xff);
}
}
}
#ifdef USE_ENTER_EEPROM
// Parts taken from Atmega's AVR103 App Note.
void eeprom_overwrite_byte(unsigned int addr, char value)
{
#ifdef USE_INTERRUPTS
cli();
#endif // USE_INTERRUPTS
do {} while(EECR & (1<<EEPE)); // Wait for completion of previous write.
do {} while(SPMCSR & (1<<SELFPRGEN)); // Wait for SPM completion.
EEAR = addr; // Set EEPROM address register.
EECR = (1<<EERE); // Start EEPROM read operation.
EEDR = value; // Set EEPROM data register.
EECR = (1<<EEMPE) | // Set Master Write Enable bit...
(1<<EEPM1); // ...and Write-only mode.
EECR |= (1<<EEPE); // Start Write-only operation.
#ifdef USE_INTERRUPTS
sei();
#endif // USE_INTERRUPTS
}
uint8_t enter_eeprom_index;
uint8_t enter_eeprom_value;
uint8_t enter_eeprom_count;
/**
* Check whether to enter the bootloader based on a 32-bit EEPROM value.
* Enter the bootloader if the number of set bits is odd. Fully erased
* there are 32-set bits. The program resets the highest rank set bit
* first, and the bootloader resets the next highest order bit. Each
* bootloader entry requires two bits be reset.
*
* It is the program's, not the bootloader's responsibility to erase the
* values when 0 is reached.
*
* @return 1 if an odd number of bits is set, otherwise 0.
*/
uint8_t enter_eeprom_check(void)
{
enter_eeprom_count = 0;
for (uint8_t i = 0; i != 4; i++)
{
enter_eeprom_value = eeprom_read_byte((uint8_t*) ENTER_EEPROM_ADDR + i);
if (enter_eeprom_value != 0)
{
enter_eeprom_index = i;
break;
}
}
uint8_t c = enter_eeprom_value;
// Now check bit at a time.
while (c & 1)
{
enter_eeprom_count += 1;
c >>= 1;
}
return enter_eeprom_count & 1;
}
void enter_eeprom_reset()
{
// Set bits to an even value.
if ((enter_eeprom_count & 1) == 0) return; // Already even.
enter_eeprom_value &= ~_BV(enter_eeprom_count - 1);
eeprom_overwrite_byte(
ENTER_EEPROM_ADDR + enter_eeprom_index, enter_eeprom_value);
}
#endif // USE_ENTER_EEPROM
#endif // __AVR_XMEGA__