PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : SD-Karte: Init + FAT-Infos geht, Sektoren auslesen nicht.



Jaecko
27.07.2008, 16:04
Hi.

Hab nun endlich mal eine elektrische Lösung gefunden, um eine SD-Karte an nem AVR zu betreiben (wurde doch die Adapterplatine von Display3000)

Von der Verbindung her geh ich davon aus, dass alles passt, da bei dem unten angefügten Testprogramm die Initialisierung sowie das Auslesen der FAT-Daten klappt.

Problem: Sobald Sektoren "manuell" gelesen werden sollen, wird der erste Sektor mit Fehler 229 ("No Data response Byte from MMC at Read"), die weiteren mit Fehler 227 ("Error response Byte at Read Command") "bestätigt"; Inhalt des Sektorbuffers ist dann durchgehend 0.

Hier mal die Ausgaben am Terminal, nachdem eine Karte erkannt wurde:


Trying to read File system...
Wait...
OK
Filesystem: 11
FAT Start Sector: 127
Root Start Sector: 2071
Data First Sector: 2071
Max. Cluster Nummber: 123895
Sectors per Cluster: 4
Root Entries: 0
Sectors per FAT: 972
Number of FATs: 2
Disksize : 247788
Disk free: 74334


Da die Informationen soweit korrekt sind, geh ich davon aus, dass die Kommunikation zwischen AVR und Karte funktioniert.

Hier die verwendeten Sources:

sdtest.bas (Ich weiss, nicht sonderlich "ordentlich", aber ist ja nur mal ein Test)


'µC: ATMega2560; HW/SOFT/FRAMESIZE: 82/68/68

$include "config_mmc.bas"
$include "config_avr-dos.bas"
Config PORTD.5 = Output
LED Alias PortD.5

Toggle LED
Waitms 500
Toggle LED
waitms 500
Toggle LED

Dim tmpbyte as Byte
DIM BTemp1 as Byte
DIM SDBuffer(520) as Byte
dim tmpword as Word
dim Sector as Word
dim bufptr as long

Config PortC.0 = Output
SDOn Alias PORTC.0

CONFIG PORTC.2 = Input ' Card detect switch
DETECT ALIAS PINC.2
PORTC.2 = 1

CONFIG PORTC.3 = Input 'Write protection switch
WPROT ALIAS PINC.3
PORTC.3 = 1

CONFIG COM4 = 9600 , Synchrone = 0 , Parity = None , Stopbits = 1 , Databits = 8 , Clockpol = 0
OPEN "COM4:" for Binary As #4
SDOn = 1
wait 1
print #4 , "Running..."
print #4 , chr(12)

Do

If DETECT = 0 Then
tmpbyte = Driveinit() ' Init MMC/SD Card
print #4 , "Init: " ; str(tmpbyte)
print #4 , "Trying to read File system..."
Btemp1 = Initfilesystem(1) ' Partition 1
print #4 , "Wait..."
' use 0 for drive without Master boot record
If Btemp1 <> 0 Then
Print #4 , "Error: " ; Btemp1 ; " at Init file system"
Else
Print #4 , " OK"
Print #4 , "Filesystem: " ; Gbfilesystem
Print #4 , "FAT Start Sector: " ; Glfatfirstsector
Print #4 , "Root Start Sector: " ; Glrootfirstsector
Print #4 , "Data First Sector: " ; Gldatafirstsector
Print #4 , "Max. Cluster Nummber: " ; Glmaxclusternumber
Print #4 , "Sectors per Cluster: " ; Gbsectorspercluster
Print #4 , "Root Entries: " ; Gwrootentries
Print #4 , "Sectors per FAT: " ; Glsectorsperfat
Print #4 , "Number of FATs: " ; Gbnumberoffats
Print #4 , "Disksize : " ; Disksize() ' show disk size in bytes
Print #4 , "Disk free: " ; Diskfree() ' show free space too
bufptr = VarPtr(SDBuffer(1))
For Sector = 0 to 8000
SDOn= 1
tmpbyte = DriveReadSector(bufptr , Sector)
Print #4 , "Sector Read Result: " ; str(tmpbyte)
For tmpword = 1 to 512
if SDBuffer(tmpword) > 0 Then Print #4 , "Data: " ; SDBuffer(tmpword) ; " in Sector " ; Sector
next
if DETECT = 1 Then Goto NoCard
wait 1
next

print #4 , "Done"

' Get first Sector
End If

Do
if DETECT = 1 Then Goto NoCard
loop
EndIf

