diff options
Diffstat (limited to 'os/hal/include/hal_persistent.h')
-rw-r--r-- | os/hal/include/hal_persistent.h | 170 |
1 files changed, 170 insertions, 0 deletions
diff --git a/os/hal/include/hal_persistent.h b/os/hal/include/hal_persistent.h new file mode 100644 index 000000000..d0d6c4472 --- /dev/null +++ b/os/hal/include/hal_persistent.h @@ -0,0 +1,170 @@ +/*
+ ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+/**
+ * @file hal_persistent.h
+ * @brief Generic persistent storage class header.
+ *
+ * @addtogroup HAL_PERSISTENT
+ * @{
+ */
+
+#ifndef HAL_PERSISTENT_H
+#define HAL_PERSISTENT_H
+
+/*===========================================================================*/
+/* Driver constants. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver pre-compile time settings. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Derived constants and error checks. */
+/*===========================================================================*/
+
+/*===========================================================================*/
+/* Driver data structures and types. */
+/*===========================================================================*/
+
+/**
+ * @brief Type of a persistent storage error code.
+ * @note Code values are kept equal to the equivalent codes in the flash
+ * interface, this is intentional.
+ */
+typedef enum {
+ PS_NO_ERROR = 0, /* No error. */
+ PS_ERROR_READ = 2, /* ECC or other error during read operation.*/
+ PS_ERROR_PROGRAM = 3, /* Program operation failed. */
+ PS_ERROR_VERIFY = 5, /* Verify operation failed. */
+ PS_ERROR_HW_FAILURE = 6 /* Controller or communication error. */
+} ps_error_t;
+
+/**
+ * @brief Type of a persistent storage offset.
+ */
+typedef uint32_t ps_offset_t;
+
+/**
+ * @brief @p BasePersistentStorage specific methods.
+ */
+#define _base_persistent_storage_methods_alone \
+ /* Storage size.*/ \
+ size_t (*getsize)(void *instance); \
+ /* Read operation.*/ \
+ ps_error_t (*read)(void *instance, ps_offset_t offset, \
+ size_t n, uint8_t *rp); \
+ /* Write operation.*/ \
+ ps_error_t (*write)(void *instance, ps_offset_t offset, \
+ size_t n, const uint8_t *wp);
+
+/**
+ * @brief @p BasePersistentStorage specific methods with inherited ones.
+ */
+#define _base_persistent_storage_methods \
+ _base_object_methods \
+ _base_persistent_storage_methods_alone
+
+/**
+ * @brief @p BasePersistentStorage virtual methods table.
+ */
+struct BasePersistentStorageVMT {
+ _base_persistent_storage_methods
+};
+
+/**
+ * @brief @p BasePersistentStorage specific data.
+ */
+#define _base_persistent_storage_data \
+ _base_object_data
+
+/**
+ * @extends BaseObject
+ *
+ * @brief Base persistent storage class.
+ */
+typedef struct {
+ /** @brief Virtual Methods Table.*/
+ const struct BasePersistentStorageVMT *vmt;
+ _base_persistent_storage_data
+} BasePersistentStorage;
+
+/*===========================================================================*/
+/* Driver macros. */
+/*===========================================================================*/
+
+/**
+ * @name Macro Functions (BasePersistentStorage)
+ * @{
+ */
+/**
+ * @brief Instance getter.
+ * @details This special method is used to get the instance of this class
+ * object from a derived class.
+ */
+#define getBasePersistentStorage(ip) ((BasePersistentStorage *)&(ip)->vmt)
+
+/**
+ * @brief Read operation.
+ *
+ * @param[in] ip pointer to a @p BasePersistentStorage or derived class
+ * @param[in] offset persistent storage offset
+ * @param[in] n number of bytes to be read
+ * @param[out] rp pointer to the data buffer
+ * @return An error code.
+ * @retval PS_NO_ERROR if there is no erase operation in progress.
+ * @retval PS_ERROR_READ if the read operation failed.
+ * @retval PS_ERROR_HW_FAILURE if access to the memory failed.
+ *
+ * @api
+ */
+#define psRead(ip, offset, n, rp) \
+ (ip)->vmt->read(ip, offset, n, rp)
+
+/**
+ * @brief Write operation.
+ *
+ * @param[in] ip pointer to a @p BasePersistentStorage or derived class
+ * @param[in] offset persistent storage offset
+ * @param[in] n number of bytes to be programmed
+ * @param[in] wp pointer to the data buffer
+ * @return An error code.
+ * @retval PS_NO_ERROR if there is no erase operation in progress.
+ * @retval PS_ERROR_WRITE if the write operation failed.
+ * @retval PS_ERROR_HW_FAILURE if access to the memory failed.
+ *
+ * @api
+ */
+#define psWrite(ip, offset, n, wp) \
+ (ip)->vmt->write(ip, offset, n, wp)
+/** @} */
+
+/*===========================================================================*/
+/* External declarations. */
+/*===========================================================================*/
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* HAL_PERSISTENT_H */
+
+/** @} */
|