Since the goal is to have the MAX3421E working, which uses multiple 8 bit transfers, we should support multiple 8 bit transfers instead of trying to use 16 bit transfers. When using spi_byte(), the device is automatically selected. After you have made as many transfers as you want, you must deselect the device with spi_deselect().
62 lines
1.5 KiB
ArmAsm
62 lines
1.5 KiB
ArmAsm
.include "io.inc65"
|
|
|
|
.export _spi_byte
|
|
.export _spi_deselect
|
|
|
|
.importzp sp, sreg, regsave, regbank
|
|
.importzp tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
|
|
|
|
.code
|
|
|
|
SPI_SCLK = $01
|
|
SPI_SSn = $02
|
|
SPI_MOSI = $04
|
|
SPI_MISO = $08
|
|
|
|
|
|
; Read and write a single byte from the SPI device
|
|
; @in A The byte to write
|
|
; @out A The read byte
|
|
|
|
_spi_byte:
|
|
phx ; Save regs
|
|
phy
|
|
ldy #$00
|
|
sta tmp1 ; Save value into tmp1
|
|
lda #$80
|
|
tax
|
|
@loop: bit tmp1 ; Check if high bit set
|
|
beq @1
|
|
lda #SPI_MOSI ; Bit not set.
|
|
bra @1
|
|
@1: lda #$00 ; Bit set
|
|
sta BB_SPI_BASE ; Write data
|
|
adc #SPI_SCLK
|
|
sta BB_SPI_BASE ; Write clock
|
|
stz tmp2
|
|
lda BB_SPI_BASE ; Check MISO value
|
|
and #SPI_MISO
|
|
beq @2
|
|
inc tmp2
|
|
@2: clc ; Shift previous value left
|
|
tya ; Add current value
|
|
asl
|
|
adc tmp2
|
|
tay ; Move read value back to y
|
|
txa
|
|
lsr ; Select next bit
|
|
tax
|
|
bne @loop ; Stop when mask is 0
|
|
tya ; Get read value from y
|
|
ply
|
|
plx
|
|
rts ; Return
|
|
|
|
; Deselect the spi device.
|
|
; spi device is automatically selected during read/write.
|
|
_spi_deselect:
|
|
pha
|
|
lda #SPI_SSn ; Raise Slave Select
|
|
sta BB_SPI_BASE
|
|
pla
|
|
rts |