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