ChibiOS/HAL  6.1.0
hal_ioblock.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_ioblock.h
19  * @brief I/O block devices access.
20  * @details This header defines an abstract interface useful to access generic
21  * I/O block devices in a standardized way.
22  *
23  * @addtogroup IO_BLOCK
24  * @details This module defines an abstract interface for accessing generic
25  * block devices.<br>
26  * Note that no code is present, just abstract interfaces-like
27  * structures, you should look at the system as to a set of
28  * abstract C++ classes (even if written in C). This system
29  * has then advantage to make the access to block devices
30  * independent from the implementation logic.
31  * @{
32  */
33 
34 #ifndef HAL_IOBLOCK_H
35 #define HAL_IOBLOCK_H
36 
37 /**
38  * @brief Driver state machine possible states.
39  */
40 typedef enum {
41  BLK_UNINIT = 0, /**< Not initialized. */
42  BLK_STOP = 1, /**< Stopped. */
43  BLK_ACTIVE = 2, /**< Interface active. */
44  BLK_CONNECTING = 3, /**< Connection in progress. */
45  BLK_DISCONNECTING = 4, /**< Disconnection in progress. */
46  BLK_READY = 5, /**< Device ready. */
47  BLK_READING = 6, /**< Read operation in progress. */
48  BLK_WRITING = 7, /**< Write operation in progress. */
49  BLK_SYNCING = 8 /**< Sync. operation in progress. */
50 } blkstate_t;
51 
52 /**
53  * @brief Block device info.
54  */
55 typedef struct {
56  uint32_t blk_size; /**< @brief Block size in bytes. */
57  uint32_t blk_num; /**< @brief Total number of blocks. */
59 
60 /**
61  * @brief @p BaseBlockDevice specific methods.
62  */
63 #define _base_block_device_methods \
64  _base_object_methods \
65  /* Removable media detection.*/ \
66  bool (*is_inserted)(void *instance); \
67  /* Removable write protection detection.*/ \
68  bool (*is_protected)(void *instance); \
69  /* Connection to the block device.*/ \
70  bool (*connect)(void *instance); \
71  /* Disconnection from the block device.*/ \
72  bool (*disconnect)(void *instance); \
73  /* Reads one or more blocks.*/ \
74  bool (*read)(void *instance, uint32_t startblk, \
75  uint8_t *buffer, uint32_t n); \
76  /* Writes one or more blocks.*/ \
77  bool (*write)(void *instance, uint32_t startblk, \
78  const uint8_t *buffer, uint32_t n); \
79  /* Write operations synchronization.*/ \
80  bool (*sync)(void *instance); \
81  /* Obtains info about the media.*/ \
82  bool (*get_info)(void *instance, BlockDeviceInfo *bdip);
83 
84 /**
85  * @brief @p BaseBlockDevice specific data.
86  */
87 #define _base_block_device_data \
88  _base_object_data \
89  /* Driver state.*/ \
90  blkstate_t state;
91 
92 /**
93  * @brief @p BaseBlockDevice virtual methods table.
94  */
97 };
98 
99 /**
100  * @extends BaseObject
101  *
102  * @brief Base block device class.
103  * @details This class represents a generic, block-accessible, device.
104  */
105 typedef struct {
106  /** @brief Virtual Methods Table.*/
107  const struct BaseBlockDeviceVMT *vmt;
110 
111 /**
112  * @name Macro Functions (BaseBlockDevice)
113  * @{
114  */
115 /**
116  * @brief Returns the driver state.
117  * @note Can be called in ISR context.
118  *
119  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
120  *
121  * @return The driver state.
122  *
123  * @special
124  */
125 #define blkGetDriverState(ip) ((ip)->state)
126 
127 /**
128  * @brief Determines if the device is transferring data.
129  * @note Can be called in ISR context.
130  *
131  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
132  *
133  * @return The driver state.
134  * @retval false the device is not transferring data.
135  * @retval true the device not transferring data.
136  *
137  * @special
138  */
139 #define blkIsTransferring(ip) ((((ip)->state) == BLK_CONNECTING) || \
140  (((ip)->state) == BLK_DISCONNECTING) || \
141  (((ip)->state) == BLK_READING) || \
142  (((ip)->state) == BLK_WRITING))
143 
144 /**
145  * @brief Returns the media insertion status.
146  * @note On some implementations this function can only be called if the
147  * device is not transferring data.
148  * The function @p blkIsTransferring() should be used before calling
149  * this function.
150  *
151  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
152  *
153  * @return The media state.
154  * @retval false media not inserted.
155  * @retval true media inserted.
156  *
157  * @api
158  */
159 #define blkIsInserted(ip) ((ip)->vmt->is_inserted(ip))
160 
161 /**
162  * @brief Returns the media write protection status.
163  *
164  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
165  *
166  * @return The media state.
167  * @retval false writable media.
168  * @retval true non writable media.
169  *
170  * @api
171  */
172 #define blkIsWriteProtected(ip) ((ip)->vmt->is_protected(ip))
173 
174 /**
175  * @brief Performs the initialization procedure on the block device.
176  * @details This function should be performed before I/O operations can be
177  * attempted on the block device and after insertion has been
178  * confirmed using @p blkIsInserted().
179  *
180  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
181  *
182  * @return The operation status.
183  * @retval HAL_SUCCESS operation succeeded.
184  * @retval HAL_FAILED operation failed.
185  *
186  * @api
187  */
188 #define blkConnect(ip) ((ip)->vmt->connect(ip))
189 
190 /**
191  * @brief Terminates operations on the block device.
192  * @details This operation safely terminates operations on the block device.
193  *
194  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
195  *
196  * @return The operation status.
197  * @retval HAL_SUCCESS operation succeeded.
198  * @retval HAL_FAILED operation failed.
199  *
200  * @api
201  */
202 #define blkDisconnect(ip) ((ip)->vmt->disconnect(ip))
203 
204 /**
205  * @brief Reads one or more blocks.
206  *
207  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
208  * @param[in] startblk first block to read
209  * @param[out] buf pointer to the read buffer
210  * @param[in] n number of blocks to read
211  *
212  * @return The operation status.
213  * @retval HAL_SUCCESS operation succeeded.
214  * @retval HAL_FAILED operation failed.
215  *
216  * @api
217  */
218 #define blkRead(ip, startblk, buf, n) \
219  ((ip)->vmt->read(ip, startblk, buf, n))
220 
221 /**
222  * @brief Writes one or more blocks.
223  *
224  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
225  * @param[in] startblk first block to write
226  * @param[out] buf pointer to the write buffer
227  * @param[in] n number of blocks to write
228  *
229  * @return The operation status.
230  * @retval HAL_SUCCESS operation succeeded.
231  * @retval HAL_FAILED operation failed.
232  *
233  * @api
234  */
235 #define blkWrite(ip, startblk, buf, n) \
236  ((ip)->vmt->write(ip, startblk, buf, n))
237 
238 /**
239  * @brief Ensures write synchronization.
240  *
241  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
242  *
243  * @return The operation status.
244  * @retval HAL_SUCCESS operation succeeded.
245  * @retval HAL_FAILED operation failed.
246  *
247  * @api
248  */
249 #define blkSync(ip) ((ip)->vmt->sync(ip))
250 
251 /**
252  * @brief Returns a media information structure.
253  *
254  * @param[in] ip pointer to a @p BaseBlockDevice or derived class
255  * @param[out] bdip pointer to a @p BlockDeviceInfo structure
256  *
257  * @return The operation status.
258  * @retval HAL_SUCCESS operation succeeded.
259  * @retval HAL_FAILED operation failed.
260  *
261  * @api
262  */
263 #define blkGetInfo(ip, bdip) ((ip)->vmt->get_info(ip, bdip))
264 
265 /** @} */
266 
267 #endif /* HAL_IOBLOCK_H */
268 
269 /** @} */
BaseBlockDevice virtual methods table.
Definition: hal_ioblock.h:95
Base block device class.
Definition: hal_ioblock.h:105
Block device info.
Definition: hal_ioblock.h:55
uint32_t blk_num
Total number of blocks.
Definition: hal_ioblock.h:57
#define _base_block_device_methods
BaseBlockDevice specific methods.
Definition: hal_ioblock.h:63
#define _base_block_device_data
BaseBlockDevice specific data.
Definition: hal_ioblock.h:87
blkstate_t
Driver state machine possible states.
Definition: hal_ioblock.h:40
uint32_t blk_size
Block size in bytes.
Definition: hal_ioblock.h:56
const struct BaseBlockDeviceVMT * vmt
Virtual Methods Table.
Definition: hal_ioblock.h:107