altos: Make sure we drop the SPI mutex when aborting cc1120 recv
[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; version 2 of the License.
7  *
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.
12  *
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.
16  */
17
18 #include <ao.h>
19
20 #if HAS_EEPROM
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
25
26 /* Total bytes of available storage */
27 __pdata ao_pos_t        ao_storage_total = 64l * 1024l;
28
29 /* Storage unit size - device reads and writes must be within blocks of this size. */
30 __pdata uint16_t        ao_storage_unit = 128;
31
32 static void
33 ao_at24c_set_address(uint8_t addr, ao_pos_t pos)
34 {
35         uint8_t a[2];
36
37         a[0] = pos >> 8;
38         a[1] = pos;
39         ao_i2c_start_bus(addr);
40         ao_i2c_send_bus(a, 2, 0);
41 }
42
43 /*
44  * Erase the specified sector
45  */
46 uint8_t
47 ao_storage_erase(ao_pos_t pos) __reentrant
48 {
49         if (pos >= ao_storage_total || pos + AO_AT24C_PAGE_LEN > ao_storage_total)
50                 return 0;
51
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);
56         return 1;
57 }
58
59 /*
60  * Write to flash
61  */
62 uint8_t
63 ao_storage_device_write(ao_pos_t pos, __xdata void *d, uint16_t len) __reentrant
64 {
65         if (pos >= ao_storage_total || pos + len > ao_storage_total)
66                 return 0;
67
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);
72         return 1;
73 }
74
75 /*
76  * Read from flash
77  */
78 uint8_t
79 ao_storage_device_read(ao_pos_t pos, __xdata void *d, uint16_t len) __reentrant
80 {
81         if (pos >= ao_storage_total || pos + len > ao_storage_total)
82                 return 0;
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);
87         return 1;
88 }
89
90 void
91 ao_storage_flush(void) __reentrant
92 {
93 }
94
95 void
96 ao_storage_setup(void)
97 {
98 }
99
100 void
101 ao_storage_device_init(void)
102 {
103 }
104 #endif