ChibiOS  0.0.0
hal_flash.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_flash.h
19  * @brief Generic flash driver class header.
20  *
21  * @addtogroup HAL_FLASH
22  * @{
23  */
24 
25 #ifndef HAL_FLASH_H
26 #define HAL_FLASH_H
27 
28 /*===========================================================================*/
29 /* Driver constants. */
30 /*===========================================================================*/
31 
32 /**
33  * @name Flash attributes
34  * @{
35  */
36 #define FLASH_ATTR_ERASED_IS_ONE 0x00000001
37 #define FLASH_ATTR_MEMORY_MAPPED 0x00000002
38 #define FLASH_ATTR_REWRITABLE 0x00000004
39 #define FLASH_ATTR_READ_ECC_CAPABLE 0x00000008
40 #define FLASH_ATTR_SUSPEND_ERASE_CAPABLE 0x00000010
41 /** @} */
42 
43 /*===========================================================================*/
44 /* Driver pre-compile time settings. */
45 /*===========================================================================*/
46 
47 /*===========================================================================*/
48 /* Derived constants and error checks. */
49 /*===========================================================================*/
50 
51 /*===========================================================================*/
52 /* Driver data structures and types. */
53 /*===========================================================================*/
54 
55 /**
56  * @brief Driver state machine possible states.
57  */
58 typedef enum {
59  FLASH_UNINIT = 0,
60  FLASH_STOP = 1,
61  FLASH_READY = 2,
62  FLASH_READ = 3,
63  FLASH_PGM = 4,
64  FLASH_ERASE = 5
66 
67 /**
68  * @brief Type of a flash error code.
69  */
70 typedef enum {
71  FLASH_NO_ERROR = 0, /* No error. */
72  FLASH_BUSY_ERASING = 1, /* Erase operation in progress. */
73  FLASH_ERROR_READ = 2, /* ECC or other error during read operation.*/
74  FLASH_ERROR_PROGRAM = 3, /* Program operation failed. */
75  FLASH_ERROR_ERASE = 4, /* Erase operation failed. */
76  FLASH_ERROR_VERIFY = 5, /* Verify operation failed. */
77  FLASH_ERROR_HW_FAILURE = 6 /* Controller or communication error. */
79 
80 /**
81  * @brief Type of a flash offset.
82  */
83 typedef uint32_t flash_offset_t;
84 
85 /**
86  * @brief Type of a flash sector number.
87  */
88 typedef uint32_t flash_sector_t;
89 
90 /**
91  * @brief Flash sector descriptor.
92  */
93 typedef struct {
94  /**
95  * @brief Sector offset.
96  */
98  /**
99  * @brief Sector size.
100  */
101  uint32_t size;
103 
104 /**
105  * @brief Type of a flash device descriptor.
106  */
107 typedef struct {
108  /**
109  * @brief Device_attributes.
110  */
111  uint32_t attributes;
112  /**
113  * @brief Size of write page.
114  */
115  uint32_t page_size;
116  /**
117  * @brief Number of sectors in the device.
118  */
120  /**
121  * @brief List of sectors for devices with non-uniform sector sizes.
122  * @note If @p NULL then the device has uniform sectors size equal
123  * to @p sector_size.
124  */
126  /**
127  * @brief Size of sectors for devices with uniform sector size.
128  * @note If zero then the device has non uniform sectors described
129  * by the @p sectors array.
130  */
131  uint32_t sectors_size;
132  /**
133  * @brief Flash address if memory mapped or zero.
134  * @note Conventionally, non memory mapped devices have address zero.
135  */
138 
139 /**
140  * @brief @p BaseFlash specific methods.
141  * @note No methods so far, just a common ancestor interface.
142  */
143 #define _base_flash_methods_alone \
144  /* Get flash device attributes.*/ \
145  const flash_descriptor_t * (*get_descriptor)(void *instance); \
146  /* Read operation.*/ \
147  flash_error_t (*read)(void *instance, flash_offset_t offset, \
148  size_t n, uint8_t *rp); \
149  /* Program operation.*/ \
150  flash_error_t (*program)(void *instance, flash_offset_t offset, \
151  size_t n, const uint8_t *pp); \
152  /* Erase whole flash device.*/ \
153  flash_error_t (*start_erase_all)(void *instance); \
154  /* Erase single sector.*/ \
155  flash_error_t (*start_erase_sector)(void *instance, \
156  flash_sector_t sector); \
157  flash_error_t (*query_erase)(void *instance, uint32_t *wait_time); \
158  /* Verify erase single sector.*/ \
159  flash_error_t (*verify_erase)(void *instance, flash_sector_t sector);
160 
161 /**
162  * @brief @p BaseFlash specific methods with inherited ones.
163  */
164 #define _base_flash_methods \
165  _base_object_methods \
166  _base_flash_methods_alone
167 
168 /**
169  * @brief @p BaseFlash virtual methods table.
170  */
171 struct BaseFlashVMT {
173 };
174 
175 /**
176  * @brief @p BaseFlash specific data.
177  */
178 #define _base_flash_data \
179  _base_object_data \
180  /* Driver state.*/ \
181  flash_state_t state;
182 
183 /**
184  * @extends BaseObject
185  *
186  * @brief Base flash class.
187  */
188 typedef struct {
189  /** @brief Virtual Methods Table.*/
190  const struct BaseFlashVMT *vmt;
192 } BaseFlash;
193 
194 /*===========================================================================*/
195 /* Driver macros. */
196 /*===========================================================================*/
197 
198 /**
199  * @name Macro Functions (BaseFlash)
200  * @{
201  */
202 /**
203  * @brief Instance getter.
204  * @details This special method is used to get the instance of this class
205  * object from a derived class.
206  */
207 #define getBaseFlash(ip) ((BaseFlash *)&(ip)->vmt)
208 
209 /**
210  * @brief Sensors get axes number.
211  *
212  * @param[in] ip pointer to a @p BaseFlash or derived class
213  * @return A flash device descriptor.
214  *
215  * @api
216  */
217 #define flashGetDescriptor(ip) \
218  (ip)->vmt->get_descriptor(ip)
219 
220 /**
221  * @brief Read operation.
222  *
223  * @param[in] ip pointer to a @p BaseFlash or derived class
224  * @param[in] offset flash offset
225  * @param[in] n number of bytes to be read
226  * @param[out] rp pointer to the data buffer
227  * @return An error code.
228  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
229  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
230  * @retval FLASH_ERROR_READ if the read operation failed.
231  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
232  *
233  * @api
234  */
235 #define flashRead(ip, offset, n, rp) \
236  (ip)->vmt->read(ip, offset, n, rp)
237 
238 /**
239  * @brief Program operation.
240  *
241  * @param[in] ip pointer to a @p BaseFlash or derived class
242  * @param[in] offset flash offset
243  * @param[in] n number of bytes to be programmed
244  * @param[in] pp pointer to the data buffer
245  * @return An error code.
246  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
247  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
248  * @retval FLASH_ERROR_PROGRAM if the program operation failed.
249  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
250  *
251  * @api
252  */
253 #define flashProgram(ip, offset, n, pp) \
254  (ip)->vmt->program(ip, offset, n, pp)
255 
256 /**
257  * @brief Starts a whole-device erase operation.
258  *
259  * @param[in] ip pointer to a @p BaseFlash or derived class
260  * @return An error code.
261  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
262  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
263  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
264  *
265  * @api
266  */
267 #define flashStartEraseAll(ip) \
268  (ip)->vmt->start_erase_all(ip)
269 
270 /**
271  * @brief Starts an sector erase operation.
272  *
273  * @param[in] ip pointer to a @p BaseFlash or derived class
274  * @param[in] sector sector to be erased
275  * @return An error code.
276  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
277  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
278  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
279  *
280  * @api
281  */
282 #define flashStartEraseSector(ip, sector) \
283  (ip)->vmt->start_erase_sector(ip, sector)
284 
285 /**
286  * @brief Queries the driver for erase operation progress.
287  *
288  * @param[in] ip pointer to a @p BaseFlash or derived class
289  * @param[out] msec recommended time, in milliseconds, that what should be
290  * spent before calling this function again, can be @p NULL
291  * @return An error code.
292  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
293  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
294  * @retval FLASH_ERROR_ERASE if the erase operation failed.
295  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
296  *
297  * @api
298  */
299 #define flashQueryErase(ip, msec) \
300  (ip)->vmt->query_erase(ip, msec)
301 
302 /**
303  * @brief Returns the erase state of a sector.
304  *
305  * @param[in] ip pointer to a @p BaseFlash or derived class
306  * @param[in] sector sector to be verified
307  * @return An error code.
308  * @retval FLASH_NO_ERROR if the sector is erased.
309  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
310  * @retval FLASH_ERROR_VERIFY if the verify operation failed.
311  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
312  *
313  * @api
314  */
315 #define flashVerifyErase(ip, sector) \
316  (ip)->vmt->verify_erase(ip, sector)
317 /** @} */
318 
319 /*===========================================================================*/
320 /* External declarations. */
321 /*===========================================================================*/
322 
323 #ifdef __cplusplus
324 extern "C" {
325 #endif
328  uint32_t flashGetSectorSize(BaseFlash *devp, flash_sector_t sector);
329 #ifdef __cplusplus
330 }
331 #endif
332 
333 #endif /* HAL_FLASH_H */
334 
335 /** @} */
BaseFlash virtual methods table.
Definition: hal_flash.h:171
uint32_t flash_sector_t
Type of a flash sector number.
Definition: hal_flash.h:88
flash_state_t
Driver state machine possible states.
Definition: hal_flash.h:58
uint32_t size
Sector size.
Definition: hal_flash.h:101
uint32_t attributes
Device_attributes.
Definition: hal_flash.h:111
#define _base_flash_data
BaseFlash specific data.
Definition: hal_flash.h:178
flash_offset_t offset
Sector offset.
Definition: hal_flash.h:97
Base flash class.
Definition: hal_flash.h:188
uint32_t sectors_size
Size of sectors for devices with uniform sector size.
Definition: hal_flash.h:131
Type of a flash device descriptor.
Definition: hal_flash.h:107
uint32_t flashGetSectorSize(BaseFlash *devp, flash_sector_t sector)
Returns the size of a sector.
Definition: hal_flash.c:109
flash_error_t
Type of a flash error code.
Definition: hal_flash.h:70
flash_error_t flashWaitErase(BaseFlash *devp)
Waits until the current erase operation is finished.
Definition: hal_flash.c:59
uint32_t page_size
Size of write page.
Definition: hal_flash.h:115
uint32_t flash_offset_t
Type of a flash offset.
Definition: hal_flash.h:83
Flash sector descriptor.
Definition: hal_flash.h:93
#define _base_flash_methods
BaseFlash specific methods with inherited ones.
Definition: hal_flash.h:164
flash_offset_t flashGetSectorOffset(BaseFlash *devp, flash_sector_t sector)
Returns the offset of a sector.
Definition: hal_flash.c:84
flash_offset_t address
Flash address if memory mapped or zero.
Definition: hal_flash.h:136
const flash_sector_descriptor_t * sectors
List of sectors for devices with non-uniform sector sizes.
Definition: hal_flash.h:125
flash_sector_t sectors_count
Number of sectors in the device.
Definition: hal_flash.h:119
const struct BaseFlashVMT * vmt
Virtual Methods Table.
Definition: hal_flash.h:190