6502-OS/system/spi.s65

113 lines
3.3 KiB
Plaintext
Raw Normal View History

2023-12-08 22:56:35 +01:00
.include "spi.h65"
2023-10-26 19:51:20 +02:00
2023-12-08 22:56:35 +01:00
Export spi_p, begin, end, read, pages_written, bytes_written
2023-10-30 22:14:33 +01:00
2023-12-08 00:13:10 +01:00
.zeropage
2023-12-08 22:56:35 +01:00
code_page: .res 2 ; CODE_START + pages_written * 256
2023-11-11 12:13:47 +01:00
.bss
2023-12-08 00:13:10 +01:00
bytes_written: .res 1
pages_written: .res 1
2023-12-08 22:56:35 +01:00
SPI_IO := spi_p::SPI_IO
CODE_START := spi_p::CODE_START
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-11-09 12:10:00 +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
2023-10-30 22:14:33 +01:00
;********************************************************************************
2023-12-08 00:13:10 +01:00
; @function Begin listening for SPI transfers
; @details
; - initialize variables
; - configure shift register to shift in under external clock
; - enable shift register interrupts
2023-10-30 22:14:33 +01:00
;********************************************************************************
2023-12-08 00:13:10 +01:00
.proc begin
stz pages_written
stz bytes_written
; store address in zp
2023-12-08 22:56:35 +01:00
lda #<CODE_START
2023-12-08 00:13:10 +01:00
sta code_page
2023-12-08 22:56:35 +01:00
lda #>CODE_START
2023-12-08 00:13:10 +01:00
sta code_page + 1
2023-10-30 22:14:33 +01:00
; todo USE MASKS
; set Shift register to shift in under external clock on CB1
2023-11-01 13:13:23 +01:00
lda #IO::ACR::SR_SIN_PHIE
sta SPI_IO + IO::ACR
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-08 00:13:10 +01:00
;********************************************************************************
; @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
2023-11-01 13:13:23 +01:00
lda SPI_IO + IO::SR
2023-12-08 22:56:35 +01:00
sta CODE_START,x
2023-12-08 00:13:10 +01:00
inc bytes_written
beq @new_page
rts
@new_page:
inc pages_written
inc code_page + 1
2023-10-30 22:14:33 +01:00
rts
.endproc
2023-10-26 19:51:20 +02:00
2023-12-08 00:13:10 +01:00
;********************************************************************************
; @function Initialize the IO Adapter for SPI
; @param ARG0-1 Address of the SPI_Pins struct
;********************************************************************************
2023-10-30 22:14:33 +01:00
;********************************************************************************
; @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
2023-10-26 19:51:20 +02:00
;********************************************************************************
; @function Send bytes
; @param X Number of bytes to send
2023-10-30 22:14:33 +01:00
; @param ARG0-1 Address of the SPI_Pins struct
; @param ARG2-3 Address of the first byte
2023-10-26 19:51:20 +02:00
;********************************************************************************
2023-10-30 22:14:33 +01:00
.proc send_data
.endproc