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; version 2 of the License.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #include <ao_storage.h>
22 /* Total bytes of available storage */
23 __pdata ao_pos_t ao_storage_total = 1024;
25 /* Block size - device is erased in these units. */
26 __pdata ao_pos_t ao_storage_block = 1024;
28 /* Byte offset of config block. Will be ao_storage_block bytes long */
29 __pdata ao_pos_t ao_storage_config = 0;
31 /* Storage unit size - device reads and writes must be within blocks of this size. */
32 __pdata uint16_t ao_storage_unit = 1024;
35 * The internal flash chip is arranged in 8 byte sectors; the
36 * chip cannot erase in units smaller than that.
38 * Writing happens in units of 2 bytes and
39 * can only change bits from 1 to 0. So, you can rewrite
40 * the same contents, or append to an existing page easily enough
44 * Erase the specified sector
47 ao_storage_erase(ao_pos_t pos) __reentrant
53 #define ao_intflash_wait_idle() do { \
54 /* Wait for any outstanding writes to complete */ \
55 while (EECR & (1 << EEPE)) \
60 ao_intflash_write(uint16_t pos, uint8_t d)
62 ao_intflash_wait_idle();
72 ao_intflash_read(uint16_t pos)
74 ao_intflash_wait_idle();
85 ao_storage_device_write(ao_pos_t pos32, __xdata void *v, uint16_t len) __reentrant
88 __xdata uint8_t *d = v;
90 if (pos >= ao_storage_total || pos + len > ao_storage_total)
94 ao_intflash_write(pos++, *d++);
103 ao_storage_device_read(ao_pos_t pos, __xdata void *v, uint16_t len) __reentrant
107 if (pos >= ao_storage_total || pos + len > ao_storage_total)
110 *d++ = ao_intflash_read(pos++);
115 ao_storage_flush(void) __reentrant
120 ao_storage_setup(void)
125 ao_storage_device_info(void) __reentrant
127 printf ("Using internal flash\n");
131 ao_storage_device_init(void)