PDA

View Full Version : FAT32 SD cards use with sdfshc32d.pbp



Alexey
- 17th August 2012, 05:38
I used the sdfshc32d.pbp extension and it works great, but if the card socket fails so the card looses connection, the program freezez or, better say starts cycling around a piece of code and does not time out so everything stops working (no hardware SPI used and micro runs at 4 MHz)

I tried to add a loop counter into the code and it seems to be working - if card is missing or it's connector failed it now normally returns and keeps going ignoring the card failure otherwise the timeout did not work - it seems to me it's original timeout counter does not work

...
loopcount var byte
loopcount= 0
waitforcmdtimeout: 'THIS IS ORIGINAL SUBROUTINE LINE IN THE CODE
if loopcount = 255 then
SD_CS = 1
return
endif
loopcount = loopcount +1
...


Maybe someone can advise if and why my approach is not good...

Thank you

Alexey

mackrackit
- 17th August 2012, 06:08
Are you using the Card Detect pin on the SD socket?

Alexey
- 17th August 2012, 07:00
Hello Dave,

No I do not. Actually my schematic allows me to use it but I prefer to let the process fail and return with a fault code then ignore it because I noticed socket i use may disconnect because of a hard vibration and I do not want this to cause failure of the whole system (it did not with FAT16 library but it does with FAT32). Also I want the system keep working (with lost ability for data logging) if the card dies. I was thinking to add use of the card detect switch to save some time on timeouts in the faulty situations which I can predict and design the system that may tolerate some unpredictable errors (like died card or faulty connector). My understanding is that the sdfshc32d.pbp does not use the card detect option itself although it does configure the input pin

Thank you,
Alexey

mackrackit
- 17th August 2012, 23:29
What about something simple like
If (FAT_error != 0) Then WhatEvever

Alexey
- 18th August 2012, 10:15
Hello Dave,

I learnt that the subroutine waitforcmdtimeout: cycles indefinitely when card is not there and you try to make a record, so sdfshc32d.pbp does not return control into the main code and there is no way to check FAT_error status. I may check SD_CD before writing but I suspect that the same may happen if card dies and does not respond (or connector fails which already happened to me once and this is why I started panicing) and it seems that limiting sycles to 255 helps (I checked and normally my loopcount = 2 after return, so actually only one sycle is needed and 255 is more than enough but still does not make a critical delay).

By the way, I have another strange thing here, do not know why - I have to initialize the card twice for it to work (tried many different cards, and all work only after init two times. This does not bother me too much, because it still works fine. I had the same with FAT16 library. Not sure, maybe all these things I have because my schematic does not have pull up resistors on card - perhaps need to try to add them to see if this changes something. However all works fine so far

Alexey

mackrackit
- 18th August 2012, 17:07
The pull-ups are a must.

As far as the 32 bit card routine not working is something I have not experienced. I use the same base code and only change the SD card include if it is a project that needs a 32 bit card.

Have you seen this?
http://www.picbasic.co.uk/forum/content.php?r=272-USB-SD-LOGGING

Alexey
- 19th August 2012, 05:57
Hello Dave,

Thank you,

Yes, I did see your data logger and like it very much. USB will be the next thing what I need to learn. I did not put pull up resistors on unused pins into mine, also i use micro SD and my MCU runs at 4 MHz maybe these things make me the need of initializing the card twice. After initing twice it runs fine.

Your code does not attempt to write to card when it is not inserted. Please try to insert a dummy card to see if the "include" freezes on timeout cycle or not when you write onto the card

Best regards,

Alexey

mackrackit
- 19th August 2012, 23:11
I just messed around with this and you are correct. There is a problem. I guess 2gb or less will have to do.

Alexey
- 28th August 2012, 17:02
Keep experimenting with sdfshc32d.pbp I found a couple of card types which do not work with this library although they did work with FAT16. Fat 32 worked with 512 Mb, 1G, 2G, 4G, 8G and 32G cardsI tried but did not work with 2G cards made by AXIOM and ATP
I learnt that FSinit: calls DiskMount: which calls MediaInitialize: which in
If (SDC_response != $00) Then SDC_status = sdcCardInitTimeout : Goto InitError
' The command succeeded.
gets SDC_response = $00 which is good

Further Gosub SendSDCCmd ' Send Byte SDC_cmd to Long SDC_address. Returns Word SDC_response.
If (SDC_response = SDC_BAD_RESPONSE) Then SDC_status = sdcCardInitCommFailure : Goto InitError
If (SDC_response.6 = 0) Then SDC_HC = 0
Responce is $80 which is goood

Then it tries to read sector and I believe somewhere there in one of the last steps initialization fails.

Is this a known issue? These cards read OK with the FAT16 library and do not work only with FAT32 (Software SPI is used)

Thank you,

Alexey

mikebar
- 14th September 2014, 02:05
Hi Alexey,
did you managed to solve the freezing problem with the sdfshc32d.pbp while using Software SPI?
I'm experiencing the same issue.
Could you please tell to the forum if you have a working fail safe version of the sdfshc32d.pbp ?

Thank you in advance.
Regards,
Mike.

Alexey
- 14th September 2014, 06:48
Hi Mike,

Please find the attached file. It is not perfect but the problem seems to be solved. At least if the card or socket fails or user removes the card when it is in use, the code does not freeze. Please let me know if it works for you

mikebar
- 14th September 2014, 22:50
Thank you a lot Alexey.
I'll give it a try.
If the only one modifications was the loop counter, I've already modified the original the sdfshc32d.pbp version in that way... and it works.
In order to give more "retry", what do you think to use a Word (or Long) sized variable for the "loopcount" instead of the Byte sized one?
In this way it could count till 65535 (or even 4294967296)...
Ok, maybe 4 billions is way too much, but just to explain the concept.

Thank you again.
Regards,
Mike.

Alexey
- 15th September 2014, 04:02
Hello Mike,

It was two years ago and I might forget but I believe all change is what described in the very first message.

I learnt that if everything works, it works at the first attempt and if not, then it uses all 255 so even 255 is too much, no need for more attempts.

By the way, I am going to surprize you (at least this was a big surprize to me after years of artificial tricks of doing signed math) - Long variables are signed and they can hold values -2147483648 to 2147483647 :)

