1 /********************************** (C) COPYRIGHT  *******************************
2 * File Name          : ch32f20x_sdio.h
3 * Author             : WCH
4 * Version            : V1.0.0
5 * Date               : 2021/08/08
6 * Description        : This file contains all the functions prototypes for the SDIO
7 *                      firmware library.
8 *******************************************************************************/
9 #ifndef __CH32F20x_SDIO_H
10 #define __CH32F20x_SDIO_H
11 
12 #ifdef __cplusplus
13  extern "C" {
14 #endif
15 
16 #include "ch32f20x.h"
17 
18 /* SDIO Init structure definition */
19 typedef struct
20 {
21   uint32_t SDIO_ClockEdge;            /* Specifies the clock transition on which the bit capture is made.
22                                          This parameter can be a value of @ref SDIO_Clock_Edge */
23 
24   uint32_t SDIO_ClockBypass;          /* Specifies whether the SDIO Clock divider bypass is
25                                          enabled or disabled.
26                                          This parameter can be a value of @ref SDIO_Clock_Bypass */
27 
28   uint32_t SDIO_ClockPowerSave;       /* Specifies whether SDIO Clock output is enabled or
29                                          disabled when the bus is idle.
30                                          This parameter can be a value of @ref SDIO_Clock_Power_Save */
31 
32   uint32_t SDIO_BusWide;              /* Specifies the SDIO bus width.
33                                          This parameter can be a value of @ref SDIO_Bus_Wide */
34 
35   uint32_t SDIO_HardwareFlowControl;  /* Specifies whether the SDIO hardware flow control is enabled or disabled.
36                                          This parameter can be a value of @ref SDIO_Hardware_Flow_Control */
37 
38   uint8_t SDIO_ClockDiv;              /* Specifies the clock frequency of the SDIO controller.
39                                          This parameter can be a value between 0x00 and 0xFF. */
40 
41 } SDIO_InitTypeDef;
42 
43 
44 typedef struct
45 {
46   uint32_t SDIO_Argument;  /* Specifies the SDIO command argument which is sent
47                               to a card as part of a command message. If a command
48                               contains an argument, it must be loaded into this register
49                               before writing the command to the command register */
50 
51   uint32_t SDIO_CmdIndex;  /* Specifies the SDIO command index. It must be lower than 0x40. */
52 
53   uint32_t SDIO_Response;  /* Specifies the SDIO response type.
54                               This parameter can be a value of @ref SDIO_Response_Type */
55 
56   uint32_t SDIO_Wait;      /* Specifies whether SDIO wait-for-interrupt request is enabled or disabled.
57                               This parameter can be a value of @ref SDIO_Wait_Interrupt_State */
58 
59   uint32_t SDIO_CPSM;      /* Specifies whether SDIO Command path state machine (CPSM)
60                               is enabled or disabled.
61                               This parameter can be a value of @ref SDIO_CPSM_State */
62 } SDIO_CmdInitTypeDef;
63 
64 typedef struct
65 {
66   uint32_t SDIO_DataTimeOut;    /* Specifies the data timeout period in card bus clock periods. */
67 
68   uint32_t SDIO_DataLength;     /* Specifies the number of data bytes to be transferred. */
69 
70   uint32_t SDIO_DataBlockSize;  /* Specifies the data block size for block transfer.
71                                    This parameter can be a value of @ref SDIO_Data_Block_Size */
72 
73   uint32_t SDIO_TransferDir;    /* Specifies the data transfer direction, whether the transfer
74                                    is a read or write.
75                                    This parameter can be a value of @ref SDIO_Transfer_Direction */
76 
77   uint32_t SDIO_TransferMode;   /* Specifies whether data transfer is in stream or block mode.
78                                    This parameter can be a value of @ref SDIO_Transfer_Type */
79 
80   uint32_t SDIO_DPSM;           /* Specifies whether SDIO Data path state machine (DPSM)
81                                    is enabled or disabled.
82                                    This parameter can be a value of @ref SDIO_DPSM_State */
83 } SDIO_DataInitTypeDef;
84 
85 
86 /* SDIO_Clock_Edge */
87 #define SDIO_ClockEdge_Rising               ((uint32_t)0x00000000)
88 #define SDIO_ClockEdge_Falling              ((uint32_t)0x00002000)
89 
90 /* SDIO_Clock_Bypass */
91 #define SDIO_ClockBypass_Disable             ((uint32_t)0x00000000)
92 #define SDIO_ClockBypass_Enable              ((uint32_t)0x00000400)
93 
94 /* SDIO_Clock_Power_Save */
95 #define SDIO_ClockPowerSave_Disable         ((uint32_t)0x00000000)
96 #define SDIO_ClockPowerSave_Enable          ((uint32_t)0x00000200)
97 
98 /* SDIO_Bus_Wide */
99 #define SDIO_BusWide_1b                     ((uint32_t)0x00000000)
100 #define SDIO_BusWide_4b                     ((uint32_t)0x00000800)
101 #define SDIO_BusWide_8b                     ((uint32_t)0x00001000)
102 
103 /* SDIO_Hardware_Flow_Control */
104 #define SDIO_HardwareFlowControl_Disable    ((uint32_t)0x00000000)
105 #define SDIO_HardwareFlowControl_Enable     ((uint32_t)0x00004000)
106 
107 /* SDIO_Power_State */
108 #define SDIO_PowerState_OFF                 ((uint32_t)0x00000000)
109 #define SDIO_PowerState_ON                  ((uint32_t)0x00000003)
110 
111 /* SDIO_Interrupt_sources */
112 #define SDIO_IT_CCRCFAIL                    ((uint32_t)0x00000001)
113 #define SDIO_IT_DCRCFAIL                    ((uint32_t)0x00000002)
114 #define SDIO_IT_CTIMEOUT                    ((uint32_t)0x00000004)
115 #define SDIO_IT_DTIMEOUT                    ((uint32_t)0x00000008)
116 #define SDIO_IT_TXUNDERR                    ((uint32_t)0x00000010)
117 #define SDIO_IT_RXOVERR                     ((uint32_t)0x00000020)
118 #define SDIO_IT_CMDREND                     ((uint32_t)0x00000040)
119 #define SDIO_IT_CMDSENT                     ((uint32_t)0x00000080)
120 #define SDIO_IT_DATAEND                     ((uint32_t)0x00000100)
121 #define SDIO_IT_STBITERR                    ((uint32_t)0x00000200)
122 #define SDIO_IT_DBCKEND                     ((uint32_t)0x00000400)
123 #define SDIO_IT_CMDACT                      ((uint32_t)0x00000800)
124 #define SDIO_IT_TXACT                       ((uint32_t)0x00001000)
125 #define SDIO_IT_RXACT                       ((uint32_t)0x00002000)
126 #define SDIO_IT_TXFIFOHE                    ((uint32_t)0x00004000)
127 #define SDIO_IT_RXFIFOHF                    ((uint32_t)0x00008000)
128 #define SDIO_IT_TXFIFOF                     ((uint32_t)0x00010000)
129 #define SDIO_IT_RXFIFOF                     ((uint32_t)0x00020000)
130 #define SDIO_IT_TXFIFOE                     ((uint32_t)0x00040000)
131 #define SDIO_IT_RXFIFOE                     ((uint32_t)0x00080000)
132 #define SDIO_IT_TXDAVL                      ((uint32_t)0x00100000)
133 #define SDIO_IT_RXDAVL                      ((uint32_t)0x00200000)
134 #define SDIO_IT_SDIOIT                      ((uint32_t)0x00400000)
135 #define SDIO_IT_CEATAEND                    ((uint32_t)0x00800000)
136 
137 /* SDIO_Response_Type */
138 #define SDIO_Response_No                    ((uint32_t)0x00000000)
139 #define SDIO_Response_Short                 ((uint32_t)0x00000040)
140 #define SDIO_Response_Long                  ((uint32_t)0x000000C0)
141 
142 /* SDIO_Wait_Interrupt_State */
143 #define SDIO_Wait_No                        ((uint32_t)0x00000000)
144 #define SDIO_Wait_IT                        ((uint32_t)0x00000100)
145 #define SDIO_Wait_Pend                      ((uint32_t)0x00000200)
146 
147 /* SDIO_CPSM_State */
148 #define SDIO_CPSM_Disable                    ((uint32_t)0x00000000)
149 #define SDIO_CPSM_Enable                     ((uint32_t)0x00000400)
150 
151 /* SDIO_Response_Registers */
152 #define SDIO_RESP1                          ((uint32_t)0x00000000)
153 #define SDIO_RESP2                          ((uint32_t)0x00000004)
154 #define SDIO_RESP3                          ((uint32_t)0x00000008)
155 #define SDIO_RESP4                          ((uint32_t)0x0000000C)
156 
157 /* SDIO_Data_Block_Size */
158 #define SDIO_DataBlockSize_1b               ((uint32_t)0x00000000)
159 #define SDIO_DataBlockSize_2b               ((uint32_t)0x00000010)
160 #define SDIO_DataBlockSize_4b               ((uint32_t)0x00000020)
161 #define SDIO_DataBlockSize_8b               ((uint32_t)0x00000030)
162 #define SDIO_DataBlockSize_16b              ((uint32_t)0x00000040)
163 #define SDIO_DataBlockSize_32b              ((uint32_t)0x00000050)
164 #define SDIO_DataBlockSize_64b              ((uint32_t)0x00000060)
165 #define SDIO_DataBlockSize_128b             ((uint32_t)0x00000070)
166 #define SDIO_DataBlockSize_256b             ((uint32_t)0x00000080)
167 #define SDIO_DataBlockSize_512b             ((uint32_t)0x00000090)
168 #define SDIO_DataBlockSize_1024b            ((uint32_t)0x000000A0)
169 #define SDIO_DataBlockSize_2048b            ((uint32_t)0x000000B0)
170 #define SDIO_DataBlockSize_4096b            ((uint32_t)0x000000C0)
171 #define SDIO_DataBlockSize_8192b            ((uint32_t)0x000000D0)
172 #define SDIO_DataBlockSize_16384b           ((uint32_t)0x000000E0)
173 
174 /* SDIO_Transfer_Direction */
175 #define SDIO_TransferDir_ToCard             ((uint32_t)0x00000000)
176 #define SDIO_TransferDir_ToSDIO             ((uint32_t)0x00000002)
177 
178 /* SDIO_Transfer_Type */
179 #define SDIO_TransferMode_Block             ((uint32_t)0x00000000)
180 #define SDIO_TransferMode_Stream            ((uint32_t)0x00000004)
181 
182 /* SDIO_DPSM_State */
183 #define SDIO_DPSM_Disable                    ((uint32_t)0x00000000)
184 #define SDIO_DPSM_Enable                     ((uint32_t)0x00000001)
185 
186 /* SDIO_Flags */
187 #define SDIO_FLAG_CCRCFAIL                  ((uint32_t)0x00000001)
188 #define SDIO_FLAG_DCRCFAIL                  ((uint32_t)0x00000002)
189 #define SDIO_FLAG_CTIMEOUT                  ((uint32_t)0x00000004)
190 #define SDIO_FLAG_DTIMEOUT                  ((uint32_t)0x00000008)
191 #define SDIO_FLAG_TXUNDERR                  ((uint32_t)0x00000010)
192 #define SDIO_FLAG_RXOVERR                   ((uint32_t)0x00000020)
193 #define SDIO_FLAG_CMDREND                   ((uint32_t)0x00000040)
194 #define SDIO_FLAG_CMDSENT                   ((uint32_t)0x00000080)
195 #define SDIO_FLAG_DATAEND                   ((uint32_t)0x00000100)
196 #define SDIO_FLAG_STBITERR                  ((uint32_t)0x00000200)
197 #define SDIO_FLAG_DBCKEND                   ((uint32_t)0x00000400)
198 #define SDIO_FLAG_CMDACT                    ((uint32_t)0x00000800)
199 #define SDIO_FLAG_TXACT                     ((uint32_t)0x00001000)
200 #define SDIO_FLAG_RXACT                     ((uint32_t)0x00002000)
201 #define SDIO_FLAG_TXFIFOHE                  ((uint32_t)0x00004000)
202 #define SDIO_FLAG_RXFIFOHF                  ((uint32_t)0x00008000)
203 #define SDIO_FLAG_TXFIFOF                   ((uint32_t)0x00010000)
204 #define SDIO_FLAG_RXFIFOF                   ((uint32_t)0x00020000)
205 #define SDIO_FLAG_TXFIFOE                   ((uint32_t)0x00040000)
206 #define SDIO_FLAG_RXFIFOE                   ((uint32_t)0x00080000)
207 #define SDIO_FLAG_TXDAVL                    ((uint32_t)0x00100000)
208 #define SDIO_FLAG_RXDAVL                    ((uint32_t)0x00200000)
209 #define SDIO_FLAG_SDIOIT                    ((uint32_t)0x00400000)
210 #define SDIO_FLAG_CEATAEND                  ((uint32_t)0x00800000)
211 
212 /* SDIO_Read_Wait_Mode */
213 #define SDIO_ReadWaitMode_CLK               ((uint32_t)0x00000001)
214 #define SDIO_ReadWaitMode_DATA2             ((uint32_t)0x00000000)
215 
216 
217 void SDIO_DeInit(void);
218 void SDIO_Init(SDIO_InitTypeDef* SDIO_InitStruct);
219 void SDIO_StructInit(SDIO_InitTypeDef* SDIO_InitStruct);
220 void SDIO_ClockCmd(FunctionalState NewState);
221 void SDIO_SetPowerState(uint32_t SDIO_PowerState);
222 uint32_t SDIO_GetPowerState(void);
223 void SDIO_ITConfig(uint32_t SDIO_IT, FunctionalState NewState);
224 void SDIO_DMACmd(FunctionalState NewState);
225 void SDIO_SendCommand(SDIO_CmdInitTypeDef *SDIO_CmdInitStruct);
226 void SDIO_CmdStructInit(SDIO_CmdInitTypeDef* SDIO_CmdInitStruct);
227 uint8_t SDIO_GetCommandResponse(void);
228 uint32_t SDIO_GetResponse(uint32_t SDIO_RESP);
229 void SDIO_DataConfig(SDIO_DataInitTypeDef* SDIO_DataInitStruct);
230 void SDIO_DataStructInit(SDIO_DataInitTypeDef* SDIO_DataInitStruct);
231 uint32_t SDIO_GetDataCounter(void);
232 uint32_t SDIO_ReadData(void);
233 void SDIO_WriteData(uint32_t Data);
234 uint32_t SDIO_GetFIFOCount(void);
235 void SDIO_StartSDIOReadWait(FunctionalState NewState);
236 void SDIO_StopSDIOReadWait(FunctionalState NewState);
237 void SDIO_SetSDIOReadWaitMode(uint32_t SDIO_ReadWaitMode);
238 void SDIO_SetSDIOOperation(FunctionalState NewState);
239 void SDIO_SendSDIOSuspendCmd(FunctionalState NewState);
240 void SDIO_CommandCompletionCmd(FunctionalState NewState);
241 void SDIO_CEATAITCmd(FunctionalState NewState);
242 void SDIO_SendCEATACmd(FunctionalState NewState);
243 FlagStatus SDIO_GetFlagStatus(uint32_t SDIO_FLAG);
244 void SDIO_ClearFlag(uint32_t SDIO_FLAG);
245 ITStatus SDIO_GetITStatus(uint32_t SDIO_IT);
246 void SDIO_ClearITPendingBit(uint32_t SDIO_IT);
247 
248 #ifdef __cplusplus
249 }
250 #endif
251 
252 #endif
253