avr: make COM2 buffer functions macros.

This commit is contained in:
Martin Preuss
2024-10-20 18:40:24 +02:00
parent d7705590fe
commit 3cba4a1f7c
2 changed files with 191 additions and 84 deletions

View File

@@ -9,136 +9,167 @@
; ===========================================================================
;
; Macros for working with message buffers.
;
; Helpfull defines:
; - COM2_BUFFER_NUM number of buffers to provide
; - COM2_BUFFER_SIZE size of each buffer (CAVE: change COM2_M_BufferPosToX when changing value!)
;
; Variables:
; - buffersUsed: .byte 1 holds number of buffers in use
; - maxBuffersUsed: .byte 1 holds max number of buffers every in use at the same time
; - buffersWritePos: .byte 1 holds current write pos
; - buffersReadPos: .byte 1 holds current read pos
; - buffers: .byte COM2_BUFFER_SIZE*COM2_BUFFER_NUM
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; COM_BufferAlloc ; @macro COM2_M_BufferAlloc
; ;
; Allocate a transfer buffer. ; Allocate a message buffer.
; IN: ;
; - nothing ; COM2_M_BufferAlloc COM2_BUFFER_NUM, com2MaxBuffersUsed, com2RecvBuffersUsed, com2RecvBuffersWritePos
; OUT: ;
; - CFLAG: set if okay, clear otherwise ; @param %0 max number of buffers
; - X: pointer to allocated buffer in SRAM ; @param %1 pointer to byte containing max number of buffers ever used
; MODIFIED REGISTERS: r16, r17, r21 ; @param %2 pointer to byte containing number of buffers currently in use
; @param %3 pointer to byte containing the write pointer
; @return CFLAG set if okay, clear otherwise
; @return X pos to allocated buffer
; @clobbers R16, R17, R21
.macro COM2_M_BufferAlloc
COM2_BufferAlloc: in r21, SREG ; save global interrupt enable bit from SREG
in r21, SREG ; save global interrupt enable bit from SREG
cli cli
lds r17, com2RecvBuffersUsed lds r17, @2
cpi r17, COM2_BUFFER_NUM cpi r17, @0
brcc COM2_AllocBuffer_error ; no buffer available brcc l_error ; no buffer available
inc r17 ; increment number of buffers used inc r17 ; increment number of buffers used
sts com2RecvBuffersUsed, r17 ; store new value sts @2, r17 ; store new value
lds r16, com2MaxBuffersUsed ; calc max buffers used lds r16, @1 ; calc max buffers used
cp r16, r17 cp r16, r17
brcc COM2_AllocBuffer_l0 brcc l0
sts com2MaxBuffersUsed, r17 sts @1, r17
COM2_AllocBuffer_l0: l0:
lds r16, com2RecvBuffersWritePos ; get current write pos lds r16, @3 ; get current write pos
mov r17, r16 ; increment for next call mov r17, r16 ; increment for next call
inc r17 inc r17
cpi r17, COM2_BUFFER_NUM ; CF set if COM_BUFFER_NUM > R17 cpi r17, @0 ; CF set if COM_BUFFER_NUM > R17
brcs COM2_AllocBuffer_l1 brcs l1
clr r17 ; wraparound clr r17 ; wraparound
COM2_AllocBuffer_l1: l1:
sts com2RecvBuffersWritePos, r17 ; store new writepos for next caller sts @3, r17 ; store new writepos for next caller
rcall COM2_BufferPosToX ; (R16, R17) rcall COM2_BufferPosToX ; (R16, R17)
out SREG, r21 ; restore global interrupt enable bit in SREG out SREG, r21 ; restore global interrupt enable bit in SREG
sec sec
ret rjmp l_end
COM2_AllocBuffer_error: l_error:
out SREG, r21 out SREG, r21
clc clc
ret l_end:
.endmacro
; @end
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; COM2_BufferDeallocBack ; @macro COM2_M_BufferDeallocBack
; ;
; Release a transfer buffer at the end of the list by decreasing the write pos. ; Release a transfer buffer at the end of the list by decreasing the write pos.
; This releases the last allocated buffer! ; This releases the last allocated buffer!
; ;
; IN: ; COM2_M_BufferDeallocBack COM2_BUFFER_NUM, com2RecvBuffersUsed, com2RecvBuffersWritePos
; - nothing ;
; OUT: ; @param %0 maximum number of buffers
; - CFLAG: set if okay, clear otherwise ; @param %1 pointer to a byte containing number of buffers used
; MODIFIED REGISTERS: r16, r17, r21 ; @param %2 pointer to a byte containing current write pos
; @return CFLAG set if okay, clear otherwise
; @clobbers r16, r17, r21
COM2_BufferDeallocBack: .macro COM2_M_BufferDeallocBack
in r21, SREG ; save global interrupt enable bit from SREG in r21, SREG ; save global interrupt enable bit from SREG
cli cli
lds r17, com2RecvBuffersUsed lds r17, @1
tst r17 tst r17
breq COM2_BufferDeallocBack_error ; no buffer allocated, nothing to release breq l_error ; no buffer allocated, nothing to release
dec r17 dec r17
sts com2RecvBuffersUsed, r17 ; store new value sts @1, r17 ; store new value
lds r17, com2RecvBuffersWritePos lds r17, @2
tst r17 ; 0? tst r17 ; 0?
brne COM2_BufferDeallocBack_l1 ; nope go directly decrement r17 brne l1 ; nope go directly decrement r17
ldi r17, COM2_BUFFER_NUM ; wrap-around ldi r17, @0 ; wrap-around
COM2_BufferDeallocBack_l1: l1:
dec r17 dec r17
sts com2RecvBuffersWritePos, r17 sts @2, r17
out SREG, r21 out SREG, r21
sec sec
ret rjmp l_end
COM2_BufferDeallocBack_error: l_error:
out SREG, r21 out SREG, r21
clc clc
ret l_end:
.endmacro
; @end
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; COM2_BufferDeallocFront ; @macro COM2_M_BufferDeallocFront
; ;
; Release a transfer buffer by increasing the read pos. ; Release a transfer buffer by increasing the read pos.
; ;
; IN: ; COM2_M_BufferDeallocFront COM2_BUFFER_NUM com2RecvBuffersUsed com2RecvBuffersReadPos
; - nothing ;
; OUT: ; @param %0 maximum number of buffers
; - CFLAG: set if okay, clear otherwise ; @param %1 pointer to a byte containing number of buffers used
; MODIFIED REGISTERS: r16, r17, r21 ; @param %2 pointer to a byte containing current read pos
; @return CFLAG set if okay, clear otherwise
; @clobbers r16, r17, r21
COM2_BufferDeallocFront: .macro COM2_M_BufferDeallocFront
in r21, SREG ; save global interrupt enable bit from SREG in r21, SREG ; save global interrupt enable bit from SREG
cli cli
lds r17, com2RecvBuffersUsed lds r17, @1
tst r17 tst r17
breq COM2_BufferDeallocFront_error ; no buffer allocated, nothing to release breq l_error ; no buffer allocated, nothing to release
dec r17 dec r17
sts com2RecvBuffersUsed, r17 ; store new value sts @1, r17 ; store new value
lds r17, com2RecvBuffersReadPos lds r17, @2
inc r17 inc r17
cpi r17, COM2_BUFFER_NUM cpi r17, @0
brcs COM2_BufferDeallocFront_l1 brcs l1
clr r17 ; wrap-around clr r17 ; wrap-around
COM2_BufferDeallocFront_l1: l1:
sts com2RecvBuffersReadPos, r17 sts @2, r17
out SREG, r21 out SREG, r21
sec sec
ret rjmp l_end
COM2_BufferDeallocFront_error: l_error:
out SREG, r21 out SREG, r21
clc clc
ret l_end:
.endmacro
; @end
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; COM2_BufferPosToX ; @macro COM2_M_BufferPosToX
; ;
; Get a pointer to the SRAM position of the given buffer. ; Get a pointer to the SRAM position of the given buffer.
; CAVE: Code must correspond to COM2_BUFFER_SIZE!! ; CAVE: Code must correspond to COM2_BUFFER_SIZE!!
; IN: ;
; - R16: buffer number (starting with 0) ; COM2_M_BufferPosToX com2RecvBuffers
; OUT: ;
; - X: pointer to buffer in SRAM ; @param %0 pointer to a 2 byte var containing pointer to buffers
; MODIFIED REGISTERS: R16, R17 ; @param R16 buffer number (starting with 0)
; @return X pointer to buffer in SRAM
; @clobbers R16, R17
COM2_BufferPosToX: .macro COM2_M_BufferPosToX
; calculate offset ; calculate offset
clr r17 clr r17
mov xl, r16 mov xl, r16
@@ -160,11 +191,12 @@ COM2_BufferPosToX:
rol xh ; *24 rol xh ; *24
; add base position of buffers ; add base position of buffers
ldi r16, LOW(com2RecvBuffers) ldi r16, LOW(@0)
ldi r17, HIGH(com2RecvBuffers) ldi r17, HIGH(@0)
add xl, r16 add xl, r16
adc xh, r17 adc xh, r17
ret .endmacro
; @end

