From 3cba4a1f7ca0b97507e0c5af72d6f1374a83b1ce Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Sun, 20 Oct 2024 18:40:24 +0200 Subject: [PATCH] avr: make COM2 buffer functions macros. --- avr/modules/com2/buffer.asm | 196 +++++++++++++++++++++--------------- avr/modules/com2/main.asm | 79 ++++++++++++++- 2 files changed, 191 insertions(+), 84 deletions(-) diff --git a/avr/modules/com2/buffer.asm b/avr/modules/com2/buffer.asm index 25b6a1d..2f93e33 100644 --- a/avr/modules/com2/buffer.asm +++ b/avr/modules/com2/buffer.asm @@ -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. -; IN: -; - nothing -; OUT: -; - CFLAG: set if okay, clear otherwise -; - X: pointer to allocated buffer in SRAM -; MODIFIED REGISTERS: r16, r17, r21 +; Allocate a message buffer. +; +; COM2_M_BufferAlloc COM2_BUFFER_NUM, com2MaxBuffersUsed, com2RecvBuffersUsed, com2RecvBuffersWritePos +; +; @param %0 max number of buffers +; @param %1 pointer to byte containing max number of buffers ever used +; @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 - -COM2_BufferAlloc: - in r21, SREG ; save global interrupt enable bit from SREG +.macro COM2_M_BufferAlloc + in r21, SREG ; save global interrupt enable bit from SREG cli - lds r17, com2RecvBuffersUsed - cpi r17, COM2_BUFFER_NUM - brcc COM2_AllocBuffer_error ; no buffer available - inc r17 ; increment number of buffers used - sts com2RecvBuffersUsed, r17 ; store new value - lds r16, com2MaxBuffersUsed ; calc max buffers used + lds r17, @2 + cpi r17, @0 + brcc l_error ; no buffer available + inc r17 ; increment number of buffers used + sts @2, r17 ; store new value + lds r16, @1 ; calc max buffers used cp r16, r17 - brcc COM2_AllocBuffer_l0 - sts com2MaxBuffersUsed, r17 -COM2_AllocBuffer_l0: - lds r16, com2RecvBuffersWritePos ; get current write pos - mov r17, r16 ; increment for next call + brcc l0 + sts @1, r17 +l0: + lds r16, @3 ; get current write pos + mov r17, r16 ; increment for next call inc r17 - cpi r17, COM2_BUFFER_NUM ; CF set if COM_BUFFER_NUM > R17 - brcs COM2_AllocBuffer_l1 - clr r17 ; wraparound -COM2_AllocBuffer_l1: - sts com2RecvBuffersWritePos, r17 ; store new writepos for next caller - rcall COM2_BufferPosToX ; (R16, R17) - out SREG, r21 ; restore global interrupt enable bit in SREG + cpi r17, @0 ; CF set if COM_BUFFER_NUM > R17 + brcs l1 + clr r17 ; wraparound +l1: + sts @3, r17 ; store new writepos for next caller + rcall COM2_BufferPosToX ; (R16, R17) + out SREG, r21 ; restore global interrupt enable bit in SREG sec - ret -COM2_AllocBuffer_error: + rjmp l_end +l_error: out SREG, r21 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. ; This releases the last allocated buffer! ; -; IN: -; - nothing -; OUT: -; - CFLAG: set if okay, clear otherwise -; MODIFIED REGISTERS: r16, r17, r21 +; COM2_M_BufferDeallocBack COM2_BUFFER_NUM, com2RecvBuffersUsed, com2RecvBuffersWritePos +; +; @param %0 maximum number of buffers +; @param %1 pointer to a byte containing number of buffers used +; @param %2 pointer to a byte containing current write pos +; @return CFLAG set if okay, clear otherwise +; @clobbers r16, r17, r21 -COM2_BufferDeallocBack: - in r21, SREG ; save global interrupt enable bit from SREG +.macro COM2_M_BufferDeallocBack + in r21, SREG ; save global interrupt enable bit from SREG cli - lds r17, com2RecvBuffersUsed + lds r17, @1 tst r17 - breq COM2_BufferDeallocBack_error ; no buffer allocated, nothing to release + breq l_error ; no buffer allocated, nothing to release dec r17 - sts com2RecvBuffersUsed, r17 ; store new value - lds r17, com2RecvBuffersWritePos - tst r17 ; 0? - brne COM2_BufferDeallocBack_l1 ; nope go directly decrement r17 - ldi r17, COM2_BUFFER_NUM ; wrap-around -COM2_BufferDeallocBack_l1: + sts @1, r17 ; store new value + lds r17, @2 + tst r17 ; 0? + brne l1 ; nope go directly decrement r17 + ldi r17, @0 ; wrap-around +l1: dec r17 - sts com2RecvBuffersWritePos, r17 + sts @2, r17 out SREG, r21 sec - ret -COM2_BufferDeallocBack_error: + rjmp l_end +l_error: out SREG, r21 clc - ret +l_end: +.endmacro +; @end ; --------------------------------------------------------------------------- -; COM2_BufferDeallocFront +; @macro COM2_M_BufferDeallocFront ; ; Release a transfer buffer by increasing the read pos. ; -; IN: -; - nothing -; OUT: -; - CFLAG: set if okay, clear otherwise -; MODIFIED REGISTERS: r16, r17, r21 +; COM2_M_BufferDeallocFront COM2_BUFFER_NUM com2RecvBuffersUsed com2RecvBuffersReadPos +; +; @param %0 maximum number of buffers +; @param %1 pointer to a byte containing number of buffers used +; @param %2 pointer to a byte containing current read pos +; @return CFLAG set if okay, clear otherwise +; @clobbers r16, r17, r21 -COM2_BufferDeallocFront: - in r21, SREG ; save global interrupt enable bit from SREG +.macro COM2_M_BufferDeallocFront + in r21, SREG ; save global interrupt enable bit from SREG cli - lds r17, com2RecvBuffersUsed + lds r17, @1 tst r17 - breq COM2_BufferDeallocFront_error ; no buffer allocated, nothing to release + breq l_error ; no buffer allocated, nothing to release dec r17 - sts com2RecvBuffersUsed, r17 ; store new value - lds r17, com2RecvBuffersReadPos + sts @1, r17 ; store new value + lds r17, @2 inc r17 - cpi r17, COM2_BUFFER_NUM - brcs COM2_BufferDeallocFront_l1 - clr r17 ; wrap-around -COM2_BufferDeallocFront_l1: - sts com2RecvBuffersReadPos, r17 + cpi r17, @0 + brcs l1 + clr r17 ; wrap-around +l1: + sts @2, r17 out SREG, r21 sec - ret -COM2_BufferDeallocFront_error: + rjmp l_end +l_error: out SREG, r21 clc - ret +l_end: +.endmacro +; @end ; --------------------------------------------------------------------------- -; COM2_BufferPosToX +; @macro COM2_M_BufferPosToX ; ; Get a pointer to the SRAM position of the given buffer. ; CAVE: Code must correspond to COM2_BUFFER_SIZE!! -; IN: -; - R16: buffer number (starting with 0) -; OUT: -; - X: pointer to buffer in SRAM -; MODIFIED REGISTERS: R16, R17 +; +; COM2_M_BufferPosToX com2RecvBuffers +; +; @param %0 pointer to a 2 byte var containing pointer to buffers +; @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 clr r17 mov xl, r16 @@ -160,11 +191,12 @@ COM2_BufferPosToX: rol xh ; *24 ; add base position of buffers - ldi r16, LOW(com2RecvBuffers) - ldi r17, HIGH(com2RecvBuffers) + ldi r16, LOW(@0) + ldi r17, HIGH(@0) add xl, r16 adc xh, r17 - ret +.endmacro +; @end diff --git a/avr/modules/com2/main.asm b/avr/modules/com2/main.asm index f722a45..1b32d31 100644 --- a/avr/modules/com2/main.asm +++ b/avr/modules/com2/main.asm @@ -8,6 +8,9 @@ ; *************************************************************************** +.include "modules/com2/buffer.asm" + + ; *************************************************************************** ; data @@ -19,6 +22,7 @@ com2DataBegin: com2Address: .byte 1 com2Interrupts: .byte 2 + com2LastMsgId: .byte 2 com2RecvStatsBegin: ; 12 bytes com2StatsPacketsIn: .byte 2 @@ -26,7 +30,6 @@ com2RecvStatsBegin: ; 12 bytes com2StatsIoError: .byte 2 com2StatsNoBufferError: .byte 2 com2StatsHandled: .byte 2 - com2StatsNotForMe: .byte 2 com2StatsMissed: .byte 2 ; currently not used com2RecvStatsEnd: @@ -36,6 +39,7 @@ com2SendStatsBegin: ; 6 bytes com2StatsBusyError: .byte 2 com2SendStatsEnd: + com2StatsNotForMe: .byte 2 com2StatsIgnored: .byte 2 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 ; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ @@ -451,7 +527,6 @@ com2IsrPcint0_end: .include "modules/com2/packets.asm" .include "modules/com2/lowlevel.asm" -.include "modules/com2/buffer.asm" .include "modules/com2/crc.asm"