MG32F157 Standard Peripherals Firmware Library
bsp_sdio_sdcard.h
1 
11 /* Define to prevent recursive inclusion -------------------------------------*/
12 #ifndef __MG32F157_SDIO_SDCARD_H
13 #define __MG32F157_SDIO_SDCARD_H
14 
15 
16 #ifdef __cplusplus
17  extern "C" {
18 #endif
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "mg32f157_sdio.h"
22 
23 /* Exported types ------------------------------------------------------------*/
24 typedef enum
25 {
29  SD_CMD_CRC_FAIL = (1),
30  SD_DATA_CRC_FAIL = (2),
31  SD_CMD_RSP_TIMEOUT = (3),
32  SD_DATA_TIMEOUT = (4),
33  SD_TX_UNDERRUN = (5),
34  SD_RX_OVERRUN = (6),
35  SD_START_BIT_ERR = (7),
36  SD_CMD_OUT_OF_RANGE = (8),
37  SD_ADDR_MISALIGNED = (9),
38  SD_BLOCK_LEN_ERR = (10),
39  SD_ERASE_SEQ_ERR = (11),
40  SD_BAD_ERASE_PARAM = (12),
41  SD_WRITE_PROT_VIOLATION = (13),
42  SD_LOCK_UNLOCK_FAILED = (14),
43  SD_COM_CRC_FAILED = (15),
44  SD_ILLEGAL_CMD = (16),
45  SD_CARD_ECC_FAILED = (17),
46  SD_CC_ERROR = (18),
47  SD_GENERAL_UNKNOWN_ERROR = (19),
48  SD_STREAM_READ_UNDERRUN = (20),
49  SD_STREAM_WRITE_OVERRUN = (21),
50  SD_CID_CSD_OVERWRITE = (22),
51  SD_WP_ERASE_SKIP = (23),
52  SD_CARD_ECC_DISABLED = (24),
53  SD_ERASE_RESET = (25),
54  SD_AKE_SEQ_ERROR = (26),
55  SD_INVALID_VOLTRANGE = (27),
56  SD_ADDR_OUT_OF_RANGE = (28),
57  SD_SWITCH_ERROR = (29),
58  SD_SDIO_DISABLED = (30),
59  SD_SDIO_FUNCTION_BUSY = (31),
60  SD_SDIO_FUNCTION_FAILED = (32),
61  SD_SDIO_UNKNOWN_FUNCTION = (33),
62 
66  SD_INTERNAL_ERROR,
67  SD_NOT_CONFIGURED,
68  SD_REQUEST_PENDING,
69  SD_REQUEST_NOT_APPLICABLE,
70  SD_INVALID_PARAMETER,
71  SD_UNSUPPORTED_FEATURE,
72  SD_UNSUPPORTED_HW,
73  SD_ERROR,
74  SD_OK = 0
75 } SD_Error;
76 
80 typedef enum
81 {
82  SD_TRANSFER_OK = 0,
83  SD_TRANSFER_BUSY = 1,
84  SD_TRANSFER_ERROR
85 } SDTransferState;
86 
90 typedef enum
91 {
92  SD_CARD_READY = ((uint32_t)0x00000001),
93  SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002),
94  SD_CARD_STANDBY = ((uint32_t)0x00000003),
95  SD_CARD_TRANSFER = ((uint32_t)0x00000004),
96  SD_CARD_SENDING = ((uint32_t)0x00000005),
97  SD_CARD_RECEIVING = ((uint32_t)0x00000006),
98  SD_CARD_PROGRAMMING = ((uint32_t)0x00000007),
99  SD_CARD_DISCONNECTED = ((uint32_t)0x00000008),
100  SD_CARD_ERROR = ((uint32_t)0x000000FF)
101 }SDCardState;
102 
103 
107 typedef struct
108 {
109  __IO uint8_t CSDStruct;
111  __IO uint8_t Reserved1;
112  __IO uint8_t TAAC;
113  __IO uint8_t NSAC;
114  __IO uint8_t MaxBusClkFrec;
116  __IO uint8_t RdBlockLen;
117  __IO uint8_t PartBlockRead;
120  __IO uint8_t DSRImpl;
121  __IO uint8_t Reserved2;
122  __IO uint32_t DeviceSize;
127  __IO uint8_t DeviceSizeMul;
128  __IO uint8_t EraseGrSize;
129  __IO uint8_t EraseGrMul;
132  __IO uint8_t ManDeflECC;
133  __IO uint8_t WrSpeedFact;
134  __IO uint8_t MaxWrBlockLen;
136  __IO uint8_t Reserved3;
139  __IO uint8_t CopyFlag;
140  __IO uint8_t PermWrProtect;
141  __IO uint8_t TempWrProtect;
142  __IO uint8_t FileFormat;
143  __IO uint8_t ECC;
144  __IO uint8_t CSD_CRC;
145  __IO uint8_t Reserved4;
146 } SD_CSD;
147 
151 typedef struct
152 {
154  __IO uint16_t OEM_AppliID;
155  __IO uint32_t ProdName1;
156  __IO uint8_t ProdName2;
157  __IO uint8_t ProdRev;
158  __IO uint32_t ProdSN;
159  __IO uint8_t Reserved1;
160  __IO uint16_t ManufactDate;
161  __IO uint8_t CID_CRC;
162  __IO uint8_t Reserved2;
163 } SD_CID;
164 
168 typedef struct
169 {
170  __IO uint8_t DAT_BUS_WIDTH;
171  __IO uint8_t SECURED_MODE;
172  __IO uint16_t SD_CARD_TYPE;
173  __IO uint32_t SIZE_OF_PROTECTED_AREA;
174  __IO uint8_t SPEED_CLASS;
175  __IO uint8_t PERFORMANCE_MOVE;
176  __IO uint8_t AU_SIZE;
177  __IO uint16_t ERASE_SIZE;
178  __IO uint8_t ERASE_TIMEOUT;
179  __IO uint8_t ERASE_OFFSET;
180 } SD_CardStatus;
181 
182 
186 typedef struct
187 {
188  SD_CSD SD_csd;
189  SD_CID SD_cid;
190  uint64_t CardCapacity;
191  uint32_t CardBlockSize;
192  uint16_t RCA;
193  uint8_t CardType;
194 } SD_CardInfo;
195 
196 /* SDIO Intialization Frequency (400KHz max) */
197 #define SDIO_INIT_CLK_DIV ((uint8_t)0x1E)
198 
199 /* SDIO Data Transfer Frequency */
200 #define SDIO_TRANSFER_CLK_DIV ((uint8_t)0x3)
201 
205 #define SD_CMD_GO_IDLE_STATE ((uint8_t)0)
206 #define SD_CMD_SEND_OP_COND ((uint8_t)1)
207 #define SD_CMD_ALL_SEND_CID ((uint8_t)2)
208 #define SD_CMD_SET_REL_ADDR ((uint8_t)3)
209 #define SD_CMD_SET_DSR ((uint8_t)4)
210 #define SD_CMD_SDIO_SEN_OP_COND ((uint8_t)5)
211 #define SD_CMD_HS_SWITCH ((uint8_t)6)
212 #define SD_CMD_SEL_DESEL_CARD ((uint8_t)7)
213 #define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8)
214 #define SD_CMD_SEND_CSD ((uint8_t)9)
215 #define SD_CMD_SEND_CID ((uint8_t)10)
216 #define SD_CMD_READ_DAT_UNTIL_STOP ((uint8_t)11)
217 #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12)
218 #define SD_CMD_SEND_STATUS ((uint8_t)13)
219 #define SD_CMD_HS_BUSTEST_READ ((uint8_t)14)
220 #define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15)
221 #define SD_CMD_SET_BLOCKLEN ((uint8_t)16)
222 #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17)
223 #define SD_CMD_READ_MULT_BLOCK ((uint8_t)18)
224 #define SD_CMD_HS_BUSTEST_WRITE ((uint8_t)19)
225 #define SD_CMD_WRITE_DAT_UNTIL_STOP ((uint8_t)20)
226 #define SD_CMD_SET_BLOCK_COUNT ((uint8_t)23)
227 #define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24)
228 #define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25)
229 #define SD_CMD_PROG_CID ((uint8_t)26)
230 #define SD_CMD_PROG_CSD ((uint8_t)27)
231 #define SD_CMD_SET_WRITE_PROT ((uint8_t)28)
232 #define SD_CMD_CLR_WRITE_PROT ((uint8_t)29)
233 #define SD_CMD_SEND_WRITE_PROT ((uint8_t)30)
234 #define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32)
236 #define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33)
238 #define SD_CMD_ERASE_GRP_START ((uint8_t)35)
241 #define SD_CMD_ERASE_GRP_END ((uint8_t)36)
244 #define SD_CMD_ERASE ((uint8_t)38)
245 #define SD_CMD_FAST_IO ((uint8_t)39)
246 #define SD_CMD_GO_IRQ_STATE ((uint8_t)40)
247 #define SD_CMD_LOCK_UNLOCK ((uint8_t)42)
248 #define SD_CMD_APP_CMD ((uint8_t)55)
249 #define SD_CMD_GEN_CMD ((uint8_t)56)
250 #define SD_CMD_NO_CMD ((uint8_t)64)
251 
256 #define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6)
257 #define SD_CMD_SD_APP_STAUS ((uint8_t)13)
258 #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22)
259 #define SD_CMD_SD_APP_OP_COND ((uint8_t)41)
260 #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42)
261 #define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51)
262 #define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52)
263 #define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53)
269 #define SD_CMD_SD_APP_GET_MKB ((uint8_t)43)
270 #define SD_CMD_SD_APP_GET_MID ((uint8_t)44)
271 #define SD_CMD_SD_APP_SET_CER_RN1 ((uint8_t)45)
272 #define SD_CMD_SD_APP_GET_CER_RN2 ((uint8_t)46)
273 #define SD_CMD_SD_APP_SET_CER_RES2 ((uint8_t)47)
274 #define SD_CMD_SD_APP_GET_CER_RES1 ((uint8_t)48)
275 #define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18)
276 #define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25)
277 #define SD_CMD_SD_APP_SECURE_ERASE ((uint8_t)38)
278 #define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49)
279 #define SD_CMD_SD_APP_SECURE_WRITE_MKB ((uint8_t)48)
281 /* Uncomment the following line to select the SDIO Data transfer mode */
282 // #define SD_DMA_MODE ((uint32_t)0x00000000)
283 #define SD_POLLING_MODE ((uint32_t)0x00000002)
284 
288 #define SD_PRESENT ((uint8_t)0x01)
289 #define SD_NOT_PRESENT ((uint8_t)0x00)
290 
294 #define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000)
295 #define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001)
296 #define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002)
297 #define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003)
298 #define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004)
299 #define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005)
300 #define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006)
301 #define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007)
302 
303 void SD_DeInit(void);
304 SD_Error SD_Init(void);
305 SDCardState SD_GetState(void);
306 SDTransferState SD_GetStatus(void);
307 SD_Error SD_PowerON(void);
308 SD_Error SD_PowerOFF(void);
309 SD_Error SD_InitializeCards(void);
310 SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo);
311 SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus);
312 SD_Error SD_EnableWideBusOperation(uint32_t WideMode);
313 SD_Error SD_SelectDeselect(uint32_t Addr);
314 SD_Error SD_ReadBlock(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize);
315 SD_Error SD_WriteBlock(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize);
316 SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
317 SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
318 SD_Error SD_StopTransfer(void);
319 SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr);
320 SD_Error SD_SendSDStatus(uint32_t *psdstatus);
321 
322 
323 
324 
325 #ifdef __cplusplus
326 }
327 #endif
328 
329 #endif /* __MG32F157_SDIO_SDCARD_H */
330 
__IO uint8_t RdBlockMisalign
Definition: bsp_sdio_sdcard.h:119
SD_Error SD_SendSDStatus(uint32_t *psdstatus)
Returns the current SD card's status.
Definition: bsp_sdio_sdcard.c:1626
SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
Allows to read blocks from a specified address in a card. The Data transfer can be managed by DMA mod...
Definition: bsp_sdio_sdcard.c:965
__IO uint8_t RdBlockLen
Definition: bsp_sdio_sdcard.h:116
__IO uint8_t Reserved1
Definition: bsp_sdio_sdcard.h:111
__IO uint8_t WriteBlockPaPartial
Definition: bsp_sdio_sdcard.h:135
SD Card Status.
Definition: bsp_sdio_sdcard.h:168
__IO uint8_t PermWrProtect
Definition: bsp_sdio_sdcard.h:140
SD_Error SD_PowerON(void)
Enquires cards about their operating voltage and configures clock controls and stores SD information ...
Definition: bsp_sdio_sdcard.c:261
__IO uint8_t MaxBusClkFrec
Definition: bsp_sdio_sdcard.h:114
__IO uint8_t ProdRev
Definition: bsp_sdio_sdcard.h:157
__IO uint8_t WrProtectGrSize
Definition: bsp_sdio_sdcard.h:130
__IO uint8_t MaxRdCurrentVDDMax
Definition: bsp_sdio_sdcard.h:124
SD_Error SD_Init(void)
Initializes the SD according to the specified parameters in the SD_HandleTypeDef and create the assoc...
Definition: bsp_sdio_sdcard.c:147
__IO uint8_t TempWrProtect
Definition: bsp_sdio_sdcard.h:141
__IO uint32_t ProdSN
Definition: bsp_sdio_sdcard.h:158
SDTransferState SD_GetStatus(void)
Gets the cuurent sd card data transfer status.
Definition: bsp_sdio_sdcard.c:234
__IO uint8_t Reserved1
Definition: bsp_sdio_sdcard.h:159
__IO uint16_t OEM_AppliID
Definition: bsp_sdio_sdcard.h:154
SD_Error SD_ReadBlock(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize)
Allows to read one block from a specified address in a card. The Data transfer can be managed by DMA ...
Definition: bsp_sdio_sdcard.c:841
__IO uint8_t ProdName2
Definition: bsp_sdio_sdcard.h:156
__IO uint8_t FileFormatGrouop
Definition: bsp_sdio_sdcard.h:138
SD_Error SD_SelectDeselect(uint32_t Addr)
Send the Select Deselect command and check the response.
Definition: bsp_sdio_sdcard.c:810
uint64_t CardCapacity
Definition: bsp_sdio_sdcard.h:190
__IO uint8_t DSRImpl
Definition: bsp_sdio_sdcard.h:120
__IO uint8_t EraseGrSize
Definition: bsp_sdio_sdcard.h:128
This file contains all the functions prototypes for the SDIO firmware library.
__IO uint8_t ManDeflECC
Definition: bsp_sdio_sdcard.h:132
SD_Error SD_WriteBlock(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize)
Allows to write one block starting from a specified address in a card. The Data transfer can be manag...
Definition: bsp_sdio_sdcard.c:1095
__IO uint8_t SysSpecVersion
Definition: bsp_sdio_sdcard.h:110
SD Card information.
Definition: bsp_sdio_sdcard.h:186
SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
Definition: bsp_sdio_sdcard.c:1212
__IO uint8_t EraseGrMul
Definition: bsp_sdio_sdcard.h:129
__IO uint8_t Reserved3
Definition: bsp_sdio_sdcard.h:136
__IO uint8_t MaxWrCurrentVDDMax
Definition: bsp_sdio_sdcard.h:126
__IO uint8_t WrProtectGrEnable
Definition: bsp_sdio_sdcard.h:131
__IO uint8_t MaxRdCurrentVDDMin
Definition: bsp_sdio_sdcard.h:123
uint32_t CardBlockSize
Definition: bsp_sdio_sdcard.h:191
SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr)
Allows to erase memory area specified for the given card.
Definition: bsp_sdio_sdcard.c:1386
__IO uint8_t TAAC
Definition: bsp_sdio_sdcard.h:112
SD_Error SD_InitializeCards(void)
Initializes the SD Card.
Definition: bsp_sdio_sdcard.c:401
SD_Error SD_PowerOFF(void)
Disable the SDIO card clock output.
Definition: bsp_sdio_sdcard.c:379
__IO uint32_t DeviceSize
Definition: bsp_sdio_sdcard.h:122
__IO uint16_t CardComdClasses
Definition: bsp_sdio_sdcard.h:115
__IO uint8_t ContentProtectAppli
Definition: bsp_sdio_sdcard.h:137
__IO uint8_t WrSpeedFact
Definition: bsp_sdio_sdcard.h:133
#define __IO
Definition: core_cm3.h:170
__IO uint8_t Reserved4
Definition: bsp_sdio_sdcard.h:145
__IO uint8_t CSD_CRC
Definition: bsp_sdio_sdcard.h:144
Card Specific Data: CSD Register.
Definition: bsp_sdio_sdcard.h:107
__IO uint8_t DeviceSizeMul
Definition: bsp_sdio_sdcard.h:127
__IO uint8_t CSDStruct
Definition: bsp_sdio_sdcard.h:109
__IO uint8_t Reserved2
Definition: bsp_sdio_sdcard.h:162
__IO uint8_t Reserved2
Definition: bsp_sdio_sdcard.h:121
__IO uint8_t CID_CRC
Definition: bsp_sdio_sdcard.h:161
Card Identification Data: CID Register.
Definition: bsp_sdio_sdcard.h:151
__IO uint8_t NSAC
Definition: bsp_sdio_sdcard.h:113
__IO uint8_t ManufacturerID
Definition: bsp_sdio_sdcard.h:153
__IO uint8_t MaxWrBlockLen
Definition: bsp_sdio_sdcard.h:134
__IO uint16_t ManufactDate
Definition: bsp_sdio_sdcard.h:160
SDCardState SD_GetState(void)
Returns the current card's state.
Definition: bsp_sdio_sdcard.c:203
SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus)
Enables wide bus opeartion for the requeseted card if supported by card.
Definition: bsp_sdio_sdcard.c:689
__IO uint8_t MaxWrCurrentVDDMin
Definition: bsp_sdio_sdcard.h:125
void SD_DeInit(void)
Definition: bsp_sdio_sdcard.c:116
__IO uint8_t CopyFlag
Definition: bsp_sdio_sdcard.h:139
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
Definition: bsp_sdio_sdcard.c:478
__IO uint8_t ECC
Definition: bsp_sdio_sdcard.h:143
__IO uint8_t FileFormat
Definition: bsp_sdio_sdcard.h:142
SD_Error SD_EnableWideBusOperation(uint32_t WideMode)
Definition: bsp_sdio_sdcard.c:764
__IO uint32_t ProdName1
Definition: bsp_sdio_sdcard.h:155
__IO uint8_t WrBlockMisalign
Definition: bsp_sdio_sdcard.h:118
__IO uint8_t PartBlockRead
Definition: bsp_sdio_sdcard.h:117
SD_Error SD_StopTransfer(void)
Aborts an ongoing data transfer.
Definition: bsp_sdio_sdcard.c:1365