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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 #define AO_AT24C_ADDR 0xa0
23 #define AO_AT24C_ADDR_WRITE (AO_AT24C_ADDR|0)
24 #define AO_AT24C_ADDR_READ (AO_AT24C_ADDR|1)
25 #define AO_AT24C_PAGE_LEN 128
27 /* Total bytes of available storage */
28 ao_pos_t ao_storage_total = 64l * 1024l;
30 /* Storage unit size - device reads and writes must be within blocks of this size. */
31 uint16_t ao_storage_unit = 128;
34 ao_at24c_set_address(uint8_t addr, ao_pos_t pos)
40 ao_i2c_start_bus(addr);
41 ao_i2c_send_bus(a, 2, 0);
45 * Erase the specified sector
48 ao_storage_erase(ao_pos_t pos)
50 if (pos >= ao_storage_total || pos + AO_AT24C_PAGE_LEN > ao_storage_total)
53 ao_mutex_get(&ao_at24c_mutex);
54 ao_at24c_set_address(AO_AT24C_ADDR_WRITE, pos);
55 ao_i2c_send_fixed_bus(0xff, AO_AT24C_PAGE_LEN, 1);
56 ao_mutex_put(&ao_at24c_mutex);
64 ao_storage_device_write(ao_pos_t pos, void *d, uint16_t len)
66 if (pos >= ao_storage_total || pos + len > ao_storage_total)
69 ao_mutex_get(&ao_m25_mutex);
70 ao_at24c_set_address(AO_AT24C_ADDR_WRITE, pos);
71 ao_i2c_send_bus(d, len, 1);
72 ao_mutex_put(&ao_m25_mutex);
80 ao_storage_device_read(ao_pos_t pos, void *d, uint16_t len)
82 if (pos >= ao_storage_total || pos + len > ao_storage_total)
84 ao_mutex_get(&ao_m25_mutex);
85 ao_at24c_set_address(AO_AT24C_ADDR_READ, pos);
86 ao_i2c_recv_bus(d, len, 1);
87 ao_mutex_put(&ao_m25_mutex);
92 ao_storage_flush(void)
97 ao_storage_setup(void)
102 ao_storage_device_init(void)