nu_sdh.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. /**************************************************************************//**
  2. * @file nu_sdh.h
  3. * @version V1.00
  4. * @brief SDH driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2021 Nuvoton Technology Corp. All rights reserved.
  8. *****************************************************************************/
  9. #include <stdio.h>
  10. #ifndef __NU_SDH_H__
  11. #define __NU_SDH_H__
  12. #ifdef __cplusplus
  13. extern "C"
  14. {
  15. #endif
  16. /** @addtogroup Standard_Driver Standard Driver
  17. @{
  18. */
  19. /** @addtogroup SDH_Driver SDH Driver
  20. @{
  21. */
  22. /** @addtogroup SDH_EXPORTED_CONSTANTS SDH Exported Constants
  23. @{
  24. */
  25. #define SDH_ERR_ID 0xFFFF0100ul /*!< SDH error ID \hideinitializer */
  26. #define SDH_TIMEOUT (SDH_ERR_ID|0x01ul) /*!< Timeout \hideinitializer */
  27. #define SDH_NO_MEMORY (SDH_ERR_ID|0x02ul) /*!< OOM \hideinitializer */
  28. /*-- function return value */
  29. #define Successful 0ul /*!< Success \hideinitializer */
  30. #define Fail 1ul /*!< Failed \hideinitializer */
  31. /*--- define type of SD card or MMC */
  32. #define SDH_TYPE_UNKNOWN 0ul /*!< Unknown card type \hideinitializer */
  33. #define SDH_TYPE_SD_HIGH 1ul /*!< SDHC card \hideinitializer */
  34. #define SDH_TYPE_SD_LOW 2ul /*!< SD card \hideinitializer */
  35. #define SDH_TYPE_MMC 3ul /*!< MMC card \hideinitializer */
  36. #define SDH_TYPE_EMMC 4ul /*!< eMMC card \hideinitializer */
  37. /* SD error */
  38. #define SDH_NO_SD_CARD (SDH_ERR_ID|0x10ul) /*!< Card removed \hideinitializer */
  39. #define SDH_ERR_DEVICE (SDH_ERR_ID|0x11ul) /*!< Device error \hideinitializer */
  40. #define SDH_INIT_TIMEOUT (SDH_ERR_ID|0x12ul) /*!< Card init timeout \hideinitializer */
  41. #define SDH_SELECT_ERROR (SDH_ERR_ID|0x13ul) /*!< Card select error \hideinitializer */
  42. #define SDH_WRITE_PROTECT (SDH_ERR_ID|0x14ul) /*!< Card write protect \hideinitializer */
  43. #define SDH_INIT_ERROR (SDH_ERR_ID|0x15ul) /*!< Card init error \hideinitializer */
  44. #define SDH_CRC7_ERROR (SDH_ERR_ID|0x16ul) /*!< CRC 7 error \hideinitializer */
  45. #define SDH_CRC16_ERROR (SDH_ERR_ID|0x17ul) /*!< CRC 16 error \hideinitializer */
  46. #define SDH_CRC_ERROR (SDH_ERR_ID|0x18ul) /*!< CRC error \hideinitializer */
  47. #define SDH_CMD8_ERROR (SDH_ERR_ID|0x19ul) /*!< Command 8 error \hideinitializer */
  48. #define MMC_FREQ 20000ul /*!< output 20MHz to MMC \hideinitializer */
  49. #define SD_FREQ 25000ul /*!< output 25MHz to SD \hideinitializer */
  50. #define SDHC_FREQ 50000ul /*!< output 50MHz to SDH \hideinitializer */
  51. #define SD_PORT0 (1 << 0) /*!< Card select SD0 \hideinitializer */
  52. #define SD_PORT1 (1 << 2) /*!< Card select SD1 \hideinitializer */
  53. #define CardDetect_From_GPIO (1ul << 8) /*!< Card detection pin is GPIO \hideinitializer */
  54. #define CardDetect_From_DAT3 (1ul << 9) /*!< Card detection pin is DAT3 \hideinitializer */
  55. #define TIMEOUT_SDH SystemCoreClock /*!< 1 second time-out \hideinitializer */
  56. #define SDH_TIMEOUT_ERR (-1L) /*!< SDH operation abort due to timeout error \hideinitializer */
  57. /*@}*/ /* end of group SDH_EXPORTED_CONSTANTS */
  58. /** @addtogroup SDH_EXPORTED_TYPEDEF SDH Exported Type Defines
  59. @{
  60. */
  61. typedef struct SDH_info_t
  62. {
  63. unsigned char IsCardInsert; /*!< Card insert state */
  64. unsigned char R3Flag;
  65. unsigned char R7Flag;
  66. unsigned char volatile DataReadyFlag;
  67. unsigned int CardType; /*!< SDHC, SD, or MMC */
  68. unsigned int RCA; /*!< Relative card address */
  69. unsigned int totalSectorN; /*!< Total sector number */
  70. unsigned int diskSize; /*!< Disk size in K bytes */
  71. int sectorSize; /*!< Sector size in bytes */
  72. unsigned char *dmabuf;
  73. } SDH_INFO_T; /*!< Structure holds SD card info */
  74. /*@}*/ /* end of group SDH_EXPORTED_TYPEDEF */
  75. /** @cond HIDDEN_SYMBOLS */
  76. extern SDH_INFO_T SD0, SD1;
  77. extern int32_t g_SDH_i32ErrCode;
  78. /** @endcond HIDDEN_SYMBOLS */
  79. /** @addtogroup SDH_EXPORTED_FUNCTIONS SDH Exported Functions
  80. @{
  81. */
  82. /**
  83. * @brief Enable specified interrupt.
  84. *
  85. * @param[in] sdh Select SDH0 or SDH1.
  86. * @param[in] u32IntMask Interrupt type mask:
  87. * \ref SDH_INTEN_BLKDIEN_Msk / \ref SDH_INTEN_CRCIEN_Msk / \ref SDH_INTEN_CDIEN_Msk /
  88. * \ref SDH_INTEN_CDSRC_Msk \ref SDH_INTEN_RTOIEN_Msk / \ref SDH_INTEN_DITOIEN_Msk /
  89. * \ref SDH_INTEN_WKIEN_Msk
  90. *
  91. * @return None.
  92. * \hideinitializer
  93. */
  94. #define SDH_ENABLE_INT(sdh, u32IntMask) ((sdh)->INTEN |= (u32IntMask))
  95. /**
  96. * @brief Disable specified interrupt.
  97. *
  98. * @param[in] sdh Select SDH0 or SDH1.
  99. * @param[in] u32IntMask Interrupt type mask:
  100. * \ref SDH_INTEN_BLKDIEN_Msk / \ref SDH_INTEN_CRCIEN_Msk / \ref SDH_INTEN_CDIEN_Msk /
  101. * \ref SDH_INTEN_RTOIEN_Msk / \ref SDH_INTEN_DITOIEN_Msk / \ref SDH_INTEN_WKIEN_Msk / \ref SDH_INTEN_CDSRC_Msk /
  102. *
  103. * @return None.
  104. * \hideinitializer
  105. */
  106. #define SDH_DISABLE_INT(sdh, u32IntMask) ((sdh)->INTEN &= ~(u32IntMask))
  107. /**
  108. * @brief Get specified interrupt flag/status.
  109. *
  110. * @param[in] sdh Select SDH0 or SDH1.
  111. * @param[in] u32IntMask Interrupt type mask:
  112. * \ref SDH_INTSTS_BLKDIF_Msk / \ref SDH_INTSTS_CRCIF_Msk / \ref SDH_INTSTS_CRC7_Msk /
  113. * \ref SDH_INTSTS_CRC16_Msk / \ref SDH_INTSTS_CRCSTS_Msk / \ref SDH_INTSTS_DAT0STS_Msk /
  114. * \ref SDH_INTSTS_CDIF_Msk \ref SDH_INTSTS_RTOIF_Msk /
  115. * \ref SDH_INTSTS_DITOIF_Msk / \ref SDH_INTSTS_CDSTS_Msk /
  116. * \ref SDH_INTSTS_DAT1STS_Msk
  117. *
  118. *
  119. * @return 0 = The specified interrupt is not happened.
  120. * 1 = The specified interrupt is happened.
  121. * \hideinitializer
  122. */
  123. #define SDH_GET_INT_FLAG(sdh, u32IntMask) (((sdh)->INTSTS & (u32IntMask))?1:0)
  124. /**
  125. * @brief Clear specified interrupt flag/status.
  126. *
  127. * @param[in] sdh Select SDH0 or SDH1.
  128. * @param[in] u32IntMask Interrupt type mask:
  129. * \ref SDH_INTSTS_BLKDIF_Msk / \ref SDH_INTSTS_CRCIF_Msk / \ref SDH_INTSTS_CDIF_Msk /
  130. * \ref SDH_INTSTS_RTOIF_Msk / \ref SDH_INTSTS_DITOIF_Msk
  131. *
  132. *
  133. * @return None.
  134. * \hideinitializer
  135. */
  136. #define SDH_CLR_INT_FLAG(sdh, u32IntMask) ((sdh)->INTSTS = (u32IntMask))
  137. /**
  138. * @brief Check SD Card inserted or removed.
  139. *
  140. * @param[in] sdh Select SDH0 or SDH1.
  141. *
  142. * @return 1: Card inserted.
  143. * 0: Card removed.
  144. * \hideinitializer
  145. */
  146. #define SDH_IS_CARD_PRESENT(sdh) (((sdh) == SDH0)? SD0.IsCardInsert : SD1.IsCardInsert)
  147. /**
  148. * @brief Get SD Card capacity.
  149. *
  150. * @param[in] sdh Select SDH0 or SDH1.
  151. *
  152. * @return SD Card capacity. (unit: KByte)
  153. * \hideinitializer
  154. */
  155. #define SDH_GET_CARD_CAPACITY(sdh) (((sdh) == SDH0)? SD0.diskSize : SD1.diskSize)
  156. void SDH_Open(SDH_T *sdh, uint32_t u32CardDetSrc);
  157. uint32_t SDH_Probe(SDH_T *sdh);
  158. uint32_t SDH_Read(SDH_T *sdh, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount);
  159. uint32_t SDH_Write(SDH_T *sdh, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount);
  160. void SDH_Set_clock(SDH_T *sdh, uint32_t sd_clock_khz);
  161. uint32_t SDH_CardDetection(SDH_T *sdh);
  162. void SDH_Open_Disk(SDH_T *sdh, uint32_t u32CardDetSrc);
  163. void SDH_Close_Disk(SDH_T *sdh);
  164. /*@}*/ /* end of group SDH_EXPORTED_FUNCTIONS */
  165. /*@}*/ /* end of group SDH_Driver */
  166. /*@}*/ /* end of group Standard_Driver */
  167. #ifdef __cplusplus
  168. }
  169. #endif
  170. #endif /* __NU_SDH_H__ */