nu_sdh.h 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /**************************************************************************//**
  2. * @file nu_sdh.h
  3. * @version V1.00
  4. * @brief M480 SDH driver header file
  5. *
  6. * SPDX-License-Identifier: Apache-2.0
  7. * @copyright (C) 2016-2020 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. /*@}*/ /* end of group SDH_EXPORTED_CONSTANTS */
  56. /** @addtogroup SDH_EXPORTED_TYPEDEF SDH Exported Type Defines
  57. @{
  58. */
  59. typedef struct SDH_info_t
  60. {
  61. unsigned char IsCardInsert; /*!< Card insert state */
  62. unsigned char R3Flag;
  63. unsigned char R7Flag;
  64. unsigned char volatile DataReadyFlag;
  65. unsigned int CardType; /*!< SDHC, SD, or MMC */
  66. unsigned int RCA; /*!< Relative card address */
  67. unsigned int totalSectorN; /*!< Total sector number */
  68. unsigned int diskSize; /*!< Disk size in K bytes */
  69. int sectorSize; /*!< Sector size in bytes */
  70. unsigned char *dmabuf;
  71. } SDH_INFO_T; /*!< Structure holds SD card info */
  72. /*@}*/ /* end of group SDH_EXPORTED_TYPEDEF */
  73. /** @cond HIDDEN_SYMBOLS */
  74. extern SDH_INFO_T SD0, SD1;
  75. /** @endcond HIDDEN_SYMBOLS */
  76. /** @addtogroup SDH_EXPORTED_FUNCTIONS SDH Exported Functions
  77. @{
  78. */
  79. /**
  80. * @brief Enable specified interrupt.
  81. *
  82. * @param[in] sdh Select SDH0 or SDH1.
  83. * @param[in] u32IntMask Interrupt type mask:
  84. * \ref SDH_INTEN_BLKDIEN_Msk / \ref SDH_INTEN_CRCIEN_Msk / \ref SDH_INTEN_CDIEN_Msk /
  85. * \ref SDH_INTEN_CDSRC_Msk \ref SDH_INTEN_RTOIEN_Msk / \ref SDH_INTEN_DITOIEN_Msk /
  86. * \ref SDH_INTEN_WKIEN_Msk
  87. *
  88. * @return None.
  89. * \hideinitializer
  90. */
  91. #define SDH_ENABLE_INT(sdh, u32IntMask) ((sdh)->INTEN |= (u32IntMask))
  92. /**
  93. * @brief Disable specified interrupt.
  94. *
  95. * @param[in] sdh Select SDH0 or SDH1.
  96. * @param[in] u32IntMask Interrupt type mask:
  97. * \ref SDH_INTEN_BLKDIEN_Msk / \ref SDH_INTEN_CRCIEN_Msk / \ref SDH_INTEN_CDIEN_Msk /
  98. * \ref SDH_INTEN_RTOIEN_Msk / \ref SDH_INTEN_DITOIEN_Msk / \ref SDH_INTEN_WKIEN_Msk / \ref SDH_INTEN_CDSRC_Msk /
  99. *
  100. * @return None.
  101. * \hideinitializer
  102. */
  103. #define SDH_DISABLE_INT(sdh, u32IntMask) ((sdh)->INTEN &= ~(u32IntMask))
  104. /**
  105. * @brief Get specified interrupt flag/status.
  106. *
  107. * @param[in] sdh Select SDH0 or SDH1.
  108. * @param[in] u32IntMask Interrupt type mask:
  109. * \ref SDH_INTSTS_BLKDIF_Msk / \ref SDH_INTSTS_CRCIF_Msk / \ref SDH_INTSTS_CRC7_Msk /
  110. * \ref SDH_INTSTS_CRC16_Msk / \ref SDH_INTSTS_CRCSTS_Msk / \ref SDH_INTSTS_DAT0STS_Msk /
  111. * \ref SDH_INTSTS_CDIF_Msk \ref SDH_INTSTS_RTOIF_Msk /
  112. * \ref SDH_INTSTS_DITOIF_Msk / \ref SDH_INTSTS_CDSTS_Msk /
  113. * \ref SDH_INTSTS_DAT1STS_Msk
  114. *
  115. *
  116. * @return 0 = The specified interrupt is not happened.
  117. * 1 = The specified interrupt is happened.
  118. * \hideinitializer
  119. */
  120. #define SDH_GET_INT_FLAG(sdh, u32IntMask) (((sdh)->INTSTS & (u32IntMask))?1:0)
  121. /**
  122. * @brief Clear specified interrupt flag/status.
  123. *
  124. * @param[in] sdh Select SDH0 or SDH1.
  125. * @param[in] u32IntMask Interrupt type mask:
  126. * \ref SDH_INTSTS_BLKDIF_Msk / \ref SDH_INTSTS_CRCIF_Msk / \ref SDH_INTSTS_CDIF_Msk /
  127. * \ref SDH_INTSTS_RTOIF_Msk / \ref SDH_INTSTS_DITOIF_Msk
  128. *
  129. *
  130. * @return None.
  131. * \hideinitializer
  132. */
  133. #define SDH_CLR_INT_FLAG(sdh, u32IntMask) ((sdh)->INTSTS = (u32IntMask))
  134. /**
  135. * @brief Check SD Card inserted or removed.
  136. *
  137. * @param[in] sdh Select SDH0 or SDH1.
  138. *
  139. * @return 1: Card inserted.
  140. * 0: Card removed.
  141. * \hideinitializer
  142. */
  143. #define SDH_IS_CARD_PRESENT(sdh) (((sdh) == SDH0)? SD0.IsCardInsert : SD1.IsCardInsert)
  144. /**
  145. * @brief Get SD Card capacity.
  146. *
  147. * @param[in] sdh Select SDH0 or SDH1.
  148. *
  149. * @return SD Card capacity. (unit: KByte)
  150. * \hideinitializer
  151. */
  152. #define SDH_GET_CARD_CAPACITY(sdh) (((sdh) == SDH0)? SD0.diskSize : SD1.diskSize)
  153. void SDH_Open(SDH_T *sdh, uint32_t u32CardDetSrc);
  154. uint32_t SDH_Probe(SDH_T *sdh);
  155. uint32_t SDH_Read(SDH_T *sdh, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount);
  156. uint32_t SDH_Write(SDH_T *sdh, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount);
  157. void SDH_Set_clock(SDH_T *sdh, uint32_t u32SDClockKhz);
  158. uint32_t SDH_CardDetection(SDH_T *sdh);
  159. void SDH_Open_Disk(SDH_T *sdh, uint32_t u32CardDetSrc);
  160. void SDH_Close_Disk(SDH_T *sdh);
  161. /*@}*/ /* end of group SDH_EXPORTED_FUNCTIONS */
  162. /*@}*/ /* end of group SDH_Driver */
  163. /*@}*/ /* end of group Standard_Driver */
  164. #ifdef __cplusplus
  165. }
  166. #endif
  167. #endif /* __NU_SDH_H__ */
  168. /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/