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.
22 * Using SPI on USART 0, with P1_2 as the chip select
28 __xdata uint8_t ao_ee_mutex;
30 #define ao_ee_delay() do { \
36 void ao_ee_cs_low(void)
43 void ao_ee_cs_high(void)
53 struct ao_ee_instruction {
56 } __xdata ao_ee_instruction;
59 ao_ee_write_enable(void)
62 ao_ee_instruction.instruction = EE_WREN;
63 ao_spi_send(&ao_ee_instruction, 1);
71 ao_ee_instruction.instruction = EE_RDSR;
72 ao_spi_send(&ao_ee_instruction, 1);
73 ao_spi_recv(&ao_ee_instruction, 1);
75 return ao_ee_instruction.instruction;
79 ao_ee_wrsr(uint8_t status)
82 ao_ee_instruction.instruction = EE_WRSR;
83 ao_ee_instruction.address[0] = status;
84 ao_spi_send(&ao_ee_instruction, 2);
88 #define EE_BLOCK_NONE 0xffff
90 static __xdata uint8_t ao_ee_data[EE_BLOCK];
91 static __pdata uint16_t ao_ee_block = EE_BLOCK_NONE;
92 static __pdata uint8_t ao_ee_block_dirty;
94 /* Write the current block to the EEPROM */
96 ao_ee_write_block(void)
100 status = ao_ee_rdsr();
101 if (status & (EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN)) {
102 status &= ~(EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN);
105 ao_ee_write_enable();
107 ao_ee_instruction.instruction = EE_WRITE;
108 ao_ee_instruction.address[0] = ao_ee_block >> 8;
109 ao_ee_instruction.address[1] = ao_ee_block;
110 ao_ee_instruction.address[2] = 0;
111 ao_spi_send(&ao_ee_instruction, 4);
112 ao_spi_send(ao_ee_data, EE_BLOCK);
115 uint8_t status = ao_ee_rdsr();
116 if ((status & EE_STATUS_WIP) == 0)
121 /* Read the current block from the EEPROM */
123 ao_ee_read_block(void)
126 ao_ee_instruction.instruction = EE_READ;
127 ao_ee_instruction.address[0] = ao_ee_block >> 8;
128 ao_ee_instruction.address[1] = ao_ee_block;
129 ao_ee_instruction.address[2] = 0;
130 ao_spi_send(&ao_ee_instruction, 4);
131 ao_spi_recv(ao_ee_data, EE_BLOCK);
136 ao_ee_flush_internal(void)
138 if (ao_ee_block_dirty) {
140 ao_ee_block_dirty = 0;
145 ao_ee_fill(uint16_t block)
147 if (block != ao_ee_block) {
148 ao_ee_flush_internal();
155 ao_ee_write(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant
161 if (pos >= AO_EE_DATA_SIZE || pos + len > AO_EE_DATA_SIZE)
165 /* Compute portion of transfer within
169 this_len = 256 - (uint16_t) this_off;
170 block = (uint16_t) (pos >> 8);
173 if (this_len & 0xff00)
174 ao_panic(AO_PANIC_EE);
176 /* Transfer the data */
177 ao_mutex_get(&ao_ee_mutex); {
181 ao_ee_flush_internal();
184 memcpy(ao_ee_data + this_off, buf, this_len);
185 ao_ee_block_dirty = 1;
186 } ao_mutex_put(&ao_ee_mutex);
188 /* See how much is left */
197 ao_ee_read(uint32_t pos, uint8_t *buf, uint16_t len) __reentrant
203 if (pos >= AO_EE_DATA_SIZE || pos + len > AO_EE_DATA_SIZE)
207 /* Compute portion of transfer within
211 this_len = 256 - (uint16_t) this_off;
212 block = (uint16_t) (pos >> 8);
215 if (this_len & 0xff00)
216 ao_panic(AO_PANIC_EE);
218 /* Transfer the data */
219 ao_mutex_get(&ao_ee_mutex); {
221 memcpy(buf, ao_ee_data + this_off, this_len);
222 } ao_mutex_put(&ao_ee_mutex);
224 /* See how much is left */
233 ao_ee_flush(void) __reentrant
235 ao_mutex_get(&ao_ee_mutex); {
236 ao_ee_flush_internal();
237 } ao_mutex_put(&ao_ee_mutex);
241 * Read/write the config block, which is in
242 * the last block of the ao_eeprom
245 ao_ee_write_config(uint8_t *buf, uint16_t len) __reentrant
247 if (len > AO_EE_BLOCK_SIZE)
249 ao_mutex_get(&ao_ee_mutex); {
250 ao_ee_fill(AO_EE_CONFIG_BLOCK);
251 memcpy(ao_ee_data, buf, len);
252 ao_ee_block_dirty = 1;
253 ao_ee_flush_internal();
254 } ao_mutex_put(&ao_ee_mutex);
259 ao_ee_read_config(uint8_t *buf, uint16_t len) __reentrant
261 if (len > AO_EE_BLOCK_SIZE)
263 ao_mutex_get(&ao_ee_mutex); {
264 ao_ee_fill(AO_EE_CONFIG_BLOCK);
265 memcpy(buf, ao_ee_data, len);
266 } ao_mutex_put(&ao_ee_mutex);
271 ee_dump(void) __reentrant
278 block = ao_cmd_lex_i;
279 if (ao_cmd_status != ao_cmd_success)
286 ao_cmd_put16((uint16_t) i);
289 ao_ee_read(((uint32_t) block << 8) | i, &b, 1);
297 ee_store(void) __reentrant
306 block = ao_cmd_lex_i;
309 addr = ((uint32_t) block << 8) | i;
312 if (ao_cmd_status != ao_cmd_success)
316 if (ao_cmd_status != ao_cmd_success)
319 ao_ee_write(addr, &b, 1);
325 __code struct ao_cmds ao_ee_cmds[] = {
326 { 'e', ee_dump, "e <block> Dump a block of EEPROM data" },
327 { 'w', ee_store, "w <block> <start> <len> <data> ... Write data to EEPROM" },
328 { 0, ee_store, NULL },
332 * To initialize the chip, set up the CS line and
340 P1DIR |= (1 << EE_CS_INDEX);
341 P1SEL &= ~(1 << EE_CS_INDEX);
343 ao_cmd_register(&ao_ee_cmds[0]);