اتصال AVR به MMC

mrmirlohi

عضو جدید
کمک فوری درباره پیکربندی mmc با avr با فرمت فایل fat32 در زبان bascom
 

ehsanavr2008

عضو جدید
سلام
این هم از کد هاش:

$regfile = "M103def.dat" 'AT103L mcu

'Constants
Const Msbl = 0
Const Msbh = 1
Const Dly = 2
Const Bits8 = 8
Const Bits16 = 16
Const Bits32 = 32

'Variables
Dim Res(16) As Byte
Dim Indat(512) As Byte 'Memory buffer for temp storage.
Dim Dat As Byte
Dim Resp As Byte
Dim Resp2 As Word
Dim I As Word
Dim J As Word
Dim X As Byte
Dim Y As Byte
Dim K As Byte
Dim Bout As Byte
Dim Addr1 As Byte
Dim Addr2 As Byte
Dim Addr3 As Byte
Dim Addr4 As Byte
Dim Addr As Long
Dim Saddr As Long
Dim Eaddr As Long

'Aliases
Cs Alias Portb.0
Clk Alias Portb.1
Miso Alias Pinb.3 'inp
Mosi Alias Portb.2 'out

'Declarations
Declare Sub Mprint
Declare Sub Minit
Declare Sub Mstatus
Declare Sub Mwrite(byval Bout As Byte , Byval Addr As Long )
Declare Sub Mread(byval Addr As Long )
Declare Sub Merase(byval Saddr As Long , Byval Eaddr As Long)

'AT103 HW SPI Portb0=SS to MMC 1, Portb1=clock to MMC 5, MISO=Portb3 to MMC 7, MOSI=Portb2 to MMC 2

Config Pinb.0 = Output
Config Pinb.1 = Output
Config Pinb.2 = Output
Config Pinb.3 = Input
Waitms 300
Config Spi = Soft , Din = Pinb.3 , Dout = Portb.2 , Ss = Portb.0 , Clock = Portb.1

Spiinit




'******** MAIN PROGRAM *************
Main:

'Initialize the MMCC
Minit


'If you want to use the ERASE routine
'Uncomment and enter the start/end 32bit address values

'Call erase subroutine enter StartAddr , EndAddr
'Merase &H00000000 , &H00001000


'call write subroutine as: BYTE-TO-WRITE, 32BIT_ADDRESS
Mwrite &HBE , &H00000000

'Call read sub from 512 from address are put in array INDAT
Mread &H00000000

'Call print sub print 512 bytes stored in INDAT array
Mprint


Xloop:
Set Cs
Shiftout Mosi , Clk , Dat , Msbl
Endloop:
Goto Endloop

End 'end program
'************ END OF PROGRAM **************



'======= SUB ROUTINES AND FUNCTIONS =======

'*** INITIALIZATION OF MMC ***
Sub Minit
Set Cs
Dat = &HFF
For I = 1 To 10
Shiftout Mosi , Clk , Dat , Msbl
Next I
Resp = 255
Reset Cs

Cmd0:
Dat = &H40
Shiftout Mosi , Clk , Dat , Msbl
Addr = &H00000000
Shiftout Mosi , Clk , Addr , Msbl
Dat = &H95
Shiftout Mosi , Clk , Dat , Msbl
While Resp <> &H01
Shiftin Miso , Clk , Resp , Msbl
Wend

Lcd "C0:" ; Hex(resp)
'Initialized
Set Cs
Waitms 50
Reset Cs
Dat = &HFF

Cmd1:
While Resp <> &H00
Set Cs
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
Reset Cs
Dat = &H41
Shiftout Mosi , Clk , Dat , Msbl
Addr = 0
Shiftout Mosi , Clk , Addr , Msbl
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
Wend

Dat = &HFF
Set Cs
Lcd "C1:" ; Hex(resp)
End Sub

'**** READ routine assumes ADDR uses Status subroutine *****
Sub Mread(byval Addr As Long)
Set Cs
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
Reset Cs
Dat = &H51
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Addr , Msbl
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl

While Resp <> 0
Shiftin Miso , Clk , Resp , Msbl
Wend

While Resp <> &HFE
Shiftin Miso , Clk , Resp , Msbl
Wend

'Lcd Hex(resp)
For I = 1 To 512
Shiftin Miso , Clk , Resp , Msbl
Indat(i) = Resp
'Print Hex(resp);
Next I

Shiftin Miso , Clk , Resp , Msbl
Shiftin Miso , Clk , Resp , Msbl

Set Cs
Mstatus
'Return
End Sub

'****Block Start Tag / End Tag and ERASE ****
Sub Merase(byval Saddr As Long , Byval Eaddr As Long)

'erase first 1024 bytes

'Block START TAG
Cmd32:

Set Cs
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
Reset Cs
Dat = &H60
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Saddr , Msbl
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
While Resp <> &H00
Shiftin Miso , Clk , Resp , Msbl
Wend

