.byte 1
mmcCardType:
.byte 1
-mmc_ocr:
- .byte 4
-
.cseg
tst temp ;set flags
ret
+;------------------------------------------------
+; Send command and receive ocr response
+; temp2: Command, zl: expected cmd response
+; yh..xl: Argument
+; return: yh..xl: ocr, z flag == 1, if return from mmcCmd was ok.
+
+mmcCmd_ocr:
+
+ rcall mmcCmd
+
+ cp temp,zl ;
+ brne mmc_cocr_e
+
+; Get trailing return value of R7 response
+
+ ldi temp2,4
+ ldiw z,0x1a ;memory address of xl
+mmc_cocr1:
+ rcall spi_rcvr
+ st z+,temp
+ dec temp2
+ brne mmc_cocr1
+ ;z-flag =1
+mmc_cocr_e:
+ ret
+
;------------------------------------------------
; Check if 1 sec timeout
; return Z-Flag set, if timeout
ldiw y,0
ldi xh,0x01
ldi xl,0xAA
- rcall mmcCmd
- cpi temp,1 ;SDv2?
- brne mmci_sdv1
+ ldi zl,1
+ rcall mmcCmd_ocr
+ brne mmci_sdv1 ;SDv2?
-; Get trailing return value of R7 response
-
- ldi temp2,4
- ldiw z,mmc_ocr
-mmci_v2l1:
- rcall spi_rcvr
- st z+,temp
- dec temp2
- brne mmci_v2l1
- sbiw z,4
- ldd temp,z+2
- cpi temp,0x01
- ldd temp,z+3
- cpc temp,xl ;Reuse 0xAA value in xl
+ cpi yl,0x01 ;ocr[2]
+ brne mmci_sdv1
+ cpi yh,0xAA ;ocr[3]
brne mmci_sdv1
; The card can work at vdd range of 2.7-3.6V.
mmci_ccc:
ldi temp2,CMD58
ldi yh,0
- rcall mmcCmd
+ ldi zl,0
+ rcall mmcCmd_ocr
+
brne mmci_sdv2end
- ldi temp2,4
-mmci_v2l3:
- rcall spi_rcvr
- st z+,temp
- dec temp2
- brne mmci_v2l3
- sbiw z,4
-
sbr temp3,CT_SD2
- ldd temp,z+0
- sbrc temp,6
+ sbrc xl,6
sbr temp3,CT_BLOCK
mmci_sdv2end:
.if MMC_DEBUG > 3
printnewline
.endif
- rcall spi_wait ; while SPI module shifts in crc part1.
+ rcall spi_wait ;while SPI module shifts in crc part1.
rcall spi_rcvr ;Read second crc.
ldi temp2,RES_OK ;Return success
;--------------------------------------------------------------
; Read word
-; TODO: Read Word to ZL,ZH at given ZL/ZH Offset
-; Need for reading of single FAT16 Entrys without killing the
-; Entrys in hostbuffer...
+; Read Word to ZL,ZH at given ZL/ZH Offset
+; Needed for reading a single FAT16 entry without killing the
+; entries in hostbuffer...
;
-; in zh,zl: Pointer to Word within the Sector to read
+; in zh,zl: Pointer to word within the sector to read
; in yh..xl: Start sector number (LBA)
-; out zh,zl : word thats been read
+; out zh,zl: Word thats been read
mmcReadWord:
.if MMC_DEBUG > 1
printnewline
- printstring "mmcRdWord "
+ printstring "mmcRdWord "
.endif
- lds _tmp0,mmcStat
- ldi temp,RES_NOTRDY
+ lds _tmp0,mmcStat
+ ldi temp,RES_NOTRDY
sbrc _tmp0,MMCST_NOINIT
ret
spi_clkfast
- lds temp,mmcCardType
+ lds temp,mmcCardType
sbrs temp,log2(CT_BLOCK)
rcall mul_yx_512 ;Convert to byte address (*512)
- ldi temp2,CMD17
+ ldi temp2,CMD17
rcall mmcCmd
- ldi temp2,RES_ERROR
+ ldi temp2,RES_ERROR
brne mmc_rdexw
; Receive a data packet from MMC
ldiw y,512 ;Number of bytes to tranfer
- ldi temp,200 ;Wait for data packet in timeout of 200ms.
- sts delay_timer1,temp
+ ldi temp,200 ;Wait for data packet in timeout of 200ms.
+ sts delay_timer1,temp
mmc_rcvw_wl:
rcall spi_rcvr
- cp temp,_255
+ cp temp,_255
brne mmc_rcvw_start
- lds temp2,delay_timer1
- cpi temp2,0
+ lds temp2,delay_timer1
+ cpi temp2,0
brne mmc_rcvw_wl
mmc_rcvw_start:
- cpi temp,0xFE ;If not valid data token,
- ldi temp2,RES_ERROR
+ cpi temp,0xFE ;If not valid data token,
+ ldi temp2,RES_ERROR
brne mmc_rdexw
rcall spi_rcvr ;Shift in first byte.
- out SPDR,_255 ;Start shift in next byte.
+ out SPDR,_255 ;Start shift in next byte.
mmc_rcvw_rl:
sbiw yl,1
breq mmc_rcvw_rle
- cp zl,_0
- cpc zh,_0
+ cp zl,_0
+ cpc zh,_0
breq mmc_rcvw_sto
sbiw zl,1
spi_waitm
- in temp,SPDR
- out SPDR,_255
+ in temp,SPDR
+ out SPDR,_255
rjmp mmc_rcvw_rl
mmc_rcvw_sto:
mov zl,temp
spi_waitm
- in temp,SPDR
- out SPDR,_255
+ in temp,SPDR
+ out SPDR,_255
mov zh,temp
mmc_rcvw_rl2:
sbiw yl,1
breq mmc_rcvw_rle
spi_waitm
- in temp,SPDR
- out SPDR,_255
+ in temp,SPDR
+ out SPDR,_255
rjmp mmc_rcvw_rl2
mmc_rcvw_rle:
rcall spi_wait ; while SPI module shifts in crc part1.
rcall spi_rcvr ;Read second crc.
- ldi temp2,RES_OK ;Return success
+ ldi temp2,RES_OK ;Return success
mmc_rdexw:
rcall mmcDeselect
spi_disable
mov temp,temp2
.if MMC_DEBUG > 1
- printstring "RdWordRes: "
+ printstring "RdWordRes: "
rcall printhex
- printstring " "
+ printstring " "
.endif
ret