2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
3 * Copyright © 2011 Anthony Towns <aj@erisian.com.au>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
21 #include <ao_storage.h>
23 /* Total bytes of available storage */
24 __pdata ao_pos_t ao_storage_total = 1024;
26 /* Block size - device is erased in these units. */
27 __pdata ao_pos_t ao_storage_block = 1024;
29 /* Byte offset of config block. Will be ao_storage_block bytes long */
30 __pdata ao_pos_t ao_storage_config = 0;
32 /* Storage unit size - device reads and writes must be within blocks of this size. */
33 __pdata uint16_t ao_storage_unit = 1024;
36 * The internal flash chip is arranged in 8 byte sectors; the
37 * chip cannot erase in units smaller than that.
39 * Writing happens in units of 2 bytes and
40 * can only change bits from 1 to 0. So, you can rewrite
41 * the same contents, or append to an existing page easily enough
45 * Erase the specified sector
48 ao_storage_erase(ao_pos_t pos) __reentrant
54 #define ao_intflash_wait_idle() do { \
55 /* Wait for any outstanding writes to complete */ \
56 while (EECR & (1 << EEPE)) \
61 ao_intflash_write(uint16_t pos, uint8_t d)
63 ao_intflash_wait_idle();
73 ao_intflash_read(uint16_t pos)
75 ao_intflash_wait_idle();
86 ao_storage_device_write(ao_pos_t pos32, __xdata void *v, uint16_t len) __reentrant
89 __xdata uint8_t *d = v;
91 if (pos >= ao_storage_total || pos + len > ao_storage_total)
95 ao_intflash_write(pos++, *d++);
104 ao_storage_device_read(ao_pos_t pos, __xdata void *v, uint16_t len) __reentrant
108 if (pos >= ao_storage_total || pos + len > ao_storage_total)
111 *d++ = ao_intflash_read(pos++);
116 ao_storage_flush(void) __reentrant
121 ao_storage_setup(void)
126 ao_storage_device_info(void) __reentrant
128 printf ("Using internal flash\n");
132 ao_storage_device_init(void)