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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 #include <ao_storage.h>
22 ao_storage_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
28 if (pos >= ao_storage_total || pos + len > ao_storage_total)
32 /* Compute portion of transfer within
35 this_off = (uint16_t) pos & (ao_storage_unit - 1);
36 this_len = ao_storage_unit - this_off;
40 if (!ao_storage_device_read(pos, buf, this_len))
43 /* See how much is left */
52 ao_storage_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant
58 if (pos >= ao_storage_total || pos + len > ao_storage_total)
62 /* Compute portion of transfer within
65 this_off = (uint16_t) pos & (ao_storage_unit - 1);
66 this_len = ao_storage_unit - this_off;
70 if (!ao_storage_device_write(pos, buf, this_len))
73 /* See how much is left */
81 static __xdata uint8_t storage_data[8];
84 ao_storage_dump(void) __reentrant
89 if (ao_cmd_status != ao_cmd_success)
91 for (i = 0; ; i += 8) {
92 if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
95 ao_cmd_put16((uint16_t) i);
96 for (j = 0; j < 8; j++) {
98 ao_cmd_put8(storage_data[j]);
107 #if HAS_STORAGE_DEBUG
109 /* not enough space for this today
112 ao_storage_store(void) __reentrant
117 static __xdata uint8_t b;
121 block = ao_cmd_lex_i;
124 addr = ((uint32_t) block << 8) | i;
127 if (ao_cmd_status != ao_cmd_success)
131 if (ao_cmd_status != ao_cmd_success)
134 ao_storage_write(addr, &b, 1);
141 ao_storage_zap(void) __reentrant
144 if (ao_cmd_status != ao_cmd_success)
146 ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
150 ao_storage_zapall(void) __reentrant
155 if (!ao_match_word("DoIt"))
157 for (pos = 0; pos < ao_storage_log_max; pos += ao_storage_block)
158 ao_storage_erase(pos);
162 ao_storage_info(void) __reentrant
165 printf("Storage size: %ld\n", (long) ao_storage_total);
166 printf("Storage erase unit: %ld\n", (long) ao_storage_block);
167 ao_storage_device_info();
170 __code struct ao_cmds ao_storage_cmds[] = {
171 { ao_storage_info, "f\0Show storage" },
172 { ao_storage_dump, "e <block>\0Dump flash" },
173 #if HAS_STORAGE_DEBUG
174 { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
176 { ao_storage_zap, "z <block>\0Erase <block>" },
177 { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
182 ao_storage_init(void)
184 ao_storage_device_init();
185 ao_cmd_register(&ao_storage_cmds[0]);