📄 indoor-sdcard.asm
字号:
; - Read byte will be returnd in temp1 register
; - MSB will be received as first bit
;
; *********************************************************
Read_Byte_SDCard:
;
; Save registers in use
;
push temp2 ; Save temp2 register
ldi temp1, 0 ; Clear the byte
ldi temp2, 8 ; 8 bits to be read
Read_Byte_SDCard_1:
;
; Low puls to SDCard clock
;
cbi SDCard_Write, SDCard_Clock
;
; Shift data left
;
lsl temp1
;
; Read input from SD Card
;
sbis SDCard_Read, SDCard_DataIn ; Skip if bit is set
rjmp Read_Byte_SDCard_2
sbr temp1, 1 ; Set bit 0 (MSB for first clock pulse)
;
Read_Byte_SDCard_2:
;
; High puls to SDCard clock
;
sbi SDCard_Write, SDCard_Clock
;
; Decrease bit counter
;
dec temp2
brne Read_Byte_SDCard_1
;
; Restore used registers
;
pop temp2
ret
; *********************************************************
; Function Write_Block_SDCard
;
; Writes a block of 512 bytes to the SD Card
; The block index defines, where the data shall be written into the SD Card
; The 512 data bytes to be written is in the SRAM at the address SDCard_DataBuffer
;
; Input parameters:
; - YH:YL and XH:XL hold the block index number (Y=High 16 bit, X=Low 16 bit)
; - SDCard_DataBuffer [0-511]: here the data to be written are located
; Output parameters:
; - temp1 = 0 -> function successfull, otherwise failed
;
; *********************************************************
Write_Block_SDCard:
;
; Save registers in use
;
push temp2 ; Save temp2 register
push ZL ; Save ZL register
push ZH ; Save ZH register
;
; Compute the correct address for the block, as defined by the block index number
; The address will be stored in the 6 byte command CMD24
; The address for the SD Card will be given in bytes, therefore we have to convert
; the block indes number to bytes
;
; Multiply Block index number by 512 (9 * rotate left with carry)
;
ldi temp1, 9
;
; Now rotate the 4 byte word 9 times
;
Write_Block_SDCard_1:
clc
rol XL
rol XH
rol YL
rol YH
;
; decrement counter
;
dec temp1
brne Write_Block_SDCard_1
;
; Now write the result to the command byte sequence
; Get SRAM address of the Command 24
;
ldi ZH, HIGH(SDCard_Command_24) ; Get high address of SRAM of Command 24
ldi ZL, LOW(SDCard_Command_24) ; Get low address of SRAM of Command 24
adiw ZH:ZL, 1 ; Point to next byte in SRAM of Command 24
;
; Store address bytes to SRAM command 24
;
st Z+, YH
st Z+, YL
st Z+, XH
st Z+, XL
;
; Write the command to the SD Card
;
ldi ZH, HIGH(SDCard_Command_24) ; Get high address of SRAM of Command 24
ldi ZL, LOW(SDCard_Command_24) ; Get low address of SRAM of Command 24
call Write_Command_SDCard
;
; When return value is not 0, then error
;
cpi temp1, 0
brne Write_Block_SDCard_5
;
; Wait for a moment and send a clock to the SD Card
;
ldi temp2, 8
Write_Block_SDCard_2:
ldi temp1, 0xFF
call Write_Byte_SDCard
dec temp2
brne Write_Block_SDCard_2
;
; Send start byte (0xFE) to SD Card
;
ldi temp1, 0xFE
call Write_Byte_SDCard
;
; Start writing 512 bytes to the SD Card
; First get the SRAM buffer address, where the bytes are stored
;
ldi XH, HIGH(512)
ldi XL, LOW(512)
ldi ZH, HIGH(SDCard_DataBuffer) ; Get high address of SRAM buffer
ldi ZL, LOW(SDCard_DataBuffer) ; Get low address of SRAM buffer
;
; The loop for writing the bytes to the SD Card
;
Write_Block_SDCard_3:
ld temp1, Z+
call Write_Byte_SDCard
;
; Decrement word counter
;
sbiw XH:XL, 1
cpi XL, 0
brne Write_Block_SDCard_3
cpi XH, 0
brne Write_Block_SDCard_3
;
; Write a dummy CRC byte two times
;
ldi temp1, 0xFF
call Write_Byte_SDCard
ldi temp1, 0xFF
call Write_Byte_SDCard
;
; Now wait for SD Card BUSY signal (0xFF)
;
Write_Block_SDCard_4:
call Read_Byte_SDCard
cpi temp1, 0xFF
brne Write_Block_SDCard_4
;
; Set return value to 0
;
ldi temp1, 0
Write_Block_SDCard_5:
;
; Disable SD Card
;
call Disable_SDCard
;
; Restore used registers
;
pop ZH ; Restore ZH register
pop ZL ; Restore ZL register
pop temp2 ; Restore temp2 register
;
ret
; *********************************************************
; Function Read_Block_SDCard
;
; Reads a block of 512 bytes from the SD Card and write it to the data SRAM buffer SDCard_DataBuffer
; The block index defines, where the data shall be read from the SD Card
; ;
; Input parameters:
; - YH:YL and XH:XL hold the block index number (Y=High 16 bit, X=Low 16 bit)
; Output parameters:
; - temp1 = 0 -> function successfull, otherwise failed
; - SDCard_DataBuffer [0-511]: here the read data will be written
;
; *********************************************************
Read_Block_SDCard:
;
; Save registers in use
;
push temp2 ; Save temp2 register
push ZL ; Save ZL register
push ZH ; Save ZH register
;
; Compute the correct address for the block, as defined by the block index number
; The address will be stored in the 6 byte SD Card command CMD16
; The address for the SD Card will be given in bytes, therefore we have to convert
; the block indes number to bytes
;
; Multiply Block index number by 512 (9 * rotate left with carry)
;
ldi temp1, 9
;
; Now rotate the 4 byte word 9 times
;
Read_Block_SDCard_1:
clc
rol XL
rol XH
rol YL
rol YH
;
; decrement counter
;
dec temp1
brne Read_Block_SDCard_1
;
; Now write the result to the command byte sequence
; Get SRAM address of the Command 16
;
ldi ZH, HIGH(SDCard_Command_16) ; Get high address of SRAM of command 16
ldi ZL, LOW(SDCard_Command_16) ; Get low address of SRAM of command 16
adiw ZH:ZL, 1 ; Point to next byte in SRAM of command 16
;
; Store address bytes to SRAM command 16
;
st Z+, YH
st Z+, YL
st Z+, XH
st Z+, XL
;
; Write the command to the SD Card
;
ldi ZH, HIGH(SDCard_Command_16) ; Get high address of SRAM of command 16
ldi ZL, LOW(SDCard_Command_16) ; Get low address of SRAM of command 16
call Write_Command_SDCard
;
; When return value is not 0, then error
;
cpi temp1, 0
brne Read_Block_SDCard_4
;
; Read the start byte (0xFE) from the SD Card
;
Read_Block_SDCard_2:
call Read_Byte_SDCard
cpi temp1, 0xFE
brne Read_Block_SDCard_2
;
; Read the 512 bytes from the SD Card and write it to the SRAM buffer
; First get the SRAM buffer address, where the bytes shall be stored
;
ldi XH, HIGH(512)
ldi XL, LOW(512)
ldi ZH, HIGH(SDCard_DataBuffer) ; Get high address of SRAM buffer
ldi ZL, LOW(SDCard_DataBuffer) ; Get low address of SRAM buffer
;
; Read the byte and write it to SRAM
;
Read_Block_SDCard_3:
call Read_Byte_SDCard
st Z+, temp1
;
; Decrement word counter
;
sbiw XH:XL, 1
cpi XL, 0
brne Read_Block_SDCard_3
cpi XH, 0
brne Read_Block_SDCard_3
;
; OK - Done
; Read a dummy CRC byte two times
;
call Read_Byte_SDCard
call Read_Byte_SDCard
;
; Set return value to 0
;
ldi temp1, 0
Read_Block_SDCard_4:
;
; Disable SD Card
;
call Disable_SDCard
;
; Restore used registers
;
pop ZH ; Restore ZH register
pop ZL ; Restore ZL register
pop temp2 ; Restore temp2 register
ret
; *********************************************************
; Function Enable_SDCard
;
; Set Chip Select of SD Card to 0
; *********************************************************
Enable_SDCard:
;
cbi SDCard_Write, SDCard_ChipSelect
ret
; *********************************************************
; Function Disable_SDCard
;
; Set Chip Select of SD Card to 1
; *********************************************************
Disable_SDCard:
;
sbi SDCard_Write, SDCard_ChipSelect
ret
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -