SDHC (FAT32 for SD cards)


Closed Thread
Results 1 to 23 of 23
  1. #1
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924

    Default SDHC (FAT32 for SD cards)

    IF
    You have used SDFS3 from MeLabs
    http://www.melabs.com/resources/samples/pbp/sdfs3.zip AND
    Now you would like to be able to use High Capacity cards THEN
    Try SDFSHC32d
    sdfshc32d.zip
    ENDIF


    I have been using it with a 4 gig card. The only change I had to make in my code from MELABS SDFS was the INCLUDE statement and in SDFSHC32d change the pin aliases to match my hardware.

    Thank you Jeremy for making the modifications.
    Last edited by mackrackit; - 4th November 2010 at 11:52. Reason: File location update
    Dave
    Always wear safety glasses while programming.

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    From Jeremy::::

    NOTE:
    Using SDFSHC32 Revision D,
    IF (Your PIC is running at 40Mhz) AND (You are using the DEFINE FS_SPEED) AND (Maybe your PCB and/or wiring isn't quite 'up-to-snuff') THEN
    You may have initialization/writing problems which WILL corrupt your SD card
    ENDIF

    Workaround: Don't use "DEFINE FS_SPEED".

    The problem is either a R/M/W issue or SD card timing problems at higher PIC speeds.
    END-NOTE
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default

    IF (Your PIC is running at 40Mhz) AND (You are using the DEFINE FS_SPEED) AND (Maybe your PCB and/or wiring isn't quite 'up-to-snuff') THEN
    You may have initialization/writing problems which WILL corrupt your SD card
    ENDIF
    It is recommended to initialize at slower speed and switch to faster for read/writes.
    Also the clock pin sucks power especially with the larger size cards.
    If you are using a resistor voltage divider to convert to 3 volts use lower values to maintain signal strength.

    Is code SPI(one bit at a time) or SD Bus(4 bits at a time) mode?
    CRC(required for SD bus mode)?
    Multiblock read and write for speed?

    Norm
    Last edited by Normnet; - 30th August 2009 at 16:45.

  4. #4
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Normnet View Post
    It is recommended to initialize at slower speed and switch to faster for read/writes.
    Also the clock pin sucks power especially with the larger size cards.
    If you are using a resistor voltage divider to convert to 3 volts use lower values to maintain signal strength.
    I did not know that about the larger cards.
    Is code SPI(one bit at a time) or SD Bus(4 bits at a time) mode?
    CRC(required for SD bus mode)?
    Multiblock read and write for speed?

    Norm
    I can not exactly answer your questions. My role in this with the original SDFS and this one has been trying to break it.
    This is from the SDFS readme. Might help.
    The PICBASIC PRO programs included in this zip allow reading and writing to
    SD (Secure Digital) and MMC (Multi Media) cards using the FAT16 file system.
    The code was written based on information from the Microchip USB framework
    and Jan Axelson's book, USB Mass Storage. Details on SD and MMC cards and
    the FAT file system can be found there.

    The included code does not actually use the security features of SD cards.
    It reads and writes to the cards using SPI in MMC mode. While the code can
    certainly be modified to take advantage of the secure digital features, a
    license to the SD consortium would need to be paid. Information about this
    can be found online.

    Speaking of licenses, the FAT file system is owned by Microsoft and must be
    licensed from them if you intend to sell a product that uses it. The license
    fee looks to be fairly nominal. More information can be found on Microsoft's
    web site.
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default

    Also the clock pin sucks power especially with the larger size cards.
    I did not know that about the larger cards.
    At least a 2 gig card compared to a 512 mb.

    Norm

  6. #6
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    From Jeremy
    The code does initialize at slow speed, roughly 50Khz. Then switches to whatever mode you set up after that, could be shiftin/shiftout (77Khz+ or so), bit-banged SPI (about 150khz @ 40Mhz) up 10Mhz using hardware SPI.

    Good info with the resistor voltage divider with larger cards. Noted. I'll try out a few cards, throw a meter on them, and see what I come up with for comparison.

    One-bit/SPI mode only. 4-bit mode/SD bus mode, takes a lot more programming than I want to deal with! Not to mention the processing time it would take to calculate CRC's 'on-the-fly'. Gotta draw the line somewhere.

    No CRC's used except during initialization, then only pre-calculated CRC's are used. You can disable CRC's in SPI mode.

    Single block commands, one sector at a time. Any more an you'd run out of ram on the PIC rather quickly. Eventually, I'll get my hands on a static ram I2C chip from Microchip and integrate that into SDFSHC32 somehow to make multi-block reads/write possible. Even then, you'd be limited to the maximum block size set by the card itself.

    All of this info will be fed back into the NOTES file.
    Dave
    Always wear safety glasses while programming.

  7. #7
    Join Date
    Oct 2004
    Posts
    440


    Did you find this post helpful? Yes | No

    Default

    See PIC iPod wav player for my FAT16 SD card reader.
    Reads and plays mono CD quality (16bit 44k) or stereo 8bit 44k from SD cards.
    Utilizes multiblock for fast sequential read and play of wav data.
    Also data for Nokia GLCD pictures or scrolling menu.

    The code was written based on information from the Microchip USB framework
    and Jan Axelson's book, USB Mass Storage. Details on SD and MMC cards and
    the FAT file system can be found there.
    Does Jan's book have any insight on 4 bit Bus mode?
    I have the CRC routine worked out but haven't had success with Bus mode yet.

    Norm

  8. #8
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    More from Jeremy:::
    See PIC iPod wav player for my FAT16 SD card reader.
    Reads and plays mono CD quality (16bit 44k) or stereo 8bit 44k from SD cards.
    Utilizes multiblock for fast sequential read and play of wav data.
    Also data for Nokia GLCD pictures or scrolling menu.
    Does Jan's book have any insight on 4 bit Bus mode?
    I have the CRC routine worked out but haven't had success with Bus mode yet.
    Agreed...Multi-block transfers aren't hard to program at all, and a heck of a lot faster than the single block mode used right now.
    Problem is the PIC doesn't have the room to store much of it! Therefore, at present, it wasn't worth the time to write that code.
    When I get my hands on one of those I2C SRAM chips, I'll get on it.
    Same thing for the GLCD/menu type stuff, and decoding BMPs (easy), JPEGs (hard), etc. Not enough room to store everything.

    Don't know about Jan's book and the SD bus mode. Haven't played with that, but I've got datasheets on it, and I think I've got decent CRC5/CRC16 (?) routines laying around somewhere.
    I'm concentrating on keeping the hardware to a bare minimum for now (5 pins vs 9 pins).
    Again, when I get that I2C SRAM chip, I'll expand out the code to handle multi-block read/write/storage, SD-bus, and whatever else I can think of.

    Found the fix for the failures relating to FS_SPEED:

    In the SectorWrite subroutine (the fix is in bold 9 lines down):
    Code:
    @ ifdef FS_SPEED
                ' Write a sector's worth of data. (optimized for speed)
                if (!FAT_FSfast) then
                    For SDC_index = 0 To (MEDIA_SECTOR_SIZE - 1)
                        SDC_data_out = SDC_buffer[SDC_index] : gosub writespim    ' Write Byte SDC_data_out.
                    next SDC_index
                else    'fast clock toggler. may have to add extra SCL = 1 : SCL = 0 if running into R/M/W issues
                    for SDC_index = 0 to (MEDIA_SECTOR_SIZE - 1)
                        FAT_sdbyteout = SDC_buffer[SDC_index] : sdo = FAT_sdbyteout.7 : scl = 1 : scl = 0 : sdo = FAT_sdbyteout.6 : scl = 1
                        scl = 0 : sdo = FAT_sdbyteout.5 : scl = 1 : scl = 0 : sdo = FAT_sdbyteout.4 : scl = 1 : scl = 0
                        sdo = FAT_sdbyteout.3 : scl = 1 : scl = 0 : sdo = FAT_sdbyteout.2 : scl = 1 : scl = 0 : sdo = FAT_sdbyteout.1
                        scl = 1 : scl = 0 : sdo = FAT_sdbyteout.0 : scl = 1 : scl = 0
                    Next SDC_index
                endif
    @ endif
    Dave
    Always wear safety glasses while programming.

  9. #9
    Join Date
    Jan 2010
    Location
    In the Shed
    Posts
    6


    Did you find this post helpful? Yes | No

    Exclamation Jeremy's site down?

    Hi,

    It looks like the site Dave linked to has moved, but the redirection from there does not go to a currently active IP.

    Does anyone know where this file can be found?
    Or, Dave, can you get Jeremy's permission to upload his file here?

    Cheers
    Platypus

  10. #10
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    File location fixed in post #1
    Last edited by mackrackit; - 4th November 2010 at 11:54.
    Dave
    Always wear safety glasses while programming.

  11. #11
    Join Date
    Jan 2010
    Location
    In the Shed
    Posts
    6


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mackrackit View Post
    Thanks, that link works.

    BTW do you know if Jeremy has any plans to get sub-directories working?

  12. #12
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    I will ask him.
    Dave
    Always wear safety glasses while programming.

  13. #13
    Join Date
    Apr 2012
    Location
    Ptolemaida
    Posts
    50


    Did you find this post helpful? Yes | No

    Default SDHC32D need help i am stuck here =[

    Hello Guys,
    i am new with sd cards and i have read almost all posts about them and seriously only you are talking about FAT32 format and have the files.Can anyone guide me?? I am not asking to get free code or something im just stuck here . I have tried just to initialize the card but i failed. Iam using 18F26K22 hardware SSP1 module for communicating with the card and hardware EUART to debug the programm. I dont want to use the SD_WE and SD_CD and i simply didn't connect them.

    Here is what i tried so far but the programm is stucked at "Gosub FSinit" command.
    Code:
    '-------------------------------------------------------------------------------
    'DEFINES  DEFINES  DEFINES  DEFINES  DEFINES  DEFINES  DEFINES  DEFINES  DEFINES
    '-------------------------------------------------------------------------------
    include "modedefs.bas"
    include "fuses_18f26k22.bas"
    include "eusart_defines_ep7.bas"
    
    DEFINE OSC 32
    '-------------------------------------------------------------------------------
    'VARIABLES  VARIABLES  VARIABLES  VARIABLES  VARIABLES  VARIABLES  VARIABLES                  
    '-------------------------------------------------------------------------------
    i var byte
    
    '  COUNTERS  COUNTERS  COUNTERS  COUNTERS  COUNTERS  COUNTERS  COUNTERS COUNTERS
    '-------------------------------------------------------------------------------
    
    
    '  FLAGS  FLAGS  FLAGS  FLAGS  FLAGS  FLAGS  FLAGS  FLAGS  FLAGS  FLAGS  FLAGS
    '-------------------------------------------------------------------------------
    
    
    '  ARRAYS  ARRAYS  ARRAYS  ARRAYS  ARRAYS  ARRAYS  ARRAYS  ARRAYS  ARRAYS  ARRAY
    '-------------------------------------------------------------------------------
    
    
    '  PORTS  PORTS  PORTS  PORTS  PORTS  PORTS  PORTS  PORTS  PORTS  PORTS  PORTS  
    '-------------------------------------------------------------------------------
    
    
    '  REGISTERS  REGISTERS  REGISTERS  REGISTERS  REGISTERS  REGISTERS  REGISTERS  
    '-------------------------------------------------------------------------------
    TRISA= %00000000  
    TRISB= %00000011
    TRISC= %00010000
    
    ANSELA=0
    ANSELB=0
    ANSELC=0
    
    include "SDFSHC32original.bas"
    SDC_UseHardSPI = TRUE
    
    goto Init
    '-------------------------------------------------------------------------------
    'ISR   ISR   ISR   ISR   ISR   ISR   ISR   ISR   ISR   ISR   ISR   ISR   ISR       
    '-------------------------------------------------------------------------------
    
    '-------------------------------------------------------------------------------
    '   Initialization     Initialization      Initialization      Initialization                        
    '-------------------------------------------------------------------------------
    Init:
      PORTB= 0
      PORTA= 0
      PORTC= 0
      FAT_FileName[0] = "N" 
      FAT_FileName[1] = "A" 
      FAT_FileName[2] = "M"
      FAT_FileName[3] = "E" 
      FAT_FileName[4] = "S" 
      FAT_FileName[5] = " "
      FAT_FileName[6] = " " 
      FAT_FileName[7] = " "
      FAT_FileName[8] = "T" 
      FAT_FileName[9] = "X" 
      FAT_FileName[10]= "T"
      Hserout ["Init routine: OK!",13,10]
      for i=0 to 10
        Hserout [fat_filename[i]]
      next i
        Hserout [13,10]
      i= 0 
      PAUSE 100
    
    '-------------------------------------------------------------------------------
    ' Main Programm  Main Programm  Main Programm  Main Programm  Main Programm            
    '-------------------------------------------------------------------------------
    Main:
      Hserout ["LoopCount= ",dec i," FAT_error= ",dec fat_error," SDC_status= ",_
      dec SDC_status," SDC_response= ",dec SDC_response,13,10]
    
      Gosub FSInit
      pause 10
      Gosub FSInit
    
      i= i+1
    goto Main                        
    
    '-------------------------------------------------------------------------------
    'Subroutines  Subroutines  Subroutines  Subroutines  Subroutines  Subroutines   
    '-------------------------------------------------------------------------------
    
    
    end
    and my SDFSHC32D.bas file is attached.
    SDFSHC32Original.bas

    Im looking forward for an answer, Kostas.

  14. #14
    Join Date
    Nov 2006
    Posts
    70


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    I'm having the same problem described at the end of this (dead?) thread. Was there ever a solution? BTW, I had it working for FAT16 - so hardware is probably OK.

    Thx

  15. #15
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,980


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    FAT16 works. For 32 there is nothing good up to this moment. Also other compilers are good only at 16 as far as I know.

    Ioannis

  16. #16
    Join Date
    Nov 2006
    Posts
    70


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    Quote Originally Posted by Ioannis View Post
    FAT16 works. For 32 there is nothing good up to this moment. Also other compilers are good only at 16 as far as I know.
    But what about the posts above. Looks like some folks had a FAT32 library working. Am I missing something?

  17. #17
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,980


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    Officially there is no library for FAT32 by Melabs. All others are experimental and with no warranty that it will not corrupt your card.

    You said on post #14 you are having problems. Same #13. Same with me.

    Ioannis

  18. #18
    Join Date
    Jan 2016
    Posts
    1


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    Quote Originally Posted by mackrackit View Post
    IF
    You have used SDFS3 from MeLabs
    http://www.melabs.com/resources/samples/pbp/sdfs3.zip AND
    Now you would like to be able to use High Capacity cards THEN
    Try SDFSHC32d
    Attachment 4910
    ENDIF


    I have been using it with a 4 gig card. The only change I had to make in my code from MELABS SDFS was the INCLUDE statement and in SDFSHC32d change the pin aliases to match my hardware.

    Thank you Jeremy for making the modifications.
    hi, i try this code but i can not worked. this code loops "waitfortime" function.what can i do?

  19. #19
    Join Date
    Sep 2009
    Posts
    750


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    I have 2 SD cards 2GB and 16GB, both formated as FAT32. 2GB works fine, 16 fails initialization.
    It fails to select FAT32
    Code:
    	Select Case (DISK_buffer[450])
    		Case $01
    			DISK_type = FAT12 : FAT_error = CE_CARDFAT12
    		Case $04, $06, $0e
    			DISK_type = FAT16
    		Case $0b, $0c
    			DISK_type = FAT32
    		Case Else
    			FAT_error = CE_BAD_PARTITION
    	End Select
    Goes straight to FAT_error = CE_BAD_PARTITION
    Tried to select FAT32, but then fails later..
    As far as I understand up to 32GB should be supported.
    Any update on that?

  20. #20
    Join Date
    Aug 2011
    Posts
    439


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    What IS the partition type value in DISK_buffer[450]?

    Looking at that byte isn't really the way to determine FAT16/FAT32...

  21. #21
    Join Date
    Sep 2009
    Posts
    750


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    I think $72. I'll check later.
    But that is way they do it... I'm not familiar at all with anything regarding SD.
    I was hoping that I could just use module as is...
    First I edited my project from 2012, and it would create file on sd card, but wouldn't write nothing into file. That was case with both cards(2 and 16 gb)...
    Then I remove all files, and download new. First I get it to work with FAT16(2gb card), then replace INCLUDE "SDFS.pbp" with INCLUDE "sdfshc32d.pbp". Tried 2GB formated as fat 16 and fat32 and it worked. Then tried 16gb fat32 and then get it to fail at FSInit, error 6 (CE_INIT_ERROR Con 6 ' An initialization error has occured)
    What else I could check and report back, if you are interested in improving sdfshc32d.

  22. #22
    Join Date
    Sep 2009
    Posts
    750


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    Now fails here
    Code:
    	For SDC_timeout1.lowbyte = $ff To 0 Step -1
    		SDC_sector_addr = 0 : Gosub SectorRead	' Long SDC_sector_addr is the sector to read.  Returns Byte SDC_status.
    		If (SDC_status = sdcValid) Then SDC_timeout1 = 0	' Got a valid response, end the loop.
    	Next SDC_timeout1.lowbyte
    	If (SDC_status != sdcValid) Then SDC_status = sdcCardNotInitFailure : Goto InitError
    	Return			' Return Byte SDC_status.
    SDC_status=2
    SDC_response=$A9
    FAT_error=6
    Last edited by pedja089; - 27th December 2022 at 14:49.

  23. #23
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,961


    Did you find this post helpful? Yes | No

    Default Re: SDHC (FAT32 for SD cards)

    I don't have answers, only more questions:
    • Are you sure it's even a coding issue?
      Doesn't going over 2GB put you into another category of card speeds?
      Are PIC18Fs even fast enough to process these I/Os?

    For example:
    https://www.spypoint.com/en/blog/41/...-types-classes

    I'm sure the larger/faster/newer PICs can do it, but I know nothing about them except Microchip advertisements.

    Robert

Members who have read this thread : 3

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts