Remove spi_word and replace with spi_deselect
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().
This commit is contained in:
2
sw/spi.h
2
sw/spi.h
@@ -4,6 +4,6 @@
|
||||
#include <stdint.h>
|
||||
|
||||
uint8_t spi_byte(uint8_t);
|
||||
uint16_t spi_word(uint16_t);
|
||||
void spi_deselect(void);
|
||||
|
||||
#endif
|
||||
59
sw/spi.s
59
sw/spi.s
@@ -1,7 +1,7 @@
|
||||
.include "io.inc65"
|
||||
|
||||
.export _spi_byte
|
||||
.export _spi_word
|
||||
.export _spi_deselect
|
||||
|
||||
.importzp sp, sreg, regsave, regbank
|
||||
.importzp tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
|
||||
@@ -47,61 +47,16 @@ _spi_byte:
|
||||
lsr ; Select next bit
|
||||
tax
|
||||
bne @loop ; Stop when mask is 0
|
||||
lda #SPI_SSn ; Raise Slave Select
|
||||
sta BB_SPI_BASE
|
||||
tya ; Get read value from y
|
||||
ply
|
||||
plx
|
||||
rts ; Return
|
||||
|
||||
|
||||
; Read and write 16 bits from the SPI device
|
||||
; @in AX The word to write
|
||||
; @out AX The read word
|
||||
|
||||
_spi_word:
|
||||
phy
|
||||
ldy #$00
|
||||
sta tmp1 ; Save value into tmp1
|
||||
stx tmp2
|
||||
lda #$02
|
||||
sta tmp4
|
||||
|
||||
@byte: 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
|
||||
tya
|
||||
asl
|
||||
inc
|
||||
bra @3
|
||||
@2: tya ; Add current value
|
||||
asl
|
||||
@3: tay ; Move read value back to y
|
||||
txa
|
||||
lsr ; Select next bit
|
||||
tax
|
||||
bne @loop ; Stop when mask is 0
|
||||
|
||||
lda tmp2 ; Switch to second byte
|
||||
sta tmp1
|
||||
sty tmp3 ; Store read data in tmp3
|
||||
dec tmp4
|
||||
bne @byte
|
||||
|
||||
; 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
|
||||
tya ; Get read value from y
|
||||
ldx tmp3
|
||||
ply
|
||||
rts ; Return
|
||||
pla
|
||||
rts
|
||||
@@ -23,13 +23,7 @@ int main(void)
|
||||
}
|
||||
printf("Done! %x\n\n", retval);
|
||||
|
||||
printf("Starting spi_word test...\n");
|
||||
retval = spi_word(0xa5a5);
|
||||
if (retval != 0) {
|
||||
printf("Expected 0 return value from spi_word.\n");
|
||||
printf("Got: %x\n\n", retval);
|
||||
return 1;
|
||||
}
|
||||
printf("Done! %x\n", retval);
|
||||
printf("Starting spi_deselect test...\n");
|
||||
spi_deselect();
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user