View previous topic :: View next topic |
Author |
Message |
njepsen
Joined: 13 Aug 2007 Posts: 469
|
Posted: Mon Jul 22, 2019 6:46 am Post subject: SD read/write error |
|
|
I am saving backup data to an SD card, but from time to time - like once a month- I get a situation where I cannot read the card.
Here is an excerpt of the code. Full code is 8000 lines.
Err 241 is "cpErrDriveRead1" and I have no idea what that means, or more importantly, why it is happening or how to recover from it.
I don't have the hardware to cycle the SD card 3.3 power supply, which has its own regulator and 2 bypass caps a 0.1 and a 1000uF, close to the sd socket. I'm using Toshiba micro SD.
Code: |
'*******************************************************************************
$regfile = "m1284pdef.dat" 'my processor
$crystal = 9830400 'system crystal
$framesize = 800
$hwstack = 550
$swstack = 550
$frameprotect = 1
stop watchdog
do
.code
.
.
$include "config_mmcsd_hc.bas"
.
.code
Print #1 , ">> internet or server didnt respond.Saving data to SD"
Sd_fault = Check_sd_card()
If Sd_fault = 0 Then
Print #1 , ">> creating SD folders"
Call Savecopy_of_data_to_sdcard()
Else
Print #1 , ">> Data lost,can't access SD card"
End If
.
.
. lots more code
.
.
loop
'***********************************************************
Function Check_sd_card() As Byte
'added cache dir at end because if its not there, dir*.* will
'give "." as a file name
'Sequence is correct, but should call driveinit() and initfilesystem(1)
'whenever a card is hot swapped.
Local Retries As Byte
Local Sd_fault As Byte
Retries = 0
Sd_fault = 0
$include "config_mmcsd_hc.bas" 'set up the high capacity SD card comms
Do
Incr Retries
Call Timedelay_ms(2000) 'Wait 2 for SD to power up
#if Pi_motherboard = 0
If Sd_inserted = 1 Then
Print #1 , ">> no SD"
Sd_fault = 1
exit sub
End If
#endif
Print #1 , ">> SD Card Type = " ;
Select Case Mmcsd_cardtype
Case 0 :
Print #1 , ">> can't init the Card - check smd settings"
Sd_fault = 1
Case 1 : Print #1 , ">> MMC"
Sd_fault = 0
Case 2 : Print #1 , ">> SDSC Spec. 1.x "
Sd_fault = 0
Case 4 : Print #1 , ">> SDSC Spec. 2.0 or later"
Sd_fault = 0
Case 12 : Print #1 , ">> SDHC Spec. 2.0 or later"
Sd_fault = 0
case else
Print #1 , ">> unknown/card missing"
End Select
Call Timedelay_ms(2000) ' Wait 2
Tempb = Driveinit() 'call drivinit()
If Gbdriveerror = 0 Then
Print #1 , ">> SD drive OK. Reading size...."
$include "config_AVR-DOS.BAS" 'help says include is only done once even if called multiple times
Sd_fault = 0
Else
Print #1 , ">> Error during Drive Init: " ; Gbdriveerror
Sd_fault = 1
End If
Tempb = Initfilesystem(1)
If Tempb <> 0 Then
Print #1 , ">> Error: " ; Tempb ; " at Init file system"
Sd_fault = 1
End If
' Lfree = Diskfree() 'CAUTION This takes a long time. Start watchdog afterwards
' Start Watchdog
' print #1 , ">. free memory =" ; Lfree
If Sd_fault <> 1 Then
Print #1 , ">> SD card OK"
Print #1 , ">> Filesystem: " ; Gbfilesystem
Print #1 , ">> FAT Start Sector: " ; Glfatfirstsector
Print #1 , ">> Root Start Sector: " ; Glrootfirstsector
Print #1 , ">> Data First Sector: " ; Gldatafirstsector
Print #1 , ">> Max. Cluster Number: " ; Glmaxclusternumber
Print #1 , ">> Sectors per Cluster: " ; Gbsectorspercluster
Print #1 , ">> Root Entries: " ; Gwrootentries
Print #1 , ">> Sectors per FAT: " ; Glsectorsperfat
Print #1 , ">> Number of FATs: " ; Gbnumberoffats
Print #1 , ">> Inititialise retries:" ; Mmcsd_try
Print #1 , ""
Print #1 , ">> AVR-DOS_Config Settings"
Print #1 , ">> " ; Cfilehandles ; " File-Handles (cFileHandles = " ; Cfilehandles ; ")"
Print #1 , ">> Directory and Fat handled in " ;
#if Csepfathandle = 1
Print #1 , ">> different buffers";
#else
Print #1 , ">> same buffer";
#endif
Print #1 , ">> (cSepFATHandle = " ; Csepfathandle ; ")"
Print #1 , ">> Directory and Fat Information of a File saved "
#if Cfatdirsaveatend = 1
Print #1 , ">> only at Flush and Close"
#else
Print #1 , ">> at every Data Sector" ;
#endif
Print #1 , ">> (cFATDirSaveAtEnd = " ; Cfatdirsaveatend ; ")"
Lsize = Disksize()
Print #1 , ">> Card size " ; Lsize ; " kB"
Print #1 , ""
End If
Loop Until Retries > 5 Or Sd_fault = 0
if sd_fault = 0 then
Chdir "\"
Mkdir Cachefoldernamestr
end if
Check_sd_card = sd_fault '0 = no fault. 1 = fault
End Function
'***********************************************************
|
When the code fails, my debug say this:
Quote: |
>> internet or server didnt respond.
>> Saving data to SD
>> SD Card Type = >> SDSC Spec. 2.0 or later
>> SD drive OK. Reading size....
>> Error: 241 at Init file system
>> SD Card Type = >> SDSC Spec. 2.0 or later
>> SD drive OK. Reading size....
>> Error: 241 at Init file system
>> SD Card Type = >> SDSC Spec. 2.0 or later
>> SD drive OK. Reading size....
>> Error: 241 at Init file system
>> SD Card Type = >> SDSC Spec. 2.0 or later
>> SD drive OK. Reading size....
>> Error: 241 at Init file system
>> SD Card Type = >> SDSC Spec.2.0 or later
>> SD drive OK. Reading size....
>> Error: 241 at Init file system
>> SD Card Type = >> SDSC Spec. 2.0 or later
>> SD drive OK. Reading size....
>> Error: 241 at Init file system
>> Data lost,can't access SD card <<<<<<<<<<<<<<<<<<<<<<<<<<< THIS IS THE PROBLEM
>> battery =13.15
|
(BASCOM-AVR version : 2.0.7.9 , Latest : 2.0.8.1 ) _________________ Neil |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Tue Jul 23, 2019 11:26 pm Post subject: |
|
|
Reaching back into dark history...
I too had rare problems getting SD cards to respond. Maybe not the same failure, but effectively preventing access. My solution at the time was to use a card supply regulator which could be enabled and disabled by a processor port pin.
Never had any problems after that, but I have no idea whether the recovery cycle was ever tripped in the field. What one could have done was to log a failure to the card, after getting access back, but I never thought of that, and in any case I only very rarely had access to the cards once installed. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
njepsen
Joined: 13 Aug 2007 Posts: 469
|
Posted: Tue Jul 23, 2019 11:35 pm Post subject: |
|
|
Thanks Adrian,
Unfortunately in the latest population of boards, i cannot cycle the SD card power. On older boards I had a FET that allowed me to do this but (foolishly) decided it was not necessary. Because this issue is so intermittent, its hard to fault find, and I was hoping someone would spot an issue with my code. SD cards for me are a bit of a black art. _________________ Neil |
|
Back to top |
|
|
AdrianJ
Joined: 16 Jan 2006 Posts: 2483 Location: Queensland
|
Posted: Wed Jul 24, 2019 12:40 am Post subject: |
|
|
Hi Neil,
My memory of all the steps needed to get an SD card going are long gone. And in any case I used them in simple SPI mode, so all those advanced access settings are different.
The repeated error 241 at initfilesystem is a bit odd, you should not get that, but I dont know if its significant.
Somehow I doubt really rare errors like you get are due to code, except possibly a marginal timing issue.
I would be more suspicious of power supply glitches. You do know that the cards themselves draw high pulse currents during the write cycles, up in the several 100 mA region ? That can cause problems with supplies. Even 1000 uF bypass may not be enough, when you work out the voltage drop over the current pulse time. You need very "stiff" regulators. FETS to switch the output can cause the same problems, its safer to switch the input side, if you go back to that idea. _________________ Adrian Jansen
Computer language is a framework for creativity |
|
Back to top |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum
|
|