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