2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
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>
23 ao_storage_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
29 if (pos >= ao_storage_total || pos + len > ao_storage_total)
33 /* Compute portion of transfer within
36 this_off = (uint16_t) pos & (ao_storage_unit - 1);
37 this_len = ao_storage_unit - this_off;
41 if (!ao_storage_device_read(pos, buf, this_len))
44 /* See how much is left */
53 ao_storage_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
59 if (pos >= ao_storage_total || pos + len > ao_storage_total)
63 /* Compute portion of transfer within
66 this_off = (uint16_t) pos & (ao_storage_unit - 1);
67 this_len = ao_storage_unit - this_off;
71 if (!ao_storage_device_write(pos, buf, this_len))
74 /* See how much is left */
82 static __xdata uint8_t storage_data[8];
85 ao_storage_dump(void) __reentrant
90 if (ao_cmd_status != ao_cmd_success)
92 for (i = 0; ; i += 8) {
93 if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
96 ao_cmd_put16((uint16_t) i);
97 for (j = 0; j < 8; j++) {
99 ao_cmd_put8(storage_data[j]);
108 #if HAS_STORAGE_DEBUG
110 /* not enough space for this today
113 ao_storage_store(void) __reentrant
118 static __xdata uint8_t b;
122 block = ao_cmd_lex_i;
125 addr = ((uint32_t) block << 8) | i;
128 if (ao_cmd_status != ao_cmd_success)
132 if (ao_cmd_status != ao_cmd_success)
135 ao_storage_write(addr, &b, 1);
142 ao_storage_zap(void) __reentrant
145 if (ao_cmd_status != ao_cmd_success)
147 ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
151 ao_storage_zapall(void) __reentrant
156 if (!ao_match_word("DoIt"))
158 for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block)
159 ao_storage_erase(pos);
163 ao_storage_info(void) __reentrant
166 printf("Storage size: %ld\n", (long) ao_storage_total);
167 printf("Storage erase unit: %ld\n", (long) ao_storage_block);
168 ao_storage_device_info();
171 __code struct ao_cmds ao_storage_cmds[] = {
172 { ao_storage_info, "f\0Show storage" },
173 { ao_storage_dump, "e <block>\0Dump flash" },
174 #if HAS_STORAGE_DEBUG
175 { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
177 { ao_storage_zap, "z <block>\0Erase <block>" },
178 { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
183 ao_storage_init(void)
185 ao_storage_device_init();
186 ao_cmd_register(&ao_storage_cmds[0]);