'Block END TAG
Cmd33:

Set Cs
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
Reset Cs
Dat = &H61
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Eaddr , Msbl
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
While Resp <> &H00
Shiftin Miso , Clk , Resp , Msbl
Wend

'ERASE SELECTED BLOCK
Cmd38:
Set Cs
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
Reset Cs
Dat = &H66
Shiftout Mosi , Clk , Dat , Msbl
Addr = 0
Shiftout Mosi , Clk , Addr , Msbl
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
While Resp = 0
Shiftin Miso , Clk , Resp , Msbl
Wend

Mstatus
End Sub

'** WRITE Routine assumes ADDR, BOUT uses Status subroutine **
Sub Mwrite(byval Bout As Byte , Byval Addr As Long)
Cmd24:
Set Cs
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
Reset Cs
Dat = &H58
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Addr , Msbl
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
While Resp <> 0
Shiftin Miso , Clk , Resp , Msbl
Wend
Lcd "C24:" ; Hex(resp)

Dat = &HFE
Shiftout Mosi , Clk , Dat , Msbl

For I = 1 To 512
Shiftout Mosi , Clk , Bout , Msbl
Next I
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl

'If Y = &H05 write is a success
Y = Resp And &H0F

'If Resp = 0 the MMC is busy
Shiftin Miso , Clk , Resp , Msbl
While Resp = 0
Shiftin Miso , Clk , Resp , Msbl
Wend

Lowerline

Lcd "Wr:" ; Hex(y)
Set Cs
Mstatus
End Sub

'Get MMC Status
Sub Mstatus
Cmd13:
Set Cs
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp , Msbl
Reset Cs
Dat = &H4D
Shiftout Mosi , Clk , Dat , Msbl
Addr = 0
Shiftout Mosi , Clk , Addr , Msbl
Dat = &HFF
Shiftout Mosi , Clk , Dat , Msbl
Shiftout Mosi , Clk , Dat , Msbl
Shiftin Miso , Clk , Resp2 , Msbl
Print "Status:" ; Hex(resp2)
Set Cs
'Return
End Sub

Sub Mprint
For I = 1 To 512
Print Hex(indat(i));
Next
Mstatus
End Sub​
این هم از توضیحات:
mmc مخفف سه كلمه ي multi media card هست

اين كارتها كارتهاي حافظه هستن كه به صورت flash memory ساخته شدن و در اندازه هاي 2m تا 2g ساخته ميشن

توي گوشي هاي جديد اكثرا از اين كارتها استفاده شده
كاربرد عمده ي اين كارتها واسه ي ميكرو واسه ي ساختن ديتا بيس هست كه ميتونه واسه تابلو روان يا هر چيز ديگه كه نياز به حافظه ي زياد داشته باشه

mmc داراي 7 پين هست كه اكثرا ميدونيد حتي تو proteus ميتونيد اين كارت رو پيدا كنيد

اين كارت با دو استندارد mmc و spi ارتباط برقرار مي كنند . برنامه ي بالا با استاندارد spi كار مي كنه

برنا مه خيلي واضحه


$regfile = "m8def.dat"
$crystal = 1000000
Config Lcdpin = Pin , Db4 = Pind.1 , Db5 = Pind.2 , Db6 = Pind.3 , Db7 = Pind.4 , E = Pind.0 , Rs = Pind.7
Config Lcd = 16 * 2

Cls

'Constants
Const Msbl = 0
Const Msbh = 1
Const Dly = 2
Const Bits8 = 8
Const Bits16 = 16
Const Bits32 = 32

'Variables
Dim Res(16) As Byte
Dim Indat(512) As Byte 'Memory buffer for temp storage.
Dim Dat As Byte
Dim Resp As Byte
Dim Resp2 As Word
Dim I As Word
Dim J As Word
Dim X As Byte
Dim Y As Byte
Dim K As Byte
Dim Bout As Byte
Dim Addr1 As Byte
Dim Addr2 As Byte
Dim Addr3 As Byte
Dim Addr4 As Byte
Dim Addr As Long
Dim Saddr As Long
Dim Eaddr As Long

'Aliases
Cs Alias Portb.0
Clk Alias Portb.1
Miso Alias Pinb.3 'inp
Mosi Alias Portb.2

اين قسمت از برنامه معرفي چيپ و بقيه ي كارها كه لازمه انجام شده


Declare Sub Mprint
Declare Sub Minit
Declare Sub Mstatus
Declare Sub Mwrite(byval Bout As Byte , Byval Addr As Long )
Declare Sub Mread(byval Addr As Long )
Declare Sub Merase(byval Saddr As Long , Byval Eaddr As Long)


اين ساب روتين ها مهمترين قسمت هاي برنامه هستن

اوليش واسه فرستادن اطلاعات خوتده شده به پورت سريال هستش
دوميش واسه Initialize كردن كارت هست كه هر بار كارت ريست بشه اين كار لازمه
سوميش ساب روتين status يا وضعيت فعلي كارت هستش

