use additional buffer size var

This commit is contained in:
matthias@arch 2023-12-31 01:58:47 +01:00
parent ff4e284f26
commit 2bfae04f50
3 changed files with 83 additions and 47 deletions

View File

@ -27,13 +27,27 @@ status_str: .res 17 ; 16 + null
beq @status_ERROR
cmp #spi_p::STATUS::DONE
beq @status_DONE
bra @status_XFER
cmp #spi_p::STATUS::XFER
beq @status_XFER
cmp #spi_p::STATUS::XFER_SIZEL
beq @status_XFER_SIZEL
cmp #spi_p::STATUS::XFER_SIZEH
beq @status_XFER_SIZEH
@status_UNKNOWN:
lda #'?'
bra @update_status_end
@status_ERROR:
lda #'E'
bra @update_status_end
@status_DONE:
lda #'O'
bra @update_status_end
@status_XFER_SIZEL:
lda #'1'
bra @update_status_end
@status_XFER_SIZEH:
lda #'2'
bra @update_status_end
@status_XFER:
lda #'X'
@update_status_end:
@ -42,9 +56,10 @@ status_str: .res 17 ; 16 + null
@print_status:
lda #lcd::LINE4
jsr lcd::set_position
Strf "%x%xb Status: %c",status_str,trans_bytes+1,trans_bytes,status_char
Strf "%x%x/%x%x|%x%x|%c",status_str,trans_bytes+1,trans_bytes,spi_p::recv_size+1,spi_p::recv_size,spi_p::buffer_size+1,spi_p::buffer_size,status_char
PrintNC status_str
@loop:
wai
; check if a byte has been transferred
@check_received:
lda spi_p::recv_bytes
@ -53,8 +68,8 @@ status_str: .res 17 ; 16 + null
@byte_received:
sta trans_bytes
lda spi_p::recv_bytes+1
lda trans_bytes+1
bra @print_status
sta trans_bytes+1
jmp @update_status
@read_keypad:
lda kp::_DEBUG_VAL
beq @loop
@ -70,7 +85,9 @@ status_str: .res 17 ; 16 + null
beq @spi_end
cmp #'C'
beq @spi_jump
bra @update_status ; any other key
cmp #'D'
beq @spi_jump2
jmp @update_status ; any other key
@spi_begin:
lda #<CODE_START
sta ARG0
@ -83,15 +100,20 @@ status_str: .res 17 ; 16 + null
jsr spi_p::begin_read
jmp @print_menu
@spi_end:
jsr spi_p::end
jsr spi_p::end_read
jmp @print_menu
@spi_jump:
jsr spi_p::end
jsr spi_p::end_read
jsr lcd::clear
Print START
Print "---START SPI---"
jmp CODE_START
@spi_jump2:
jsr spi_p::end_read
jsr lcd::clear
Printf " >>> %x%x >>> ", spi_p::buffer_ptr+1, spi_p::buffer_ptr
jmp (spi_p::buffer_ptr)
@return_home:
jsr spi_p::end
jsr spi_p::end_read
jmp home
.endproc
@ -101,6 +123,5 @@ MENU:
.byte "B> Stop Transfer"
.asciiz "C> Jump Home <*"
; .asciiz "0b0p Status: X"
FMT_STATUS: .asciiz "%x%xb Status:%x"
BEGIN: .asciiz "---BEGIN SPI---"
START: .asciiz "---START SPI---"

View File

@ -12,16 +12,17 @@ INCLUDE_SPI = 1
.scope spi_p
SPI_IO = IO2
Import spi_p, irq_handler, end, status
Import spi_p, begin_read, irq_read_byte, recv_bytes
Import spi_p, begin_write, irq_write_byte, sent_bytes
Import spi_p, init, irq_handler, status, buffer_size, recv_size
ImportZp spi_p, buffer_ptr
Import spi_p, begin_read, end_read, irq_read_byte, recv_bytes
Import spi_p, begin_write, end_write, irq_write_byte, sent_bytes
.enum STATUS
XFER_SIZEL = %10000000
XFER_SIZEH = %01000000
XFER = %00000001
DONE = %00000000
ERROR = %11111111
ERROR = %00100000
.endenum
.endscope

View File

