fsl_sdhc.h 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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_SDHC_H_
  31. #define _FSL_SDHC_H_
  32. #include "fsl_common.h"
  33. /*!
  34. * @addtogroup sdhc
  35. * @{
  36. */
  37. /******************************************************************************
  38. * Definitions.
  39. *****************************************************************************/
  40. /*! @name Driver version */
  41. /*@{*/
  42. /*! @brief Driver version 2.1.5. */
  43. #define FSL_SDHC_DRIVER_VERSION (MAKE_VERSION(2U, 1U, 5U))
  44. /*@}*/
  45. /*! @brief Maximum block count can be set one time */
  46. #define SDHC_MAX_BLOCK_COUNT (SDHC_BLKATTR_BLKCNT_MASK >> SDHC_BLKATTR_BLKCNT_SHIFT)
  47. /*! @brief SDHC status */
  48. enum _sdhc_status
  49. {
  50. kStatus_SDHC_BusyTransferring = MAKE_STATUS(kStatusGroup_SDHC, 0U), /*!< Transfer is on-going */
  51. kStatus_SDHC_PrepareAdmaDescriptorFailed = MAKE_STATUS(kStatusGroup_SDHC, 1U), /*!< Set DMA descriptor failed */
  52. kStatus_SDHC_SendCommandFailed = MAKE_STATUS(kStatusGroup_SDHC, 2U), /*!< Send command failed */
  53. kStatus_SDHC_TransferDataFailed = MAKE_STATUS(kStatusGroup_SDHC, 3U), /*!< Transfer data failed */
  54. kStatus_SDHC_DMADataBufferAddrNotAlign =
  55. MAKE_STATUS(kStatusGroup_SDHC, 4U), /*!< data buffer addr not align in DMA mode */
  56. };
  57. /*! @brief Host controller capabilities flag mask */
  58. enum _sdhc_capability_flag
  59. {
  60. kSDHC_SupportAdmaFlag = SDHC_HTCAPBLT_ADMAS_MASK, /*!< Support ADMA */
  61. kSDHC_SupportHighSpeedFlag = SDHC_HTCAPBLT_HSS_MASK, /*!< Support high-speed */
  62. kSDHC_SupportDmaFlag = SDHC_HTCAPBLT_DMAS_MASK, /*!< Support DMA */
  63. kSDHC_SupportSuspendResumeFlag = SDHC_HTCAPBLT_SRS_MASK, /*!< Support suspend/resume */
  64. kSDHC_SupportV330Flag = SDHC_HTCAPBLT_VS33_MASK, /*!< Support voltage 3.3V */
  65. #if defined FSL_FEATURE_SDHC_HAS_V300_SUPPORT && FSL_FEATURE_SDHC_HAS_V300_SUPPORT
  66. kSDHC_SupportV300Flag = SDHC_HTCAPBLT_VS30_MASK, /*!< Support voltage 3.0V */
  67. #endif
  68. #if defined FSL_FEATURE_SDHC_HAS_V180_SUPPORT && FSL_FEATURE_SDHC_HAS_V180_SUPPORT
  69. kSDHC_SupportV180Flag = SDHC_HTCAPBLT_VS18_MASK, /*!< Support voltage 1.8V */
  70. #endif
  71. /* Put additional two flags in HTCAPBLT_MBL's position. */
  72. kSDHC_Support4BitFlag = (SDHC_HTCAPBLT_MBL_SHIFT << 0U), /*!< Support 4 bit mode */
  73. kSDHC_Support8BitFlag = (SDHC_HTCAPBLT_MBL_SHIFT << 1U), /*!< Support 8 bit mode */
  74. };
  75. /*! @brief Wakeup event mask */
  76. enum _sdhc_wakeup_event
  77. {
  78. kSDHC_WakeupEventOnCardInt = SDHC_PROCTL_WECINT_MASK, /*!< Wakeup on card interrupt */
  79. kSDHC_WakeupEventOnCardInsert = SDHC_PROCTL_WECINS_MASK, /*!< Wakeup on card insertion */
  80. kSDHC_WakeupEventOnCardRemove = SDHC_PROCTL_WECRM_MASK, /*!< Wakeup on card removal */
  81. kSDHC_WakeupEventsAll = (kSDHC_WakeupEventOnCardInt | kSDHC_WakeupEventOnCardInsert |
  82. kSDHC_WakeupEventOnCardRemove), /*!< All wakeup events */
  83. };
  84. /*! @brief Reset type mask */
  85. enum _sdhc_reset
  86. {
  87. kSDHC_ResetAll = SDHC_SYSCTL_RSTA_MASK, /*!< Reset all except card detection */
  88. kSDHC_ResetCommand = SDHC_SYSCTL_RSTC_MASK, /*!< Reset command line */
  89. kSDHC_ResetData = SDHC_SYSCTL_RSTD_MASK, /*!< Reset data line */
  90. kSDHC_ResetsAll = (kSDHC_ResetAll | kSDHC_ResetCommand | kSDHC_ResetData), /*!< All reset types */
  91. };
  92. /*! @brief Transfer flag mask */
  93. enum _sdhc_transfer_flag
  94. {
  95. kSDHC_EnableDmaFlag = SDHC_XFERTYP_DMAEN_MASK, /*!< Enable DMA */
  96. kSDHC_CommandTypeSuspendFlag = (SDHC_XFERTYP_CMDTYP(1U)), /*!< Suspend command */
  97. kSDHC_CommandTypeResumeFlag = (SDHC_XFERTYP_CMDTYP(2U)), /*!< Resume command */
  98. kSDHC_CommandTypeAbortFlag = (SDHC_XFERTYP_CMDTYP(3U)), /*!< Abort command */
  99. kSDHC_EnableBlockCountFlag = SDHC_XFERTYP_BCEN_MASK, /*!< Enable block count */
  100. kSDHC_EnableAutoCommand12Flag = SDHC_XFERTYP_AC12EN_MASK, /*!< Enable auto CMD12 */
  101. kSDHC_DataReadFlag = SDHC_XFERTYP_DTDSEL_MASK, /*!< Enable data read */
  102. kSDHC_MultipleBlockFlag = SDHC_XFERTYP_MSBSEL_MASK, /*!< Multiple block data read/write */
  103. kSDHC_ResponseLength136Flag = SDHC_XFERTYP_RSPTYP(1U), /*!< 136 bit response length */
  104. kSDHC_ResponseLength48Flag = SDHC_XFERTYP_RSPTYP(2U), /*!< 48 bit response length */
  105. kSDHC_ResponseLength48BusyFlag = SDHC_XFERTYP_RSPTYP(3U), /*!< 48 bit response length with busy status */
  106. kSDHC_EnableCrcCheckFlag = SDHC_XFERTYP_CCCEN_MASK, /*!< Enable CRC check */
  107. kSDHC_EnableIndexCheckFlag = SDHC_XFERTYP_CICEN_MASK, /*!< Enable index check */
  108. kSDHC_DataPresentFlag = SDHC_XFERTYP_DPSEL_MASK, /*!< Data present flag */
  109. };
  110. /*! @brief Present status flag mask */
  111. enum _sdhc_present_status_flag
  112. {
  113. kSDHC_CommandInhibitFlag = SDHC_PRSSTAT_CIHB_MASK, /*!< Command inhibit */
  114. kSDHC_DataInhibitFlag = SDHC_PRSSTAT_CDIHB_MASK, /*!< Data inhibit */
  115. kSDHC_DataLineActiveFlag = SDHC_PRSSTAT_DLA_MASK, /*!< Data line active */
  116. kSDHC_SdClockStableFlag = SDHC_PRSSTAT_SDSTB_MASK, /*!< SD bus clock stable */
  117. kSDHC_WriteTransferActiveFlag = SDHC_PRSSTAT_WTA_MASK, /*!< Write transfer active */
  118. kSDHC_ReadTransferActiveFlag = SDHC_PRSSTAT_RTA_MASK, /*!< Read transfer active */
  119. kSDHC_BufferWriteEnableFlag = SDHC_PRSSTAT_BWEN_MASK, /*!< Buffer write enable */
  120. kSDHC_BufferReadEnableFlag = SDHC_PRSSTAT_BREN_MASK, /*!< Buffer read enable */
  121. kSDHC_CardInsertedFlag = SDHC_PRSSTAT_CINS_MASK, /*!< Card inserted */
  122. kSDHC_CommandLineLevelFlag = SDHC_PRSSTAT_CLSL_MASK, /*!< Command line signal level */
  123. kSDHC_Data0LineLevelFlag = (1U << 24U), /*!< Data0 line signal level */
  124. kSDHC_Data1LineLevelFlag = (1U << 25U), /*!< Data1 line signal level */
  125. kSDHC_Data2LineLevelFlag = (1U << 26U), /*!< Data2 line signal level */
  126. kSDHC_Data3LineLevelFlag = (1U << 27U), /*!< Data3 line signal level */
  127. kSDHC_Data4LineLevelFlag = (1U << 28U), /*!< Data4 line signal level */
  128. kSDHC_Data5LineLevelFlag = (1U << 29U), /*!< Data5 line signal level */
  129. kSDHC_Data6LineLevelFlag = (1U << 30U), /*!< Data6 line signal level */
  130. kSDHC_Data7LineLevelFlag = (1U << 31U), /*!< Data7 line signal level */
  131. };
  132. /*! @brief Interrupt status flag mask */
  133. enum _sdhc_interrupt_status_flag
  134. {
  135. kSDHC_CommandCompleteFlag = SDHC_IRQSTAT_CC_MASK, /*!< Command complete */
  136. kSDHC_DataCompleteFlag = SDHC_IRQSTAT_TC_MASK, /*!< Data complete */
  137. kSDHC_BlockGapEventFlag = SDHC_IRQSTAT_BGE_MASK, /*!< Block gap event */
  138. kSDHC_DmaCompleteFlag = SDHC_IRQSTAT_DINT_MASK, /*!< DMA interrupt */
  139. kSDHC_BufferWriteReadyFlag = SDHC_IRQSTAT_BWR_MASK, /*!< Buffer write ready */
  140. kSDHC_BufferReadReadyFlag = SDHC_IRQSTAT_BRR_MASK, /*!< Buffer read ready */
  141. kSDHC_CardInsertionFlag = SDHC_IRQSTAT_CINS_MASK, /*!< Card inserted */
  142. kSDHC_CardRemovalFlag = SDHC_IRQSTAT_CRM_MASK, /*!< Card removed */
  143. kSDHC_CardInterruptFlag = SDHC_IRQSTAT_CINT_MASK, /*!< Card interrupt */
  144. kSDHC_CommandTimeoutFlag = SDHC_IRQSTAT_CTOE_MASK, /*!< Command timeout error */
  145. kSDHC_CommandCrcErrorFlag = SDHC_IRQSTAT_CCE_MASK, /*!< Command CRC error */
  146. kSDHC_CommandEndBitErrorFlag = SDHC_IRQSTAT_CEBE_MASK, /*!< Command end bit error */
  147. kSDHC_CommandIndexErrorFlag = SDHC_IRQSTAT_CIE_MASK, /*!< Command index error */
  148. kSDHC_DataTimeoutFlag = SDHC_IRQSTAT_DTOE_MASK, /*!< Data timeout error */
  149. kSDHC_DataCrcErrorFlag = SDHC_IRQSTAT_DCE_MASK, /*!< Data CRC error */
  150. kSDHC_DataEndBitErrorFlag = SDHC_IRQSTAT_DEBE_MASK, /*!< Data end bit error */
  151. kSDHC_AutoCommand12ErrorFlag = SDHC_IRQSTAT_AC12E_MASK, /*!< Auto CMD12 error */
  152. kSDHC_DmaErrorFlag = SDHC_IRQSTAT_DMAE_MASK, /*!< DMA error */
  153. kSDHC_CommandErrorFlag = (kSDHC_CommandTimeoutFlag | kSDHC_CommandCrcErrorFlag | kSDHC_CommandEndBitErrorFlag |
  154. kSDHC_CommandIndexErrorFlag), /*!< Command error */
  155. kSDHC_DataErrorFlag = (kSDHC_DataTimeoutFlag | kSDHC_DataCrcErrorFlag | kSDHC_DataEndBitErrorFlag |
  156. kSDHC_AutoCommand12ErrorFlag), /*!< Data error */
  157. kSDHC_ErrorFlag = (kSDHC_CommandErrorFlag | kSDHC_DataErrorFlag | kSDHC_DmaErrorFlag), /*!< All error */
  158. kSDHC_DataFlag = (kSDHC_DataCompleteFlag | kSDHC_DmaCompleteFlag | kSDHC_BufferWriteReadyFlag |
  159. kSDHC_BufferReadReadyFlag | kSDHC_DataErrorFlag | kSDHC_DmaErrorFlag), /*!< Data interrupts */
  160. kSDHC_CommandFlag = (kSDHC_CommandErrorFlag | kSDHC_CommandCompleteFlag), /*!< Command interrupts */
  161. kSDHC_CardDetectFlag = (kSDHC_CardInsertionFlag | kSDHC_CardRemovalFlag), /*!< Card detection interrupts */
  162. kSDHC_AllInterruptFlags = (kSDHC_BlockGapEventFlag | kSDHC_CardInterruptFlag | kSDHC_CommandFlag | kSDHC_DataFlag |
  163. kSDHC_ErrorFlag), /*!< All flags mask */
  164. };
  165. /*! @brief Auto CMD12 error status flag mask */
  166. enum _sdhc_auto_command12_error_status_flag
  167. {
  168. kSDHC_AutoCommand12NotExecutedFlag = SDHC_AC12ERR_AC12NE_MASK, /*!< Not executed error */
  169. kSDHC_AutoCommand12TimeoutFlag = SDHC_AC12ERR_AC12TOE_MASK, /*!< Timeout error */
  170. kSDHC_AutoCommand12EndBitErrorFlag = SDHC_AC12ERR_AC12EBE_MASK, /*!< End bit error */
  171. kSDHC_AutoCommand12CrcErrorFlag = SDHC_AC12ERR_AC12CE_MASK, /*!< CRC error */
  172. kSDHC_AutoCommand12IndexErrorFlag = SDHC_AC12ERR_AC12IE_MASK, /*!< Index error */
  173. kSDHC_AutoCommand12NotIssuedFlag = SDHC_AC12ERR_CNIBAC12E_MASK, /*!< Not issued error */
  174. };
  175. /*! @brief ADMA error status flag mask */
  176. enum _sdhc_adma_error_status_flag
  177. {
  178. kSDHC_AdmaLenghMismatchFlag = SDHC_ADMAES_ADMALME_MASK, /*!< Length mismatch error */
  179. kSDHC_AdmaDescriptorErrorFlag = SDHC_ADMAES_ADMADCE_MASK, /*!< Descriptor error */
  180. };
  181. /*!
  182. * @brief ADMA error state
  183. *
  184. * This state is the detail state when ADMA error has occurred.
  185. */
  186. typedef enum _sdhc_adma_error_state
  187. {
  188. kSDHC_AdmaErrorStateStopDma = 0x00U, /*!< Stop DMA */
  189. kSDHC_AdmaErrorStateFetchDescriptor = 0x01U, /*!< Fetch descriptor */
  190. kSDHC_AdmaErrorStateChangeAddress = 0x02U, /*!< Change address */
  191. kSDHC_AdmaErrorStateTransferData = 0x03U, /*!< Transfer data */
  192. } sdhc_adma_error_state_t;
  193. /*! @brief Force event mask */
  194. enum _sdhc_force_event
  195. {
  196. kSDHC_ForceEventAutoCommand12NotExecuted = SDHC_FEVT_AC12NE_MASK, /*!< Auto CMD12 not executed error */
  197. kSDHC_ForceEventAutoCommand12Timeout = SDHC_FEVT_AC12TOE_MASK, /*!< Auto CMD12 timeout error */
  198. kSDHC_ForceEventAutoCommand12CrcError = SDHC_FEVT_AC12CE_MASK, /*!< Auto CMD12 CRC error */
  199. kSDHC_ForceEventEndBitError = SDHC_FEVT_AC12EBE_MASK, /*!< Auto CMD12 end bit error */
  200. kSDHC_ForceEventAutoCommand12IndexError = SDHC_FEVT_AC12IE_MASK, /*!< Auto CMD12 index error */
  201. kSDHC_ForceEventAutoCommand12NotIssued = SDHC_FEVT_CNIBAC12E_MASK, /*!< Auto CMD12 not issued error */
  202. kSDHC_ForceEventCommandTimeout = SDHC_FEVT_CTOE_MASK, /*!< Command timeout error */
  203. kSDHC_ForceEventCommandCrcError = SDHC_FEVT_CCE_MASK, /*!< Command CRC error */
  204. kSDHC_ForceEventCommandEndBitError = SDHC_FEVT_CEBE_MASK, /*!< Command end bit error */
  205. kSDHC_ForceEventCommandIndexError = SDHC_FEVT_CIE_MASK, /*!< Command index error */
  206. kSDHC_ForceEventDataTimeout = SDHC_FEVT_DTOE_MASK, /*!< Data timeout error */
  207. kSDHC_ForceEventDataCrcError = SDHC_FEVT_DCE_MASK, /*!< Data CRC error */
  208. kSDHC_ForceEventDataEndBitError = SDHC_FEVT_DEBE_MASK, /*!< Data end bit error */
  209. kSDHC_ForceEventAutoCommand12Error = SDHC_FEVT_AC12E_MASK, /*!< Auto CMD12 error */
  210. kSDHC_ForceEventCardInt = SDHC_FEVT_CINT_MASK, /*!< Card interrupt */
  211. kSDHC_ForceEventDmaError = SDHC_FEVT_DMAE_MASK, /*!< Dma error */
  212. kSDHC_ForceEventsAll =
  213. (kSDHC_ForceEventAutoCommand12NotExecuted | kSDHC_ForceEventAutoCommand12Timeout |
  214. kSDHC_ForceEventAutoCommand12CrcError | kSDHC_ForceEventEndBitError | kSDHC_ForceEventAutoCommand12IndexError |
  215. kSDHC_ForceEventAutoCommand12NotIssued | kSDHC_ForceEventCommandTimeout | kSDHC_ForceEventCommandCrcError |
  216. kSDHC_ForceEventCommandEndBitError | kSDHC_ForceEventCommandIndexError | kSDHC_ForceEventDataTimeout |
  217. kSDHC_ForceEventDataCrcError | kSDHC_ForceEventDataEndBitError | kSDHC_ForceEventAutoCommand12Error |
  218. kSDHC_ForceEventCardInt | kSDHC_ForceEventDmaError), /*!< All force event flags mask */
  219. };
  220. /*! @brief Data transfer width */
  221. typedef enum _sdhc_data_bus_width
  222. {
  223. kSDHC_DataBusWidth1Bit = 0U, /*!< 1-bit mode */
  224. kSDHC_DataBusWidth4Bit = 1U, /*!< 4-bit mode */
  225. kSDHC_DataBusWidth8Bit = 2U, /*!< 8-bit mode */
  226. } sdhc_data_bus_width_t;
  227. /*! @brief Endian mode */
  228. typedef enum _sdhc_endian_mode
  229. {
  230. kSDHC_EndianModeBig = 0U, /*!< Big endian mode */
  231. kSDHC_EndianModeHalfWordBig = 1U, /*!< Half word big endian mode */
  232. kSDHC_EndianModeLittle = 2U, /*!< Little endian mode */
  233. } sdhc_endian_mode_t;
  234. /*! @brief DMA mode */
  235. typedef enum _sdhc_dma_mode
  236. {
  237. kSDHC_DmaModeNo = 0U, /*!< No DMA */
  238. kSDHC_DmaModeAdma1 = 1U, /*!< ADMA1 is selected */
  239. kSDHC_DmaModeAdma2 = 2U, /*!< ADMA2 is selected */
  240. } sdhc_dma_mode_t;
  241. /*! @brief SDIO control flag mask */
  242. enum _sdhc_sdio_control_flag
  243. {
  244. kSDHC_StopAtBlockGapFlag = 0x01, /*!< Stop at block gap */
  245. kSDHC_ReadWaitControlFlag = 0x02, /*!< Read wait control */
  246. kSDHC_InterruptAtBlockGapFlag = 0x04, /*!< Interrupt at block gap */
  247. kSDHC_ExactBlockNumberReadFlag = 0x08, /*!< Exact block number read */
  248. };
  249. /*! @brief MMC card boot mode */
  250. typedef enum _sdhc_boot_mode
  251. {
  252. kSDHC_BootModeNormal = 0U, /*!< Normal boot */
  253. kSDHC_BootModeAlternative = 1U, /*!< Alternative boot */
  254. } sdhc_boot_mode_t;
  255. /*! @brief The command type */
  256. typedef enum _sdhc_card_command_type
  257. {
  258. kCARD_CommandTypeNormal = 0U, /*!< Normal command */
  259. kCARD_CommandTypeSuspend = 1U, /*!< Suspend command */
  260. kCARD_CommandTypeResume = 2U, /*!< Resume command */
  261. kCARD_CommandTypeAbort = 3U, /*!< Abort command */
  262. } sdhc_card_command_type_t;
  263. /*!
  264. * @brief The command response type.
  265. *
  266. * Define the command response type from card to host controller.
  267. */
  268. typedef enum _sdhc_card_response_type
  269. {
  270. kCARD_ResponseTypeNone = 0U, /*!< Response type: none */
  271. kCARD_ResponseTypeR1 = 1U, /*!< Response type: R1 */
  272. kCARD_ResponseTypeR1b = 2U, /*!< Response type: R1b */
  273. kCARD_ResponseTypeR2 = 3U, /*!< Response type: R2 */
  274. kCARD_ResponseTypeR3 = 4U, /*!< Response type: R3 */
  275. kCARD_ResponseTypeR4 = 5U, /*!< Response type: R4 */
  276. kCARD_ResponseTypeR5 = 6U, /*!< Response type: R5 */
  277. kCARD_ResponseTypeR5b = 7U, /*!< Response type: R5b */
  278. kCARD_ResponseTypeR6 = 8U, /*!< Response type: R6 */
  279. kCARD_ResponseTypeR7 = 9U, /*!< Response type: R7 */
  280. } sdhc_card_response_type_t;
  281. /*! @brief The alignment size for ADDRESS filed in ADMA1's descriptor */
  282. #define SDHC_ADMA1_ADDRESS_ALIGN (4096U)
  283. /*! @brief The alignment size for LENGTH field in ADMA1's descriptor */
  284. #define SDHC_ADMA1_LENGTH_ALIGN (4096U)
  285. /*! @brief The alignment size for ADDRESS field in ADMA2's descriptor */
  286. #define SDHC_ADMA2_ADDRESS_ALIGN (4U)
  287. /*! @brief The alignment size for LENGTH filed in ADMA2's descriptor */
  288. #define SDHC_ADMA2_LENGTH_ALIGN (4U)
  289. /* ADMA1 descriptor table
  290. * |------------------------|---------|--------------------------|
  291. * | Address/page field |Reserved | Attribute |
  292. * |------------------------|---------|--------------------------|
  293. * |31 12|11 6|05 |04 |03|02 |01 |00 |
  294. * |------------------------|---------|----|----|--|---|---|-----|
  295. * | address or data length | 000000 |Act2|Act1| 0|Int|End|Valid|
  296. * |------------------------|---------|----|----|--|---|---|-----|
  297. *
  298. *
  299. * |------|------|-----------------|-------|-------------|
  300. * | Act2 | Act1 | Comment | 31-28 | 27 - 12 |
  301. * |------|------|-----------------|---------------------|
  302. * | 0 | 0 | No op | Don't care |
  303. * |------|------|-----------------|-------|-------------|
  304. * | 0 | 1 | Set data length | 0000 | Data Length |
  305. * |------|------|-----------------|-------|-------------|
  306. * | 1 | 0 | Transfer data | Data address |
  307. * |------|------|-----------------|---------------------|
  308. * | 1 | 1 | Link descriptor | Descriptor address |
  309. * |------|------|-----------------|---------------------|
  310. */
  311. /*! @brief The bit shift for ADDRESS filed in ADMA1's descriptor */
  312. #define SDHC_ADMA1_DESCRIPTOR_ADDRESS_SHIFT (12U)
  313. /*! @brief The bit mask for ADDRESS field in ADMA1's descriptor */
  314. #define SDHC_ADMA1_DESCRIPTOR_ADDRESS_MASK (0xFFFFFU)
  315. /*! @brief The bit shift for LENGTH filed in ADMA1's descriptor */
  316. #define SDHC_ADMA1_DESCRIPTOR_LENGTH_SHIFT (12U)
  317. /*! @brief The mask for LENGTH field in ADMA1's descriptor */
  318. #define SDHC_ADMA1_DESCRIPTOR_LENGTH_MASK (0xFFFFU)
  319. /*! @brief The maximum value of LENGTH filed in ADMA1's descriptor */
  320. #define SDHC_ADMA1_DESCRIPTOR_MAX_LENGTH_PER_ENTRY (SDHC_ADMA1_DESCRIPTOR_LENGTH_MASK + 1U)
  321. /*! @brief The mask for the control/status field in ADMA1 descriptor */
  322. enum _sdhc_adma1_descriptor_flag
  323. {
  324. kSDHC_Adma1DescriptorValidFlag = (1U << 0U), /*!< Valid flag */
  325. kSDHC_Adma1DescriptorEndFlag = (1U << 1U), /*!< End flag */
  326. kSDHC_Adma1DescriptorInterrupFlag = (1U << 2U), /*!< Interrupt flag */
  327. kSDHC_Adma1DescriptorActivity1Flag = (1U << 4U), /*!< Activity 1 flag */
  328. kSDHC_Adma1DescriptorActivity2Flag = (1U << 5U), /*!< Activity 2 flag */
  329. kSDHC_Adma1DescriptorTypeNop = (kSDHC_Adma1DescriptorValidFlag), /*!< No operation */
  330. kSDHC_Adma1DescriptorTypeTransfer =
  331. (kSDHC_Adma1DescriptorActivity2Flag | kSDHC_Adma1DescriptorValidFlag), /*!< Transfer data */
  332. kSDHC_Adma1DescriptorTypeLink = (kSDHC_Adma1DescriptorActivity1Flag | kSDHC_Adma1DescriptorActivity2Flag |
  333. kSDHC_Adma1DescriptorValidFlag), /*!< Link descriptor */
  334. kSDHC_Adma1DescriptorTypeSetLength =
  335. (kSDHC_Adma1DescriptorActivity1Flag | kSDHC_Adma1DescriptorValidFlag), /*!< Set data length */
  336. };
  337. /* ADMA2 descriptor table
  338. * |----------------|---------------|-------------|--------------------------|
  339. * | Address field | Length | Reserved | Attribute |
  340. * |----------------|---------------|-------------|--------------------------|
  341. * |63 32|31 16|15 06|05 |04 |03|02 |01 |00 |
  342. * |----------------|---------------|-------------|----|----|--|---|---|-----|
  343. * | 32-bit address | 16-bit length | 0000000000 |Act2|Act1| 0|Int|End|Valid|
  344. * |----------------|---------------|-------------|----|----|--|---|---|-----|
  345. *
  346. *
  347. * | Act2 | Act1 | Comment | Operation |
  348. * |------|------|-----------------|-------------------------------------------------------------------|
  349. * | 0 | 0 | No op | Don't care |
  350. * |------|------|-----------------|-------------------------------------------------------------------|
  351. * | 0 | 1 | Reserved | Read this line and go to next one |
  352. * |------|------|-----------------|-------------------------------------------------------------------|
  353. * | 1 | 0 | Transfer data | Transfer data with address and length set in this descriptor line |
  354. * |------|------|-----------------|-------------------------------------------------------------------|
  355. * | 1 | 1 | Link descriptor | Link to another descriptor |
  356. * |------|------|-----------------|-------------------------------------------------------------------|
  357. */
  358. /*! @brief The bit shift for LENGTH field in ADMA2's descriptor */
  359. #define SDHC_ADMA2_DESCRIPTOR_LENGTH_SHIFT (16U)
  360. /*! @brief The bit mask for LENGTH field in ADMA2's descriptor */
  361. #define SDHC_ADMA2_DESCRIPTOR_LENGTH_MASK (0xFFFFU)
  362. /*! @brief The maximum value of LENGTH field in ADMA2's descriptor */
  363. #define SDHC_ADMA2_DESCRIPTOR_MAX_LENGTH_PER_ENTRY (SDHC_ADMA2_DESCRIPTOR_LENGTH_MASK)
  364. /*! @brief ADMA1 descriptor control and status mask */
  365. enum _sdhc_adma2_descriptor_flag
  366. {
  367. kSDHC_Adma2DescriptorValidFlag = (1U << 0U), /*!< Valid flag */
  368. kSDHC_Adma2DescriptorEndFlag = (1U << 1U), /*!< End flag */
  369. kSDHC_Adma2DescriptorInterruptFlag = (1U << 2U), /*!< Interrupt flag */
  370. kSDHC_Adma2DescriptorActivity1Flag = (1U << 4U), /*!< Activity 1 mask */
  371. kSDHC_Adma2DescriptorActivity2Flag = (1U << 5U), /*!< Activity 2 mask */
  372. kSDHC_Adma2DescriptorTypeNop = (kSDHC_Adma2DescriptorValidFlag), /*!< No operation */
  373. kSDHC_Adma2DescriptorTypeReserved =
  374. (kSDHC_Adma2DescriptorActivity1Flag | kSDHC_Adma2DescriptorValidFlag), /*!< Reserved */
  375. kSDHC_Adma2DescriptorTypeTransfer =
  376. (kSDHC_Adma2DescriptorActivity2Flag | kSDHC_Adma2DescriptorValidFlag), /*!< Transfer type */
  377. kSDHC_Adma2DescriptorTypeLink = (kSDHC_Adma2DescriptorActivity1Flag | kSDHC_Adma2DescriptorActivity2Flag |
  378. kSDHC_Adma2DescriptorValidFlag), /*!< Link type */
  379. };
  380. /*! @brief Defines the adma1 descriptor structure. */
  381. typedef uint32_t sdhc_adma1_descriptor_t;
  382. /*! @brief Defines the ADMA2 descriptor structure. */
  383. typedef struct _sdhc_adma2_descriptor
  384. {
  385. uint32_t attribute; /*!< The control and status field */
  386. const uint32_t *address; /*!< The address field */
  387. } sdhc_adma2_descriptor_t;
  388. /*!
  389. * @brief SDHC capability information.
  390. *
  391. * Defines a structure to save the capability information of SDHC.
  392. */
  393. typedef struct _sdhc_capability
  394. {
  395. uint32_t specVersion; /*!< Specification version */
  396. uint32_t vendorVersion; /*!< Vendor version */
  397. uint32_t maxBlockLength; /*!< Maximum block length united as byte */
  398. uint32_t maxBlockCount; /*!< Maximum block count can be set one time */
  399. uint32_t flags; /*!< Capability flags to indicate the support information(_sdhc_capability_flag) */
  400. } sdhc_capability_t;
  401. /*! @brief Card transfer configuration.
  402. *
  403. * Define structure to configure the transfer-related command index/argument/flags and data block
  404. * size/data block numbers. This structure needs to be filled each time a command is sent to the card.
  405. */
  406. typedef struct _sdhc_transfer_config
  407. {
  408. size_t dataBlockSize; /*!< Data block size */
  409. uint32_t dataBlockCount; /*!< Data block count */
  410. uint32_t commandArgument; /*!< Command argument */
  411. uint32_t commandIndex; /*!< Command index */
  412. uint32_t flags; /*!< Transfer flags(_sdhc_transfer_flag) */
  413. } sdhc_transfer_config_t;
  414. /*! @brief Data structure to configure the MMC boot feature */
  415. typedef struct _sdhc_boot_config
  416. {
  417. uint32_t ackTimeoutCount; /*!< Timeout value for the boot ACK. The available range is 0 ~ 15. */
  418. sdhc_boot_mode_t bootMode; /*!< Boot mode selection. */
  419. uint32_t blockCount; /*!< Stop at block gap value of automatic mode. Available range is 0 ~ 65535. */
  420. bool enableBootAck; /*!< Enable or disable boot ACK */
  421. bool enableBoot; /*!< Enable or disable fast boot */
  422. bool enableAutoStopAtBlockGap; /*!< Enable or disable auto stop at block gap function in boot period */
  423. } sdhc_boot_config_t;
  424. /*! @brief Data structure to initialize the SDHC */
  425. typedef struct _sdhc_config
  426. {
  427. bool cardDetectDat3; /*!< Enable DAT3 as card detection pin */
  428. sdhc_endian_mode_t endianMode; /*!< Endian mode */
  429. sdhc_dma_mode_t dmaMode; /*!< DMA mode */
  430. uint32_t readWatermarkLevel; /*!< Watermark level for DMA read operation. Available range is 1 ~ 128. */
  431. uint32_t writeWatermarkLevel; /*!< Watermark level for DMA write operation. Available range is 1 ~ 128. */
  432. } sdhc_config_t;
  433. /*!
  434. * @brief Card data descriptor
  435. *
  436. * Defines a structure to contain data-related attribute. 'enableIgnoreError' is used for the case that upper card
  437. * driver
  438. * want to ignore the error event to read/write all the data not to stop read/write immediately when error event
  439. * happen for example bus testing procedure for MMC card.
  440. */
  441. typedef struct _sdhc_data
  442. {
  443. bool enableAutoCommand12; /*!< Enable auto CMD12 */
  444. bool enableIgnoreError; /*!< Enable to ignore error event to read/write all the data */
  445. size_t blockSize; /*!< Block size */
  446. uint32_t blockCount; /*!< Block count */
  447. uint32_t *rxData; /*!< Buffer to save data read */
  448. const uint32_t *txData; /*!< Data buffer to write */
  449. } sdhc_data_t;
  450. /*!
  451. * @brief Card command descriptor
  452. *
  453. * Define card command-related attribute.
  454. */
  455. typedef struct _sdhc_command
  456. {
  457. uint32_t index; /*!< Command index */
  458. uint32_t argument; /*!< Command argument */
  459. sdhc_card_command_type_t type; /*!< Command type */
  460. sdhc_card_response_type_t responseType; /*!< Command response type */
  461. uint32_t response[4U]; /*!< Response for this command */
  462. uint32_t responseErrorFlags; /*!< response error flag, the flag which need to check
  463. the command reponse*/
  464. } sdhc_command_t;
  465. /*! @brief Transfer state */
  466. typedef struct _sdhc_transfer
  467. {
  468. sdhc_data_t *data; /*!< Data to transfer */
  469. sdhc_command_t *command; /*!< Command to send */
  470. } sdhc_transfer_t;
  471. /*! @brief SDHC handle typedef */
  472. typedef struct _sdhc_handle sdhc_handle_t;
  473. /*! @brief SDHC callback functions. */
  474. typedef struct _sdhc_transfer_callback
  475. {
  476. void (*CardInserted)(void); /*!< Card inserted occurs when DAT3/CD pin is for card detect */
  477. void (*CardRemoved)(void); /*!< Card removed occurs */
  478. void (*SdioInterrupt)(void); /*!< SDIO card interrupt occurs */
  479. void (*SdioBlockGap)(void); /*!< SDIO card stopped at block gap occurs */
  480. void (*TransferComplete)(SDHC_Type *base,
  481. sdhc_handle_t *handle,
  482. status_t status,
  483. void *userData); /*!< Transfer complete callback */
  484. } sdhc_transfer_callback_t;
  485. /*!
  486. * @brief SDHC handle
  487. *
  488. * Defines the structure to save the SDHC state information and callback function. The detailed interrupt status when
  489. * sending a command or transfering data can be obtained from the interruptFlags field by using the mask defined in
  490. * sdhc_interrupt_flag_t.
  491. *
  492. * @note All the fields except interruptFlags and transferredWords must be allocated by the user.
  493. */
  494. struct _sdhc_handle
  495. {
  496. /* Transfer parameter */
  497. sdhc_data_t *volatile data; /*!< Data to transfer */
  498. sdhc_command_t *volatile command; /*!< Command to send */
  499. /* Transfer status */
  500. volatile uint32_t interruptFlags; /*!< Interrupt flags of last transaction */
  501. volatile uint32_t transferredWords; /*!< Words transferred by DATAPORT way */
  502. /* Callback functions */
  503. sdhc_transfer_callback_t callback; /*!< Callback function */
  504. void *userData; /*!< Parameter for transfer complete callback */
  505. };
  506. /*! @brief SDHC transfer function. */
  507. typedef status_t (*sdhc_transfer_function_t)(SDHC_Type *base, sdhc_transfer_t *content);
  508. /*! @brief SDHC host descriptor */
  509. typedef struct _sdhc_host
  510. {
  511. SDHC_Type *base; /*!< SDHC peripheral base address */
  512. uint32_t sourceClock_Hz; /*!< SDHC source clock frequency united in Hz */
  513. sdhc_config_t config; /*!< SDHC configuration */
  514. sdhc_capability_t capability; /*!< SDHC capability information */
  515. sdhc_transfer_function_t transfer; /*!< SDHC transfer function */
  516. } sdhc_host_t;
  517. /*************************************************************************************************
  518. * API
  519. ************************************************************************************************/
  520. #if defined(__cplusplus)
  521. extern "C" {
  522. #endif
  523. /*!
  524. * @name Initialization and deinitialization
  525. * @{
  526. */
  527. /*!
  528. * @brief SDHC module initialization function.
  529. *
  530. * Configures the SDHC according to the user configuration.
  531. *
  532. * Example:
  533. @code
  534. sdhc_config_t config;
  535. config.cardDetectDat3 = false;
  536. config.endianMode = kSDHC_EndianModeLittle;
  537. config.dmaMode = kSDHC_DmaModeAdma2;
  538. config.readWatermarkLevel = 128U;
  539. config.writeWatermarkLevel = 128U;
  540. SDHC_Init(SDHC, &config);
  541. @endcode
  542. *
  543. * @param base SDHC peripheral base address.
  544. * @param config SDHC configuration information.
  545. * @retval kStatus_Success Operate successfully.
  546. */
  547. void SDHC_Init(SDHC_Type *base, const sdhc_config_t *config);
  548. /*!
  549. * @brief Deinitializes the SDHC.
  550. *
  551. * @param base SDHC peripheral base address.
  552. */
  553. void SDHC_Deinit(SDHC_Type *base);
  554. /*!
  555. * @brief Resets the SDHC.
  556. *
  557. * @param base SDHC peripheral base address.
  558. * @param mask The reset type mask(_sdhc_reset).
  559. * @param timeout Timeout for reset.
  560. * @retval true Reset successfully.
  561. * @retval false Reset failed.
  562. */
  563. bool SDHC_Reset(SDHC_Type *base, uint32_t mask, uint32_t timeout);
  564. /* @} */
  565. /*!
  566. * @name DMA Control
  567. * @{
  568. */
  569. /*!
  570. * @brief Sets the ADMA descriptor table configuration.
  571. *
  572. * @param base SDHC peripheral base address.
  573. * @param dmaMode DMA mode.
  574. * @param table ADMA table address.
  575. * @param tableWords ADMA table buffer length united as Words.
  576. * @param data Data buffer address.
  577. * @param dataBytes Data length united as bytes.
  578. * @retval kStatus_OutOfRange ADMA descriptor table length isn't enough to describe data.
  579. * @retval kStatus_Success Operate successfully.
  580. */
  581. status_t SDHC_SetAdmaTableConfig(SDHC_Type *base,
  582. sdhc_dma_mode_t dmaMode,
  583. uint32_t *table,
  584. uint32_t tableWords,
  585. const uint32_t *data,
  586. uint32_t dataBytes);
  587. /* @} */
  588. /*!
  589. * @name Interrupts
  590. * @{
  591. */
  592. /*!
  593. * @brief Enables the interrupt status.
  594. *
  595. * @param base SDHC peripheral base address.
  596. * @param mask Interrupt status flags mask(_sdhc_interrupt_status_flag).
  597. */
  598. static inline void SDHC_EnableInterruptStatus(SDHC_Type *base, uint32_t mask)
  599. {
  600. base->IRQSTATEN |= mask;
  601. }
  602. /*!
  603. * @brief Disables the interrupt status.
  604. *
  605. * @param base SDHC peripheral base address.
  606. * @param mask The interrupt status flags mask(_sdhc_interrupt_status_flag).
  607. */
  608. static inline void SDHC_DisableInterruptStatus(SDHC_Type *base, uint32_t mask)
  609. {
  610. base->IRQSTATEN &= ~mask;
  611. }
  612. /*!
  613. * @brief Enables the interrupt signal corresponding to the interrupt status flag.
  614. *
  615. * @param base SDHC peripheral base address.
  616. * @param mask The interrupt status flags mask(_sdhc_interrupt_status_flag).
  617. */
  618. static inline void SDHC_EnableInterruptSignal(SDHC_Type *base, uint32_t mask)
  619. {
  620. base->IRQSIGEN |= mask;
  621. }
  622. /*!
  623. * @brief Disables the interrupt signal corresponding to the interrupt status flag.
  624. *
  625. * @param base SDHC peripheral base address.
  626. * @param mask The interrupt status flags mask(_sdhc_interrupt_status_flag).
  627. */
  628. static inline void SDHC_DisableInterruptSignal(SDHC_Type *base, uint32_t mask)
  629. {
  630. base->IRQSIGEN &= ~mask;
  631. }
  632. /* @} */
  633. /*!
  634. * @name Status
  635. * @{
  636. */
  637. /*!
  638. * @brief Gets the current interrupt status.
  639. *
  640. * @param base SDHC peripheral base address.
  641. * @return Current interrupt status flags mask(_sdhc_interrupt_status_flag).
  642. */
  643. static inline uint32_t SDHC_GetInterruptStatusFlags(SDHC_Type *base)
  644. {
  645. return base->IRQSTAT;
  646. }
  647. /*!
  648. * @brief Clears a specified interrupt status.
  649. *
  650. * @param base SDHC peripheral base address.
  651. * @param mask The interrupt status flags mask(_sdhc_interrupt_status_flag).
  652. */
  653. static inline void SDHC_ClearInterruptStatusFlags(SDHC_Type *base, uint32_t mask)
  654. {
  655. base->IRQSTAT = mask;
  656. }
  657. /*!
  658. * @brief Gets the status of auto command 12 error.
  659. *
  660. * @param base SDHC peripheral base address.
  661. * @return Auto command 12 error status flags mask(_sdhc_auto_command12_error_status_flag).
  662. */
  663. static inline uint32_t SDHC_GetAutoCommand12ErrorStatusFlags(SDHC_Type *base)
  664. {
  665. return base->AC12ERR;
  666. }
  667. /*!
  668. * @brief Gets the status of the ADMA error.
  669. *
  670. * @param base SDHC peripheral base address.
  671. * @return ADMA error status flags mask(_sdhc_adma_error_status_flag).
  672. */
  673. static inline uint32_t SDHC_GetAdmaErrorStatusFlags(SDHC_Type *base)
  674. {
  675. return base->ADMAES;
  676. }
  677. /*!
  678. * @brief Gets a present status.
  679. *
  680. * This function gets the present SDHC's status except for an interrupt status and an error status.
  681. *
  682. * @param base SDHC peripheral base address.
  683. * @return Present SDHC's status flags mask(_sdhc_present_status_flag).
  684. */
  685. static inline uint32_t SDHC_GetPresentStatusFlags(SDHC_Type *base)
  686. {
  687. return base->PRSSTAT;
  688. }
  689. /* @} */
  690. /*!
  691. * @name Bus Operations
  692. * @{
  693. */
  694. /*!
  695. * @brief Gets the capability information.
  696. *
  697. * @param base SDHC peripheral base address.
  698. * @param capability Structure to save capability information.
  699. */
  700. void SDHC_GetCapability(SDHC_Type *base, sdhc_capability_t *capability);
  701. /*!
  702. * @brief Enables or disables the SD bus clock.
  703. *
  704. * @param base SDHC peripheral base address.
  705. * @param enable True to enable, false to disable.
  706. */
  707. static inline void SDHC_EnableSdClock(SDHC_Type *base, bool enable)
  708. {
  709. if (enable)
  710. {
  711. base->SYSCTL |= SDHC_SYSCTL_SDCLKEN_MASK;
  712. }
  713. else
  714. {
  715. base->SYSCTL &= ~SDHC_SYSCTL_SDCLKEN_MASK;
  716. }
  717. }
  718. /*!
  719. * @brief Sets the SD bus clock frequency.
  720. *
  721. * @param base SDHC peripheral base address.
  722. * @param srcClock_Hz SDHC source clock frequency united in Hz.
  723. * @param busClock_Hz SD bus clock frequency united in Hz.
  724. *
  725. * @return The nearest frequency of busClock_Hz configured to SD bus.
  726. */
  727. uint32_t SDHC_SetSdClock(SDHC_Type *base, uint32_t srcClock_Hz, uint32_t busClock_Hz);
  728. /*!
  729. * @brief Sends 80 clocks to the card to set it to the active state.
  730. *
  731. * This function must be called each time the card is inserted to ensure that the card can receive the command
  732. * correctly.
  733. *
  734. * @param base SDHC peripheral base address.
  735. * @param timeout Timeout to initialize card.
  736. * @retval true Set card active successfully.
  737. * @retval false Set card active failed.
  738. */
  739. bool SDHC_SetCardActive(SDHC_Type *base, uint32_t timeout);
  740. /*!
  741. * @brief Sets the data transfer width.
  742. *
  743. * @param base SDHC peripheral base address.
  744. * @param width Data transfer width.
  745. */
  746. static inline void SDHC_SetDataBusWidth(SDHC_Type *base, sdhc_data_bus_width_t width)
  747. {
  748. base->PROCTL = ((base->PROCTL & ~SDHC_PROCTL_DTW_MASK) | SDHC_PROCTL_DTW(width));
  749. }
  750. /*!
  751. * @brief Sets the card transfer-related configuration.
  752. *
  753. * This function fills the card transfer-related command argument/transfer flag/data size. The command and data are sent
  754. by
  755. * SDHC after calling this function.
  756. *
  757. * Example:
  758. @code
  759. sdhc_transfer_config_t transferConfig;
  760. transferConfig.dataBlockSize = 512U;
  761. transferConfig.dataBlockCount = 2U;
  762. transferConfig.commandArgument = 0x01AAU;
  763. transferConfig.commandIndex = 8U;
  764. transferConfig.flags |= (kSDHC_EnableDmaFlag | kSDHC_EnableAutoCommand12Flag | kSDHC_MultipleBlockFlag);
  765. SDHC_SetTransferConfig(SDHC, &transferConfig);
  766. @endcode
  767. *
  768. * @param base SDHC peripheral base address.
  769. * @param config Command configuration structure.
  770. */
  771. void SDHC_SetTransferConfig(SDHC_Type *base, const sdhc_transfer_config_t *config);
  772. /*!
  773. * @brief Gets the command response.
  774. *
  775. * @param base SDHC peripheral base address.
  776. * @param index The index of response register, range from 0 to 3.
  777. * @return Response register transfer.
  778. */
  779. static inline uint32_t SDHC_GetCommandResponse(SDHC_Type *base, uint32_t index)
  780. {
  781. assert(index < 4U);
  782. return base->CMDRSP[index];
  783. }
  784. /*!
  785. * @brief Fills the the data port.
  786. *
  787. * This function is used to implement the data transfer by Data Port instead of DMA.
  788. *
  789. * @param base SDHC peripheral base address.
  790. * @param data The data about to be sent.
  791. */
  792. static inline void SDHC_WriteData(SDHC_Type *base, uint32_t data)
  793. {
  794. base->DATPORT = data;
  795. }
  796. /*!
  797. * @brief Retrieves the data from the data port.
  798. *
  799. * This function is used to implement the data transfer by Data Port instead of DMA.
  800. *
  801. * @param base SDHC peripheral base address.
  802. * @return The data has been read.
  803. */
  804. static inline uint32_t SDHC_ReadData(SDHC_Type *base)
  805. {
  806. return base->DATPORT;
  807. }
  808. /*!
  809. * @brief Enables or disables a wakeup event in low-power mode.
  810. *
  811. * @param base SDHC peripheral base address.
  812. * @param mask Wakeup events mask(_sdhc_wakeup_event).
  813. * @param enable True to enable, false to disable.
  814. */
  815. static inline void SDHC_EnableWakeupEvent(SDHC_Type *base, uint32_t mask, bool enable)
  816. {
  817. if (enable)
  818. {
  819. base->PROCTL |= mask;
  820. }
  821. else
  822. {
  823. base->PROCTL &= ~mask;
  824. }
  825. }
  826. /*!
  827. * @brief Enables or disables the card detection level for testing.
  828. *
  829. * @param base SDHC peripheral base address.
  830. * @param enable True to enable, false to disable.
  831. */
  832. static inline void SDHC_EnableCardDetectTest(SDHC_Type *base, bool enable)
  833. {
  834. if (enable)
  835. {
  836. base->PROCTL |= SDHC_PROCTL_CDSS_MASK;
  837. }
  838. else
  839. {
  840. base->PROCTL &= ~SDHC_PROCTL_CDSS_MASK;
  841. }
  842. }
  843. /*!
  844. * @brief Sets the card detection test level.
  845. *
  846. * This function sets the card detection test level to indicate whether the card is inserted into the SDHC when DAT[3]/
  847. * CD pin is selected as a card detection pin. This function can also assert the pin logic when DAT[3]/CD pin is
  848. * selected
  849. * as the card detection pin.
  850. *
  851. * @param base SDHC peripheral base address.
  852. * @param high True to set the card detect level to high.
  853. */
  854. static inline void SDHC_SetCardDetectTestLevel(SDHC_Type *base, bool high)
  855. {
  856. if (high)
  857. {
  858. base->PROCTL |= SDHC_PROCTL_CDTL_MASK;
  859. }
  860. else
  861. {
  862. base->PROCTL &= ~SDHC_PROCTL_CDTL_MASK;
  863. }
  864. }
  865. /*!
  866. * @brief Enables or disables the SDIO card control.
  867. *
  868. * @param base SDHC peripheral base address.
  869. * @param mask SDIO card control flags mask(_sdhc_sdio_control_flag).
  870. * @param enable True to enable, false to disable.
  871. */
  872. void SDHC_EnableSdioControl(SDHC_Type *base, uint32_t mask, bool enable);
  873. /*!
  874. * @brief Restarts a transaction which has stopped at the block GAP for the SDIO card.
  875. *
  876. * @param base SDHC peripheral base address.
  877. */
  878. static inline void SDHC_SetContinueRequest(SDHC_Type *base)
  879. {
  880. base->PROCTL |= SDHC_PROCTL_CREQ_MASK;
  881. }
  882. /*!
  883. * @brief Configures the MMC boot feature.
  884. *
  885. * Example:
  886. @code
  887. sdhc_boot_config_t config;
  888. config.ackTimeoutCount = 4;
  889. config.bootMode = kSDHC_BootModeNormal;
  890. config.blockCount = 5;
  891. config.enableBootAck = true;
  892. config.enableBoot = true;
  893. config.enableAutoStopAtBlockGap = true;
  894. SDHC_SetMmcBootConfig(SDHC, &config);
  895. @endcode
  896. *
  897. * @param base SDHC peripheral base address.
  898. * @param config The MMC boot configuration information.
  899. */
  900. void SDHC_SetMmcBootConfig(SDHC_Type *base, const sdhc_boot_config_t *config);
  901. /*!
  902. * @brief Forces generating events according to the given mask.
  903. *
  904. * @param base SDHC peripheral base address.
  905. * @param mask The force events mask(_sdhc_force_event).
  906. */
  907. static inline void SDHC_SetForceEvent(SDHC_Type *base, uint32_t mask)
  908. {
  909. base->FEVT = mask;
  910. }
  911. /* @} */
  912. /*!
  913. * @name Transactional
  914. * @{
  915. */
  916. /*!
  917. * @brief Transfers the command/data using a blocking method.
  918. *
  919. * This function waits until the command response/data is received or the SDHC encounters an error by polling the status
  920. * flag.
  921. * This function support non word align data addr transfer support, if data buffer addr is not align in DMA mode,
  922. * the API will continue finish the transfer by polling IO directly
  923. * The application must not call this API in multiple threads at the same time. Because of that this API doesn't support
  924. * the re-entry mechanism.
  925. *
  926. * @note There is no need to call the API 'SDHC_TransferCreateHandle' when calling this API.
  927. *
  928. * @param base SDHC peripheral base address.
  929. * @param admaTable ADMA table address, can't be null if transfer way is ADMA1/ADMA2.
  930. * @param admaTableWords ADMA table length united as words, can't be 0 if transfer way is ADMA1/ADMA2.
  931. * @param transfer Transfer content.
  932. * @retval kStatus_InvalidArgument Argument is invalid.
  933. * @retval kStatus_SDHC_PrepareAdmaDescriptorFailed Prepare ADMA descriptor failed.
  934. * @retval kStatus_SDHC_SendCommandFailed Send command failed.
  935. * @retval kStatus_SDHC_TransferDataFailed Transfer data failed.
  936. * @retval kStatus_Success Operate successfully.
  937. */
  938. status_t SDHC_TransferBlocking(SDHC_Type *base,
  939. uint32_t *admaTable,
  940. uint32_t admaTableWords,
  941. sdhc_transfer_t *transfer);
  942. /*!
  943. * @brief Creates the SDHC handle.
  944. *
  945. * @param base SDHC peripheral base address.
  946. * @param handle SDHC handle pointer.
  947. * @param callback Structure pointer to contain all callback functions.
  948. * @param userData Callback function parameter.
  949. */
  950. void SDHC_TransferCreateHandle(SDHC_Type *base,
  951. sdhc_handle_t *handle,
  952. const sdhc_transfer_callback_t *callback,
  953. void *userData);
  954. /*!
  955. * @brief Transfers the command/data using an interrupt and an asynchronous method.
  956. *
  957. * This function sends a command and data and returns immediately. It doesn't wait the transfer complete or encounter an
  958. * error.
  959. * This function support non word align data addr transfer support, if data buffer addr is not align in DMA mode,
  960. * the API will continue finish the transfer by polling IO directly
  961. * The application must not call this API in multiple threads at the same time. Because of that this API doesn't support
  962. * the re-entry mechanism.
  963. *
  964. * @note Call the API 'SDHC_TransferCreateHandle' when calling this API.
  965. *
  966. * @param base SDHC peripheral base address.
  967. * @param handle SDHC handle.
  968. * @param admaTable ADMA table address, can't be null if transfer way is ADMA1/ADMA2.
  969. * @param admaTableWords ADMA table length united as words, can't be 0 if transfer way is ADMA1/ADMA2.
  970. * @param transfer Transfer content.
  971. * @retval kStatus_InvalidArgument Argument is invalid.
  972. * @retval kStatus_SDHC_BusyTransferring Busy transferring.
  973. * @retval kStatus_SDHC_PrepareAdmaDescriptorFailed Prepare ADMA descriptor failed.
  974. * @retval kStatus_Success Operate successfully.
  975. */
  976. status_t SDHC_TransferNonBlocking(
  977. SDHC_Type *base, sdhc_handle_t *handle, uint32_t *admaTable, uint32_t admaTableWords, sdhc_transfer_t *transfer);
  978. /*!
  979. * @brief IRQ handler for the SDHC.
  980. *
  981. * This function deals with the IRQs on the given host controller.
  982. *
  983. * @param base SDHC peripheral base address.
  984. * @param handle SDHC handle.
  985. */
  986. void SDHC_TransferHandleIRQ(SDHC_Type *base, sdhc_handle_t *handle);
  987. /* @} */
  988. #if defined(__cplusplus)
  989. }
  990. #endif
  991. /*! @} */
  992. #endif /* _FSL_SDHC_H_*/