1 /**************************************************************************//** 2 * @file 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 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 /*@}*/ /* end of group SDH_EXPORTED_CONSTANTS */ 71 72 /** @addtogroup SDH_EXPORTED_TYPEDEF SDH Exported Type Defines 73 @{ 74 */ 75 typedef struct SDH_info_t 76 { 77 unsigned int CardType; /*!< SDHC, SD, or MMC */ 78 unsigned int RCA; /*!< Relative card address */ 79 unsigned char IsCardInsert; /*!< Card insert state */ 80 unsigned int totalSectorN; /*!< Total sector number */ 81 unsigned int diskSize; /*!< Disk size in K bytes */ 82 int sectorSize; /*!< Sector size in bytes */ 83 } SDH_INFO_T; /*!< Structure holds SD card info */ 84 85 /*@}*/ /* end of group SDH_EXPORTED_TYPEDEF */ 86 87 /** @cond HIDDEN_SYMBOLS */ 88 extern uint8_t g_u8R3Flag; 89 extern uint8_t volatile g_u8SDDataReadyFlag; 90 extern SDH_INFO_T SD0, SD1; 91 /** @endcond HIDDEN_SYMBOLS */ 92 93 /** @addtogroup SDH_EXPORTED_FUNCTIONS SDH Exported Functions 94 @{ 95 */ 96 97 /** 98 * @brief Enable specified interrupt. 99 * 100 * @param[in] sdh Select SDH0 or SDH1. 101 * @param[in] u32IntMask Interrupt type mask: 102 * \ref SDH_INTEN_BLKDIEN_Msk / \ref SDH_INTEN_CRCIEN_Msk / \ref SDH_INTEN_CDIEN_Msk / 103 * \ref SDH_INTEN_CDSRC_Msk \ref SDH_INTEN_RTOIEN_Msk / \ref SDH_INTEN_DITOIEN_Msk / 104 * \ref SDH_INTEN_WKIEN_Msk 105 * 106 * @return None. 107 * \hideinitializer 108 */ 109 #define SDH_ENABLE_INT(sdh, u32IntMask) ((sdh)->INTEN |= (u32IntMask)) 110 111 /** 112 * @brief Disable specified interrupt. 113 * 114 * @param[in] sdh Select SDH0 or SDH1. 115 * @param[in] u32IntMask Interrupt type mask: 116 * \ref SDH_INTEN_BLKDIEN_Msk / \ref SDH_INTEN_CRCIEN_Msk / \ref SDH_INTEN_CDIEN_Msk / 117 * \ref SDH_INTEN_RTOIEN_Msk / \ref SDH_INTEN_DITOIEN_Msk / \ref SDH_INTEN_WKIEN_Msk / \ref SDH_INTEN_CDSRC_Msk / 118 * 119 * @return None. 120 * \hideinitializer 121 */ 122 #define SDH_DISABLE_INT(sdh, u32IntMask) ((sdh)->INTEN &= ~(u32IntMask)) 123 124 /** 125 * @brief Get specified interrupt flag/status. 126 * 127 * @param[in] sdh Select SDH0 or SDH1. 128 * @param[in] u32IntMask Interrupt type mask: 129 * \ref SDH_INTSTS_BLKDIF_Msk / \ref SDH_INTSTS_CRCIF_Msk / \ref SDH_INTSTS_CRC7_Msk / 130 * \ref SDH_INTSTS_CRC16_Msk / \ref SDH_INTSTS_CRCSTS_Msk / \ref SDH_INTSTS_DAT0STS_Msk / 131 * \ref SDH_INTSTS_CDIF_Msk \ref SDH_INTSTS_RTOIF_Msk / 132 * \ref SDH_INTSTS_DITOIF_Msk / \ref SDH_INTSTS_CDSTS_Msk / 133 * \ref SDH_INTSTS_DAT1STS_Msk 134 * 135 * 136 * @return 0 = The specified interrupt is not happened. 137 * 1 = The specified interrupt is happened. 138 * \hideinitializer 139 */ 140 #define SDH_GET_INT_FLAG(sdh, u32IntMask) (((sdh)->INTSTS & (u32IntMask))?1:0) 141 142 143 /** 144 * @brief Clear specified interrupt flag/status. 145 * 146 * @param[in] sdh Select SDH0 or SDH1. 147 * @param[in] u32IntMask Interrupt type mask: 148 * \ref SDH_INTSTS_BLKDIF_Msk / \ref SDH_INTSTS_CRCIF_Msk / \ref SDH_INTSTS_CDIF_Msk / 149 * \ref SDH_INTSTS_RTOIF_Msk / \ref SDH_INTSTS_DITOIF_Msk 150 * 151 * 152 * @return None. 153 * \hideinitializer 154 */ 155 #define SDH_CLR_INT_FLAG(sdh, u32IntMask) ((sdh)->INTSTS = (u32IntMask)) 156 157 158 /** 159 * @brief Check SD Card inserted or removed. 160 * 161 * @param[in] sdh Select SDH0 or SDH1. 162 * 163 * @return 1: Card inserted. 164 * 0: Card removed. 165 * \hideinitializer 166 */ 167 #define SDH_IS_CARD_PRESENT(sdh) (((sdh) == SDH0)? SD0.IsCardInsert : SD1.IsCardInsert) 168 169 /** 170 * @brief Get SD Card capacity. 171 * 172 * @param[in] sdh Select SDH0 or SDH1. 173 * 174 * @return SD Card capacity. (unit: KByte) 175 * \hideinitializer 176 */ 177 #define SDH_GET_CARD_CAPACITY(sdh) (((sdh) == SDH0)? SD0.diskSize : SD1.diskSize) 178 179 180 void SDH_Open(SDH_T *sdh, uint32_t u32CardDetSrc); 181 uint32_t SDH_Probe(SDH_T *sdh); 182 uint32_t SDH_Read(SDH_T *sdh, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount); 183 uint32_t SDH_Write(SDH_T *sdh, uint8_t *pu8BufAddr, uint32_t u32StartSec, uint32_t u32SecCount); 184 185 uint32_t SDH_CardDetection(SDH_T *sdh); 186 void SDH_Open_Disk(SDH_T *sdh, uint32_t u32CardDetSrc); 187 void SDH_Close_Disk(SDH_T *sdh); 188 189 190 /*@}*/ /* end of group SDH_EXPORTED_FUNCTIONS */ 191 192 /*@}*/ /* end of group SDH_Driver */ 193 194 /*@}*/ /* end of group Standard_Driver */ 195 196 #ifdef __cplusplus 197 } 198 #endif 199 200 #endif /* __SDH_H__ */ 201 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/ 202