sdh.h 7.1 KB

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