Statistics
| Branch: | Tag: | Revision:

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)