Subversion Repositories NaviCtrl

Rev

Rev 27 | Rev 70 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27 Rev 41
Line 55... Line 55...
55
// +  POSSIBILITY OF SUCH DAMAGE. 
55
// +  POSSIBILITY OF SUCH DAMAGE. 
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
56
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
57
#include <stdio.h>
57
#include <stdio.h>
58
#include <string.h>
58
#include <string.h>
59
#include "91x_lib.h"
59
#include "91x_lib.h"
60
#include "uart.h"
60
#include "uart1.h"
61
#include "sdc.h"
61
#include "sdc.h"
62
#include "ssc.h"
62
#include "ssc.h"
63
#include "timer.h"
63
#include "timer.h"
64
#include "main.h"
64
#include "main.h"
65
#include "crc16.h"
65
#include "crc16.h"
Line 179... Line 179...
179
        }
179
        }
180
        crc = (crc<<1)|1; // set terminating bit to 1
180
        crc = (crc<<1)|1; // set terminating bit to 1
181
        return(crc);
181
        return(crc);
182
}
182
}
Line 183... Line 183...
183
 
183
 
184
u8 SDC_WaitForBusy()
184
u8 SDC_WaitForBusy(u32 timeout)
185
{
185
{
186
        u8 rsp = 0;
186
        u8 rsp = 0;
Line 187... Line 187...
187
        u16 timeout=0;
187
        u32 timestamp = 0;
188
 
188
 
-
 
189
        SSC_ClearRxFifo();
189
        SSC_ClearRxFifo();
190
        SSC_Enable();                           // enable chipselect.
190
        SSC_Enable();                           // enable chipselect.
191
        timestamp = SetDelay(timeout);
191
        do     
192
        do     
192
        {
193
        {
193
                rsp = SSC_GetChar();
194
                rsp = SSC_GetChar();
194
                if(timeout++>500) break;
195
                if(CheckDelay(timestamp)) break;
195
        }while(rsp != 0xFF);            // wait while card is busy (data out low)
196
        }while(rsp != 0xFF);            // wait while card is busy (data out low)
Line 207... Line 208...
207
// Returnvalue: The function returns the first response byte like for R1 commands
208
// Returnvalue: The function returns the first response byte like for R1 commands
208
//________________________________________________________________________________________________________________________________________
209
//________________________________________________________________________________________________________________________________________
209
u8 SDC_SendCMDR1(u8 CmdNo, u32 arg)
210
u8 SDC_SendCMDR1(u8 CmdNo, u32 arg)
210
{
211
{
211
        u8 r1;
212
        u8 r1;
212
        u16 Timeout = 0;
213
        u16 timeout = 0;
213
        u16 a;
214
        u16 a;
214
        u8 cmd[6];
215
        u8 cmd[6];
Line 215... Line 216...
215
 
216
 
216
        SSC_ClearRxFifo();      // clear the rx fifo
217
        SSC_ClearRxFifo();      // clear the rx fifo
217
        SSC_Enable();           // enable chipselect.
218
        SSC_Enable();           // enable chipselect.
218
        SDC_WaitForBusy();
219
        SDC_WaitForBusy(500);   // wait 500ms until card is busy
219
        SSC_ClearRxFifo();      // clear the rx fifo
220
        SSC_ClearRxFifo();      // clear the rx fifo
Line 220... Line -...
220
        SSC_GetChar();      // dummy to sync
-
 
221
 
221
        SSC_GetChar();      // dummy to sync
222
        /* Send cmd10 (SEND_CID) */;
222
 
223
        cmd[0] = 0x40|CmdNo; // set command index
223
        cmd[0] = 0x40|CmdNo; // set command index
224
        cmd[1] = (arg & 0xFF000000)>>24;
224
        cmd[1] = (arg & 0xFF000000)>>24;
225
        cmd[2] = (arg & 0x00FF0000)>>16;
225
        cmd[2] = (arg & 0x00FF0000)>>16;
Line 229... Line 229...
229
        for (a = 0;a < 6; a++) // send the command sequence to the sdcard (6 bytes)
229
        for (a = 0;a < 6; a++) // send the command sequence to the sdcard (6 bytes)
230
        {      
230
        {      
231
                SSC_PutChar(cmd[a]);
231
                SSC_PutChar(cmd[a]);
232
        }
232
        }
233
        SSC_ClearRxFifo();      // clear the rx fifo to discard the bytes received during the transmission of the 6 command bytes
233
        SSC_ClearRxFifo();      // clear the rx fifo to discard the bytes received during the transmission of the 6 command bytes
-
 
234
 
234
        do                                     
235
        do                                     
235
        {
236
        {
236
                r1 = SSC_GetChar();       // get byte from sd-card
237
                r1 = SSC_GetChar();       // get byte from sd-card
237
                if (Timeout++ > 500) return(r1);
238
                if (timeout++ >500) break;
238
        }while(r1 == 0xFF); // wait for the response byte from sd-card.
239
        }while(r1 == 0xFF); // wait for the response byte from sd-card.
239
        return(r1);
240
        return(r1);
240
}
241
}
Line 241... Line 242...
241
 
242
 
242
 
243
 
243
//________________________________________________________________________________________________________________________________________
244
//________________________________________________________________________________________________________________________________________
244
// Function:    SDC_SendACMDR1(u8 CmdNo, u32 arg);
245
// Function:    SDC_SendACMDR1(u8 CmdNo, u32 arg);
245
// 
246
//                                         
246
// Description: This function send a application command frame to the SD-Card in spi-mode. 
247
// Description: This function send a application command frame to the SD-Card in spi-mode. 
247
//                              
248
//                              
248
//
249
//
Line 268... Line 269...
268
//________________________________________________________________________________________________________________________________________
269
//________________________________________________________________________________________________________________________________________
Line 269... Line 270...
269
 
270
 
270
SD_Result_t SDC_GetData(u8 CmdNo, u32 addr, u8 *Buffer, u32 len)
271
SD_Result_t SDC_GetData(u8 CmdNo, u32 addr, u8 *Buffer, u32 len)
271
{
272
{
272
        u8 rsp;        
-
 
273
        u16 timeout;
273
        u8 rsp;        
274
        u16 a, Crc16;
274
        u16 a, crc16;
Line 275... Line 275...
275
        SD_Result_t result = SD_ERROR_UNKNOWN;
275
        SD_Result_t result = SD_ERROR_UNKNOWN;
276
 
276
 
277
        // send the command     
277
        // send the command     
Line 288... Line 288...
288
                if((rsp & 0xF0) == 0x00) // data error token 
288
                if((rsp & 0xF0) == 0x00) // data error token 
289
                {
289
                {
290
                        result = SD_ERROR_READ_DATA;
290
                        result = SD_ERROR_READ_DATA;
291
                        goto end;
291
                        goto end;
292
                }
292
                }
293
//              if(timeout++>500) break;
-
 
294
        }while(rsp != DATA_START_TOKEN);
293
        }while(rsp != DATA_START_TOKEN);
295
        // data start token received
294
        // data start token received
296
        for (a = 0; a < len; a++)       // read the block from the SSC
295
        for (a = 0; a < len; a++)       // read the block from the SSC
297
        {
296
        {
298
                Buffer[a] = SSC_GetChar();
297
                Buffer[a] = SSC_GetChar();
299
        }
298
        }
300
        // Read two bytes CRC16-Data checksum
299
        // Read two bytes CRC16-Data checksum
301
        Crc16 = SSC_GetChar(); // highbyte fisrt        
300
        crc16 = SSC_GetChar(); // highbyte fisrt        
302
        Crc16 = (Crc16<<8)|SSC_GetChar(); // lowbyte last
301
        crc16 = (crc16<<8)|SSC_GetChar(); // lowbyte last
303
//      if(Crc16 != CRC16(Buffer, len)) result = SD_ERROR_CRC_DATA;
302
/*      if(crc16 != CRC16(Buffer, len)) result = SD_ERROR_CRC_DATA;
304
/*      else */result = SD_SUCCESS;
303
        else */result = SD_SUCCESS;
Line 305... Line 304...
305
       
304
       
306
        end:
-
 
307
        //SSC_Disable();        // disable sdcard.
305
        end:
308
        if(result != SD_SUCCESS)
306
        if(result != SD_SUCCESS)
309
        {
307
        {
310
                sprintf(text,"Error %02X reading data from sd card (R1=%02X).\r\n", result, rsp);
308
                sprintf(text,"Error %02X reading data from sd card (R1=%02X).\r\n", result, rsp);
311
                SerialPutString(text);
309
                SerialPutString(text);
Line 327... Line 325...
327
{
325
{
328
        u8 text[50];
326
        u8 text[50];
329
        u8 pn[6];
327
        u8 pn[6];
330
        u16 temp1, temp2;
328
        u16 temp1, temp2;
Line 331... Line 329...
331
 
329
 
332
        sprintf(text, "\r\nManufacturer ID: %i\r\n", pCID[0]);
330
        sprintf(text, "\r\n Manufacturer ID: %i\r\n", pCID[0]);
333
        SerialPutString(text);
331
        SerialPutString(text);
334
        memcpy(pn, &pCID[1], 2);
332
        memcpy(pn, &pCID[1], 2);
335
        pn[2] = '\0'; // terminate string
333
        pn[2] = '\0'; // terminate string
336
        sprintf(text, "Application ID: %s\r\n",pn);
334
        sprintf(text, " Application ID: %s\r\n",pn);
337
        SerialPutString(text);
335
        SerialPutString(text);
338
        memcpy(pn, &pCID[3], 5);
336
        memcpy(pn, &pCID[3], 5);
339
        pn[5] = '\0'; // terminate string
337
        pn[5] = '\0'; // terminate string
340
        sprintf(text, "Product Name: %s\r\n",pn);
338
        sprintf(text, " Product Name: %s\r\n",pn);
341
        SerialPutString(text);
339
        SerialPutString(text);
342
        sprintf(text, "Product Rev.: %i.%i\r\n",pCID[8]>>4, pCID[8]&0xF);
340
        sprintf(text, " Product Rev.: %i.%i\r\n",pCID[8]>>4, pCID[8]&0xF);
343
        SerialPutString(text);
341
        SerialPutString(text);
344
        SerialPutString("Serial No.: ");
342
        SerialPutString(" Serial No.: ");
345
        for(temp1 = 0; temp1<4; temp1++)
343
        for(temp1 = 0; temp1<4; temp1++)
346
        {
344
        {
347
                sprintf(text,"%02X", pCID[9+temp1]);
345
                sprintf(text,"%02X", pCID[9+temp1]);
348
                SerialPutString(text);
346
                SerialPutString(text);
349
        }
347
        }
350
        SerialPutString("\r\n");
348
        SerialPutString("\r\n");
351
        temp1 = pCID[14] & 0x0F;    // month 
349
        temp1 = pCID[14] & 0x0F;    // month 
352
        temp2 = ((pCID[14]>>4)|(pCID[13]<<4)) + 2000; // year 
350
        temp2 = ((pCID[14]>>4)|(pCID[13]<<4)) + 2000; // year 
353
        sprintf(text, "Manufac. Date: %i/%i\r\n\r\n",temp1, temp2);
351
        sprintf(text, " Manufac. Date: %i/%i\r\n\r\n",temp1, temp2);
354
        SerialPutString(text);
352
        SerialPutString(text);
Line 355... Line 353...
355
}
353
}
356
 
354
 
Line 392... Line 390...
392
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
390
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
393
//________________________________________________________________________________________________________________________________________
391
//________________________________________________________________________________________________________________________________________
Line 394... Line 392...
394
 
392
 
395
SD_Result_t SDC_Init(void)
393
SD_Result_t SDC_Init(void)
396
{
394
{
397
        u32 Timeout = 0;
395
        u32 timeout = 0;
398
        u8 text[50];
396
        u8 text[50];
399
        u8 rsp[6]; // SD-SPI response buffer
397
        u8 rsp[6]; // SD-SPI response buffer
Line 400... Line 398...
400
        SD_Result_t result = SD_ERROR_UNKNOWN;
398
        SD_Result_t result = SD_ERROR_UNKNOWN;
401
               
399
               
402
        if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot
400
        if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot
403
        {
401
        {
404
                SerialPutString("\r\n  SSC init...");  
402
                SerialPutString("\r\n SSC init...");
405
                SSC_Init();
403
                SSC_Init();
406
                SerialPutString("ok");
404
                SerialPutString("ok");
407
       
405
 
408
                SerialPutString("\r\n  SDC init...");  
406
                SerialPutString("\r\n SDC init...");
409
                SDCardInfo.Valid = 0;
407
                SDCardInfo.Valid = 0;
410
                /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
408
                /* The host shall supply power to the card so that the voltage is reached to Vdd_min within 250ms and
411
                start to supply at least 74 SD clocks to the SD card with keeping cmd line to high. In case of SPI
409
                start to supply at least 74 SD clocks to the SD card with keeping cmd line to high. In case of SPI
412
                mode, CS shall be held to high during 74 clock cycles. */
410
                mode, CS shall be held to high during 74 clock cycles. */
Line 413... Line 411...
413
                SSC_Disable(); // set SD_CS high
411
                SSC_Disable(); // set SD_CS high
414
                SSC_ClearRxFifo();      // clear the rx fifo
412
                SSC_ClearRxFifo();      // clear the rx fifo
415
       
413
       
416
                for (Timeout = 0; Timeout < 15; Timeout++)      // 15*8 = 120 cycles
414
                for (timeout = 0; timeout < 15; timeout++)      // 15*8 = 120 cycles
Line 417... Line 415...
417
                {
415
                {
418
                        SSC_PutChar(0xFF);
416
                        SSC_PutChar(0xFF);
419
                }
417
                }
420
       
418
       
421
                // switch to idle state
419
                // switch to idle state
422
                while(SDC_SendCMDR1(CMD_GO_IDLE_STATE, 0UL) != R1_IDLE_STATE)                                          
420
                while(SDC_SendCMDR1(CMD_GO_IDLE_STATE, 0UL) != R1_IDLE_STATE)                                          
423
                {
421
                {
424
                        if (Timeout++ > 20)
422
                        if (timeout++ > 20)
425
                        {
423
                        {
426
                                SerialPutString("reset timeout");
424
                                SerialPutString("reset timeout");
427
                                result = SD_ERROR_RESET;
425
                                result = SD_ERROR_RESET;
428
                                goto end;                                                                              
426
                                goto end;                                                                              
429
                        }
427
                        }
430
                }
428
                }
431
            // enable crc feature
429
            // enable crc feature
432
/*              if(SDC_SendCMDR1(CMD_CRC_ON_OFF, 1UL) != R1_IDLE_STATE)
430
/*              if(SDC_SendCMDR1(CMD_CRC_ON_OFF, 1UL) != R1_IDLE_STATE)
433
                {
431
                {
434
                                sprintf(text,"Bad cmd59 R1=%02X.\r\n", rsp[0]);
432
                                sprintf(text,"Bad cmd59 R1=%02X.", rsp[0]);
435
                                SerialPutString(text);
433
                                SerialPutString(text);
436
                                result = SD_ERROR_BAD_RESPONSE;
434
                                result = SD_ERROR_BAD_RESPONSE;
437
                                goto end;
435
                                goto end;
438
                }*/
436
                }*/
439
                // check for card hw version                                            
437
                // check for card hw version                                            
440
                // 2.7-3.6V Range = 0x01, check pattern 0xAA
438
                // 2.7-3.6V Range = 0x01, check pattern 0xAA
441
                rsp[0] = SDC_SendCMDR1(CMD_SEND_IF_COND, 0x000001AA);
439
                rsp[0] = SDC_SendCMDR1(CMD_SEND_IF_COND, 0x000001AA);
442
                // answer to cmd58 is an R7 response (R1+ 4Byte IFCond)
440
                // answer to cmd58 is an R7 response (R1+ 4Byte IFCond)
443
                if(rsp[0] & R1_BAD_RESPONSE)
441
                if(rsp[0] & R1_BAD_RESPONSE)
444
                {
442
                {
445
                        sprintf(text,"Bad cmd8 R1=%02X.\r\n", rsp[0]);
443
                        sprintf(text,"Bad cmd8 R1=%02X.", rsp[0]);
446
                        SerialPutString(text);
444
                        SerialPutString(text);
Line 455... Line 453...
455
                else
453
                else
456
                {
454
                {
457
                   // Ver2.00 or later SD Memory Card
455
                   // Ver2.00 or later SD Memory Card
458
                   // reading the remaining bytes of the R7 response
456
                   // reading the remaining bytes of the R7 response
459
                   SDCardInfo.Version = VER_20;
457
                   SDCardInfo.Version = VER_20;
460
                   for(Timeout = 1; Timeout < 5; Timeout++)
458
                   for(timeout = 1; timeout < 5; timeout++)
461
                   {
459
                   {
462
                                rsp[Timeout] = SSC_GetChar();
460
                                rsp[timeout] = SSC_GetChar();
463
                   }
461
                   }
464
                   //check pattern
462
                   //check pattern
465
                   if(rsp[4]!= 0xAA)
463
                   if(rsp[4]!= 0xAA)
466
                   {
464
                   {
467
                                SerialPutString("Bad cmd8 R7 check pattern.\r\n");
465
                                SerialPutString("Bad cmd8 R7 check pattern.\r\n");
Line 479... Line 477...
479
       
477
       
480
                rsp[0] = SDC_SendCMDR1(CMD_READ_OCR, 0UL);
478
                rsp[0] = SDC_SendCMDR1(CMD_READ_OCR, 0UL);
481
                // answer to cmd58 is an R3 response (R1 + 4Byte OCR)
479
                // answer to cmd58 is an R3 response (R1 + 4Byte OCR)
482
                if(rsp[0] & R1_BAD_RESPONSE)
480
                if(rsp[0] & R1_BAD_RESPONSE)
483
                {
481
                {
484
                        sprintf(text,"Bad cmd58 R1 %02x.\r\n", rsp[0]);
482
                        sprintf(text,"Bad cmd58 R1 %02x.", rsp[0]);
485
                        SerialPutString(text);
483
                        SerialPutString(text);
486
                        result = SD_ERROR_BAD_RESPONSE;
484
                        result = SD_ERROR_BAD_RESPONSE;
487
                        goto end;
485
                        goto end;
488
                }
486
                }
489
                if(rsp[0] & R1_ILLEGAL_CMD)
487
                if(rsp[0] & R1_ILLEGAL_CMD)
490
                {
488
                {
491
                        SerialPutString("Not an SD-CARD.\r\n");
489
                        SerialPutString("Not an SD-CARD.");
492
                        result = SD_ERROR_NO_SDCARD;
490
                        result = SD_ERROR_NO_SDCARD;
493
                        goto end;
491
                        goto end;
494
                }
492
                }
495
                // read 4 bytes of OCR register
493
                // read 4 bytes of OCR register
496
                for(Timeout = 1; Timeout < 5; Timeout++)
494
                for(timeout = 1; timeout < 5; timeout++)
497
                {
495
                {
498
                        rsp[Timeout] = SSC_GetChar();
496
                        rsp[timeout] = SSC_GetChar();
499
                }
497
                }
500
                //      NavicCtrl uses 3.3 V,  therefore check for bit 20 & 21 
498
                //      NavicCtrl uses 3.3 V,  therefore check for bit 20 & 21 
501
                if((rsp[2] & 0x30) != 0x30)
499
                if((rsp[2] & 0x30) != 0x30)
502
                {
500
                {
503
                        // supply voltage is not supported by sd-card
501
                        // supply voltage is not supported by sd-card
504
                        SerialPutString("Card is incompatible to 3.3V.\r\n");
502
                        SerialPutString("Card is incompatible to 3.3V.");
505
                        result = SD_ERROR_BAD_VOLTAGE_RANGE;
503
                        result = SD_ERROR_BAD_VOLTAGE_RANGE;
506
                        goto end;
504
                        goto end;
Line 507... Line 505...
507
                }
505
                }
508
               
506
               
509
                // Initialize the sd-card sending continously ACMD_SEND_OP_COND (only supported by SD cards)
507
                // Initialize the sd-card sending continously ACMD_SEND_OP_COND (only supported by SD cards)
510
                Timeout =  SetDelay(2000); // set timeout to 2000 ms (large cards tend to longer) 
508
                timeout =  SetDelay(2000); // set timeout to 2000 ms (large cards tend to longer) 
511
                do
509
                do
512
                {
510
                {
513
                        rsp[0] = SDC_SendACMDR1(ACMD_SEND_OP_COND, 0UL);
511
                        rsp[0] = SDC_SendACMDR1(ACMD_SEND_OP_COND, 0UL);
514
                        if(rsp[0] & R1_BAD_RESPONSE)
512
                        if(rsp[0] & R1_BAD_RESPONSE)
515
                        {
513
                        {
516
                                sprintf(text,"Bad Acmd41 R1=%02X.\r\n", rsp[0]);
514
                                sprintf(text,"Bad Acmd41 R1=%02X.", rsp[0]);
517
                                SerialPutString(text);
515
                                SerialPutString(text);
518
                                result = SD_ERROR_BAD_RESPONSE;
516
                                result = SD_ERROR_BAD_RESPONSE;
519
                                goto end;
517
                                goto end;
520
                        }
518
                        }
521
                        if(CheckDelay(Timeout))
519
                        if(CheckDelay(timeout))
522
                        {
520
                        {
523
                            SerialPutString("Init timeout.\r\n");
521
                            SerialPutString("Init timeout.");
524
                                result = SD_ERROR_INITIALIZE;
522
                                result = SD_ERROR_INITIALIZE;
525
                                goto end;
523
                                goto end;
Line 526... Line 524...
526
                        }
524
                        }
527
                } while(rsp[0] & R1_IDLE_STATE); // loop until idle state
525
                } while(rsp[0] & R1_IDLE_STATE); // loop until idle state
528
               
526
               
529
                if(rsp[0] != R1_NO_ERR)
527
                if(rsp[0] != R1_NO_ERR)
530
                {
528
                {
531
                        SerialPutString("Init error.\r\n");
529
                        SerialPutString("Init error.");
532
                        result = SD_ERROR_INITIALIZE;
530
                        result = SD_ERROR_INITIALIZE;
533
                        goto end;      
531
                        goto end;      
534
                }
532
                }
535
                /* set block size to 512 bytes */
533
                /* set block size to 512 bytes */
536
        if(SDC_SendCMDR1(CMD_SET_BLOCKLEN, 512UL) != R1_NO_ERR)
534
        if(SDC_SendCMDR1(CMD_SET_BLOCKLEN, 512UL) != R1_NO_ERR)
537
        {
535
        {
538
                SerialPutString("Error setting block length to 512.\r\n");
536
                SerialPutString("Error setting block length to 512.");
539
                        result = SD_ERROR_SET_BLOCKLEN;
-
 
540
                        goto end;
537
                        result = SD_ERROR_SET_BLOCKLEN;
541
        }
538
                        goto end;
542
 
539
        }
543
 
540
               
Line 544... Line 541...
544
                //SSC_Disable(); // set SD_CS high                                               
541
                //SSC_Disable(); // set SD_CS high                                                                       
545
                // here is the right place to inrease the SPI boud rate to maximum              
542
                // here is the right place to inrease the SPI boud rate to maximum              
Line 555... Line 552...
555
       
552
       
556
                // read CSD register
553
                // read CSD register
557
                result = SDC_GetCSD((u8 *)&SDCardInfo.CSD);
554
                result = SDC_GetCSD((u8 *)&SDCardInfo.CSD);
558
                if(result != SD_SUCCESS)
555
                if(result != SD_SUCCESS)
559
                {
556
                {
560
                        SerialPutString("Error reading CSD.\r\n");
557
                        SerialPutString("Error reading CSD.");
561
                        goto end;
558
                        goto end;
Line 562... Line 559...
562
                }
559
                }
Line 599... Line 596...
599
       
596
       
600
                        c_size = ((u32)(SDCardInfo.CSD[7] & 0x3F))<<16; //CSD[07] -> [71:64]
597
                        c_size = ((u32)(SDCardInfo.CSD[7] & 0x3F))<<16; //CSD[07] -> [71:64]
601
                        c_size |= ((u32)SDCardInfo.CSD[8])<<8;                  //CSD[08] -> [63:56]
598
                        c_size |= ((u32)SDCardInfo.CSD[8])<<8;                  //CSD[08] -> [63:56]
602
                        c_size |= (u32)SDCardInfo.CSD[9];                               //CSD[09] -> [55:48];
599
                        c_size |= (u32)SDCardInfo.CSD[9];                               //CSD[09] -> [55:48];
603
                        SDCardInfo.Capacity = (c_size + 1)* 512L * 1024L;
600
                        SDCardInfo.Capacity = (c_size + 1)* 512L * 1024L;
-
 
601
                        break;
604
                        break; 
602
       
605
                 default: //unknown CSD Version
603
                default: //unknown CSD Version
606
                        SDCardInfo.Capacity = 0;
604
                        SDCardInfo.Capacity = 0;
607
                        break; 
605
                        break; 
Line 608... Line 606...
608
                }
606
                }
Line 619... Line 617...
619
                }
617
                }
620
                u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L));
618
                u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L));
621
                sprintf(text, "\r\n  Capacity = %i MB", mb_size);
619
                sprintf(text, "\r\n  Capacity = %i MB", mb_size);
622
                SerialPutString(text);
620
                SerialPutString(text);
Line 623... Line 621...
623
       
621
       
624
//              SDC_PrintCID((u8 *)&SDCardInfo.CID);
622
                SDC_PrintCID((u8 *)&SDCardInfo.CID);
625
                SDCardInfo.Valid = 1;
-
 
626
                       
623
                SDCardInfo.Valid = 1;
627
                // jump point for error condition before
624
                // jump point for error condition before
628
                end:
625
                end:
629
                SSC_Disable();
626
                SSC_Disable();
630
        }
627
        }
631
        else
628
        else
632
        {
-
 
633
                //SerialPutString("\r\nNo Card in Slot.");
629
        {
634
                SSC_Deinit();
630
                SSC_Deinit();
635
                SDCardInfo.Valid = 0;
631
                SDCardInfo.Valid = 0;
-
 
632
                result = SD_ERROR_NOCARD;
636
                result = SD_ERROR_NOCARD;
633
                SerialPutString("No Card in Slot.");
637
        }
-
 
638
//      if(result != SD_SUCCESS)                SerialPutString("nok ");
-
 
639
 
634
        }
640
        return(result);
635
        return(result);
Line 641... Line 636...
641
}
636
}
Line 650... Line 645...
650
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
645
// Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode.
651
//________________________________________________________________________________________________________________________________________
646
//________________________________________________________________________________________________________________________________________
Line 652... Line 647...
652
 
647
 
653
SD_Result_t SDC_Deinit(void)
648
SD_Result_t SDC_Deinit(void)
654
{
649
{
655
        SerialPutString("SDC deinit...");
650
        SerialPutString("\r\n SDC deinit...");
Line 656... Line 651...
656
        SSC_Deinit();
651
        SSC_Deinit();
657
 
652
 
658
        SDCardInfo.Valid = 0;
653
        SDCardInfo.Valid = 0;
Line 659... Line 654...
659
        SDCardInfo.Capacity = 0;
654
        SDCardInfo.Capacity = 0;
660
        SDCardInfo.Version = VER_UNKNOWN;
655
        SDCardInfo.Version = VER_UNKNOWN;
661
 
656
 
Line 662... Line 657...
662
        SerialPutString("ok\r\n");
657
        SerialPutString("ok");
Line 674... Line 669...
674
//________________________________________________________________________________________________________________________________________
669
//________________________________________________________________________________________________________________________________________
Line 675... Line 670...
675
 
670
 
676
SD_Result_t SDC_PutSector(u32 addr, const u8 *Buffer)
671
SD_Result_t SDC_PutSector(u32 addr, const u8 *Buffer)
677
{
672
{
-
 
673
        u8 rsp;
678
        u8 rsp;
674
        u16 a, crc16;
679
        u16 timeout = 0;
-
 
680
        u16 a, Crc16;
-
 
681
        //u32 Timeout = 0;
675
        u16 timeout = 0;
Line 682... Line 676...
682
        SD_Result_t result = SD_ERROR_UNKNOWN;
676
        SD_Result_t result = SD_ERROR_UNKNOWN;
683
 
677
 
684
        addr = addr << 9; // convert sectoradress to byteadress
678
        addr = addr << 9; // convert sectoradress to byteadress
Line 691... Line 685...
691
        SSC_ClearRxFifo();             
685
        SSC_ClearRxFifo();             
692
        for (a=0;a<20;a++)                                      // at least one byte
686
        for (a=0;a<20;a++)                                      // at least one byte
693
        {
687
        {
694
                SSC_GetChar();
688
                SSC_GetChar();
695
        }
689
        }
696
        Crc16 = CRC16(Buffer, 512);         // calc checksum for data block
690
        crc16 = CRC16(Buffer, 512);         // calc checksum for data block
697
        SSC_PutChar(DATA_START_TOKEN);          // send data start of header to the SSC 
691
        SSC_PutChar(DATA_START_TOKEN);          // send data start of header to the SSC 
Line 698... Line 692...
698
       
692
       
699
        for (a=0;a<512;a++)                                     // transmit one sector (normaly 512bytes) of data to the sdcard.
693
        for (a=0;a<512;a++)                                     // transmit one sector (normaly 512bytes) of data to the sdcard.
700
        {
694
        {
701
                SSC_PutChar(Buffer[a]);
695
                SSC_PutChar(Buffer[a]);
702
        }
696
        }
703
        // write two bytes of crc16 to the sdcard
697
        // write two bytes of crc16 to the sdcard
704
        SSC_PutChar((u8)(Crc16>>8));            // write high byte first
698
        SSC_PutChar((u8)(crc16>>8));            // write high byte first
705
        SSC_PutChar((u8)(0x00FF&Crc16));        // lowbyte last
699
        SSC_PutChar((u8)(0x00FF&crc16));        // lowbyte last
706
        SSC_ClearRxFifo();
700
        SSC_ClearRxFifo();
707
        do                                                                      // wait for data response token
701
        do                                                                      // wait for data response token
708
        {
702
        {
709
                rsp = SSC_GetChar();
703
                rsp = SSC_GetChar();
-
 
704
                if(timeout++ > 500)
-
 
705
                {
-
 
706
                        result = SD_ERROR_TIMEOUT;
-
 
707
                        goto end;
710
                if(timeout++>500) break;
708
                }
711
        }while((rsp & 0x11) != 0x01 );
709
        }while((rsp & 0x11) != 0x01 );
712
        // analyse data response token
710
        // analyse data response token
713
        switch(rsp & DATA_RESPONSE_MASK)
711
        switch(rsp & DATA_RESPONSE_MASK)
714
        {
712
        {
Line 727... Line 725...
727
                        result = SD_ERROR_UNKNOWN;
725
                        result = SD_ERROR_UNKNOWN;
728
                        goto end;
726
                        goto end;
729
                        break;
727
                        break;
Line 730... Line 728...
730
 
728
 
731
        }
729
        }
732
        // wait until the sdcard is busy.
730
        // wait 2 seconds until the sdcard is busy.
733
        rsp = SDC_WaitForBusy();
731
        rsp = SDC_WaitForBusy(2000);
734
        if(rsp != 0xFF)
732
        if(rsp != 0xFF)
735
        {
733
        {
736
                result =  SD_ERROR_TIMEOUT;
734
                result =  SD_ERROR_TIMEOUT;
737
                goto end;
735
                goto end;
-
 
736
        }
738
        }
737
 
739
        // check card status
738
        // check card status
-
 
739
        rsp = SDC_SendCMDR1(CMD_SEND_STATUS, 0);
740
        rsp = SDC_SendCMDR1(CMD_SEND_STATUS, 0);
740
        // first byte of R2 response is like R1 response
741
        if(rsp != R1_NO_ERR)
741
        if(rsp != R1_NO_ERR)
742
        {
742
        {
743
                result =  SD_ERROR_BAD_RESPONSE;
743
                result =  SD_ERROR_BAD_RESPONSE;
744
                SSC_GetChar();
744
                SSC_GetChar(); // read out 2nd byte
745
                goto end;
745
                goto end;
746
        }
746
        }
747
        // 2nd byte of r2 response
747
        // 2nd byte of r2 response
748
        rsp = SSC_GetChar();
748
        rsp = SSC_GetChar();
Line 751... Line 751...
751
                result =  SD_ERROR_WRITE_DATA;
751
                result =  SD_ERROR_WRITE_DATA;
752
                SSC_GetChar();
752
                SSC_GetChar();
753
                goto end;
753
                goto end;
754
        }
754
        }
755
        end:
755
        end:
756
        //SSC_Disable();                                                // disable sdcard.
-
 
757
        if(result != SD_SUCCESS)
756
        if(result != SD_SUCCESS)
758
        {
757
        {
759
                sprintf(text,"Error %02X writing data to sd card (R=%02X).\r\n", result, rsp);
758
                sprintf(text,"Error %02X writing data to sd card (R=%02X).\r\n", result, rsp);
760
                SerialPutString(text);
759
                SerialPutString(text);
761
        }
760
        }
762
        return(result);
761
        return(result);
763
}
762
}
Line 764... Line 763...
764
 
763
 
765
 
764
 
766
//________________________________________________________________________________________________________________________________________
765
//________________________________________________________________________________________________________________________________________