Cs_mmc Alias Porte.4 : Config Cs_mmc = Output 'Connected to Pin No.1 Of MMC (Cs_mmc)
Mosi_mmc Alias Porte.5 : Config Mosi_mmc = Output 'Connected to Pin No.2 Of MMC (DataIn)
Clk_mmc Alias Porte.7 : Config Clk_mmc = Output 'Connected to Pin No.5 Of MMC (Clock)
Miso_mmc Alias Pine.6 : Config Miso_mmc = Input 'Connected to Pin No.7 Of MMC (DataOut)
Declare Sub Mmc_init
Declare Sub Mmc_status
Declare Sub Mmc_singleread
Declare Sub Mmc_singlewrite
Dim Sector_pointer As Word
Dim Free_counter As Word
Dim Sector_number As Long
Dim Addr As Long
Dim Mmc_dat As Byte
Dim Mmc_resp As Byte
Dim Mmc_resp2 As Word
Dim Yyyyy As Byte
Const Msbl = 0 : Const Msbh = 1
Const Spidelayyyyy = 0
Waitms 128
Mmc_init
Sub Mmc_init
Print "--Start Initializing MMC ..."
Set Cs_mmc
Mmc_dat = &HFF
For Free_counter = 1 To 10
Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Next Free_counter
Mmc_resp = 255
Print "--Command No.=0"
Reset Cs_mmc
Cmd0:
Mmc_dat = &H40 : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy '8 bite avval
Addr = 0 : Shiftout Mosi_mmc , Clk_mmc , Addr , Msbl , 32 , Spidelayyyyy '32 bit argooman
Mmc_dat = &H95 : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
While Mmc_resp <> &H01
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Wend
Print "--Command Response :" ; Hex(mmc_resp)
Set Cs_mmc
Waitms 50
Print "--Command No.=1"
Reset Cs_mmc
Mmc_dat = &HFF
Cmd1:
While Mmc_resp <> &H00
Set Cs_mmc
Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Reset Cs_mmc
Mmc_dat = &H41 : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Addr = 0 : Shiftout Mosi_mmc , Clk_mmc , Addr , Msbl , 32 , Spidelayyyyy
Mmc_dat = &HFF : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Wend
Mmc_dat = &HFF
Set Cs_mmc
Print "--Command Response :" ; Hex(mmc_resp)
Print ""
Mmc_status '***************************chek for status
Print "--Initialize Complete."
End Sub
'-------------------------- MMC Initializing END -------------------------------
'-------------------------------------------------------------------------------
'---------------------------- Read MMC Status ----------------------------------
Sub Mmc_status
Cmd13:
Set Cs_mmc
Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Reset Cs_mmc
Mmc_dat = &H4D : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Addr = 0 : Shiftout Mosi_mmc , Clk_mmc , Addr , Msbl , 32 , Spidelayyyyy
Mmc_dat = &HFF : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp2 , Msbl , 8 , Spidelayyyyy
'Print "" : Print "--Status :" ; Hex(mmc_resp2)
Set Cs_mmc
End Sub
'--------------------------- Read MMC Status END -------------------------------
'-------------------------------------------------------------------------------
'------------------------- MMC Read Single Sector ------------------------------
Sub Mmc_singleread
Addr = Sector_number * 512
'Print "----------------------------------"
'Print "--Start Sector Reading ..."
'Print "--Read Sector Address :" ; Addr
'Print "--Command No.=17,(Read Single Block)"
Set Cs_mmc
Mmc_dat = &HFF
Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
'Cmd17:
Reset Cs_mmc
Mmc_dat = &H51 : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftout Mosi_mmc , Clk_mmc , Addr , Msbl , 32 , Spidelayyyyy
Mmc_dat = &HFF : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
While Mmc_resp <> 0
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Wend
'Print "--Command Response :" ; Hex(mmc_resp) : Print ""
While Mmc_resp <> &HFE
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Wend
For Sector_pointer = 1 To 512
Shiftin Miso_mmc , Clk_mmc , Mmc_data(sector_pointer) , Msbl , 8 , Spidelayyyyy
' Mmc_data(sector_pointer) = Mmc_resp
Next Sector_pointer
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Set Cs_mmc
Mmc_status '***************************chek for status
'Print "--Sector Reading Finished."
End Sub
'----------------------- MMC Read Single Sector END ----------------------------
'-------------------------------------------------------------------------------
'------------------------ MMC Write Single Sector ------------------------------
Sub Mmc_singlewrite
Addr = Sector_number * 512
Print "--Start Sector Writing ..."
Print "--Write Sector Address :" ; Addr
'Print "--Command No.=24(Write Single Block)"
Set Cs_mmc
Mmc_dat = &HFF : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Cmd24:
Reset Cs_mmc
Mmc_dat = &H58 : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftout Mosi_mmc , Clk_mmc , Addr , Msbl , 32 , Spidelayyyyy
Mmc_dat = &HFF : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
While Mmc_resp <> 0
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Wend
Print "--Command Response :" ; Hex(mmc_resp) : Print ""
Mmc_dat = &HFE : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
For Sector_pointer = 1 To 512
Mmc_dat = Mmc_data(sector_pointer)
Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Next Sector_pointer
Mmc_dat = &HFF : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Mmc_dat = &HFF : Shiftout Mosi_mmc , Clk_mmc , Mmc_dat , Msbl , 8 , Spidelayyyyy
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Yyyyy = Mmc_resp And &H0F
If Yyyyy = &H05 Then
Print "" : Print "--Data Accepted."
End If
Waitms 10
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
If Mmc_resp = 0 Then Print "--MMC Is Busy!"
While Mmc_resp = 0
Shiftin Miso_mmc , Clk_mmc , Mmc_resp , Msbl , 8 , Spidelayyyyy
Wend
Print "--MMC Is Ready!"
Set Cs_mmc
Mmc_status
Print "--Sector Writing Finished."
End Sub
'----------------------- MMC Write Single Sector END ---------------------------