2023-12-08 22:56:35 +01:00
|
|
|
.include "spi.h65"
|
2023-10-26 19:51:20 +02:00
|
|
|
|
2023-12-23 14:17:49 +01:00
|
|
|
Export spi_p, begin_read, irq_read_byte, begin_write, irq_write_byte, end, recv_bytes, sent_bytes, status, spi_irq
|
2023-10-30 22:14:33 +01:00
|
|
|
|
2023-12-08 00:13:10 +01:00
|
|
|
.zeropage
|
2023-12-23 14:17:49 +01:00
|
|
|
buffer_start: .res 2
|
2023-11-11 12:13:47 +01:00
|
|
|
.bss
|
2023-12-23 14:17:49 +01:00
|
|
|
recv_bytes:
|
|
|
|
sent_bytes: .res 2
|
|
|
|
status: .res 1
|
|
|
|
buffer_size: .res 2
|
|
|
|
irq_handler: .res 2
|
|
|
|
|
2023-12-08 00:13:10 +01:00
|
|
|
|
2023-12-08 22:56:35 +01:00
|
|
|
SPI_IO := spi_p::SPI_IO
|
2023-12-08 00:13:10 +01:00
|
|
|
|
2023-12-08 22:56:35 +01:00
|
|
|
; spi-transferred code will be placed here
|
2023-12-08 00:13:10 +01:00
|
|
|
; SPI_P = as peripheral
|
2023-11-11 12:13:47 +01:00
|
|
|
.code
|
2023-12-23 14:17:49 +01:00
|
|
|
; .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
|
|
|
|
;; The transfer must be: <SIZE, >SIZE, data (SIZE bytes)
|
|
|
|
;;
|
|
|
|
;; - initialize variables
|
|
|
|
;; - configure shift register to shift in under external clock
|
|
|
|
;; - enable shift register interrupts
|
|
|
|
;; @param ARG0-1: Start address of the buffer
|
|
|
|
;; @param ARG2-3: Size of the buffer
|
|
|
|
;; @todo: Use irq register handler
|
|
|
|
;; @note: The buffer must be large enough for the received data.
|
|
|
|
;;********************************************************************************
|
|
|
|
.proc begin_read
|
|
|
|
stz recv_bytes
|
|
|
|
stz recv_bytes+1
|
|
|
|
lda #%spi_p::STATUS::XFER_SIZEL
|
|
|
|
sta status
|
|
|
|
|
2023-12-08 00:13:10 +01:00
|
|
|
; store address in zp
|
2023-12-23 14:17:49 +01:00
|
|
|
lda #<ARG0
|
|
|
|
sta buffer_start
|
|
|
|
lda #>ARG1
|
|
|
|
sta buffer_start+1
|
|
|
|
|
|
|
|
; load irq handler
|
|
|
|
lda #<irq_read_byte
|
|
|
|
sta irq_handler
|
|
|
|
lda #>irq_read_byte
|
|
|
|
sta irq_handler+1
|
2023-10-30 22:14:33 +01:00
|
|
|
|
2023-12-23 14:17:49 +01:00
|
|
|
; temporarily store the low byte in buffer, will be checked later
|
|
|
|
lda #<ARG2
|
|
|
|
sta (buffer_start)
|
|
|
|
lda #>ARG3
|
|
|
|
sta buffer_size+1
|
|
|
|
; set Shift register to shift in under external clock on CB1
|
|
|
|
MaskedWrite SPI_IO+IO::ACR, #IO::ACR::SR_SIN_PHIE, #IO::ACR_MASK::SR
|
2023-10-30 22:14:33 +01:00
|
|
|
; enable SR interrupts
|
2023-11-01 13:13:23 +01:00
|
|
|
lda #(IO::IRQ::IRQ | IO::IRQ::SR)
|
|
|
|
sta SPI_IO + IO::IER
|
2023-12-08 00:13:10 +01:00
|
|
|
; load SR to reset
|
|
|
|
lda SPI_IO + IO::SR
|
2023-10-30 22:14:33 +01:00
|
|
|
rts
|
|
|
|
.endproc
|
|
|
|
|
2023-12-08 22:56:35 +01:00
|
|
|
|
2023-12-23 14:17:49 +01:00
|
|
|
;;********************************************************************************
|
|
|
|
;; @function Stop reading/writing from/to SPI
|
|
|
|
;; @details
|
|
|
|
;; Disables shift register interrupts
|
|
|
|
;; @modifies A
|
|
|
|
;;********************************************************************************
|
2023-12-08 00:13:10 +01:00
|
|
|
.proc end
|
|
|
|
; disable SR interrupts
|
|
|
|
lda #IO::IRQ::SR
|
|
|
|
sta SPI_IO + IO::IER
|
|
|
|
rts
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
|
2023-12-23 14:17:49 +01:00
|
|
|
;;********************************************************************************
|
|
|
|
;; @function Read a byte from SPI
|
|
|
|
;; @details
|
|
|
|
;; Reads a byte from the shift register and stores it in the SPI code buffer
|
|
|
|
;; The first two bytes must be the size of the incumong data (LE).
|
|
|
|
;; The spi_p:status is updated according to the current stage of the transfer.
|
|
|
|
;; If the buffer size given in begin_read is too small too fit the incoming data,
|
|
|
|
;; the status is set to ERROR and spi (shift register) interrupts are disabled before
|
|
|
|
;; the first real byte is read.
|
|
|
|
;; @modifies A,Y
|
|
|
|
;;********************************************************************************
|
|
|
|
.proc irq_read_byte
|
2023-11-01 13:13:23 +01:00
|
|
|
lda SPI_IO + IO::SR
|
2023-12-23 14:17:49 +01:00
|
|
|
bit status
|
|
|
|
bmi @read_size1
|
|
|
|
bvs @read_size2
|
|
|
|
ldy recv_bytes
|
|
|
|
sta (buffer_start),y
|
|
|
|
inc recv_bytes
|
2023-12-08 00:13:10 +01:00
|
|
|
beq @new_page
|
|
|
|
rts
|
|
|
|
@new_page:
|
2023-12-23 14:17:49 +01:00
|
|
|
inc recv_bytes+1
|
|
|
|
inc buffer_start+1
|
|
|
|
rts
|
|
|
|
@read_size1:
|
|
|
|
sta buffer_size
|
|
|
|
lda #spi_p::STATUS::XFER_SIZEH
|
|
|
|
sta status
|
|
|
|
rts
|
|
|
|
@read_size2:
|
|
|
|
; todo: check if the buffer is large enough:
|
|
|
|
; low byte of buffer size in (buffer_start), high byte in buffer_size+1
|
|
|
|
; low byte of recv buffer size in buffer_size, high byte in A
|
|
|
|
cmp buffer_size+1
|
|
|
|
beq @hieq
|
|
|
|
sta buffer_size+1
|
|
|
|
bcs @enough ; buffer larger
|
|
|
|
bra @not_enough
|
|
|
|
@hieq: ; high bytes are equal, check lo
|
|
|
|
lda (buffer_start)
|
|
|
|
cmp buffer_size
|
|
|
|
bcs @enough
|
|
|
|
@not_enough:
|
|
|
|
lda #spi_p::STATUS::ERROR
|
|
|
|
sta status
|
|
|
|
; disable SR interrupts
|
|
|
|
lda #IO::IRQ::SR
|
|
|
|
sta SPI_IO + IO::IER
|
|
|
|
rts
|
|
|
|
@enough:
|
|
|
|
lda #spi_p::STATUS::XFER
|
|
|
|
sta status
|
|
|
|
rts
|
|
|
|
.endproc
|
|
|
|
|
|
|
|
|
|
|
|
;;********************************************************************************
|
|
|
|
;; @function Begin writing to spi
|
|
|
|
;; @details
|
|
|
|
;; - initialize variables
|
|
|
|
;; - configure shift register to shift in under external clock
|
|
|
|
;; - enable shift register interrupts
|
|
|
|
;; @param ARG0-1: Start address of the buffer
|
|
|
|
;; @param ARG2-3: Size of the buffer
|
|
|
|
;; @todo: Use irq register handler
|
|
|
|
;;********************************************************************************
|
|
|
|
.proc begin_write
|
|
|
|
stz recv_bytes
|
|
|
|
stz recv_bytes+1
|
|
|
|
; store address in zp
|
|
|
|
lda #<ARG0
|
|
|
|
sta buffer_start
|
|
|
|
lda #>ARG1
|
|
|
|
sta buffer_start+1
|
|
|
|
; store size
|
|
|
|
lda #<ARG2
|
|
|
|
sta buffer_size
|
|
|
|
lda #>ARG3
|
|
|
|
sta buffer_size+1
|
|
|
|
; load irq handler
|
|
|
|
lda #<irq_read_byte
|
|
|
|
sta irq_handler
|
|
|
|
lda #>irq_read_byte
|
|
|
|
sta irq_handler+1
|
|
|
|
; set Shift register to shift out under external clock on CB1
|
|
|
|
MaskedWrite SPI_IO+IO::ACR, #IO::ACR::SR_SOUT_PHIE, #IO::ACR_MASK::SR
|
|
|
|
; enable SR interrupts
|
|
|
|
lda #(IO::IRQ::IRQ | IO::IRQ::SR)
|
|
|
|
sta SPI_IO + IO::IER
|
|
|
|
; write the first byte
|
|
|
|
@write_size1:
|
|
|
|
lda buffer_size
|
|
|
|
sta SPI_IO+IO_SR
|
|
|
|
lda #spi_p::STATUS::XFER_SIZEH
|
|
|
|
sta status
|
|
|
|
rts
|
2023-10-30 22:14:33 +01:00
|
|
|
rts
|
|
|
|
.endproc
|
|
|
|
|
2023-12-23 14:17:49 +01:00
|
|
|
;;;********************************************************************************
|
|
|
|
;;; @function Write a byte
|
|
|
|
;;; @details
|
|
|
|
;;; Write a byte to the spi shift reister
|
|
|
|
;;; @modifies A,Y
|
|
|
|
;;;********************************************************************************
|
|
|
|
;.proc irq_write_byte
|
|
|
|
; ldy sent_bytes
|
|
|
|
; lda (buffer_start),y
|
|
|
|
; sta SPI_IO + IO::SR
|
|
|
|
; inc sent_bytes
|
|
|
|
; beq @new_page
|
|
|
|
; rts
|
|
|
|
;@new_page:
|
|
|
|
; inc sent_bytes+1
|
|
|
|
; inc buffer_start+1
|
|
|
|
; rts
|
|
|
|
;.endproc
|
|
|
|
|
|
|
|
|
|
|
|
;;********************************************************************************
|
|
|
|
;; @function Write a byte to SPI
|
|
|
|
;; @details
|
|
|
|
;; Write a byte from the buffer to the (spi) shift register.
|
|
|
|
;; The first two bytes are the size of the data (LE).
|
|
|
|
;; The spi_p:status is updated according to the current stage of the transfer.
|
|
|
|
;; @modifies A,Y
|
|
|
|
;;********************************************************************************
|
|
|
|
.proc irq_write_byte
|
|
|
|
bit status
|
|
|
|
bvs @write_size2 ; bit 6 set -> XFER_SIZEH
|
|
|
|
ldy sent_bytes
|
|
|
|
lda (buffer_start),y
|
|
|
|
sta SPI_IO + IO::SR
|
|
|
|
inc sent_bytes
|
|
|
|
beq @new_page
|
|
|
|
rts
|
|
|
|
@new_page:
|
|
|
|
inc sent_bytes+1
|
|
|
|
inc buffer_start+1
|
|
|
|
rts
|
|
|
|
@write_size2:
|
|
|
|
lda buffer_size+1
|
|
|
|
sta SPI_IO+IO_SR
|
|
|
|
lda #spi_p::STATUS::XFER
|
|
|
|
sta status
|
|
|
|
rts
|
2023-10-30 22:14:33 +01:00
|
|
|
.endproc
|
2023-12-23 14:17:49 +01:00
|
|
|
|
|
|
|
|
2023-10-30 22:14:33 +01:00
|
|
|
.endproc
|