Status
Bits Identifier Type Value Description Clear condition
31 OUT_OF_RANGE E R ’0’= no error
’1’= error
The commands argument was out
of allowed range for this card.
C
30 ADDRESS_ERROR E R X ’0’= no error
’1’= error
A misaligned address, which did
not match the block length was
used in the command.
C
29 BLOCK_LEN_ERRO
R
E R ’0’= no error
’1’= error
The transferred block length is not
allowed for this card or the
number of transferred bytes does
not match the block length
C
28 ERASE_SEQ_ERR
OR
E R ’0’= no error
’1’= error
An error in the sequence of erase
commands occurred.
C
27 ERASE_PARAM E X ’0’= no error
’1’= error
An invalid selection, sectors or
groups, for erase.
C
26 WP_VIOLATION E R X ’0’= not protected
’1’= protected
The command tried to write a
write protected block.
C
25 CARD_IS_LOCKED S X ’0’= card
unlocked
’1’= card locked
When set, signals that the card is
locked by the host.
A
24 LOCK_UNLOCK_
FAILED
E R X ’0’= no error
’1’= error
Set when a sequence or
password error has been detected
in lock/unlock card command or it
there was an attempt to access a
locked card.
C
23 COM_CRC_ERROR E R ’0’= no error
’1’= error
The CRC check of the previous
command failed.
B
22 ILLEGAL_COMMAN
D
E R ’0’= no error
’1’= error
Command not legal for the current
state
B
21 CARD_ECC_FAILE
D
E X ’0’= success
’1’= failure
Card internal ECC was applied
but the correction of data is failed.
C
20 CC_ERROR E R X ’0’= no error
’1’= error
Internal card controller error C
19 ERROR E R X ’0’= no error
’1’= error
A general or an unknown error
occurred during the operation.
C
18 UNDERRUN E X ’0’= no error
’1’= error
The card could not sustain data
transfer in stream read mode.
C
17 OVERRUN E X ’0’= no error
’1’= error
The card could not sustain data
programming in stream write
mode.
C
16 CID_OVERWRITE/
CSD_OVERWRITE
E R X ’0’= no error
’1’= error
can be either one of the following
errors:
· The CID register is already
written and can not be
overwritten.
· The read only section of the
CSD does not match the card
content.
· An attempt to reversecopy (set
as original) or permanent WP
(unprotect) bits was done.
C
15 WP_ERASE_SKIP S X ’0’= not protected
’1’= protected
Only partial address space was
erased due to existing WP blocks.
C
14 CARD_ECC_
DISABLED
S X ’0’= enabled
’1’= disabled
The command has been executed
without using the internal ECC.
A
13 ERASE_RESET S R ’0’= cleared
’1’= set
An erase sequence was cleared
before executing because an out
of erase sequence command was
received
C
12:9 CURRENT_STATE S X 0 = idle
1 = ready
2 = ident
3 = stby
4 = tran
5 = data
6 = rcv
7 = prg
8 = dis
9-15 = reserved
Current state of the card. B
8 BUFFER_EMPTY S X ’0’= not empty
’1’= empty
corresponds to buffer empty
signalling on the bus
A
7:6 reserved Permanently 0
5 APP_CMD S R ’0’= disabled
’1’= enabled
The card will expect ACMD or
indication that the command has
been interpreted as ACMD.
C
4 reserved Permanently 0
3:2 reserved for application specific commands
1:0 reserved for manufacturer test mode
اين متغير 16 بيتي تمام اطلاعات مربوط به وضعيت فعلي كارت طبق قوانين بالا به ماميگه

اين اطلاعات از ديتا شيت يك كارت 32 مگا بايتي گرفته شده از لينك زير دانلود كنيد

HB288032MM1 - Hitachi Semiconductor
MultiMediaCard 32 MByte
http://www.datasheet4u.com/html/H/B/2/HB288032MM1_HitachiSemiconductor.pdf.html



چها رمين و پنجمين ساب روتين براي خواندن و نوشتن هستن

mmc به صورت سكتور سكتور دسته بندي شده كه هر سكتور 512 بايت هست

براي خوندن حتي يك بايت ما مجبوريم سكتور مربوط به اون بايت رو بخونيم كه برنامه اون رو در يك متغير بافر به نام indat دخيره ميكنه

اين قانون واسه نوشتن و يا حتي براي ساب روتين erase اين قانون سكتور صادقه

يعني ما فقط سكتور به سكتور ميتونيم erase كنيم

براي تغير يك بايت بايد اول سكتور مربوطه رو بخونيم بعد اطلاعاط درون متغير indat رو هر جور خواستيم تغير ميديم بعد در اخر دوباره متغير indat رو در سكتور مربوطه write مي كنيم

اميد وارم كه مفيد بوده باشه
 

Similar threads

بالا