Author Topic: SD Card not formatting  (Read 14242 times)

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3187
    • View Profile
    • uTasker
Re: SD Card not formatting
« Reply #15 on: March 26, 2015, 04:59:18 AM »
Hi Neil

I fixed the HW and now have results with a Kinetis in SPI mode:

SD-card MARK (1534066688 kBytes) FAT32
Bytes per sector: 512
Cluster size: 4096
Directory base: 0x00000002
FAT start: 0x0000005f
FAT size: 0x00000ef3
Number of FATs: 2
LBA: 0x00001e45
Total clusters: 0x00077737
Info sect: 0x00000040
Free clusters: 0x00077723
Next free: 0x00000016
CSD: 0x7f 0x00 0x32 0x5b 0x5a 0x83 0xbd 0x6d 0xb7 0xff 0x80 0x0a 0x80 0x00 0x8d 0xe5


Compare with
CSD: 0x00 0x7f 0x00 0x32 0x5b 0x5a 0x83 0xbd 0x6d 0xb7 0xff 0x80 0x0a 0x80 0x00 0x00
as read in SDHC mode; it looks to be shifted.

Notice that the size is huge (1'534 GBytes) and the CSD is different.
The FAT information is the same (since it was formatted by the PC and can be read correctly).

The problem is that when the card is formatted it will be done so based on its size (read from the CSD register) and so will result in a very large FAT and it goes on for a long time and the end result is obviously not correct.

You once had a similar probelm with a shift in the CSD being read - I have one of the cards with this problem somewhere but didn't find it just now. Do you remember whether they it was the same make??

Why some cards read the value shifted (by a byte and seemingly only in SPI mode) is a mystery. I wonder whether information could be received from the manufacturer about this phenomenon?
Otherwise it may be worth working out a technique to identify that the read content is false and correlate to what is expected and thus correct the mis-interpretation. This would allow the correct size to be known and then the formatting to be performed normally (rather than formatting as if it were a >1TByte card).

This would then 'rescue' the cards that you have with this problem.

Could you maybe check that the ones that won't format (seemingly go on forever) read the same CSD and that all 'good' ones read it correctly (without the shift)?

Regards

Mark

P.S: Since I get the same behaviour with a Kinetis it is not processor type related.


Offline neil

  • Sr. Member
  • ****
  • Posts: 433
    • View Profile
Re: SD Card not formatting
« Reply #16 on: March 26, 2015, 10:11:21 AM »
Hi Mark,
  Thanks for the reply.

I will be back in the office beginning of next week so will be able to test the cards I have.

Regarding the previous SD cards that gave the same problem, I cant remember what the manufacturer is, sorry.

Strange with these cards , I purchased 20 (all same brand) from the same supplier, and probably half worked.

Do you think you can have a fix for these?


Regards
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3187
    • View Profile
    • uTasker
Re: SD Card not formatting
« Reply #17 on: March 27, 2015, 02:18:47 AM »
Hi Neil

I did a bit of research. The CSD is defined in the "SD Specifications Part 1 Physical Layer Simplified Specification Version 3.01 May 18, 2010" and is 128 bits long. It is however preceeded by a preamble which is also read. Therefore I also checked this and saw the following differences (your card is the third and I have shown it shifted, compensating for the missing start byte):

CSD: 0xff 0xfe 0x40 0x0e 0x00 0x32 0x5b 0x59 0x00 0x00 0x3b 0x37 0x7f 0x80 0x0a 0x40 0x40 0xaf - 8G version 2
CSD: 0xff 0xfe 0x40 0x0e 0x00 0x32 0x5b 0x59 0x00 0x00 0x1d 0x8a 0x7f 0x80 0x0a 0x40 0x40 0xb9 - 4G version 2
CSD:      0xfe 0x00 0x7f 0x00 0x32 0x5b 0x5a 0x83 0xbd 0x6d 0xb7 0xff 0x80 0x0a 0x80 0x00 0x8d (0xe5) - 2G version 1
CSD: 0xff 0xfe 0x00 0x4f 0x00 0x32 0x5f 0x59 0xa3 0xcb 0xff 0xff 0xff 0x8f 0x8a 0x40 0x00 0x29 - 1G version 1
     ---(start)[                          CSD   128 bits (16 bytes)                          ]
             version
                  access times
                             speed
                                                                                           CRC-7


Although the first byte is missing it is in fact quite easy to identify that the pre-amble byte is at the wrong location. Also the content can be checked using the CRC-7 at the end to be absolutely sure.


Just after reading the CSD into the buffer called ucData[] (fnSendSD_command(ucSEND_CSD_CMD9, &ucResult, ucData))) I have added this code:

            #if !defined SD_CONTROLLER_AVAILABLE
            if (fnCheckCSD(ucData) != 0) {
                uTaskerStateChange(OWN_TASK, UTASKER_ACTIVATE);          // schedule the task to retry
                _return;                                                 // not recognised or there was a CRC error
            }
            #endif