Best regards,
Alexey

mikebar
- 16th September 2014, 00:35
Hi Alexey,
thank you again for your time to get that problem solved.
Yes, I've tried it and seems that the things you suggested in the very first message, it is actually the only one modification done to the whole sdfshc32d.pbp

BTW, thank you to clarify about the signed "Long" variables. It was a mistake by me to point out a number like 4294967296...

Regards,
Mike.

xapmanis
- 7th March 2015, 22:19
Hello, i have used this code to test writing or reading to my SD card, Kingstone 4GB.

'************************************************* ******************************
'* Name : microSD_Test1
'* Author : Kostas
'* Notice :
'* :
'* Date :
'* Version : 1.0
'* Notes :
'* :
'************************************************* ******************************
include "modedefs.bas"
include "fuses_18f26k22.bas"
DEFINE OSC 64

DEFINE HSER_RCSTA 90h 'Hser receive status init
DEFINE HSER_TXSTA 20h 'Hser transmit status init
DEFINE HSER_BAUD 9600 'Hser baud rate
DEFINE HSER_CLROERR 1 'Hser clear overflow automatically

'Variables
i var byte
k var byte

'Registers
INTCON= 0 'disable all interrupts
INTCON2= %10000000 'Disable all pull-up resistors
TRISA= %00000000
TRISB= %00000000
TRISC= %00010000

ADCON0= 0
ANSELA=0
ANSELB=0
ANSELC=0

include "SDFSHC32D2K.bas"
SDC_UseHardSPI = TRUE

goto Init
'ISR routine


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]
i= 0
PAUSE 100


' ---------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 100
Gosub FSInit
i= i+1
hserout ["LoopCount= ",dec i," FAT_error= ",dec fat_error," SDC_status= ",_
dec SDC_status," SDC_response= ",dec SDC_response,13,10]
pause 100
Gosub FSInit
i= i+1
hserout ["LoopCount= ",dec i," FAT_error= ",dec fat_error," SDC_status= ",_
dec SDC_status," SDC_response= ",dec SDC_response,13,10]

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"

FAT_mode = "r" ' Read mode
Gosub FSfopen
If (FAT_error != 0) Then
i= i+1
hserout ["LoopCount= ",dec i," FAT_error= ",dec fat_error," SDC_status= ",_
dec SDC_status," SDC_response= ",dec SDC_response,13,10]
endif
FAT_count = 1 ' Read 1 byte to buffer at a time
Gosub FSfread
i= i+1
hserout ["LoopCount= ",dec i," FAT_error= ",dec fat_error," SDC_status= ",_
dec SDC_status," SDC_response= ",dec SDC_response,13,10]
k=0
While (FAT_error = 0)
Hserout [FAT_dest[k]]
k=k+1
FAT_count = 1 ' Read 1 byte to buffer at a time
Gosub FSfread
Wend

i= i+1

goto Main


end

And i receive this message in the serial communicator programm:

