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>
|
#include <stdint.h>
|
||||||
|
|
||||||
void spi_write_byte(uint8_t);
|
uint8_t spi_byte(uint8_t);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
22
sw/spi.s
22
sw/spi.s
@@ -1,6 +1,6 @@
|
|||||||
.include "io.inc65"
|
.include "io.inc65"
|
||||||
|
|
||||||
.export _spi_write_byte
|
.export _spi_byte
|
||||||
|
|
||||||
.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
|
||||||
@@ -10,31 +10,45 @@
|
|||||||
SPI_SCLK = $01
|
SPI_SCLK = $01
|
||||||
SPI_SSn = $02
|
SPI_SSn = $02
|
||||||
SPI_MOSI = $04
|
SPI_MOSI = $04
|
||||||
|
SPI_MISO = $08
|
||||||
|
|
||||||
|
|
||||||
; Write a single byte to the SPI device
|
; Write a single byte to the SPI device
|
||||||
; @in A The byte to write
|
; @in A The byte to write
|
||||||
|
; @out A The read byte
|
||||||
|
|
||||||
_spi_write_byte:
|
_spi_byte:
|
||||||
phx ; Save regs
|
phx ; Save regs
|
||||||
phy
|
phy
|
||||||
|
ldy #$00
|
||||||
sta tmp1 ; Save value into tmp1
|
sta tmp1 ; Save value into tmp1
|
||||||
lda #$80
|
lda #$80
|
||||||
tax
|
tax
|
||||||
@loop: bit tmp1 ; Check if high bit set
|
@loop: bit tmp1 ; Check if high bit set
|
||||||
beq @1
|
beq @1
|
||||||
lda #SPI_MOSI ; Bit not set.
|
lda #SPI_MOSI ; Bit not set.
|
||||||
bra @1
|
bra @1
|
||||||
@1: lda #$00 ; Bit set
|
@1: lda #$00 ; Bit set
|
||||||
sta BB_SPI_BASE ; Write data
|
sta BB_SPI_BASE ; Write data
|
||||||
adc #SPI_SCLK
|
adc #SPI_SCLK
|
||||||
sta BB_SPI_BASE ; Write clock
|
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
|
txa
|
||||||
lsr ; Select next bit
|
lsr ; Select next bit
|
||||||
tax
|
tax
|
||||||
bne @loop ; Stop when mask is 0
|
bne @loop ; Stop when mask is 0
|
||||||
lda #SPI_SSn ; Raise Slave Select
|
lda #SPI_SSn ; Raise Slave Select
|
||||||
sta BB_SPI_BASE
|
sta BB_SPI_BASE
|
||||||
ply ; Restore regs
|
tya ; Get read value from y
|
||||||
|
ply
|
||||||
plx
|
plx
|
||||||
rts ; Return
|
rts ; Return
|
||||||
|
|||||||
@@ -1,10 +1,26 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
#include <spi.h>
|
#include <spi.h>
|
||||||
|
|
||||||
|
char retval;
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
printf("Starting spi_write_byte test...\n");
|
printf("Setting SPI location to 0x02\n");
|
||||||
spi_write_byte(0xa5);
|
*(uint8_t*)0x7ff0 = 2;
|
||||||
printf("Done!\n");
|
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;
|
return 0;
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user