From fff193ea0f020443cc31521665037b6059e5387c Mon Sep 17 00:00:00 2001 From: Byron Lathi Date: Thu, 10 Mar 2022 10:26:36 -0600 Subject: [PATCH] Add basic spi code Implements the bit bang spi protocol --- sw/io.inc65 | 1 + sw/spi.h | 8 ++++++++ sw/spi.s | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+) create mode 100644 sw/io.inc65 create mode 100644 sw/spi.h create mode 100644 sw/spi.s diff --git a/sw/io.inc65 b/sw/io.inc65 new file mode 100644 index 0000000..8f254ac --- /dev/null +++ b/sw/io.inc65 @@ -0,0 +1 @@ +BB_SPI_BASE = $7ff0 diff --git a/sw/spi.h b/sw/spi.h new file mode 100644 index 0000000..22ebed1 --- /dev/null +++ b/sw/spi.h @@ -0,0 +1,8 @@ +#ifndef _SPI_H +#define _SPI_H + +#include + +void spi_write_byte(uint8_t); + +#endif \ No newline at end of file diff --git a/sw/spi.s b/sw/spi.s new file mode 100644 index 0000000..eb5abb4 --- /dev/null +++ b/sw/spi.s @@ -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