NoCard:

If WPROT = 1 Then
Print #4 , "Card protected"
Else
Print #4 , "Card open"
endif

do
if DETECT = 0 Then Goto Card:
loop
card:

Loop
End


config_mmc.bas


' THIS IS AN INCLUDE FILE
' DO NOT COMPILE
$nocompile
'-------------------------------------------------------------------------------
' Config_MMC.BAS
' Config File for MMC Flash Cards Driver
' (c) 2003-2005 , MCS Electronics / Vögel Franz Josef
'-------------------------------------------------------------------------------
' Place MMC.LIB in the LIB-Path of BASCOM-AVR installation
'
'Connection as following
'MMC M128/M103
'1 MMC_CS PORTB.0
'2 MOSI PORTB.2
'3 GND
'4 +3.3V
'5 CLOCK PORTB.1
'6 GND
'7 MISO, PORTB.3

' you can vary MMC_CS on HW-SPI and all pins on SOFT-SPI, check settings
' ========== Start of user definable range =====================================

' you can use HW-SPI of the AVR (recommended) or a driver build in Soft-SPI, if
' the HW-SPI of the AVR is occupied by an other SPI-Device with different settings

' Declare here you SPI-Mode
' using HW-SPI: cMMC_Soft = 0
' not using HW_SPI: cMMC_Soft = 1

Const Cmmc_soft = 0

#if Cmmc_soft = 0

' --------- Start of Section for HW-SPI ----------------------------------------

' define Chip-Select Pin
Config PinC.1 = Output ' define here Pin for CS of MMC/SD Card
Mmc_cs Alias PortC.1
Set Mmc_cs

' Define here SS Pin of HW-SPI of the CPU (f.e. Pinb.0 on M128)
Config Pinb.0 = Output ' define here Pin of SPI SS
Spi_ss Alias Portb.0
Set Spi_ss ' Set SPI-SS to Output and High por Proper work of
' SPI as Master

' HW-SPI is configured to highest Speed
Config Spi = Hard , Interrupt = Off , Data Order = Msb , Master = Yes , Polarity = High , Phase = 1 , Clockrate = 128 , Noss = 1
Spsr = 1 ' Double speed on ATMega128
Spiinit ' Init SPI

' --------- End of Section for HW-SPI ------------------------------------------

#else ' Config here SPI pins, if not using HW SPI

' --------- Start of Section for Soft-SPI --------------------------------------

' Chip Select Pin => Pin 1 of MMC/SD
Config PinC.1 = Output
Mmc_cs Alias PortC.1
Set Mmc_cs

' MOSI - Pin => Pin 2 of MMC/SD
Config PinB.2 = Output
Set PinB.2
Mmc_portmosi Alias PortB
Bmmc_mosi Alias 2

' MISO - Pin => Pin 7 of MMC/SD
Config PinB.3 = Input
Mmc_portmiso Alias PinB
Bmmc_miso Alias 3

' SCK - Pin => Pin 5 of MMC/SD
Config PinB.1 = Output
Set PinB.1
Mmc_portsck Alias PortB
Bmmc_sck Alias 1

' --------- End of Section for Soft-SPI ----------------------------------------

#endif

' ========== End of user definable range =======================================


' Error
Const Cperrdrivereset = 225 ' Error response Byte at Reset command
Const Cperrdriveinit = 226 ' Error response Byte at Init Command
Const Cperrdrivereadcommand = 227 ' Error response Byte at Read Command
Const Cperrdrivewritecommand = 228 ' Error response Byte at Write Command
Const Cperrdrivereadresponse = 229 ' No Data response Byte from MMC at Read
Const Cperrdrivewriteresponse = 230 ' No Data response Byte from MMC at Write
Const Cperrdrive = 231
Const Cperrdrivenotsupported = 232 ' return code for DriveGetIdentity, not supported yet

Wait 1 ' Wait some time before initialising MMC/SD
Dim Gbdriveerror As Byte ' General Driver Error register
Dim Gbdriveerrorreg As Byte ' Driver load Error-Register of HD in case of error
Dim Gbdrivestatusreg As Byte ' Driver load Status-Register of HD on case of error
Dim Gbdrivedebug As Byte
$lib "MMC.LIB" ' link driver library
$external _mmc
'Gbdriveerror = Driveinit() ' Init MMC/SD Card


config_avr-dos.bas


$nocompile
' Config File-System for Version 5.5:

' === User Settings ================================================== ==========

