Rev 438 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 438 | Rev 450 | ||
---|---|---|---|
Line 366... | Line 366... | ||
366 | SD_Result_t SDC_GetCSD(u8 * pCSD) |
366 | SD_Result_t SDC_GetCSD(u8 * pCSD) |
367 | { |
367 | { |
368 | return SDC_GetData(CMD_SEND_CSD, 0UL, pCSD, 16); |
368 | return SDC_GetData(CMD_SEND_CSD, 0UL, pCSD, 16); |
369 | } |
369 | } |
Line 370... | Line -... | ||
370 | - | ||
371 | 370 | ||
372 | //________________________________________________________________________________________________________________________________________ |
371 | //________________________________________________________________________________________________________________________________________ |
373 | // Funtion: SDC_Init(void); |
372 | // Funtion: SDC_Init(void); |
374 | // |
373 | // |
375 | // Description: This function initialises the SDCard to spi-mode. |
374 | // Description: This function initialises the SDCard to spi-mode. |
376 | // |
375 | // |
377 | // |
376 | // |
378 | // Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode. |
377 | // Returnvalue: the function returns 0 if the initialisation was successfull otherwise the function returns an errorcode. |
Line 379... | Line 378... | ||
379 | //________________________________________________________________________________________________________________________________________ |
378 | //________________________________________________________________________________________________________________________________________ |
380 | 379 | ||
381 | SD_Result_t SDC_Init(void) |
380 | SD_Result_t SDC_Init(unsigned char print) |
- | 381 | { |
|
382 | { |
382 | u32 timeout = 0; |
383 | u32 timeout = 0; |
383 | u16 speed; |
- | 384 | u8 rsp[6]; // SD-SPI response buffer |
|
- | 385 | SD_Result_t result = SD_ERROR_UNKNOWN; |
|
Line 384... | Line 386... | ||
384 | u8 rsp[6]; // SD-SPI response buffer |
386 | if(SPI_Speed < 500) SPI_Speed = 500; // nur zur Sicherheit |
Line 385... | Line 387... | ||
385 | SD_Result_t result = SD_ERROR_UNKNOWN; |
387 | speed = SPI_Speed; // weil das bei SSC_Init() auf 400 gesetzt werden würde |
386 | 388 | ||
387 | //SDCardWriteRetryCounterMax = 0; |
389 | //SDCardWriteRetryCounterMax = 0; |
388 | 390 | ||
389 | if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot |
391 | if(SD_SWITCH) // init only if the SD-Switch is indicating a card in the slot |
Line 390... | Line 392... | ||
390 | { |
392 | { |
391 | UART1_PutString("\r\n SSC init..."); |
393 | if(print) UART1_PutString("\r\n SSC init..."); |
392 | SSC_Init(); |
394 | SSC_Init(); |
393 | UART1_PutString("ok"); |
395 | if(print) UART1_PutString("ok"); |
394 | 396 | ||
395 | UART1_PutString("\r\n SDC init..."); |
397 | if(print) UART1_PutString("\r\n SDC init..."); |
Line 528... | Line 530... | ||
528 | } |
530 | } |
Line 529... | Line 531... | ||
529 | 531 | ||
530 | SSC_Disable(); // set SD_CS high |
532 | SSC_Disable(); // set SD_CS high |
Line 531... | Line 533... | ||
531 | // here is the right place to inrease the SPI baud rate to maximum |
533 | // here is the right place to inrease the SPI baud rate to maximum |
- | 534 | ||
- | 535 | SSC_Speed(speed); |
|
532 | 536 | if(print) { sprintf(text,"\r\n Setting SPI speed to %d ", SPI_Speed);UART1_PutString(text); } |
|
Line 533... | Line 537... | ||
533 | SSC_Speed(2000); |
537 | |
534 | SSC_Enable(); // set SD_CS high |
538 | SSC_Enable(); // set SD_CS high |
535 | 539 | ||
536 | // read CID register |
540 | // read CID register |
Line 537... | Line 541... | ||
537 | result = SDC_GetCID((u8 *)&SDCardInfo.CID); |
541 | result = SDC_GetCID((u8 *)&SDCardInfo.CID); |
538 | if(result != SD_SUCCESS) |
542 | if(result != SD_SUCCESS) |
539 | { |
543 | { |
540 | 544 | ||
- | 545 | if(print) UART1_PutString(" failed - Set interface to low speed..."); |
|
541 | UART1_PutString(" Set interface to low speed..."); |
546 | SSC_Disable(); // set SD_CS high |
542 | SSC_Disable(); // set SD_CS high |
547 | // here is the right place to inrease the SPI baud rate to maximum |
543 | // here is the right place to inrease the SPI baud rate to maximum |
548 | SPI_Speed = 1000; |
Line 544... | Line 549... | ||
544 | SSC_Speed(1000); |
549 | SSC_Speed(SPI_Speed); |
Line 559... | Line 564... | ||
559 | { |
564 | { |
560 | UART1_PutString("Error reading CSD."); |
565 | UART1_PutString("Error reading CSD."); |
561 | goto end; |
566 | goto end; |
562 | } |
567 | } |
Line 563... | Line 568... | ||
563 | 568 | ||
Line 564... | Line 569... | ||
564 | UART1_PutString("ok\r\n"); |
569 | if(print) UART1_PutString("ok\r\n"); |
565 | 570 | ||
Line 566... | Line 571... | ||
566 | u8 c_size_mult, read_bl_len; |
571 | u8 c_size_mult, read_bl_len; |
Line 606... | Line 611... | ||
606 | default: //unknown CSD Version |
611 | default: //unknown CSD Version |
607 | SDCardInfo.Capacity = 0; |
612 | SDCardInfo.Capacity = 0; |
608 | break; |
613 | break; |
609 | } |
614 | } |
Line 610... | Line 615... | ||
610 | 615 | ||
611 | switch(SDCardInfo.Version) |
616 | if(print) switch(SDCardInfo.Version) |
612 | { |
617 | { |
613 | case VER_1X: |
618 | case VER_1X: |
614 | UART1_PutString(" SD-CARD V1.x"); |
619 | UART1_PutString(" SD-CARD V1.x"); |
615 | break; |
620 | break; |
616 | case VER_20: |
621 | case VER_20: |
617 | UART1_PutString(" SD-CARD V2.0 or later"); |
622 | UART1_PutString(" SD-CARD V2.0 or later"); |
618 | default: |
623 | default: |
619 | break; |
624 | break; |
- | 625 | } |
|
- | 626 | if(print) |
|
620 | } |
627 | { |
621 | u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L)); |
628 | u16 mb_size = (u16)(SDCardInfo.Capacity/(1024L*1024L)); |
622 | sprintf(text, "\r\n Capacity = %i MB", mb_size); |
629 | sprintf(text, "\r\n Capacity = %i MB", mb_size); |
- | 630 | UART1_PutString(text); |
|
Line 623... | Line 631... | ||
623 | UART1_PutString(text); |
631 | } |
624 | 632 | ||
625 | SDC_PrintCID((u8 *)&SDCardInfo.CID); |
633 | if(print) SDC_PrintCID((u8 *)&SDCardInfo.CID); |
626 | SDCardInfo.Valid = 1; |
634 | SDCardInfo.Valid = 1; |
627 | // jump point for error condition before |
635 | // jump point for error condition before |
628 | end: |
636 | end: |
Line 765... | Line 773... | ||
765 | } |
773 | } |
766 | end: |
774 | end: |
767 | //SSC_Disable(); // disable CS |
775 | //SSC_Disable(); // disable CS |
768 | if(result != SD_SUCCESS) |
776 | if(result != SD_SUCCESS) |
769 | { |
777 | { |
- | 778 | ||
770 | sprintf(text,"Error %02X writing data to sd card (R=%02X).\r\n", result, rsp); |
779 | sprintf(text,"\r\nError %02X writing data to sd card (R=%02X)", result, rsp); |
771 | UART1_PutString(text); |
780 | UART1_PutString(text); |
772 | retryCounter++; |
781 | retryCounter++; |
- | 782 | ||
- | 783 | if(SPI_Speed > 1000) |
|
- | 784 | { |
|
- | 785 | SPI_Speed -= 200; |
|
- | 786 | sprintf(text," new Speed = %d", SPI_Speed); |
|
- | 787 | UART1_PutString(text); |
|
- | 788 | } |
|
- | 789 | SDC_Init(0); |
|
773 | } |
790 | } |
- | 791 | else |
|
- | 792 | { |
|
- | 793 | if(retryCounter == 1) UART1_PutString(" Problem solved\r\n"); |
|
774 | else break; // Success -> end retry loop |
794 | break; // Success -> end retry loop |
- | 795 | } |
|
Line 775... | Line 796... | ||
775 | 796 | ||
776 | } |
797 | } |
777 | if (retryCounter > SDCardWriteRetryCounterMax) |
798 | if (retryCounter > SDCardWriteRetryCounterMax) |
778 | { SDCardWriteRetryCounterMax = retryCounter; |
799 | { SDCardWriteRetryCounterMax = retryCounter; |