PDA

View Full Version : SDHC (FAT32 for SD cards)



mackrackit
- 30th August 2009, 02:53
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
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.

mackrackit
- 30th August 2009, 16:19
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

Normnet
- 30th August 2009, 16:42
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

mackrackit
- 30th August 2009, 19:45
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. :eek:
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.

Normnet
- 30th August 2009, 20:38
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

mackrackit
- 30th August 2009, 23:46
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.

Normnet
- 31st August 2009, 00:16
See PIC iPod wav player (http://www.picbasic.org/forum/showthread.php?t=10662) 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

mackrackit
- 31st August 2009, 02:11
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):


@ 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

Platypus
- 8th January 2010, 15:29
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

mackrackit
- 9th January 2010, 07:56
File location fixed in post #1

Platypus
- 9th January 2010, 12:11
Give this a try

http://216.221.103.16/pbpsdfs/sdfshc32.htm

Thanks, that link works.

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

mackrackit
- 9th January 2010, 12:47
I will ask him.

xapmanis
- 17th September 2013, 18:49
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 :frown: :frown:. 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.



'-------------------------------------------------------------------------------
'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.
7084

Im looking forward for an answer, Kostas.

brid0030
- 20th January 2015, 01:30
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

Ioannis
- 20th January 2015, 09:46
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

brid0030
- 21st January 2015, 01:55
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?

Ioannis
- 21st January 2015, 06:56
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

tatala
- 25th January 2016, 09:06
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
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?

pedja089
- 27th December 2022, 02:53
I have 2 SD cards 2GB and 16GB, both formated as FAT32. 2GB works fine, 16 fails initialization.
It fails to select FAT32

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?

tumbleweed
- 27th December 2022, 13:18
What IS the partition type value in DISK_buffer[450]?

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

pedja089
- 27th December 2022, 13:38
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.

pedja089
- 27th December 2022, 14:46
Now fails here

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

Demon
- 15th October 2023, 20:16
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/sd-card-types-classes

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

Robert