hal_persistent.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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_persistent.h
  15. * @brief Generic persistent storage class header.
  16. *
  17. * @addtogroup HAL_PERSISTENT
  18. * @details This module define an abstract interface for generic persistent
  19. * storage. Such storage has a fixed size and can be read and
  20. * written.
  21. * @{
  22. */
  23. #ifndef HAL_PERSISTENT_H
  24. #define HAL_PERSISTENT_H
  25. /*===========================================================================*/
  26. /* Driver constants. */
  27. /*===========================================================================*/
  28. /*===========================================================================*/
  29. /* Driver pre-compile time settings. */
  30. /*===========================================================================*/
  31. /*===========================================================================*/
  32. /* Derived constants and error checks. */
  33. /*===========================================================================*/
  34. /*===========================================================================*/
  35. /* Driver data structures and types. */
  36. /*===========================================================================*/
  37. /**
  38. * @brief Type of a persistent storage error code.
  39. * @note Code values are kept equal to the equivalent codes in the flash
  40. * interface, this is intentional.
  41. */
  42. typedef enum {
  43. PS_NO_ERROR = 0, /* No error. */
  44. PS_ERROR_READ = 2, /* ECC or other error during read operation.*/
  45. PS_ERROR_WRITE= 3, /* Program operation failed. */
  46. PS_ERROR_VERIFY = 5, /* Verify operation failed. */
  47. PS_ERROR_HW_FAILURE = 6 /* Controller or communication error. */
  48. } ps_error_t;
  49. /**
  50. * @brief Type of a persistent storage offset.
  51. */
  52. typedef uint32_t ps_offset_t;
  53. /**
  54. * @brief @p BasePersistentStorage specific methods.
  55. */
  56. #define _base_pers_storage_methods_alone \
  57. /* Storage size.*/ \
  58. size_t (*getsize)(void *instance); \
  59. /* Read operation.*/ \
  60. ps_error_t (*read)(void *instance, ps_offset_t offset, \
  61. size_t n, uint8_t *rp); \
  62. /* Write operation.*/ \
  63. ps_error_t (*write)(void *instance, ps_offset_t offset, \
  64. size_t n, const uint8_t *wp);
  65. /**
  66. * @brief @p BasePersistentStorage specific methods with inherited ones.
  67. */
  68. #define _base_pers_storage_methods \
  69. _base_object_methods \
  70. _base_pers_storage_methods_alone
  71. /**
  72. * @brief @p BasePersistentStorage virtual methods table.
  73. */
  74. struct BasePersistentStorageVMT {
  75. _base_pers_storage_methods
  76. };
  77. /**
  78. * @brief @p BasePersistentStorage specific data.
  79. */
  80. #define _base_persistent_storage_data \
  81. _base_object_data
  82. /**
  83. * @extends BaseObject
  84. *
  85. * @brief Base persistent storage class.
  86. */
  87. typedef struct {
  88. /** @brief Virtual Methods Table.*/
  89. const struct BasePersistentStorageVMT *vmt;
  90. _base_persistent_storage_data
  91. } BasePersistentStorage;
  92. /*===========================================================================*/
  93. /* Driver macros. */
  94. /*===========================================================================*/
  95. /**
  96. * @name Macro Functions (BasePersistentStorage)
  97. * @{
  98. */
  99. /**
  100. * @brief Instance getter.
  101. * @details This special method is used to get the instance of this class
  102. * object from a derived class.
  103. */
  104. #define getBasePersistentStorage(ip) ((BasePersistentStorage *)&(ip)->vmt)
  105. /**
  106. * @brief Get storage size.
  107. *
  108. * @param[in] ip pointer to a @p BasePersistentStorage or derived class
  109. * @return The storage size in bytes.
  110. *
  111. * @api
  112. */
  113. #define psGetStorageSize(ip) \
  114. (ip)->vmt->getsize(ip)
  115. /**
  116. * @brief Read operation.
  117. *
  118. * @param[in] ip pointer to a @p BasePersistentStorage or derived class
  119. * @param[in] offset persistent storage offset
  120. * @param[in] n number of bytes to be read
  121. * @param[out] rp pointer to the data buffer
  122. * @return An error code.
  123. * @retval PS_NO_ERROR if there is no erase operation in progress.
  124. * @retval PS_ERROR_READ if the read operation failed.
  125. * @retval PS_ERROR_HW_FAILURE if access to the memory failed.
  126. *
  127. * @api
  128. */
  129. #define psRead(ip, offset, n, rp) \
  130. (ip)->vmt->read(ip, offset, n, rp)
  131. /**
  132. * @brief Write operation.
  133. *
  134. * @param[in] ip pointer to a @p BasePersistentStorage or derived class
  135. * @param[in] offset persistent storage offset
  136. * @param[in] n number of bytes to be written
  137. * @param[in] wp pointer to the data buffer
  138. * @return An error code.
  139. * @retval PS_NO_ERROR if there is no erase operation in progress.
  140. * @retval PS_ERROR_WRITE if the write operation failed.
  141. * @retval PS_ERROR_HW_FAILURE if access to the memory failed.
  142. *
  143. * @api
  144. */
  145. #define psWrite(ip, offset, n, wp) \
  146. (ip)->vmt->write(ip, offset, n, wp)
  147. /** @} */
  148. /*===========================================================================*/
  149. /* External declarations. */
  150. /*===========================================================================*/
  151. #ifdef __cplusplus
  152. extern "C" {
  153. #endif
  154. #ifdef __cplusplus
  155. }
  156. #endif
  157. #endif /* HAL_PERSISTENT_H */
  158. /** @} */