fsl_sdspi.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef _FSL_SDSPI_H_
  31. #define _FSL_SDSPI_H_
  32. #include "fsl_common.h"
  33. #include "fsl_specification.h"
  34. /******************************************************************************
  35. * Definitions
  36. *****************************************************************************/
  37. /*! @brief Driver version. */
  38. #define FSL_SDSPI_DRIVER_VERSION (MAKE_VERSION(2U, 1U, 1U)) /*2.1.1*/
  39. /*! @brief Default block size */
  40. #define FSL_SDSPI_DEFAULT_BLOCK_SIZE (512U)
  41. /*!
  42. * @addtogroup SDSPI
  43. * @{
  44. */
  45. /*! @brief SDSPI API status */
  46. enum _sdspi_status
  47. {
  48. kStatus_SDSPI_SetFrequencyFailed = MAKE_STATUS(kStatusGroup_SDSPI, 0U), /*!< Set frequency failed */
  49. kStatus_SDSPI_ExchangeFailed = MAKE_STATUS(kStatusGroup_SDSPI, 1U), /*!< Exchange data on SPI bus failed */
  50. kStatus_SDSPI_WaitReadyFailed = MAKE_STATUS(kStatusGroup_SDSPI, 2U), /*!< Wait card ready failed */
  51. kStatus_SDSPI_ResponseError = MAKE_STATUS(kStatusGroup_SDSPI, 3U), /*!< Response is error */
  52. kStatus_SDSPI_WriteProtected = MAKE_STATUS(kStatusGroup_SDSPI, 4U), /*!< Write protected */
  53. kStatus_SDSPI_GoIdleFailed = MAKE_STATUS(kStatusGroup_SDSPI, 5U), /*!< Go idle failed */
  54. kStatus_SDSPI_SendCommandFailed = MAKE_STATUS(kStatusGroup_SDSPI, 6U), /*!< Send command failed */
  55. kStatus_SDSPI_ReadFailed = MAKE_STATUS(kStatusGroup_SDSPI, 7U), /*!< Read data failed */
  56. kStatus_SDSPI_WriteFailed = MAKE_STATUS(kStatusGroup_SDSPI, 8U), /*!< Write data failed */
  57. kStatus_SDSPI_SendInterfaceConditionFailed =
  58. MAKE_STATUS(kStatusGroup_SDSPI, 9U), /*!< Send interface condition failed */
  59. kStatus_SDSPI_SendOperationConditionFailed =
  60. MAKE_STATUS(kStatusGroup_SDSPI, 10U), /*!< Send operation condition failed */
  61. kStatus_SDSPI_ReadOcrFailed = MAKE_STATUS(kStatusGroup_SDSPI, 11U), /*!< Read OCR failed */
  62. kStatus_SDSPI_SetBlockSizeFailed = MAKE_STATUS(kStatusGroup_SDSPI, 12U), /*!< Set block size failed */
  63. kStatus_SDSPI_SendCsdFailed = MAKE_STATUS(kStatusGroup_SDSPI, 13U), /*!< Send CSD failed */
  64. kStatus_SDSPI_SendCidFailed = MAKE_STATUS(kStatusGroup_SDSPI, 14U), /*!< Send CID failed */
  65. kStatus_SDSPI_StopTransmissionFailed = MAKE_STATUS(kStatusGroup_SDSPI, 15U), /*!< Stop transmission failed */
  66. kStatus_SDSPI_SendApplicationCommandFailed =
  67. MAKE_STATUS(kStatusGroup_SDSPI, 16U), /*!< Send application command failed */
  68. };
  69. /*! @brief SDSPI card flag */
  70. enum _sdspi_card_flag
  71. {
  72. kSDSPI_SupportHighCapacityFlag = (1U << 0U), /*!< Card is high capacity */
  73. kSDSPI_SupportSdhcFlag = (1U << 1U), /*!< Card is SDHC */
  74. kSDSPI_SupportSdxcFlag = (1U << 2U), /*!< Card is SDXC */
  75. kSDSPI_SupportSdscFlag = (1U << 3U), /*!< Card is SDSC */
  76. };
  77. /*! @brief SDSPI response type */
  78. typedef enum _sdspi_response_type
  79. {
  80. kSDSPI_ResponseTypeR1 = 0U, /*!< Response 1 */
  81. kSDSPI_ResponseTypeR1b = 1U, /*!< Response 1 with busy */
  82. kSDSPI_ResponseTypeR2 = 2U, /*!< Response 2 */
  83. kSDSPI_ResponseTypeR3 = 3U, /*!< Response 3 */
  84. kSDSPI_ResponseTypeR7 = 4U, /*!< Response 7 */
  85. } sdspi_response_type_t;
  86. /*! @brief SDSPI command */
  87. typedef struct _sdspi_command
  88. {
  89. uint8_t index; /*!< Command index */
  90. uint32_t argument; /*!< Command argument */
  91. uint8_t responseType; /*!< Response type */
  92. uint8_t response[5U]; /*!< Response content */
  93. } sdspi_command_t;
  94. /*! @brief SDSPI host state. */
  95. typedef struct _sdspi_host
  96. {
  97. uint32_t busBaudRate; /*!< Bus baud rate */
  98. status_t (*setFrequency)(uint32_t frequency); /*!< Set frequency of SPI */
  99. status_t (*exchange)(uint8_t *in, uint8_t *out, uint32_t size); /*!< Exchange data over SPI */
  100. uint32_t (*getCurrentMilliseconds)(void); /*!< Get current time in milliseconds */
  101. } sdspi_host_t;
  102. /*!
  103. * @brief SD Card Structure
  104. *
  105. * Define the card structure including the necessary fields to identify and describe the card.
  106. */
  107. typedef struct _sdspi_card
  108. {
  109. sdspi_host_t *host; /*!< Host state information */
  110. uint32_t relativeAddress; /*!< Relative address of the card */
  111. uint32_t flags; /*!< Flags defined in _sdspi_card_flag. */
  112. uint8_t rawCid[16U]; /*!< Raw CID content */
  113. uint8_t rawCsd[16U]; /*!< Raw CSD content */
  114. uint8_t rawScr[8U]; /*!< Raw SCR content */
  115. uint32_t ocr; /*!< Raw OCR content */
  116. sd_cid_t cid; /*!< CID */
  117. sd_csd_t csd; /*!< CSD */
  118. sd_scr_t scr; /*!< SCR */
  119. uint32_t blockCount; /*!< Card total block number */
  120. uint32_t blockSize; /*!< Card block size */
  121. } sdspi_card_t;
  122. /*************************************************************************************************
  123. * API
  124. ************************************************************************************************/
  125. #if defined(__cplusplus)
  126. extern "C" {
  127. #endif
  128. /*!
  129. * @name SDSPI Function
  130. * @{
  131. */
  132. /*!
  133. * @brief Initializes the card on a specific SPI instance.
  134. *
  135. * This function initializes the card on a specific SPI instance.
  136. *
  137. * @param card Card descriptor
  138. * @retval kStatus_SDSPI_SetFrequencyFailed Set frequency failed.
  139. * @retval kStatus_SDSPI_GoIdleFailed Go idle failed.
  140. * @retval kStatus_SDSPI_SendInterfaceConditionFailed Send interface condition failed.
  141. * @retval kStatus_SDSPI_SendOperationConditionFailed Send operation condition failed.
  142. * @retval kStatus_Timeout Send command timeout.
  143. * @retval kStatus_SDSPI_NotSupportYet Not support yet.
  144. * @retval kStatus_SDSPI_ReadOcrFailed Read OCR failed.
  145. * @retval kStatus_SDSPI_SetBlockSizeFailed Set block size failed.
  146. * @retval kStatus_SDSPI_SendCsdFailed Send CSD failed.
  147. * @retval kStatus_SDSPI_SendCidFailed Send CID failed.
  148. * @retval kStatus_Success Operate successfully.
  149. */
  150. status_t SDSPI_Init(sdspi_card_t *card);
  151. /*!
  152. * @brief Deinitializes the card.
  153. *
  154. * This function deinitializes the specific card.
  155. *
  156. * @param card Card descriptor
  157. */
  158. void SDSPI_Deinit(sdspi_card_t *card);
  159. /*!
  160. * @brief Checks whether the card is write-protected.
  161. *
  162. * This function checks if the card is write-protected via CSD register.
  163. *
  164. * @param card Card descriptor.
  165. * @retval true Card is read only.
  166. * @retval false Card isn't read only.
  167. */
  168. bool SDSPI_CheckReadOnly(sdspi_card_t *card);
  169. /*!
  170. * @brief Reads blocks from the specific card.
  171. *
  172. * This function reads blocks from specific card.
  173. *
  174. * @param card Card descriptor.
  175. * @param buffer the buffer to hold the data read from card
  176. * @param startBlock the start block index
  177. * @param blockCount the number of blocks to read
  178. * @retval kStatus_SDSPI_SendCommandFailed Send command failed.
  179. * @retval kStatus_SDSPI_ReadFailed Read data failed.
  180. * @retval kStatus_SDSPI_StopTransmissionFailed Stop transmission failed.
  181. * @retval kStatus_Success Operate successfully.
  182. */
  183. status_t SDSPI_ReadBlocks(sdspi_card_t *card, uint8_t *buffer, uint32_t startBlock, uint32_t blockCount);
  184. /*!
  185. * @brief Writes blocks of data to the specific card.
  186. *
  187. * This function writes blocks to specific card
  188. *
  189. * @param card Card descriptor.
  190. * @param buffer the buffer holding the data to be written to the card
  191. * @param startBlock the start block index
  192. * @param blockCount the number of blocks to write
  193. * @retval kStatus_SDSPI_WriteProtected Card is write protected.
  194. * @retval kStatus_SDSPI_SendCommandFailed Send command failed.
  195. * @retval kStatus_SDSPI_ResponseError Response is error.
  196. * @retval kStatus_SDSPI_WriteFailed Write data failed.
  197. * @retval kStatus_SDSPI_ExchangeFailed Exchange data over SPI failed.
  198. * @retval kStatus_SDSPI_WaitReadyFailed Wait card to be ready status failed.
  199. * @retval kStatus_Success Operate successfully.
  200. */
  201. status_t SDSPI_WriteBlocks(sdspi_card_t *card, uint8_t *buffer, uint32_t startBlock, uint32_t blockCount);
  202. /* @} */
  203. #if defined(__cplusplus)
  204. }
  205. #endif
  206. /*! @} */
  207. #endif /* _FSL_SDSPI_H_ */