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.
21 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
27 if (pos >= ao_storage_total || pos + len > ao_storage_total)
31 /* Compute portion of transfer within
34 this_off = (uint16_t) pos & (ao_storage_unit - 1);
35 this_len = ao_storage_unit - this_off;
39 if (!ao_storage_device_read(pos, buf, this_len))
42 /* See how much is left */
51 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
57 if (pos >= ao_storage_total || pos + len > ao_storage_total)
61 /* Compute portion of transfer within
64 this_off = (uint16_t) pos & (ao_storage_unit - 1);
65 this_len = ao_storage_unit - this_off;
69 if (!ao_storage_device_write(pos, buf, this_len))
72 /* See how much is left */
80 static __xdata uint8_t storage_data[8];
83 ao_storage_dump(void) __reentrant
88 if (ao_cmd_status != ao_cmd_success)
90 for (i = 0; ; i += 8) {
91 if (ao_storage_read(((uint32_t) (ao_cmd_lex_i) << 8) + i,
94 ao_cmd_put16((uint16_t) i);
95 for (j = 0; j < 8; j++) {
97 ao_cmd_put8(storage_data[j]);
106 #if HAS_STORAGE_DEBUG
108 /* not enough space for this today
111 ao_storage_store(void) __reentrant
116 static __xdata uint8_t b;
120 block = ao_cmd_lex_i;
123 addr = ((uint32_t) block << 8) | i;
126 if (ao_cmd_status != ao_cmd_success)
130 if (ao_cmd_status != ao_cmd_success)
133 ao_storage_write(addr, &b, 1);
140 ao_storage_zap(void) __reentrant
143 if (ao_cmd_status != ao_cmd_success)
145 ao_storage_erase((uint32_t) ao_cmd_lex_i << 8);
149 ao_storage_zapall(void) __reentrant
154 if (!ao_match_word("DoIt"))
156 for (pos = 0; pos < ao_storage_config; pos += ao_storage_block)
157 ao_storage_erase(pos);
161 ao_storage_info(void) __reentrant
164 printf("Storage size: %ld\n", ao_storage_total);
165 printf("Storage erase unit: %ld\n", ao_storage_block);
166 ao_storage_device_info();
169 __code struct ao_cmds ao_storage_cmds[] = {
170 { ao_storage_info, "f\0Show storage" },
171 { ao_storage_dump, "e <block>\0Dump flash" },
172 #if HAS_STORAGE_DEBUG
173 { ao_storage_store, "w <block> <start> <len> <data> ...\0Write data to flash" },
175 { ao_storage_zap, "z <block>\0Erase <block>" },
176 { ao_storage_zapall,"Z <key>\0Erase all. <key> is doit with D&I" },
181 ao_storage_init(void)
183 ao_storage_device_init();
184 ao_cmd_register(&ao_storage_cmds[0]);