altos: Allow use of internal EEPROM for config storage
[fw/altos] / src / core / ao_storage.h
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 #ifndef _AO_STORAGE_H_
19 #define _AO_STORAGE_H_
20
21 /*
22  * Storage interface, provided by one of the eeprom or flash
23  * drivers
24  */
25
26 #ifndef ao_storage_pos_t
27 #define ao_storage_pos_t uint32_t
28 #endif
29
30 typedef ao_storage_pos_t ao_pos_t;
31
32 /* Total bytes of available storage */
33 extern __pdata ao_pos_t ao_storage_total;
34
35 /* Block size - device is erased in these units. At least 256 bytes */
36 extern __pdata ao_pos_t ao_storage_block;
37
38 #ifndef USE_STORAGE_CONFIG
39 #define USE_STORAGE_CONFIG 1
40 #endif
41
42 #if USE_STORAGE_CONFIG
43 /* Byte offset of config block. Will be ao_storage_block bytes long */
44 extern __pdata ao_pos_t ao_storage_config;
45
46 #define ao_storage_log_max      ao_storage_config
47 #else
48 #define ao_storage_log_max      ao_storage_total
49 #endif
50
51 /* Storage unit size - device reads and writes must be within blocks of this size. Usually 256 bytes. */
52 extern __pdata uint16_t ao_storage_unit;
53
54 #define AO_STORAGE_ERASE_LOG    (ao_storage_config + AO_CONFIG_MAX_SIZE)
55
56 /* Initialize above values. Can only be called once the OS is running */
57 void
58 ao_storage_setup(void) __reentrant;
59
60 /* Write data. Returns 0 on failure, 1 on success */
61 uint8_t
62 ao_storage_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant;
63
64 /* Read data. Returns 0 on failure, 1 on success */
65 uint8_t
66 ao_storage_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant;
67
68 /* Erase a block of storage. This always clears ao_storage_block bytes */
69 uint8_t
70 ao_storage_erase(ao_pos_t pos) __reentrant;
71
72 /* Flush any pending writes to stable storage */
73 void
74 ao_storage_flush(void) __reentrant;
75
76 /* Initialize the storage code */
77 void
78 ao_storage_init(void);
79
80 /*
81  * Low-level functions wrapped by ao_storage.c
82  */
83
84 /* Read data within a storage unit */
85 uint8_t
86 ao_storage_device_read(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant;
87
88 /* Write data within a storage unit */
89 uint8_t
90 ao_storage_device_write(ao_pos_t pos, __xdata void *buf, uint16_t len) __reentrant;
91
92 /* Initialize low-level device bits */
93 void
94 ao_storage_device_init(void);
95
96 /* Print out information about flash chips */
97 void
98 ao_storage_device_info(void) __reentrant;
99
100 #endif /* _AO_STORAGE_H_ */