amiro-blt / Target / Demo / ARMCM4_STM32F405_Power_Management_GCC / Boot / lib / fatfs / mmc.c @ 69661903
History | View | Annotate | Download (108 KB)
1 |
/****************************************************************************************
|
---|---|
2 |
| Project Name: Application Development using the Olimex STM32-E407 board
|
3 |
| Description: Disk I/O source file for FatFS configured for an MMC card on SDIO.
|
4 |
| File Name: mmc.c
|
5 |
| Notes: The SDIO interface was derived from the SDCARD example that comes with
|
6 |
| the Standard Peripheral Library from STMicroelectronics as falls under
|
7 |
| their copyright and license. By using this file you agree to:
|
8 |
|
|
9 |
| COPYRIGHT 2013 STMicroelectronics
|
10 |
|
|
11 |
| Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
|
12 |
| You may not use this file except in compliance with the License.
|
13 |
| You may obtain a copy of the License at:
|
14 |
|
|
15 |
| http://www.st.com/software_license_agreement_liberty_v2
|
16 |
|
|
17 |
| Unless required by applicable law or agreed to in writing, software
|
18 |
| distributed under the License is distributed on an "AS IS" BASIS,
|
19 |
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
20 |
| See the License for the specific language governing permissions and
|
21 |
| limitations under the License.
|
22 |
|
|
23 |
|****************************************************************************************/
|
24 |
|
25 |
|
26 |
/****************************************************************************************
|
27 |
* Include files
|
28 |
****************************************************************************************/
|
29 |
#include <string.h> |
30 |
#include "diskio.h" |
31 |
#include "stm32f4xx.h" |
32 |
|
33 |
/****************************************************************************************
|
34 |
* Type definitions
|
35 |
****************************************************************************************/
|
36 |
/**
|
37 |
* @brief SDIO specific error defines
|
38 |
*/
|
39 |
typedef enum |
40 |
{ |
41 |
SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */ |
42 |
SD_DATA_CRC_FAIL = (2), /*!< Data bock sent/received (CRC check Failed) */ |
43 |
SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */ |
44 |
SD_DATA_TIMEOUT = (4), /*!< Data time out */ |
45 |
SD_TX_UNDERRUN = (5), /*!< Transmit FIFO under-run */ |
46 |
SD_RX_OVERRUN = (6), /*!< Receive FIFO over-run */ |
47 |
SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in widE bus mode */ |
48 |
SD_CMD_OUT_OF_RANGE = (8), /*!< CMD's argument was out of range.*/ |
49 |
SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */ |
50 |
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 */ |
51 |
SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs.*/ |
52 |
SD_BAD_ERASE_PARAM = (12), /*!< An Invalid selection for erase groups */ |
53 |
SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */ |
54 |
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 */ |
55 |
SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */ |
56 |
SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */ |
57 |
SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */ |
58 |
SD_CC_ERROR = (18), /*!< Internal card controller error */ |
59 |
SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or Unknown error */ |
60 |
SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */ |
61 |
SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */ |
62 |
SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */ |
63 |
SD_WP_ERASE_SKIP = (23), /*!< only partial address space was erased */ |
64 |
SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */ |
65 |
SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */ |
66 |
SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */ |
67 |
SD_INVALID_VOLTRANGE = (27),
|
68 |
SD_ADDR_OUT_OF_RANGE = (28),
|
69 |
SD_SWITCH_ERROR = (29),
|
70 |
SD_SDIO_DISABLED = (30),
|
71 |
SD_SDIO_FUNCTION_BUSY = (31),
|
72 |
SD_SDIO_FUNCTION_FAILED = (32),
|
73 |
SD_SDIO_UNKNOWN_FUNCTION = (33),
|
74 |
|
75 |
/**
|
76 |
* @brief Standard error defines
|
77 |
*/
|
78 |
SD_INTERNAL_ERROR, |
79 |
SD_NOT_CONFIGURED, |
80 |
SD_REQUEST_PENDING, |
81 |
SD_REQUEST_NOT_APPLICABLE, |
82 |
SD_INVALID_PARAMETER, |
83 |
SD_UNSUPPORTED_FEATURE, |
84 |
SD_UNSUPPORTED_HW, |
85 |
SD_ERROR, |
86 |
SD_OK = 0
|
87 |
} SD_Error; |
88 |
|
89 |
/**
|
90 |
* @brief SDIO Transfer state
|
91 |
*/
|
92 |
typedef enum |
93 |
{ |
94 |
SD_TRANSFER_OK = 0,
|
95 |
SD_TRANSFER_BUSY = 1,
|
96 |
SD_TRANSFER_ERROR |
97 |
} SDTransferState; |
98 |
|
99 |
/**
|
100 |
* @brief SD Card States
|
101 |
*/
|
102 |
typedef enum |
103 |
{ |
104 |
SD_CARD_READY = ((uint32_t)0x00000001),
|
105 |
SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002),
|
106 |
SD_CARD_STANDBY = ((uint32_t)0x00000003),
|
107 |
SD_CARD_TRANSFER = ((uint32_t)0x00000004),
|
108 |
SD_CARD_SENDING = ((uint32_t)0x00000005),
|
109 |
SD_CARD_RECEIVING = ((uint32_t)0x00000006),
|
110 |
SD_CARD_PROGRAMMING = ((uint32_t)0x00000007),
|
111 |
SD_CARD_DISCONNECTED = ((uint32_t)0x00000008),
|
112 |
SD_CARD_ERROR = ((uint32_t)0x000000FF)
|
113 |
}SDCardState; |
114 |
|
115 |
|
116 |
/**
|
117 |
* @brief Card Specific Data: CSD Register
|
118 |
*/
|
119 |
typedef struct |
120 |
{ |
121 |
__IO uint8_t CSDStruct; /*!< CSD structure */
|
122 |
__IO uint8_t SysSpecVersion; /*!< System specification version */
|
123 |
__IO uint8_t Reserved1; /*!< Reserved */
|
124 |
__IO uint8_t TAAC; /*!< Data read access-time 1 */
|
125 |
__IO uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */
|
126 |
__IO uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */
|
127 |
__IO uint16_t CardComdClasses; /*!< Card command classes */
|
128 |
__IO uint8_t RdBlockLen; /*!< Max. read data block length */
|
129 |
__IO uint8_t PartBlockRead; /*!< Partial blocks for read allowed */
|
130 |
__IO uint8_t WrBlockMisalign; /*!< Write block misalignment */
|
131 |
__IO uint8_t RdBlockMisalign; /*!< Read block misalignment */
|
132 |
__IO uint8_t DSRImpl; /*!< DSR implemented */
|
133 |
__IO uint8_t Reserved2; /*!< Reserved */
|
134 |
__IO uint32_t DeviceSize; /*!< Device Size */
|
135 |
__IO uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */
|
136 |
__IO uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */
|
137 |
__IO uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */
|
138 |
__IO uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */
|
139 |
__IO uint8_t DeviceSizeMul; /*!< Device size multiplier */
|
140 |
__IO uint8_t EraseGrSize; /*!< Erase group size */
|
141 |
__IO uint8_t EraseGrMul; /*!< Erase group size multiplier */
|
142 |
__IO uint8_t WrProtectGrSize; /*!< Write protect group size */
|
143 |
__IO uint8_t WrProtectGrEnable; /*!< Write protect group enable */
|
144 |
__IO uint8_t ManDeflECC; /*!< Manufacturer default ECC */
|
145 |
__IO uint8_t WrSpeedFact; /*!< Write speed factor */
|
146 |
__IO uint8_t MaxWrBlockLen; /*!< Max. write data block length */
|
147 |
__IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */
|
148 |
__IO uint8_t Reserved3; /*!< Reserded */
|
149 |
__IO uint8_t ContentProtectAppli; /*!< Content protection application */
|
150 |
__IO uint8_t FileFormatGrouop; /*!< File format group */
|
151 |
__IO uint8_t CopyFlag; /*!< Copy flag (OTP) */
|
152 |
__IO uint8_t PermWrProtect; /*!< Permanent write protection */
|
153 |
__IO uint8_t TempWrProtect; /*!< Temporary write protection */
|
154 |
__IO uint8_t FileFormat; /*!< File Format */
|
155 |
__IO uint8_t ECC; /*!< ECC code */
|
156 |
__IO uint8_t CSD_CRC; /*!< CSD CRC */
|
157 |
__IO uint8_t Reserved4; /*!< always 1*/
|
158 |
} SD_CSD; |
159 |
|
160 |
/**
|
161 |
* @brief Card Identification Data: CID Register
|
162 |
*/
|
163 |
typedef struct |
164 |
{ |
165 |
__IO uint8_t ManufacturerID; /*!< ManufacturerID */
|
166 |
__IO uint16_t OEM_AppliID; /*!< OEM/Application ID */
|
167 |
__IO uint32_t ProdName1; /*!< Product Name part1 */
|
168 |
__IO uint8_t ProdName2; /*!< Product Name part2*/
|
169 |
__IO uint8_t ProdRev; /*!< Product Revision */
|
170 |
__IO uint32_t ProdSN; /*!< Product Serial Number */
|
171 |
__IO uint8_t Reserved1; /*!< Reserved1 */
|
172 |
__IO uint16_t ManufactDate; /*!< Manufacturing Date */
|
173 |
__IO uint8_t CID_CRC; /*!< CID CRC */
|
174 |
__IO uint8_t Reserved2; /*!< always 1 */
|
175 |
} SD_CID; |
176 |
|
177 |
/**
|
178 |
* @brief SD Card Status
|
179 |
*/
|
180 |
typedef struct |
181 |
{ |
182 |
__IO uint8_t DAT_BUS_WIDTH; |
183 |
__IO uint8_t SECURED_MODE; |
184 |
__IO uint16_t SD_CARD_TYPE; |
185 |
__IO uint32_t SIZE_OF_PROTECTED_AREA; |
186 |
__IO uint8_t SPEED_CLASS; |
187 |
__IO uint8_t PERFORMANCE_MOVE; |
188 |
__IO uint8_t AU_SIZE; |
189 |
__IO uint16_t ERASE_SIZE; |
190 |
__IO uint8_t ERASE_TIMEOUT; |
191 |
__IO uint8_t ERASE_OFFSET; |
192 |
} SD_CardStatus; |
193 |
|
194 |
|
195 |
/**
|
196 |
* @brief SD Card information
|
197 |
*/
|
198 |
typedef struct |
199 |
{ |
200 |
SD_CSD SD_csd; |
201 |
SD_CID SD_cid; |
202 |
uint64_t CardCapacity; /*!< Card Capacity */
|
203 |
uint32_t CardBlockSize; /*!< Card Block Size */
|
204 |
uint16_t RCA; |
205 |
uint8_t CardType; |
206 |
} SD_CardInfo; |
207 |
|
208 |
|
209 |
/****************************************************************************************
|
210 |
* Defines
|
211 |
****************************************************************************************/
|
212 |
/**
|
213 |
* @brief SDIO Commands Index
|
214 |
*/
|
215 |
#define SD_CMD_GO_IDLE_STATE ((uint8_t)0) |
216 |
#define SD_CMD_SEND_OP_COND ((uint8_t)1) |
217 |
#define SD_CMD_ALL_SEND_CID ((uint8_t)2) |
218 |
#define SD_CMD_SET_REL_ADDR ((uint8_t)3) /*!< SDIO_SEND_REL_ADDR for SD Card */ |
219 |
#define SD_CMD_SET_DSR ((uint8_t)4) |
220 |
#define SD_CMD_SDIO_SEN_OP_COND ((uint8_t)5) |
221 |
#define SD_CMD_HS_SWITCH ((uint8_t)6) |
222 |
#define SD_CMD_SEL_DESEL_CARD ((uint8_t)7) |
223 |
#define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8) |
224 |
#define SD_CMD_SEND_CSD ((uint8_t)9) |
225 |
#define SD_CMD_SEND_CID ((uint8_t)10) |
226 |
#define SD_CMD_READ_DAT_UNTIL_STOP ((uint8_t)11) /*!< SD Card doesn't support it */ |
227 |
#define SD_CMD_STOP_TRANSMISSION ((uint8_t)12) |
228 |
#define SD_CMD_SEND_STATUS ((uint8_t)13) |
229 |
#define SD_CMD_HS_BUSTEST_READ ((uint8_t)14) |
230 |
#define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15) |
231 |
#define SD_CMD_SET_BLOCKLEN ((uint8_t)16) |
232 |
#define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17) |
233 |
#define SD_CMD_READ_MULT_BLOCK ((uint8_t)18) |
234 |
#define SD_CMD_HS_BUSTEST_WRITE ((uint8_t)19) |
235 |
#define SD_CMD_WRITE_DAT_UNTIL_STOP ((uint8_t)20) /*!< SD Card doesn't support it */ |
236 |
#define SD_CMD_SET_BLOCK_COUNT ((uint8_t)23) /*!< SD Card doesn't support it */ |
237 |
#define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24) |
238 |
#define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25) |
239 |
#define SD_CMD_PROG_CID ((uint8_t)26) /*!< reserved for manufacturers */ |
240 |
#define SD_CMD_PROG_CSD ((uint8_t)27) |
241 |
#define SD_CMD_SET_WRITE_PROT ((uint8_t)28) |
242 |
#define SD_CMD_CLR_WRITE_PROT ((uint8_t)29) |
243 |
#define SD_CMD_SEND_WRITE_PROT ((uint8_t)30) |
244 |
#define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32) /*!< To set the address of the first write |
245 |
block to be erased. (For SD card only) */
|
246 |
#define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33) /*!< To set the address of the last write block of the |
247 |
continuous range to be erased. (For SD card only) */
|
248 |
#define SD_CMD_ERASE_GRP_START ((uint8_t)35) /*!< To set the address of the first write block to be erased. |
249 |
(For MMC card only spec 3.31) */
|
250 |
|
251 |
#define SD_CMD_ERASE_GRP_END ((uint8_t)36) /*!< To set the address of the last write block of the |
252 |
continuous range to be erased. (For MMC card only spec 3.31) */
|
253 |
|
254 |
#define SD_CMD_ERASE ((uint8_t)38) |
255 |
#define SD_CMD_FAST_IO ((uint8_t)39) /*!< SD Card doesn't support it */ |
256 |
#define SD_CMD_GO_IRQ_STATE ((uint8_t)40) /*!< SD Card doesn't support it */ |
257 |
#define SD_CMD_LOCK_UNLOCK ((uint8_t)42) |
258 |
#define SD_CMD_APP_CMD ((uint8_t)55) |
259 |
#define SD_CMD_GEN_CMD ((uint8_t)56) |
260 |
#define SD_CMD_NO_CMD ((uint8_t)64) |
261 |
|
262 |
/**
|
263 |
* @brief Following commands are SD Card Specific commands.
|
264 |
* SDIO_APP_CMD should be sent before sending these commands.
|
265 |
*/
|
266 |
#define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6) /*!< For SD Card only */ |
267 |
#define SD_CMD_SD_APP_STAUS ((uint8_t)13) /*!< For SD Card only */ |
268 |
#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22) /*!< For SD Card only */ |
269 |
#define SD_CMD_SD_APP_OP_COND ((uint8_t)41) /*!< For SD Card only */ |
270 |
#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /*!< For SD Card only */ |
271 |
#define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51) /*!< For SD Card only */ |
272 |
#define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52) /*!< For SD I/O Card only */ |
273 |
#define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53) /*!< For SD I/O Card only */ |
274 |
|
275 |
/**
|
276 |
* @brief Following commands are SD Card Specific security commands.
|
277 |
* SDIO_APP_CMD should be sent before sending these commands.
|
278 |
*/
|
279 |
#define SD_CMD_SD_APP_GET_MKB ((uint8_t)43) /*!< For SD Card only */ |
280 |
#define SD_CMD_SD_APP_GET_MID ((uint8_t)44) /*!< For SD Card only */ |
281 |
#define SD_CMD_SD_APP_SET_CER_RN1 ((uint8_t)45) /*!< For SD Card only */ |
282 |
#define SD_CMD_SD_APP_GET_CER_RN2 ((uint8_t)46) /*!< For SD Card only */ |
283 |
#define SD_CMD_SD_APP_SET_CER_RES2 ((uint8_t)47) /*!< For SD Card only */ |
284 |
#define SD_CMD_SD_APP_GET_CER_RES1 ((uint8_t)48) /*!< For SD Card only */ |
285 |
#define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18) /*!< For SD Card only */ |
286 |
#define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /*!< For SD Card only */ |
287 |
#define SD_CMD_SD_APP_SECURE_ERASE ((uint8_t)38) /*!< For SD Card only */ |
288 |
#define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49) /*!< For SD Card only */ |
289 |
#define SD_CMD_SD_APP_SECURE_WRITE_MKB ((uint8_t)48) /*!< For SD Card only */ |
290 |
|
291 |
/* Uncomment the following line to select the SDIO Data transfer mode */
|
292 |
#define SD_POLLING_MODE ((uint32_t)0x00000002) |
293 |
|
294 |
#if defined (SD_POLLING_MODE)
|
295 |
/* do not use DMA mode if configured for polling */
|
296 |
#if defined (SD_DMA_MODE)
|
297 |
#undef SD_DMA_MODE
|
298 |
#endif
|
299 |
#else
|
300 |
/* always use DMA mode if configured for interrupt mode */
|
301 |
#define SD_DMA_MODE ((uint32_t)0x00000000) |
302 |
#endif
|
303 |
|
304 |
/**
|
305 |
* @brief SD detection on its memory slot
|
306 |
*/
|
307 |
#define SD_PRESENT ((uint8_t)0x01) |
308 |
#define SD_NOT_PRESENT ((uint8_t)0x00) |
309 |
|
310 |
/**
|
311 |
* @brief Supported SD Memory Cards
|
312 |
*/
|
313 |
#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000) |
314 |
#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001) |
315 |
#define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002) |
316 |
#define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003) |
317 |
#define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004) |
318 |
#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005) |
319 |
#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006) |
320 |
#define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007) |
321 |
|
322 |
/**
|
323 |
* @brief SDIO Static flags, TimeOut, FIFO Address
|
324 |
*/
|
325 |
#ifndef NULL |
326 |
#define NULL ((void*)0) |
327 |
#endif
|
328 |
|
329 |
#define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF) |
330 |
#define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000) |
331 |
|
332 |
/**
|
333 |
* @brief Mask for errors Card Status R1 (OCR Register)
|
334 |
*/
|
335 |
#define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000) |
336 |
#define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000) |
337 |
#define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000) |
338 |
#define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000) |
339 |
#define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000) |
340 |
#define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000) |
341 |
#define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000) |
342 |
#define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000) |
343 |
#define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000) |
344 |
#define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000) |
345 |
#define SD_OCR_CC_ERROR ((uint32_t)0x00100000) |
346 |
#define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000) |
347 |
#define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000) |
348 |
#define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000) |
349 |
#define SD_OCR_CID_CSD_OVERWRIETE ((uint32_t)0x00010000) |
350 |
#define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000) |
351 |
#define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000) |
352 |
#define SD_OCR_ERASE_RESET ((uint32_t)0x00002000) |
353 |
#define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008) |
354 |
#define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008) |
355 |
|
356 |
/**
|
357 |
* @brief Masks for R6 Response
|
358 |
*/
|
359 |
#define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000) |
360 |
#define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000) |
361 |
#define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000) |
362 |
|
363 |
#define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000) |
364 |
#define SD_HIGH_CAPACITY ((uint32_t)0x40000000) |
365 |
#define SD_STD_CAPACITY ((uint32_t)0x00000000) |
366 |
#define SD_CHECK_PATTERN ((uint32_t)0x000001AA) |
367 |
|
368 |
#define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF) |
369 |
#define SD_ALLZERO ((uint32_t)0x00000000) |
370 |
|
371 |
#define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000) |
372 |
#define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000) |
373 |
#define SD_CARD_LOCKED ((uint32_t)0x02000000) |
374 |
|
375 |
|
376 |
#define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF) |
377 |
#define SD_0TO7BITS ((uint32_t)0x000000FF) |
378 |
#define SD_8TO15BITS ((uint32_t)0x0000FF00) |
379 |
#define SD_16TO23BITS ((uint32_t)0x00FF0000) |
380 |
#define SD_24TO31BITS ((uint32_t)0xFF000000) |
381 |
#define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF) |
382 |
|
383 |
#define SD_HALFFIFO ((uint32_t)0x00000008) |
384 |
#define SD_HALFFIFOBYTES ((uint32_t)0x00000020) |
385 |
|
386 |
/**
|
387 |
* @brief Command Class Supported
|
388 |
*/
|
389 |
#define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080) |
390 |
#define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040) |
391 |
#define SD_CCCC_ERASE ((uint32_t)0x00000020) |
392 |
|
393 |
/**
|
394 |
* @brief Following commands are SD Card Specific commands.
|
395 |
* SDIO_APP_CMD should be sent before sending these commands.
|
396 |
*/
|
397 |
#define SDIO_SEND_IF_COND ((uint32_t)0x00000008) |
398 |
|
399 |
/**
|
400 |
* @brief SD FLASH SDIO Interface
|
401 |
*/
|
402 |
#define SD_DETECT_PIN GPIO_Pin_11 /* PC.11 */ |
403 |
#define SD_DETECT_GPIO_PORT GPIOC /* GPIOC */ |
404 |
#define SD_DETECT_GPIO_CLK RCC_AHB1Periph_GPIOC
|
405 |
|
406 |
#define SDIO_FIFO_ADDRESS ((uint32_t)0x40012C80) |
407 |
/**
|
408 |
* @brief SDIO Intialization Frequency (400KHz max)
|
409 |
*/
|
410 |
#define SDIO_INIT_CLK_DIV ((uint8_t)0x76) |
411 |
/**
|
412 |
* @brief SDIO Data Transfer Frequency (25MHz max)
|
413 |
*/
|
414 |
#define SDIO_TRANSFER_CLK_DIV ((uint8_t)0x0) |
415 |
|
416 |
#define SD_SDIO_DMA DMA2
|
417 |
#define SD_SDIO_DMA_CLK RCC_AHB1Periph_DMA2
|
418 |
|
419 |
#define SD_SDIO_DMA_STREAM DMA2_Stream3
|
420 |
#define SD_SDIO_DMA_CHANNEL DMA_Channel_4
|
421 |
#define SD_SDIO_DMA_FLAG_FEIF DMA_FLAG_FEIF3
|
422 |
#define SD_SDIO_DMA_FLAG_DMEIF DMA_FLAG_DMEIF3
|
423 |
#define SD_SDIO_DMA_FLAG_TEIF DMA_FLAG_TEIF3
|
424 |
#define SD_SDIO_DMA_FLAG_HTIF DMA_FLAG_HTIF3
|
425 |
#define SD_SDIO_DMA_FLAG_TCIF DMA_FLAG_TCIF3
|
426 |
#define SD_SDIO_DMA_IRQn DMA2_Stream3_IRQn
|
427 |
#define SD_SDIO_DMA_IRQHANDLER DMA2_Stream3_IRQHandler
|
428 |
|
429 |
#define SD_BLOCKSIZE (512) |
430 |
|
431 |
|
432 |
/****************************************************************************************
|
433 |
* Local data declarations
|
434 |
****************************************************************************************/
|
435 |
static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1;
|
436 |
static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0; |
437 |
static uint8_t SDSTATUS_Tab[64]; |
438 |
static __IO uint32_t StopCondition = 0; |
439 |
static __IO SD_Error TransferError = SD_OK;
|
440 |
static __IO uint32_t TransferEnd = 0, DMAEndOfTransfer = 1; |
441 |
static SD_CardInfo SDCardInfo;
|
442 |
static SDIO_InitTypeDef SDIO_InitStructure;
|
443 |
static SDIO_CmdInitTypeDef SDIO_CmdInitStructure;
|
444 |
static SDIO_DataInitTypeDef SDIO_DataInitStructure;
|
445 |
static volatile DSTATUS Stat = STA_NOINIT; /* Disk status */ |
446 |
|
447 |
|
448 |
/****************************************************************************************
|
449 |
* Function prototypes
|
450 |
****************************************************************************************/
|
451 |
void SD_DeInit(void); |
452 |
SD_Error SD_Init(void);
|
453 |
SDTransferState SD_GetStatus(void);
|
454 |
SDCardState SD_GetState(void);
|
455 |
uint8_t SD_Detect(void);
|
456 |
SD_Error SD_PowerON(void);
|
457 |
SD_Error SD_PowerOFF(void);
|
458 |
SD_Error SD_InitializeCards(void);
|
459 |
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo); |
460 |
SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus); |
461 |
SD_Error SD_EnableWideBusOperation(uint32_t WideMode); |
462 |
SD_Error SD_SelectDeselect(uint64_t addr); |
463 |
SD_Error SD_ReadBlock(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize); |
464 |
SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks); |
465 |
SD_Error SD_WriteBlock(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize); |
466 |
SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks); |
467 |
SDTransferState SD_GetTransferState(void);
|
468 |
SD_Error SD_StopTransfer(void);
|
469 |
SD_Error SD_Erase(uint64_t startaddr, uint64_t endaddr); |
470 |
SD_Error SD_SendStatus(uint32_t *pcardstatus); |
471 |
SD_Error SD_SendSDStatus(uint32_t *psdstatus); |
472 |
SD_Error SD_ProcessIRQSrc(void);
|
473 |
void SD_ProcessDMAIRQ(void); |
474 |
SD_Error SD_WaitReadOperation(void);
|
475 |
SD_Error SD_WaitWriteOperation(void);
|
476 |
static SD_Error CmdError(void); |
477 |
static SD_Error CmdResp1Error(uint8_t cmd);
|
478 |
static SD_Error CmdResp7Error(void); |
479 |
static SD_Error CmdResp3Error(void); |
480 |
static SD_Error CmdResp2Error(void); |
481 |
static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca);
|
482 |
static SD_Error SDEnWideBus(FunctionalState NewState);
|
483 |
static SD_Error IsCardProgramming(uint8_t *pstatus);
|
484 |
static SD_Error FindSCR(uint16_t rca, uint32_t *pscr);
|
485 |
static void SD_LowLevel_DeInit(void); |
486 |
static void SD_LowLevel_Init(void); |
487 |
static void SD_LowLevel_DMA_TxConfig(uint32_t *BufferSRC, uint32_t BufferSize); |
488 |
static void SD_LowLevel_DMA_RxConfig(uint32_t *BufferDST, uint32_t BufferSize); |
489 |
|
490 |
|
491 |
/**
|
492 |
* @brief DeInitializes the SDIO interface.
|
493 |
* @param None
|
494 |
* @retval None
|
495 |
*/
|
496 |
void SD_DeInit(void) |
497 |
{ |
498 |
SD_LowLevel_DeInit(); |
499 |
} |
500 |
|
501 |
/**
|
502 |
* @brief Initializes the SD Card and put it into StandBy State (Ready for data
|
503 |
* transfer).
|
504 |
* @param None
|
505 |
* @retval SD_Error: SD Card Error code.
|
506 |
*/
|
507 |
SD_Error SD_Init(void)
|
508 |
{ |
509 |
__IO SD_Error errorstatus = SD_OK; |
510 |
|
511 |
/* SDIO Peripheral Low Level Init */
|
512 |
SD_LowLevel_Init(); |
513 |
|
514 |
SDIO_DeInit(); |
515 |
|
516 |
errorstatus = SD_PowerON(); |
517 |
|
518 |
if (errorstatus != SD_OK)
|
519 |
{ |
520 |
/*!< CMD Response TimeOut (wait for CMDSENT flag) */
|
521 |
return(errorstatus);
|
522 |
} |
523 |
|
524 |
errorstatus = SD_InitializeCards(); |
525 |
|
526 |
if (errorstatus != SD_OK)
|
527 |
{ |
528 |
/*!< CMD Response TimeOut (wait for CMDSENT flag) */
|
529 |
return(errorstatus);
|
530 |
} |
531 |
|
532 |
/*!< Configure the SDIO peripheral */
|
533 |
/*!< SDIO_CK = SDIOCLK / (SDIO_TRANSFER_CLK_DIV + 2) */
|
534 |
/*!< on STM32F4xx devices, SDIOCLK is fixed to 48MHz */
|
535 |
SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; |
536 |
SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising; |
537 |
SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; |
538 |
SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; |
539 |
SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; |
540 |
SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; |
541 |
SDIO_Init(&SDIO_InitStructure); |
542 |
|
543 |
/*----------------- Read CSD/CID MSD registers ------------------*/
|
544 |
errorstatus = SD_GetCardInfo(&SDCardInfo); |
545 |
|
546 |
if (errorstatus == SD_OK)
|
547 |
{ |
548 |
/*----------------- Select Card --------------------------------*/
|
549 |
errorstatus = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16));
|
550 |
} |
551 |
|
552 |
if (errorstatus == SD_OK)
|
553 |
{ |
554 |
errorstatus = SD_EnableWideBusOperation(SDIO_BusWide_4b); |
555 |
} |
556 |
|
557 |
return(errorstatus);
|
558 |
} |
559 |
|
560 |
/**
|
561 |
* @brief Gets the cuurent sd card data transfer status.
|
562 |
* @param None
|
563 |
* @retval SDTransferState: Data Transfer state.
|
564 |
* This value can be:
|
565 |
* - SD_TRANSFER_OK: No data transfer is acting
|
566 |
* - SD_TRANSFER_BUSY: Data transfer is acting
|
567 |
*/
|
568 |
SDTransferState SD_GetStatus(void)
|
569 |
{ |
570 |
SDCardState cardstate = SD_CARD_TRANSFER; |
571 |
|
572 |
cardstate = SD_GetState(); |
573 |
|
574 |
if (cardstate == SD_CARD_TRANSFER)
|
575 |
{ |
576 |
return(SD_TRANSFER_OK);
|
577 |
} |
578 |
else if(cardstate == SD_CARD_ERROR) |
579 |
{ |
580 |
return (SD_TRANSFER_ERROR);
|
581 |
} |
582 |
else
|
583 |
{ |
584 |
return(SD_TRANSFER_BUSY);
|
585 |
} |
586 |
} |
587 |
|
588 |
/**
|
589 |
* @brief Returns the current card's state.
|
590 |
* @param None
|
591 |
* @retval SDCardState: SD Card Error or SD Card Current State.
|
592 |
*/
|
593 |
SDCardState SD_GetState(void)
|
594 |
{ |
595 |
uint32_t resp1 = 0;
|
596 |
|
597 |
if(SD_Detect()== SD_PRESENT)
|
598 |
{ |
599 |
if (SD_SendStatus(&resp1) != SD_OK)
|
600 |
{ |
601 |
return SD_CARD_ERROR;
|
602 |
} |
603 |
else
|
604 |
{ |
605 |
return (SDCardState)((resp1 >> 9) & 0x0F); |
606 |
} |
607 |
} |
608 |
else
|
609 |
{ |
610 |
return SD_CARD_ERROR;
|
611 |
} |
612 |
} |
613 |
|
614 |
/**
|
615 |
* @brief Detect if SD card is correctly plugged in the memory slot.
|
616 |
* @param None
|
617 |
* @retval Return if SD is detected or not
|
618 |
*/
|
619 |
uint8_t SD_Detect(void)
|
620 |
{ |
621 |
__IO uint8_t status = SD_PRESENT; |
622 |
|
623 |
/*!< Check GPIO to detect SD */
|
624 |
if (GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) == Bit_RESET)
|
625 |
{ |
626 |
status = SD_NOT_PRESENT; |
627 |
} |
628 |
return status;
|
629 |
} |
630 |
|
631 |
/**
|
632 |
* @brief Enquires cards about their operating voltage and configures
|
633 |
* clock controls.
|
634 |
* @param None
|
635 |
* @retval SD_Error: SD Card Error code.
|
636 |
*/
|
637 |
SD_Error SD_PowerON(void)
|
638 |
{ |
639 |
__IO SD_Error errorstatus = SD_OK; |
640 |
uint32_t response = 0, count = 0, validvoltage = 0; |
641 |
uint32_t SDType = SD_STD_CAPACITY; |
642 |
|
643 |
/*!< Power ON Sequence -----------------------------------------------------*/
|
644 |
/*!< Configure the SDIO peripheral */
|
645 |
/*!< SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) */
|
646 |
/*!< on STM32F4xx devices, SDIOCLK is fixed to 48MHz */
|
647 |
/*!< SDIO_CK for initialization should not exceed 400 KHz */
|
648 |
SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV; |
649 |
SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising; |
650 |
SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; |
651 |
SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; |
652 |
SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; |
653 |
SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; |
654 |
SDIO_Init(&SDIO_InitStructure); |
655 |
|
656 |
/*!< Set Power State to ON */
|
657 |
SDIO_SetPowerState(SDIO_PowerState_ON); |
658 |
|
659 |
/*!< Enable SDIO Clock */
|
660 |
SDIO_ClockCmd(ENABLE); |
661 |
|
662 |
/*!< CMD0: GO_IDLE_STATE ---------------------------------------------------*/
|
663 |
/*!< No CMD response required */
|
664 |
SDIO_CmdInitStructure.SDIO_Argument = 0x0;
|
665 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE; |
666 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No; |
667 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
668 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
669 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
670 |
|
671 |
errorstatus = CmdError(); |
672 |
|
673 |
if (errorstatus != SD_OK)
|
674 |
{ |
675 |
/*!< CMD Response TimeOut (wait for CMDSENT flag) */
|
676 |
return(errorstatus);
|
677 |
} |
678 |
|
679 |
/*!< CMD8: SEND_IF_COND ----------------------------------------------------*/
|
680 |
/*!< Send CMD8 to verify SD card interface operating condition */
|
681 |
/*!< Argument: - [31:12]: Reserved (shall be set to '0')
|
682 |
- [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
|
683 |
- [7:0]: Check Pattern (recommended 0xAA) */
|
684 |
/*!< CMD Response: R7 */
|
685 |
SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN; |
686 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND; |
687 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
688 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
689 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
690 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
691 |
|
692 |
errorstatus = CmdResp7Error(); |
693 |
|
694 |
if (errorstatus == SD_OK)
|
695 |
{ |
696 |
CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0; /*!< SD Card 2.0 */
|
697 |
SDType = SD_HIGH_CAPACITY; |
698 |
} |
699 |
else
|
700 |
{ |
701 |
/*!< CMD55 */
|
702 |
SDIO_CmdInitStructure.SDIO_Argument = 0x00;
|
703 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; |
704 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
705 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
706 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
707 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
708 |
errorstatus = CmdResp1Error(SD_CMD_APP_CMD); |
709 |
} |
710 |
/*!< CMD55 */
|
711 |
SDIO_CmdInitStructure.SDIO_Argument = 0x00;
|
712 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; |
713 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
714 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
715 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
716 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
717 |
errorstatus = CmdResp1Error(SD_CMD_APP_CMD); |
718 |
|
719 |
/*!< If errorstatus is Command TimeOut, it is a MMC card */
|
720 |
/*!< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch)
|
721 |
or SD card 1.x */
|
722 |
if (errorstatus == SD_OK)
|
723 |
{ |
724 |
/*!< SD CARD */
|
725 |
/*!< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
|
726 |
while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL))
|
727 |
{ |
728 |
|
729 |
/*!< SEND CMD55 APP_CMD with RCA as 0 */
|
730 |
SDIO_CmdInitStructure.SDIO_Argument = 0x00;
|
731 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; |
732 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
733 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
734 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
735 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
736 |
|
737 |
errorstatus = CmdResp1Error(SD_CMD_APP_CMD); |
738 |
|
739 |
if (errorstatus != SD_OK)
|
740 |
{ |
741 |
return(errorstatus);
|
742 |
} |
743 |
SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType; |
744 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND; |
745 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
746 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
747 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
748 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
749 |
|
750 |
errorstatus = CmdResp3Error(); |
751 |
if (errorstatus != SD_OK)
|
752 |
{ |
753 |
return(errorstatus);
|
754 |
} |
755 |
|
756 |
response = SDIO_GetResponse(SDIO_RESP1); |
757 |
validvoltage = (((response >> 31) == 1) ? 1 : 0); |
758 |
count++; |
759 |
} |
760 |
if (count >= SD_MAX_VOLT_TRIAL)
|
761 |
{ |
762 |
errorstatus = SD_INVALID_VOLTRANGE; |
763 |
return(errorstatus);
|
764 |
} |
765 |
|
766 |
if (response &= SD_HIGH_CAPACITY)
|
767 |
{ |
768 |
CardType = SDIO_HIGH_CAPACITY_SD_CARD; |
769 |
} |
770 |
|
771 |
}/*!< else MMC Card */
|
772 |
|
773 |
return(errorstatus);
|
774 |
} |
775 |
|
776 |
/**
|
777 |
* @brief Turns the SDIO output signals off.
|
778 |
* @param None
|
779 |
* @retval SD_Error: SD Card Error code.
|
780 |
*/
|
781 |
SD_Error SD_PowerOFF(void)
|
782 |
{ |
783 |
SD_Error errorstatus = SD_OK; |
784 |
|
785 |
/*!< Set Power State to OFF */
|
786 |
SDIO_SetPowerState(SDIO_PowerState_OFF); |
787 |
|
788 |
return(errorstatus);
|
789 |
} |
790 |
|
791 |
/**
|
792 |
* @brief Intialises all cards or single card as the case may be Card(s) come
|
793 |
* into standby state.
|
794 |
* @param None
|
795 |
* @retval SD_Error: SD Card Error code.
|
796 |
*/
|
797 |
SD_Error SD_InitializeCards(void)
|
798 |
{ |
799 |
SD_Error errorstatus = SD_OK; |
800 |
uint16_t rca = 0x01;
|
801 |
|
802 |
if (SDIO_GetPowerState() == SDIO_PowerState_OFF)
|
803 |
{ |
804 |
errorstatus = SD_REQUEST_NOT_APPLICABLE; |
805 |
return(errorstatus);
|
806 |
} |
807 |
|
808 |
if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
|
809 |
{ |
810 |
/*!< Send CMD2 ALL_SEND_CID */
|
811 |
SDIO_CmdInitStructure.SDIO_Argument = 0x0;
|
812 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID; |
813 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long; |
814 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
815 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
816 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
817 |
|
818 |
errorstatus = CmdResp2Error(); |
819 |
|
820 |
if (SD_OK != errorstatus)
|
821 |
{ |
822 |
return(errorstatus);
|
823 |
} |
824 |
|
825 |
CID_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
|
826 |
CID_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
|
827 |
CID_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
|
828 |
CID_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
|
829 |
} |
830 |
if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_SECURE_DIGITAL_IO_COMBO_CARD == CardType)
|
831 |
|| (SDIO_HIGH_CAPACITY_SD_CARD == CardType)) |
832 |
{ |
833 |
/*!< Send CMD3 SET_REL_ADDR with argument 0 */
|
834 |
/*!< SD Card publishes its RCA. */
|
835 |
SDIO_CmdInitStructure.SDIO_Argument = 0x00;
|
836 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR; |
837 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
838 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
839 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
840 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
841 |
|
842 |
errorstatus = CmdResp6Error(SD_CMD_SET_REL_ADDR, &rca); |
843 |
|
844 |
if (SD_OK != errorstatus)
|
845 |
{ |
846 |
return(errorstatus);
|
847 |
} |
848 |
} |
849 |
|
850 |
if (SDIO_SECURE_DIGITAL_IO_CARD != CardType)
|
851 |
{ |
852 |
RCA = rca; |
853 |
|
854 |
/*!< Send CMD9 SEND_CSD with argument as card's RCA */
|
855 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16);
|
856 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD; |
857 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long; |
858 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
859 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
860 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
861 |
|
862 |
errorstatus = CmdResp2Error(); |
863 |
|
864 |
if (SD_OK != errorstatus)
|
865 |
{ |
866 |
return(errorstatus);
|
867 |
} |
868 |
|
869 |
CSD_Tab[0] = SDIO_GetResponse(SDIO_RESP1);
|
870 |
CSD_Tab[1] = SDIO_GetResponse(SDIO_RESP2);
|
871 |
CSD_Tab[2] = SDIO_GetResponse(SDIO_RESP3);
|
872 |
CSD_Tab[3] = SDIO_GetResponse(SDIO_RESP4);
|
873 |
} |
874 |
|
875 |
errorstatus = SD_OK; /*!< All cards get intialized */
|
876 |
|
877 |
return(errorstatus);
|
878 |
} |
879 |
|
880 |
/**
|
881 |
* @brief Returns information about specific card.
|
882 |
* @param cardinfo: pointer to a SD_CardInfo structure that contains all SD card
|
883 |
* information.
|
884 |
* @retval SD_Error: SD Card Error code.
|
885 |
*/
|
886 |
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo) |
887 |
{ |
888 |
SD_Error errorstatus = SD_OK; |
889 |
uint8_t tmp = 0;
|
890 |
|
891 |
cardinfo->CardType = (uint8_t)CardType; |
892 |
cardinfo->RCA = (uint16_t)RCA; |
893 |
|
894 |
/*!< Byte 0 */
|
895 |
tmp = (uint8_t)((CSD_Tab[0] & 0xFF000000) >> 24); |
896 |
cardinfo->SD_csd.CSDStruct = (tmp & 0xC0) >> 6; |
897 |
cardinfo->SD_csd.SysSpecVersion = (tmp & 0x3C) >> 2; |
898 |
cardinfo->SD_csd.Reserved1 = tmp & 0x03;
|
899 |
|
900 |
/*!< Byte 1 */
|
901 |
tmp = (uint8_t)((CSD_Tab[0] & 0x00FF0000) >> 16); |
902 |
cardinfo->SD_csd.TAAC = tmp; |
903 |
|
904 |
/*!< Byte 2 */
|
905 |
tmp = (uint8_t)((CSD_Tab[0] & 0x0000FF00) >> 8); |
906 |
cardinfo->SD_csd.NSAC = tmp; |
907 |
|
908 |
/*!< Byte 3 */
|
909 |
tmp = (uint8_t)(CSD_Tab[0] & 0x000000FF); |
910 |
cardinfo->SD_csd.MaxBusClkFrec = tmp; |
911 |
|
912 |
/*!< Byte 4 */
|
913 |
tmp = (uint8_t)((CSD_Tab[1] & 0xFF000000) >> 24); |
914 |
cardinfo->SD_csd.CardComdClasses = tmp << 4;
|
915 |
|
916 |
/*!< Byte 5 */
|
917 |
tmp = (uint8_t)((CSD_Tab[1] & 0x00FF0000) >> 16); |
918 |
cardinfo->SD_csd.CardComdClasses |= (tmp & 0xF0) >> 4; |
919 |
cardinfo->SD_csd.RdBlockLen = tmp & 0x0F;
|
920 |
|
921 |
/*!< Byte 6 */
|
922 |
tmp = (uint8_t)((CSD_Tab[1] & 0x0000FF00) >> 8); |
923 |
cardinfo->SD_csd.PartBlockRead = (tmp & 0x80) >> 7; |
924 |
cardinfo->SD_csd.WrBlockMisalign = (tmp & 0x40) >> 6; |
925 |
cardinfo->SD_csd.RdBlockMisalign = (tmp & 0x20) >> 5; |
926 |
cardinfo->SD_csd.DSRImpl = (tmp & 0x10) >> 4; |
927 |
cardinfo->SD_csd.Reserved2 = 0; /*!< Reserved */ |
928 |
|
929 |
if ((CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1) || (CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0))
|
930 |
{ |
931 |
cardinfo->SD_csd.DeviceSize = (tmp & 0x03) << 10; |
932 |
|
933 |
/*!< Byte 7 */
|
934 |
tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF); |
935 |
cardinfo->SD_csd.DeviceSize |= (tmp) << 2;
|
936 |
|
937 |
/*!< Byte 8 */
|
938 |
tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24); |
939 |
cardinfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6; |
940 |
|
941 |
cardinfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3; |
942 |
cardinfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07);
|
943 |
|
944 |
/*!< Byte 9 */
|
945 |
tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16); |
946 |
cardinfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5; |
947 |
cardinfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2; |
948 |
cardinfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1; |
949 |
/*!< Byte 10 */
|
950 |
tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8); |
951 |
cardinfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7; |
952 |
|
953 |
cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ;
|
954 |
cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2)); |
955 |
cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
|
956 |
cardinfo->CardCapacity *= cardinfo->CardBlockSize; |
957 |
} |
958 |
else if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) |
959 |
{ |
960 |
/*!< Byte 7 */
|
961 |
tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF); |
962 |
cardinfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16; |
963 |
|
964 |
/*!< Byte 8 */
|
965 |
tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24); |
966 |
|
967 |
cardinfo->SD_csd.DeviceSize |= (tmp << 8);
|
968 |
|
969 |
/*!< Byte 9 */
|
970 |
tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16); |
971 |
|
972 |
cardinfo->SD_csd.DeviceSize |= (tmp); |
973 |
|
974 |
/*!< Byte 10 */
|
975 |
tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8); |
976 |
|
977 |
cardinfo->CardCapacity = ((uint64_t)cardinfo->SD_csd.DeviceSize + 1) * 512 * 1024; |
978 |
cardinfo->CardBlockSize = 512;
|
979 |
} |
980 |
|
981 |
|
982 |
cardinfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6; |
983 |
cardinfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1; |
984 |
|
985 |
/*!< Byte 11 */
|
986 |
tmp = (uint8_t)(CSD_Tab[2] & 0x000000FF); |
987 |
cardinfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7; |
988 |
cardinfo->SD_csd.WrProtectGrSize = (tmp & 0x7F);
|
989 |
|
990 |
/*!< Byte 12 */
|
991 |
tmp = (uint8_t)((CSD_Tab[3] & 0xFF000000) >> 24); |
992 |
cardinfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7; |
993 |
cardinfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5; |
994 |
cardinfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2; |
995 |
cardinfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2; |
996 |
|
997 |
/*!< Byte 13 */
|
998 |
tmp = (uint8_t)((CSD_Tab[3] & 0x00FF0000) >> 16); |
999 |
cardinfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6; |
1000 |
cardinfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5; |
1001 |
cardinfo->SD_csd.Reserved3 = 0;
|
1002 |
cardinfo->SD_csd.ContentProtectAppli = (tmp & 0x01);
|
1003 |
|
1004 |
/*!< Byte 14 */
|
1005 |
tmp = (uint8_t)((CSD_Tab[3] & 0x0000FF00) >> 8); |
1006 |
cardinfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7; |
1007 |
cardinfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6; |
1008 |
cardinfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5; |
1009 |
cardinfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4; |
1010 |
cardinfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2; |
1011 |
cardinfo->SD_csd.ECC = (tmp & 0x03);
|
1012 |
|
1013 |
/*!< Byte 15 */
|
1014 |
tmp = (uint8_t)(CSD_Tab[3] & 0x000000FF); |
1015 |
cardinfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1; |
1016 |
cardinfo->SD_csd.Reserved4 = 1;
|
1017 |
|
1018 |
|
1019 |
/*!< Byte 0 */
|
1020 |
tmp = (uint8_t)((CID_Tab[0] & 0xFF000000) >> 24); |
1021 |
cardinfo->SD_cid.ManufacturerID = tmp; |
1022 |
|
1023 |
/*!< Byte 1 */
|
1024 |
tmp = (uint8_t)((CID_Tab[0] & 0x00FF0000) >> 16); |
1025 |
cardinfo->SD_cid.OEM_AppliID = tmp << 8;
|
1026 |
|
1027 |
/*!< Byte 2 */
|
1028 |
tmp = (uint8_t)((CID_Tab[0] & 0x000000FF00) >> 8); |
1029 |
cardinfo->SD_cid.OEM_AppliID |= tmp; |
1030 |
|
1031 |
/*!< Byte 3 */
|
1032 |
tmp = (uint8_t)(CID_Tab[0] & 0x000000FF); |
1033 |
cardinfo->SD_cid.ProdName1 = tmp << 24;
|
1034 |
|
1035 |
/*!< Byte 4 */
|
1036 |
tmp = (uint8_t)((CID_Tab[1] & 0xFF000000) >> 24); |
1037 |
cardinfo->SD_cid.ProdName1 |= tmp << 16;
|
1038 |
|
1039 |
/*!< Byte 5 */
|
1040 |
tmp = (uint8_t)((CID_Tab[1] & 0x00FF0000) >> 16); |
1041 |
cardinfo->SD_cid.ProdName1 |= tmp << 8;
|
1042 |
|
1043 |
/*!< Byte 6 */
|
1044 |
tmp = (uint8_t)((CID_Tab[1] & 0x0000FF00) >> 8); |
1045 |
cardinfo->SD_cid.ProdName1 |= tmp; |
1046 |
|
1047 |
/*!< Byte 7 */
|
1048 |
tmp = (uint8_t)(CID_Tab[1] & 0x000000FF); |
1049 |
cardinfo->SD_cid.ProdName2 = tmp; |
1050 |
|
1051 |
/*!< Byte 8 */
|
1052 |
tmp = (uint8_t)((CID_Tab[2] & 0xFF000000) >> 24); |
1053 |
cardinfo->SD_cid.ProdRev = tmp; |
1054 |
|
1055 |
/*!< Byte 9 */
|
1056 |
tmp = (uint8_t)((CID_Tab[2] & 0x00FF0000) >> 16); |
1057 |
cardinfo->SD_cid.ProdSN = tmp << 24;
|
1058 |
|
1059 |
/*!< Byte 10 */
|
1060 |
tmp = (uint8_t)((CID_Tab[2] & 0x0000FF00) >> 8); |
1061 |
cardinfo->SD_cid.ProdSN |= tmp << 16;
|
1062 |
|
1063 |
/*!< Byte 11 */
|
1064 |
tmp = (uint8_t)(CID_Tab[2] & 0x000000FF); |
1065 |
cardinfo->SD_cid.ProdSN |= tmp << 8;
|
1066 |
|
1067 |
/*!< Byte 12 */
|
1068 |
tmp = (uint8_t)((CID_Tab[3] & 0xFF000000) >> 24); |
1069 |
cardinfo->SD_cid.ProdSN |= tmp; |
1070 |
|
1071 |
/*!< Byte 13 */
|
1072 |
tmp = (uint8_t)((CID_Tab[3] & 0x00FF0000) >> 16); |
1073 |
cardinfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4; |
1074 |
cardinfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8; |
1075 |
|
1076 |
/*!< Byte 14 */
|
1077 |
tmp = (uint8_t)((CID_Tab[3] & 0x0000FF00) >> 8); |
1078 |
cardinfo->SD_cid.ManufactDate |= tmp; |
1079 |
|
1080 |
/*!< Byte 15 */
|
1081 |
tmp = (uint8_t)(CID_Tab[3] & 0x000000FF); |
1082 |
cardinfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1; |
1083 |
cardinfo->SD_cid.Reserved2 = 1;
|
1084 |
|
1085 |
return(errorstatus);
|
1086 |
} |
1087 |
|
1088 |
/**
|
1089 |
* @brief Enables wide bus opeartion for the requeseted card if supported by
|
1090 |
* card.
|
1091 |
* @param WideMode: Specifies the SD card wide bus mode.
|
1092 |
* This parameter can be one of the following values:
|
1093 |
* @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
|
1094 |
* @arg SDIO_BusWide_4b: 4-bit data transfer
|
1095 |
* @arg SDIO_BusWide_1b: 1-bit data transfer
|
1096 |
* @retval SD_Error: SD Card Error code.
|
1097 |
*/
|
1098 |
SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus) |
1099 |
{ |
1100 |
SD_Error errorstatus = SD_OK; |
1101 |
uint8_t tmp = 0;
|
1102 |
|
1103 |
errorstatus = SD_SendSDStatus((uint32_t *)SDSTATUS_Tab); |
1104 |
|
1105 |
if (errorstatus != SD_OK)
|
1106 |
{ |
1107 |
return(errorstatus);
|
1108 |
} |
1109 |
|
1110 |
/*!< Byte 0 */
|
1111 |
tmp = (uint8_t)((SDSTATUS_Tab[0] & 0xC0) >> 6); |
1112 |
cardstatus->DAT_BUS_WIDTH = tmp; |
1113 |
|
1114 |
/*!< Byte 0 */
|
1115 |
tmp = (uint8_t)((SDSTATUS_Tab[0] & 0x20) >> 5); |
1116 |
cardstatus->SECURED_MODE = tmp; |
1117 |
|
1118 |
/*!< Byte 2 */
|
1119 |
tmp = (uint8_t)((SDSTATUS_Tab[2] & 0xFF)); |
1120 |
cardstatus->SD_CARD_TYPE = tmp << 8;
|
1121 |
|
1122 |
/*!< Byte 3 */
|
1123 |
tmp = (uint8_t)((SDSTATUS_Tab[3] & 0xFF)); |
1124 |
cardstatus->SD_CARD_TYPE |= tmp; |
1125 |
|
1126 |
/*!< Byte 4 */
|
1127 |
tmp = (uint8_t)(SDSTATUS_Tab[4] & 0xFF); |
1128 |
cardstatus->SIZE_OF_PROTECTED_AREA = tmp << 24;
|
1129 |
|
1130 |
/*!< Byte 5 */
|
1131 |
tmp = (uint8_t)(SDSTATUS_Tab[5] & 0xFF); |
1132 |
cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 16;
|
1133 |
|
1134 |
/*!< Byte 6 */
|
1135 |
tmp = (uint8_t)(SDSTATUS_Tab[6] & 0xFF); |
1136 |
cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 8;
|
1137 |
|
1138 |
/*!< Byte 7 */
|
1139 |
tmp = (uint8_t)(SDSTATUS_Tab[7] & 0xFF); |
1140 |
cardstatus->SIZE_OF_PROTECTED_AREA |= tmp; |
1141 |
|
1142 |
/*!< Byte 8 */
|
1143 |
tmp = (uint8_t)((SDSTATUS_Tab[8] & 0xFF)); |
1144 |
cardstatus->SPEED_CLASS = tmp; |
1145 |
|
1146 |
/*!< Byte 9 */
|
1147 |
tmp = (uint8_t)((SDSTATUS_Tab[9] & 0xFF)); |
1148 |
cardstatus->PERFORMANCE_MOVE = tmp; |
1149 |
|
1150 |
/*!< Byte 10 */
|
1151 |
tmp = (uint8_t)((SDSTATUS_Tab[10] & 0xF0) >> 4); |
1152 |
cardstatus->AU_SIZE = tmp; |
1153 |
|
1154 |
/*!< Byte 11 */
|
1155 |
tmp = (uint8_t)(SDSTATUS_Tab[11] & 0xFF); |
1156 |
cardstatus->ERASE_SIZE = tmp << 8;
|
1157 |
|
1158 |
/*!< Byte 12 */
|
1159 |
tmp = (uint8_t)(SDSTATUS_Tab[12] & 0xFF); |
1160 |
cardstatus->ERASE_SIZE |= tmp; |
1161 |
|
1162 |
/*!< Byte 13 */
|
1163 |
tmp = (uint8_t)((SDSTATUS_Tab[13] & 0xFC) >> 2); |
1164 |
cardstatus->ERASE_TIMEOUT = tmp; |
1165 |
|
1166 |
/*!< Byte 13 */
|
1167 |
tmp = (uint8_t)((SDSTATUS_Tab[13] & 0x3)); |
1168 |
cardstatus->ERASE_OFFSET = tmp; |
1169 |
|
1170 |
return(errorstatus);
|
1171 |
} |
1172 |
|
1173 |
/**
|
1174 |
* @brief Enables wide bus opeartion for the requeseted card if supported by
|
1175 |
* card.
|
1176 |
* @param WideMode: Specifies the SD card wide bus mode.
|
1177 |
* This parameter can be one of the following values:
|
1178 |
* @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC)
|
1179 |
* @arg SDIO_BusWide_4b: 4-bit data transfer
|
1180 |
* @arg SDIO_BusWide_1b: 1-bit data transfer
|
1181 |
* @retval SD_Error: SD Card Error code.
|
1182 |
*/
|
1183 |
SD_Error SD_EnableWideBusOperation(uint32_t WideMode) |
1184 |
{ |
1185 |
SD_Error errorstatus = SD_OK; |
1186 |
|
1187 |
/*!< MMC Card doesn't support this feature */
|
1188 |
if (SDIO_MULTIMEDIA_CARD == CardType)
|
1189 |
{ |
1190 |
errorstatus = SD_UNSUPPORTED_FEATURE; |
1191 |
return(errorstatus);
|
1192 |
} |
1193 |
else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType)) |
1194 |
{ |
1195 |
if (SDIO_BusWide_8b == WideMode)
|
1196 |
{ |
1197 |
errorstatus = SD_UNSUPPORTED_FEATURE; |
1198 |
return(errorstatus);
|
1199 |
} |
1200 |
else if (SDIO_BusWide_4b == WideMode) |
1201 |
{ |
1202 |
errorstatus = SDEnWideBus(ENABLE); |
1203 |
|
1204 |
if (SD_OK == errorstatus)
|
1205 |
{ |
1206 |
/*!< Configure the SDIO peripheral */
|
1207 |
SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; |
1208 |
SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising; |
1209 |
SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; |
1210 |
SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; |
1211 |
SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_4b; |
1212 |
SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; |
1213 |
SDIO_Init(&SDIO_InitStructure); |
1214 |
} |
1215 |
} |
1216 |
else
|
1217 |
{ |
1218 |
errorstatus = SDEnWideBus(DISABLE); |
1219 |
|
1220 |
if (SD_OK == errorstatus)
|
1221 |
{ |
1222 |
/*!< Configure the SDIO peripheral */
|
1223 |
SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; |
1224 |
SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising; |
1225 |
SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; |
1226 |
SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; |
1227 |
SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; |
1228 |
SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; |
1229 |
SDIO_Init(&SDIO_InitStructure); |
1230 |
} |
1231 |
} |
1232 |
} |
1233 |
|
1234 |
return(errorstatus);
|
1235 |
} |
1236 |
|
1237 |
/**
|
1238 |
* @brief Selects od Deselects the corresponding card.
|
1239 |
* @param addr: Address of the Card to be selected.
|
1240 |
* @retval SD_Error: SD Card Error code.
|
1241 |
*/
|
1242 |
SD_Error SD_SelectDeselect(uint64_t addr) |
1243 |
{ |
1244 |
SD_Error errorstatus = SD_OK; |
1245 |
|
1246 |
/*!< Send CMD7 SDIO_SEL_DESEL_CARD */
|
1247 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)addr; |
1248 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD; |
1249 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1250 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1251 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1252 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1253 |
|
1254 |
errorstatus = CmdResp1Error(SD_CMD_SEL_DESEL_CARD); |
1255 |
|
1256 |
return(errorstatus);
|
1257 |
} |
1258 |
|
1259 |
/**
|
1260 |
* @brief Allows to read one block from a specified address in a card. The Data
|
1261 |
* transfer can be managed by DMA mode or Polling mode.
|
1262 |
* @note This operation should be followed by two functions to check if the
|
1263 |
* DMA Controller and SD Card status.
|
1264 |
* - SD_ReadWaitOperation(): this function insure that the DMA
|
1265 |
* controller has finished all data transfer.
|
1266 |
* - SD_GetStatus(): to check that the SD Card has finished the
|
1267 |
* data transfer and it is ready for data.
|
1268 |
* @param readbuff: pointer to the buffer that will contain the received data
|
1269 |
* @param ReadAddr: Address from where data are to be read.
|
1270 |
* @param BlockSize: the SD card Data block size. The Block size should be 512.
|
1271 |
* @retval SD_Error: SD Card Error code.
|
1272 |
*/
|
1273 |
SD_Error SD_ReadBlock(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize) |
1274 |
{ |
1275 |
SD_Error errorstatus = SD_OK; |
1276 |
#if defined (SD_POLLING_MODE)
|
1277 |
uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;
|
1278 |
#endif
|
1279 |
|
1280 |
TransferError = SD_OK; |
1281 |
TransferEnd = 0;
|
1282 |
StopCondition = 0;
|
1283 |
|
1284 |
SDIO->DCTRL = 0x0;
|
1285 |
|
1286 |
#if defined (SD_DMA_MODE)
|
1287 |
SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); |
1288 |
SDIO_DMACmd(ENABLE); |
1289 |
SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, BlockSize); |
1290 |
#endif
|
1291 |
|
1292 |
if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
|
1293 |
{ |
1294 |
BlockSize = 512;
|
1295 |
ReadAddr /= 512;
|
1296 |
} |
1297 |
|
1298 |
/* Set Block Size for Card */
|
1299 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; |
1300 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; |
1301 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1302 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1303 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1304 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1305 |
|
1306 |
uint32_t timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */
|
1307 |
|
1308 |
while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET)) |
1309 |
{ |
1310 |
timeout--; |
1311 |
} |
1312 |
|
1313 |
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); |
1314 |
|
1315 |
if (SD_OK != errorstatus)
|
1316 |
{ |
1317 |
return(errorstatus);
|
1318 |
} |
1319 |
|
1320 |
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; |
1321 |
SDIO_DataInitStructure.SDIO_DataLength = BlockSize; |
1322 |
SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4; |
1323 |
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; |
1324 |
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; |
1325 |
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; |
1326 |
SDIO_DataConfig(&SDIO_DataInitStructure); |
1327 |
|
1328 |
/*!< Send CMD17 READ_SINGLE_BLOCK */
|
1329 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr; |
1330 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK; |
1331 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1332 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1333 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1334 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1335 |
|
1336 |
errorstatus = CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK); |
1337 |
|
1338 |
if (errorstatus != SD_OK)
|
1339 |
{ |
1340 |
return(errorstatus);
|
1341 |
} |
1342 |
|
1343 |
#if defined (SD_POLLING_MODE)
|
1344 |
/*!< In case of single block transfer, no need of stop transfer at all.*/
|
1345 |
/*!< Polling mode */
|
1346 |
while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
|
1347 |
{ |
1348 |
if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
|
1349 |
{ |
1350 |
for (count = 0; count < 8; count++) |
1351 |
{ |
1352 |
*(tempbuff + count) = SDIO_ReadData(); |
1353 |
} |
1354 |
tempbuff += 8;
|
1355 |
} |
1356 |
} |
1357 |
|
1358 |
if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
|
1359 |
{ |
1360 |
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); |
1361 |
errorstatus = SD_DATA_TIMEOUT; |
1362 |
return(errorstatus);
|
1363 |
} |
1364 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) |
1365 |
{ |
1366 |
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); |
1367 |
errorstatus = SD_DATA_CRC_FAIL; |
1368 |
return(errorstatus);
|
1369 |
} |
1370 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) |
1371 |
{ |
1372 |
SDIO_ClearFlag(SDIO_FLAG_RXOVERR); |
1373 |
errorstatus = SD_RX_OVERRUN; |
1374 |
return(errorstatus);
|
1375 |
} |
1376 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) |
1377 |
{ |
1378 |
SDIO_ClearFlag(SDIO_FLAG_STBITERR); |
1379 |
errorstatus = SD_START_BIT_ERR; |
1380 |
return(errorstatus);
|
1381 |
} |
1382 |
count = SD_DATATIMEOUT; |
1383 |
while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0)) |
1384 |
{ |
1385 |
*tempbuff = SDIO_ReadData(); |
1386 |
tempbuff++; |
1387 |
count--; |
1388 |
} |
1389 |
|
1390 |
/*!< Clear all the static flags */
|
1391 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
1392 |
|
1393 |
#endif
|
1394 |
DMAEndOfTransfer = 0;
|
1395 |
|
1396 |
return(errorstatus);
|
1397 |
} |
1398 |
|
1399 |
/**
|
1400 |
* @brief Allows to read blocks from a specified address in a card. The Data
|
1401 |
* transfer can be managed by DMA mode or Polling mode.
|
1402 |
* @note This operation should be followed by two functions to check if the
|
1403 |
* DMA Controller and SD Card status.
|
1404 |
* - SD_ReadWaitOperation(): this function insure that the DMA
|
1405 |
* controller has finished all data transfer.
|
1406 |
* - SD_GetStatus(): to check that the SD Card has finished the
|
1407 |
* data transfer and it is ready for data.
|
1408 |
* @param readbuff: pointer to the buffer that will contain the received data.
|
1409 |
* @param ReadAddr: Address from where data are to be read.
|
1410 |
* @param BlockSize: the SD card Data block size. The Block size should be 512.
|
1411 |
* @param NumberOfBlocks: number of blocks to be read.
|
1412 |
* @retval SD_Error: SD Card Error code.
|
1413 |
*/
|
1414 |
SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint64_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks) |
1415 |
{ |
1416 |
SD_Error errorstatus = SD_OK; |
1417 |
TransferError = SD_OK; |
1418 |
TransferEnd = 0;
|
1419 |
StopCondition = 1;
|
1420 |
|
1421 |
SDIO->DCTRL = 0x0;
|
1422 |
|
1423 |
SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); |
1424 |
SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, (NumberOfBlocks * BlockSize)); |
1425 |
SDIO_DMACmd(ENABLE); |
1426 |
|
1427 |
if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
|
1428 |
{ |
1429 |
BlockSize = 512;
|
1430 |
ReadAddr /= 512;
|
1431 |
} |
1432 |
|
1433 |
/*!< Set Block Size for Card */
|
1434 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; |
1435 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; |
1436 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1437 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1438 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1439 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1440 |
|
1441 |
uint32_t timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */
|
1442 |
|
1443 |
while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET)) |
1444 |
{ |
1445 |
timeout--; |
1446 |
} |
1447 |
|
1448 |
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); |
1449 |
|
1450 |
if (SD_OK != errorstatus)
|
1451 |
{ |
1452 |
return(errorstatus);
|
1453 |
} |
1454 |
|
1455 |
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; |
1456 |
SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize; |
1457 |
SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4; |
1458 |
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; |
1459 |
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; |
1460 |
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; |
1461 |
SDIO_DataConfig(&SDIO_DataInitStructure); |
1462 |
|
1463 |
/*!< Send CMD18 READ_MULT_BLOCK with argument data address */
|
1464 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr; |
1465 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK; |
1466 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1467 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1468 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1469 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1470 |
|
1471 |
errorstatus = CmdResp1Error(SD_CMD_READ_MULT_BLOCK); |
1472 |
|
1473 |
if (errorstatus != SD_OK)
|
1474 |
{ |
1475 |
return(errorstatus);
|
1476 |
} |
1477 |
|
1478 |
DMAEndOfTransfer = 0;
|
1479 |
|
1480 |
return(errorstatus);
|
1481 |
} |
1482 |
|
1483 |
/**
|
1484 |
* @brief This function waits until the SDIO DMA data transfer is finished.
|
1485 |
* This function should be called after SDIO_ReadMultiBlocks() function
|
1486 |
* to insure that all data sent by the card are already transferred by
|
1487 |
* the DMA controller.
|
1488 |
* @param None.
|
1489 |
* @retval SD_Error: SD Card Error code.
|
1490 |
*/
|
1491 |
SD_Error SD_WaitReadOperation(void)
|
1492 |
{ |
1493 |
SD_Error errorstatus = SD_OK; |
1494 |
uint32_t timeout; |
1495 |
|
1496 |
timeout = SD_DATATIMEOUT; |
1497 |
|
1498 |
while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0)) |
1499 |
{ |
1500 |
timeout--; |
1501 |
} |
1502 |
|
1503 |
DMAEndOfTransfer = 0x00;
|
1504 |
|
1505 |
timeout = SD_DATATIMEOUT; |
1506 |
|
1507 |
while(((SDIO->STA & SDIO_FLAG_RXACT)) && (timeout > 0)) |
1508 |
{ |
1509 |
timeout--; |
1510 |
} |
1511 |
|
1512 |
if (StopCondition == 1) |
1513 |
{ |
1514 |
errorstatus = SD_StopTransfer(); |
1515 |
StopCondition = 0;
|
1516 |
} |
1517 |
|
1518 |
if ((timeout == 0) && (errorstatus == SD_OK)) |
1519 |
{ |
1520 |
errorstatus = SD_DATA_TIMEOUT; |
1521 |
} |
1522 |
|
1523 |
/*!< Clear all the static flags */
|
1524 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
1525 |
|
1526 |
if (TransferError != SD_OK)
|
1527 |
{ |
1528 |
return(TransferError);
|
1529 |
} |
1530 |
else
|
1531 |
{ |
1532 |
return(errorstatus);
|
1533 |
} |
1534 |
} |
1535 |
|
1536 |
/**
|
1537 |
* @brief Allows to write one block starting from a specified address in a card.
|
1538 |
* The Data transfer can be managed by DMA mode or Polling mode.
|
1539 |
* @note This operation should be followed by two functions to check if the
|
1540 |
* DMA Controller and SD Card status.
|
1541 |
* - SD_ReadWaitOperation(): this function insure that the DMA
|
1542 |
* controller has finished all data transfer.
|
1543 |
* - SD_GetStatus(): to check that the SD Card has finished the
|
1544 |
* data transfer and it is ready for data.
|
1545 |
* @param writebuff: pointer to the buffer that contain the data to be transferred.
|
1546 |
* @param WriteAddr: Address from where data are to be read.
|
1547 |
* @param BlockSize: the SD card Data block size. The Block size should be 512.
|
1548 |
* @retval SD_Error: SD Card Error code.
|
1549 |
*/
|
1550 |
SD_Error SD_WriteBlock(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize) |
1551 |
{ |
1552 |
SD_Error errorstatus = SD_OK; |
1553 |
|
1554 |
#if defined (SD_POLLING_MODE)
|
1555 |
uint32_t bytestransferred = 0, count = 0, restwords = 0; |
1556 |
uint32_t *tempbuff = (uint32_t *)writebuff; |
1557 |
#endif
|
1558 |
|
1559 |
TransferError = SD_OK; |
1560 |
TransferEnd = 0;
|
1561 |
StopCondition = 0;
|
1562 |
|
1563 |
SDIO->DCTRL = 0x0;
|
1564 |
|
1565 |
#if defined (SD_DMA_MODE)
|
1566 |
SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); |
1567 |
SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, BlockSize); |
1568 |
SDIO_DMACmd(ENABLE); |
1569 |
#endif
|
1570 |
|
1571 |
if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
|
1572 |
{ |
1573 |
BlockSize = 512;
|
1574 |
WriteAddr /= 512;
|
1575 |
} |
1576 |
|
1577 |
/* Set Block Size for Card */
|
1578 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; |
1579 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; |
1580 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1581 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1582 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1583 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1584 |
|
1585 |
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); |
1586 |
|
1587 |
if (SD_OK != errorstatus)
|
1588 |
{ |
1589 |
return(errorstatus);
|
1590 |
} |
1591 |
|
1592 |
/*!< Send CMD24 WRITE_SINGLE_BLOCK */
|
1593 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr; |
1594 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK; |
1595 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1596 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1597 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1598 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1599 |
|
1600 |
errorstatus = CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK); |
1601 |
|
1602 |
if (errorstatus != SD_OK)
|
1603 |
{ |
1604 |
return(errorstatus);
|
1605 |
} |
1606 |
|
1607 |
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; |
1608 |
SDIO_DataInitStructure.SDIO_DataLength = BlockSize; |
1609 |
SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4; |
1610 |
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; |
1611 |
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; |
1612 |
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; |
1613 |
SDIO_DataConfig(&SDIO_DataInitStructure); |
1614 |
|
1615 |
/*!< In case of single data block transfer no need of stop command at all */
|
1616 |
#if defined (SD_POLLING_MODE)
|
1617 |
while (!(SDIO->STA & (SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR)))
|
1618 |
{ |
1619 |
if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET)
|
1620 |
{ |
1621 |
if ((512 - bytestransferred) < 32) |
1622 |
{ |
1623 |
restwords = ((512 - bytestransferred) % 4 == 0) ? ((512 - bytestransferred) / 4) : (( 512 - bytestransferred) / 4 + 1); |
1624 |
for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4) |
1625 |
{ |
1626 |
SDIO_WriteData(*tempbuff); |
1627 |
} |
1628 |
} |
1629 |
else
|
1630 |
{ |
1631 |
for (count = 0; count < 8; count++) |
1632 |
{ |
1633 |
SDIO_WriteData(*(tempbuff + count)); |
1634 |
} |
1635 |
tempbuff += 8;
|
1636 |
bytestransferred += 32;
|
1637 |
} |
1638 |
} |
1639 |
} |
1640 |
if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
|
1641 |
{ |
1642 |
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); |
1643 |
errorstatus = SD_DATA_TIMEOUT; |
1644 |
return(errorstatus);
|
1645 |
} |
1646 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) |
1647 |
{ |
1648 |
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); |
1649 |
errorstatus = SD_DATA_CRC_FAIL; |
1650 |
return(errorstatus);
|
1651 |
} |
1652 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET) |
1653 |
{ |
1654 |
SDIO_ClearFlag(SDIO_FLAG_TXUNDERR); |
1655 |
errorstatus = SD_TX_UNDERRUN; |
1656 |
return(errorstatus);
|
1657 |
} |
1658 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) |
1659 |
{ |
1660 |
SDIO_ClearFlag(SDIO_FLAG_STBITERR); |
1661 |
errorstatus = SD_START_BIT_ERR; |
1662 |
return(errorstatus);
|
1663 |
} |
1664 |
|
1665 |
#endif
|
1666 |
|
1667 |
DMAEndOfTransfer = 0;
|
1668 |
return(errorstatus);
|
1669 |
} |
1670 |
|
1671 |
/**
|
1672 |
* @brief Allows to write blocks starting from a specified address in a card.
|
1673 |
* The Data transfer can be managed by DMA mode only.
|
1674 |
* @note This operation should be followed by two functions to check if the
|
1675 |
* DMA Controller and SD Card status.
|
1676 |
* - SD_ReadWaitOperation(): this function insure that the DMA
|
1677 |
* controller has finished all data transfer.
|
1678 |
* - SD_GetStatus(): to check that the SD Card has finished the
|
1679 |
* data transfer and it is ready for data.
|
1680 |
* @param WriteAddr: Address from where data are to be read.
|
1681 |
* @param writebuff: pointer to the buffer that contain the data to be transferred.
|
1682 |
* @param BlockSize: the SD card Data block size. The Block size should be 512.
|
1683 |
* @param NumberOfBlocks: number of blocks to be written.
|
1684 |
* @retval SD_Error: SD Card Error code.
|
1685 |
*/
|
1686 |
SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint64_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks) |
1687 |
{ |
1688 |
SD_Error errorstatus = SD_OK; |
1689 |
|
1690 |
TransferError = SD_OK; |
1691 |
TransferEnd = 0;
|
1692 |
StopCondition = 1;
|
1693 |
SDIO->DCTRL = 0x0;
|
1694 |
|
1695 |
SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | SDIO_IT_RXOVERR | SDIO_IT_STBITERR, ENABLE); |
1696 |
SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, (NumberOfBlocks * BlockSize)); |
1697 |
SDIO_DMACmd(ENABLE); |
1698 |
|
1699 |
if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
|
1700 |
{ |
1701 |
BlockSize = 512;
|
1702 |
WriteAddr /= 512;
|
1703 |
} |
1704 |
|
1705 |
/* Set Block Size for Card */
|
1706 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; |
1707 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; |
1708 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1709 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1710 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1711 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1712 |
|
1713 |
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); |
1714 |
|
1715 |
if (SD_OK != errorstatus)
|
1716 |
{ |
1717 |
return(errorstatus);
|
1718 |
} |
1719 |
|
1720 |
/*!< To improve performance */
|
1721 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16);
|
1722 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; |
1723 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1724 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1725 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1726 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1727 |
|
1728 |
|
1729 |
errorstatus = CmdResp1Error(SD_CMD_APP_CMD); |
1730 |
|
1731 |
if (errorstatus != SD_OK)
|
1732 |
{ |
1733 |
return(errorstatus);
|
1734 |
} |
1735 |
/*!< To improve performance */
|
1736 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks; |
1737 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT; |
1738 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1739 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1740 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1741 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1742 |
|
1743 |
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCK_COUNT); |
1744 |
|
1745 |
if (errorstatus != SD_OK)
|
1746 |
{ |
1747 |
return(errorstatus);
|
1748 |
} |
1749 |
|
1750 |
|
1751 |
/*!< Send CMD25 WRITE_MULT_BLOCK with argument data address */
|
1752 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr; |
1753 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK; |
1754 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1755 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1756 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1757 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1758 |
|
1759 |
errorstatus = CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK); |
1760 |
|
1761 |
if (SD_OK != errorstatus)
|
1762 |
{ |
1763 |
return(errorstatus);
|
1764 |
} |
1765 |
|
1766 |
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; |
1767 |
SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize; |
1768 |
SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4; |
1769 |
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; |
1770 |
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; |
1771 |
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; |
1772 |
SDIO_DataConfig(&SDIO_DataInitStructure); |
1773 |
|
1774 |
DMAEndOfTransfer = 0;
|
1775 |
return(errorstatus);
|
1776 |
} |
1777 |
|
1778 |
/**
|
1779 |
* @brief This function waits until the SDIO DMA data transfer is finished.
|
1780 |
* This function should be called after SDIO_WriteBlock() and
|
1781 |
* SDIO_WriteMultiBlocks() function to insure that all data sent by the
|
1782 |
* card are already transferred by the DMA controller.
|
1783 |
* @param None.
|
1784 |
* @retval SD_Error: SD Card Error code.
|
1785 |
*/
|
1786 |
SD_Error SD_WaitWriteOperation(void)
|
1787 |
{ |
1788 |
SD_Error errorstatus = SD_OK; |
1789 |
uint32_t timeout; |
1790 |
|
1791 |
timeout = SD_DATATIMEOUT; |
1792 |
|
1793 |
while ((DMAEndOfTransfer == 0x00) && (TransferEnd == 0) && (TransferError == SD_OK) && (timeout > 0)) |
1794 |
{ |
1795 |
if (DMA_GetITStatus(DMA2_Stream3, DMA_IT_TCIF3) == 1) |
1796 |
{ |
1797 |
DMAEndOfTransfer = 1;
|
1798 |
/// clear the interrupt bits
|
1799 |
DMA_ClearITPendingBit(DMA2_Stream3, DMA_IT_TCIF3); |
1800 |
} |
1801 |
timeout--; |
1802 |
} |
1803 |
|
1804 |
timeout = SD_DATATIMEOUT; |
1805 |
|
1806 |
while((SDIO->DCOUNT != 0) && (timeout > 0)) |
1807 |
{ |
1808 |
timeout--; |
1809 |
} |
1810 |
|
1811 |
if (StopCondition == 1) |
1812 |
{ |
1813 |
errorstatus = SD_StopTransfer(); |
1814 |
StopCondition = 0;
|
1815 |
} |
1816 |
|
1817 |
if ((timeout == 0) && (errorstatus == SD_OK)) |
1818 |
{ |
1819 |
errorstatus = SD_DATA_TIMEOUT; |
1820 |
} |
1821 |
|
1822 |
/*!< Clear all the static flags */
|
1823 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
1824 |
|
1825 |
if (TransferError != SD_OK)
|
1826 |
{ |
1827 |
return(TransferError);
|
1828 |
} |
1829 |
else
|
1830 |
{ |
1831 |
return(errorstatus);
|
1832 |
} |
1833 |
} |
1834 |
|
1835 |
/**
|
1836 |
* @brief Gets the cuurent data transfer state.
|
1837 |
* @param None
|
1838 |
* @retval SDTransferState: Data Transfer state.
|
1839 |
* This value can be:
|
1840 |
* - SD_TRANSFER_OK: No data transfer is acting
|
1841 |
* - SD_TRANSFER_BUSY: Data transfer is acting
|
1842 |
*/
|
1843 |
SDTransferState SD_GetTransferState(void)
|
1844 |
{ |
1845 |
if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT))
|
1846 |
{ |
1847 |
return(SD_TRANSFER_BUSY);
|
1848 |
} |
1849 |
else
|
1850 |
{ |
1851 |
return(SD_TRANSFER_OK);
|
1852 |
} |
1853 |
} |
1854 |
|
1855 |
/**
|
1856 |
* @brief Aborts an ongoing data transfer.
|
1857 |
* @param None
|
1858 |
* @retval SD_Error: SD Card Error code.
|
1859 |
*/
|
1860 |
SD_Error SD_StopTransfer(void)
|
1861 |
{ |
1862 |
SD_Error errorstatus = SD_OK; |
1863 |
|
1864 |
/*!< Send CMD12 STOP_TRANSMISSION */
|
1865 |
SDIO_CmdInitStructure.SDIO_Argument = 0x0;
|
1866 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_STOP_TRANSMISSION; |
1867 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1868 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1869 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1870 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1871 |
|
1872 |
errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION); |
1873 |
|
1874 |
return(errorstatus);
|
1875 |
} |
1876 |
|
1877 |
/**
|
1878 |
* @brief Allows to erase memory area specified for the given card.
|
1879 |
* @param startaddr: the start address.
|
1880 |
* @param endaddr: the end address.
|
1881 |
* @retval SD_Error: SD Card Error code.
|
1882 |
*/
|
1883 |
SD_Error SD_Erase(uint64_t startaddr, uint64_t endaddr) |
1884 |
{ |
1885 |
SD_Error errorstatus = SD_OK; |
1886 |
uint32_t delay = 0;
|
1887 |
__IO uint32_t maxdelay = 0;
|
1888 |
uint8_t cardstate = 0;
|
1889 |
|
1890 |
/*!< Check if the card coomnd class supports erase command */
|
1891 |
if (((CSD_Tab[1] >> 20) & SD_CCCC_ERASE) == 0) |
1892 |
{ |
1893 |
errorstatus = SD_REQUEST_NOT_APPLICABLE; |
1894 |
return(errorstatus);
|
1895 |
} |
1896 |
|
1897 |
maxdelay = 120000 / ((SDIO->CLKCR & 0xFF) + 2); |
1898 |
|
1899 |
if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
|
1900 |
{ |
1901 |
errorstatus = SD_LOCK_UNLOCK_FAILED; |
1902 |
return(errorstatus);
|
1903 |
} |
1904 |
|
1905 |
if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)
|
1906 |
{ |
1907 |
startaddr /= 512;
|
1908 |
endaddr /= 512;
|
1909 |
} |
1910 |
|
1911 |
/*!< According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
|
1912 |
if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType))
|
1913 |
{ |
1914 |
/*!< Send CMD32 SD_ERASE_GRP_START with argument as addr */
|
1915 |
SDIO_CmdInitStructure.SDIO_Argument =(uint32_t)startaddr; |
1916 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_START; |
1917 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1918 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1919 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1920 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1921 |
|
1922 |
errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_START); |
1923 |
if (errorstatus != SD_OK)
|
1924 |
{ |
1925 |
return(errorstatus);
|
1926 |
} |
1927 |
|
1928 |
/*!< Send CMD33 SD_ERASE_GRP_END with argument as addr */
|
1929 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)endaddr; |
1930 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_END; |
1931 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1932 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1933 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1934 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1935 |
|
1936 |
errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_END); |
1937 |
if (errorstatus != SD_OK)
|
1938 |
{ |
1939 |
return(errorstatus);
|
1940 |
} |
1941 |
} |
1942 |
|
1943 |
/*!< Send CMD38 ERASE */
|
1944 |
SDIO_CmdInitStructure.SDIO_Argument = 0;
|
1945 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ERASE; |
1946 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1947 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1948 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1949 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1950 |
|
1951 |
errorstatus = CmdResp1Error(SD_CMD_ERASE); |
1952 |
|
1953 |
if (errorstatus != SD_OK)
|
1954 |
{ |
1955 |
return(errorstatus);
|
1956 |
} |
1957 |
|
1958 |
for (delay = 0; delay < maxdelay; delay++) |
1959 |
{} |
1960 |
|
1961 |
/*!< Wait till the card is in programming state */
|
1962 |
errorstatus = IsCardProgramming(&cardstate); |
1963 |
delay = SD_DATATIMEOUT; |
1964 |
while ((delay > 0) && (errorstatus == SD_OK) && ((SD_CARD_PROGRAMMING == cardstate) || (SD_CARD_RECEIVING == cardstate))) |
1965 |
{ |
1966 |
errorstatus = IsCardProgramming(&cardstate); |
1967 |
delay--; |
1968 |
} |
1969 |
|
1970 |
return(errorstatus);
|
1971 |
} |
1972 |
|
1973 |
/**
|
1974 |
* @brief Returns the current card's status.
|
1975 |
* @param pcardstatus: pointer to the buffer that will contain the SD card
|
1976 |
* status (Card Status register).
|
1977 |
* @retval SD_Error: SD Card Error code.
|
1978 |
*/
|
1979 |
SD_Error SD_SendStatus(uint32_t *pcardstatus) |
1980 |
{ |
1981 |
SD_Error errorstatus = SD_OK; |
1982 |
|
1983 |
if (pcardstatus == NULL) |
1984 |
{ |
1985 |
errorstatus = SD_INVALID_PARAMETER; |
1986 |
return(errorstatus);
|
1987 |
} |
1988 |
|
1989 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
|
1990 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS; |
1991 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
1992 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
1993 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
1994 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
1995 |
|
1996 |
errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS); |
1997 |
|
1998 |
if (errorstatus != SD_OK)
|
1999 |
{ |
2000 |
return(errorstatus);
|
2001 |
} |
2002 |
|
2003 |
*pcardstatus = SDIO_GetResponse(SDIO_RESP1); |
2004 |
|
2005 |
return(errorstatus);
|
2006 |
} |
2007 |
|
2008 |
/**
|
2009 |
* @brief Returns the current SD card's status.
|
2010 |
* @param psdstatus: pointer to the buffer that will contain the SD card status
|
2011 |
* (SD Status register).
|
2012 |
* @retval SD_Error: SD Card Error code.
|
2013 |
*/
|
2014 |
SD_Error SD_SendSDStatus(uint32_t *psdstatus) |
2015 |
{ |
2016 |
SD_Error errorstatus = SD_OK; |
2017 |
uint32_t count = 0;
|
2018 |
|
2019 |
if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
|
2020 |
{ |
2021 |
errorstatus = SD_LOCK_UNLOCK_FAILED; |
2022 |
return(errorstatus);
|
2023 |
} |
2024 |
|
2025 |
/*!< Set block size for card if it is not equal to current block size for card. */
|
2026 |
SDIO_CmdInitStructure.SDIO_Argument = 64;
|
2027 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; |
2028 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2029 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2030 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2031 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2032 |
|
2033 |
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); |
2034 |
|
2035 |
if (errorstatus != SD_OK)
|
2036 |
{ |
2037 |
return(errorstatus);
|
2038 |
} |
2039 |
|
2040 |
/*!< CMD55 */
|
2041 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
|
2042 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; |
2043 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2044 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2045 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2046 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2047 |
errorstatus = CmdResp1Error(SD_CMD_APP_CMD); |
2048 |
|
2049 |
if (errorstatus != SD_OK)
|
2050 |
{ |
2051 |
return(errorstatus);
|
2052 |
} |
2053 |
|
2054 |
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; |
2055 |
SDIO_DataInitStructure.SDIO_DataLength = 64;
|
2056 |
SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_64b; |
2057 |
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; |
2058 |
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; |
2059 |
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; |
2060 |
SDIO_DataConfig(&SDIO_DataInitStructure); |
2061 |
|
2062 |
/*!< Send ACMD13 SD_APP_STAUS with argument as card's RCA.*/
|
2063 |
SDIO_CmdInitStructure.SDIO_Argument = 0;
|
2064 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_STAUS; |
2065 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2066 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2067 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2068 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2069 |
errorstatus = CmdResp1Error(SD_CMD_SD_APP_STAUS); |
2070 |
|
2071 |
if (errorstatus != SD_OK)
|
2072 |
{ |
2073 |
return(errorstatus);
|
2074 |
} |
2075 |
|
2076 |
while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
|
2077 |
{ |
2078 |
if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)
|
2079 |
{ |
2080 |
for (count = 0; count < 8; count++) |
2081 |
{ |
2082 |
*(psdstatus + count) = SDIO_ReadData(); |
2083 |
} |
2084 |
psdstatus += 8;
|
2085 |
} |
2086 |
} |
2087 |
|
2088 |
if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
|
2089 |
{ |
2090 |
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); |
2091 |
errorstatus = SD_DATA_TIMEOUT; |
2092 |
return(errorstatus);
|
2093 |
} |
2094 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) |
2095 |
{ |
2096 |
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); |
2097 |
errorstatus = SD_DATA_CRC_FAIL; |
2098 |
return(errorstatus);
|
2099 |
} |
2100 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) |
2101 |
{ |
2102 |
SDIO_ClearFlag(SDIO_FLAG_RXOVERR); |
2103 |
errorstatus = SD_RX_OVERRUN; |
2104 |
return(errorstatus);
|
2105 |
} |
2106 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) |
2107 |
{ |
2108 |
SDIO_ClearFlag(SDIO_FLAG_STBITERR); |
2109 |
errorstatus = SD_START_BIT_ERR; |
2110 |
return(errorstatus);
|
2111 |
} |
2112 |
|
2113 |
count = SD_DATATIMEOUT; |
2114 |
while ((SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) && (count > 0)) |
2115 |
{ |
2116 |
*psdstatus = SDIO_ReadData(); |
2117 |
psdstatus++; |
2118 |
count--; |
2119 |
} |
2120 |
/*!< Clear all the static status flags*/
|
2121 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
2122 |
|
2123 |
return(errorstatus);
|
2124 |
} |
2125 |
|
2126 |
/**
|
2127 |
* @brief Allows to process all the interrupts that are high.
|
2128 |
* @param None
|
2129 |
* @retval SD_Error: SD Card Error code.
|
2130 |
*/
|
2131 |
SD_Error SD_ProcessIRQSrc(void)
|
2132 |
{ |
2133 |
if (SDIO_GetITStatus(SDIO_IT_DATAEND) != RESET)
|
2134 |
{ |
2135 |
TransferError = SD_OK; |
2136 |
SDIO_ClearITPendingBit(SDIO_IT_DATAEND); |
2137 |
TransferEnd = 1;
|
2138 |
} |
2139 |
else if (SDIO_GetITStatus(SDIO_IT_DCRCFAIL) != RESET) |
2140 |
{ |
2141 |
SDIO_ClearITPendingBit(SDIO_IT_DCRCFAIL); |
2142 |
TransferError = SD_DATA_CRC_FAIL; |
2143 |
} |
2144 |
else if (SDIO_GetITStatus(SDIO_IT_DTIMEOUT) != RESET) |
2145 |
{ |
2146 |
SDIO_ClearITPendingBit(SDIO_IT_DTIMEOUT); |
2147 |
TransferError = SD_DATA_TIMEOUT; |
2148 |
} |
2149 |
else if (SDIO_GetITStatus(SDIO_IT_RXOVERR) != RESET) |
2150 |
{ |
2151 |
SDIO_ClearITPendingBit(SDIO_IT_RXOVERR); |
2152 |
TransferError = SD_RX_OVERRUN; |
2153 |
} |
2154 |
else if (SDIO_GetITStatus(SDIO_IT_TXUNDERR) != RESET) |
2155 |
{ |
2156 |
SDIO_ClearITPendingBit(SDIO_IT_TXUNDERR); |
2157 |
TransferError = SD_TX_UNDERRUN; |
2158 |
} |
2159 |
else if (SDIO_GetITStatus(SDIO_IT_STBITERR) != RESET) |
2160 |
{ |
2161 |
SDIO_ClearITPendingBit(SDIO_IT_STBITERR); |
2162 |
TransferError = SD_START_BIT_ERR; |
2163 |
} |
2164 |
|
2165 |
SDIO_ITConfig(SDIO_IT_DCRCFAIL | SDIO_IT_DTIMEOUT | SDIO_IT_DATAEND | |
2166 |
SDIO_IT_TXFIFOHE | SDIO_IT_RXFIFOHF | SDIO_IT_TXUNDERR | |
2167 |
SDIO_IT_RXOVERR | SDIO_IT_STBITERR, DISABLE); |
2168 |
return(TransferError);
|
2169 |
} |
2170 |
|
2171 |
/**
|
2172 |
* @brief This function waits until the SDIO DMA data transfer is finished.
|
2173 |
* @param None.
|
2174 |
* @retval None.
|
2175 |
*/
|
2176 |
void SD_ProcessDMAIRQ(void) |
2177 |
{ |
2178 |
if(DMA2->LISR & SD_SDIO_DMA_FLAG_TCIF)
|
2179 |
{ |
2180 |
DMAEndOfTransfer = 0x01;
|
2181 |
DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_TCIF|SD_SDIO_DMA_FLAG_FEIF); |
2182 |
} |
2183 |
} |
2184 |
|
2185 |
/**
|
2186 |
* @brief Checks for error conditions for CMD0.
|
2187 |
* @param None
|
2188 |
* @retval SD_Error: SD Card Error code.
|
2189 |
*/
|
2190 |
static SD_Error CmdError(void) |
2191 |
{ |
2192 |
SD_Error errorstatus = SD_OK; |
2193 |
uint32_t timeout; |
2194 |
|
2195 |
timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */
|
2196 |
|
2197 |
while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET)) |
2198 |
{ |
2199 |
timeout--; |
2200 |
} |
2201 |
|
2202 |
if (timeout == 0) |
2203 |
{ |
2204 |
errorstatus = SD_CMD_RSP_TIMEOUT; |
2205 |
return(errorstatus);
|
2206 |
} |
2207 |
|
2208 |
/*!< Clear all the static flags */
|
2209 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
2210 |
|
2211 |
return(errorstatus);
|
2212 |
} |
2213 |
|
2214 |
/**
|
2215 |
* @brief Checks for error conditions for R7 response.
|
2216 |
* @param None
|
2217 |
* @retval SD_Error: SD Card Error code.
|
2218 |
*/
|
2219 |
static SD_Error CmdResp7Error(void) |
2220 |
{ |
2221 |
SD_Error errorstatus = SD_OK; |
2222 |
uint32_t status; |
2223 |
uint32_t timeout = SDIO_CMD0TIMEOUT; |
2224 |
|
2225 |
status = SDIO->STA; |
2226 |
|
2227 |
while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) && (timeout > 0)) |
2228 |
{ |
2229 |
timeout--; |
2230 |
status = SDIO->STA; |
2231 |
} |
2232 |
|
2233 |
if ((timeout == 0) || (status & SDIO_FLAG_CTIMEOUT)) |
2234 |
{ |
2235 |
/*!< Card is not V2.0 complient or card does not support the set voltage range */
|
2236 |
errorstatus = SD_CMD_RSP_TIMEOUT; |
2237 |
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); |
2238 |
return(errorstatus);
|
2239 |
} |
2240 |
|
2241 |
if (status & SDIO_FLAG_CMDREND)
|
2242 |
{ |
2243 |
/*!< Card is SD V2.0 compliant */
|
2244 |
errorstatus = SD_OK; |
2245 |
SDIO_ClearFlag(SDIO_FLAG_CMDREND); |
2246 |
return(errorstatus);
|
2247 |
} |
2248 |
return(errorstatus);
|
2249 |
} |
2250 |
|
2251 |
/**
|
2252 |
* @brief Checks for error conditions for R1 response.
|
2253 |
* @param cmd: The sent command index.
|
2254 |
* @retval SD_Error: SD Card Error code.
|
2255 |
*/
|
2256 |
static SD_Error CmdResp1Error(uint8_t cmd)
|
2257 |
{ |
2258 |
SD_Error errorstatus = SD_OK; |
2259 |
uint32_t status; |
2260 |
uint32_t response_r1; |
2261 |
|
2262 |
status = SDIO->STA; |
2263 |
|
2264 |
while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
|
2265 |
{ |
2266 |
status = SDIO->STA; |
2267 |
} |
2268 |
|
2269 |
if (status & SDIO_FLAG_CTIMEOUT)
|
2270 |
{ |
2271 |
errorstatus = SD_CMD_RSP_TIMEOUT; |
2272 |
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); |
2273 |
return(errorstatus);
|
2274 |
} |
2275 |
else if (status & SDIO_FLAG_CCRCFAIL) |
2276 |
{ |
2277 |
errorstatus = SD_CMD_CRC_FAIL; |
2278 |
SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); |
2279 |
return(errorstatus);
|
2280 |
} |
2281 |
|
2282 |
/*!< Check response received is of desired command */
|
2283 |
if (SDIO_GetCommandResponse() != cmd)
|
2284 |
{ |
2285 |
errorstatus = SD_ILLEGAL_CMD; |
2286 |
return(errorstatus);
|
2287 |
} |
2288 |
|
2289 |
/*!< Clear all the static flags */
|
2290 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
2291 |
|
2292 |
/*!< We have received response, retrieve it for analysis */
|
2293 |
response_r1 = SDIO_GetResponse(SDIO_RESP1); |
2294 |
|
2295 |
if ((response_r1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
|
2296 |
{ |
2297 |
return(errorstatus);
|
2298 |
} |
2299 |
|
2300 |
if (response_r1 & SD_OCR_ADDR_OUT_OF_RANGE)
|
2301 |
{ |
2302 |
return(SD_ADDR_OUT_OF_RANGE);
|
2303 |
} |
2304 |
|
2305 |
if (response_r1 & SD_OCR_ADDR_MISALIGNED)
|
2306 |
{ |
2307 |
return(SD_ADDR_MISALIGNED);
|
2308 |
} |
2309 |
|
2310 |
if (response_r1 & SD_OCR_BLOCK_LEN_ERR)
|
2311 |
{ |
2312 |
return(SD_BLOCK_LEN_ERR);
|
2313 |
} |
2314 |
|
2315 |
if (response_r1 & SD_OCR_ERASE_SEQ_ERR)
|
2316 |
{ |
2317 |
return(SD_ERASE_SEQ_ERR);
|
2318 |
} |
2319 |
|
2320 |
if (response_r1 & SD_OCR_BAD_ERASE_PARAM)
|
2321 |
{ |
2322 |
return(SD_BAD_ERASE_PARAM);
|
2323 |
} |
2324 |
|
2325 |
if (response_r1 & SD_OCR_WRITE_PROT_VIOLATION)
|
2326 |
{ |
2327 |
return(SD_WRITE_PROT_VIOLATION);
|
2328 |
} |
2329 |
|
2330 |
if (response_r1 & SD_OCR_LOCK_UNLOCK_FAILED)
|
2331 |
{ |
2332 |
return(SD_LOCK_UNLOCK_FAILED);
|
2333 |
} |
2334 |
|
2335 |
if (response_r1 & SD_OCR_COM_CRC_FAILED)
|
2336 |
{ |
2337 |
return(SD_COM_CRC_FAILED);
|
2338 |
} |
2339 |
|
2340 |
if (response_r1 & SD_OCR_ILLEGAL_CMD)
|
2341 |
{ |
2342 |
return(SD_ILLEGAL_CMD);
|
2343 |
} |
2344 |
|
2345 |
if (response_r1 & SD_OCR_CARD_ECC_FAILED)
|
2346 |
{ |
2347 |
return(SD_CARD_ECC_FAILED);
|
2348 |
} |
2349 |
|
2350 |
if (response_r1 & SD_OCR_CC_ERROR)
|
2351 |
{ |
2352 |
return(SD_CC_ERROR);
|
2353 |
} |
2354 |
|
2355 |
if (response_r1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
|
2356 |
{ |
2357 |
return(SD_GENERAL_UNKNOWN_ERROR);
|
2358 |
} |
2359 |
|
2360 |
if (response_r1 & SD_OCR_STREAM_READ_UNDERRUN)
|
2361 |
{ |
2362 |
return(SD_STREAM_READ_UNDERRUN);
|
2363 |
} |
2364 |
|
2365 |
if (response_r1 & SD_OCR_STREAM_WRITE_OVERRUN)
|
2366 |
{ |
2367 |
return(SD_STREAM_WRITE_OVERRUN);
|
2368 |
} |
2369 |
|
2370 |
if (response_r1 & SD_OCR_CID_CSD_OVERWRIETE)
|
2371 |
{ |
2372 |
return(SD_CID_CSD_OVERWRITE);
|
2373 |
} |
2374 |
|
2375 |
if (response_r1 & SD_OCR_WP_ERASE_SKIP)
|
2376 |
{ |
2377 |
return(SD_WP_ERASE_SKIP);
|
2378 |
} |
2379 |
|
2380 |
if (response_r1 & SD_OCR_CARD_ECC_DISABLED)
|
2381 |
{ |
2382 |
return(SD_CARD_ECC_DISABLED);
|
2383 |
} |
2384 |
|
2385 |
if (response_r1 & SD_OCR_ERASE_RESET)
|
2386 |
{ |
2387 |
return(SD_ERASE_RESET);
|
2388 |
} |
2389 |
|
2390 |
if (response_r1 & SD_OCR_AKE_SEQ_ERROR)
|
2391 |
{ |
2392 |
return(SD_AKE_SEQ_ERROR);
|
2393 |
} |
2394 |
return(errorstatus);
|
2395 |
} |
2396 |
|
2397 |
/**
|
2398 |
* @brief Checks for error conditions for R3 (OCR) response.
|
2399 |
* @param None
|
2400 |
* @retval SD_Error: SD Card Error code.
|
2401 |
*/
|
2402 |
static SD_Error CmdResp3Error(void) |
2403 |
{ |
2404 |
SD_Error errorstatus = SD_OK; |
2405 |
uint32_t status; |
2406 |
|
2407 |
status = SDIO->STA; |
2408 |
|
2409 |
while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
|
2410 |
{ |
2411 |
status = SDIO->STA; |
2412 |
} |
2413 |
|
2414 |
if (status & SDIO_FLAG_CTIMEOUT)
|
2415 |
{ |
2416 |
errorstatus = SD_CMD_RSP_TIMEOUT; |
2417 |
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); |
2418 |
return(errorstatus);
|
2419 |
} |
2420 |
/*!< Clear all the static flags */
|
2421 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
2422 |
return(errorstatus);
|
2423 |
} |
2424 |
|
2425 |
/**
|
2426 |
* @brief Checks for error conditions for R2 (CID or CSD) response.
|
2427 |
* @param None
|
2428 |
* @retval SD_Error: SD Card Error code.
|
2429 |
*/
|
2430 |
static SD_Error CmdResp2Error(void) |
2431 |
{ |
2432 |
SD_Error errorstatus = SD_OK; |
2433 |
uint32_t status; |
2434 |
|
2435 |
status = SDIO->STA; |
2436 |
|
2437 |
while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
|
2438 |
{ |
2439 |
status = SDIO->STA; |
2440 |
} |
2441 |
|
2442 |
if (status & SDIO_FLAG_CTIMEOUT)
|
2443 |
{ |
2444 |
errorstatus = SD_CMD_RSP_TIMEOUT; |
2445 |
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); |
2446 |
return(errorstatus);
|
2447 |
} |
2448 |
else if (status & SDIO_FLAG_CCRCFAIL) |
2449 |
{ |
2450 |
errorstatus = SD_CMD_CRC_FAIL; |
2451 |
SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); |
2452 |
return(errorstatus);
|
2453 |
} |
2454 |
|
2455 |
/*!< Clear all the static flags */
|
2456 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
2457 |
|
2458 |
return(errorstatus);
|
2459 |
} |
2460 |
|
2461 |
/**
|
2462 |
* @brief Checks for error conditions for R6 (RCA) response.
|
2463 |
* @param cmd: The sent command index.
|
2464 |
* @param prca: pointer to the variable that will contain the SD card relative
|
2465 |
* address RCA.
|
2466 |
* @retval SD_Error: SD Card Error code.
|
2467 |
*/
|
2468 |
static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca)
|
2469 |
{ |
2470 |
SD_Error errorstatus = SD_OK; |
2471 |
uint32_t status; |
2472 |
uint32_t response_r1; |
2473 |
|
2474 |
status = SDIO->STA; |
2475 |
|
2476 |
while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND)))
|
2477 |
{ |
2478 |
status = SDIO->STA; |
2479 |
} |
2480 |
|
2481 |
if (status & SDIO_FLAG_CTIMEOUT)
|
2482 |
{ |
2483 |
errorstatus = SD_CMD_RSP_TIMEOUT; |
2484 |
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); |
2485 |
return(errorstatus);
|
2486 |
} |
2487 |
else if (status & SDIO_FLAG_CCRCFAIL) |
2488 |
{ |
2489 |
errorstatus = SD_CMD_CRC_FAIL; |
2490 |
SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); |
2491 |
return(errorstatus);
|
2492 |
} |
2493 |
|
2494 |
/*!< Check response received is of desired command */
|
2495 |
if (SDIO_GetCommandResponse() != cmd)
|
2496 |
{ |
2497 |
errorstatus = SD_ILLEGAL_CMD; |
2498 |
return(errorstatus);
|
2499 |
} |
2500 |
|
2501 |
/*!< Clear all the static flags */
|
2502 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
2503 |
|
2504 |
/*!< We have received response, retrieve it. */
|
2505 |
response_r1 = SDIO_GetResponse(SDIO_RESP1); |
2506 |
|
2507 |
if (SD_ALLZERO == (response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)))
|
2508 |
{ |
2509 |
*prca = (uint16_t) (response_r1 >> 16);
|
2510 |
return(errorstatus);
|
2511 |
} |
2512 |
|
2513 |
if (response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR)
|
2514 |
{ |
2515 |
return(SD_GENERAL_UNKNOWN_ERROR);
|
2516 |
} |
2517 |
|
2518 |
if (response_r1 & SD_R6_ILLEGAL_CMD)
|
2519 |
{ |
2520 |
return(SD_ILLEGAL_CMD);
|
2521 |
} |
2522 |
|
2523 |
if (response_r1 & SD_R6_COM_CRC_FAILED)
|
2524 |
{ |
2525 |
return(SD_COM_CRC_FAILED);
|
2526 |
} |
2527 |
|
2528 |
return(errorstatus);
|
2529 |
} |
2530 |
|
2531 |
/**
|
2532 |
* @brief Enables or disables the SDIO wide bus mode.
|
2533 |
* @param NewState: new state of the SDIO wide bus mode.
|
2534 |
* This parameter can be: ENABLE or DISABLE.
|
2535 |
* @retval SD_Error: SD Card Error code.
|
2536 |
*/
|
2537 |
static SD_Error SDEnWideBus(FunctionalState NewState)
|
2538 |
{ |
2539 |
SD_Error errorstatus = SD_OK; |
2540 |
|
2541 |
uint32_t scr[2] = {0, 0}; |
2542 |
|
2543 |
if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED)
|
2544 |
{ |
2545 |
errorstatus = SD_LOCK_UNLOCK_FAILED; |
2546 |
return(errorstatus);
|
2547 |
} |
2548 |
|
2549 |
/*!< Get SCR Register */
|
2550 |
errorstatus = FindSCR(RCA, scr); |
2551 |
|
2552 |
if (errorstatus != SD_OK)
|
2553 |
{ |
2554 |
return(errorstatus);
|
2555 |
} |
2556 |
|
2557 |
/*!< If wide bus operation to be enabled */
|
2558 |
if (NewState == ENABLE)
|
2559 |
{ |
2560 |
/*!< If requested card supports wide bus operation */
|
2561 |
if ((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO) |
2562 |
{ |
2563 |
/*!< Send CMD55 APP_CMD with argument as card's RCA.*/
|
2564 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
|
2565 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; |
2566 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2567 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2568 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2569 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2570 |
|
2571 |
errorstatus = CmdResp1Error(SD_CMD_APP_CMD); |
2572 |
|
2573 |
if (errorstatus != SD_OK)
|
2574 |
{ |
2575 |
return(errorstatus);
|
2576 |
} |
2577 |
|
2578 |
/*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
|
2579 |
SDIO_CmdInitStructure.SDIO_Argument = 0x2;
|
2580 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH; |
2581 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2582 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2583 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2584 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2585 |
|
2586 |
errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH); |
2587 |
|
2588 |
if (errorstatus != SD_OK)
|
2589 |
{ |
2590 |
return(errorstatus);
|
2591 |
} |
2592 |
return(errorstatus);
|
2593 |
} |
2594 |
else
|
2595 |
{ |
2596 |
errorstatus = SD_REQUEST_NOT_APPLICABLE; |
2597 |
return(errorstatus);
|
2598 |
} |
2599 |
} /*!< If wide bus operation to be disabled */
|
2600 |
else
|
2601 |
{ |
2602 |
/*!< If requested card supports 1 bit mode operation */
|
2603 |
if ((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO) |
2604 |
{ |
2605 |
/*!< Send CMD55 APP_CMD with argument as card's RCA.*/
|
2606 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
|
2607 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; |
2608 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2609 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2610 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2611 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2612 |
|
2613 |
|
2614 |
errorstatus = CmdResp1Error(SD_CMD_APP_CMD); |
2615 |
|
2616 |
if (errorstatus != SD_OK)
|
2617 |
{ |
2618 |
return(errorstatus);
|
2619 |
} |
2620 |
|
2621 |
/*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
|
2622 |
SDIO_CmdInitStructure.SDIO_Argument = 0x00;
|
2623 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH; |
2624 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2625 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2626 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2627 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2628 |
|
2629 |
errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH); |
2630 |
|
2631 |
if (errorstatus != SD_OK)
|
2632 |
{ |
2633 |
return(errorstatus);
|
2634 |
} |
2635 |
|
2636 |
return(errorstatus);
|
2637 |
} |
2638 |
else
|
2639 |
{ |
2640 |
errorstatus = SD_REQUEST_NOT_APPLICABLE; |
2641 |
return(errorstatus);
|
2642 |
} |
2643 |
} |
2644 |
} |
2645 |
|
2646 |
/**
|
2647 |
* @brief Checks if the SD card is in programming state.
|
2648 |
* @param pstatus: pointer to the variable that will contain the SD card state.
|
2649 |
* @retval SD_Error: SD Card Error code.
|
2650 |
*/
|
2651 |
static SD_Error IsCardProgramming(uint8_t *pstatus)
|
2652 |
{ |
2653 |
SD_Error errorstatus = SD_OK; |
2654 |
__IO uint32_t respR1 = 0, status = 0; |
2655 |
|
2656 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
|
2657 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS; |
2658 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2659 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2660 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2661 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2662 |
|
2663 |
status = SDIO->STA; |
2664 |
while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)))
|
2665 |
{ |
2666 |
status = SDIO->STA; |
2667 |
} |
2668 |
|
2669 |
if (status & SDIO_FLAG_CTIMEOUT)
|
2670 |
{ |
2671 |
errorstatus = SD_CMD_RSP_TIMEOUT; |
2672 |
SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); |
2673 |
return(errorstatus);
|
2674 |
} |
2675 |
else if (status & SDIO_FLAG_CCRCFAIL) |
2676 |
{ |
2677 |
errorstatus = SD_CMD_CRC_FAIL; |
2678 |
SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); |
2679 |
return(errorstatus);
|
2680 |
} |
2681 |
|
2682 |
status = (uint32_t)SDIO_GetCommandResponse(); |
2683 |
|
2684 |
/*!< Check response received is of desired command */
|
2685 |
if (status != SD_CMD_SEND_STATUS)
|
2686 |
{ |
2687 |
errorstatus = SD_ILLEGAL_CMD; |
2688 |
return(errorstatus);
|
2689 |
} |
2690 |
|
2691 |
/*!< Clear all the static flags */
|
2692 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
2693 |
|
2694 |
|
2695 |
/*!< We have received response, retrieve it for analysis */
|
2696 |
respR1 = SDIO_GetResponse(SDIO_RESP1); |
2697 |
|
2698 |
/*!< Find out card status */
|
2699 |
*pstatus = (uint8_t) ((respR1 >> 9) & 0x0000000F); |
2700 |
|
2701 |
if ((respR1 & SD_OCR_ERRORBITS) == SD_ALLZERO)
|
2702 |
{ |
2703 |
return(errorstatus);
|
2704 |
} |
2705 |
|
2706 |
if (respR1 & SD_OCR_ADDR_OUT_OF_RANGE)
|
2707 |
{ |
2708 |
return(SD_ADDR_OUT_OF_RANGE);
|
2709 |
} |
2710 |
|
2711 |
if (respR1 & SD_OCR_ADDR_MISALIGNED)
|
2712 |
{ |
2713 |
return(SD_ADDR_MISALIGNED);
|
2714 |
} |
2715 |
|
2716 |
if (respR1 & SD_OCR_BLOCK_LEN_ERR)
|
2717 |
{ |
2718 |
return(SD_BLOCK_LEN_ERR);
|
2719 |
} |
2720 |
|
2721 |
if (respR1 & SD_OCR_ERASE_SEQ_ERR)
|
2722 |
{ |
2723 |
return(SD_ERASE_SEQ_ERR);
|
2724 |
} |
2725 |
|
2726 |
if (respR1 & SD_OCR_BAD_ERASE_PARAM)
|
2727 |
{ |
2728 |
return(SD_BAD_ERASE_PARAM);
|
2729 |
} |
2730 |
|
2731 |
if (respR1 & SD_OCR_WRITE_PROT_VIOLATION)
|
2732 |
{ |
2733 |
return(SD_WRITE_PROT_VIOLATION);
|
2734 |
} |
2735 |
|
2736 |
if (respR1 & SD_OCR_LOCK_UNLOCK_FAILED)
|
2737 |
{ |
2738 |
return(SD_LOCK_UNLOCK_FAILED);
|
2739 |
} |
2740 |
|
2741 |
if (respR1 & SD_OCR_COM_CRC_FAILED)
|
2742 |
{ |
2743 |
return(SD_COM_CRC_FAILED);
|
2744 |
} |
2745 |
|
2746 |
if (respR1 & SD_OCR_ILLEGAL_CMD)
|
2747 |
{ |
2748 |
return(SD_ILLEGAL_CMD);
|
2749 |
} |
2750 |
|
2751 |
if (respR1 & SD_OCR_CARD_ECC_FAILED)
|
2752 |
{ |
2753 |
return(SD_CARD_ECC_FAILED);
|
2754 |
} |
2755 |
|
2756 |
if (respR1 & SD_OCR_CC_ERROR)
|
2757 |
{ |
2758 |
return(SD_CC_ERROR);
|
2759 |
} |
2760 |
|
2761 |
if (respR1 & SD_OCR_GENERAL_UNKNOWN_ERROR)
|
2762 |
{ |
2763 |
return(SD_GENERAL_UNKNOWN_ERROR);
|
2764 |
} |
2765 |
|
2766 |
if (respR1 & SD_OCR_STREAM_READ_UNDERRUN)
|
2767 |
{ |
2768 |
return(SD_STREAM_READ_UNDERRUN);
|
2769 |
} |
2770 |
|
2771 |
if (respR1 & SD_OCR_STREAM_WRITE_OVERRUN)
|
2772 |
{ |
2773 |
return(SD_STREAM_WRITE_OVERRUN);
|
2774 |
} |
2775 |
|
2776 |
if (respR1 & SD_OCR_CID_CSD_OVERWRIETE)
|
2777 |
{ |
2778 |
return(SD_CID_CSD_OVERWRITE);
|
2779 |
} |
2780 |
|
2781 |
if (respR1 & SD_OCR_WP_ERASE_SKIP)
|
2782 |
{ |
2783 |
return(SD_WP_ERASE_SKIP);
|
2784 |
} |
2785 |
|
2786 |
if (respR1 & SD_OCR_CARD_ECC_DISABLED)
|
2787 |
{ |
2788 |
return(SD_CARD_ECC_DISABLED);
|
2789 |
} |
2790 |
|
2791 |
if (respR1 & SD_OCR_ERASE_RESET)
|
2792 |
{ |
2793 |
return(SD_ERASE_RESET);
|
2794 |
} |
2795 |
|
2796 |
if (respR1 & SD_OCR_AKE_SEQ_ERROR)
|
2797 |
{ |
2798 |
return(SD_AKE_SEQ_ERROR);
|
2799 |
} |
2800 |
|
2801 |
return(errorstatus);
|
2802 |
} |
2803 |
|
2804 |
/**
|
2805 |
* @brief Find the SD card SCR register value.
|
2806 |
* @param rca: selected card address.
|
2807 |
* @param pscr: pointer to the buffer that will contain the SCR value.
|
2808 |
* @retval SD_Error: SD Card Error code.
|
2809 |
*/
|
2810 |
static SD_Error FindSCR(uint16_t rca, uint32_t *pscr)
|
2811 |
{ |
2812 |
uint32_t index = 0;
|
2813 |
SD_Error errorstatus = SD_OK; |
2814 |
uint32_t tempscr[2] = {0, 0}; |
2815 |
|
2816 |
/*!< Set Block Size To 8 Bytes */
|
2817 |
/*!< Send CMD55 APP_CMD with argument as card's RCA */
|
2818 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8;
|
2819 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; |
2820 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2821 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2822 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2823 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2824 |
|
2825 |
errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); |
2826 |
|
2827 |
if (errorstatus != SD_OK)
|
2828 |
{ |
2829 |
return(errorstatus);
|
2830 |
} |
2831 |
|
2832 |
/*!< Send CMD55 APP_CMD with argument as card's RCA */
|
2833 |
SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16;
|
2834 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; |
2835 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2836 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2837 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2838 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2839 |
|
2840 |
errorstatus = CmdResp1Error(SD_CMD_APP_CMD); |
2841 |
|
2842 |
if (errorstatus != SD_OK)
|
2843 |
{ |
2844 |
return(errorstatus);
|
2845 |
} |
2846 |
SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; |
2847 |
SDIO_DataInitStructure.SDIO_DataLength = 8;
|
2848 |
SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b; |
2849 |
SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; |
2850 |
SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; |
2851 |
SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; |
2852 |
SDIO_DataConfig(&SDIO_DataInitStructure); |
2853 |
|
2854 |
|
2855 |
/*!< Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
|
2856 |
SDIO_CmdInitStructure.SDIO_Argument = 0x0;
|
2857 |
SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR; |
2858 |
SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; |
2859 |
SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; |
2860 |
SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; |
2861 |
SDIO_SendCommand(&SDIO_CmdInitStructure); |
2862 |
|
2863 |
errorstatus = CmdResp1Error(SD_CMD_SD_APP_SEND_SCR); |
2864 |
|
2865 |
if (errorstatus != SD_OK)
|
2866 |
{ |
2867 |
return(errorstatus);
|
2868 |
} |
2869 |
|
2870 |
while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))
|
2871 |
{ |
2872 |
if (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)
|
2873 |
{ |
2874 |
*(tempscr + index) = SDIO_ReadData(); |
2875 |
index++; |
2876 |
} |
2877 |
} |
2878 |
|
2879 |
if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)
|
2880 |
{ |
2881 |
SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); |
2882 |
errorstatus = SD_DATA_TIMEOUT; |
2883 |
return(errorstatus);
|
2884 |
} |
2885 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) |
2886 |
{ |
2887 |
SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); |
2888 |
errorstatus = SD_DATA_CRC_FAIL; |
2889 |
return(errorstatus);
|
2890 |
} |
2891 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) |
2892 |
{ |
2893 |
SDIO_ClearFlag(SDIO_FLAG_RXOVERR); |
2894 |
errorstatus = SD_RX_OVERRUN; |
2895 |
return(errorstatus);
|
2896 |
} |
2897 |
else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) |
2898 |
{ |
2899 |
SDIO_ClearFlag(SDIO_FLAG_STBITERR); |
2900 |
errorstatus = SD_START_BIT_ERR; |
2901 |
return(errorstatus);
|
2902 |
} |
2903 |
|
2904 |
/*!< Clear all the static flags */
|
2905 |
SDIO_ClearFlag(SDIO_STATIC_FLAGS); |
2906 |
|
2907 |
*(pscr + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) | ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24); |
2908 |
|
2909 |
*(pscr) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) | ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24); |
2910 |
|
2911 |
return(errorstatus);
|
2912 |
} |
2913 |
|
2914 |
/**
|
2915 |
* @brief DeInitializes the SDIO interface.
|
2916 |
* @param None
|
2917 |
* @retval None
|
2918 |
*/
|
2919 |
static void SD_LowLevel_DeInit(void) |
2920 |
{ |
2921 |
GPIO_InitTypeDef GPIO_InitStructure; |
2922 |
|
2923 |
/*!< Disable SDIO Clock */
|
2924 |
SDIO_ClockCmd(DISABLE); |
2925 |
|
2926 |
/*!< Set Power State to OFF */
|
2927 |
SDIO_SetPowerState(SDIO_PowerState_OFF); |
2928 |
|
2929 |
/*!< DeInitializes the SDIO peripheral */
|
2930 |
SDIO_DeInit(); |
2931 |
|
2932 |
/* Disable the SDIO APB2 Clock */
|
2933 |
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, DISABLE); |
2934 |
|
2935 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_MCO); |
2936 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_MCO); |
2937 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_MCO); |
2938 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_MCO); |
2939 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_MCO); |
2940 |
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_MCO); |
2941 |
|
2942 |
/* Configure PC.08, PC.09, PC.10, PC.11 pins: D0, D1, D2, D3 pins */
|
2943 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11; |
2944 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; |
2945 |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; |
2946 |
GPIO_Init(GPIOC, &GPIO_InitStructure); |
2947 |
|
2948 |
/* Configure PD.02 CMD line */
|
2949 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; |
2950 |
GPIO_Init(GPIOD, &GPIO_InitStructure); |
2951 |
|
2952 |
/* Configure PC.12 pin: CLK pin */
|
2953 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; |
2954 |
GPIO_Init(GPIOC, &GPIO_InitStructure); |
2955 |
} |
2956 |
|
2957 |
/**
|
2958 |
* @brief Initializes the SD Card and put it into StandBy State (Ready for
|
2959 |
* data transfer).
|
2960 |
* @param None
|
2961 |
* @retval None
|
2962 |
*/
|
2963 |
static void SD_LowLevel_Init(void) |
2964 |
{ |
2965 |
GPIO_InitTypeDef GPIO_InitStructure; |
2966 |
#if !defined (SD_POLLING_MODE)
|
2967 |
NVIC_InitTypeDef NVIC_InitStructure; |
2968 |
#endif
|
2969 |
|
2970 |
/* GPIOC and GPIOD Periph clock enable */
|
2971 |
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOD | SD_DETECT_GPIO_CLK, ENABLE); |
2972 |
|
2973 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_SDIO); |
2974 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_SDIO); |
2975 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SDIO); |
2976 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SDIO); |
2977 |
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SDIO); |
2978 |
GPIO_PinAFConfig(GPIOD, GPIO_PinSource2, GPIO_AF_SDIO); |
2979 |
|
2980 |
/* Configure PC.08, PC.09, PC.10, PC.11 pins: D0, D1, D2, D3 pins */
|
2981 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11; |
2982 |
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_25MHz; |
2983 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
2984 |
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; |
2985 |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; |
2986 |
GPIO_Init(GPIOC, &GPIO_InitStructure); |
2987 |
|
2988 |
/* Configure PD.02 CMD line */
|
2989 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; |
2990 |
GPIO_Init(GPIOD, &GPIO_InitStructure); |
2991 |
|
2992 |
/* Configure PC.12 pin: CLK pin */
|
2993 |
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; |
2994 |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; |
2995 |
GPIO_Init(GPIOC, &GPIO_InitStructure); |
2996 |
|
2997 |
/*!< Configure SD_SPI_DETECT_PIN pin: SD Card detect pin */
|
2998 |
GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN; |
2999 |
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; |
3000 |
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; |
3001 |
GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure); |
3002 |
|
3003 |
/* Enable the SDIO APB2 Clock */
|
3004 |
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SDIO, ENABLE); |
3005 |
|
3006 |
/* Enable the DMA2 Clock */
|
3007 |
RCC_AHB1PeriphClockCmd(SD_SDIO_DMA_CLK, ENABLE); |
3008 |
|
3009 |
#if !defined (SD_POLLING_MODE)
|
3010 |
/* SDIO Interrupt ENABLE */
|
3011 |
NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn; |
3012 |
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
|
3013 |
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
|
3014 |
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; |
3015 |
NVIC_Init(&NVIC_InitStructure); |
3016 |
|
3017 |
/* DMA2 STREAMx Interrupt ENABLE */
|
3018 |
NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn; |
3019 |
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
|
3020 |
NVIC_Init(&NVIC_InitStructure); |
3021 |
#endif
|
3022 |
} |
3023 |
|
3024 |
/**
|
3025 |
* @brief Configures the DMA2 Channel4 for SDIO Tx request.
|
3026 |
* @param BufferSRC: pointer to the source buffer
|
3027 |
* @param BufferSize: buffer size
|
3028 |
* @retval None
|
3029 |
*/
|
3030 |
static void SD_LowLevel_DMA_TxConfig(uint32_t *BufferSRC, uint32_t BufferSize) |
3031 |
{ |
3032 |
DMA_InitTypeDef SDDMA_InitStructure; |
3033 |
|
3034 |
DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_FEIF | SD_SDIO_DMA_FLAG_DMEIF | SD_SDIO_DMA_FLAG_TEIF | SD_SDIO_DMA_FLAG_HTIF | SD_SDIO_DMA_FLAG_TCIF); |
3035 |
|
3036 |
/* DMA2 Stream3 or Stream6 disable */
|
3037 |
DMA_Cmd(SD_SDIO_DMA_STREAM, DISABLE); |
3038 |
|
3039 |
/* DMA2 Stream3 or Stream6 Config */
|
3040 |
DMA_DeInit(SD_SDIO_DMA_STREAM); |
3041 |
|
3042 |
SDDMA_InitStructure.DMA_Channel = SD_SDIO_DMA_CHANNEL; |
3043 |
SDDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_ADDRESS; |
3044 |
SDDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)BufferSRC; |
3045 |
SDDMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; |
3046 |
SDDMA_InitStructure.DMA_BufferSize = BufferSize; |
3047 |
SDDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; |
3048 |
SDDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; |
3049 |
SDDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; |
3050 |
SDDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; |
3051 |
SDDMA_InitStructure.DMA_Mode = DMA_Mode_Normal; |
3052 |
SDDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; |
3053 |
SDDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; |
3054 |
SDDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; |
3055 |
SDDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4; |
3056 |
SDDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4; |
3057 |
DMA_Init(SD_SDIO_DMA_STREAM, &SDDMA_InitStructure); |
3058 |
DMA_ITConfig(SD_SDIO_DMA_STREAM, DMA_IT_TC, ENABLE); |
3059 |
DMA_FlowControllerConfig(SD_SDIO_DMA_STREAM, DMA_FlowCtrl_Peripheral); |
3060 |
|
3061 |
/* DMA2 Stream3 or Stream6 enable */
|
3062 |
DMA_Cmd(SD_SDIO_DMA_STREAM, ENABLE); |
3063 |
} |
3064 |
|
3065 |
/**
|
3066 |
* @brief Configures the DMA2 Channel4 for SDIO Rx request.
|
3067 |
* @param BufferDST: pointer to the destination buffer
|
3068 |
* @param BufferSize: buffer size
|
3069 |
* @retval None
|
3070 |
*/
|
3071 |
static void SD_LowLevel_DMA_RxConfig(uint32_t *BufferDST, uint32_t BufferSize) |
3072 |
{ |
3073 |
DMA_InitTypeDef SDDMA_InitStructure; |
3074 |
|
3075 |
DMA_ClearFlag(SD_SDIO_DMA_STREAM, SD_SDIO_DMA_FLAG_FEIF | SD_SDIO_DMA_FLAG_DMEIF | SD_SDIO_DMA_FLAG_TEIF | SD_SDIO_DMA_FLAG_HTIF | SD_SDIO_DMA_FLAG_TCIF); |
3076 |
|
3077 |
/* DMA2 Stream3 or Stream6 disable */
|
3078 |
DMA_Cmd(SD_SDIO_DMA_STREAM, DISABLE); |
3079 |
|
3080 |
/* DMA2 Stream3 or Stream6 Config */
|
3081 |
DMA_DeInit(SD_SDIO_DMA_STREAM); |
3082 |
|
3083 |
SDDMA_InitStructure.DMA_Channel = SD_SDIO_DMA_CHANNEL; |
3084 |
SDDMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_ADDRESS; |
3085 |
SDDMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)BufferDST; |
3086 |
SDDMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; |
3087 |
SDDMA_InitStructure.DMA_BufferSize = BufferSize; |
3088 |
SDDMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; |
3089 |
SDDMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; |
3090 |
SDDMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; |
3091 |
SDDMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; |
3092 |
SDDMA_InitStructure.DMA_Mode = DMA_Mode_Normal; |
3093 |
SDDMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; |
3094 |
SDDMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Enable; |
3095 |
SDDMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; |
3096 |
SDDMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_INC4; |
3097 |
SDDMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_INC4; |
3098 |
DMA_Init(SD_SDIO_DMA_STREAM, &SDDMA_InitStructure); |
3099 |
DMA_ITConfig(SD_SDIO_DMA_STREAM, DMA_IT_TC, ENABLE); |
3100 |
DMA_FlowControllerConfig(SD_SDIO_DMA_STREAM, DMA_FlowCtrl_Peripheral); |
3101 |
|
3102 |
/* DMA2 Stream3 or Stream6 enable */
|
3103 |
DMA_Cmd(SD_SDIO_DMA_STREAM, ENABLE); |
3104 |
} |
3105 |
|
3106 |
|
3107 |
/**
|
3108 |
* @brief This function handles SDIO global interrupt request.
|
3109 |
* @param None
|
3110 |
* @retval None
|
3111 |
*/
|
3112 |
void SDIO_IRQHandler(void) |
3113 |
{ |
3114 |
#if !defined (SD_POLLING_MODE)
|
3115 |
/* Process All SDIO Interrupt Sources */
|
3116 |
SD_ProcessIRQSrc(); |
3117 |
#endif
|
3118 |
} |
3119 |
|
3120 |
/**
|
3121 |
* @brief This function handles DMA2 Stream3 or DMA2 Stream6 global interrupts
|
3122 |
* requests.
|
3123 |
* @param None
|
3124 |
* @retval None
|
3125 |
*/
|
3126 |
void SD_SDIO_DMA_IRQHANDLER(void) |
3127 |
{ |
3128 |
#if !defined (SD_POLLING_MODE)
|
3129 |
/* Process DMA2 Stream3 or DMA2 Stream6 Interrupt Sources */
|
3130 |
SD_ProcessDMAIRQ(); |
3131 |
#endif
|
3132 |
} |
3133 |
|
3134 |
/*!
|
3135 |
* \brief initializes the sd card
|
3136 |
* \param the disk number
|
3137 |
* \return disk status
|
3138 |
*/
|
3139 |
DSTATUS disk_initialize (BYTE drv) |
3140 |
{ |
3141 |
SD_Error result = SD_Init(); |
3142 |
if (result == SD_OK) {
|
3143 |
SD_CardInfo card; |
3144 |
Stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
|
3145 |
SD_GetCardInfo(&card); |
3146 |
return RES_OK;
|
3147 |
} else {
|
3148 |
Stat = STA_NOINIT; /* Set STA_NOINIT */
|
3149 |
return RES_NOTRDY;
|
3150 |
} |
3151 |
} |
3152 |
|
3153 |
/*!
|
3154 |
* \brief get the sd cards Status
|
3155 |
* \param the disk number
|
3156 |
* \return disk status
|
3157 |
* \note Supports only single drive
|
3158 |
*/
|
3159 |
DSTATUS disk_status (BYTE drv) |
3160 |
{ |
3161 |
Stat = STA_NOINIT; |
3162 |
|
3163 |
if ((drv == 0) && (SD_GetStatus() == 0)) |
3164 |
{ |
3165 |
Stat &= ~STA_NOINIT; |
3166 |
} |
3167 |
|
3168 |
return Stat;
|
3169 |
} |
3170 |
|
3171 |
/*!
|
3172 |
* \brief reads multiple sectors from the sd Card
|
3173 |
* \param drv: disk number
|
3174 |
* \param buff: pointer to the data buffer to store read data
|
3175 |
* \param sector: start sector number (LBA)
|
3176 |
* \param count: sector count
|
3177 |
*/
|
3178 |
DRESULT disk_read (BYTE drv, BYTE *buff, DWORD sector, BYTE count) |
3179 |
{ |
3180 |
DWORD scratch [SD_BLOCKSIZE / 4]; /* Alignment ensured, need enough stack */ |
3181 |
SD_Error sdstatus = SD_OK; |
3182 |
|
3183 |
if (drv != 0) |
3184 |
{ |
3185 |
return RES_ERROR;
|
3186 |
} |
3187 |
|
3188 |
#if defined (SD_POLLING_MODE)
|
3189 |
while (count--)
|
3190 |
{ |
3191 |
sdstatus = SD_ReadBlock((BYTE *)scratch, (uint32_t )((sector + count) * SD_BLOCKSIZE), SD_BLOCKSIZE); |
3192 |
if (sdstatus != SD_OK)
|
3193 |
{ |
3194 |
return RES_ERROR;
|
3195 |
} |
3196 |
|
3197 |
while(SD_GetStatus() == SD_TRANSFER_BUSY);
|
3198 |
|
3199 |
memcpy (&buff[count * SD_BLOCKSIZE], scratch, SD_BLOCKSIZE); |
3200 |
|
3201 |
if (count == 0) |
3202 |
{ |
3203 |
return RES_OK;
|
3204 |
} |
3205 |
} |
3206 |
#else
|
3207 |
if ((DWORD)buff & 3) /* DMA Alignment issue, do single up to aligned buffer */ |
3208 |
{ |
3209 |
while (count--)
|
3210 |
{ |
3211 |
sdstatus = SD_ReadBlock((BYTE *)scratch, (uint32_t )((sector + count) * SD_BLOCKSIZE), SD_BLOCKSIZE); |
3212 |
if (sdstatus != SD_OK)
|
3213 |
{ |
3214 |
return RES_ERROR;
|
3215 |
} |
3216 |
|
3217 |
/* Check if the Transfer is finished */
|
3218 |
sdstatus = SD_WaitReadOperation(); |
3219 |
|
3220 |
while(SD_GetStatus() == SD_TRANSFER_BUSY);
|
3221 |
|
3222 |
memcpy (&buff[count * SD_BLOCKSIZE], scratch, SD_BLOCKSIZE); |
3223 |
|
3224 |
if ((sdstatus == SD_OK) && (count == 0)) |
3225 |
{ |
3226 |
return RES_OK;
|
3227 |
} |
3228 |
else if (sdstatus != SD_OK) |
3229 |
{ |
3230 |
return RES_ERROR;
|
3231 |
} |
3232 |
|
3233 |
} |
3234 |
} |
3235 |
else
|
3236 |
{ |
3237 |
sdstatus = SD_ReadMultiBlocks((BYTE *)buff, (uint32_t )(sector * SD_BLOCKSIZE), SD_BLOCKSIZE, count); |
3238 |
if (sdstatus != SD_OK)
|
3239 |
{ |
3240 |
return RES_ERROR;
|
3241 |
} |
3242 |
|
3243 |
/* Check if the Transfer is finished */
|
3244 |
sdstatus = SD_WaitReadOperation(); |
3245 |
|
3246 |
while(SD_GetStatus() == SD_TRANSFER_BUSY);
|
3247 |
|
3248 |
if (sdstatus == SD_OK)
|
3249 |
{ |
3250 |
return RES_OK;
|
3251 |
} |
3252 |
} |
3253 |
#endif /* defined (SD_POLLING_MODE) */ |
3254 |
|
3255 |
return RES_ERROR;
|
3256 |
} |
3257 |
|
3258 |
/*!
|
3259 |
* \brief write multiple sectors to the sd Card
|
3260 |
* \param drv: Physical drive nmuber (0)
|
3261 |
* \param buff: Pointer to the data to be written
|
3262 |
* \param sector: Start sector number (LBA)
|
3263 |
* \param count: Sector count (1..255)
|
3264 |
* \note this function needs DMA access and is disabled if _READONLY is set
|
3265 |
*/
|
3266 |
#if _READONLY == 0 |
3267 |
DRESULT disk_write(BYTE drv, const BYTE *buff, DWORD sector, BYTE count)
|
3268 |
{ |
3269 |
DWORD scratch [SD_BLOCKSIZE / 4]; /* Alignment ensured, need enough stack */ |
3270 |
SD_Error sdstatus = SD_OK; |
3271 |
|
3272 |
if (drv != 0) |
3273 |
{ |
3274 |
return RES_ERROR;
|
3275 |
} |
3276 |
|
3277 |
#if defined (SD_POLLING_MODE)
|
3278 |
while (count--)
|
3279 |
{ |
3280 |
memcpy (scratch, &buff[count * SD_BLOCKSIZE], SD_BLOCKSIZE); |
3281 |
|
3282 |
sdstatus = SD_WriteBlock((BYTE *)scratch, (uint32_t )((sector + count) * SD_BLOCKSIZE), SD_BLOCKSIZE); |
3283 |
|
3284 |
/* note that the SD_TX_UNDERRUN is sometimes triggered when the SDIO is configured
|
3285 |
* for polling mode (SD_POLLING_MODE), but testing shows, the block was correctly
|
3286 |
* written so no need to flag this event as an error. note that when running in
|
3287 |
* interrupt driven mode, the SD_TX_UNDERRUN interrupt is not used, which explains
|
3288 |
* why it only happens in polling mode.
|
3289 |
*/
|
3290 |
if ( (sdstatus != SD_OK) && (sdstatus != SD_TX_UNDERRUN) )
|
3291 |
{ |
3292 |
return RES_ERROR;
|
3293 |
} |
3294 |
|
3295 |
while(SD_GetStatus() == SD_TRANSFER_BUSY);
|
3296 |
|
3297 |
if (count == 0) |
3298 |
{ |
3299 |
return RES_OK;
|
3300 |
} |
3301 |
} |
3302 |
#else
|
3303 |
if ((DWORD)buff & 3) /* DMA Alignment issue, do single up to aligned buffer */ |
3304 |
{ |
3305 |
while (count--)
|
3306 |
{ |
3307 |
memcpy (scratch, &buff[count * SD_BLOCKSIZE], SD_BLOCKSIZE); |
3308 |
sdstatus = SD_WriteBlock((BYTE *)scratch, (uint32_t )((sector + count) * SD_BLOCKSIZE), SD_BLOCKSIZE); |
3309 |
if (sdstatus != SD_OK)
|
3310 |
{ |
3311 |
return RES_ERROR;
|
3312 |
} |
3313 |
|
3314 |
/* Check if the Transfer is finished */
|
3315 |
sdstatus = SD_WaitWriteOperation(); |
3316 |
|
3317 |
while(SD_GetStatus() == SD_TRANSFER_BUSY);
|
3318 |
|
3319 |
if ((sdstatus == SD_OK) && (count == 0)) |
3320 |
{ |
3321 |
return RES_OK;
|
3322 |
} |
3323 |
else if (sdstatus != SD_OK) |
3324 |
{ |
3325 |
return RES_ERROR;
|
3326 |
} |
3327 |
} |
3328 |
} |
3329 |
else
|
3330 |
{ |
3331 |
sdstatus = SD_WriteMultiBlocks((BYTE *)buff, (uint32_t )(sector * SD_BLOCKSIZE), SD_BLOCKSIZE, count); |
3332 |
if (sdstatus != SD_OK)
|
3333 |
{ |
3334 |
return RES_ERROR;
|
3335 |
} |
3336 |
|
3337 |
/* Check if the Transfer is finished */
|
3338 |
sdstatus = SD_WaitWriteOperation(); |
3339 |
|
3340 |
while(SD_GetStatus() == SD_TRANSFER_BUSY);
|
3341 |
|
3342 |
if (sdstatus == SD_OK)
|
3343 |
{ |
3344 |
return RES_OK;
|
3345 |
} |
3346 |
} |
3347 |
#endif
|
3348 |
|
3349 |
return RES_ERROR;
|
3350 |
} |
3351 |
#endif
|
3352 |
|
3353 |
/*!
|
3354 |
* \brief ioctl implementation
|
3355 |
* \param drv: Physical drive nmuber (0)
|
3356 |
* \param ctrl: Control code
|
3357 |
* \param buff: Buffer to send/receive control data
|
3358 |
* \return operation result
|
3359 |
* \note some ioctl's are not implemented because there is no need for them
|
3360 |
* or the API's differes
|
3361 |
*/
|
3362 |
DRESULT disk_ioctl(BYTE drv, BYTE ctrl, void *buff)
|
3363 |
{ |
3364 |
DRESULT res; |
3365 |
SD_CardInfo SDCardInfo; |
3366 |
|
3367 |
res = RES_ERROR; |
3368 |
|
3369 |
if (Stat & STA_NOINIT) return RES_NOTRDY; |
3370 |
|
3371 |
switch (ctrl) {
|
3372 |
case CTRL_SYNC : /* Make sure that no pending write process */ |
3373 |
res = RES_OK; |
3374 |
break;
|
3375 |
|
3376 |
case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */ |
3377 |
if(drv == 0) |
3378 |
{ |
3379 |
SD_GetCardInfo(&SDCardInfo); |
3380 |
*(DWORD*)buff = SDCardInfo.CardCapacity / SD_BLOCKSIZE; |
3381 |
} |
3382 |
res = RES_OK; |
3383 |
break;
|
3384 |
|
3385 |
case GET_SECTOR_SIZE : /* Get R/W sector size (WORD) */ |
3386 |
*(WORD*)buff = SD_BLOCKSIZE; |
3387 |
res = RES_OK; |
3388 |
break;
|
3389 |
|
3390 |
case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */ |
3391 |
if(drv == 0) |
3392 |
*(DWORD*)buff = 32;
|
3393 |
res = RES_OK; |
3394 |
break;
|
3395 |
|
3396 |
default:
|
3397 |
res = RES_PARERR; |
3398 |
} |
3399 |
return res;
|
3400 |
} |
3401 |
|
3402 |
/*!
|
3403 |
* \brief user provided timer function for FatFs module
|
3404 |
*/
|
3405 |
DWORD get_fattime(void)
|
3406 |
{ |
3407 |
/* No RTC supprt. Return a fixed value 2013/5/10 0:00:00 */
|
3408 |
return ((DWORD)(2013 - 1980) << 25) /* Y */ |
3409 |
| ((DWORD)5 << 21) /* M */ |
3410 |
| ((DWORD)10 << 16) /* D */ |
3411 |
| ((DWORD)0 << 11) /* H */ |
3412 |
| ((DWORD)0 << 5) /* M */ |
3413 |
| ((DWORD)0 >> 1); /* S */ |
3414 |
} |
3415 |
|
3416 |
/********************************* end of mmc.c ****************************************/
|
3417 |
|