aboutsummaryrefslogtreecommitdiffstats
path: root/os/hal/include/hal_persistent.h
blob: f6ba14e825f1321b69a211adfd97979842c9d054 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
    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_WRITE= 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   Get storage size.
 *
 * @param[in] ip        pointer to a @p BasePersistentStorage or derived class
 * @return              The storage size in bytes.
 *
 * @api
 */
#define psGetStorageSize(ip)                                                \
  (ip)->vmt->getsize(ip)

/**
 * @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 written
 * @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 */

/** @} */