Reading sectors from SD Memory?

Discussion in 'Embedded Systems and Microcontrollers' started by spinnaker, Jan 1, 2012.

  1. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    I have been struggling trying to read anything to successfully read a sector on an SD memory chip. I have tried my own code that I pieced together with code I found scattered on the internet.

    It initializes and appears to read the sectors successfully, I am getting my 0XFE start of data token but when I compare it to what I see in a HexEditor on my PC on the same card the bytes don't jive. For example in sector one on this SD chip all bytes are supposed to be 0xFF but when I looked at it with my code on the PIC, all bytes are 0xFF with the exception that every 8th byte is a 0x04.

    Also I seem to need to read 1 extra byte reading one sector to get the next sector to read successfully. The extra byte is in addition to the 2 CRC bytes which I am reading and the 8 clock cycles needed at the end of a read.

    Which brings up another question. What type of CRC is this? I think it is supposed to be CRC16 but when I calculate CRC16 for the bytes from my PIC code or what is displayed on the HexEditor, nothing seems to match up.

    Can anyone provide any assistance?
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    I've only dealt with the filesystem level so data could be transferred to a PC by moving the card.

    What OS are you looking at the hex data with?

    If you use linux, and look directly at the device, you may get the same results as your PIC. Windows may be hiding "user doesn't need to see" data.
  3. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009

    I'm using Windows 7. I doubt if anything is being hidden. If you look at my results there seems to be a pattern in what I am seeing (or not seeing) on the Pic.
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    Try writing the sector with a different value and see what the checksum shows up as on both platforms.

    Is the checksum generated by the SD card, or by the PIC/PC?
  5. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    Tried that. I couldn't get it to write either. It took the command OK but never finished the write.

    I am hoping half of my issues are connection problems. I have a an SD connector soldered to a perf board. My pullup resistors are on a separate old proto board that has probably been pushed past it's useful life. I think what I will do is to solder the pullups on the perf board and do away with the proto board.
  6. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    Maybe try ordering one of the PIC Sticks, or whatever they are called that have an LCD on one side, and a PIC and SD connector on the other side?

    I'll try to find the source for that, I think it was the display/guts for an MP3 player, but on professional boards with mostly surface mount devices.

    That would give you a $25 "Known Working" platform to work backwards from.
  7. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    Yep that is what I was thinking. Microchip has one but they use their convoluted library. I'd like to have a 3rd party one out their with their own code.

    Sparkfun use to have one from a company out of England but looks like they no longer carry it.

    No LCD. An LCD would be nice.
    Last edited: Jan 2, 2012
  8. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    Here's a homemade one with project files.

    It's written in basic, and so is the library, their might be a tidbit or two of info in that area as well, I've found undocumented tricks in odder places.

  9. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    Thanks. I have seen a few of these around. What I really need is one I can purchase. I might just pop for one of those full fledged development boards assuming they come with working sample code. I will probably be in the same place again should I decide to do graphics. It is a lot easier when you have a know working setup.

    Strange this guy has no pullup resistors.

    I am back to square one now. I rewired by perf board now I have nothing working. Not sure what I did wrong.

    I am going to try and add a pullup to the SDO of the SD but from waht I have seen it is not supposed to be needed.
  10. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    I went ahead and ordered one of these

    only $10 shipped. At least I will know it is not now I have the SD hooked up with one of these.

    I am going to post another thread on the development board I have found.
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    I believe he can run that way because he is not monitoring either the Card Detect or Write Protect lines.
  12. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    I believe the specs call for a pull up on the SD memory's SDO.
  13. davebee

    Well-Known Member

    Oct 22, 2008
    A quick check you can use when reading sectors from a FAT formatted SD card is to look for the DOS signature $55AA as the last two bytes of sector 0. If they show up in bytes 510 and 511 of the result byte array then you're reading the card.

    Another thing you could do is use a PC to reformat the card, then write a small text file that contains some distinctive text patterns. Then use your SD read routine to search for the text in the sectors. The text should appear just past all the FAT directory sectors, which should be within probably just a few hundred sectors of the card.

    As far as I know, the data CRC is a standard CCITT CRC16, but I've never actually done any data CRC encoding or decoding. I have done command CRC7 encoding, and found that they use a little trick - after all the CRC encoding is done, they shift the results left by one and set bit 0, so unless you know to do that, you'll never get the algorithm to work.
  14. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    crc8 of the quoted post is E7
  15. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009

    I finally got things up and running again. I am indeed reading $55AA in 510 and 511. But I am still seeing the difference in data when compared to reading it with a hex editor on my PC as opposed to reading it with my Pic code. See Post #1.

    For example byte 508 of sector #1 is a $CB in the Hex Editor but when I read it from the Pic it is an $04.

    On my hex editor all bytes are $00 in sectors 1 and 2 but on the Pic I am seeing a $0F then a $04 about every 7 and 8th byte

    Here are the first 100 bytes or so of sector 1 from the pic

    Code ( (Unknown Language)):
    2. sector_buffer            
    3.                [0]                  0x00
    4.                [1]                  0x00
    5.                [2]                  0x00
    6.                [3]                  0x0F
    7.                [4]                  0x04
    8.                [5]                  0x00
    9.                [6]                  0x00
    10.                [7]                  0x00
    11.                [8]                  0x00
    12.                [9]                  0x00
    13.                [10]                 0x00
    14.                [11]                 0x0F
    15.                [12]                 0x04
    16.                [13]                 0x00
    17.                [14]                 0x00
    18.                [15]                 0x00
    19.                [16]                 0x00
    20.                [17]                 0x00
    21.                [18]                 0x00
    22.                [19]                 0x0F
    23.                [20]                 0x04
    24.                [21]                 0x00
    25.                [22]                 0x00
    26.                [23]                 0x00
    27.                [24]                 0x00
    28.                [25]                 0x00
    29.                [26]                 0x00
    30.                [27]                 0x0F
    31.                [28]                 0x04
    32.                [29]                 0x00
    33.                [30]                 0x00
    34.                [31]                 0x00
    35.                [32]                 0x00
    36.                [33]                 0x00
    37.                [34]                 0x00
    38.                [35]                 0x0F
    39.                [36]                 0x04
    40.                [37]                 0x00
    41.                [38]                 0x00
    42.                [39]                 0x00
    43.                [40]                 0x00
    44.                [41]                 0x00
    45.                [42]                 0x00
    46.                [43]                 0x0F
    47.                [44]                 0x04
    48.                [45]                 0x00
    49.                [46]                 0x00
    50.                [47]                 0x00
    51.                [48]                 0x00
    52.                [49]                 0x00
    53.                [50]                 0x00
    54.                [51]                 0x0F
    55.                [52]                 0x04
    56.                [53]                 0x00
    57.                [54]                 0x00
    58.                [55]                 0x00
    59.                [56]                 0x00
    60.                [57]                 0x00
    61.                [58]                 0x00
    62.                [59]                 0x0F
    63.                [60]                 0x04
    64.                [61]                 0x00
    65.                [62]                 0x00
    66.                [63]                 0x00
    67.                [64]                 0x00
    68.                [65]                 0x00
    69.                [66]                 0x00
    70.                [67]                 0x0F
    71.                [68]                 0x04
    72.                [69]                 0x00
    73.                [70]                 0x00
    74.                [71]                 0x00
    75.                [72]                 0x00
    76.                [73]                 0x00
    77.                [74]                 0x00
    78.                [75]                 0x0F
    79.                [76]                 0x04
    80.                [77]                 0x00
    81.                [78]                 0x00
    82.                [79]                 0x00
    83.                [80]                 0x00
    84.                [81]                 0x00
    85.                [82]                 0x00
    86.                [83]                 0x0F
    87.                [84]                 0x04
    88.                [85]                 0x00
    89.                [86]                 0x00
    90.                [87]                 0x00
    91.                [88]                 0x00
    92.                [89]                 0x00
    93.                [90]                 0x00
    94.                [91]                 0x0F
    95.                [92]                 0x04
    96.                [93]                 0x00
    97.                [94]                 0x00
    98.                [95]                 0x00
    99.                [96]                 0x00
    100.                [97]                 0x00
    101.                [98]                 0x00
    102.                [99]                 0x0F
    103.                [100]                0x04
    104.                [101]                0x00
    105.                [102]                0x00
    106.                [103]                0x00
    107.                [104]                0x00
    108.                [105]                0x00
    109.                [106]                0x00
    110.                [107]                0x0F
    111.                [108]                0x04
    112.                [109]                0x00
    113.                [110]                0x00
    114.                [111]                0x00
    115.                [112]                0x00
    116.                [113]                0x00
    117.                [114]                0x00
    118.                [115]                0x0F
    119.                [116]                0x04
    120.                [117]                0x00
    121.                [118]                0x00
    122.                [119]                0x00
    123.                [120]                0x00
    124.                [121]                0x00
    125.                [122]                0x00
    126.                [123]                0x0F
    127.                [124]                0x04
    This is pretty much confirmed by what I am seeing on my logic analyzer so it is the response of the SD chip and not how I am reading the SPI.

    Here is an overview of the two "problem" bytes.


    Here is a closeup of the $0F byte.


    Here is a closeup of the $04 byte

    Also I seem to need to read 3 additional bytes after the 512 block and 2 crc bytes to get every other sector to read correctly.
  16. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
  17. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    I'd post your logic analyzer shots of it there, in addition to the fact you are using a direct wired SD card only, and not trying to hack a reader from an MP3 player.

    Also, that thread is from 2007, you might get better luck starting a new thread.
  18. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    I finally am reading the SD chip without those crazy $F0 $04 bytes. Turns out it was the SD chip.

    But it gets stranger. When I read sector 0, among other text I am seeing Invalid partition table Error loading operating system Missing operating system

    in sector 0

    But when I use the HexEditor on the PC I dod not see Invalid in sector zero. In fact if the search is working properly, the word Invalid is no where to be found anywhere on the SD chip.
  19. spinnaker

    Thread Starter AAC Fanatic!

    Oct 29, 2009
    Strange 2 different chips now with those strange $0f $04 bytes.

    I can't see how I can have the same exact issue with 2 different chips if the fault is in the chips. It must be the code.
  20. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    The "Invalid Sector" would be an OS message, like the card isn't responding correctly.

    I'd try another card, $5, I'd also try a different card reader, that's one common thing that hasn't changed either.