Init routine: OK!
LoopCount= 0 FAT_error= 6 SDC_status= 2 SDC_response= 13
LoopCount= 1 FAT_error= 6 SDC_status= 2 SDC_response= 13
LoopCount= 2 FAT_error= 6 SDC_status= 2 SDC_response= 13
LoopCount= 3 FAT_error= 7 SDC_status= 2 SDC_response= 13
LoopCount= 4 FAT_error= 61 SDC_status= 2 SDC_response= 13
LoopCount= 5 FAT_error= 61 SDC_status= 2 SDC_response= 13
LoopCount= 6 FAT_error= 6 SDC_status= 2 SDC_response= 13
LoopCount= 7 FAT_error= 6 SDC_status= 2 SDC_response= 13
LoopCount= 8 FAT_error= 7 SDC_status= 2 SDC_response= 13
LoopCount= 9 FAT_error= 61 SDC_status= 2 SDC_response= 13
LoopCount= 10 FAT_error= 61 SDC_status= 2 SDC_response= 13
.....

And goes on like this.

I would like to ask if you know where i can find the FAT_error, the SDC_status and SDC_response.
I want to implement SD card to many of my projects so any help will be apreciated.

xapmanis
- 7th March 2015, 23:18
I would like to ask if you know where i can find the FAT_error, the SDC_status and SDC_response.

I should have done my studies before i post... I found the error table after a little search.
But the thing is that i got my card corrupted :-/
Anyway, i will try fixing this tomorow.

mikebar
- 8th March 2015, 10:48
Hi Xapmanis,
I'm happy you'va found the error table list.
But, how did you get corrupted your SD card?
Using it with the library or in some other way?
It happened to me once that after using it in my project, a card get corrupted. The only way to fix it was to format using Windows.
Also, I've seen that you're using the include SDFSHC32D2K.bas, while the normal include for that library is SDFSHC32D2.bas.
What the "K" version means?

Regards,
Mike.

xapmanis
- 8th March 2015, 13:21
Hello there !!
I kept the original file SDFSHC32D2.bas intact because i wanted to experiment arround. K stands for Kostas, my name :P. I just adjusted the PORT values to my PIC18F26K22 and enabled HRWSPI1 at PORTC. I did run the code above, i did insert the card and powered the device at 3.3 volt. The result was after some thrash messages from serial communicator ( probably due to bad timing or wiring ) i tried to check my card into the laptop but unfortunatly my laptop can't even format the card or detect it, i dont know why. I will buy couple of cards from ebay to experiment.
Also i would like to ask. Have you managed to make it work with FAT32 ?! Is it a problem that my PIC runs at 64MHz ?

I will keep studing the SDFSHC32D2.bas file and the examples because last time i dealed with it was like a year ago before getting in the army.
Best regards, Kostas.

mikebar
- 8th March 2015, 14:53
Hi Xapmanis,
I've never tried to use it with the FAT32...
Probably the fact that the chip run at 64MHz SHOULD NOT hurt the timing... well... you're using the hardware SPI....
Did you tried to setup the library to use the software SPI (and set accordingly the ports to do that) instead of the hardware one?
Have you checked in your schematic if you're pulling-up all the SD Card pins?
Have you double checked if in NO CASE your power supply and or signal level, never exceed the 3.6V as ABSOLUTE MAXIMUM ratings for the SD?

Regards,
Mike.

xapmanis
- 8th March 2015, 18:42
I thought u did succeed making this work with FAT32. Didn't you ?? Also i have mikroelektrinika's EasyPIC7 for main testing boards and a module for SD/MicroSD cards from ebay that has aready pullups and voltage lvl shifter on it if i want to use the card at 5v. But for safety i used 3.3v powered from my laptop usb port.
Also i will give it a try to use software SPI, i dont care about speeds anyway ( for now...).
Thank you for the feedback mate!
I'll let you know if anything changes, i ordered 3 more cards to play with :P !!!

Ioannis
- 8th March 2015, 21:16
USB port supplies 5Volts, not 3.3. Do you have a low drop regulator then?

Ioannis

xapmanis
- 8th March 2015, 23:11
Hello ioannis !!!
yes, as i said earlier i use easypic7 board and i can select 5v or 3.3v with the help of a jumper. I use 3.3 v to power my pic also to power the sd card module

xapmanis
- 10th March 2015, 16:17
FACT 1. If you don't damage anything you won't learn. Even if you still know if you don't do all necesary connections check with clear mind, you will, and that's for sure, damage something !!
I had reversed polarity :-/ 3.3v to ground and Gnd to Vcc. That's why my SD card is gone...

FACT 2. I had also SDO1 connected with the DO pin of the module and the SDI1 with DI so there was no communication -_^ !!

FACT 3. Waiting for the 2 new SD cards 4GB size to check the example codes !!! I will let you know about my progress on the topick.