FAT32 library and SDHC cards

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
bakerj
Posts: 28
Joined: 21 Sep 2015 01:06

FAT32 library and SDHC cards

#1 Post by bakerj » 09 Nov 2015 14:48

I am using the Mikrobasic Pro for PIC32 comiler with the FAT32 library. I am trying to write to an SD card. I am finding that if I use an old SD card I can get the FAT32_init to work but if I use any modern SDHC card the initilization fails. The libarary states that it is compatible with both SD and SDHC so I am confused as to why it wont work. I have tried an 8GB class 4 SDHC card and an 8GB class 10 card. I have tried using different speeds for initializing the SPI bus but nothing seems to work. Has anyone else had issues with SDHC cards and the FAT32 library?

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: FAT32 library and SDHC cards

#2 Post by Aleksandar.Mitrovic » 09 Nov 2015 16:20

Dear Baker,

We tested our libraries and examples with 8GB SDHC cards and everything was working.

Can you tell we which hardware are you using? Also exactly the name of the card which you are using?
Did you format your cards before you use them?

Best regards,
Aleksandar

bakerj
Posts: 28
Joined: 21 Sep 2015 01:06

Re: FAT32 library and SDHC cards

#3 Post by bakerj » 09 Nov 2015 16:42

I am using the 1) Mikrobasic Pro for PIC32, 2)MikroProg USB programmer 3)PIC32MX130F064B chip at 40MHz. The SD cards are 1) Sandisk 256 MB Sd card (this is the one that works) 2) Sony SDHC class 10, 8GB card and 3)Sandisk class 4, 8GB card. All cards are formatted to FAT32 format. The relevant snippet of code that I am using for the MMC initialization is as follows:

'MMC/SD card variables
dim err as int8
dim fid as short
dim wrbuf as uint8[530]
bufferIndex as word
dim
filename as string[20] ' File name
character as byte
loop_, loop2 as byte
size as longint

' MMC module connections
dim MMC_chip_select as sbit at LATB10_bit ' for writing to output pin always use latch
dim MMC_chip_select_direction as sbit at TRISB10_bit
.
.
.
main:
TRISA=0
TRISB=0
trisb.4=0
TRISB.5=1 'MISO SD card
TRISB.12=0
TRISB.14=0
TRISB.15=0 'SDI is an input for the RF module (output for MCU)
TRISB.13=1 'SDO
' TRISB=0

AD1CON1.15=0 ' turn off ADC
ANSELB=0 'make portb pins digital
CM1CON.15=0 'turn off Comparator 1
CM2CON.15=0 'turn off Comparator 2
CM3CON.15=0 'turn off Comparator 3
JTAGEN_bit = 0 ' Disable JTAG
PMCON.15=0 'turn off parallel master port
RPB9R=0 'turn off pin remapping for pin RB9
RPB8R=0 'turn off pin remapping for pin RB8
RPB14R=0 'turn off pin remapping for pin RB14
SDI1R=%00000001 'map SDI to pin RB5
RPB11R=%0011 'map SDO to pin RB11
CTMUCON.15=0 'turn off Charge Time Measurement Unit
CVRCON.15=0 'turn off voltage reference

'=======================================================
'initialize SPI
SPI1_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 64, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE)
Delay_ms(10)

'--------initiialize MMC Card--------
err = FAT32_Init()
while(err < 0) ' ...retry each 100 millisecond
err = FAT32_Init()
Delay_ms(100)
led_Green=not led_green
wend
'reinitialize SPI to higher speed
SPI1_Init_Advanced(_SPI_MASTER, _SPI_8_BIT, 8, _SPI_SS_DISABLE, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_HIGH, _SPI_ACTIVE_2_IDLE)
Delay_ms(10)
' open file
filename="TestFAT32lib"
fid = FAT32_Open(@filename, FILE_WRITE_F32)
'-------------End MMC card---------

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: FAT32 library and SDHC cards

