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.
19 #include "ao_25lc1024.h"
21 #define EE_BLOCK_SIZE ((uint16_t) (256))
22 #define EE_BLOCK_SHIFT 8
23 #define EE_DEVICE_SIZE ((uint32_t) 128 * (uint32_t) 1024)
25 /* Total bytes of available storage */
26 __pdata uint32_t ao_storage_total;
28 /* Block size - device is erased in these units. At least 256 bytes */
29 __pdata uint32_t ao_storage_block;
31 /* Byte offset of config block. Will be ao_storage_block bytes long */
32 __pdata uint32_t ao_storage_config;
34 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
35 __pdata uint16_t ao_storage_unit;
38 * Using SPI on USART 0, with P1_2 as the chip select
44 static __xdata uint8_t ao_ee_mutex;
46 #define ao_ee_delay() do { \
52 #define ao_ee_cs_low() ao_spi_get_bit(EE_CS)
54 #define ao_ee_cs_high() ao_spi_put_bit(EE_CS)
56 struct ao_ee_instruction {
59 } __xdata ao_ee_instruction;
62 ao_ee_write_enable(void)
65 ao_ee_instruction.instruction = EE_WREN;
66 ao_spi_send(&ao_ee_instruction, 1);
74 ao_ee_instruction.instruction = EE_RDSR;
75 ao_spi_send(&ao_ee_instruction, 1);
76 ao_spi_recv(&ao_ee_instruction, 1);
78 return ao_ee_instruction.instruction;
82 ao_ee_wrsr(uint8_t status)
85 ao_ee_instruction.instruction = EE_WRSR;
86 ao_ee_instruction.address[0] = status;
87 ao_spi_send(&ao_ee_instruction, 2);
91 #define EE_BLOCK_NONE 0xffff
93 static __xdata uint8_t ao_ee_data[EE_BLOCK_SIZE];
94 static __pdata uint16_t ao_ee_block = EE_BLOCK_NONE;
95 static __pdata uint8_t ao_ee_block_dirty;
97 /* Write the current block to the EEPROM */
99 ao_ee_write_block(void)
103 status = ao_ee_rdsr();
104 if (status & (EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN)) {
105 status &= ~(EE_STATUS_BP0|EE_STATUS_BP1|EE_STATUS_WPEN);
108 ao_ee_write_enable();
110 ao_ee_instruction.instruction = EE_WRITE;
111 ao_ee_instruction.address[0] = ao_ee_block >> 8;
112 ao_ee_instruction.address[1] = ao_ee_block;
113 ao_ee_instruction.address[2] = 0;
114 ao_spi_send(&ao_ee_instruction, 4);
115 ao_spi_send(ao_ee_data, EE_BLOCK_SIZE);
118 uint8_t status = ao_ee_rdsr();
119 if ((status & EE_STATUS_WIP) == 0)
124 /* Read the current block from the EEPROM */
126 ao_ee_read_block(void)
129 ao_ee_instruction.instruction = EE_READ;
130 ao_ee_instruction.address[0] = ao_ee_block >> 8;
131 ao_ee_instruction.address[1] = ao_ee_block;
132 ao_ee_instruction.address[2] = 0;
133 ao_spi_send(&ao_ee_instruction, 4);
134 ao_spi_recv(ao_ee_data, EE_BLOCK_SIZE);
139 ao_ee_flush_internal(void)
141 if (ao_ee_block_dirty) {
143 ao_ee_block_dirty = 0;
148 ao_ee_fill(uint16_t block)
150 if (block != ao_ee_block) {
151 ao_ee_flush_internal();
158 ao_storage_device_write(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
160 uint16_t block = (uint16_t) (pos >> EE_BLOCK_SHIFT);
162 /* Transfer the data */
163 ao_mutex_get(&ao_ee_mutex); {
164 if (len != EE_BLOCK_SIZE)
167 ao_ee_flush_internal();
170 ao_xmemcpy(ao_ee_data + (uint16_t) (pos & 0xff), buf, len);
171 ao_ee_block_dirty = 1;
172 } ao_mutex_put(&ao_ee_mutex);
177 ao_storage_device_read(uint32_t pos, __xdata void *buf, uint16_t len) __reentrant
179 uint16_t block = (uint16_t) (pos >> EE_BLOCK_SHIFT);
181 /* Transfer the data */
182 ao_mutex_get(&ao_ee_mutex); {
184 ao_xmemcpy(buf, ao_ee_data + (uint16_t) (pos & 0xff), len);
185 } ao_mutex_put(&ao_ee_mutex);
190 ao_storage_flush(void) __reentrant
192 ao_mutex_get(&ao_ee_mutex); {
193 ao_ee_flush_internal();
194 } ao_mutex_put(&ao_ee_mutex);
198 ao_storage_erase(uint32_t pos) __reentrant
200 ao_mutex_get(&ao_ee_mutex); {
201 ao_ee_flush_internal();
202 ao_ee_block = (uint16_t) (pos >> EE_BLOCK_SHIFT);
203 ao_xmemset(ao_ee_data, 0xff, EE_BLOCK_SIZE);
204 ao_ee_block_dirty = 1;
205 } ao_mutex_put(&ao_ee_mutex);
210 ee_store(void) __reentrant
215 ao_storage_setup(void)
217 if (ao_storage_total == 0) {
218 ao_storage_total = EE_DEVICE_SIZE;
219 ao_storage_block = EE_BLOCK_SIZE;
220 ao_storage_config = EE_DEVICE_SIZE - EE_BLOCK_SIZE;
221 ao_storage_unit = EE_BLOCK_SIZE;
226 ao_storage_device_info(void) __reentrant
231 * To initialize the chip, set up the CS line and
235 ao_storage_device_init(void)
239 P1DIR |= (1 << EE_CS_INDEX);
240 P1SEL &= ~(1 << EE_CS_INDEX);