1 /*********************************************************************************************************//**
2  * @file    ht32f1xxxx_sdio.h
3  * @version $Rev:: 2458         $
4  * @date    $Date:: 2021-08-05 #$
5  * @brief   The header file of the SDIO library.
6  *************************************************************************************************************
7  * @attention
8  *
9  * Firmware Disclaimer Information
10  *
11  * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
12  *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
13  *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
14  *    other intellectual property laws.
15  *
16  * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
17  *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
18  *    other than HOLTEK and the customer.
19  *
20  * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
21  *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
22  *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
23  *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
24  *
25  * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
26  ************************************************************************************************************/
27 
28 /* Define to prevent recursive inclusion -------------------------------------------------------------------*/
29 #ifndef __HT32F1XXXX_SDIO_H
30 #define __HT32F1XXXX_SDIO_H
31 
32 #ifdef __cplusplus
33  extern "C" {
34 #endif
35 
36 /* Includes ------------------------------------------------------------------------------------------------*/
37 #include "ht32.h"
38 
39 /** @addtogroup HT32F1xxxx_Peripheral_Driver HT32F1xxxx Peripheral Driver
40   * @{
41   */
42 
43 /** @addtogroup SDIO
44   * @{
45   */
46 
47 
48 /* Exported types ------------------------------------------------------------------------------------------*/
49 /** @defgroup SDIO_Exported_Types SDIO exported types
50   * @{
51   */
52 typedef struct
53 {
54   u32 SDIO_ClockDiv;              /*!< Specify the SDIO clock divider value.
55                                        This parameter can be a value between 1 and 256. */
56 
57   u32 SDIO_ClockPeriod;           /*!< Specify whether SDIO clock has longer or shorter low period
58                                        when the SDIO clock divider value is odd.
59                                        This parameter can be a value of @ref SDIO_Clock_Period */
60 
61   u32 SDIO_ClockPowerSave;        /*!< Specify whether SDIO clock output is enabled or disabled
62                                        when the bus is idle.
63                                        This parameter can be a value of @ref SDIO_Clock_Power_Save */
64 
65   u32 SDIO_BusWide;               /*!< Specify the SDIO bus width.
66                                        This parameter can be a value of @ref SDIO_Bus_Wide */
67 
68   u32 SDIO_BusMode;               /*!< Specify the SDIO bus Mode.
69                                        This parameter can be a value of @ref SDIO_Bus_Mode */
70 } SDIO_InitTypeDef;
71 
72 
73 typedef struct
74 {
75   u32 SDIO_Argument;              /*!< Specify the SDIO command argument which is sent
76                                        to a card as part of a command message. If a command
77                                        contains an argument, it must be loaded into this register
78                                        before writing the command to the command register */
79 
80   u32 SDIO_CmdIndex;              /*!< Specify the SDIO command index. It must be lower than 0x40.          */
81 
82   u32 SDIO_Response;              /*!< Specify the SDIO response type.
83                                        This parameter can be a value of @ref SDIO_Response_Type */
84 
85   u32 SDIO_DatPresent;            /*!< Specify whether data is present on the SDIO data line
86                                        after SDIO command or not.
87                                        This parameter can be a value of @ref SDIO_Data_Present */
88 
89   u32 SDIO_CmdIdxChk;             /*!< Specify whether SDIO command index check function is enabled or not.
90                                        This parameter can be a value of @ref SDIO_CmdIdx_Check */
91 
92   u32 SDIO_CmdCrcChk;             /*!< Specify whether SDIO command CRC check function is enabled or not.
93                                        This parameter can be a value of @ref SDIO_CmdCrc_Check */
94 } SDIO_CmdInitTypeDef;
95 
96 
97 typedef struct
98 {
99   u32 SDIO_DataBlockCount;        /*!< Specify the number of data block count to be transferred.
100                                        This parameter can be a value between 1 and 65535. */
101 
102   u32 SDIO_DataBlockSize;         /*!< Specify the data block size for block transfer.
103                                        This parameter can be a value between 1 and 2048. */
104 
105   u32 SDIO_TransferDir;           /*!< Specify the data transfer direction, whether the transfer
106                                        is a read or write.
107                                        This parameter can be a value of @ref SDIO_Transfer_Direction */
108 
109   u32 SDIO_TransferMode;          /*!< Specify whether data transfer is in single block or multi-block mode.
110                                        This parameter can be a value of @ref SDIO_Transfer_Mode */
111 
112   u32 SDIO_DataTimeOut;           /*!< Specify the data timeout period in card bus clock periods.
113                                        This parameter can be a value between 0x1 and 0x00ffffff. */
114 
115 } SDIO_DataInitTypeDef;
116 /**
117   * @}
118   */
119 
120 /* Exported constants --------------------------------------------------------------------------------------*/
121 /** @defgroup SDIO_Exported_Constants SDIO exported constants
122   * @{
123   */
124 
125 /** @defgroup SDIO_Clock_Div SDIO Clock Div
126   * @{
127   */
128 #define IS_SDIO_CLOC_DIV(DIV)           ((DIV > 0) && (DIV < 256))
129 /**
130   * @}
131   */
132 
133 /** @defgroup SDIO_Clock_Period SDIO Clock Period
134   * @{
135   */
136 #define SDIO_Clock_LowPeriod_Shorter    (0x00000000)
137 #define SDIO_Clock_LowPeriod_Longer     (0x00000008)
138 
139 #define IS_SDIO_CLOCK_PERIOD(PERIOD)    (((PERIOD) == SDIO_Clock_LowPeriod_Shorter) || \
140                                          ((PERIOD) == SDIO_Clock_LowPeriod_Longer))
141 /**
142   * @}
143   */
144 
145 /** @defgroup SDIO_Clock_Power_Save SDIO Clock Power Save
146   * @{
147   */
148 #define SDIO_Clock_PowerSave_Disable    (0x00000000)
149 #define SDIO_Clock_PowerSave_StopLow    (0x00000002)
150 #define SDIO_Clock_PowerSave_StopHigh   (0x00000003)
151 
152 #define IS_SDIO_CLOCK_POWER_SAVE(SAVE)  (((SAVE) == SDIO_Clock_PowerSave_Disable) || \
153                                          ((SAVE) == SDIO_Clock_PowerSave_StopLow) || \
154                                          ((SAVE) == SDIO_Clock_PowerSave_StopHigh))
155 /**
156   * @}
157   */
158 
159 /** @defgroup SDIO_Bus_Wide SDIO Bus Wide
160   * @{
161   */
162 #define SDIO_BusWide_1b                 (0x00000000)
163 #define SDIO_BusWide_4b                 (0x00000002)
164 
165 #define IS_SDIO_BUS_WIDE(WIDE)          (((WIDE) == SDIO_BusWide_1b) || \
166                                          ((WIDE) == SDIO_BusWide_4b))
167 /**
168   * @}
169   */
170 
171 /** @defgroup SDIO_Bus_Mode SDIO Bus Mode
172   * @{
173   */
174 #define SDIO_BusMode_NormalSpeed        (0x00000000)
175 #define SDIO_BusMode_HighSpeed          (0x00000004)
176 
177 #define IS_SDIO_BUS_MODE(MODE)          (((MODE) == SDIO_BusMode_NormalSpeed) || \
178                                          ((MODE) == SDIO_BusMode_HighSpeed))
179 /**
180   * @}
181   */
182 
183 /** @defgroup SDIO_Command_Index SDIO Command Index
184   * @{
185   */
186 #define IS_SDIO_CMD_INDEX(INDEX)        ((INDEX) < 0x40)
187 /**
188   * @}
189   */
190 
191 /** @defgroup SDIO_Response_Type SDIO Response Type
192   * @{
193   */
194 #define SDIO_Response_No                (0x00000000)
195 #define SDIO_Response_Long              (0x00000001)
196 #define SDIO_Response_Short             (0x00000002)
197 
198 #define IS_SDIO_RESPONSE(RESPONSE)      (((RESPONSE) == SDIO_Response_No)    || \
199                                          ((RESPONSE) == SDIO_Response_Short) || \
200                                          ((RESPONSE) == SDIO_Response_Long))
201 /**
202   * @}
203   */
204 
205 /** @defgroup SDIO_Data_Present SDIO Data Present
206   * @{
207   */
208 #define SDIO_Data_Present_No            (0x00000000)
209 #define SDIO_Data_Present_Yes           (0x00000020)
210 
211 #define IS_SDIO_DATA_PRESENT(PRESENT)   (((PRESENT) == SDIO_Data_Present_No) || \
212                                          ((PRESENT) == SDIO_Data_Present_Yes))
213 /**
214   * @}
215   */
216 
217 /** @defgroup SDIO_CmdIdx_Check SDIO CmdIdx Check
218   * @{
219   */
220 #define SDIO_CmdIdxChk_No               (0x00000000)
221 #define SDIO_CmdIdxChk_Yes              (0x00000010)
222 
223 #define IS_SDIO_CMD_IDX_CHK(CHK)        (((CHK) == SDIO_CmdIdxChk_No) || \
224                                          ((CHK) == SDIO_CmdIdxChk_Yes))
225 /**
226   * @}
227   */
228 
229 /** @defgroup SDIO_CmdCrc_Check SDIO CmdCrc Check
230   * @{
231   */
232 #define SDIO_CmdCrcChk_No               (0x00000000)
233 #define SDIO_CmdCrcChk_Yes              (0x00000008)
234 
235 #define IS_SDIO_CMD_CRC_CHK(CHK)        (((CHK) == SDIO_CmdCrcChk_No) || \
236                                          ((CHK) == SDIO_CmdCrcChk_Yes))
237 /**
238   * @}
239   */
240 
241 /** @defgroup SDIO_Data_Block_Count SDIO Data Block Count
242   * @{
243   */
244 #define IS_SDIO_DATA_BLOCK_COUNT(COUNT) ((COUNT > 0) && (COUNT <= 65535))
245 /**
246   * @}
247   */
248 
249 /** @defgroup SDIO_Data_Block_Size SDIO Data Block Size
250   * @{
251   */
252 #define IS_SDIO_DATA_BLOCK_SIZE(SIZE)   ((SIZE > 0) && (SIZE <= 2048))
253 /**
254   * @}
255   */
256 
257 /** @defgroup SDIO_Transfer_Mode SDIO Transfer Mode
258   * @{
259   */
260 #define SDIO_SingleBlock_Transfer       (0x00000000)
261 #define SDIO_MultiBlock_Transfer        (0x00000022)
262 #define SDIO_SingleBlock_DMA_Transfer   (0x00000100)
263 #define SDIO_MultiBlock_DMA_Transfer    (0x00000122)
264 
265 #define IS_SDIO_TRANSFER_MODE(MODE)     (((MODE) == SDIO_SingleBlock_Transfer)     || \
266                                          ((MODE) == SDIO_MultiBlock_Transfer)      || \
267                                          ((MODE) == SDIO_SingleBlock_DMA_Transfer) || \
268                                          ((MODE) == SDIO_MultiBlock_DMA_Transfer))
269 /**
270   * @}
271   */
272 /** @defgroup SDIO_Transfer_Direction SDIO Transfer Direction
273   * @{
274   */
275 #define SDIO_TransferDir_ToCard         (0x00000000)
276 #define SDIO_TransferDir_ToSDIO         (0x00000010)
277 
278 #define IS_SDIO_TRANSFER_DIR(DIR)       (((DIR) == SDIO_TransferDir_ToCard) || \
279                                          ((DIR) == SDIO_TransferDir_ToSDIO))
280 /**
281   * @}
282   */
283 
284 
285 /** @defgroup SDIO_Data_TimeOut SDIO Data TimeOut
286   * @{
287   */
288 #define IS_SDIO_DATA_TIMEOUT(TIME)      ((TIME > 0) && (TIME <= 0x00ffffff))
289 /**
290   * @}
291   */
292 
293 /** @defgroup SDIO_Flags SDIO Flags
294   * @{
295   */
296 #define SDIO_FLAG_CMD_SEND              (0x00000001)
297 #define SDIO_FLAG_TRANS_END             (0x00000002)
298 #define SDIO_FLAG_BUF_OVERFLOW          (0x00000008)
299 #define SDIO_FLAG_BUF_UNDERFLOW         (0x00000010)
300 #define SDIO_FLAG_BUF_HALF              (0x00000020)
301 #define SDIO_FLAG_BUF_FULL              (0x00000040)
302 #define SDIO_FLAG_BUF_EMPTY             (0x00000080)
303 #define SDIO_FLAG_ERR                   (0x00008000)
304 #define SDIO_FLAG_CMD_TIMEOUT           (0x00010000)
305 #define SDIO_FLAG_CMD_CRCERR            (0x00020000)
306 #define SDIO_FLAG_CMD_ENDERR            (0x00040000)
307 #define SDIO_FLAG_CMD_IDXERR            (0x00080000)
308 #define SDIO_FLAG_DATA_TIMEOUT          (0x00100000)
309 #define SDIO_FLAG_DATA_CRCERR           (0x00200000)
310 #define SDIO_FLAG_DATA_ENDERR           (0x00400000)
311 #define SDIO_FLAG_CARD_INT              (0x01000000)
312 #define SDIO_FLAG_DAT_ERR               (0x02000000)
313 #define SDIO_FLAG_CMD_ERR               (0x04000000)
314 
315 #define IS_SDIO_FLAG(FLAG)              (((FLAG) != 0) && (((FLAG) & 0x077F80FB) != 0))
316 /**
317   * @}
318   */
319 
320 /** @defgroup SDIO_Response_Registers SDIO Response Registers
321   * @{
322   */
323 #define SDIO_RESP1                      (0x00000000)
324 #define SDIO_RESP2                      (0x00000004)
325 #define SDIO_RESP3                      (0x00000008)
326 #define SDIO_RESP4                      (0x0000000C)
327 
328 #define IS_SDIO_RESP(RESP)              (((RESP) == SDIO_RESP1) || \
329                                          ((RESP) == SDIO_RESP2) || \
330                                          ((RESP) == SDIO_RESP3) || \
331                                          ((RESP) == SDIO_RESP4))
332 /**
333   * @}
334   */
335 
336 /** @defgroup SDIO_Interrupt_sources SDIO Interrupt sources
337   * @{
338   */
339 #define SDIO_INT_CMD_SEND               (0x00000001)
340 #define SDIO_INT_TRANS_END              (0x00000002)
341 #define SDIO_INT_BUF_OVERFLOW           (0x00000008)
342 #define SDIO_INT_BUF_UNDERFLOW          (0x00000010)
343 #define SDIO_INT_BUF_HALF               (0x00000020)
344 #define SDIO_INT_BUF_FULL               (0x00000040)
345 #define SDIO_INT_BUF_EMPTY              (0x00000080)
346 #define SDIO_INT_CMD_TIMEOUT            (0x00010000)
347 #define SDIO_INT_CMD_CRCERR             (0x00020000)
348 #define SDIO_INT_CMD_IDXERR             (0x00080000)
349 #define SDIO_INT_DATA_TIMEOUT           (0x00100000)
350 #define SDIO_INT_DATA_CRCERR            (0x00200000)
351 #define SDIO_INT_CARD_INT               (0x01000000)
352 
353 #define IS_SDIO_INT(INT)                (((INT) != 0) && (((INT) & 0x013B00FB) != 0))
354 /**
355   * @}
356   */
357 
358 /**
359   * @}
360   */
361 
362 /* Exported macro ------------------------------------------------------------------------------------------*/
363 /** @defgroup SDIO_Exported_Macro SDIO exported macro
364   * @{
365   */
366 #define RESET_CPSM()                    {\
367                                         HT_SDIO->SWRST |= (1UL << 1);\
368                                         while (HT_SDIO->SWRST & (1UL << 1));\
369                                         }
370 
371 #define RESET_DPSM()                    {\
372                                         HT_SDIO->SWRST |= (1UL << 2);\
373                                         while (HT_SDIO->SWRST & (1UL << 2));\
374                                         }
375 /**
376   * @}
377   */
378 
379 /* Exported functions --------------------------------------------------------------------------------------*/
380 /** @defgroup SDIO_Exported_Functions SDIO exported functions
381   * @{
382   */
383 void SDIO_DeInit(void);
384 void SDIO_Init(SDIO_InitTypeDef* SDIO_InitStruct);
385 void SDIO_ClockCmd(ControlStatus Cmd);
386 u32 SDIO_ReadData(void);
387 void SDIO_WriteData(u32 Data);
388 u32 SDIO_GetFIFOCount(void);
389 void SDIO_SendCommand(SDIO_CmdInitTypeDef *SDIO_CmdInitStruct);
390 u32 SDIO_GetResponse(u32 SDIO_RESP);
391 void SDIO_DataConfig(SDIO_DataInitTypeDef* SDIO_DataInitStruct);
392 void SDIO_FlagConfig(u32 SDIO_FLAG, ControlStatus NewState);
393 FlagStatus SDIO_GetFlagStatus(u32 SDIO_FLAG);
394 void SDIO_ClearFlag(u32 SDIO_FLAG);
395 void SDIO_IntConfig(u32 SDIO_INT, ControlStatus NewState);
396 /**
397   * @}
398   */
399 
400 
401 /**
402   * @}
403   */
404 
405 /**
406   * @}
407   */
408 
409 #ifdef __cplusplus
410 }
411 #endif
412 
413 #endif
414