• AVR Freaks

Hot![PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards?

Author
seccoxiru
Junior Member
  • Total Posts : 91
  • Reward points : 0
  • Joined: 2011/10/27 13:42:59
  • Location: 0
  • Status: offline
2018/11/01 16:15:06 (permalink)
0

[PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards?

I have several 4Gb microsd cards. For my application, compatibility from 8 to 32Gb cards is suitable.
For the first time I do have to interface a microsd card, using a MX470F512L running at 100MHz.
I don't need to have a file system, just need to write at the addresses/blocks of the card to generate a log.
I tried fat32 file system from MLA examples, but did not had sucess with it, the example was to create a txt file inside the card directory, but didnt worked even considering that I changed hardware setup to have compatibility with my board.
 
I noticed that there are different types of microSD cards, in terms of initialization sequence, and they support different commands or command sequence.
 
What can I do as a starting point to initialize my cards, and then be able to write/read on the card?
Is there a default initialization sequence?
 
Regards.
Jeferson
#1

8 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 9998
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: offline
    Re: [PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards? 2018/11/01 16:23:10 (permalink)
    4 (1)
    Can you give some specific examples of the different cards you are referring to?
    Is it perhaps the difference between SDHC and SDXC cards?
     

    Nearly there...
    #2
    seccoxiru
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2011/10/27 13:42:59
    • Location: 0
    • Status: offline
    Re: [PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards? 2018/11/01 16:35:05 (permalink)
    0
    qhb
    Can you give some specific examples of the different cards you are referring to?
    Is it perhaps the difference between SDHC and SDXC cards?



    Yes, SDHC, 4~32 Gb. These are the cards I would like to use.
    All them, also from different manucturers, have the same sequence of initialization in terms of SPI commands?
    #3
    andersm
    Super Member
    • Total Posts : 2605
    • Reward points : 0
    • Joined: 2012/10/07 14:57:44
    • Location: 0
    • Status: offline
    Re: [PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards? 2018/11/01 16:35:30 (permalink)
    4 (1)
    The SD memory card protocol and commands are described in the physical layer simplified specification you can download from the SD Association's website.
    #4
    seccoxiru
    Junior Member
    • Total Posts : 91
    • Reward points : 0
    • Joined: 2011/10/27 13:42:59
    • Location: 0
    • Status: offline
    Re: [PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards? 2018/11/02 12:54:41 (permalink)
    0
    could somebody explain what compose a SDHC command in a simply way? A SDHC command is a single SPI write? or a SDHC command is composed of multiple SPI writes? (do the commands have arguments?)
    #5
    DavidBLit
    Super Member
    • Total Posts : 1574
    • Reward points : 0
    • Joined: 2012/02/18 13:08:48
    • Location: The Land of Confusion
    • Status: offline
    Re: [PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards? 2018/11/02 13:53:24 (permalink)
    4 (1)
    seccoxiru
    could somebody explain what compose a SDHC command in a simply way? A SDHC command is a single SPI write? or a SDHC command is composed of multiple SPI writes? (do the commands have arguments?)

    Have you read the spec?

    Yeah, "//Code and stuff".
    #6
    NorvisLM
    Super Member
    • Total Posts : 80
    • Reward points : 0
    • Joined: 2015/10/06 07:32:11
    • Location: 0
    • Status: offline
    Re: [PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards? 2018/11/02 14:45:09 (permalink)
    0
    Commands are a single byte followed by a 32 bit argument field, followed by a checksum.
     
    Responses vary in length depending on the command.
     
     
    #7
    jg_ee
    Super Member
    • Total Posts : 148
    • Reward points : 0
    • Joined: 2015/04/30 10:54:52
    • Location: Colorado
    • Status: offline
    Re: [PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards? 2018/11/02 14:49:48 (permalink)
    0
    Here is a teaser from the demo app of the most popular FAT file system, FatFS from elmchan which you can download as reference.
     

    /* Send command packet */
     xmit_spi(0x40 | cmd); /* Start + Command index */
     xmit_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
     xmit_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
     xmit_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
     xmit_spi((BYTE)arg); /* Argument[7..0] */
     n = 0x01; /* Dummy CRC + Stop */
     if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
     if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
     xmit_spi(n);

     /* Receive command response */
     if (cmd == CMD12) rcvr_spi(); /* Skip a stuff byte when stop reading */
     n = 10; /* Wait for a valid response in timeout of 10 attempts */
     do
      res = rcvr_spi();
     while ((res & 0x80) && --n);

     
    Google "Elm-Chan FatFs"
    #8
    LostInSpace
    Super Member
    • Total Posts : 251
    • Reward points : 0
    • Joined: 2016/03/11 22:47:59
    • Location: 0
    • Status: offline
    Re: [PIC32MX-SD] Is there a "most common" initialization sequence for microSD cards? 2018/11/02 18:57:34 (permalink)
    5 (1)
    As one who just implemented SD Card interface on a project - here is what I would do.
     
    1) Take a look at FatFS - It is Open Source, on the Web and quite easy to understand C code. You can take a look at how it implemented the initialization sequence. Compare this to the SD Card specifications. It will start to make sense.
     
    Or,
    2) I understand your reluctance to use the Fat File System, especially when your initial attempt did not work. I too had this idea as a backup plan if I could not get the Fat File System going. Fortunately I found the some FatFs examples that worked (see below). Just think of the interoperability and ease of use if you can examine data easily on a PC directly without some other translation software because you are storing the data on a standard File System.
     
    With this in mind I found that the example posted here works,
    Microchip uses FatFS as the basis for their SD Card driver- you can find FatFS buried in their code.
    microchipdeveloper.com/harmony:audio-player-lab2
    This example shows you how to setup Harmony and the other drivers properly. 
    Then to get this working you can copy the application code from the example application "sdcard_fat_single_disk" App.c and you will have a working example in a short period of time.
     
    When I had this working it proved that my hardware did indeed work, but this example uses the Harmony Dynamic drivers - this would not work for me in my final application - So I found an example of FatFs using register SPI Calls.
     
    This uses FatFS directly and uses Register based calls to do the SPI (and no interrupts). I was able to quickly understand this implementation and rewire just a few calls to use my Static SPI implementation.
    tutorial.cytron.io/2017/09/17/fatfs-for-pic32mxmz/
    This tutorial (above) does use a timer for timeouts, but it is written in such a way as to not require the timer, if you never timeout - which was a good assumption for my exact application. Your mileage may vary.
     
    Based on these two examples I was able to get the latest version of FatFs directly from the author, make about 6 changes in his code and I was done.
     
    Hopefully this helps you also.  :-)
    #9
    Jump to:
    © 2019 APG vNext Commercial Version 4.5