View File

@@ -8,6 +8,9 @@
; *************************************************************************** ; ***************************************************************************
.include "modules/com2/buffer.asm"
; *************************************************************************** ; ***************************************************************************
; data ; data
@@ -19,6 +22,7 @@ com2DataBegin:
com2Address: .byte 1 com2Address: .byte 1
com2Interrupts: .byte 2 com2Interrupts: .byte 2
com2LastMsgId: .byte 2
com2RecvStatsBegin: ; 12 bytes com2RecvStatsBegin: ; 12 bytes
com2StatsPacketsIn: .byte 2 com2StatsPacketsIn: .byte 2
@@ -26,7 +30,6 @@ com2RecvStatsBegin: ; 12 bytes
com2StatsIoError: .byte 2 com2StatsIoError: .byte 2
com2StatsNoBufferError: .byte 2 com2StatsNoBufferError: .byte 2
com2StatsHandled: .byte 2 com2StatsHandled: .byte 2
com2StatsNotForMe: .byte 2
com2StatsMissed: .byte 2 ; currently not used com2StatsMissed: .byte 2 ; currently not used
com2RecvStatsEnd: com2RecvStatsEnd:
@@ -36,6 +39,7 @@ com2SendStatsBegin: ; 6 bytes
com2StatsBusyError: .byte 2 com2StatsBusyError: .byte 2
com2SendStatsEnd: com2SendStatsEnd:
com2StatsNotForMe: .byte 2
com2StatsIgnored: .byte 2 com2StatsIgnored: .byte 2
com2RecvBuffersUsed: .byte 1 com2RecvBuffersUsed: .byte 1
@@ -399,6 +403,78 @@ COM2_SendPacket_okay:
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Buffer Management
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ---------------------------------------------------------------------------
; @routine COM2_BufferAlloc
;
; Allocate a transfer buffer.
;
; @return CFLAG set if okay, clear otherwise
; @return X pointer to allocated buffer in SRAM
; @clobbers r16, r17, r21
COM2_BufferAlloc:
COM2_M_BufferAlloc COM2_BUFFER_NUM, com2MaxBuffersUsed, com2RecvBuffersUsed, com2RecvBuffersWritePos
ret
; @end
; ---------------------------------------------------------------------------
; @routine COM2_BufferDeallocBack
;
; Release a transfer buffer at the end of the list by decreasing the write pos.
; This releases the last allocated buffer!
;
; @return CFLAG set if okay, clear otherwise
; @clobbers r16, r17, r21
COM2_BufferDeallocBack:
COM2_M_BufferDeallocBack COM2_BUFFER_NUM, com2RecvBuffersUsed, com2RecvBuffersWritePos
ret
; @end
; ---------------------------------------------------------------------------
; @routine COM2_BufferDeallocFront
;
; Release a transfer buffer by increasing the read pos.
;
; @return CFLAG set if okay, clear otherwise
; @clobbers r16, r17, r21
COM2_BufferDeallocFront:
COM2_M_BufferDeallocFront COM2_BUFFER_NUM, com2RecvBuffersUsed, com2RecvBuffersReadPos
ret
; @end
; ---------------------------------------------------------------------------
; @routine COM2_BufferPosToX
;
; Get a pointer to the SRAM position of the given buffer.
; CAVE: Code must correspond to COM2_BUFFER_SIZE!!
;
; @param R16 buffer number (starting with 0)
; @return X pointer to buffer in SRAM
; @clobbers R16, R17
COM2_BufferPosToX:
COM2_M_BufferPosToX com2RecvBuffers
ret
; @end
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ISR ; ISR
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
@@ -451,7 +527,6 @@ com2IsrPcint0_end:
.include "modules/com2/packets.asm" .include "modules/com2/packets.asm"
.include "modules/com2/lowlevel.asm" .include "modules/com2/lowlevel.asm"
.include "modules/com2/buffer.asm"
.include "modules/com2/crc.asm" .include "modules/com2/crc.asm"