ChibiOS  21.6.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 /**
37  * @brief Defines one as the erased bit state.
38  */
39 #define FLASH_ATTR_ERASED_IS_ONE 0x00000001U
40 /**
41  * @brief The memory is accessible in a memory mapped mode.
42  */
43 #define FLASH_ATTR_MEMORY_MAPPED 0x00000002U
44 /**
45  * @brief Programmed pages can be programmed again.
46  * @note This is incompatible and alternative to @p FLASH_ATTR_ECC_CAPABLE.
47  */
48 #define FLASH_ATTR_REWRITABLE 0x00000004U
49 /**
50  * @brief The memory is protected by an ECC mechanism.
51  * @note This usually puts restrictions on the program operations.
52  * - Program operations can only happen at offsets aligned to
53  * write page boundaries.
54  * - The programmed data size must be a multiple of the write
55  * page size.
56  * - Programmed pages cannot be re-programmed.
57  * .
58  */
59 #define FLASH_ATTR_ECC_CAPABLE 0x00000008U
60 /**
61  * @brief The device is able to overwrite zero to a line.
62  * @note This attribute is only meaningful for those devices that support
63  * ECC, so also @p FLASH_ATTR_ECC_CAPABLE must be specified.
64  */
65 #define FLASH_ATTR_ECC_ZERO_LINE_CAPABLE 0x00000010U
66 /**
67  * @brief The device is able to suspend erase operations.
68  */
69 #define FLASH_ATTR_SUSPEND_ERASE_CAPABLE 0x00000020U
70 /** @} */
71 
72 /*===========================================================================*/
73 /* Driver pre-compile time settings. */
74 /*===========================================================================*/
75 
76 /*===========================================================================*/
77 /* Derived constants and error checks. */
78 /*===========================================================================*/
79 
80 /*===========================================================================*/
81 /* Driver data structures and types. */
82 /*===========================================================================*/
83 
84 /**
85  * @brief Driver state machine possible states.
86  */
87 typedef enum {
88  FLASH_UNINIT = 0,
89  FLASH_STOP = 1,
90  FLASH_READY = 2,
91  FLASH_READ = 3,
92  FLASH_PGM = 4,
93  FLASH_ERASE = 5
95 
96 /**
97  * @brief Type of a flash error code.
98  */
99 typedef enum {
100  FLASH_NO_ERROR = 0, /* No error. */
101  FLASH_BUSY_ERASING = 1, /* Erase operation in progress. */
102  FLASH_ERROR_READ = 2, /* ECC or other error during read operation.*/
103  FLASH_ERROR_PROGRAM = 3, /* Program operation failed. */
104  FLASH_ERROR_ERASE = 4, /* Erase operation failed. */
105  FLASH_ERROR_VERIFY = 5, /* Verify operation failed. */
106  FLASH_ERROR_HW_FAILURE = 6, /* Controller or communication error. */
107  FLASH_ERROR_UNIMPLEMENTED = 7 /* Unimplemented functionality. */
108 } flash_error_t;
109 
110 /**
111  * @brief Type of a flash offset.
112  */
113 typedef uint32_t flash_offset_t;
114 
115 /**
116  * @brief Type of a flash sector number.
117  */
118 typedef uint32_t flash_sector_t;
119 
120 /**
121  * @brief Flash sector descriptor.
122  */
123 typedef struct {
124  /**
125  * @brief Sector offset.
126  */
128  /**
129  * @brief Sector size.
130  */
131  uint32_t size;
133 
134 /**
135  * @brief Type of a flash device descriptor.
136  */
137 typedef struct {
138  /**
139  * @brief Device_attributes.
140  */
141  uint32_t attributes;
142  /**
143  * @brief Size of write page.
144  */
145  uint32_t page_size;
146  /**
147  * @brief Number of sectors in the device.
148  */
150  /**
151  * @brief List of sectors for devices with non-uniform sector sizes.
152  * @note If @p NULL then the device has uniform sectors size equal
153  * to @p sector_size.
154  */
156  /**
157  * @brief Size of sectors for devices with uniform sector size.
158  * @note If zero then the device has non uniform sectors described
159  * by the @p sectors array.
160  */
161  uint32_t sectors_size;
162  /**
163  * @brief Flash address if memory mapped or zero.
164  * @note Conventionally, non memory mapped devices have address @p NULL.
165  */
166  uint8_t *address;
167  /**
168  * @brief Flash size.
169  */
170  uint32_t size;
172 
173 /**
174  * @brief @p BaseFlash specific methods.
175  */
176 #define _base_flash_methods_alone \
177  /* Get flash device attributes.*/ \
178  const flash_descriptor_t * (*get_descriptor)(void *instance); \
179  /* Read operation.*/ \
180  flash_error_t (*read)(void *instance, flash_offset_t offset, \
181  size_t n, uint8_t *rp); \
182  /* Program operation.*/ \
183  flash_error_t (*program)(void *instance, flash_offset_t offset, \
184  size_t n, const uint8_t *pp); \
185  /* Erase whole flash device.*/ \
186  flash_error_t (*start_erase_all)(void *instance); \
187  /* Erase single sector.*/ \
188  flash_error_t (*start_erase_sector)(void *instance, \
189  flash_sector_t sector); \
190  flash_error_t (*query_erase)(void *instance, uint32_t *wait_time); \
191  /* Verify erase single sector.*/ \
192  flash_error_t (*verify_erase)(void *instance, flash_sector_t sector);
193 
194 /**
195  * @brief @p BaseFlash specific methods with inherited ones.
196  */
197 #define _base_flash_methods \
198  _base_object_methods \
199  _base_flash_methods_alone
200 
201 /**
202  * @brief @p BaseFlash virtual methods table.
203  */
204 struct BaseFlashVMT {
206 };
207 
208 /**
209  * @brief @p BaseFlash specific data.
210  */
211 #define _base_flash_data \
212  _base_object_data \
213  /* Driver state.*/ \
214  flash_state_t state;
215 
216 /**
217  * @extends BaseObject
218  *
219  * @brief Base flash class.
220  */
221 typedef struct {
222  /** @brief Virtual Methods Table.*/
223  const struct BaseFlashVMT *vmt;
225 } BaseFlash;
226 
227 /*===========================================================================*/
228 /* Driver macros. */
229 /*===========================================================================*/
230 
231 /**
232  * @name Macro Functions (BaseFlash)
233  * @{
234  */
235 /**
236  * @brief Instance getter.
237  * @details This special method is used to get the instance of this class
238  * object from a derived class.
239  */
240 #define getBaseFlash(ip) ((BaseFlash *)&(ip)->vmt)
241 
242 /**
243  * @brief Gets the flash descriptor structure.
244  *
245  * @param[in] ip pointer to a @p BaseFlash or derived class
246  * @return A flash device descriptor.
247  *
248  * @api
249  */
250 #define flashGetDescriptor(ip) \
251  (ip)->vmt->get_descriptor(ip)
252 
253 /**
254  * @brief Read operation.
255  *
256  * @param[in] ip pointer to a @p BaseFlash or derived class
257  * @param[in] offset flash offset
258  * @param[in] n number of bytes to be read
259  * @param[out] rp pointer to the data buffer
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_READ if the read operation failed.
264  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
265  *
266  * @api
267  */
268 #define flashRead(ip, offset, n, rp) \
269  (ip)->vmt->read(ip, offset, n, rp)
270 
271 /**
272  * @brief Program operation.
273  *
274  * @param[in] ip pointer to a @p BaseFlash or derived class
275  * @param[in] offset flash offset
276  * @param[in] n number of bytes to be programmed
277  * @param[in] pp pointer to the data buffer
278  * @return An error code.
279  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
280  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
281  * @retval FLASH_ERROR_PROGRAM if the program operation failed.
282  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
283  *
284  * @api
285  */
286 #define flashProgram(ip, offset, n, pp) \
287  (ip)->vmt->program(ip, offset, n, pp)
288 
289 /**
290  * @brief Starts a whole-device erase operation.
291  *
292  * @param[in] ip pointer to a @p BaseFlash or derived class
293  * @return An error code.
294  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
295  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
296  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
297  *
298  * @api
299  */
300 #define flashStartEraseAll(ip) \
301  (ip)->vmt->start_erase_all(ip)
302 
303 /**
304  * @brief Starts an sector erase operation.
305  *
306  * @param[in] ip pointer to a @p BaseFlash or derived class
307  * @param[in] sector sector to be erased
308  * @return An error code.
309  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
310  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
311  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
312  *
313  * @api
314  */
315 #define flashStartEraseSector(ip, sector) \
316  (ip)->vmt->start_erase_sector(ip, sector)
317 
318 /**
319  * @brief Queries the driver for erase operation progress.
320  *
321  * @param[in] ip pointer to a @p BaseFlash or derived class
322  * @param[out] msec recommended time, in milliseconds, that
323  * should be spent before calling this
324  * function again, can be @p NULL
325  * @return An error code.
326  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
327  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
328  * @retval FLASH_ERROR_ERASE if the erase operation failed.
329  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
330  *
331  * @api
332  */
333 #define flashQueryErase(ip, msec) \
334  (ip)->vmt->query_erase(ip, msec)
335 
336 /**
337  * @brief Returns the erase state of a sector.
338  *
339  * @param[in] ip pointer to a @p BaseFlash or derived class
340  * @param[in] sector sector to be verified
341  * @return An error code.
342  * @retval FLASH_NO_ERROR if the sector is erased.
343  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
344  * @retval FLASH_ERROR_VERIFY if the verify operation failed.
345  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
346  *
347  * @api
348  */
349 #define flashVerifyErase(ip, sector) \
350  (ip)->vmt->verify_erase(ip, sector)
351 /** @} */
352 
353 /*===========================================================================*/
354 /* External declarations. */
355 /*===========================================================================*/
356 
357 #ifdef __cplusplus
358 extern "C" {
359 #endif
362  uint32_t flashGetSectorSize(BaseFlash *devp, flash_sector_t sector);
363 #ifdef __cplusplus
364 }
365 #endif
366 
367 #endif /* HAL_FLASH_H */
368 
369 /** @} */
_base_flash_methods
#define _base_flash_methods
BaseFlash specific methods with inherited ones.
Definition: hal_flash.h:197
flash_state_t
flash_state_t
Driver state machine possible states.
Definition: hal_flash.h:87
flash_descriptor_t
Type of a flash device descriptor.
Definition: hal_flash.h:137
flashGetSectorSize
uint32_t flashGetSectorSize(BaseFlash *devp, flash_sector_t sector)
Returns the size of a sector.
Definition: hal_flash.c:109
flash_descriptor_t::size
uint32_t size
Flash size.
Definition: hal_flash.h:170
flash_descriptor_t::attributes
uint32_t attributes
Device_attributes.
Definition: hal_flash.h:141
flashGetSectorOffset
flash_offset_t flashGetSectorOffset(BaseFlash *devp, flash_sector_t sector)
Returns the offset of a sector.
Definition: hal_flash.c:84
flash_sector_t
uint32_t flash_sector_t
Type of a flash sector number.
Definition: hal_flash.h:118
flash_descriptor_t::sectors_size
uint32_t sectors_size
Size of sectors for devices with uniform sector size.
Definition: hal_flash.h:161
flash_descriptor_t::sectors
const flash_sector_descriptor_t * sectors
List of sectors for devices with non-uniform sector sizes.
Definition: hal_flash.h:155
_base_flash_data
#define _base_flash_data
BaseFlash specific data.
Definition: hal_flash.h:211
BaseFlash
Base flash class.
Definition: hal_flash.h:221
BaseFlash::vmt
const struct BaseFlashVMT * vmt
Virtual Methods Table.
Definition: hal_flash.h:223
flash_descriptor_t::sectors_count
flash_sector_t sectors_count
Number of sectors in the device.
Definition: hal_flash.h:149
flash_descriptor_t::page_size
uint32_t page_size
Size of write page.
Definition: hal_flash.h:145
flashWaitErase
flash_error_t flashWaitErase(BaseFlash *devp)
Waits until the current erase operation is finished.
Definition: hal_flash.c:59
flash_sector_descriptor_t::size
uint32_t size
Sector size.
Definition: hal_flash.h:131
flash_descriptor_t::address
uint8_t * address
Flash address if memory mapped or zero.
Definition: hal_flash.h:166
flash_error_t
flash_error_t
Type of a flash error code.
Definition: hal_flash.h:99
flash_sector_descriptor_t
Flash sector descriptor.
Definition: hal_flash.h:123
flash_offset_t
uint32_t flash_offset_t
Type of a flash offset.
Definition: hal_flash.h:113
flash_sector_descriptor_t::offset
flash_offset_t offset
Sector offset.
Definition: hal_flash.h:127
BaseFlashVMT
BaseFlash virtual methods table.
Definition: hal_flash.h:204