6502-OS/system/spi.s65

113 lines
3.3 KiB
Plaintext

.include "spi.h65"
Export spi_p, begin, end, read, pages_written, bytes_written
.zeropage
code_page: .res 2 ; CODE_START + pages_written * 256
.bss
bytes_written: .res 1
pages_written: .res 1
SPI_IO := spi_p::SPI_IO
CODE_START := spi_p::CODE_START
; spi-transferred code will be placed here
; SPI_P = as peripheral
.code
.struct SPI_P_Pins
; VIA addresses
DDR_a .word ; address of the data direction register
R_a .word ; address of the register
; pin mask
SCLK_p .byte ; Serial Clock
POCI_p .byte ; Peripheral Out / Controller In
PICO_p .byte ; Peripheral In / Controller Out
CSB_p .byte ; Chip Select
; settings
CPOL .byte ; Clock Polarity
CPHA .byte ; Clock Phase
.endstruct
;********************************************************************************
; @function Begin listening for SPI transfers
; @details
; - initialize variables
; - configure shift register to shift in under external clock
; - enable shift register interrupts
;********************************************************************************
.proc begin
stz pages_written
stz bytes_written
; store address in zp
lda #<CODE_START
sta code_page
lda #>CODE_START
sta code_page + 1
; todo USE MASKS
; set Shift register to shift in under external clock on CB1
lda #IO::ACR::SR_SIN_PHIE
sta SPI_IO + IO::ACR
; enable SR interrupts
lda #(IO::IRQ::IRQ | IO::IRQ::SR)
sta SPI_IO + IO::IER
; load SR to reset
lda SPI_IO + IO::SR
rts
.endproc
;********************************************************************************
; @function Stop listening for SPI transfers
; @details
; Disables shift register interrupts
;********************************************************************************
.proc end
; disable SR interrupts
lda #IO::IRQ::SR
sta SPI_IO + IO::IER
rts
.endproc
;********************************************************************************
; @function Read a byte from SPI
; @details
; Reads a byte from the shift register and stores it in the SPI code buffer
;********************************************************************************
.proc read
ldx bytes_written
lda SPI_IO + IO::SR
sta CODE_START,x
inc bytes_written
beq @new_page
rts
@new_page:
inc pages_written
inc code_page + 1
rts
.endproc
;********************************************************************************
; @function Initialize the IO Adapter for SPI
; @param ARG0-1 Address of the SPI_Pins struct
;********************************************************************************
;********************************************************************************
; @function Read bytes
; @param X Number of bytes to send
; @param ARG0-1 Address of the SPI_Pins struct
; @param ARG2-3 Address of the first byte
;********************************************************************************
.proc recv_data
.endproc
;********************************************************************************
; @function Send bytes
; @param X Number of bytes to send
; @param ARG0-1 Address of the SPI_Pins struct
; @param ARG2-3 Address of the first byte
;********************************************************************************
.proc send_data
.endproc