The routine fnCheckCSD() checks for a shifted start and corrects it in the buffer if found. It also checked the CRC-7 of the content. In case the CRC-7 were bad it would repeat the read until it is correct.

This routine is below and can be located anywhere before the task in the file mass_storage().
I could then read all of these cards, including yours, and get the correct CSD content. Then I could also re-format your card successfully.

SD-card MARK (2008023040 bytes) FAT32
Bytes per sector: 512
Cluster size: 4096
Directory base: 0x00000002
FAT start: 0x0000005f
FAT size: 0x00000ef3
Number of FATs: 2
LBA: 0x00001e45
Total clusters: 0x00077737
Info sect: 0x00000040
Free clusters: 0x00077735
Next free: 0x00000004
CSD: 0x00 0x7f 0x00 0x32 0x5b 0x5a 0x83 0xbd 0x6d 0xb7 0xff 0x80 0x0a 0x80 0x00 0x8d


This uses the function uReverseMemcpy(), which you may not have in your Coldfire version so I have also added the SW implementation below (Kinetis uses DMA).

Good luck!

Regards

Mark


Code: [Select]
#if defined SDCARD_SUPPORT && !defined SD_CONTROLLER_AVAILABLE
static int fnCheckCSD(unsigned char ucData[18])
{
    unsigned char ucCRC = 0;
    int iOffset;
    int i, j;
    unsigned char ucByte;

    if (ucData[0] == 0xfe) {                                             // if a synchronisation is found at the start of the buffer we assume that the SD card returned the CSD content one byte earlier than normal
        iOffset = 1;
    }
    else {
        iOffset = 2;                                                     // normal location for the content start
    }
    for (i = 0; i < 15; i++) {                                           // check the CRC-7 of the content
        ucByte = ucData[iOffset++];
        for (j = 0; j < 8; j++) {                                        // for each bit of the byte
            ucCRC <<= 1;
            if ((ucByte & 0x80) ^ (ucCRC & 0x80)) {
                ucCRC ^=0x09;
            }
            ucByte <<= 1;
        }
    }
    if ((ucCRC & 0x7f) == (ucData[iOffset] >> 1)) {                      // check that the received CRC-7 matches with the value calculated for the content
        if (iOffset == 16) {
            uReverseMemcpy(&ucData[2], &ucData[1], 16);                  // shift the content to the expected location in case it was originally shifted
        }
        return 0;                                                        // CRC-7 is correct and the data is in the correct location
    }
    else {
        return -1;                                                       // invalid CRC-7
    }
}
#endif


Code: [Select]
// uMemcpy() executed in a reverse order to allow shifting buffers to the right
//
extern void *uReverseMemcpy(void *ptrTo, const void *ptrFrom, size_t Size)
{
    void *buffer = ptrTo;
    unsigned char *ptr1 = (unsigned char *)ptrTo;
    unsigned char *ptr2 = (unsigned char *)ptrFrom;
    ptr1 += Size;                                                        // move to the end of the buffers
    ptr2 += Size;

    while (Size--) {
        *(--ptr1) = *(--ptr2);                                           // copy backwards
    }

    return buffer;
}
« Last Edit: March 27, 2015, 04:18:36 AM by mark »

Offline neil

  • Sr. Member
  • ****
  • Posts: 433
    • View Profile
Re: SD Card not formatting
« Reply #18 on: March 27, 2015, 09:55:10 AM »
Hi Mark,
  Thanks very much for sorting this issue, I will update my code when I am back in the office on Monday. Are you planning to update the mass_storage.c file and place on your download page? If so I can simply download the whole lot, saving me modifying your code (in case I update the wrong part of your code)

Many Thanks again

Regards
Neil

Offline mark

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3187
    • View Profile
    • uTasker
Re: SD Card not formatting
« Reply #19 on: March 27, 2015, 10:50:59 AM »
Neil

It is best if you modify the code since there may be a number of other changes in my code base.
I have however sent my complete mass_storage.c by email as reference.

I also tested your card (sucessfully) on a different board in the meantime (during other work).

Regards

Mark

Offline neil

  • Sr. Member
  • ****
  • Posts: 433
    • View Profile
Re: SD Card not formatting
« Reply #20 on: March 27, 2015, 11:13:31 AM »
Hi Mark,
   That's good news, glad its all working.  Just keep or throw away the card I wont need it, as I will now use the cards that weren't formatting.

Thanks again.

Regards
Neil

Offline neil

  • Sr. Member
  • ****
  • Posts: 433
    • View Profile
Re: SD Card not formatting
« Reply #21 on: March 30, 2015, 01:33:51 PM »
Hi Mark,
  Just to let you know, I have tried a card here and works well. I will now try the rest of the bunch, but don't think there will be any problems.

Thanks again for your help

Regards
Neil