From 2bfae04f50383d2d0e578cc40d8aace149b815e6 Mon Sep 17 00:00:00 2001 From: "matthias@arch" Date: Sun, 31 Dec 2023 01:58:47 +0100 Subject: [PATCH] use additional buffer size var --- programs/spi-menu.s65 | 41 ++++++++++++++++------ system/spi.h65 | 9 ++--- system/spi.s65 | 80 +++++++++++++++++++++++++------------------ 3 files changed, 83 insertions(+), 47 deletions(-) diff --git a/programs/spi-menu.s65 b/programs/spi-menu.s65 index f036ee4..9641a6d 100644 --- a/programs/spi-menu.s65 +++ b/programs/spi-menu.s65 @@ -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 #>> %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---" diff --git a/system/spi.h65 b/system/spi.h65 index 26fa136..f1ec84b 100644 --- a/system/spi.h65 +++ b/system/spi.h65 @@ -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 diff --git a/system/spi.s65 b/system/spi.s65 index 652ddaa..7895d24 100644 --- a/system/spi.s65 +++ b/system/spi.s65 @@ -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 #ARG1 - sta buffer_start+1 + lda ARG0 + sta buffer_ptr + lda ARG1 + sta buffer_ptr+1 ; load irq handler 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 #ARG1 - sta buffer_start+1 + sta buffer_ptr+1 ; store size lda # 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 -