[read_sec]
'***************************************************************
'issue Read Sector command to card
'***************************************************************
'Sub Read_sec(lbasector As Long , Byval Sec2xfer As Byte)
'lbasector= Y+2,Y+3 Sec2xfer=Y+0,Y+1
'entry point if registers need to be loaded by soft stack
Read_sec:
ldd Xl, Y+0
ldd Xh, Y+1 'X register pair points to Sec2xfer
ld r24, X 'r24 contains Sec2xfer
ldd Xl, Y+2
ldd Xh, Y+3 'X register pair points to lbasector
ld r16, X+ 'r16 contains bits 0 - 7 of LBA & post incr
ld r17, X+ 'r17 contains bits 8 - 15 of LBA & post incr
ld r18, X+ 'r18 contains bits 16 - 23 of LBA & post incr
ld r19, X 'r19 contains bits 24 to 31 of LBA only 27 is used
'entry point if registers pre loaded
'r24 = sectors to transfer, r16 to r19 = lbasector
[Readsec]
$EXTERNAL Lba2add , Txrxspi , Txrxspi_wait4resp , Chk4error
Readsec:
rcall Txrxspi255 'send 255 without chip select to flush card buffer
Cbi Spiport , Sdmmccs 'make SD / MMC Chip Select active
rcall Lba2add 'convert LBA to address
ldi r23, Read_multiple_block
rcall Txrxspi
mov r23, r19 'send address hi
rcall Txrxspi
mov r23, r18
rcall Txrxspi
mov r23, r17
rcall Txrxspi
mov r23, r16
rcall Txrxspi_wait4resp 'send address lo & wait for response
rcall Chk4error 'check for any error
ret
[end]
[get_sec]
'***************************************************************
'Get da Sector Data from the card and Stick it in da RAM
'***************************************************************
'Sub Get_sec(buffer() , Byval Sec_expected As Byte )
'buffer()= Y+2,Y+3 Sec_expected=Y+0,Y+1
'entry point if registers need to be loaded by soft stack
Get_sec:
ldd Xl, Y+0
ldd Xh, Y+1 'X register pair points to Sec_expected
ld r24, X 'r24 contains Sec_expected
ldd Xl, Y+2 'X register pair points to buffer
ldd Xh, Y+3
'entry point if registers pre loaded
'r24 = Sector Counter (Sec_expected)
'Xl & Xh = (X register pair) points to buffer
[Getsec]
$EXTERNAL Txrxspi_wait4token , Txrxspi , Txrxspi_wait4resp , Chk4error
Getsec:
rcall Txrxspi_wait4token 'wait for data token
ldi r25, 0 'word counter
Getsec_loop:
cpi r25, &HFF 'have we got 2 bytes to go?
brne Getsec_loop1 'if not, keep looping
dec r24 'decr sec_expected
brne Getsec_loop1 'have we finished?
ldi r23, Stop_transmission 'if so, then stop txing
rcall Txrxspi 'get a byte in r23
st X+, r23 'store to buffer
rjmp Getsec_loop2
Getsec_loop1:
rcall Txrxspi255 'get a byte in r23
st X+, r23 'store to buffer
Getsec_loop2:
rcall Txrxspi255
st X+, r23 'again. 2 bytes = 1 word
inc r25 'incr word counter
brne getsec_loop 'have we finished transfering 1 block
rcall Txrxspi255 'get crc16 hi & dump it
rcall Txrxspi255 'get crc16 lo & dump it
tst r24 'is sector counter = 0
brne Getsec 'if not finished, get some more data
rcall Txrxspi_wait4resp 'wait for valid response
rcall Chk4error 'check for any error
sbi Spiport, Sdmmccs 'make SD / MMC Chip Select not active
ret
[end]