ChibiOS/HAL  6.1.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  */
142 #define _base_flash_methods_alone \
143  /* Get flash device attributes.*/ \
144  const flash_descriptor_t * (*get_descriptor)(void *instance); \
145  /* Read operation.*/ \
146  flash_error_t (*read)(void *instance, flash_offset_t offset, \
147  size_t n, uint8_t *rp); \
148  /* Program operation.*/ \
149  flash_error_t (*program)(void *instance, flash_offset_t offset, \
150  size_t n, const uint8_t *pp); \
151  /* Erase whole flash device.*/ \
152  flash_error_t (*start_erase_all)(void *instance); \
153  /* Erase single sector.*/ \
154  flash_error_t (*start_erase_sector)(void *instance, \
155  flash_sector_t sector); \
156  flash_error_t (*query_erase)(void *instance, uint32_t *wait_time); \
157  /* Verify erase single sector.*/ \
158  flash_error_t (*verify_erase)(void *instance, flash_sector_t sector);
159 
160 /**
161  * @brief @p BaseFlash specific methods with inherited ones.
162  */
163 #define _base_flash_methods \
164  _base_object_methods \
165  _base_flash_methods_alone
166 
167 /**
168  * @brief @p BaseFlash virtual methods table.
169  */
170 struct BaseFlashVMT {
172 };
173 
174 /**
175  * @brief @p BaseFlash specific data.
176  */
177 #define _base_flash_data \
178  _base_object_data \
179  /* Driver state.*/ \
180  flash_state_t state;
181 
182 /**
183  * @extends BaseObject
184  *
185  * @brief Base flash class.
186  */
187 typedef struct {
188  /** @brief Virtual Methods Table.*/
189  const struct BaseFlashVMT *vmt;
191 } BaseFlash;
192 
193 /*===========================================================================*/
194 /* Driver macros. */
195 /*===========================================================================*/
196 
197 /**
198  * @name Macro Functions (BaseFlash)
199  * @{
200  */
201 /**
202  * @brief Instance getter.
203  * @details This special method is used to get the instance of this class
204  * object from a derived class.
205  */
206 #define getBaseFlash(ip) ((BaseFlash *)&(ip)->vmt)
207 
208 /**
209  * @brief Gets the flash descriptor structure.
210  *
211  * @param[in] ip pointer to a @p BaseFlash or derived class
212  * @return A flash device descriptor.
213  *
214  * @api
215  */
216 #define flashGetDescriptor(ip) \
217  (ip)->vmt->get_descriptor(ip)
218 
219 /**
220  * @brief Read operation.
221  *
222  * @param[in] ip pointer to a @p BaseFlash or derived class
223  * @param[in] offset flash offset
224  * @param[in] n number of bytes to be read
225  * @param[out] rp pointer to the data buffer
226  * @return An error code.
227  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
228  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
229  * @retval FLASH_ERROR_READ if the read operation failed.
230  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
231  *
232  * @api
233  */
234 #define flashRead(ip, offset, n, rp) \
235  (ip)->vmt->read(ip, offset, n, rp)
236 
237 /**
238  * @brief Program operation.
239  *
240  * @param[in] ip pointer to a @p BaseFlash or derived class
241  * @param[in] offset flash offset
242  * @param[in] n number of bytes to be programmed
243  * @param[in] pp pointer to the data buffer
244  * @return An error code.
245  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
246  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
247  * @retval FLASH_ERROR_PROGRAM if the program operation failed.
248  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
249  *
250  * @api
251  */
252 #define flashProgram(ip, offset, n, pp) \
253  (ip)->vmt->program(ip, offset, n, pp)
254 
255 /**
256  * @brief Starts a whole-device erase operation.
257  *
258  * @param[in] ip pointer to a @p BaseFlash or derived class
259  * @return An error code.
260  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
261  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
262  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
263  *
264  * @api
265  */
266 #define flashStartEraseAll(ip) \
267  (ip)->vmt->start_erase_all(ip)
268 
269 /**
270  * @brief Starts an sector erase operation.
271  *
272  * @param[in] ip pointer to a @p BaseFlash or derived class
273  * @param[in] sector sector to be erased
274  * @return An error code.
275  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
276  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
277  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
278  *
279  * @api
280  */
281 #define flashStartEraseSector(ip, sector) \
282  (ip)->vmt->start_erase_sector(ip, sector)
283 
284 /**
285  * @brief Queries the driver for erase operation progress.
286  *
287  * @param[in] ip pointer to a @p BaseFlash or derived class
288  * @param[out] msec recommended time, in milliseconds, that what should be
289  * spent before calling this function again, can be @p NULL
290  * @return An error code.
291  * @retval FLASH_NO_ERROR if there is no erase operation in progress.
292  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
293  * @retval FLASH_ERROR_ERASE if the erase operation failed.
294  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
295  *
296  * @api
297  */
298 #define flashQueryErase(ip, msec) \
299  (ip)->vmt->query_erase(ip, msec)
300 
301 /**
302  * @brief Returns the erase state of a sector.
303  *
304  * @param[in] ip pointer to a @p BaseFlash or derived class
305  * @param[in] sector sector to be verified
306  * @return An error code.
307  * @retval FLASH_NO_ERROR if the sector is erased.
308  * @retval FLASH_BUSY_ERASING if there is an erase operation in progress.
309  * @retval FLASH_ERROR_VERIFY if the verify operation failed.
310  * @retval FLASH_ERROR_HW_FAILURE if access to the memory failed.
311  *
312  * @api
313  */
314 #define flashVerifyErase(ip, sector) \
315  (ip)->vmt->verify_erase(ip, sector)
316 /** @} */
317 
318 /*===========================================================================*/
319 /* External declarations. */
320 /*===========================================================================*/
321 
322 #ifdef __cplusplus
323 extern "C" {
324 #endif
327  uint32_t flashGetSectorSize(BaseFlash *devp, flash_sector_t sector);
328 #ifdef __cplusplus
329 }
330 #endif
331 
332 #endif /* HAL_FLASH_H */
333 
334 /** @} */
BaseFlash virtual methods table.
Definition: hal_flash.h:170
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:177
flash_offset_t offset
Sector offset.
Definition: hal_flash.h:97
Base flash class.
Definition: hal_flash.h:187
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:163
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:189