' Count of file-handles, each file-handle needs 524 Bytes of SRAM
Const Cfilehandles = 2 ' [default = 2]

' Handling of FAT-Buffer in SRAM:
' 0 = FAT- and DIR-Buffer is handled in one SRAM buffer with 561 bytes
' 1 = FAT- and DIR-Buffer is handled in separate SRAM buffers with 1078 bytes
' Parameter 1 increased speed of file-handling
Const Csepfathandle = 1 ' [default = 1]

' Handling of pending FAT and Directory information of open files
' 0 = FAT and Directory Information is updated every time a data sector of the file is updated
' 1 = FAT and Directory Information is only updated at FLUSH and SAVE command
' Parameter 1 increases writing speed of data significantly
Const Cfatdirsaveatend = 1 ' [default = 1]


' Surrounding String with Quotation Marks at the Command WRITE
' 0 = No Surrounding of strings with quotation.marks
' 1 = Surrounding of strings with quotation.marks (f.E. "Text")
Const Ctextquotationmarks = 1 ' [default = 1]


' Write second FAT. Windows accepts a not updated second FAT
' PC-Command: chkdsk /f corrects the second FAT, it overwrites the
' second FAT with the first FAT
' set this parameter to 0 for high speed continuing saving data
' 0 = Second FAT is not updated
' 1 = Second FAT is updated if exist
Const Cfatsecondupdate = 1 ' [default = 1]


' Character to separate ASCII Values in WRITE - statement (and INPUT)
' Normally a comma (,) is used. but it can be changed to other values, f.E.
' to TAB (ASCII-Code 9) if EXCEL Files with Tab separated values should be
' written or read. This parameter works for WRITE and INPUT
' Parameter value is the ASSCII-Code of the separator
' 44 = comma [default]
' 9 = TAB ' [default = 44]
Const Cvariableseparator = 44




' === End of User Setting ================================================== ====



' === Variables for AVR-DOS ================================================== ==

' FileSystem Basis Informationen
Dim Gldrivesectors As Long
Dim Gbdoserror As Byte

' Master Boot Record
Dim Gbfilesystem As Byte
' Partition Boot Record
Dim Gbfilesystemstatus As Byte
Dim Glfatfirstsector As Long
Dim Gbnumberoffats As Byte
Dim Glsectorsperfat As Long
Dim Glrootfirstsector As Long
Dim Gwrootentries As Word
Dim Gldatafirstsector As Long
Dim Gbsectorspercluster As Byte
Dim Glmaxclusternumber As Long
Dim Gllastsearchedcluster As Long

' Additional info
Dim Glfs_temp1 As Long

' Block für Directory Handling

Dim Gldirfirstsectornumber As Long

Dim Gwfreedirentry As Word
Dim Glfreedirsectornumber As Long

Dim Gsdir0tempfilename As String * 11
Dim Gwdir0entry As Word ' Keep together with next, otherwise change _DIR
Dim Gldir0sectornumber As Long

Dim Gstempfilename As String * 11
Dim Gwdirentry As Word
Dim Gldirsectornumber As Long
Dim Gbdirbufferstatus As Byte
Dim Gbdirbuffer(512) As Byte
Const C_filesystemsramsize1 = 594
#if Csepfathandle = 1
Dim Glfatsectornumber As Long
Dim Gbfatbufferstatus As Byte
Dim Gbfatbuffer(512) As Byte
Const C_filesystemsramsize2 = 517
#else
Const C_filesystemsramsize2 = 0
#endif

' File Handle Block
Const Co_filenumber = 0
Const Co_filemode = 1
Const Co_filedirentry = 2 : Const Co_filedirentry_2 = 3
Const Co_filedirsectornumber = 4
Const Co_filefirstcluster = 8
Const Co_filesize = 12
Const Co_fileposition = 16
Const Co_filesectornumber = 20
Const Co_filebufferstatus = 24
Const Co_filebuffer = 25
Const C_filehandlesize = Co_filebuffer + 513 ' incl. one Additional Byte for 00 as string terminator
' for direct text reading from File-buffer
Const C_filehandlesize_m = 65536 - C_filehandlesize ' for use with add immediate word with subi, sbci
' = minus c_FileHandleSize in Word-Format

Const C_filehandlessize = C_filehandlesize * Cfilehandles


Dim Abfilehandles(c_filehandlessize) As Byte
Const C_filesystemsramsize = C_filesystemsramsize1 + C_filesystemsramsize2 + C_filehandlessize


' End of variables for AVR-DOS ================================================

