recipe/rc2014/sdcard: we read data!

This commit is contained in:
Virgil Dupras 2019-05-07 17:28:07 -04:00
parent beeb40eb93
commit 3d82d7fb30
2 changed files with 74 additions and 22 deletions

View File

@ -83,9 +83,19 @@ the block creation part.
## Testing CD card initialization
To test that you can properly initialize a SD card, you can compile this [user
program](sdinit.asm) (see [Makefile](Makefile)) and then
[run it from memory][run-from-mem]. Success means the card is initialized.
This receipes contains a little [user program](sdinit.asm) that initializes a
SD card, reads the first 12 bytes from its first sector and prints it.
The first thing we'll do is fill the SD card's first 12 bytes with "Hello
World!":
echo "Hello World!" > /dev/sdX
Then, you can run `make` from within this folder to compile `sdinit.bin` and
then [upload and run][run-from-mem] that code from memory. You might need to
call the routine more than once (On my local tests, I need to call it twice).
If all goes well, you should see your "Hello World!" printed to the console!
## Create a block device from the SD card reader

View File

@ -4,7 +4,7 @@
call JUMP_SDCWAKEUP
; We expect a 0x01 R1 response
ld hl, sCmd0
ld hl, sCmd
call JUMP_PRINTSTR
ld a, 0b01000000 ; CMD0
ld hl, 0
@ -12,27 +12,21 @@
ld c, 0x95
call JUMP_SDCCMDR1
cp 0x01
jr nz, .error
jp nz, .error
ld hl, sOk
call JUMP_PRINTSTR
; We expect a 0x01 R1 response followed by 0x0001aa R7 response
ld hl, sCmd8
ld hl, sCmd
call JUMP_PRINTSTR
ld a, 0b01001000 ; CMD8
ld hl, 0
ld de, 0x01aa
ld c, 0x87
call JUMP_SDCCMDR7
ld a, h
cp 0
jr nz, .error
ld a, l
cp 0
jr nz, .error
ld a, d
cp 0x01
jr nz, .error
jp nz, .error
ld a, e
cp 0xaa
jr nz, .error
@ -43,9 +37,9 @@
; the card goes out of idle mode, that is, when it stops sending us
; 0x01 response and send us 0x00 instead. Any other response means that
; initialization failed.
ld hl, sCmd41
ld hl, sCmd
call JUMP_PRINTSTR
.loop:
.loop1:
ld a, 0b01110111 ; CMD55
ld hl, 0
ld de, 0
@ -57,25 +51,73 @@
ld de, 0x0000
call JUMP_SDCCMDR1
cp 0x01
jr z, .loop
jr z, .loop1
cp 0
jr nz, .error
; Success! out of idle mode!
ld hl, sOk
call JUMP_PRINTSTR
; Alright, normally we should configure block size and all, but this is
; too exciting and we'll play it dirty: we'll read just enough bytes
; to fetch our "Hello World!" and print it and we'll leave the SD card
; hanging. Yeah, I know, not very polite.
out (5), a
ld hl, sCmd
call JUMP_PRINTSTR
ld a, 0b01010001 ; CMD17
ld hl, 0 ; read single block at addr 0
ld de, 0
call JUMP_SDCCMD
cp 0
jr nz, .error
ld hl, sCmd
call JUMP_PRINTSTR
; Command sent, no error, now let's wait for our data response.
ld b, 20
.loop3:
call JUMP_SDCWAITRESP
; 0xfe is the expected data token for CMD17
cp 0xfe
jr z, .loop3end
cp 0xff
jr nz, .error
djnz .loop3
jr .error
.loop3end:
ld hl, sGettingData
call JUMP_PRINTSTR
; Data packets follow immediately
ld b, 12 ; size of "Hello World!"
ld hl, sDest ; sDest has null chars, we'll be alright
; printing it.
.loop2:
call JUMP_SDCWAITRESP
ld (hl), a
inc hl
djnz .loop2
out (6), a
ld hl, sDest
call JUMP_PRINTSTR
ret
.error:
call JUMP_PRINTHEX
ld hl, sErr
call JUMP_PRINTSTR
ret
sCmd0:
.db "Sending CMD0", 0xa, 0xd, 0
sCmd8:
.db "Sending CMD8", 0xa, 0xd, 0
sCmd41:
.db "Sending CMD41", 0xa, 0xd, 0
sCmd:
.db "CMD", 0xa, 0xd, 0
sGettingData:
.db "Data", 0xa, 0xd, 0
sOk:
.db "Ok", 0xa, 0xd, 0
sErr:
.db "Err", 0xa, 0xd, 0
sDest:
.fill 0x10