diskio_blkdev.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * Copyright (c) 2016 - 2019, 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 DISKIO_SDCARD_H_
  41. #define DISKIO_SDCARD_H_
  42. #include "diskio.h"
  43. #include "nrf_block_dev.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**@file
  48. *
  49. * @defgroup diskio_blockdev FatFS disk I/O interface based on block device.
  50. * @{
  51. *
  52. * @brief This module implements the FatFs disk API. Internals of this module are based on block device.
  53. *
  54. */
  55. /**
  56. * @brief FatFs disk I/O block device configuration structure.
  57. * */
  58. typedef struct
  59. {
  60. const nrf_block_dev_t * p_block_device; ///< Block device associated with a FatFs drive.
  61. /**
  62. * @brief FatFs disk interface synchronous wait function.
  63. *
  64. * The function to be called repeatedly until the disk I/O operation is completed.
  65. */
  66. void (*wait_func)(void);
  67. } diskio_blkdev_config_t;
  68. /**
  69. * @brief Disk I/O block device.
  70. * */
  71. typedef struct
  72. {
  73. diskio_blkdev_config_t config; ///< Disk I/O configuration.
  74. nrf_block_dev_result_t last_result; ///< Result of the last I/O operation.
  75. volatile DSTATUS state; ///< Current disk state.
  76. volatile bool busy; ///< Disk busy flag.
  77. } diskio_blkdev_t;
  78. /**
  79. * @brief Initializer of @ref diskio_blkdev_t.
  80. *
  81. * @param blk_device Block device handle.
  82. * @param wait_funcion User wait function (NULL is allowed).
  83. * */
  84. #define DISKIO_BLOCKDEV_CONFIG(blk_device, wait_funcion) { \
  85. .config = { \
  86. .p_block_device = (blk_device), \
  87. .wait_func = (wait_funcion), \
  88. }, \
  89. .last_result = NRF_BLOCK_DEV_RESULT_SUCCESS, \
  90. .state = STA_NOINIT, \
  91. .busy = false \
  92. }
  93. /**
  94. * @brief FatFs disk initialization.
  95. *
  96. * Initializes a block device assigned to a drive number.
  97. *
  98. * @param[in] drv Drive number.
  99. *
  100. * @return Disk status code.
  101. * */
  102. DSTATUS disk_initialize(BYTE drv);
  103. /**
  104. * @brief FatFs disk uninitialization.
  105. *
  106. * Uninitializes a block device assigned to a drive number.
  107. *
  108. * @param[in] drv Drive index.
  109. *
  110. * @return Disk status code.
  111. * */
  112. DSTATUS disk_uninitialize(BYTE drv);
  113. /**
  114. * @brief FatFs disk status get.
  115. *
  116. * @param[in] drv Drive index.
  117. *
  118. * @return Disk status code.
  119. * */
  120. DSTATUS disk_status(BYTE drv);
  121. /**
  122. * @brief FatFs disk sector read.
  123. *
  124. * @param[in] drv Drive number.
  125. * @param[out] buff Output buffer.
  126. * @param[in] sector Sector start number.
  127. * @param[in] count Sector count.
  128. *
  129. * @return FatFs standard error code.
  130. * */
  131. DRESULT disk_read(BYTE drv, BYTE* buff, DWORD sector, UINT count);
  132. /**
  133. * @brief FatFs disk sector write.
  134. *
  135. * @param[in] drv Drive number.
  136. * @param[in] buff Input buffer.
  137. * @param[in] sector Sector start number.
  138. * @param[in] count Sector count.
  139. *
  140. * @return FatFs standard error code.
  141. * */
  142. DRESULT disk_write(BYTE drv, const BYTE* buff, DWORD sector, UINT count);
  143. /**
  144. * @brief FatFs disk I/O control operation.
  145. *
  146. * @param[in] drv Drive number.
  147. * @param[in] cmd I/O control command.
  148. * @param buff I/O control parameter (optional).
  149. *
  150. * @return FatFs standard error code.
  151. * */
  152. DRESULT disk_ioctl(BYTE drv, BYTE cmd, void* buff);
  153. /**
  154. * @brief Registers a block device array.
  155. *
  156. * @warning This function must be called before any other function from this header.
  157. *
  158. * @param[in] diskio_blkdevs Disk I/O block device array.
  159. * @param[in] count Number of elements in a block device array.
  160. * */
  161. void diskio_blockdev_register(diskio_blkdev_t * diskio_blkdevs, size_t count);
  162. /** @} */
  163. #ifdef __cplusplus
  164. }
  165. #endif
  166. #endif /*DISKIO_SDCARD_H_*/