2 * Copyright © 2012 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 AO_AT24C_ADDR 0xa0
22 #define AO_AT24C_ADDR_WRITE (AO_AT24C_ADDR|0)
23 #define AO_AT24C_ADDR_READ (AO_AT24C_ADDR|1)
24 #define AO_AT24C_PAGE_LEN 128
26 /* Total bytes of available storage */
27 __pdata ao_pos_t ao_storage_total = 64l * 1024l;
29 /* Storage unit size - device reads and writes must be within blocks of this size. */
30 __pdata uint16_t ao_storage_unit = 128;
33 ao_at24c_set_address(uint8_t addr, ao_pos_t pos)
39 ao_i2c_start_bus(addr);
40 ao_i2c_send_bus(a, 2, 0);
44 * Erase the specified sector
47 ao_storage_erase(ao_pos_t pos) __reentrant
49 if (pos >= ao_storage_total || pos + AO_AT24C_PAGE_LEN > ao_storage_total)
52 ao_mutex_get(&ao_at24c_mutex);
53 ao_at24c_set_address(AO_AT24C_ADDR_WRITE, pos);
54 ao_i2c_send_fixed_bus(0xff, AO_AT24C_PAGE_LEN, 1);
55 ao_mutex_put(&ao_at24c_mutex);
63 ao_storage_device_write(ao_pos_t pos, __xdata void *d, uint16_t len) __reentrant
65 if (pos >= ao_storage_total || pos + len > ao_storage_total)
68 ao_mutex_get(&ao_m25_mutex);
69 ao_at24c_set_address(AO_AT24C_ADDR_WRITE, pos);
70 ao_i2c_send_bus(d, len, 1);
71 ao_mutex_put(&ao_m25_mutex);
79 ao_storage_device_read(ao_pos_t pos, __xdata void *d, uint16_t len) __reentrant
81 if (pos >= ao_storage_total || pos + len > ao_storage_total)
83 ao_mutex_get(&ao_m25_mutex);
84 ao_at24c_set_address(AO_AT24C_ADDR_READ, pos);
85 ao_i2c_recv_bus(d, len, 1);
86 ao_mutex_put(&ao_m25_mutex);
91 ao_storage_flush(void) __reentrant
96 ao_storage_setup(void)
101 ao_storage_device_init(void)