nrf_block_dev.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /**
  2. * Copyright (c) 2016 - 2018, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #ifndef NRF_BLOCK_DEV_H__
  41. #define NRF_BLOCK_DEV_H__
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. #include "sdk_common.h"
  46. #include "nrf_assert.h"
  47. #include <stddef.h>
  48. /**@file
  49. *
  50. * @defgroup nrf_block_dev Block device
  51. * @{
  52. * @ingroup app_common
  53. *
  54. * @brief This module implements unified block device API. It could used as a middle layer between
  55. * filesystems and memories.
  56. */
  57. /**
  58. * @brief Block device request descriptor item.
  59. */
  60. typedef struct {
  61. uint32_t blk_id; //!< Block ID
  62. uint32_t blk_count; //!< Block count
  63. void * p_buff; //!< Data buffer
  64. } nrf_block_req_t;
  65. /**
  66. * @brief Helper macro to create block device read/write request item
  67. *
  68. * @param name Instance name
  69. * @param block_start Block number start
  70. * @param block_count Number of blocks
  71. * @param buff Buffer to read/write
  72. */
  73. #define NRF_BLOCK_DEV_REQUEST(name, block_start, block_count, buff) \
  74. nrf_block_req_t name = { \
  75. .blk_id = block_start, \
  76. .blk_count = block_count, \
  77. .p_buff = buff, \
  78. }
  79. /**
  80. * @brief Block device events.
  81. *
  82. * Events are propagated when event handler is defined (@ref nrf_blk_dev_init)
  83. *
  84. */
  85. typedef enum {
  86. NRF_BLOCK_DEV_EVT_INIT, /**< Passed to event handler when init is done*/
  87. NRF_BLOCK_DEV_EVT_UNINIT, /**< Passed to event handler when uninit is done*/
  88. NRF_BLOCK_DEV_EVT_BLK_READ_DONE, /**< Passed to event handler block read operation is done*/
  89. NRF_BLOCK_DEV_EVT_BLK_WRITE_DONE, /**< Passed to event handler block write operation is done*/
  90. } nrf_block_dev_event_type_t;
  91. typedef enum {
  92. NRF_BLOCK_DEV_RESULT_SUCCESS = 0, /**< Operation completed succsefully*/
  93. NRF_BLOCK_DEV_RESULT_IO_ERROR, /**< I/O error*/
  94. NRF_BLOCK_DEV_RESULT_TIMEOUT, /**< Device timeout*/
  95. } nrf_block_dev_result_t;
  96. /**
  97. * @brief Block device event
  98. * */
  99. typedef struct {
  100. nrf_block_dev_event_type_t ev_type; //!< Event type
  101. nrf_block_dev_result_t result; //!< Operation status
  102. nrf_block_req_t const * p_blk_req; //!< Block request
  103. void const * p_context; //!< Event context
  104. } nrf_block_dev_event_t;
  105. struct nrf_block_dev_s;
  106. /**
  107. * @brief Block device event handler.
  108. *
  109. * @param[in] p_blk_dev Block device handle
  110. * @param[in] p_event Block device event
  111. */
  112. typedef void (* nrf_block_dev_ev_handler)(struct nrf_block_dev_s const * p_blk_dev,
  113. nrf_block_dev_event_t const * p_event);
  114. /**
  115. * @brief Block device geometry
  116. */
  117. typedef struct {
  118. uint32_t blk_count; //!< Block count
  119. uint32_t blk_size; //!< Block size
  120. } nrf_block_dev_geometry_t;
  121. /**
  122. * @brief Block device information strings
  123. */
  124. typedef struct {
  125. const char * p_vendor; //!< Vendor string
  126. const char * p_product; //!< Product string
  127. const char * p_revision; //!< Revision string
  128. } nrf_block_dev_info_strings_t;
  129. /**
  130. * @brief Block device information config
  131. *
  132. * @param vendor Vendor string
  133. * @param product Product string
  134. * @param revision Revision string
  135. * */
  136. #define NFR_BLOCK_DEV_INFO_CONFIG(vendor, product, revision) ( { \
  137. .p_vendor = vendor, \
  138. .p_product = product, \
  139. .p_revision = revision, \
  140. })
  141. /**
  142. * @brief Empty info string initializer
  143. * */
  144. #define NFR_BLOCK_DEV_INFO_CONFIG_EMPTY \
  145. NFR_BLOCK_DEV_INFO_CONFIG(NULL, NULL, NULL)
  146. /**
  147. * @brief Block device IOCTL requests
  148. */
  149. typedef enum {
  150. NRF_BLOCK_DEV_IOCTL_REQ_CACHE_FLUSH = 0, /**< Cache flush IOCTL request*/
  151. NRF_BLOCK_DEV_IOCTL_REQ_INFO_STRINGS, /**< Get info strings IOCTL request*/
  152. } nrf_block_dev_ioctl_req_t;
  153. /**
  154. * @brief Helper macro to get block device address from specific instance
  155. *
  156. * @param instance Block device instance
  157. * @param member Block device member name
  158. * */
  159. #define NRF_BLOCKDEV_BASE_ADDR(instance, member) &(instance).member
  160. /**
  161. * @brief Block device API
  162. * */
  163. typedef struct nrf_block_dev_s {
  164. struct nrf_block_dev_ops_s {
  165. /**
  166. * @brief @ref nrf_blk_dev_init
  167. */
  168. ret_code_t (*init)(struct nrf_block_dev_s const * p_blk_dev,
  169. nrf_block_dev_ev_handler ev_handler,
  170. void const * p_context);
  171. /**
  172. * @brief @ref nrf_blk_dev_uninit
  173. */
  174. ret_code_t (*uninit)(struct nrf_block_dev_s const * p_blk_dev);
  175. /**
  176. * @brief @ref nrf_blk_dev_read_req
  177. */
  178. ret_code_t (*read_req)(struct nrf_block_dev_s const * p_blk_dev,
  179. nrf_block_req_t const * p_blk);
  180. /**
  181. * @brief @ref nrf_blk_dev_write_req
  182. */
  183. ret_code_t (*write_req)(struct nrf_block_dev_s const * p_blk_dev,
  184. nrf_block_req_t const * p_blk);
  185. /**
  186. * @brief @ref nrf_blk_dev_ioctl
  187. */
  188. ret_code_t (*ioctl)(struct nrf_block_dev_s const * p_blk_dev,
  189. nrf_block_dev_ioctl_req_t req,
  190. void * p_data);
  191. /**
  192. * @brief @ref nrf_blk_dev_geometry
  193. */
  194. nrf_block_dev_geometry_t const * (*geometry)(struct nrf_block_dev_s const * p_blk_dev);
  195. } const * p_ops;
  196. } nrf_block_dev_t;
  197. /**
  198. * @brief Internals of @ref nrf_block_dev_t
  199. * */
  200. typedef struct nrf_block_dev_ops_s nrf_block_dev_ops_t;
  201. /**
  202. * @brief Initializes a block device.
  203. *
  204. * @param[in] p_blk_dev Block device handle
  205. * @param[in] ev_handler Event handler (pass NULL to work in synchronous mode)
  206. * @param[in] p_context Context passed to event handler
  207. *
  208. * @return Standard error code
  209. */
  210. static inline ret_code_t nrf_blk_dev_init(nrf_block_dev_t const * p_blk_dev,
  211. nrf_block_dev_ev_handler ev_handler,
  212. void const * p_context)
  213. {
  214. ASSERT(p_blk_dev->p_ops->init);
  215. return p_blk_dev->p_ops->init(p_blk_dev, ev_handler, p_context);
  216. }
  217. /**
  218. * @brief Un-initializes a block device.
  219. *
  220. * @param[in] p_blk_dev Block device handle
  221. *
  222. * @return Standard error code
  223. */
  224. static inline ret_code_t nrf_blk_dev_uninit(nrf_block_dev_t const * p_blk_dev)
  225. {
  226. ASSERT(p_blk_dev->p_ops->uninit);
  227. return p_blk_dev->p_ops->uninit(p_blk_dev);
  228. }
  229. /**
  230. * @brief Block read request.
  231. *
  232. * In synchronous mode this function will execute the read operation
  233. * and wait for its completion. In asynchronous mode the function will only request
  234. * the operation and return immediately. Then, the @ref NRF_BLOCK_DEV_EVT_BLK_READ_DONE
  235. * event will signal that operation has been completed and the specified buffer contains
  236. * valid data.
  237. *
  238. * @param[in] p_blk_dev Block device handle
  239. * @param[in] p_blk Block device request
  240. *
  241. * @return Standard error code
  242. */
  243. static inline ret_code_t nrf_blk_dev_read_req(nrf_block_dev_t const * p_blk_dev,
  244. nrf_block_req_t const * p_blk)
  245. {
  246. ASSERT(p_blk_dev->p_ops->read_req);
  247. ASSERT(p_blk_dev->p_ops->geometry);
  248. if (p_blk->blk_id >= p_blk_dev->p_ops->geometry(p_blk_dev)->blk_count)
  249. {
  250. return NRF_ERROR_INVALID_PARAM;
  251. }
  252. return p_blk_dev->p_ops->read_req(p_blk_dev, p_blk);
  253. }
  254. /**
  255. * @brief Block write request.
  256. *
  257. * In synchronous mode this function will execute the write operation
  258. * and wait for its completion. In asynchronous mode the function will only request
  259. * the operation and return immediately. Then, the @ref NRF_BLOCK_DEV_EVT_BLK_WRITE_DONE
  260. * event will signal that operation has been completed and the specified buffer
  261. * can be freed.
  262. *
  263. * @param[in] p_blk_dev Block device handle
  264. * @param[in] p_blk Block device request
  265. *
  266. * @return Standard error code
  267. */
  268. static inline ret_code_t nrf_blk_dev_write_req(nrf_block_dev_t const * p_blk_dev,
  269. nrf_block_req_t const * p_blk)
  270. {
  271. ASSERT(p_blk_dev->p_ops->write_req);
  272. ASSERT(p_blk_dev->p_ops->geometry);
  273. if (p_blk->blk_id >= p_blk_dev->p_ops->geometry(p_blk_dev)->blk_count)
  274. {
  275. return NRF_ERROR_INVALID_PARAM;
  276. }
  277. return p_blk_dev->p_ops->write_req(p_blk_dev, p_blk);
  278. }
  279. /**
  280. * @brief IO control function.
  281. *
  282. * @param[in] p_blk_dev Block device handle
  283. * @param[in] req Block device ioctl request
  284. * @param[in] p_data Block device ioctl data
  285. *
  286. * @return Standard error code
  287. * */
  288. static inline ret_code_t nrf_blk_dev_ioctl(nrf_block_dev_t const * p_blk_dev,
  289. nrf_block_dev_ioctl_req_t req,
  290. void * p_data)
  291. {
  292. ASSERT(p_blk_dev->p_ops->ioctl);
  293. return p_blk_dev->p_ops->ioctl(p_blk_dev, req, p_data);
  294. }
  295. /**
  296. * @brief Return a geometry of a block device.
  297. *
  298. * @param[in] p_blk_dev Block device handle
  299. *
  300. * @return Block size and count @ref nrf_block_dev_geometry_t
  301. */
  302. static inline nrf_block_dev_geometry_t const *
  303. nrf_blk_dev_geometry(nrf_block_dev_t const * p_blk_dev)
  304. {
  305. ASSERT(p_blk_dev->p_ops->geometry);
  306. return p_blk_dev->p_ops->geometry(p_blk_dev);
  307. }
  308. /** @} */
  309. #ifdef __cplusplus
  310. }
  311. #endif
  312. #endif /* NRF_BLOCK_DEV_H__ */