app_sdcard.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * Copyright (c) 2016 - 2020, 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. /**@file
  41. *
  42. * @defgroup app_sdcard SD card library
  43. * @{
  44. * @ingroup app_common
  45. *
  46. * @brief Asynchronous Secure Digital card (SDC) and MultiMedia card (MMC) library.
  47. */
  48. #ifndef APP_SDCARD_H_
  49. #define APP_SDCARD_H_
  50. #include "app_util_platform.h"
  51. #include "sdk_config.h"
  52. #define SDC_SECTOR_SIZE 512 ///< Size of a single SD card block in bytes.
  53. #define APP_SDCARD_CONFIG(MOSI_PIN, MISO_PIN, SCK_PIN, CS_PIN) { \
  54. .mosi_pin = MOSI_PIN, \
  55. .miso_pin = MISO_PIN, \
  56. .sck_pin = SCK_PIN, \
  57. .cs_pin = CS_PIN \
  58. }
  59. /**
  60. * @brief SDC operation result.
  61. */
  62. typedef enum {
  63. SDC_SUCCESS = 0, ///< Operation successful.
  64. SDC_ERROR_NOT_RESPONDING, ///< Card is not responding or not present.
  65. SDC_ERROR_TIMEOUT, ///< Card response timeout.
  66. SDC_ERROR_NOT_SUPPORTED, ///< Operation not supported.
  67. SDC_ERROR_COMMUNICATION, ///< Communication error.
  68. SDC_ERROR_DATA, ///< Data read/write error.
  69. SDC_ERROR_INTERNAL, ///< Internal error.
  70. } sdc_result_t;
  71. /**
  72. * @brief SDC event type.
  73. */
  74. typedef enum {
  75. SDC_EVT_INIT = 0, ///< Initialization procedure.
  76. SDC_EVT_READ, ///< Data read procedure.
  77. SDC_EVT_WRITE ///< Data write procedure.
  78. } sdc_evt_type_t;
  79. /**
  80. * @brief SDC event structure.
  81. */
  82. typedef struct {
  83. sdc_evt_type_t type; ///< Event type.
  84. sdc_result_t result; ///< Operation result.
  85. } sdc_evt_t;
  86. /**
  87. * @brief SDC type and version.
  88. */
  89. typedef enum {
  90. SDC_TYPE_UNKNOWN = 0, ///< Unknown / uninitialized card.
  91. SDC_TYPE_MMCV3, ///< MultiMedia card (MMC) version 3.
  92. SDC_TYPE_SDV1, ///< Secure Digital card (SDC) version 1.0.
  93. SDC_TYPE_SDV2 ///< Secure Digital card (SDC) version 2.0.
  94. } sdc_version_t;
  95. /**
  96. * @brief SDC type information structure.
  97. */
  98. typedef struct {
  99. sdc_version_t version : 3; ///< Card type and version (SD or MMC).
  100. uint8_t sdhc : 1; ///< Standard Capacity or High Capacity card.
  101. } sdc_type_t;
  102. /**
  103. * @brief SDC configuration structure.
  104. */
  105. typedef struct {
  106. uint8_t mosi_pin; ///< Serial data in (MOSI / DI) pin number.
  107. uint8_t miso_pin; ///< Serial data out (MISO / DO) pin number.
  108. uint8_t sck_pin; ///< Serial clock (SCK) pin number.
  109. uint8_t cs_pin; ///< Chip select (CS) pin number.
  110. } app_sdc_config_t;
  111. /**
  112. * @brief SDC information structure.
  113. */
  114. typedef struct {
  115. uint32_t num_blocks; ///< Number of available data blocks.
  116. uint16_t block_len; ///< Length (in bytes) of a single data block.
  117. sdc_type_t type; ///< Card type information structure.
  118. } app_sdc_info_t;
  119. /**
  120. * @brief SDC event handler type.
  121. */
  122. typedef void (*sdc_event_handler_t)(sdc_evt_t const * p_event);
  123. /**
  124. * @brief Function for initializing the card.
  125. *
  126. * @param[in] p_config Pointer to the SDC configuration structure.
  127. * @param[in] event_handler Pointer to the event handler function.
  128. *
  129. * @retval NRF_SUCCESS If initialization process was started succesfully.
  130. * @retval NRF_ERROR_INVALID_STATE If the card is already initialized or the initialization is in progress.
  131. * @retval NRF_ERROR_INVALID_PARAM If invalid parameters were specified.
  132. */
  133. ret_code_t app_sdc_init(app_sdc_config_t const * const p_config, sdc_event_handler_t event_handler);
  134. /**
  135. * @brief Function for uninitializing the card.
  136. *
  137. * @retval NRF_SUCCESS If card was uninitialized succesfully.
  138. * @retval NRF_ERROR_INVALID_STATE If the card is not initialized.
  139. * @retval NRF_ERROR_BUSY If there is an operation in progress.
  140. */
  141. ret_code_t app_sdc_uninit(void);
  142. /**
  143. * @brief Function for retrieving the card busy state.
  144. *
  145. * @retval true If there is an operation in progress.
  146. * @retval false If the card is in idle state.
  147. */
  148. bool app_sdc_busy_check(void);
  149. /**
  150. * @brief Function for reading the data blocks from the card.
  151. *
  152. * @param[out] p_buf Pointer to the data buffer. Must not be null.
  153. * @param[in] block_address Number of the first block to be read.
  154. * @param[in] block_count Number of blocks to read. Must be greater than 0.
  155. *
  156. * @retval NRF_SUCCESS If block read operation was started succesfully.
  157. * @retval NRF_ERROR_INVALID_STATE If the card is not initialized.
  158. * @retval NRF_ERROR_BUSY If there is already an operation active.
  159. * @retval NRF_ERROR_INVALID_PARAM If invalid parameters were specified.
  160. */
  161. ret_code_t app_sdc_block_read(uint8_t * p_buf, uint32_t block_address, uint16_t block_count);
  162. /**
  163. * @brief Function for writing the data blocks to the card.
  164. *
  165. * @param[out] p_buf Pointer to the data to be written. Must not be null.
  166. * @param[in] block_address Number of the first block to write.
  167. * @param[in] block_count Number of blocks to write. Must be greater than 0.
  168. *
  169. * @retval NRF_SUCCESS If block write operation was started succesfully.
  170. * @retval NRF_ERROR_INVALID_STATE If the card is not initialized.
  171. * @retval NRF_ERROR_BUSY If there is already an operation active.
  172. * @retval NRF_ERROR_INVALID_PARAM If invalid parameters were specified.
  173. */
  174. ret_code_t app_sdc_block_write(uint8_t const * p_buf, uint32_t block_address, uint16_t block_count);
  175. /**
  176. * @brief Function for retrieving the card information structure.
  177. *
  178. * @return Pointer to the card information structure or NULL if card is not initialized.
  179. */
  180. app_sdc_info_t const * app_sdc_info_get(void);
  181. #endif //APP_SDC_H_
  182. /** @} */