2 * Copyright © 2009 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 #define EE_BLOCK_SIZE ((uint16_t) (256))
22 #define EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
24 /* Total bytes of available storage */
25 __xdata uint32_t ao_storage_total;
27 /* Block size - device is erased in these units. At least 256 bytes */
28 __xdata uint32_t ao_storage_block;
30 /* Byte offset of config block. Will be ao_storage_block bytes long */
31 __xdata uint32_t ao_storage_config;
34 * Using SPI on USART 0, with P1_2 as the chip select
40 static __xdata uint8_t ao_ee_mutex;
42 #define ao_ee_delay() do { \
48 static void ao_ee_cs_low(void)
55 static void ao_ee_cs_high(void)
62 struct ao_ee_instruction {
65 } __xdata ao_ee_instruction;
68 ao_ee_write_enable(void)
71 ao_ee_instruction.instruction = EE_WREN;
72 ao_spi_send(&ao_ee_instruction, 1);
80 ao_ee_instruction.instruction = EE_RDSR;
81 ao_spi_send(&ao_ee_instruction, 1);
82 ao_spi_recv(&ao_ee_instruction, 1);
84 return ao_ee_instruction.instruction;
88 ao_ee_wrsr(uint8_t status)
91 ao_ee_instruction.instruction = EE_WRSR;
92 ao_ee_instruction.address[0] = status;
93 ao_spi_send(&ao_ee_instruction, 2);
97 #define EE_BLOCK_NONE 0xffff
99 static __xdata uint8_t ao_ee_data[EE_BLOCK_SIZE];
100 static __pdata uint16_t ao_ee_block = EE_BLOCK_NONE;
101 static __pdata uint8_t ao_ee_block_dirty;
103 /* Write the current block to the EEPROM */
105 ao_ee_write_block(void)
109 status = ao_ee_rdsr();
110 if (status & (EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN)) {
111 status &= ~(EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN);
114 ao_ee_write_enable();
116 ao_ee_instruction.instruction = EE_WRITE;
117 ao_ee_instruction.address[0] = ao_ee_block >> 8;
118 ao_ee_instruction.address[1] = ao_ee_block;
119 ao_ee_instruction.address[2] = 0;
120 ao_spi_send(&ao_ee_instruction, 4);
121 ao_spi_send(ao_ee_data, EE_BLOCK_SIZE);
124 uint8_t status = ao_ee_rdsr();
125 if ((status & EE_STATUS_WIP) == 0)
130 /* Read the current block from the EEPROM */
132 ao_ee_read_block(void)
135 ao_ee_instruction.instruction = EE_READ;
136 ao_ee_instruction.address[0] = ao_ee_block >> 8;
137 ao_ee_instruction.address[1] = ao_ee_block;
138 ao_ee_instruction.address[2] = 0;
139 ao_spi_send(&ao_ee_instruction, 4);
140 ao_spi_recv(ao_ee_data, EE_BLOCK_SIZE);
145 ao_ee_flush_internal(void)
147 if (ao_ee_block_dirty) {
149 ao_ee_block_dirty = 0;
154 ao_ee_fill(uint16_t block)
156 if (block != ao_ee_block) {
157 ao_ee_flush_internal();
164 ao_storage_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
170 if (pos >= ao_storage_total || pos + len > ao_storage_total)
174 /* Compute portion of transfer within
178 this_len = EE_BLOCK_SIZE - (uint16_t) this_off;
179 block = (uint16_t) (pos >> 8);
182 if (this_len & 0xff00)
183 ao_panic(AO_PANIC_EE);
185 /* Transfer the data */
186 ao_mutex_get(&ao_ee_mutex); {
187 if (this_len != EE_BLOCK_SIZE)
190 ao_ee_flush_internal();
193 memcpy(ao_ee_data + this_off, buf, this_len);
194 ao_ee_block_dirty = 1;
195 } ao_mutex_put(&ao_ee_mutex);
197 /* See how much is left */
206 ao_storage_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
212 if (pos >= ao_storage_total || pos + len > ao_storage_total)
216 /* Compute portion of transfer within
220 this_len = EE_BLOCK_SIZE - (uint16_t) this_off;
221 block = (uint16_t) (pos >> 8);
224 if (this_len & 0xff00)
225 ao_panic(AO_PANIC_EE);
227 /* Transfer the data */
228 ao_mutex_get(&ao_ee_mutex); {
230 memcpy(buf, ao_ee_data + this_off, this_len);
231 } ao_mutex_put(&ao_ee_mutex);
233 /* See how much is left */
242 ao_storage_flush(void) __reentrant
244 ao_mutex_get(&ao_ee_mutex); {
245 ao_ee_flush_internal();
246 } ao_mutex_put(&ao_ee_mutex);
250 ao_storage_erase(uint32_t pos) __reentrant
252 ao_mutex_get(&ao_ee_mutex); {
253 uint16_t block = (uint16_t) (pos >> 8);
255 memset(ao_ee_data, 0xff, EE_BLOCK_SIZE);
256 ao_ee_block_dirty = 1;
257 } ao_mutex_put(&ao_ee_mutex);
262 ee_dump(void) __reentrant
264 static __xdata uint8_t b;
269 block = ao_cmd_lex_i;
270 if (ao_cmd_status != ao_cmd_success)
277 ao_cmd_put16((uint16_t) i);
280 ao_storage_read(((uint32_t) block << 8) | i, &b, 1);
288 ee_store(void) __reentrant
293 static __xdata uint8_t b;
297 block = ao_cmd_lex_i;
300 addr = ((uint32_t) block << 8) | i;
303 if (ao_cmd_status != ao_cmd_success)
307 if (ao_cmd_status != ao_cmd_success)
310 ao_storage_write(addr, &b, 1);
316 __code struct ao_cmds ao_ee_cmds[] = {
317 { 'e', ee_dump, "e <block> Dump a block of EEPROM data" },
318 { 'w', ee_store, "w <block> <start> <len> <data> ... Write data to EEPROM" },
319 { 0, ee_store, NULL },
323 ao_storage_setup(void)
325 if (ao_storage_total == 0) {
326 ao_storage_total = EE_DEVICE_SIZE;
327 ao_storage_block = EE_BLOCK_SIZE;
328 ao_storage_config = EE_DEVICE_SIZE - EE_BLOCK_SIZE;
333 * To initialize the chip, set up the CS line and
337 ao_storage_init(void)
341 P1DIR |= (1 << EE_CS_INDEX);
342 P1SEL &= ~(1 << EE_CS_INDEX);
344 ao_cmd_register(&ao_ee_cmds[0]);