Line No. | Rev | Author | Line |
---|---|---|---|
1 | 32 | kaklik | /****************************************************************************** |
2 | * |
||
3 | * Microchip Memory Disk Drive File System |
||
4 | * |
||
5 | ****************************************************************************** |
||
6 | * FileName: SD-SPI.h |
||
7 | * Dependencies: GenericTypeDefs.h |
||
8 | * FSconfig.h |
||
9 | * FSDefs.h |
||
10 | * Processor: PIC18/PIC24/dsPIC30/dsPIC33/PIC32 |
||
11 | * Compiler: C18/C30/C32 |
||
12 | * Company: Microchip Technology, Inc. |
||
13 | * |
||
14 | * Software License Agreement |
||
15 | * |
||
16 | * The software supplied herewith by Microchip Technology Incorporated |
||
17 | * (the Company) for its PICmicro® Microcontroller is intended and |
||
18 | * supplied to you, the Companys customer, for use solely and |
||
19 | * exclusively on Microchip PICmicro Microcontroller products. The |
||
20 | * software is owned by the Company and/or its supplier, and is |
||
21 | * protected under applicable copyright laws. All rights are reserved. |
||
22 | * Any use in violation of the foregoing restrictions may subject the |
||
23 | * user to criminal sanctions under applicable laws, as well as to |
||
24 | * civil liability for the breach of the terms and conditions of this |
||
25 | * license. |
||
26 | * |
||
27 | * THIS SOFTWARE IS PROVIDED IN AN AS IS CONDITION. NO WARRANTIES, |
||
28 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED |
||
29 | * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A |
||
30 | * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, |
||
31 | * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR |
||
32 | * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. |
||
33 | * |
||
34 | *****************************************************************************/ |
||
35 | //DOM-IGNORE-BEGIN |
||
36 | /******************************************************************** |
||
37 | Change History: |
||
38 | Rev Description |
||
39 | ---- ----------------------- |
||
40 | 1.2.4 - 1.2.6 No Change |
||
41 | ********************************************************************/ |
||
42 | //DOM-IGNORE-END |
||
43 | |||
44 | #ifndef SDMMC_H |
||
45 | #define SDMMC_H |
||
46 | |||
47 | #include "GenericTypeDefs.h" |
||
48 | #include "FSconfig.h" |
||
49 | #include "MDD File System\FSDefs.h" |
||
50 | |||
51 | |||
52 | #ifdef __18CXX |
||
53 | // Description: This macro is used to initialize a PIC18 SPI module with a 4x prescale divider |
||
54 | #define SYNC_MODE_FAST 0x00 |
||
55 | // Description: This macro is used to initialize a PIC18 SPI module with a 16x prescale divider |
||
56 | #define SYNC_MODE_MED 0x01 |
||
57 | // Description: This macro is used to initialize a PIC18 SPI module with a 64x prescale divider |
||
58 | #define SYNC_MODE_SLOW 0x02 |
||
59 | #elif defined __PIC32MX__ |
||
60 | // Description: This macro is used to initialize a PIC32 SPI module |
||
61 | #define SYNC_MODE_FAST 0x3E |
||
62 | // Description: This macro is used to initialize a PIC32 SPI module |
||
63 | #define SYNC_MODE_SLOW 0x3C |
||
64 | #else |
||
65 | // Description: This macro indicates the SPI enable bit for 16-bit PICs |
||
66 | #ifndef MASTER_ENABLE_ON |
||
67 | #define MASTER_ENABLE_ON 0x0020 |
||
68 | #endif |
||
69 | |||
70 | // Description: This macro is used to initialize a 16-bit PIC SPI module |
||
71 | #ifndef SYNC_MODE_FAST |
||
72 | #define SYNC_MODE_FAST 0x3E |
||
73 | #endif |
||
74 | // Description: This macro is used to initialize a 16-bit PIC SPI module |
||
75 | #ifndef SYNC_MODE_SLOW |
||
76 | #define SYNC_MODE_SLOW 0x3C |
||
77 | #endif |
||
78 | |||
79 | // Description: This macro is used to initialize a 16-bit PIC SPI module secondary prescaler |
||
80 | #ifndef SEC_PRESCAL_1_1 |
||
81 | #define SEC_PRESCAL_1_1 0x001c |
||
82 | #endif |
||
83 | // Description: This macro is used to initialize a 16-bit PIC SPI module primary prescaler |
||
84 | #ifndef PRI_PRESCAL_1_1 |
||
85 | #define PRI_PRESCAL_1_1 0x0003 |
||
86 | #endif |
||
87 | #endif |
||
88 | |||
89 | |||
90 | |||
91 | /*****************************************************************/ |
||
92 | /* Strcutures and defines */ |
||
93 | /*****************************************************************/ |
||
94 | |||
95 | |||
96 | // Description: This macro represents an SD card start token |
||
97 | #define DATA_START_TOKEN 0xFE |
||
98 | |||
99 | // Description: This macro represents an SD card data accepted token |
||
100 | #define DATA_ACCEPTED 0x05 |
||
101 | |||
102 | // Description: This macro indicates that the SD card expects to transmit or receive more data |
||
103 | #define MOREDATA !0 |
||
104 | |||
105 | // Description: This macro indicates that the SD card does not expect to transmit or receive more data |
||
106 | #define NODATA 0 |
||
107 | |||
108 | // Description: This macro represents a floating SPI bus condition |
||
109 | #define MMC_FLOATING_BUS 0xFF |
||
110 | |||
111 | // Description: This macro represents a bad SD card response byte |
||
112 | #define MMC_BAD_RESPONSE MMC_FLOATING_BUS |
||
113 | |||
114 | // The SDMMC Commands |
||
115 | |||
116 | // Description: This macro defines the command code to reset the SD card |
||
117 | #define cmdGO_IDLE_STATE 0 |
||
118 | // Description: This macro defines the command code to initialize the SD card |
||
119 | #define cmdSEND_OP_COND 1 |
||
120 | // Description: This macro defined the command code to check for sector addressing |
||
121 | #define cmdSEND_IF_COND 8 |
||
122 | // Description: This macro defines the command code to get the Card Specific Data |
||
123 | #define cmdSEND_CSD 9 |
||
124 | // Description: This macro defines the command code to get the Card Information |
||
125 | #define cmdSEND_CID 10 |
||
126 | // Description: This macro defines the command code to stop transmission during a multi-block read |
||
127 | #define cmdSTOP_TRANSMISSION 12 |
||
128 | // Description: This macro defines the command code to get the card status information |
||
129 | #define cmdSEND_STATUS 13 |
||
130 | // Description: This macro defines the command code to set the block length of the card |
||
131 | #define cmdSET_BLOCKLEN 16 |
||
132 | // Description: This macro defines the command code to read one block from the card |
||
133 | #define cmdREAD_SINGLE_BLOCK 17 |
||
134 | // Description: This macro defines the command code to read multiple blocks from the card |
||
135 | #define cmdREAD_MULTI_BLOCK 18 |
||
136 | // Description: This macro defines the command code to write one block to the card |
||
137 | #define cmdWRITE_SINGLE_BLOCK 24 |
||
138 | // Description: This macro defines the command code to write multiple blocks to the card |
||
139 | #define cmdWRITE_MULTI_BLOCK 25 |
||
140 | // Description: This macro defines the command code to set the address of the start of an erase operation |
||
141 | #define cmdTAG_SECTOR_START 32 |
||
142 | // Description: This macro defines the command code to set the address of the end of an erase operation |
||
143 | #define cmdTAG_SECTOR_END 33 |
||
144 | // Description: This macro defines the command code to erase all previously selected blocks |
||
145 | #define cmdERASE 38 |
||
146 | // Description: This macro defines the command code to begin application specific command inputs |
||
147 | #define cmdAPP_CMD 55 |
||
148 | // Description: This macro defines the command code to get the OCR register information from the card |
||
149 | #define cmdREAD_OCR 58 |
||
150 | // Description: This macro defines the command code to disable CRC checking |
||
151 | #define cmdCRC_ON_OFF 59 |
||
152 | |||
153 | |||
154 | // Description: Enumeration of different SD response types |
||
155 | typedef enum |
||
156 | { |
||
157 | R1, // R1 type response |
||
158 | R1b, // R1b type response |
||
159 | R2, // R2 type response |
||
160 | R3, // R3 type response |
||
161 | R7 // R7 type response |
||
162 | }RESP; |
||
163 | |||
164 | // Summary: SD card command data structure |
||
165 | // Description: The typMMC_CMD structure is used to create a command table of information needed for each relevant SD command |
||
166 | typedef struct |
||
167 | { |
||
168 | BYTE CmdCode; // The command code |
||
169 | BYTE CRC; // The CRC value for that command |
||
170 | RESP responsetype; // The response type |
||
171 | BYTE moredataexpected; // Set to MOREDATA or NODATA, depending on whether more data is expected or not |
||
172 | } typMMC_CMD; |
||
173 | |||
174 | |||
175 | // Summary: An SD command packet |
||
176 | // Description: This union represents different ways to access an SD card command packet |
||
177 | typedef union |
||
178 | { |
||
179 | // This structure allows array-style access of command bytes |
||
180 | struct |
||
181 | { |
||
182 | #ifdef __18CXX |
||
183 | BYTE field[6]; // BYTE array |
||
184 | #else |
||
185 | BYTE field[7]; |
||
186 | #endif |
||
187 | }; |
||
188 | // This structure allows byte-wise access of packet command bytes |
||
189 | struct |
||
190 | { |
||
191 | BYTE crc; // The CRC byte |
||
192 | #if defined __C30__ |
||
193 | BYTE c30filler; // Filler space (since bitwise declarations can't cross a WORD boundary) |
||
194 | #elif defined __C32__ |
||
195 | BYTE c32filler[3]; // Filler space (since bitwise declarations can't cross a DWORD boundary) |
||
196 | #endif |
||
197 | |||
198 | BYTE addr0; // Address byte 0 |
||
199 | BYTE addr1; // Address byte 1 |
||
200 | BYTE addr2; // Address byte 2 |
||
201 | BYTE addr3; // Address byte 3 |
||
202 | BYTE cmd; // Command code byte |
||
203 | }; |
||
204 | // This structure allows bitwise access to elements of the command bytes |
||
205 | struct |
||
206 | { |
||
207 | BYTE END_BIT:1; // Packet end bit |
||
208 | BYTE CRC7:7; // CRC value |
||
209 | DWORD address; // Address |
||
210 | BYTE CMD_INDEX:6; // Command code |
||
211 | BYTE TRANSMIT_BIT:1; // Transmit bit |
||
212 | BYTE START_BIT:1; // Packet start bit |
||
213 | }; |
||
214 | } CMD_PACKET; |
||
215 | |||
216 | |||
217 | // Summary: The format of an R1 type response |
||
218 | // Description: This union represents different ways to access an SD card R1 type response packet. |
||
219 | typedef union |
||
220 | { |
||
221 | BYTE _byte; // Byte-wise access |
||
222 | // This structure allows bitwise access of the response |
||
223 | struct |
||
224 | { |
||
225 | unsigned IN_IDLE_STATE:1; // Card is in idle state |
||
226 | unsigned ERASE_RESET:1; // Erase reset flag |
||
227 | unsigned ILLEGAL_CMD:1; // Illegal command flag |
||
228 | unsigned CRC_ERR:1; // CRC error flag |
||
229 | unsigned ERASE_SEQ_ERR:1; // Erase sequence error flag |
||
230 | unsigned ADDRESS_ERR:1; // Address error flag |
||
231 | unsigned PARAM_ERR:1; // Parameter flag |
||
232 | unsigned B7:1; // Unused bit 7 |
||
233 | }; |
||
234 | } RESPONSE_1; |
||
235 | |||
236 | // Summary: The format of an R2 type response |
||
237 | // Description: This union represents different ways to access an SD card R2 type response packet |
||
238 | typedef union |
||
239 | { |
||
240 | WORD _word; |
||
241 | struct |
||
242 | { |
||
243 | BYTE _byte0; |
||
244 | BYTE _byte1; |
||
245 | }; |
||
246 | struct |
||
247 | { |
||
248 | unsigned IN_IDLE_STATE:1; |
||
249 | unsigned ERASE_RESET:1; |
||
250 | unsigned ILLEGAL_CMD:1; |
||
251 | unsigned CRC_ERR:1; |
||
252 | unsigned ERASE_SEQ_ERR:1; |
||
253 | unsigned ADDRESS_ERR:1; |
||
254 | unsigned PARAM_ERR:1; |
||
255 | unsigned B7:1; |
||
256 | unsigned CARD_IS_LOCKED:1; |
||
257 | unsigned WP_ERASE_SKIP_LK_FAIL:1; |
||
258 | unsigned ERROR:1; |
||
259 | unsigned CC_ERROR:1; |
||
260 | unsigned CARD_ECC_FAIL:1; |
||
261 | unsigned WP_VIOLATION:1; |
||
262 | unsigned ERASE_PARAM:1; |
||
263 | unsigned OUTRANGE_CSD_OVERWRITE:1; |
||
264 | }; |
||
265 | } RESPONSE_2; |
||
266 | |||
267 | // Summary: The format of an R1 type response |
||
268 | // Description: This union represents different ways to access an SD card R1 type response packet. |
||
269 | typedef union |
||
270 | { |
||
271 | struct |
||
272 | { |
||
273 | BYTE _byte; // Byte-wise access |
||
274 | DWORD _returnVal; |
||
275 | } bytewise; |
||
276 | // This structure allows bitwise access of the response |
||
277 | struct |
||
278 | { |
||
279 | struct |
||
280 | { |
||
281 | unsigned IN_IDLE_STATE:1; // Card is in idle state |
||
282 | unsigned ERASE_RESET:1; // Erase reset flag |
||
283 | unsigned ILLEGAL_CMD:1; // Illegal command flag |
||
284 | unsigned CRC_ERR:1; // CRC error flag |
||
285 | unsigned ERASE_SEQ_ERR:1; // Erase sequence error flag |
||
286 | unsigned ADDRESS_ERR:1; // Address error flag |
||
287 | unsigned PARAM_ERR:1; // Parameter flag |
||
288 | unsigned B7:1; // Unused bit 7 |
||
289 | }bits; |
||
290 | DWORD _returnVal; |
||
291 | } bitwise; |
||
292 | } RESPONSE_7; |
||
293 | |||
294 | // Summary: A union of responses from an SD card |
||
295 | // Description: The MMC_RESPONSE union represents any of the possible responses that an SD card can return after |
||
296 | // being issued a command. |
||
297 | typedef union |
||
298 | { |
||
299 | RESPONSE_1 r1; |
||
300 | RESPONSE_2 r2; |
||
301 | RESPONSE_7 r7; |
||
302 | }MMC_RESPONSE; |
||
303 | |||
304 | |||
305 | // Summary: A description of the card specific data register |
||
306 | // Description: This union represents different ways to access information in a packet with SD card CSD informaiton. For more |
||
307 | // information on the CSD register, consult an SD card user's manual. |
||
308 | typedef union |
||
309 | { |
||
310 | struct |
||
311 | { |
||
312 | DWORD _u320; |
||
313 | DWORD _u321; |
||
314 | DWORD _u322; |
||
315 | DWORD _u323; |
||
316 | }; |
||
317 | struct |
||
318 | { |
||
319 | BYTE _byte[16]; |
||
320 | }; |
||
321 | struct |
||
322 | { |
||
323 | unsigned NOT_USED :1; |
||
324 | unsigned CRC :7; |
||
325 | unsigned ECC :2; |
||
326 | unsigned FILE_FORMAT :2; |
||
327 | unsigned TMP_WRITE_PROTECT :1; |
||
328 | unsigned PERM_WRITE_PROTECT :1; |
||
329 | unsigned COPY :1; |
||
330 | unsigned FILE_FORMAT_GRP :1; |
||
331 | unsigned RESERVED_1 :5; |
||
332 | unsigned WRITE_BL_PARTIAL :1; |
||
333 | unsigned WRITE_BL_LEN_L :2; |
||
334 | unsigned WRITE_BL_LEN_H :2; |
||
335 | unsigned R2W_FACTOR :3; |
||
336 | unsigned DEFAULT_ECC :2; |
||
337 | unsigned WP_GRP_ENABLE :1; |
||
338 | unsigned WP_GRP_SIZE :5; |
||
339 | unsigned ERASE_GRP_SIZE_L :3; |
||
340 | unsigned ERASE_GRP_SIZE_H :2; |
||
341 | unsigned SECTOR_SIZE :5; |
||
342 | unsigned C_SIZE_MULT_L :1; |
||
343 | unsigned C_SIZE_MULT_H :2; |
||
344 | unsigned VDD_W_CURR_MAX :3; |
||
345 | unsigned VDD_W_CUR_MIN :3; |
||
346 | unsigned VDD_R_CURR_MAX :3; |
||
347 | unsigned VDD_R_CURR_MIN :3; |
||
348 | unsigned C_SIZE_L :2; |
||
349 | unsigned C_SIZE_H :8; |
||
350 | unsigned C_SIZE_U :2; |
||
351 | unsigned RESERVED_2 :2; |
||
352 | unsigned DSR_IMP :1; |
||
353 | unsigned READ_BLK_MISALIGN :1; |
||
354 | unsigned WRITE_BLK_MISALIGN :1; |
||
355 | unsigned READ_BL_PARTIAL :1; |
||
356 | unsigned READ_BL_LEN :4; |
||
357 | unsigned CCC_L :4; |
||
358 | unsigned CCC_H :8; |
||
359 | unsigned TRAN_SPEED :8; |
||
360 | unsigned NSAC :8; |
||
361 | unsigned TAAC :8; |
||
362 | unsigned RESERVED_3 :2; |
||
363 | unsigned SPEC_VERS :4; |
||
364 | unsigned CSD_STRUCTURE :2; |
||
365 | }; |
||
366 | } CSD; |
||
367 | |||
368 | |||
369 | // Summary: A description of the card information register |
||
370 | // Description: This union represents different ways to access information in a packet with SD card CID register informaiton. For more |
||
371 | // information on the CID register, consult an SD card user's manual. |
||
372 | typedef union |
||
373 | { |
||
374 | struct |
||
375 | { |
||
376 | DWORD _u320; |
||
377 | DWORD _u321; |
||
378 | DWORD _u322; |
||
379 | DWORD _u323; |
||
380 | }; |
||
381 | struct |
||
382 | { |
||
383 | BYTE _byte[16]; |
||
384 | }; |
||
385 | struct |
||
386 | { |
||
387 | unsigned NOT_USED :1; |
||
388 | unsigned CRC :7; |
||
389 | unsigned MDT :8; |
||
390 | DWORD PSN; |
||
391 | unsigned PRV :8; |
||
392 | char PNM[6]; |
||
393 | WORD OID; |
||
394 | unsigned MID :8; |
||
395 | }; |
||
396 | } CID; |
||
397 | |||
398 | #ifndef FALSE |
||
399 | #define FALSE 0 |
||
400 | #endif |
||
401 | #ifndef TRUE |
||
402 | #define TRUE !FALSE |
||
403 | #endif |
||
404 | |||
405 | #define INPUT 1 |
||
406 | #define OUTPUT 0 |
||
407 | |||
408 | |||
409 | // Description: A delay prescaler |
||
410 | #define DELAY_PRESCALER (BYTE) 8 |
||
411 | |||
412 | // Description: An approximation of the number of cycles per delay loop of overhead |
||
413 | #define DELAY_OVERHEAD (BYTE) 5 |
||
414 | |||
415 | // Description: An approximate calculation of how many times to loop to delay 1 ms in the Delayms function |
||
416 | #define MILLISECDELAY (WORD) ((GetInstructionClock()/DELAY_PRESCALER/(WORD)1000) - DELAY_OVERHEAD) |
||
417 | |||
418 | |||
419 | // Summary: An enumeration of SD commands |
||
420 | // Description: This enumeration corresponds to the position of each command in the sdmmc_cmdtable array |
||
421 | // These macros indicate to the SendMMCCmd function which element of the sdmmc_cmdtable array |
||
422 | // to retrieve command code information from. |
||
423 | typedef enum |
||
424 | { |
||
425 | GO_IDLE_STATE, |
||
426 | SEND_OP_COND, |
||
427 | SEND_IF_COND, |
||
428 | SEND_CSD, |
||
429 | SEND_CID, |
||
430 | STOP_TRANSMISSION, |
||
431 | SEND_STATUS, |
||
432 | SET_BLOCKLEN, |
||
433 | READ_SINGLE_BLOCK, |
||
434 | READ_MULTI_BLOCK, |
||
435 | WRITE_SINGLE_BLOCK, |
||
436 | WRITE_MULTI_BLOCK, |
||
437 | TAG_SECTOR_START, |
||
438 | TAG_SECTOR_END, |
||
439 | ERASE, |
||
440 | APP_CMD, |
||
441 | READ_OCR, |
||
442 | CRC_ON_OFF |
||
443 | }sdmmc_cmd; |
||
444 | |||
445 | |||
446 | #define SD_MODE_NORMAL 0 |
||
447 | #define SD_MODE_HC 1 |
||
448 | |||
449 | |||
450 | /***************************************************************************/ |
||
451 | /* Macros */ |
||
452 | /***************************************************************************/ |
||
453 | |||
454 | // Description: A macro to send clock cycles to dummy-read the CRC |
||
455 | #define mReadCRC() WriteSPIM(0xFF);WriteSPIM(0xFF); |
||
456 | |||
457 | // Description: A macro to send clock cycles to dummy-write the CRC |
||
458 | #define mSendCRC() WriteSPIM(0xFF);WriteSPIM(0xFF); |
||
459 | |||
460 | // Description: A macro to send 8 clock cycles for SD timing requirements |
||
461 | #define mSend8ClkCycles() WriteSPIM(0xFF); |
||
462 | |||
463 | /*****************************************************************************/ |
||
464 | /* Prototypes */ |
||
465 | /*****************************************************************************/ |
||
466 | |||
467 | DWORD MDD_SDSPI_ReadCapacity(void); |
||
468 | WORD MDD_SDSPI_ReadSectorSize(void); |
||
469 | void MDD_SDSPI_InitIO(void); |
||
470 | |||
471 | BYTE MDD_SDSPI_MediaDetect(void); |
||
472 | MEDIA_INFORMATION * MDD_SDSPI_MediaInitialize(void); |
||
473 | BYTE MDD_SDSPI_SectorRead(DWORD sector_addr, BYTE* buffer); |
||
474 | BYTE MDD_SDSPI_SectorWrite(DWORD sector_addr, BYTE* buffer, BYTE allowWriteToZero); |
||
475 | |||
476 | BYTE MDD_SDSPI_WriteProtectState(void); |
||
477 | BYTE MDD_SDSPI_ShutdownMedia(void); |
||
478 | |||
479 | #if defined __C30__ || defined __C32__ |
||
480 | extern BYTE ReadByte( BYTE* pBuffer, WORD index ); |
||
481 | extern WORD ReadWord( BYTE* pBuffer, WORD index ); |
||
482 | extern DWORD ReadDWord( BYTE* pBuffer, WORD index ); |
||
483 | #endif |
||
484 | |||
485 | #endif |
Powered by WebSVN v2.8.3