• AVR Freaks

sd-card writing raw without filesystem

Author
prinz3nroll3
Super Member
  • Total Posts : 270
  • Reward points : 0
  • Joined: 2007/03/14 09:38:25
  • Location: germany
  • Status: offline
2009/06/30 09:32:44 (permalink)
0

sd-card writing raw without filesystem

Hello,
i need some help urgent.
 
we have buy many SD Card Sandisk Ultra II with 2 GB.
 
We design an board with an pic18f67j10 that use the sd card like an flash eeprom.
An File system is not necessary. i write the data direct on the sectors (raw)
 
the first attemps all works great, i can read and write without any problmens.
but at once it is impossible to write on the card. 
i plug the card in the computer and it is impossible to write or format the sd-card.
i write for testing purpose some byte on sector 0, with the program (HxD) under windows, the data was written,
but after an reload (f5) the changes was rejected.
-> the card stops working with the microcontroller and computer
 
the write cycles was not above 5000.
 
is the card locked or what happend?
 
i can read the complete card with HxD, but i cant write under windows
and although the microcontroller cant write anything?
 
Why?
What happend?
 
Plz help me!
 
 
thx for your reply
#1

12 Replies Related Threads

    asmallri
    Super Member
    • Total Posts : 1864
    • Reward points : 0
    • Joined: 2004/05/26 09:00:05
    • Location: Perth, Australia
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/06/30 15:20:13 (permalink)
    0
    When you are working with the PIC your are probably writing to physical addresses. When you are writing with test utilities on the PC you are writing to logical addresses w.r.t the start of the partition of the filesystem. The two are not necessarily the same.

    Regards, Andrew

    http://www.brushelectronics.com/index.php?page=software
    Home of Ethernet, SD Card, and Encrypted Serial and USB Bootloaders for PICs!!
    #2
    eddygo
    Super Member
    • Total Posts : 691
    • Reward points : 0
    • Joined: 2004/03/04 15:45:37
    • Location: Bucharest, ROMANIA
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/06/30 16:30:05 (permalink)
    3.5 (2)
    I write raw also and never happened by pic mistake/my drivers.
    Maybe is PC or windoze problem.... if your card reader is Realtek based chipset... consider'it garbage. I spent once all day to figure out how can I lock (accidentally) my card in some project (simmilar your description), I analyzed and re-analyzed my pic raw driver. Finally I bought another two (unbranded) card readers and everithing is working great Smile

    Interesting, since then, my (inside notebook) card reader is able to read any card but at format stage is saying something that 'Format is impossible... bla bla'... maybe my xp and/or card reader driver crashed... who knows... I preffer to buy another $10 reader... many manufacturers these days are forced by micro***ft to *not* provide XP (well written) drivers anymore.

    Hope helps,
    Edi

    PS. Unless you haven't play with lock commands or write protect sectors (some cards has ability to write protect some blocks) I think isn't SD problem at all. Just read CSR register immediatelly after write a 512 block or after erase cmd. You should get R2 = 0x0000.

    I strongly recommended you to read CSR after each write/erase op, to see what errors may appear. It's possible card reply 'ok' to write command but due to lock/other to actually not write all/partial.

     
    post edited by eddygo - 2009/06/30 16:35:21

    ENEA, OSECK
    http://www.enea.com
    #3
    prinz3nroll3
    Super Member
    • Total Posts : 270
    • Reward points : 0
    • Joined: 2007/03/14 09:38:25
    • Location: germany
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/06/30 22:37:55 (permalink)
    0
    Hello,
    thanks for the fast answers!
    the HxD tool for windows list the sectors and not the partition sectors.
    the next problem is that the pic cant although write through the card.
    the write to the card works without an error (response 5) but
    the card content dont change.
    i check how much work it is to implement the csr register read after an sector write.

    somebody else an idea or an explanation???


    #4
    asmallri
    Super Member
    • Total Posts : 1864
    • Reward points : 0
    • Joined: 2004/05/26 09:00:05
    • Location: Perth, Australia
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/07/01 03:10:12 (permalink)
    0
    If the PIC appears to be writing without errors then either you are not writing to the correct address or you are not reading the address the PIC is writing to.


    Regards, Andrew

    http://www.brushelectronics.com/index.php?page=software
    Home of Ethernet, SD Card, and Encrypted Serial and USB Bootloaders for PICs!!
    #5
    eddygo
    Super Member
    • Total Posts : 691
    • Reward points : 0
    • Joined: 2004/03/04 15:45:37
    • Location: Bucharest, ROMANIA
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/07/01 03:51:17 (permalink)
    0
    First of all, get a good well known hexeditor like HexWorkshop or WinHex. Both can read and convenient display physically SD disks.

    Second, you said you have SD, so your addressing scheme is absolute -> you have to compute yourself to align to card block size. Minimum block size is 512 B due to NAND memory technology and you can read this info from CSD register. Now, if you don't touch CSD (some cards allow to change block size to 512, 1024, 2048, etc) any card will support 512 bytes block even if their default block size is set to 1024 or 2048. We tested several dozens from 256MB to 16GB. So resuming, for SD you can use addresses:

    - first block = 0
    - 2'nd block = 512
    - 3'rd block = 1024

    Remember, you cannot write misaligned blocks even they are 512 bytes size, so you must provide correct (start) address of desired block in order writing to complete. It's a good thing to read CSR register (CMD13 if I remember well) to see any errors may appear, including illegal address.
    post edited by eddygo - 2009/07/01 03:58:41

    ENEA, OSECK
    http://www.enea.com
    #6
    prinz3nroll3
    Super Member
    • Total Posts : 270
    • Reward points : 0
    • Joined: 2007/03/14 09:38:25
    • Location: germany
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/07/02 01:55:05 (permalink)
    0
    ORIGINAL: eddygo

    First of all, get a good well known hexeditor like HexWorkshop or WinHex. Both can read and convenient display physically SD disks.

    HxD allthough and it is free ;)

    Second, you said you have SD, so your addressing scheme is absolute -> you have to compute yourself to align to card block size. Minimum block size is 512 B due to NAND memory technology and you can read this info from CSD register. Now, if you don't touch CSD (some cards allow to change block size to 512, 1024, 2048, etc) any card will support 512 bytes block even if their default block size is set to 1024 or 2048. We tested several dozens from 256MB to 16GB. So resuming, for SD you can use addresses:

    - first block = 0
    - 2'nd block = 512
    - 3'rd block = 1024

    Remember, you cannot write misaligned blocks even they are 512 bytes size, so you must provide correct (start) address of desired block in order writing to complete. It's a good thing to read CSR register (CMD13 if I remember well) to see any errors may appear, including illegal address.

    thx, yes i allways write only 512byte every 512 bytes. i know about the misalignment.
    i m testing now with an transcend card until yet now error.

    #7
    eddygo
    Super Member
    • Total Posts : 691
    • Reward points : 0
    • Joined: 2004/03/04 15:45:37
    • Location: Bucharest, ROMANIA
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/07/02 03:49:10 (permalink)
    0
    Take care, with Transcend we had very strange problems:

    1) SD model (consumer ?) 1 GB : If you read CID register before CSD, at erase stage (CMD32,33,38) the card will refuse to erase, returning R2=0x0002 meaning WP EraseSkip, Lock/Unlock Cmd Failed. Workaround found so far, read CSD & CID consecutive and in this order.

    2) SDHC model, class 6, 4GB : Using multiple block write (CMD25), we measured write time (delta, including mcu stuff) for 2 blocks using external logic analyzer connected to some PIC out debug pin. The strange thing was this time differs (a lot) from block no# to block no#. For example after eraseing all card, T~40mS from block 0 -> 6, then from block 7 -> abt. 1500, when returning to wite block 0 (our descriptor for raw) write time was T~220mS. If jumping at block ~12000 works fine again. Never found workaround. 

    i m testing now with an transcend card until yet now error.


    Post your write logic flow.
    post edited by eddygo - 2009/07/02 03:51:08

    ENEA, OSECK
    http://www.enea.com
    #8
    prinz3nroll3
    Super Member
    • Total Posts : 270
    • Reward points : 0
    • Joined: 2007/03/14 09:38:25
    • Location: germany
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/07/02 05:32:15 (permalink)
    0
    Hi eddygo,
    thanks again for your answer.
    i ve some other problem with the erase cmd.
    i can erase sectors but i dont know how can i get the response R1b.
    i look at the spec and r1b == r1 with an optional busy signal.
    but i read every time an 0xFF as r1b

    BYTE MDD_SDSPI_Sector_Erase(DWORD sect_start,DWORD sect_end)
    {
      MMC_RESPONSE response;
      BYTE data_response;   
     
      response = SendMMCCmd(TAG_SECTOR_START,sect_start << 9);
      if(response.r1._byte != 0x00)
          return 0;

      response = SendMMCCmd(TAG_SECTOR_END,sect_end << 9);
      if(response.r1._byte != 0x00)
          return 0;
      response = SendMMCCmd(ERASE,0x00);
      data_response = MDD_SDSPI_ReadMedia();
      if(response.r1._byte != 0x00)
          return 0;

      return 1;   
    }

    the code above is written by me
    response (r1b) is allways != 0x00;

    the cmd write function

    MMC_RESPONSE SendMMCCmdManual(BYTE cmd, DWORD address)
    {
      WORD timeout = 0x8;
      BYTE index;
      MMC_RESPONSE    response;
      CMD_PACKET  CmdPacket;
     
      SD_CS = 0;                           //Card Select
     
      // Copy over data
      CmdPacket.cmd        = sdmmc_cmdtable[cmd].CmdCode;
      CmdPacket.address    = address;
      CmdPacket.crc        = sdmmc_cmdtable[cmd].CRC;       // Calc CRC here
     
      CmdPacket.TRANSMIT_BIT = 1;             //Set Tranmission bit
     
      WriteSPIManual(CmdPacket.cmd);                //Send Command
      WriteSPIManual(CmdPacket.addr3);              //Most Significant Byte
      WriteSPIManual(CmdPacket.addr2);
      WriteSPIManual(CmdPacket.addr1);
      WriteSPIManual(CmdPacket.addr0);              //Least Significant Byte
      WriteSPIManual(CmdPacket.crc);                //Send CRC
     
      // see if we are going to get a response
      if(sdmmc_cmdtable[cmd].responsetype == R1 || sdmmc_cmdtable[cmd].responsetype == R1b)
      {
          do
          {
              response.r1._byte = ReadMediaManual();
              timeout--;
          }while(response.r1._byte == MMC_FLOATING_BUS && timeout != 0);
      }
      else if(sdmmc_cmdtable[cmd].responsetype == R2)
      {
          ReadMediaManual();
         
          response.r2._byte1 = ReadMediaManual();
          response.r2._byte0 = ReadMediaManual();
      }

      if(sdmmc_cmdtable[cmd].responsetype == R1b)
      {
          response.r1._byte = 0x00;
         
          for(index =0; index < 0xFF && response.r1._byte == 0x00; index++)
          {
              timeout = 0xFFFF;
             
              do
              {
                  response.r1._byte = ReadMediaManual();
                  timeout--;
              }while(response.r1._byte == 0x00 && timeout != 0);
          }
      }

      WriteSPIManual(0xFF);                      //Required clocking (see spec)

      // see if we are expecting data or not
      if(!(sdmmc_cmdtable[cmd].moredataexpected))
          SD_CS = 1;

      return(response);
    }

    the code is from the mdd library.
    post edited by prinz3nroll3 - 2009/07/02 05:34:09
    #9
    eddygo
    Super Member
    • Total Posts : 691
    • Reward points : 0
    • Joined: 2004/03/04 15:45:37
    • Location: Bucharest, ROMANIA
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/07/02 12:12:05 (permalink)
    0
    i can erase sectors but i dont know how can i get the response R1b.
    i look at the spec and r1b == r1 with an optional busy signal.
    but i read every time an 0xFF as r1b


    Either erase is very quick (improbable, fastest I've seen had ~100mS) or you actually don't erase anything or just partial. Immediate after ERASE just check R2 which is response to CMD13 (read CSR register), If CSR=0 then you indeed succeed. SD datasheets are very annoying, read very patient.

    R1b is in fact R1 but followed by BUSY signal. This BUSY signal is nothing else than keeping Dout line at low state. After tagging START and END blocks (I prefer blocks rather than sectors) and after ERASE cmd (you must follow this order) the card will acknoledge erase cmd by sending R1, and then, will begin to erase signaling with BUSY.

    It's important to expect replies from all commands because even they are aligned to 8 bits on your SCK, can appear next byte or next two bytes after sending cmd. So this is CMD38 scenario I use:

    - send cmd38, args = 0, crc=off (use dummy 0xFF)
    - send another dummy 8 SCK (it's NCR, check specs, required)
    - watch in small loop to catch R1 (anything else than 0xFF)
           - if 0xFF means timeout, fail to send CMD38
           - if 0x00 we can continue, card just understood CMD38
           - anything else than R1=0x00 means error, analyze
    - asumming R1=0x00, card should be BUSY now, so reading data you'll get 0x00
      Wait in large loop (up to 15 Sec) to catch 0xFF
           - when 0xFF, no more BUSY, erase is done
           - if still 0x00 something is wrong, still BUSY (never happened after 15 Sec any card)

    Now even everithing is OK, you must send CMD13 and check CSR=0 to declare success. There is another command I can't remember now which return actually well written or erased blocks but I never used. I always erase all card by computing END from info provided by CSD (START=0) and works perfect.

    I guess you miss NCR (pading with extra SCK your commands). Also, make sure your Din (card Dout) is pulled up with 100k.



     

    ENEA, OSECK
    http://www.enea.com
    #10
    prinz3nroll3
    Super Member
    • Total Posts : 270
    • Reward points : 0
    • Joined: 2007/03/14 09:38:25
    • Location: germany
    • Status: offline
    RE: sd-card writing raw without filesystem 2009/07/07 01:35:37 (permalink)
    0
    Hello eddygo,
    wow thanks a lot for your great answer.
    i ve implement the cmd13 and check it for 0x00.
    the erase/write/read works now.
    thanks again a lot for your answer.

    #11
    DarthVader
    New Member
    • Total Posts : 1
    • Reward points : 0
    • Joined: 2010/12/08 10:44:17
    • Location: 0
    • Status: offline
    Re: RE: sd-card writing raw without filesystem 2010/12/08 11:17:17 (permalink)
    0
    Can anyone please guide me how to use SD card without file system?  Is there a link or example project somewhere that I can use as a reference?
    #12
    moty22
    New Member
    • Total Posts : 2
    • Reward points : 0
    • Joined: 2010/08/17 12:35:49
    • Location: 0
    • Status: offline
    Re: RE: sd-card writing raw without filesystem 2010/12/09 15:22:15 (permalink)
    0
    DarthVader

    Can anyone please guide me how to use SD card without file system?  Is there a link or example project somewhere that I can use as a reference?


    I have a simple project using SD Card and PIC 16F876 without file system. You can find details on my website.
    http://www.moty22.co.uk/sd.php?
    #13
    Jump to:
    © 2019 APG vNext Commercial Version 4.5