avr: new com2 and timer stack basically works again.

This commit is contained in:
Martin Preuss
2023-04-12 21:46:34 +02:00
parent 18d34450e7
commit b4c0ab273d
15 changed files with 461 additions and 171 deletions

View File

@@ -12,7 +12,8 @@
; ***************************************************************************
; defines
.equ COM2_BUFFER_SIZE = 24
.equ COM2_BUFFER_SIZE = 24 ; CAVE: must change code in COM2_BufferPosToX when changing this!
.equ COM2_BUFFER_NUM = 4
.equ COM2_MAXWAIT_US = 100 ; maximum wait time in microseconds when waiting for rising/falling clock
.equ COM2_MAINTENANCE_ADDR = 0xc1
@@ -53,16 +54,25 @@ com2DataBegin:
com2Interrupts: .byte 2
com2StatsPacketsIn: .byte 2
com2StatsPacketsOut: .byte 2
com2StatsIoError: .byte 2
com2StatsContentError: .byte 2
com2StatsNoBufferError: .byte 2
com2StatsIgnored: .byte 2
com2StatsHandled: .byte 2
com2StatsMissed: .byte 2 ; currently not used
com2StatsPacketsOut: .byte 2
com2StatsBusyError: .byte 2
com2StatsCollisions: .byte 2
com2RecvBuffer: .byte COM2_BUFFER_SIZE
com2RecvBuffersUsed: .byte 1
com2MaxBuffersUsed: .byte 1
com2RecvBuffersWritePos: .byte 1
com2RecvBuffersReadPos: .byte 1
com2RecvBuffers: .byte COM2_BUFFER_SIZE*COM2_BUFFER_NUM
com2SendBuffer: .byte COM2_BUFFER_SIZE
com2DataEnd:
@@ -134,19 +144,32 @@ Com2_Init:
; REGS: (R1, R3, R16, R17, R18, R19, R22, X)
COM2_Run:
lds r16, com2RecvBuffer
tst r16
brne COM2_Run_packetReceived
clc
ret
rjmp com2HandleNextPacketInQueue
COM2_Run_packetReceived:
ldi xl, LOW(com2RecvBuffer)
ldi xh, HIGH(com2RecvBuffer)
com2HandleNextPacketInQueue:
lds r16, com2RecvBuffersUsed
tst r16
breq com2HandleNextPacketInQueue_retNc ; no buffers in use
lds r16, com2RecvBuffersReadPos
rcall COM2_BufferPosToX ; get current read buffer to X (R16, R17)
rcall onPacketReceived
clr r16
sts com2RecvBuffer, r16 ; unlock buffer
sec
brcs com2HandleNextPacketInQueue_handled
ldi xl, LOW(com2StatsIgnored)
ldi xh, HIGH(com2StatsIgnored)
rjmp com2HandleNextPacketInQueue_incCounterDeallocBuffer
com2HandleNextPacketInQueue_handled:
ldi xl, LOW(com2StatsHandled)
ldi xh, HIGH(com2StatsHandled)
com2HandleNextPacketInQueue_incCounterDeallocBuffer:
rcall Utils_IncrementCounter16 ; (r18, r19, r22)
rcall COM2_BufferDeallocFront ; (r16, r17, r21)
sec ; always return with set CFLAG
ret
com2HandleNextPacketInQueue_retNc:
clc
ret
@@ -286,13 +309,9 @@ com2CalcPayloadSize_l2:
; REGS: r16, r17, x (r18, r19, r20, r21, r22)
com2ReceivePacket:
ldi xl, LOW(com2RecvBuffer)
ldi xh, HIGH(com2RecvBuffer)
rcall COM2_BufferAlloc ; (r16, r17, r21)
brcs com2ReceivePacket_bufferAvailable
ld r16, X ; check: buffer in use?
tst r16
breq com2ReceivePacket_bufferAvailable
ldi xl, LOW(com2StatsNoBufferError) ; buffer in use, don't release
ldi xh, HIGH(com2StatsNoBufferError) ; just increment error counter
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
@@ -300,6 +319,39 @@ com2ReceivePacket:
ret
com2ReceivePacket_bufferAvailable:
push xl
push xh
rcall com2ReceivePacketIntoX
pop xh
pop xl
brcs com2ReceivePacket_received
rcall COM2_BufferDeallocBack
clc
ret
com2ReceivePacket_received:
rcall com2CheckMessageInBuffer ; (R16, R17, R18, R19, R20, X)
brcs com2ReceivePacket_crcOkay
ldi xl, LOW(com2StatsContentError)
ldi xh, HIGH(com2StatsContentError)
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
clc
ret
com2ReceivePacket_crcOkay:
ldi xl, LOW(com2StatsPacketsIn)
ldi xh, HIGH(com2StatsPacketsIn)
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
sec
ret
; ---------------------------------------------------------------------------
; receive packet into buffer pointed to by X.
;
; OUT:
; - CFLAG: set if okay (packet received), cleared on error
; REGS: r16, r17, x (r18, r19, r20, r21, r22)
com2ReceivePacketIntoX:
; read destination address
rcall com2ReceiveByte ; read byte (R16, R17, R20, R21, R22)
brcc com2ReceivePacket_ioError
@@ -311,7 +363,6 @@ com2ReceivePacket_bufferAvailable:
breq com2ReceivePacket_acceptAddr
clc ; not for me
ret
com2ReceivePacket_acceptAddr:
st X+, r16 ; store dest address, lock buffer
; read msg length
@@ -330,32 +381,18 @@ com2ReceivePacket_loop:
st X+, r16
dec r17
brne com2ReceivePacket_loop
ldi xl, LOW(com2RecvBuffer)
ldi xh, HIGH(com2RecvBuffer)
rcall com2CheckMessageInBuffer ; (R16, R17, R18, R19, R20, X)
brcc com2ReceivePacket_contentError
; done, increment packet counter, set flags
ldi xl, LOW(com2StatsPacketsIn)
ldi xh, HIGH(com2StatsPacketsIn)
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
sec
ret
com2ReceivePacket_ioError:
ldi xl, LOW(com2StatsIoError)
ldi xh, HIGH(com2StatsIoError)
rjmp com2ReceivePacket_incCounter
com2ReceivePacket_contentError:
ldi xl, LOW(com2StatsContentError)
ldi xh, HIGH(com2StatsContentError)
rjmp com2ReceivePacket_incCounter
com2ReceivePacket_incCounter:
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
clr r16
sts com2RecvBuffer, r16 ; unlock buffer
clc
ret
@@ -378,6 +415,7 @@ COM2_SendPacketAtX:
in r15, SREG
push r15
cli
Utils_WaitNanoSecs COM_BIT_LENGTH, 0, r22 ; wait for one bit duration
; check for ATTN line: busy?
cbi COM_PORT_ATTN, COM_PINNUM_ATTN ; disable pullup on ATTN
cbi COM_DDR_ATTN, COM_PINNUM_ATTN ; set ATTN as input
@@ -478,7 +516,9 @@ com2IsrPcint0:
push r22
push xh
push xl
rcall com2ReceivePacket ; (r16, r17, r18, r19, r20, r21, r22, x)
push r15
rcall com2ReceivePacket ; (r16, r17, r18, r19, r20, r21, r22, x)
pop r15
lds xl, com2Interrupts
lds xh, com2Interrupts+1
adiw xh:xl, 1
@@ -494,8 +534,8 @@ com2IsrPcint0:
pop r17
pop r16
pop r1
com2IsrPcint0_end:
out SREG, r15
pop r15
reti
@@ -799,6 +839,174 @@ com2CalcCrc8_l1:
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; buffer code
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ---------------------------------------------------------------------------
; COM_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
COM2_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
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
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
sec
ret
COM2_AllocBuffer_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM2_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_BufferDeallocBack:
in r21, SREG ; save global interrupt enable bit from SREG
cli
lds r17, com2RecvBuffersUsed
tst r17
breq COM2_BufferDeallocBack_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:
dec r17
sts com2RecvBuffersWritePos, r17
out SREG, r21
sec
ret
COM2_BufferDeallocBack_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM2_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_BufferDeallocFront:
in r21, SREG ; save global interrupt enable bit from SREG
cli
lds r17, com2RecvBuffersUsed
tst r17
breq COM2_BufferDeallocFront_error ; no buffer allocated, nothing to release
dec r17
sts com2RecvBuffersUsed, r17 ; store new value
lds r17, com2RecvBuffersReadPos
inc r17
cpi r17, COM2_BUFFER_NUM
brcs COM2_BufferDeallocFront_l1
clr r17 ; wrap-around
COM2_BufferDeallocFront_l1:
sts com2RecvBuffersReadPos, r17
out SREG, r21
sec
ret
COM2_BufferDeallocFront_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM2_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_BufferPosToX:
; calculate offset
clr r17
mov xl, r16
clr xh
lsl xl
rol xh ; *2
add xl, r16
adc xh, r17 ; *3
lsl xl
rol xh ; *6
lsl xl
rol xh ; *12
lsl xl
rol xh ; *24
; add base position of buffers
ldi r16, LOW(com2RecvBuffers)
ldi r17, HIGH(com2RecvBuffers)
add xl, r16
adc xh, r17
ret
COM2_END:
.equ MODULE_SIZE_COM2 = COM2_END-COM2_BEGIN