hal_ioblock.h 9.2 KB

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