Add spi_word()
This commit is contained in:
1
sw/spi.h
1
sw/spi.h
@@ -4,5 +4,6 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
uint8_t spi_byte(uint8_t);
|
uint8_t spi_byte(uint8_t);
|
||||||
|
uint16_t spi_word(uint16_t);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
55
sw/spi.s
55
sw/spi.s
@@ -1,6 +1,7 @@
|
|||||||
.include "io.inc65"
|
.include "io.inc65"
|
||||||
|
|
||||||
.export _spi_byte
|
.export _spi_byte
|
||||||
|
.export _spi_word
|
||||||
|
|
||||||
.importzp sp, sreg, regsave, regbank
|
.importzp sp, sreg, regsave, regbank
|
||||||
.importzp tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
|
.importzp tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
|
||||||
@@ -13,7 +14,7 @@ SPI_MOSI = $04
|
|||||||
SPI_MISO = $08
|
SPI_MISO = $08
|
||||||
|
|
||||||
|
|
||||||
; Write a single byte to the SPI device
|
; Read and write a single byte from the SPI device
|
||||||
; @in A The byte to write
|
; @in A The byte to write
|
||||||
; @out A The read byte
|
; @out A The read byte
|
||||||
|
|
||||||
@@ -52,3 +53,55 @@ _spi_byte:
|
|||||||
ply
|
ply
|
||||||
plx
|
plx
|
||||||
rts ; Return
|
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
|
||||||
|
|
||||||
|
lda #SPI_SSn ; Raise Slave Select
|
||||||
|
sta BB_SPI_BASE
|
||||||
|
tya ; Get read value from y
|
||||||
|
ldx tmp3
|
||||||
|
ply
|
||||||
|
rts ; Return
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
#include <spi.h>
|
#include <spi.h>
|
||||||
|
|
||||||
char retval;
|
uint16_t retval;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
@@ -21,6 +21,15 @@ int main(void)
|
|||||||
printf("Expected 0 return value from spi_byte\n");
|
printf("Expected 0 return value from spi_byte\n");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
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("Done! %x\n", retval);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user