avr: new uart_bitbang module works now.

This commit is contained in:
Martin Preuss
2024-10-20 23:09:24 +02:00
parent c90c33ec11
commit efc91241d9
8 changed files with 137 additions and 318 deletions

View File

@@ -42,12 +42,9 @@ com2SendStatsEnd:
com2StatsNotForMe: .byte 2
com2StatsIgnored: .byte 2
com2RecvBuffersUsed: .byte 1
com2MaxBuffersUsed: .byte 1
com2RecvBuffersWritePos: .byte 1
com2RecvBuffersReadPos: .byte 1
com2MaxSendBuffersUsed: .byte 1
com2MaxRecvBuffersUsed: .byte 1
com2RecvBuffers: .byte COM2_BUFFER_SIZE*COM2_BUFFER_NUM
com2SendBuffer: .byte COM2_BUFFER_SIZE
com2DataEnd:
@@ -63,12 +60,6 @@ COM2_BEGIN:
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Module interface
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ---------------------------------------------------------------------------
; Com2_Init
;
@@ -89,47 +80,48 @@ Com2_Init:
clr r16
sts com2Address, r16
; setup pins and interrupts
cbi COM_PORT_DATA, COM_PINNUM_DATA ; disable internal pullup for DATA
cbi COM_DDR_DATA, COM_PINNUM_DATA ; set DATA port as input
cbi COM_PORT_ATTN, COM_PINNUM_ATTN ; disable internal pullup for ATTN
cbi COM_DDR_ATTN, COM_PINNUM_ATTN ; set ATTN port as input
sbi COM_IRQ_ADDR_ATTN, COM_IRQ_BIT_ATTN ; enable pin change irq for ATTN line
in r16, GIMSK ; enable pin change irq PCIE0 or PCIE1
ori r16, (1<<COM_IRQ_GIMSK_ATTN)
out GIMSK, R16
ldi r16, (1<<COM_IRQ_GIFR_ATTN) ; clear pending irq by writing 1 to ATTN bit
out GIFR, r16
sec
ret
rjmp COMIO_Init
; ---------------------------------------------------------------------------
; Com_Run
; @routine Com2_Run @global
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if something done, can be called again immediately (otherwise: wait for timer interrupt and retry)
; REGS: (R1, R3, R16, R17, R18, R19, R22, X)
; @return CFLAG set if something done, can be called again immediately (otherwise: wait for timer interrupt and retry)
; @clobbers (R1, R3, R16, R17, R18, R19, R22, X)
COM2_Run:
rjmp com2HandleNextPacketInQueue
rcall COMIO_Run
brcs COM2_Run_carrySet
rjmp com2HandleNextPacketInQueue ; return CFLAG from com2HandleNextPacketInQueue
COM2_Run_carrySet:
rcall com2HandleNextPacketInQueue
sec ; return CFLAG set regardless of com2HandleNextPacketInQueue
ret
; @end
; ---------------------------------------------------------------------------
; @routine com2HandleNextPacketInQueue
;
; Get next received packet, call onPacketReceived on it and release it
; @return CFLAG set if something done
; @clobbers any
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 COMIO_GetNextReceivedPacket
brcc com2HandleNextPacketInQueue_retNc
push xl
push xh
rcall com2CheckMessageInBuffer
pop xh
pop xl
brcs com2HandleNextPacketInQueue_crcOk
ldi xl, LOW(com2StatsContentError)
ldi xh, HIGH(com2StatsContentError)
rjmp com2HandleNextPacketInQueue_incCounterDeallocBuffer
com2HandleNextPacketInQueue_crcOk:
rcall onPacketReceived
brcs com2HandleNextPacketInQueue_handled
ldi xl, LOW(com2StatsIgnored)
@@ -140,13 +132,13 @@ com2HandleNextPacketInQueue_handled:
ldi xh, HIGH(com2StatsHandled)
com2HandleNextPacketInQueue_incCounterDeallocBuffer:
rcall Utils_IncrementCounter16 ; (r18, r19, r22)
rcall COM2_BufferDeallocFront ; (r16, r17, r21)
sec ; always return with set CFLAG
rcall COMIO_ReleaseReceivedPacket
sec
ret
com2HandleNextPacketInQueue_retNc:
clc
ret
; @end
@@ -173,7 +165,7 @@ COM2_WriteMsgWithCmdAndSrcAddr:
ldi r17, COM2_PAYLOAD_FLAGS_SECONDS
push xh
push xl
rcall COM2_BeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
rcall COM2_BeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
pop xl
pop xh
rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, X)
@@ -189,13 +181,12 @@ COM2_WriteMsgWithCmdAndSrcAddr_error:
; begin packet with variable payload.
;
; IN:
; - R16: destination address
; - R17: flags
; - R18: command (e.g. CPRO_CMD_PING)
; - X : pointer to packet buffer
; OUT:
; - X : points to end of packet as it was written so far
; MODIFIED REGS: R3, R16, R17, R18, R19, R20, R21, X (R4)
; @param R16 destination address
; @param R17 flags
; @param R18 command (e.g. CPRO_CMD_PING)
; @param X pointer to packet buffer
; @return X points to end of packet as it was written so far
; @clobbers R3, R16, R17, R18, R19, R20, R21, X (R4)
COM2_BeginMsgWithVariablePayload:
; write header (dest address, msg length)
@@ -213,14 +204,7 @@ COM2_BeginMsgWithVariablePayload:
lsr r3 ; shift out COM2_PAYLOAD_FLAGS_SECONDS
brcc COM2_BeginMsgWithVariablePayload_l1
; write seconds
lds r16, timerModuleCounterSecs ; adding of current seconds counter requested
st X+, r16
lds r16, timerModuleCounterSecs+1
st X+, r16
lds r16, timerModuleCounterSecs+2
st X+, r16
lds r16, timerModuleCounterSecs+3
st X+, r16
rcall COM2_AddSecsToBuffer
COM2_BeginMsgWithVariablePayload_l1:
lsr r3 ; shift out COM2_PAYLOAD_FLAGS_UID
brcc COM2_BeginMsgWithVariablePayload_l2
@@ -237,9 +221,6 @@ COM2_BeginMsgWithVariablePayload_l2:
; Write UID into buffer given by X.
;
; @return X points to behind written uid
; @param R16 destination address
; @param R17 flags
; @param R18 command (e.g. CPRO_CMD_PING)
; @param X pointer to packet buffer
; @clobbers: r18, r19, r20, r21 (r16)
@@ -258,6 +239,54 @@ COM2_AddUidToBuffer:
; ---------------------------------------------------------------------------
; @routine COM2_AddSecsToBuffer @global
;
; Write current seconds counter (4 bytes) into buffer given by X, advance X.
;
; @return X points to behind written seconds
; @param X pointer to packet buffer
; @clobbers: r16
COM2_AddSecsToBuffer:
lds r16, timerModuleCounterSecs ; add current seconds counter
st X+, r16
lds r16, timerModuleCounterSecs+1
st X+, r16
lds r16, timerModuleCounterSecs+2
st X+, r16
lds r16, timerModuleCounterSecs+3
st X+, r16
ret
; @end
; ---------------------------------------------------------------------------
; @routine COM2_AddNextMsgIdToBuffer @global
;
; Write next message id (2 bytes) into buffer given by X, advance X.
;
; @return X points to behind written message id
; @param X pointer to packet buffer
; @clobbers: r16, r17, r18
COM2_AddNextMsgIdToBuffer:
ldi r18, 1
lds r16, com2LastMsgId
lds r17, com2LastMsgId+1
add r16, r18
dec r18
adc r17, r18
sts com2LastMsgId, r16
sts com2LastMsgId+1, r17
st X+, r16
st X+, r17
ret
; @end
; ---------------------------------------------------------------------------
; com2CalcPayloadSize
;
@@ -290,243 +319,22 @@ com2CalcPayloadSize_l2:
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; Sending and receiving messages
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; ---------------------------------------------------------------------------
; receive packet.
;
; OUT:
; - CFLAG: set if okay (packet received), cleared on error
; REGS: r16, r17, x (r18, r19, r20, r21, r22)
com2ReceivePacket:
rcall COM2_BufferAlloc ; (r16, r17, r21)
brcs 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)
clc
ret
com2ReceivePacket_bufferAvailable:
push xl
push xh
lds r16, com2Address
ldi r17, (COM2_BUFFER_SIZE-3) ; buffer size minus dst addr, payload len, crc byte
rcall com2ReceivePacketRaw
pop xh
pop xl
brcc com2ReceivePacket_error
rcall com2CheckMessageInBuffer ; (R16, R17, R18, R19, R20, X)
brcc com2ReceivePacket_dataError
; everything okay
ldi xl, LOW(com2StatsPacketsIn)
ldi xh, HIGH(com2StatsPacketsIn)
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
sec
ret
com2ReceivePacket_error:
cpi r16, COM2_ERROR_IOERROR
breq com2ReceivePacket_ioError
cpi r16, COM2_ERROR_DATAERROR
breq com2ReceivePacket_dataError
cpi r16, COM2_ERROR_NOTFORME
breq com2ReceivePacket_notForMe
rjmp com2ReceivePacket_retnc
com2ReceivePacket_ioError:
ldi xl, LOW(com2StatsIoError)
ldi xh, HIGH(com2StatsIoError)
rjmp com2ReceivePacket_incCounterDeallocNc
com2ReceivePacket_dataError:
ldi xl, LOW(com2StatsContentError)
ldi xh, HIGH(com2StatsContentError)
rjmp com2ReceivePacket_incCounterDeallocNc
com2ReceivePacket_notForMe:
ldi xl, LOW(com2StatsNotForMe)
ldi xh, HIGH(com2StatsNotForMe)
rjmp com2ReceivePacket_incCounterDeallocNc
com2ReceivePacket_incCounterDeallocNc:
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
com2ReceivePacket_deallocRetnc:
rcall COM2_BufferDeallocBack
com2ReceivePacket_retnc:
clc
ret
; ---------------------------------------------------------------------------
; @routine COM2_SendPacket
; send packet from sendBuffer, if line free.
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay (packet sent), cleared on error
; REGS: r22, x (r18, r19, r22)
; @return CFLAG set if okay (packet sent), cleared on error
; @clobbers any (depending on io implementation)
COM2_SendPacket:
ldi xl, LOW(com2SendBuffer)
ldi xh, HIGH(com2SendBuffer)
COM2_SendPacketAtX:
rcall COM2_SendPacketWithAttn
brcs COM2_SendPacket_okay
cpi r16, COM2_ERROR_BUSY
breq COM2_SendPacket_busyError
cpi r16, COM2_ERROR_COLLISION
breq COM2_SendPacket_collError
COM2_SendPacket_busyError:
ldi xl, LOW(com2StatsBusyError)
ldi xh, HIGH(com2StatsBusyError)
rjmp COM2_SendPacket_incCounterRetNc
COM2_SendPacket_collError:
ldi xl, LOW(com2StatsCollisions)
ldi xh, HIGH(com2StatsCollisions)
COM2_SendPacket_incCounterRetNc:
rcall Utils_IncrementCounter16 ; (r18, r19, r22)
clc
ret
COM2_SendPacket_okay:
ldi xl, LOW(com2StatsPacketsOut)
ldi xh, HIGH(com2StatsPacketsOut)
rcall Utils_IncrementCounter16 ; (r18, r19, r22)
sec
ret
rjmp COMIO_SendPacket
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
; 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
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
com2IsrPcint0:
push r15
in r15, SREG
sbic COM_PIN_ATTN, COM_PINNUM_ATTN
rjmp com2IsrPcint0_end
; low, read packet
push r1
push r16
push r17
push r18
push r19
push r20
push r21
push r22
push xh
push xl
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
sts com2Interrupts, xl
sts com2Interrupts+1, xh
pop xl
pop xh
pop r22
pop r21
pop r20
pop r19
pop r18
pop r17
pop r16
pop r1
com2IsrPcint0_end:
out SREG, r15
pop r15
reti
.include "modules/com2/packets.asm"
.include "modules/com2/lowlevel.asm"
.include "modules/com2/crc.asm"