1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
/**********************************************************************
* $Id$ lpc_mci.h 2011-06-02
*//**
* @file lpc_mci.h
* @brief Contains all macro definitions and function prototypes
* support for MCI firmware library on LPC
* @version 2.0
* @date 29. June. 2011
* @author NXP MCU SW Application Team
*
* Copyright(C) 2011, NXP Semiconductor
* All rights reserved.
*
***********************************************************************
* Software that is described herein is for illustrative purposes only
* which provides customers with programming information regarding the
* products. This software is supplied "AS IS" without any warranties.
* NXP Semiconductors assumes no responsibility or liability for the
* use of the software, conveys no license or title under any patent,
* copyright, or mask work right to the product. NXP Semiconductors
* reserves the right to make changes in the software without
* notification. NXP Semiconductors also make no representation or
* warranty that such application will be suitable for the specified
* use without further testing or modification.
* Permission to use, copy, modify, and distribute this software and its
* documentation is hereby granted, under NXP Semiconductors'
* relevant copyright in the software, without fee, provided that it
* is used in conjunction with NXP Semiconductors microcontrollers. This
* copyright, permission, and disclaimer notice must appear in all copies of
* this code.
**********************************************************************/
/* Peripheral group ----------------------------------------------------------- */
/** @defgroup MCI MCI (Multimedia Card Interface)
* @ingroup LPC_CMSIS_FwLib_Drivers
* @{
*/
#ifndef __LPC_MCI_H_
#define __LPC_MCI_H_
/** @defgroup MCI_Public_Macros MCI Public Macros
* @{
*/
#define MCI_DMA_ENABLED (1)
#define HIGH_LVL (1)
#define LOW_LVL (0)
/* SD/MMC Command list, per MMC spec. SD Memory Card Spec. Simplified version */
/* GO_IDLE_STATE(MMC) or RESET(SD) */
#define CMD0_GO_IDLE_STATE (0)
/* SEND_OP_COND(MMC) or ACMD41(SD) */
#define CMD1_SEND_OP_COND (1)
/* ALL_SEND_CID */
#define CMD2_ALL_SEND_CID (2)
/* SET_RELATE_ADDR */
#define CMD3_SET_RELATIVE_ADDR (3)
/* Set Bus-Width 1 bit or 4 bits */
#define ACMD6_SET_BUS_WIDTH (6)
/* SELECT/DESELECT_CARD */
#define CMD7_SELECT_CARD (7)
/**Sending interface condition cmd */
#define CMD8_SEND_IF_COND (8)
/* SEND_CSD */
#define CMD9_SEND_CSD (9)
/* Stop either READ or WRITE operation */
#define CMD12_STOP_TRANSMISSION 12
/* SEND_STATUS */
#define CMD13_SEND_STATUS (13)
/* SET_BLOCK_LEN */
#define CMD16_SET_BLOCK_LEN (16)
/* READ_SINGLE_BLOCK */
#define CMD17_READ_SINGLE_BLOCK (17)
/* READ_MULTIPLE_BLOCK */
#define CMD18_READ_MULTIPLE_BLOCK (18)
/* WRITE_BLOCK */
#define CMD24_WRITE_BLOCK (24)
/* WRITE_MULTIPLE_BLOCK */
#define CMD25_WRITE_MULTIPLE_BLOCK (25)
/* Start erase block number*/
#define CMD32_ERASE_WR_BLK_START (32)
/* End erase block number*/
#define CMD33_ERASE_WR_BLK_END (33)
/* Start erase*/
#define CMD38_ERASE (38)
/* ACMD41 for SD card */
#define ACMD41_SEND_APP_OP_COND (41)
/* APP_CMD, the following will a ACMD */
#define CMD55_APP_CMD (55)
#define OCR_INDEX (0x00FF8000)
#define RCA_ARGUMENT_POS (16)
#define RCA_ARGUMENT_MASK (0xFFFF)
/* Card Status (coded in 32 bits) in R1 & R1b Response */
#define CARD_STATUS_OUT_OF_RANGE ( 1 << 31)
#define CARD_STATUS_ADDRESS_ERROR ( 1 << 30)
#define CARD_STATUS_BLOCK_LEN_ERROR ( 1 << 29)
#define CARD_STATUS_ERASE_SEQ_ERROR ( 1 << 28)
#define CARD_STATUS_ERASE_PARAM_ERROR ( 1 << 27)
#define CARD_STATUS_WP_VIOLATION ( 1 << 26)
#define CARD_STATUS_CARD_IS_LOCKED ( 1 << 25)
#define CARD_STATUS_COM_CRC_ERROR ( 1 << 23)
#define CARD_STATUS_ILLEGAL_COMMAND ( 1 << 22)
#define CARD_STATUS_CARD_ECC_FAILED ( 1 << 21)
#define CARD_STATUS_CC_ERROR ( 1 << 20)
#define CARD_STATUS_GEN_ERROR ( 1 << 19)
#define CARD_STATUS_CSD_OVERWRITE ( 1 << 16)
#define CARD_STATUS_WP_ERASE_SKIP ( 1 << 15)
#define CARD_STATUS_CARD_ECC_DISABLED ( 1 << 14)
#define CARD_STATUS_ERASE_RESET ( 1 << 13)
#define CARD_STATUS_READY_FOR_DATA ( 1 << 8)
#define CARD_STATUS_ACMD_ENABLE ( 1 << 5)
#define CARD_STATUS_ERR_MASK (0xFDF88008)
#define CARDSTATEOF(x) ((x>>9) & 0x0F)
#define CARD_STATE_IDLE (0)
#define CARD_STATE_READY (1)
#define CARD_STATE_IDENT (2)
#define CARD_STATE_STBY (3)
#define CARD_STATE_TRAN (4)
#define CARD_STATE_DATA (5)
#define CARD_STATE_RCV (6)
#define CARD_STATE_PRG (7)
#define CARD_STATE_DIS (8)
/* CID in R2 reponse (Code length is 136 bits) */
#define MCI_CID_MANUFACTURER_ID_WPOS (24) //pos in word 0
#define MCI_CID_MANUFACTURER_ID_WBMASK (0xFF)
#define MCI_CID_OEMAPPLICATION_ID_WPOS (8) //pos in word 0
#define MCI_CID_OEMAPPLICATION_ID_WBMASK (0xFFFF)
#define MCI_CID_PRODUCTNAME_ID_H_WPOS (0) //pos in word 0
#define MCI_CID_PRODUCTNAME_ID_H_WBMASK (0xFF)
#define MCI_CID_PRODUCTNAME_ID_L_WPOS (0) //pos in word 1
#define MCI_CID_PRODUCTNAME_ID_L_WBMASK (0xFFFFFFFF)
#define MCI_CID_PRODUCTREVISION_ID_WPOS (24) //pos in word 2
#define MCI_CID_PRODUCTREVISION_ID_WBMASK (0xFF)
#define MCI_CID_PRODUCTSERIALNUM_ID_H_WPOS (0) //pos in word 2
#define MCI_CID_PRODUCTSERIALNUM_ID_H_WBMASK (0x00FFFFFF)
#define MCI_CID_PRODUCTSERIALNUM_ID_L_WPOS (24) //pos in word 3
#define MCI_CID_PRODUCTSERIALNUM_ID_L_WBMASK (0xFF)
#define MCI_CID_PRODUCTSERIALNUM_ID_WBMASK (0xFFFFFFFF)
#define MCI_CID_RESERVED_ID_WPOS (20) //pos in word 3
#define MCI_CID_RESERVED_ID_WBMASK (0x1F)
#define MCI_CID_MANUFACTURINGDATE_ID_WPOS (8) //in word 3
#define MCI_CID_MANUFACTURINGDATE_ID_WBMASK (0x0FFF)
#define MCI_CID_CHECKSUM_ID_WPOS (1) //in word 3
#define MCI_CID_CHECKSUM_ID_WBMASK (0x7F)
#define MCI_CID_UNUSED_ID_WPOS (0) //in word 3
#define MCI_CID_UNUSED_ID_WBMASK (0x01)
/* R6 (Published RCA response) */
#define RCA_RES_CARD_STATUS_POS (0)
#define RCA_RES_CARD_STATUS_MASK (0xFFFF)
#define RCA_RES_NEW_PUBLISHED_RCA_POS (16)
#define RCA_RES_NEW_PUBLISHED_RCA_MASK (0xFFFF)
/* R7 (Card interface condition) */
#define MCI_CMD8_VOLTAGESUPPLIED_POS (8)
#define MCI_CMD8_VOLTAGESUPPLIED_BMASK (0x0F)
#define MCI_CMD8_VOLATAGESUPPLIED_NOT_DEFINED (0)
#define MCI_CMD8_VOLATAGESUPPLIED_27_36 (1) /*2.7 - 3.6V*/
#define MCI_CMD8_CHECKPATTERN_POS (0)
#define MCI_CMD8_CHECKPATTERN_BMASK (0xFF)
#define MCI_SLOW_RATE (400000) /* 400KHz */
#define MCI_NORMAL_RATE (20000000) /* 20MHz */
#define SD_1_BIT (0)
#define SD_4_BIT (1)
#define DATA_TIMER_VALUE_R (MCI_NORMAL_RATE/4) // 250ms
#define DATA_TIMER_VALUE_W (MCI_NORMAL_RATE) // 1000ms
#define DATA_RW_MAX_LEN (0xFFFF)
#define EXPECT_NO_RESP (0)
#define EXPECT_SHORT_RESP (1)
#define EXPECT_LONG_RESP (2)
#define MCI_OUTPUT_MODE_PUSHPULL (0)
#define MCI_OUTPUT_MODE_OPENDRAIN (1)
#define NOT_ALLOW_CMD_TIMER (0)
#define ALLOW_CMD_TIMER (1)
#define MCI_DISABLE_CMD_TIMER (1<<8)
/* For the SD card I tested, the minimum block length is 512 */
/* For MMC, the restriction is loose, due to the variety of SD and MMC
card support, ideally, the driver should read CSD register to find the
speed and block length for the card, and set them accordingly. */
/* In this driver example, it will support both MMC and SD cards, it
does read the information by send SEND_CSD to poll the card status,
but, it doesn't configure them accordingly. this is not intended to
support all the SD and MMC card. */
/* DATA_BLOCK_LEN table
DATA_BLOCK_LEN Actual Size( BLOCK_LENGTH )
11 2048
10 1024
9 512
8 256
7 128
6 64
5 32
4 16
3 8
2 4
1 2
*/
/* This is the size of the buffer of origin data */
#define MCI_DMA_SIZE (1000UL)
/* This is the area original data is stored or data to be written to the SD/MMC card. */
#define MCI_DMA_SRC_ADDR LPC_PERI_RAM_BASE
/* This is the area, after reading from the SD/MMC*/
#define MCI_DMA_DST_ADDR (MCI_DMA_SRC_ADDR + MCI_DMA_SIZE)
/* To simplify the programming, please note that, BLOCK_LENGTH is a multiple
of FIFO_SIZE */
#define DATA_BLOCK_LEN (9) /* Block size field in DATA_CTRL */
#define BLOCK_LENGTH (1 << DATA_BLOCK_LEN)
/* for SD card, 128, the size of the flash */
/* card is 512 * 128 = 64K */
#define BLOCK_NUM 0x80
#define FIFO_SIZE 16
#define BUS_WIDTH_1BIT 0
#define BUS_WIDTH_4BITS 10
/* MCI Status register bit information */
#define MCI_CMD_CRC_FAIL (1 << 0)
#define MCI_DATA_CRC_FAIL (1 << 1)
#define MCI_CMD_TIMEOUT (1 << 2)
#define MCI_DATA_TIMEOUT (1 << 3)
#define MCI_TX_UNDERRUN (1 << 4)
#define MCI_RX_OVERRUN (1 << 5)
#define MCI_CMD_RESP_END (1 << 6)
#define MCI_CMD_SENT (1 << 7)
#define MCI_DATA_END (1 << 8)
#define MCI_START_BIT_ERR (1 << 9)
#define MCI_DATA_BLK_END (1 << 10)
#define MCI_CMD_ACTIVE (1 << 11)
#define MCI_TX_ACTIVE (1 << 12)
#define MCI_RX_ACTIVE (1 << 13)
#define MCI_TX_HALF_EMPTY (1 << 14)
#define MCI_RX_HALF_FULL (1 << 15)
#define MCI_TX_FIFO_FULL (1 << 16)
#define MCI_RX_FIFO_FULL (1 << 17)
#define MCI_TX_FIFO_EMPTY (1 << 18)
#define MCI_RX_FIFO_EMPTY (1 << 19)
#define MCI_TX_DATA_AVAIL (1 << 20)
#define MCI_RX_DATA_AVAIL (1 << 21)
/***********************************************************************
* MCI Data control register definitions
**********************************************************************/
/** Data transfer enable */
#define MCI_DATACTRL_ENABLE_POS (0)
#define MCI_DATACTRL_ENABLE_MASK (0x01)
#define MCI_DATACTRL_ENABLE (1 << MCI_DATACTRL_ENABLE_POS)
#define MCI_DATACTRL_DISABLE (0 << MCI_DATACTRL_ENABLE_POS)
/** Data transfer direction */
#define MCI_DATACTRL_DIR_POS (1)
#define MCI_DATACTRL_DIR_MASK (0x01)
#define MCI_DATACTRL_DIR_FROM_CARD (1 << MCI_DATACTRL_DIR_POS)
#define MCI_DATACTRL_DIR_TO_CARD (0 << MCI_DATACTRL_DIR_POS)
/** Data transfer mode */
#define MCI_DATACTRL_XFER_MODE_POS (2)
#define MCI_DATACTRL_XFER_MODE_MASK (0x01)
#define MCI_DATACTRL_XFER_MODE_STREAM (1 << MCI_DATACTRL_XFER_MODE_POS)
#define MCI_DATACTRL_XFER_MODE_BLOCK (0 << MCI_DATACTRL_XFER_MODE_POS)
/** Enable DMA */
#define MCI_DATACTRL_DMA_ENABLE_POS (3)
#define MCI_DATACTRL_DMA_ENABLE_MASK (0x01)
#define MCI_DATACTRL_DMA_ENABLE (1 << MCI_DATACTRL_DMA_ENABLE_POS)
#define MCI_DATACTRL_DMA_DISABLE (0 << MCI_DATACTRL_DMA_ENABLE_POS)
/** Data block length macro */
#define MCI_DTATCTRL_BLOCKSIZE(n) _SBF(4, (n & 0xF))
#define CMD_INT_MASK (MCI_CMD_CRC_FAIL | MCI_CMD_TIMEOUT | MCI_CMD_RESP_END \
| MCI_CMD_SENT | MCI_CMD_ACTIVE)
#define DATA_ERR_INT_MASK (MCI_DATA_CRC_FAIL | MCI_DATA_TIMEOUT | MCI_TX_UNDERRUN \
| MCI_RX_OVERRUN | MCI_START_BIT_ERR)
#define ACTIVE_INT_MASK ( MCI_TX_ACTIVE | MCI_RX_ACTIVE)
#define FIFO_INT_MASK (MCI_TX_HALF_EMPTY | MCI_RX_HALF_FULL \
| MCI_TX_FIFO_FULL | MCI_RX_FIFO_FULL \
| MCI_TX_FIFO_EMPTY | MCI_RX_FIFO_EMPTY \
| MCI_DATA_BLK_END )
#define FIFO_TX_INT_MASK (MCI_TX_HALF_EMPTY )
#define FIFO_RX_INT_MASK (MCI_RX_HALF_FULL )
#define DATA_END_INT_MASK (MCI_DATA_END | MCI_DATA_BLK_END)
#define ERR_TX_INT_MASK (MCI_DATA_CRC_FAIL | MCI_DATA_TIMEOUT | MCI_TX_UNDERRUN | MCI_START_BIT_ERR)
#define ERR_RX_INT_MASK (MCI_DATA_CRC_FAIL | MCI_DATA_TIMEOUT | MCI_RX_OVERRUN | MCI_START_BIT_ERR)
/* Error code on the command response. */
#define INVALID_RESPONSE 0xFFFFFFFF
/**
* @}
*/
/** @defgroup MCI_Public_Types MCI Public Types
* @{
*/
typedef enum mci_card_state
{
MCI_CARDSTATE_IDLE = 0,
MCI_CARDSTATE_READY,
MCI_CARDSTATE_IDENDTIFIED,
MCI_CARDSTATE_STBY,
MCI_CARDSTATE_TRAN,
MCI_CARDSTATE_DATA,
MCI_CARDSTATE_RCV,
MCI_CARDSTATE_PRG,
MCI_CARDSTATE_DIS,
}en_Mci_CardState;
typedef enum mci_func_error
{
MCI_FUNC_OK = 0,
MCI_FUNC_FAILED = -1,
MCI_FUNC_BAD_PARAMETERS = -2,
MCI_FUNC_BUS_NOT_IDLE = -3,
MCI_FUNC_TIMEOUT = -3,
MCI_FUNC_ERR_STATE = -4,
MCI_FUNC_NOT_READY = -5,
}en_Mci_Func_Error;
typedef enum mci_card_type
{
MCI_SDHC_SDXC_CARD = 3,
MCI_SDSC_V2_CARD = 2,
MCI_MMC_CARD = 1,
MCI_SDSC_V1_CARD = 0,
MCI_CARD_UNKNOWN = -1,
}en_Mci_CardType;
typedef struct mci_cid
{
/** Manufacturer ID */
uint8_t MID;
/** OEM/Application ID */
uint16_t OID;
/** Product name 8-bits higher */
uint8_t PNM_H;
/** Product name 32-bits Lower */
uint32_t PNM_L;
/** Product revision */
uint8_t PRV;
/** Product serial number */
uint32_t PSN;
/** reserved: 4 bit */
uint8_t reserved;
/** Manufacturing date: 12 bit */
uint16_t MDT;
/** CRC7 checksum: 7 bit */
uint8_t CRC;
/** not used, always: 1 bit always 1 */
uint8_t unused;
} st_Mci_CardId;
typedef struct cmd_info{
/** Command ID*/
uint32_t CmdIndex;
/** Command Argument*/
uint32_t Argument;
/** Expected response: no response, short response or long response */
uint32_t ExpectResp;
/** Allow timeout */
uint32_t AllowTimeout;
/** Command Response Info */
uint32_t *CmdResp;
} st_Mci_CmdInfo;
/**
* @}
*/
/** @defgroup MCI_Public_Functions MCI Public Functions
* @{
*/
int32_t MCI_Init(uint8_t powerActiveLevel );
void MCI_SendCmd( st_Mci_CmdInfo* pCmdIf );
int32_t MCI_GetCmdResp( uint32_t CmdIndex, uint32_t NeedRespFlag, uint32_t *CmdRespStatus );
int32_t MCI_CmdResp(st_Mci_CmdInfo *pCmdIf);
void MCI_Set_MCIClock( uint32_t clockrate );
int32_t MCI_SetBusWidth( uint32_t width );
int32_t MCI_Acmd_SendOpCond(uint8_t hcsVal);
int32_t MCI_CardInit( void );
en_Mci_CardType MCI_GetCardType(void);
int32_t MCI_CardReset( void );
int32_t MCI_Cmd_SendIfCond(void);
int32_t MCI_GetCID(st_Mci_CardId* cidValue);
int32_t MCI_SetCardAddress( void );
uint32_t MCI_GetCardAddress(void);
int32_t MCI_GetCSD(uint32_t* csdVal);
int32_t MCI_Cmd_SelectCard( void );
int32_t MCI_GetCardStatus(int32_t* cardStatus);
uint32_t MCI_GetDataXferEndState(void);
uint32_t MCI_GetXferErrState(void);
int32_t MCI_SetBlockLen( uint32_t blockLength );
int32_t MCI_Acmd_SendBusWidth( uint32_t buswidth );
int32_t MCI_Cmd_StopTransmission( void );
int32_t MCI_Cmd_WriteBlock(uint32_t blockNum, uint32_t numOfBlock);
int32_t MCI_Cmd_ReadBlock(uint32_t blockNum, uint32_t numOfBlock);
int32_t MCI_WriteBlock(volatile uint8_t* memblock, uint32_t blockNum, uint32_t numOfBlock);
int32_t MCI_ReadBlock(volatile uint8_t* destBlock, uint32_t blockNum, uint32_t numOfBlock);
#if MCI_DMA_ENABLED
void MCI_DMA_IRQHandler (void);
#endif
/**
* @}
*/
#endif /* end __LPC_MCI_H_ */
/**
* @}
*/
/****************************************************************************
** End Of File
****************************************************************************/
|