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