' Definitions of Constants ================================================== ==

' Bit definiton for FileSystemStatus

Dfilesystemstatusfat Alias 0 : Const Dfilesystemstatusfat = 0 ' 0 = FAT16, 1 = FAT32
Dfilesystemsubdir Alias 1 : Const Dfilesystemsubdir = 1 ' 0 = Root-Directory, 1 = Sub-Directory
Const Dmfilesystemsubdir =(2 ^ Dfilesystemsubdir) ' not used yet
Const Dmfilesystemdirincluster =(2 ^ Dfilesystemstatusfat + 2 ^ Dfilesystemsubdir) ' not used yet
Dfatsecondupdate Alias 7 : Const Dfatsecondupdate = 7 ' Bit-position for parameter of
' Update second FAT in gbFileSystemStatus


' Bit Definitions for BufferStatus (FAT, DIR, File)

Deof Alias 1 : Const Deof = 1 : Const Dmeof =(2 ^ Deof)
Deofinsector Alias 2 : Const Deofinsector = 2 : Const Dmeofinsector =(2 ^ Deofinsector)
Dwritepending Alias 3 : Const Dwritepending = 3 : Const Dmwritepending =(2 ^ Dwritepending)
Dfatsector Alias 4 : Const Dfatsector = 4 : Const Dmfatsector =(2 ^ Dfatsector) ' For Writing Sector back (FATNumber times)
Dfileempty Alias 5 : Const Dfileempty = 5 : Const Dmfileempty =(2 ^ Dfileempty)

' New feature for reduce saving
Dfatdirwritepending Alias 6 : Const Dfatdirwritepending = 6 : Const Dmfatdirwritepending =(2 ^ Dfatdirwritepending)
Dfatdirsaveatend Alias 7 : Const Dfatdirsaveatend = 7 : Const Dmfatdirsaveatend =(2 ^ Dfatdirsaveatend)
Dfatdirsaveanyway Alias 0 : Const Dfatdirsaveanyway = 0 : Const Dmfatdirsaveanyway =(2 ^ Dfatdirsaveanyway)




Const Dmeofall =(2 ^ Deof + 2 ^ Deofinsector)
Const Dmeof_empty =(2 ^ Deof + 2 ^ Deofinsector + 2 ^ Dfileempty)


Const Cp_fatbufferinitstatus =(2 ^ Dfatsector)
Const Cp_dirbufferinitstatus = 0


#if Cfatdirsaveatend = 1
Const Cp_filebufferinitstatus =(2 ^ Dfatdirsaveatend)
#else
Const Cp_filebufferinitstatus = 0
#endif



#if Cfatsecondupdate = 0
Const Cp_fatsecondupdate =(2 ^ Dfatsecondupdate)
#else
Const Cp_fatsecondupdate = 0
#endif


' Bit definitions for FileMode (Similar to DOS File Attribut)
Dreadonly Alias 0 : Const Dreadonly = 0
'Const cpFileReadOnly = &H21 ' Archiv and read-only Bit set
Const Cpfilewrite = &H20 ' Archiv Bit set


' Error Codes

' Group Number is upper nibble of Error-Code
' Group 0 (0-15): No Error or File End Information
Const Cpnoerror = 0
Const Cpendoffile = 1

' Group 1 (17-31): File System Init
Const Cpnombr = 17
Const Cpnopbr = 18
Const Cpfilesystemnotsupported = 19
Const Cpsectorsizenotsupported = 20
Const Cpsectorsperclusternotsupported = 21
Const Cpcountofclustersnotsupported = 22

' Group 2 (32-47): FAT - Error
Const Cpnonextcluster = 33
Const Cpnofreecluster = 34
Const Cpclustererror = 35
' Group 3 (49-63): Directory Error
Const Cpnofreedirentry = 49
Const Cpfileexists = 50
Const Cpfiledeletenotallowed = 51
Const Cpsubdirectorynotempty = 52
Const Cpsubdirectoryerror = 53
Const Cpnotasubdirectory = 54
' Group 4 (65-79): File Handle
Const Cpnofreefilenumber = 65
Const Cpfilenotfound = 66
Const Cpfilenumbernotfound = 67
Const Cpfileopennohandle = 68
Const Cpfileopenhandleinuse = 69
Const Cpfileopenshareconflict = 70
Const Cpfileinuse = 71
Const Cpfilereadonly = 72
Const Cpfilenowildcardallowed = 73
Const Cpfilenumberinvalid = 74 ' Zero is not allowed

