sd.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**************************************************************************//**
  2. * @file sd.h
  3. * @version V1.00
  4. * $Revision: 12 $
  5. * $Date: 14/11/04 10:10a $
  6. * @brief NUC472/NUC442 SD driver header file
  7. *
  8. * @note
  9. * Copyright (C) 2013 Nuvoton Technology Corp. All rights reserved.
  10. *****************************************************************************/
  11. #include <stdio.h>
  12. #ifndef __SD_H__
  13. #define __SD_H__
  14. /** @addtogroup NUC472_442_Device_Driver NUC472/NUC442 Device Driver
  15. @{
  16. */
  17. /** @addtogroup NUC472_442_SD_Driver SD Driver
  18. @{
  19. */
  20. /** @addtogroup NUC472_442_SD_EXPORTED_CONSTANTS SD Exported Constants
  21. @{
  22. */
  23. #define SD_CARD 0
  24. #define SD_ERR_ID 0xFFFF0100
  25. #define SD_TIMEOUT (SD_ERR_ID|0x01)
  26. #define SD_NO_MEMORY (SD_ERR_ID|0x02)
  27. //-- function return value
  28. #define Successful 0
  29. #define Fail 1
  30. //--- define type of SD card or MMC
  31. #define SD_TYPE_UNKNOWN 0
  32. #define SD_TYPE_SD_HIGH 1
  33. #define SD_TYPE_SD_LOW 2
  34. #define SD_TYPE_MMC 3
  35. #define SD_TYPE_EMMC 4
  36. /* SD error */
  37. #define SD_NO_SD_CARD (SD_ERR_ID|0x10)
  38. #define SD_ERR_DEVICE (SD_ERR_ID|0x11)
  39. #define SD_INIT_TIMEOUT (SD_ERR_ID|0x12)
  40. #define SD_SELECT_ERROR (SD_ERR_ID|0x13)
  41. #define SD_WRITE_PROTECT (SD_ERR_ID|0x14)
  42. #define SD_INIT_ERROR (SD_ERR_ID|0x15)
  43. #define SD_CRC7_ERROR (SD_ERR_ID|0x16)
  44. #define SD_CRC16_ERROR (SD_ERR_ID|0x17)
  45. #define SD_CRC_ERROR (SD_ERR_ID|0x18)
  46. #define SD_CMD8_ERROR (SD_ERR_ID|0x19)
  47. #define SD_FREQ 12000
  48. #define SDHC_FREQ 12000
  49. #define STOR_STRING_LEN 32
  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 (1 << 8) /*!< Card detection pin is GPIO \hideinitializer */
  53. #define CardDetect_From_DAT3 (1 << 9) /*!< Card detection pin is DAT3 \hideinitializer */
  54. /*@}*/ /* end of group NUC472_442_SD_EXPORTED_CONSTANTS */
  55. /** @addtogroup NUC472_442_SD_EXPORTED_TYPEDEF SD Exported Type Defines
  56. @{
  57. */
  58. typedef struct SD_info_t {
  59. uint32_t CardType; /*!< SDHC, SD, or MMC */
  60. uint32_t RCA; /*!< relative card address */
  61. uint8_t IsCardInsert; /*!< card insert state */
  62. } SD_INFO_T;
  63. /* we allocate one of these for every device that we remember */
  64. typedef struct disk_data_t {
  65. struct disk_data_t *next; /*!< next device */
  66. /* information about the device -- always good */
  67. unsigned int totalSectorN; /*!< total sector number */
  68. unsigned int diskSize; /*!< disk size in Kbytes */
  69. int sectorSize; /*!< sector size in bytes */
  70. char vendor[STOR_STRING_LEN]; /*!< SD card vendor */
  71. char product[STOR_STRING_LEN]; /*!< *SD card product id */
  72. char serial[STOR_STRING_LEN]; /*!< SD card serial number */
  73. } DISK_DATA_T;
  74. /*@}*/ /* end of group NUC472_442_SD_EXPORTED_TYPEDEF */
  75. /// @cond HIDDEN_SYMBOLS
  76. extern SD_INFO_T SD0;
  77. extern SD_INFO_T SD1;
  78. /// @endcond HIDDEN_SYMBOLS
  79. /** @addtogroup NUC472_442_SD_EXPORTED_FUNCTIONS SD Exported Functions
  80. @{
  81. */
  82. /**
  83. * @brief Enable specified interrupt.
  84. *
  85. * @param[in] u32IntMask Interrupt type mask:
  86. * \ref SDH_INTEN_BLKDIEN_Msk / \ref SDH_INTEN_CRCIEN_Msk / \ref SDH_INTEN_CDIEN0_Msk / \ref SDH_INTEN_CDIEN1_Msk /
  87. * \ref SDH_INTEN_CDSRC0_Msk / \ref SDH_INTEN_CDSRC1_Msk / \ref SDH_INTEN_RTOIEN_Msk / \ref SDH_INTEN_DITOIEN_Msk /
  88. * \ref SDH_INTEN_WKIEN_Msk
  89. *
  90. * @return None.
  91. * \hideinitializer
  92. */
  93. #define SD_ENABLE_INT(u32IntMask) (SD->INTEN |= (u32IntMask))
  94. /**
  95. * @brief Disable specified interrupt.
  96. *
  97. * @param[in] u32IntMask Interrupt type mask:
  98. * \ref SDH_INTEN_BLKDIEN_Msk / \ref SDH_INTEN_CRCIEN_Msk / \ref SDH_INTEN_CDIEN0_Msk / \ref SDH_INTEN_CDIEN1_Msk /
  99. * \ref SDH_INTEN_SDHOST0IEN_Msk / \ref SDH_INTEN_SDHOST1IEN_Msk / \ref SDH_INTEN_RTOIEN_Msk / \ref SDH_INTEN_DITOIEN_Msk /
  100. * \ref SDH_INTEN_WKIEN_Msk / \ref SDH_INTEN_CDSRC0_Msk / \ref SDH_INTEN_CDSRC1_Msk
  101. *
  102. * @return None.
  103. * \hideinitializer
  104. */
  105. #define SD_DISABLE_INT(u32IntMask) (SD->INTEN &= ~(u32IntMask))
  106. /**
  107. * @brief Get specified interrupt flag/status.
  108. *
  109. * @param[in] u32IntMask Interrupt type mask:
  110. * \ref SDH_INTSTS_BLKDIF_Msk / \ref SDH_INTSTS_CRCIF_Msk / \ref SDH_INTSTS_CRC7_Msk /
  111. * \ref SDH_INTSTS_CRC16_Msk / \ref SDH_INTSTS_CRCSTS_Msk / \ref SDH_INTSTS_DAT0STS_Msk / \ref SDH_INTSTS_CDIF0_Msk /
  112. * \ref SDH_INTSTS_CDIF1_Msk / \ref SDH_INTSTS_SDHOST0IF_Msk / \ref SDH_INTSTS_SDHOST1IF_Msk / \ref SDH_INTSTS_RTOIF_Msk /
  113. * \ref SDH_INTSTS_DINTOIF_Msk / \ref SDH_INTSTS_CDSTS0_Msk / \ref SDH_INTSTS_CDSTS1_Msk / \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 SD_GET_INT_FLAG(u32IntMask) ((SD->INTSTS & (u32IntMask))?1:0)
  121. /**
  122. * @brief Clear specified interrupt flag/status.
  123. *
  124. * @param[in] u32IntMask Interrupt type mask:
  125. * \ref SDH_INTSTS_BLKDIF_Msk / \ref SDH_INTSTS_CRCIF_Msk / \ref SDH_INTSTS_CDIF0_Msk /
  126. * \ref SDH_INTSTS_CDIF1_Msk / \ref SDH_INTSTS_SDHOST0IF_Msk / \ref SDH_INTSTS_SDHOST1IF_Msk /
  127. * \ref SDH_INTSTS_RTOIF_Msk / \ref SDH_INTSTS_DINTOIF_Msk
  128. *
  129. *
  130. * @return None.
  131. * \hideinitializer
  132. */
  133. #define SD_CLR_INT_FLAG(u32IntMask) (SD->INTSTS = u32IntMask)
  134. /**
  135. * @brief Check SD Card inserted or removed.
  136. *
  137. * @param[in] u32CardNum Select SD0 or SD1. ( \ref SD_PORT0 / \ref SD_PORT1)
  138. *
  139. * @return 1: Card inserted.
  140. * 0: Card removed.
  141. * \hideinitializer
  142. */
  143. #define SD_IS_CARD_PRESENT(u32CardNum) ((u32CardNum & (SD_PORT0))?(SD0.IsCardInsert):(SD1.IsCardInsert))
  144. /**
  145. * @brief Get SD Card capacity.
  146. *
  147. * @param[in] u32CardNum Select SD0 or SD1. ( \ref SD_PORT0 / \ref SD_PORT1)
  148. *
  149. * @return SD Card capacity. (unit: KByte)
  150. * \hideinitializer
  151. */
  152. #define SD_GET_CARD_CAPACITY(u32CardNum) ((u32CardNum & (SD_PORT0))?(SD_DiskInfo0.diskSize):(SD_DiskInfo1.diskSize))
  153. void SD_Open(uint32_t u32CardDetSrc);
  154. void SD_Probe(uint32_t u32CardNum);
  155. uint32_t SD_Read(uint32_t u32CardNum, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount);
  156. uint32_t SD_Write(uint32_t u32CardNum, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount);
  157. /*@}*/ /* end of group NUC472_442_ADC_EXPORTED_FUNCTIONS */
  158. /*@}*/ /* end of group NUC472_442_ADC_Driver */
  159. /*@}*/ /* end of group NUC472_442_Device_Driver */
  160. #endif //end of __SD_H__
  161. /*** (C) COPYRIGHT 2013 Nuvoton Technology Corp. ***/