altos/telefireone-v1.0: Track ao_led_init API change
[fw/altos] / src / drivers / ao_at24c.c
1 /*
2  * Copyright © 2012 Keith Packard <keithp@keithp.com>
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <ao.h>
20
21 #if HAS_EEPROM
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
26
27 /* Total bytes of available storage */
28 ao_pos_t        ao_storage_total = 64l * 1024l;
29
30 /* Storage unit size - device reads and writes must be within blocks of this size. */
31 uint16_t        ao_storage_unit = 128;
32
33 static void
34 ao_at24c_set_address(uint8_t addr, ao_pos_t pos)
35 {
36         uint8_t a[2];
37
38         a[0] = pos >> 8;
39         a[1] = pos;
40         ao_i2c_start_bus(addr);
41         ao_i2c_send_bus(a, 2, 0);
42 }
43
44 /*
45  * Erase the specified sector
46  */
47 uint8_t
48 ao_storage_erase(ao_pos_t pos) 
49 {
50         if (pos >= ao_storage_total || pos + AO_AT24C_PAGE_LEN > ao_storage_total)
51                 return 0;
52
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);
57         return 1;
58 }
59
60 /*
61  * Write to flash
62  */
63 uint8_t
64 ao_storage_device_write(ao_pos_t pos, void *d, uint16_t len) 
65 {
66         if (pos >= ao_storage_total || pos + len > ao_storage_total)
67                 return 0;
68
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);
73         return 1;
74 }
75
76 /*
77  * Read from flash
78  */
79 uint8_t
80 ao_storage_device_read(ao_pos_t pos, void *d, uint16_t len) 
81 {
82         if (pos >= ao_storage_total || pos + len > ao_storage_total)
83                 return 0;
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);
88         return 1;
89 }
90
91 void
92 ao_storage_flush(void) 
93 {
94 }
95
96 void
97 ao_storage_setup(void)
98 {
99 }
100
101 void
102 ao_storage_device_init(void)
103 {
104 }
105 #endif