Change spi_write_byte to spi_byte
the SPI module reads and writes at the same time. If you don't want to write a value, then write all zeros or whatever the device you are communicating with wants.
This commit is contained in:
2
sw/spi.h
2
sw/spi.h
@@ -3,6 +3,6 @@
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void spi_write_byte(uint8_t);
|
||||
uint8_t spi_byte(uint8_t);
|
||||
|
||||
#endif
|
||||
20
sw/spi.s
20
sw/spi.s
@@ -1,6 +1,6 @@
|
||||
.include "io.inc65"
|
||||
|
||||
.export _spi_write_byte
|
||||
.export _spi_byte
|
||||
|
||||
.importzp sp, sreg, regsave, regbank
|
||||
.importzp tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
|
||||
@@ -10,14 +10,17 @@
|
||||
SPI_SCLK = $01
|
||||
SPI_SSn = $02
|
||||
SPI_MOSI = $04
|
||||
SPI_MISO = $08
|
||||
|
||||
|
||||
; Write a single byte to the SPI device
|
||||
; @in A The byte to write
|
||||
; @out A The read byte
|
||||
|
||||
_spi_write_byte:
|
||||
_spi_byte:
|
||||
phx ; Save regs
|
||||
phy
|
||||
ldy #$00
|
||||
sta tmp1 ; Save value into tmp1
|
||||
lda #$80
|
||||
tax
|
||||
@@ -29,12 +32,23 @@ _spi_write_byte:
|
||||
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
|
||||
lda #SPI_SSn ; Raise Slave Select
|
||||
sta BB_SPI_BASE
|
||||
ply ; Restore regs
|
||||
tya ; Get read value from y
|
||||
ply
|
||||
plx
|
||||
rts ; Return
|
||||
|
||||
@@ -1,10 +1,26 @@
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include <spi.h>
|
||||
|
||||
char retval;
|
||||
|
||||
int main(void)
|
||||
{
|
||||
printf("Starting spi_write_byte test...\n");
|
||||
spi_write_byte(0xa5);
|
||||
printf("Done!\n");
|
||||
printf("Setting SPI location to 0x02\n");
|
||||
*(uint8_t*)0x7ff0 = 2;
|
||||
if (!(*(uint8_t*)0x7ff0 == 2)) {
|
||||
printf("Expected 0x02 at 0x7ff0\n");
|
||||
return 1;
|
||||
}
|
||||
printf("Done!\n\n");
|
||||
|
||||
printf("Starting spi_byte test...\n");
|
||||
retval = spi_byte(0xa5);
|
||||
if (retval != 0) {
|
||||
printf("Expected 0 return value from spi_byte\n");
|
||||
return 1;
|
||||
}
|
||||
printf("Done! %x\n", retval);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user