@ -1,17 +1,20 @@
.include "spi.h65"
.include "system.h65"
Export spi_p, irq_handler, end, status
Export spi_p, begin_read, irq_read_byte, recv_bytes
Export spi_p, begin_write, irq_write_byte, sent_bytes
Export spi_p, init, irq_handler, status, buffer_size, recv_size
ExportZp spi_p, buffer_ptr
Export spi_p, begin_read, end_read, irq_read_byte, recv_bytes
Export spi_p, begin_write, end_write, irq_write_byte, sent_bytes
.zeropage
buffer_start: .res 2
buffer_ptr: .res 2
.bss
recv_bytes:
sent_bytes: .res 2
status: .res 1
buffer_size: .res 2
recv_size:
send_size: .res 2
irq_handler: .res 2
@ -27,6 +30,13 @@ SPI_IO := spi_p::SPI_IO
.proc init
lda #spi_p::STATUS::DONE
sta status
stz recv_bytes
stz recv_bytes+1
stz buffer_size
stz buffer_size+1
stz recv_size
stz recv_size+1
rts
.endproc
@ -46,14 +56,16 @@ SPI_IO := spi_p::SPI_IO
.proc begin_read
stz recv_bytes
stz recv_bytes+1
stz recv_size
stz recv_size+1
lda #spi_p::STATUS::XFER_SIZEL
sta status
; store address in zp
lda #<ARG0
sta buffer_start
lda #>ARG1
sta buffer_start+1
lda ARG0
sta buffer_ptr
lda ARG1
sta buffer_ptr+1
; load irq handler
lda #<irq_read_byte
@ -62,9 +74,9 @@ SPI_IO := spi_p::SPI_IO
sta irq_handler+1
; temporarily store the low byte in buffer, will be checked later
lda #<ARG2
sta (buffer_start)
lda #>ARG3
lda ARG2
sta buffer_size
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
@ -83,10 +95,13 @@ SPI_IO := spi_p::SPI_IO
;; Disables shift register interrupts
;; @modifies A
;;********************************************************************************
.proc end
end_write:
.proc end_read
; disable SR interrupts
lda #IO::IRQ::SR
sta SPI_IO + IO::IER
lda #spi_p::STATUS::DONE
sta status
rts
.endproc
@ -104,35 +119,36 @@ SPI_IO := spi_p::SPI_IO
;;********************************************************************************
.proc irq_read_byte
lda SPI_IO + IO::SR
; Printf "%x",
bit status
bmi @read_size1
bvs @read_size2
bmi @read_size1 ; bit7 set => XFER_SIZEL
bvs @read_size2 ; bit6 set => XFER_SIZEH
ldy recv_bytes
sta (buffer_start),y
sta (buffer_ptr),y
inc recv_bytes
beq @new_page
rts
@new_page:
@new_page: ; increment high bytes
inc recv_bytes+1
inc buffer_start+1
inc buffer_ptr+1
rts
@read_size1:
sta buffer_size
sta recv_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
sta recv_size+1
; check if the buffer is large enough
cmp buffer_size+1
beq @hieq
sta buffer_size+1
bcs @enough ; buffer larger
bcc @enough ; recv_size+1 < buffer_size+1
bra @not_enough
@hieq: ; high bytes are equal, check lo
lda (buffer_start)
cmp buffer_size
lda buffer_size
cmp recv_size
bcs @enough
@not_enough:
lda #spi_p::STATUS::ERROR
@ -163,9 +179,9 @@ SPI_IO := spi_p::SPI_IO
stz recv_bytes+1
; store address in zp
lda #<ARG0
sta buffer_start
sta buffer_ptr
lda #>ARG1
sta buffer_start+1
sta buffer_ptr+1
; store size
lda #<ARG2
sta buffer_size
@ -188,7 +204,6 @@ SPI_IO := spi_p::SPI_IO
lda #spi_p::STATUS::XFER_SIZEH
sta status
rts
rts
.endproc
;;;********************************************************************************
@ -199,14 +214,14 @@ SPI_IO := spi_p::SPI_IO
;;;********************************************************************************
;.proc irq_write_byte
; ldy sent_bytes
; lda (buffer_start),y
; lda (buffer_ptr),y
; sta SPI_IO + IO::SR
; inc sent_bytes
; beq @new_page
; rts
;@new_page:
; inc sent_bytes+1
; inc buffer_start+1
; inc buffer_ptr+1
; rts
;.endproc
@ -223,14 +238,14 @@ SPI_IO := spi_p::SPI_IO
bit status
bvs @write_size2 ; bit 6 set -> XFER_SIZEH
ldy sent_bytes
lda (buffer_start),y
lda (buffer_ptr),y
sta SPI_IO + IO::SR
inc sent_bytes
beq @new_page
rts
@new_page:
inc sent_bytes+1
inc buffer_start+1
inc buffer_ptr+1
rts
@write_size2:
lda buffer_size+1
@ -239,4 +254,3 @@ SPI_IO := spi_p::SPI_IO
sta status
rts
.endproc