;----------------------------------------------------------------------------------------- ; Execute SPI command. Writes command and two data bytes stored in zero-page locations. ; Reads status and two data bytes and stores back in same locations. ; Set ZP_CMD_PKG before calling, A and X scrambled. ; Uses SPI channel 1 by default, or set channel bits in A and call SPICmd+2 ;----------------------------------------------------------------------------------------- ; SPI card commands (assumes slot 4) SPI_CS equ $C0C1 SPI_EXEC equ $C0C0 SPI_DATA equ $C0C2 SPI_RESET equ $C0C3 ; command and data bytes for spi_cmd ZP_CMD_PKG equ $EB ZP_CMD_STAT equ $EB ZP_DATAH equ $EC ZP_DATAL equ $ED SPICmd lda #$01 ; use SPI channel 0 (set bit 0) ldx #$00 sta SPI_CS ; begin SPI transaction .nextByte lda ZP_CMD_PKG,x ; load byte (one command byte, two data bytes) sta SPI_DATA ; store in SPI data latch sta SPI_EXEC ; execute SPI transfer .wait lda SPI_CS ; get transfer status bpl .wait ; wait until all 8 bits are sent lda SPI_DATA ; get reply byte from SPI data latch sta ZP_CMD_PKG,x ; store byte (one status byte, two data bytes) inx cpx #$03 bne .nextByte ; repeat for all three bytes sta SPI_RESET ; end SPI transaction rts