ChibiOS/HAL  7.0.3
hal_persistent.h
Go to the documentation of this file.
1 /*
2  ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 /**
18  * @file hal_persistent.h
19  * @brief Generic persistent storage class header.
20  *
21  * @addtogroup HAL_PERSISTENT
22  * @details This module define an abstract interface for generic persistent
23  * storage. Such storage has a fixed size and can be read and
24  * written.
25  * @{
26  */
27 
28 #ifndef HAL_PERSISTENT_H
29 #define HAL_PERSISTENT_H
30 
31 /*===========================================================================*/
32 /* Driver constants. */
33 /*===========================================================================*/
34 
35 /*===========================================================================*/
36 /* Driver pre-compile time settings. */
37 /*===========================================================================*/
38 
39 /*===========================================================================*/
40 /* Derived constants and error checks. */
41 /*===========================================================================*/
42 
43 /*===========================================================================*/
44 /* Driver data structures and types. */
45 /*===========================================================================*/
46 
47 /**
48  * @brief Type of a persistent storage error code.
49  * @note Code values are kept equal to the equivalent codes in the flash
50  * interface, this is intentional.
51  */
52 typedef enum {
53  PS_NO_ERROR = 0, /* No error. */
54  PS_ERROR_READ = 2, /* ECC or other error during read operation.*/
55  PS_ERROR_WRITE= 3, /* Program operation failed. */
56  PS_ERROR_VERIFY = 5, /* Verify operation failed. */
57  PS_ERROR_HW_FAILURE = 6 /* Controller or communication error. */
58 } ps_error_t;
59 
60 /**
61  * @brief Type of a persistent storage offset.
62  */
63 typedef uint32_t ps_offset_t;
64 
65 /**
66  * @brief @p BasePersistentStorage specific methods.
67  */
68 #define _base_pers_storage_methods_alone \
69  /* Storage size.*/ \
70  size_t (*getsize)(void *instance); \
71  /* Read operation.*/ \
72  ps_error_t (*read)(void *instance, ps_offset_t offset, \
73  size_t n, uint8_t *rp); \
74  /* Write operation.*/ \
75  ps_error_t (*write)(void *instance, ps_offset_t offset, \
76  size_t n, const uint8_t *wp);
77 
78 /**
79  * @brief @p BasePersistentStorage specific methods with inherited ones.
80  */
81 #define _base_pers_storage_methods \
82  _base_object_methods \
83  _base_pers_storage_methods_alone
84 
85 /**
86  * @brief @p BasePersistentStorage virtual methods table.
87  */
90 };
91 
92 /**
93  * @brief @p BasePersistentStorage specific data.
94  */
95 #define _base_persistent_storage_data \
96  _base_object_data
97 
98 /**
99  * @extends BaseObject
100  *
101  * @brief Base persistent storage class.
102  */
103 typedef struct {
104  /** @brief Virtual Methods Table.*/
108 
109 /*===========================================================================*/
110 /* Driver macros. */
111 /*===========================================================================*/
112 
113 /**
114  * @name Macro Functions (BasePersistentStorage)
115  * @{
116  */
117 /**
118  * @brief Instance getter.
119  * @details This special method is used to get the instance of this class
120  * object from a derived class.
121  */
122 #define getBasePersistentStorage(ip) ((BasePersistentStorage *)&(ip)->vmt)
123 
124 /**
125  * @brief Get storage size.
126  *
127  * @param[in] ip pointer to a @p BasePersistentStorage or derived class
128  * @return The storage size in bytes.
129  *
130  * @api
131  */
132 #define psGetStorageSize(ip) \
133  (ip)->vmt->getsize(ip)
134 
135 /**
136  * @brief Read operation.
137  *
138  * @param[in] ip pointer to a @p BasePersistentStorage or derived class
139  * @param[in] offset persistent storage offset
140  * @param[in] n number of bytes to be read
141  * @param[out] rp pointer to the data buffer
142  * @return An error code.
143  * @retval PS_NO_ERROR if there is no erase operation in progress.
144  * @retval PS_ERROR_READ if the read operation failed.
145  * @retval PS_ERROR_HW_FAILURE if access to the memory failed.
146  *
147  * @api
148  */
149 #define psRead(ip, offset, n, rp) \
150  (ip)->vmt->read(ip, offset, n, rp)
151 
152 /**
153  * @brief Write operation.
154  *
155  * @param[in] ip pointer to a @p BasePersistentStorage or derived class
156  * @param[in] offset persistent storage offset
157  * @param[in] n number of bytes to be written
158  * @param[in] wp pointer to the data buffer
159  * @return An error code.
160  * @retval PS_NO_ERROR if there is no erase operation in progress.
161  * @retval PS_ERROR_WRITE if the write operation failed.
162  * @retval PS_ERROR_HW_FAILURE if access to the memory failed.
163  *
164  * @api
165  */
166 #define psWrite(ip, offset, n, wp) \
167  (ip)->vmt->write(ip, offset, n, wp)
168 /** @} */
169 
170 /*===========================================================================*/
171 /* External declarations. */
172 /*===========================================================================*/
173 
174 #ifdef __cplusplus
175 extern "C" {
176 #endif
177 
178 #ifdef __cplusplus
179 }
180 #endif
181 
182 #endif /* HAL_PERSISTENT_H */
183 
184 /** @} */
#define _base_pers_storage_methods
BasePersistentStorage specific methods with inherited ones.
ps_error_t
Type of a persistent storage error code.
Base persistent storage class.
BasePersistentStorage virtual methods table.
uint32_t ps_offset_t
Type of a persistent storage offset.
#define _base_persistent_storage_data
BasePersistentStorage specific data.
const struct BasePersistentStorageVMT * vmt
Virtual Methods Table.