Add basic spi code

Implements the bit bang spi protocol
This commit is contained in:
Byron Lathi
2022-03-10 10:26:36 -06:00
parent 80d49b4f87
commit fff193ea0f
3 changed files with 58 additions and 0 deletions

1
sw/io.inc65 Normal file
View File

@@ -0,0 +1 @@
BB_SPI_BASE = $7ff0

8
sw/spi.h Normal file
View File

@@ -0,0 +1,8 @@
#ifndef _SPI_H
#define _SPI_H
#include <stdint.h>
void spi_write_byte(uint8_t);
#endif

49
sw/spi.s Normal file
View File

@@ -0,0 +1,49 @@
.include "io.inc65"
.export _spi_write_byte
.importzp sp, sreg, regsave, regbank
.importzp tmp1, tmp2, tmp3, tmp4, ptr1, ptr2, ptr3, ptr4
.code
; Write a single byte to the SPI device
; @in A The byte to write
_spi_write_byte:
phx ; Save regs
phy
sta tmp1 ; Save value into tmp1
lda #$80
tax
@loop: bit tmp1 ; Check if high bit set
beq @1
lda #$04 ; Bit not set.
bra @1
@1: lda #$00 ; Bit set
sta BB_SPI_BASE ; Write data
adc #$01
sta BB_SPI_BASE ; Write clock
txa
lsr ; Select net bit
bne @loop ; Stop when mask is 0
ply ; Restore regs
plx
rts ; Return
and #$01 ; Get first bit
asl
asl
sta BB_SPI_BASE ; write bit without clock
adc #$01
sta BB_SPI_BASE ; write bit with clock
tay
and #$022
ply
plx
rts