hal_flash.h 11 KB

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