comparison stm32_eval_sdio_sd.h @ 49:ace431a0d0f5

Add SDIO code poached from STM. Use FatFS to read from SD card. LFN doesn't work reliably so it's disabled for now.
author Daniel O'Connor <darius@dons.net.au>
date Wed, 03 Apr 2013 23:34:20 +1030
parents
children
comparison
equal deleted inserted replaced
48:2f336d212c74 49:ace431a0d0f5
1 /**
2 ******************************************************************************
3 * @file stm32_eval_sdio_sd.h
4 * @author MCD Application Team
5 * @version V4.5.0
6 * @date 07-March-2011
7 * @brief This file contains all the functions prototypes for the SD Card
8 * stm32_eval_sdio_sd driver firmware library.
9 ******************************************************************************
10 * @attention
11 *
12 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
13 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
14 * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
15 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
16 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
17 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
18 *
19 * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
20 ******************************************************************************
21 */
22
23 /* Define to prevent recursive inclusion -------------------------------------*/
24 #ifndef __STM32_EVAL_SDIO_SD_H
25 #define __STM32_EVAL_SDIO_SD_H
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30
31 /* Includes ------------------------------------------------------------------*/
32 #include "stm32_eval.h"
33
34 /** @addtogroup Utilities
35 * @{
36 */
37
38 /** @addtogroup STM32_EVAL
39 * @{
40 */
41
42 /** @addtogroup Common
43 * @{
44 */
45
46 /** @addtogroup STM32_EVAL_SDIO_SD
47 * @{
48 */
49
50 /** @defgroup STM32_EVAL_SDIO_SD_Exported_Types
51 * @{
52 */
53 typedef enum
54 {
55 /**
56 * @brief SDIO specific error defines
57 */
58 SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */
59 SD_DATA_CRC_FAIL = (2), /*!< Data bock sent/received (CRC check Failed) */
60 SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */
61 SD_DATA_TIMEOUT = (4), /*!< Data time out */
62 SD_TX_UNDERRUN = (5), /*!< Transmit FIFO under-run */
63 SD_RX_OVERRUN = (6), /*!< Receive FIFO over-run */
64 SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in widE bus mode */
65 SD_CMD_OUT_OF_RANGE = (8), /*!< CMD's argument was out of range.*/
66 SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */
67 SD_BLOCK_LEN_ERR = (10), /*!< Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */
68 SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs.*/
69 SD_BAD_ERASE_PARAM = (12), /*!< An Invalid selection for erase groups */
70 SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */
71 SD_LOCK_UNLOCK_FAILED = (14), /*!< Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card */
72 SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */
73 SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */
74 SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */
75 SD_CC_ERROR = (18), /*!< Internal card controller error */
76 SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or Unknown error */
77 SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */
78 SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */
79 SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */
80 SD_WP_ERASE_SKIP = (23), /*!< only partial address space was erased */
81 SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */
82 SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */
83 SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */
84 SD_INVALID_VOLTRANGE = (27),
85 SD_ADDR_OUT_OF_RANGE = (28),
86 SD_SWITCH_ERROR = (29),
87 SD_SDIO_DISABLED = (30),
88 SD_SDIO_FUNCTION_BUSY = (31),
89 SD_SDIO_FUNCTION_FAILED = (32),
90 SD_SDIO_UNKNOWN_FUNCTION = (33),
91
92 /**
93 * @brief Standard error defines
94 */
95 SD_INTERNAL_ERROR,
96 SD_NOT_CONFIGURED,
97 SD_REQUEST_PENDING,
98 SD_REQUEST_NOT_APPLICABLE,
99 SD_INVALID_PARAMETER,
100 SD_UNSUPPORTED_FEATURE,
101 SD_UNSUPPORTED_HW,
102 SD_ERROR,
103 SD_OK = 0
104 } SD_Error;
105
106 /**
107 * @brief SDIO Transfer state
108 */
109 typedef enum
110 {
111 SD_TRANSFER_OK = 0,
112 SD_TRANSFER_BUSY = 1,
113 SD_TRANSFER_ERROR
114 } SDTransferState;
115
116 /**
117 * @brief SD Card States
118 */
119 typedef enum
120 {
121 SD_CARD_READY = ((uint32_t)0x00000001),
122 SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002),
123 SD_CARD_STANDBY = ((uint32_t)0x00000003),
124 SD_CARD_TRANSFER = ((uint32_t)0x00000004),
125 SD_CARD_SENDING = ((uint32_t)0x00000005),
126 SD_CARD_RECEIVING = ((uint32_t)0x00000006),
127 SD_CARD_PROGRAMMING = ((uint32_t)0x00000007),
128 SD_CARD_DISCONNECTED = ((uint32_t)0x00000008),
129 SD_CARD_ERROR = ((uint32_t)0x000000FF)
130 }SDCardState;
131
132
133 /**
134 * @brief Card Specific Data: CSD Register
135 */
136 typedef struct
137 {
138 __IO uint8_t CSDStruct; /*!< CSD structure */
139 __IO uint8_t SysSpecVersion; /*!< System specification version */
140 __IO uint8_t Reserved1; /*!< Reserved */
141 __IO uint8_t TAAC; /*!< Data read access-time 1 */
142 __IO uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */
143 __IO uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */
144 __IO uint16_t CardComdClasses; /*!< Card command classes */
145 __IO uint8_t RdBlockLen; /*!< Max. read data block length */
146 __IO uint8_t PartBlockRead; /*!< Partial blocks for read allowed */
147 __IO uint8_t WrBlockMisalign; /*!< Write block misalignment */
148 __IO uint8_t RdBlockMisalign; /*!< Read block misalignment */
149 __IO uint8_t DSRImpl; /*!< DSR implemented */
150 __IO uint8_t Reserved2; /*!< Reserved */
151 __IO uint32_t DeviceSize; /*!< Device Size */
152 __IO uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
153 __IO uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
154 __IO uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
155 __IO uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
156 __IO uint8_t DeviceSizeMul; /*!< Device size multiplier */
157 __IO uint8_t EraseGrSize; /*!< Erase group size */
158 __IO uint8_t EraseGrMul; /*!< Erase group size multiplier */
159 __IO uint8_t WrProtectGrSize; /*!< Write protect group size */
160 __IO uint8_t WrProtectGrEnable; /*!< Write protect group enable */
161 __IO uint8_t ManDeflECC; /*!< Manufacturer default ECC */
162 __IO uint8_t WrSpeedFact; /*!< Write speed factor */
163 __IO uint8_t MaxWrBlockLen; /*!< Max. write data block length */
164 __IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
165 __IO uint8_t Reserved3; /*!< Reserded */
166 __IO uint8_t ContentProtectAppli; /*!< Content protection application */
167 __IO uint8_t FileFormatGrouop; /*!< File format group */
168 __IO uint8_t CopyFlag; /*!< Copy flag (OTP) */
169 __IO uint8_t PermWrProtect; /*!< Permanent write protection */
170 __IO uint8_t TempWrProtect; /*!< Temporary write protection */
171 __IO uint8_t FileFormat; /*!< File Format */
172 __IO uint8_t ECC; /*!< ECC code */
173 __IO uint8_t CSD_CRC; /*!< CSD CRC */
174 __IO uint8_t Reserved4; /*!< always 1*/
175 } SD_CSD;
176
177 /**
178 * @brief Card Identification Data: CID Register
179 */
180 typedef struct
181 {
182 __IO uint8_t ManufacturerID; /*!< ManufacturerID */
183 __IO uint16_t OEM_AppliID; /*!< OEM/Application ID */
184 __IO uint32_t ProdName1; /*!< Product Name part1 */
185 __IO uint8_t ProdName2; /*!< Product Name part2*/
186 __IO uint8_t ProdRev; /*!< Product Revision */
187 __IO uint32_t ProdSN; /*!< Product Serial Number */
188 __IO uint8_t Reserved1; /*!< Reserved1 */
189 __IO uint16_t ManufactDate; /*!< Manufacturing Date */
190 __IO uint8_t CID_CRC; /*!< CID CRC */
191 __IO uint8_t Reserved2; /*!< always 1 */
192 } SD_CID;
193
194 /**
195 * @brief SD Card Status
196 */
197 typedef struct
198 {
199 __IO uint8_t DAT_BUS_WIDTH;
200 __IO uint8_t SECURED_MODE;
201 __IO uint16_t SD_CARD_TYPE;
202 __IO uint32_t SIZE_OF_PROTECTED_AREA;
203 __IO uint8_t SPEED_CLASS;
204 __IO uint8_t PERFORMANCE_MOVE;
205 __IO uint8_t AU_SIZE;
206 __IO uint16_t ERASE_SIZE;
207 __IO uint8_t ERASE_TIMEOUT;
208 __IO uint8_t ERASE_OFFSET;
209 } SD_CardStatus;
210
211
212 /**
213 * @brief SD Card information
214 */
215 typedef struct
216 {
217 SD_CSD SD_csd;
218 SD_CID SD_cid;
219 uint32_t CardCapacity; /*!< Card Capacity */
220 uint32_t CardBlockSize; /*!< Card Block Size */
221 uint16_t RCA;
222 uint8_t CardType;
223 } SD_CardInfo;
224
225 /**
226 * @}
227 */
228
229 /** @defgroup STM32_EVAL_SDIO_SD_Exported_Constants
230 * @{
231 */
232
233 /**
234 * @brief SDIO Commands Index
235 */
236 #define SD_CMD_GO_IDLE_STATE ((uint8_t)0)
237 #define SD_CMD_SEND_OP_COND ((uint8_t)1)
238 #define SD_CMD_ALL_SEND_CID ((uint8_t)2)
239 #define SD_CMD_SET_REL_ADDR ((uint8_t)3) /*!< SDIO_SEND_REL_ADDR for SD Card */
240 #define SD_CMD_SET_DSR ((uint8_t)4)
241 #define SD_CMD_SDIO_SEN_OP_COND ((uint8_t)5)
242 #define SD_CMD_HS_SWITCH ((uint8_t)6)
243 #define SD_CMD_SEL_DESEL_CARD ((uint8_t)7)
244 #define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8)
245 #define SD_CMD_SEND_CSD ((uint8_t)9)
246 #define SD_CMD_SEND_CID ((uint8_t)10)
247 #define SD_CMD_READ_DAT_UNTIL_STOP ((uint8_t)11) /*!< SD Card doesn't support it */
248 #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12)
249 #define SD_CMD_SEND_STATUS ((uint8_t)13)
250 #define SD_CMD_HS_BUSTEST_READ ((uint8_t)14)
251 #define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15)
252 #define SD_CMD_SET_BLOCKLEN ((uint8_t)16)
253 #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17)
254 #define SD_CMD_READ_MULT_BLOCK ((uint8_t)18)
255 #define SD_CMD_HS_BUSTEST_WRITE ((uint8_t)19)
256 #define SD_CMD_WRITE_DAT_UNTIL_STOP ((uint8_t)20) /*!< SD Card doesn't support it */
257 #define SD_CMD_SET_BLOCK_COUNT ((uint8_t)23) /*!< SD Card doesn't support it */
258 #define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24)
259 #define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25)
260 #define SD_CMD_PROG_CID ((uint8_t)26) /*!< reserved for manufacturers */
261 #define SD_CMD_PROG_CSD ((uint8_t)27)
262 #define SD_CMD_SET_WRITE_PROT ((uint8_t)28)
263 #define SD_CMD_CLR_WRITE_PROT ((uint8_t)29)
264 #define SD_CMD_SEND_WRITE_PROT ((uint8_t)30)
265 #define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32) /*!< To set the address of the first write
266 block to be erased. (For SD card only) */
267 #define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33) /*!< To set the address of the last write block of the
268 continuous range to be erased. (For SD card only) */
269 #define SD_CMD_ERASE_GRP_START ((uint8_t)35) /*!< To set the address of the first write block to be erased.
270 (For MMC card only spec 3.31) */
271
272 #define SD_CMD_ERASE_GRP_END ((uint8_t)36) /*!< To set the address of the last write block of the
273 continuous range to be erased. (For MMC card only spec 3.31) */
274
275 #define SD_CMD_ERASE ((uint8_t)38)
276 #define SD_CMD_FAST_IO ((uint8_t)39) /*!< SD Card doesn't support it */
277 #define SD_CMD_GO_IRQ_STATE ((uint8_t)40) /*!< SD Card doesn't support it */
278 #define SD_CMD_LOCK_UNLOCK ((uint8_t)42)
279 #define SD_CMD_APP_CMD ((uint8_t)55)
280 #define SD_CMD_GEN_CMD ((uint8_t)56)
281 #define SD_CMD_NO_CMD ((uint8_t)64)
282
283 /**
284 * @brief Following commands are SD Card Specific commands.
285 * SDIO_APP_CMD should be sent before sending these commands.
286 */
287 #define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6) /*!< For SD Card only */
288 #define SD_CMD_SD_APP_STAUS ((uint8_t)13) /*!< For SD Card only */
289 #define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22) /*!< For SD Card only */
290 #define SD_CMD_SD_APP_OP_COND ((uint8_t)41) /*!< For SD Card only */
291 #define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /*!< For SD Card only */
292 #define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51) /*!< For SD Card only */
293 #define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52) /*!< For SD I/O Card only */
294 #define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53) /*!< For SD I/O Card only */
295
296 /**
297 * @brief Following commands are SD Card Specific security commands.
298 * SDIO_APP_CMD should be sent before sending these commands.
299 */
300 #define SD_CMD_SD_APP_GET_MKB ((uint8_t)43) /*!< For SD Card only */
301 #define SD_CMD_SD_APP_GET_MID ((uint8_t)44) /*!< For SD Card only */
302 #define SD_CMD_SD_APP_SET_CER_RN1 ((uint8_t)45) /*!< For SD Card only */
303 #define SD_CMD_SD_APP_GET_CER_RN2 ((uint8_t)46) /*!< For SD Card only */
304 #define SD_CMD_SD_APP_SET_CER_RES2 ((uint8_t)47) /*!< For SD Card only */
305 #define SD_CMD_SD_APP_GET_CER_RES1 ((uint8_t)48) /*!< For SD Card only */
306 #define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18) /*!< For SD Card only */
307 #define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /*!< For SD Card only */
308 #define SD_CMD_SD_APP_SECURE_ERASE ((uint8_t)38) /*!< For SD Card only */
309 #define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49) /*!< For SD Card only */
310 #define SD_CMD_SD_APP_SECURE_WRITE_MKB ((uint8_t)48) /*!< For SD Card only */
311
312 /**
313 * @brief SD detection on its memory slot
314 */
315 #define SD_PRESENT ((uint8_t)0x01)
316 #define SD_NOT_PRESENT ((uint8_t)0x00)
317
318 /**
319 * @brief Supported SD Memory Cards
320 */
321 #define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000)
322 #define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001)
323 #define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002)
324 #define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003)
325 #define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004)
326 #define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005)
327 #define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006)
328 #define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007)
329
330 /**
331 * @}
332 */
333
334 /** @defgroup STM32_EVAL_SDIO_SD_Exported_Macros
335 * @{
336 */
337 /**
338 * @}
339 */
340
341 /** @defgroup STM32_EVAL_SDIO_SD_Exported_Functions
342 * @{
343 */
344 void SD_DeInit(void);
345 SD_Error SD_Init(void);
346 SDTransferState SD_GetStatus(void);
347 SDCardState SD_GetState(void);
348 uint8_t SD_Detect(void);
349 SD_Error SD_PowerON(void);
350 SD_Error SD_PowerOFF(void);
351 SD_Error SD_InitializeCards(void);
352 SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo);
353 SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus);
354 SD_Error SD_EnableWideBusOperation(uint32_t WideMode);
355 SD_Error SD_SelectDeselect(uint32_t addr);
356 SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize);
357 SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
358 SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize);
359 SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
360 SDTransferState SD_GetTransferState(void);
361 SD_Error SD_StopTransfer(void);
362 SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr);
363 SD_Error SD_SendStatus(uint32_t *pcardstatus);
364 SD_Error SD_SendSDStatus(uint32_t *psdstatus);
365 SD_Error SD_ProcessIRQSrc(void);
366 SD_Error SD_WaitReadOperation(void);
367 SD_Error SD_WaitWriteOperation(void);
368 #ifdef __cplusplus
369 }
370 #endif
371
372 #endif /* __STM32_EVAL_SDIO_SD_H */
373 /**
374 * @}
375 */
376
377 /**
378 * @}
379 */
380
381 /**
382 * @}
383 */
384
385 /**
386 * @}
387 */
388
389 /**
390 * @}
391 */
392
393 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/