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

View File

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

View File

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