#4 Post by Aleksandar.Mitrovic » 10 Nov 2015 15:52

Hi,

I suggest you to use some partition program and set format your cards with this settings:
Create As: Primary
File System: FAT32
Cluster Size: 512 (this should be default)
Partition Align To: Cylinder

Also, did you try to customize our example for FAT32?

Best regards,
Aleksandar

bakerj
Posts: 28
Joined: 21 Sep 2015 01:06

Re: FAT32 library and SDHC cards

#5 Post by bakerj » 11 Nov 2015 17:01

OK, I will give these re-formatting options a try. I did not attempt to directly modify your example code. That's a good idea just to make sure I start from a good set of code.

Knowing that the newer SDHC cards are actually supported is a big help (I wasnt sure actually). Based on your replies it sounds like the problem is in my setup/code and not in the library.

I will reply back if I can get them to work. Thanks for the feedback.

bakerj
Posts: 28
Joined: 21 Sep 2015 01:06

Re: FAT32 library and SDHC cards

#6 Post by bakerj » 13 Nov 2015 22:15

Hi Alexander,

So I have reformatted both the S4 and S10 cards and removed a stray connection in my breadboard (I think this is why the class 4 device did not work). The class 4 SDHC card now works fine but the class 10 card does not. It continues to fail at the initialization step. Could you please confirm that the library has indeed been checked with class 10 cards? Agaiin this is just so that I can be sure the problem is with my setup and not with an incompatibility. Thanks

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: FAT32 library and SDHC cards

#7 Post by Aleksandar.Mitrovic » 16 Nov 2015 15:54

Hi,

I checked, we tested our library with Class 4 cards. And we use them on mikromedia boards.

Unfortunately we didn't use Class 10 cards nor did we test them.
But there should not be any problem.

If you have some problem please check the speed of your SPI since the minimum writing speed for this cards should be 10 Mb/s or better.

Best regards,
Aleksandar

bakerj
Posts: 28
Joined: 21 Sep 2015 01:06

Re: FAT32 library and SDHC cards

#8 Post by bakerj » 16 Nov 2015 16:04

OK, thanks for the reply.

calixarene
Posts: 17
Joined: 03 Apr 2012 06:31

Re: FAT32 library and SDHC cards

#9 Post by calixarene » 20 Nov 2015 13:18

Hi bakerj,

I have the same problem previously. In debug mode, the MMC can be initialized without problem for class 4 and class 10 but it failed in the FAT32 initialization. When I used the library from DANY (http://www.libstock.com/projects/view/58/fat), I found more cards could be recognized. I have tried ScanDisk class 4 and class 10 (from 8G, 16G and 32 G). The library worked. I don't know whether it will work for other cards from different brand. You can give it a try. I don't know the reason but I guess it is how the initialization routine in identifying the returned values from the card. The functions and the usage of this library are different from FAT32 and so you might need to rewrite the code.

In here, I would like to say thanks to Dany for creating many useful libraries that help me solve the problems.

bakerj
Posts: 28
Joined: 21 Sep 2015 01:06

Re: FAT32 library and SDHC cards

#10 Post by bakerj » 20 Nov 2015 17:34

Thanks for the information. I will give this library a try.

erpay
Posts: 32
Joined: 11 Nov 2012 00:03

Re: FAT32 library and SDHC cards

#11 Post by erpay » 20 Apr 2016 19:23

I have the same problem with class 10 sd kart, but it works well with small classes.

I think Mikroe should fix this problem.

User avatar
Aleksandar.Mitrovic
mikroElektronika team
Posts: 1697
Joined: 11 Mar 2015 12:48

Re: FAT32 library and SDHC cards

#12 Post by Aleksandar.Mitrovic » 21 Apr 2016 09:51

Dear erpay,

Can you please tell me have you tried some other SD cards Class 10 from a different manufacturer?

We have tested our library with some SD cards Class 10 and its working.

Best regards,
Aleksandar

Post Reply

Return to “mikroBasic PRO for PIC32 General”