fsl_sdhc.h 44 KB

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