' Group 7 (97-127): other errors
Const Cpfilepositionerror = 97
Const Cpfileaccesserror = 98
Const Cpinvalidfileposition = 99
Const Cpfilesizetogreat = 100

Const Cpdrivererrorstart = &HC0


' Range 224 to 255 is reserved for Driver

' Other Constants
' File Open Mode / stored in File-handle return-value of Fileattr(FN#, [1])
Const Cpfileopeninput = 1 ' Read
Const Cpfileopenoutput = 2 ' Write sequential
'Const cpFileOpenRandom = 4 ' not in use yet
Const Cpfileopenappend = 8 ' Write sequential; first set Pointer to end
Const Cpfileopenbinary = 32 ' Read and Write; Pointer can be changed by user


' permission Masks for file access routine regarding to the file open mode
Const Cfilewrite_mode = &B00101010 ' Binary, Append, Output
Const Cfileread_mode = &B00100001 ' Binary, Input
Const Cfileseekset_mode = &B00100000 ' Binary
Const Cfileinputline = &B00100001 ' Binary, Input
Const Cfileput_mode = &B00100000 ' Binary
Const Cfileget_mode = &B00100000 ' Binary

' Directory attributs in FAT16/32
Const Cpfileopenallowed = &B00100001 ' Read Only and Archiv may be set
Const Cpfiledeleteallowed = &B00100000
Const Cpfilesearchallowed = &B00111101 ' Do no search hidden Files
' Bit 0 = Read Only
' Bit 1 = Hidden
' Bit 2 = System
' Bit 3 = Volume ID
' Bit 4 = Directory
' Bit 5 = Archiv
' Long File name has Bit 0+1+2+3 set
Dim Lastdosmem As Byte
$lib "AVR-DOS.Lbx"


Sieht da irgendwo jemand noch nen Fehler?

mfG

for_ro
28.07.2008, 20:16
Hallo Jaecko,
du hast Sector als Word deklariert, sollte eigentlich Long sein.
Vielleicht nimmt er daher ganz andere Sektoren, als du denkst.

Gruß

Rolf

for_ro
30.07.2008, 18:56
Hallo Jaecko,
ich habe gerade mal dein Programm geflasht, aber alles weggelassen, was ich so an externer hardware nicht habe. Ich benutze SOft-SPI.
Wenn ich es mit DIM SECTOR AS WORD mache, kommt so ein Output:

Running...
Init: 0
Trying to read File system...
Wait...
OK
Filesystem: 6
FAT Start Sector: 250
Root Start Sector: 736
Data First Sector: 768
Max. Cluster Nummber: 62067
Sectors per Cluster: 32
Root Entries: 512
Sectors per FAT: 243
Number of FATs: 2
Disksize : 993056
Disk free: 51040
Sector Read Result: 229
Sector Read Result: 229
Sector Read Result: 229
Sector Read Result: 229
Sector Read Result: 229
Sector Read Result: 229
Sector Read Result: 229
Sector Read Result: 229
Sector Read Result: 229
Sector Read Result: 229
Done

Wenn ich es mit DIM SECTOR AS LONG mache, dann dies:

Running...
Init: 0
Trying to read File system...
Wait...
OK
Filesystem: 6
FAT Start Sector: 250
Root Start Sector: 736
Data First Sector: 768
Max. Cluster Nummber: 62067
Sectors per Cluster: 32
Root Entries: 512
Sectors per FAT: 243
Number of FATs: 2
Disksize : 993056
Disk free: 51040
Sector Read Result: 0
Data: 3 in Sector 0
Data: 61 in Sector 0
Data: 6 in Sector 0
Data: 28 in Sector 0
Data: 252 in Sector 0
Data: 217 in Sector 0
Data: 249 in Sector 0
Data: 7 in Sector 0
Data: 83 in Sector 0
Data: 30 in Sector 0
Data: 85 in Sector 0
Data: 170 in Sector 0
Sector Read Result: 0
Sector Read Result: 0
Sector Read Result: 0
Sector Read Result: 0
Sector Read Result: 0
Sector Read Result: 0
Sector Read Result: 0
Sector Read Result: 0
Sector Read Result: 0
Done

wobei ich nur die ersten 10 Sektoren ausgegeben habe.
Meine SD ist 1GB groß, enthält 6 Files und ist fast voll.

Gruß

Rolf

Jaecko
30.07.2008, 19:42
Thx... Stimmt, lag tatsächlich am Typ. Kaum isses long, schon gehts.