From aca739338aa897568389c1f31c87c7e42d310578 Mon Sep 17 00:00:00 2001 From: Byron Lathi Date: Thu, 10 Mar 2022 14:43:49 -0600 Subject: [PATCH] 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(). --- sw/spi.h | 2 +- sw/spi.s | 59 ++++++-------------------------------------- sw/tests/test_main.c | 10 ++------ 3 files changed, 10 insertions(+), 61 deletions(-) diff --git a/sw/spi.h b/sw/spi.h index 254a75b..9afc44e 100644 --- a/sw/spi.h +++ b/sw/spi.h @@ -4,6 +4,6 @@ #include uint8_t spi_byte(uint8_t); -uint16_t spi_word(uint16_t); +void spi_deselect(void); #endif \ No newline at end of file diff --git a/sw/spi.s b/sw/spi.s index b4181ea..2d04470 100644 --- a/sw/spi.s +++ b/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 \ No newline at end of file + pla + rts \ No newline at end of file diff --git a/sw/tests/test_main.c b/sw/tests/test_main.c index ff292c6..c579ed3 100644 --- a/sw/tests/test_main.c +++ b/sw/tests/test_main.c @@ -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; } \ No newline at end of file