1013 lines
27 KiB
NASM
1013 lines
27 KiB
NASM
; ***************************************************************************
|
|
; copyright : (C) 2023 by Martin Preuss
|
|
; email : martin@libchipcard.de
|
|
;
|
|
; ***************************************************************************
|
|
; * This file is part of the project "AqHome". *
|
|
; * Please see toplevel file COPYING of that project for license details. *
|
|
; ***************************************************************************
|
|
|
|
|
|
|
|
; ***************************************************************************
|
|
; defines
|
|
|
|
.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
|
|
.equ COM2_CRC_POLYNOMIAL = 0x97
|
|
|
|
|
|
; flags for variable payload enqueue function
|
|
.equ COM2_PAYLOAD_FLAGS_SECONDS = 0x01
|
|
.equ COM2_PAYLOAD_FLAGS_UID = 0x02
|
|
.equ COM2_PAYLOAD_FLAGS_RESERVED1 = 0x04
|
|
.equ COM2_PAYLOAD_FLAGS_NUM0 = 0x08
|
|
.equ COM2_PAYLOAD_FLAGS_NUM1 = 0x10
|
|
.equ COM2_PAYLOAD_FLAGS_NUM2 = 0x20
|
|
.equ COM2_PAYLOAD_FLAGS_NUM3 = 0x40
|
|
.equ COM2_PAYLOAD_FLAGS_NUM4 = 0x80
|
|
.equ COM2_PAYLOAD_FLAGS_SHIFT_NUM = 3
|
|
|
|
|
|
|
|
.equ COM2_MSG_OFFS_DESTADDR = 0
|
|
.equ COM2_MSG_OFFS_MSGLEN = 1
|
|
.equ COM2_MSG_OFFS_MSGDATA = 2
|
|
.equ COM2_MSG_OFFS_CMD = 2 ; first at COM2_MSG_OFFS_MSGDATA
|
|
.equ COM2_MSG_OFFS_SRCADDR = 3
|
|
.equ COM2_MSG_OFFS_PAYLOAD = 4 ; payload for the cmd follows here
|
|
|
|
|
|
|
|
; ***************************************************************************
|
|
; data
|
|
|
|
.dseg
|
|
|
|
|
|
com2DataBegin:
|
|
com2Address: .byte 1
|
|
|
|
com2Interrupts: .byte 2
|
|
|
|
com2StatsPacketsIn: .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
|
|
|
|
|
|
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:
|
|
|
|
|
|
|
|
; ***************************************************************************
|
|
; code
|
|
|
|
.cseg
|
|
|
|
COM2_BEGIN:
|
|
|
|
|
|
|
|
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
; Module interface
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; Com2_Init
|
|
;
|
|
; IN:
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear on error
|
|
; USED: R16, R17, X, Y
|
|
|
|
Com2_Init:
|
|
; preset SRAM data area
|
|
ldi xh, HIGH(com2DataBegin)
|
|
ldi xl, LOW(com2DataBegin)
|
|
clr r16
|
|
ldi r17, (com2DataEnd-com2DataBegin)
|
|
rcall Utils_FillSram
|
|
|
|
; set address to 0 (will be updated later)
|
|
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
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; Com_Run
|
|
;
|
|
; 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)
|
|
|
|
COM2_Run:
|
|
rjmp com2HandleNextPacketInQueue
|
|
|
|
|
|
|
|
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
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
; Preparing messages
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; Write a simple packet into the given buffer with payload being only CMD and source address.
|
|
;
|
|
; IN:
|
|
; - R16: destination address
|
|
; - R18: command (e.g. CPRO_CMD_PING or CPRO_CMD_PONG)
|
|
; - X : pointer to buffer to write to
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; MODIFIED REGS: R16, R17, X, Y (R3, R4, R15, R16, R17, R18, R19, R20, R21)
|
|
|
|
COM2_WriteMsgWithCmdAndSrcAddr:
|
|
ldi r17, COM2_PAYLOAD_FLAGS_SECONDS
|
|
push xh
|
|
push xl
|
|
rcall COM2_BeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
|
|
pop xl
|
|
pop xh
|
|
rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, X)
|
|
sec
|
|
ret
|
|
COM2_WriteMsgWithCmdAndSrcAddr_error:
|
|
clc
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; 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)
|
|
|
|
COM2_BeginMsgWithVariablePayload:
|
|
; write header (dest address, msg length)
|
|
st X+, r16 ; destination address
|
|
mov r16, r17 ; calculate payload size
|
|
mov r3, r17
|
|
rcall com2CalcPayloadSize ; (R4, R16, R17)
|
|
inc r16 ; add CMD byte
|
|
inc r16 ; add source address byte
|
|
st X+, r16
|
|
; write payload
|
|
st X+, r18 ; 0: CMD
|
|
lds r16, com2Address ; 1: source address
|
|
st X+, r16
|
|
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
|
|
COM2_BeginMsgWithVariablePayload_l1:
|
|
lsr r3 ; shift out shift out COM2_PAYLOAD_FLAGS_UID
|
|
brcc COM2_BeginMsgWithVariablePayload_l2
|
|
; write uid
|
|
push xh
|
|
push xl
|
|
rcall Utils_ReadUid ; (R16, X)
|
|
pop xl
|
|
pop xh
|
|
st X+, r18
|
|
st X+, r19
|
|
st X+, r20
|
|
st X+, r21
|
|
COM2_BeginMsgWithVariablePayload_l2:
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; com2CalcPayloadSize
|
|
;
|
|
; Calculate payload size from given flags
|
|
;
|
|
; IN:
|
|
; - R16: flags
|
|
; OUT:
|
|
; - R16: payload size
|
|
; MODIFIED REGS: R4, R16, R17
|
|
|
|
com2CalcPayloadSize:
|
|
clr r4
|
|
ldi r17, 4
|
|
lsr r16 ; shift out COM2_PAYLOAD_FLAGS_SECONDS
|
|
brcc com2CalcPayloadSize_l1
|
|
add r4, r17 ; add 4 bytes
|
|
com2CalcPayloadSize_l1:
|
|
lsr r16 ; shift out COM2_PAYLOAD_FLAGS_UID
|
|
brcc com2CalcPayloadSize_l2
|
|
add r4, r17 ; add 4 bytes
|
|
com2CalcPayloadSize_l2:
|
|
lsr r16 ; shift out reserved1, after this R16 contains COM2_PAYLOAD_FLAGS_NUM0-4
|
|
add r16, r4 ; add previous bytes to R16
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
; 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
|
|
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
|
|
; compare destination address (accept "FF" and own address)
|
|
cpi r16, 0xff
|
|
breq com2ReceivePacket_acceptAddr
|
|
lds r17, com2Address
|
|
cp r16, r17
|
|
breq com2ReceivePacket_acceptAddr
|
|
clc ; not for me
|
|
ret
|
|
com2ReceivePacket_acceptAddr:
|
|
st X+, r16 ; store dest address, lock buffer
|
|
; read msg length
|
|
rcall com2ReceiveByte ; read packet length (R16, R17, R20, R21, R22)
|
|
brcc com2ReceivePacket_ioError
|
|
st X+, r16
|
|
cpi r16, (COM2_BUFFER_SIZE-3)
|
|
brcc com2ReceivePacket_contentError ; packet too long
|
|
inc r16 ; account for checksum byte
|
|
mov r17, r16
|
|
com2ReceivePacket_loop:
|
|
push r17
|
|
rcall com2ReceiveByte ; read byte (R16, R17, R20, R21, R22)
|
|
pop r17
|
|
brcc com2ReceivePacket_ioError
|
|
st X+, r16
|
|
dec r17
|
|
brne com2ReceivePacket_loop
|
|
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)
|
|
clc
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; 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)
|
|
|
|
COM2_SendPacket:
|
|
ldi xl, LOW(com2SendBuffer)
|
|
ldi xh, HIGH(com2SendBuffer)
|
|
|
|
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
|
|
nop ; needed to sample current input
|
|
sbis COM_PIN_ATTN, COM_PINNUM_ATTN ; ATTN low?
|
|
rjmp COM2_SendPacket_lineBusyError ; jump if it is
|
|
|
|
cbi COM_PORT_ATTN, COM_PINNUM_ATTN ; set ATTN low
|
|
sbi COM_DDR_ATTN, COM_PINNUM_ATTN ; set ATTN as output
|
|
Utils_WaitNanoSecs COM_BIT_LENGTH, 0, r22 ; wait for one bit duration
|
|
|
|
adiw xh:xl, COM2_MSG_OFFS_MSGLEN
|
|
ld r16, X
|
|
sbiw xh:xl, COM2_MSG_OFFS_MSGLEN
|
|
inc r16 ; account for dest addr
|
|
inc r16 ; account for msglen byte
|
|
inc r16 ; account for crc byte
|
|
rcall com2SendPacketRaw
|
|
|
|
cbi COM_DDR_ATTN, COM_PINNUM_ATTN ; release ATTN line (by setting direction to IN)
|
|
brcc COM2_SendPacket_ioError
|
|
; packet successfully sent
|
|
ldi xl, LOW(com2StatsPacketsOut)
|
|
ldi xh, HIGH(com2StatsPacketsOut)
|
|
rcall Utils_IncrementCounter16 ; (r18, r19, r22)
|
|
pop r15
|
|
out SREG, r15
|
|
sec
|
|
ret
|
|
|
|
COM2_SendPacket_ioError:
|
|
ldi xl, LOW(com2StatsCollisions)
|
|
ldi xh, HIGH(com2StatsCollisions)
|
|
rjmp COM2_SendPacket_incCounter
|
|
|
|
COM2_SendPacket_lineBusyError:
|
|
ldi xl, LOW(com2StatsBusyError)
|
|
ldi xh, HIGH(com2StatsBusyError)
|
|
rjmp COM2_SendPacket_incCounter
|
|
|
|
COM2_SendPacket_incCounter:
|
|
rcall Utils_IncrementCounter16 ; (r18, r19, r22)
|
|
pop r15
|
|
out SREG, r15
|
|
clc
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; send packet over wire, expects ATTN to be low.
|
|
;
|
|
; IN:
|
|
; - r16: number of bytes to send
|
|
; - x : ptr to buffer to send
|
|
; OUT:
|
|
; - nothing
|
|
; REGS: r16, r17, x (r21, r22)
|
|
|
|
com2SendPacketRaw:
|
|
mov r17, r16
|
|
com2SendPacket_loop:
|
|
ld r16, X+
|
|
rcall com2SendByte ; send byte (R16, R21, R22)
|
|
brcc com2SendPacket_ioError
|
|
dec r17
|
|
brne com2SendPacket_loop
|
|
sec
|
|
ret
|
|
com2SendPacket_ioError:
|
|
clc
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
; 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
; Lowlevel IO
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; com2SendByte
|
|
;
|
|
; Send a byte.
|
|
; We only set the data pin to low at the beginning for the startbit. After that
|
|
; we only change the pin direction (e.g. input vs output):
|
|
; - for 0 bit: set DDR to output, forcing the data line low
|
|
; - for 1 bit: set DDR to input, letting the external pullup R pull the data line to HIGH
|
|
; since the output pin is still set to 0 the internal pullup is disabled
|
|
; IN:
|
|
; - R16: byte to send
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; MODIFIED REGS: R16, R21, R22
|
|
|
|
com2SendByte:
|
|
ldi r21, 8 ; +1 bits left
|
|
; send startbit
|
|
cbi COM_PORT_DATA, COM_PINNUM_DATA ; +2 set DATA low
|
|
sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output
|
|
Utils_WaitNanoSecs COM_BIT_LENGTH, 5, r22 ; wait for one bit duration
|
|
; send data bits
|
|
com2SendByte_loop: ; 9 for low bit
|
|
lsr r16 ; 1+ bit to send -> CARRY
|
|
brcs com2SendByte_setHigh ; HI: +2, LO: +1
|
|
com2SendByte_setLow:
|
|
sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output
|
|
Utils_WaitNanoSecs COM_BIT_LENGTH, 9, r22
|
|
rjmp com2SendByte_loopEnd ; +2
|
|
com2SendByte_setHigh:
|
|
cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE
|
|
nop ; +1 (to make pin change available)
|
|
Utils_WaitNanoSecs COM_BIT_LENGTH/2, 11, r22 ; wait for half a bit length for line to safely settle
|
|
sbis COM_PIN_DATA, COM_PINNUM_DATA ; +1 if no skip, +2 if skipped
|
|
rjmp com2SendByte_error ; +2 if error (collision: we wanted line to be high but it is low)
|
|
Utils_WaitNanoSecs COM_BIT_LENGTH/2, 0, r22
|
|
com2SendByte_loopEnd:
|
|
dec r21 ; +1
|
|
brne com2SendByte_loop ; +2, sum per loop: 10 cycles
|
|
; send stopbit
|
|
cbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as input, pullup R makes it ONE
|
|
Utils_WaitNanoSecs COM_BIT_LENGTH, 4, r22 ; wait for one bit length
|
|
sec
|
|
ret
|
|
|
|
com2SendByte_error:
|
|
clc
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; com2ReceiveByte
|
|
;
|
|
; Receive a byte.
|
|
;
|
|
; IN:
|
|
; - nothing
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; - R16: byte read (if CFLAG set)
|
|
; MODIFIED REGS: R16, R20, R21, R22 (R17)
|
|
|
|
com2ReceiveByte:
|
|
cbi COM_DDR_DATA, COM_PINNUM_DATA ; set DATA port as input
|
|
cbi COM_PORT_DATA, COM_PINNUM_DATA ; disable internal pullup for DATA
|
|
|
|
ldi r21, 8 ; bits left
|
|
clr r20 ; byte currently receiving
|
|
; wait for startbit
|
|
rcall com2WaitForDataLow ; (R17)
|
|
brcc com2ReceiveByte_error
|
|
Utils_WaitNanoSecs COM_BIT_LENGTH/2, 5, r22 ; goto middle of startbit to maximize sync stability
|
|
com2ReceiveByte_loop:
|
|
Utils_WaitNanoSecs COM_BIT_LENGTH, 8, r22 ; 8 cycles used in the complete loop between waits
|
|
sec ; +1
|
|
sbic COM_PIN_DATA, COM_PINNUM_DATA ; LOW: +2, HIGH: +1
|
|
rjmp com2ReceiveByte_shiftIn ; HIGH: +2, rjmp, use set CFLAG
|
|
clc ; LOW: +1
|
|
com2ReceiveByte_shiftIn:
|
|
ror r20 ; +1
|
|
dec r21 ; +1
|
|
brne com2ReceiveByte_loop ; +2, sum per loop: 8 cycles
|
|
rcall com2WaitForDataHigh ; wait for start of stopbit
|
|
brcc com2ReceiveByte_error
|
|
mov r16, r20
|
|
sec
|
|
ret
|
|
com2ReceiveByte_error:
|
|
clc
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; com2WaitForDataLow
|
|
;
|
|
; Waits up to COM2_MAXWAIT_US loops for low data line
|
|
; IN:
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; MODIFIED REGISTERS: r17, r22
|
|
|
|
com2WaitForDataLow:
|
|
ldi r17, COM2_MAXWAIT_US
|
|
|
|
com2WaitForDataLow_loop:
|
|
sbis COM_PIN_DATA, COM_PINNUM_DATA
|
|
rjmp com2WaitForDataLow_done
|
|
rcall com2WaitFor1000ns
|
|
dec r17
|
|
brne com2WaitForDataLow_loop
|
|
clc ; timeout
|
|
ret
|
|
|
|
com2WaitForDataLow_done:
|
|
sec ; ok
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; com2WaitForDataHigh
|
|
;
|
|
; Waits up to COM2_MAXWAIT_US loops for high data line
|
|
; IN:
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; MODIFIED REGISTERS: r17, r22, X
|
|
|
|
com2WaitForDataHigh:
|
|
ldi r17, COM2_MAXWAIT_US
|
|
|
|
com2WaitForDataHigh_loop:
|
|
sbic COM_PIN_DATA, COM_PINNUM_DATA
|
|
rjmp com2WaitForDataHigh_done
|
|
rcall com2WaitFor1000ns
|
|
dec r17
|
|
brne com2WaitForDataHigh_loop
|
|
clc ; timeout
|
|
ret
|
|
|
|
com2WaitForDataHigh_done:
|
|
sec ; ok
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; com2WaitForAttnHigh
|
|
;
|
|
; Waits up to COM2_MAXWAIT_US loops for high ATTN line
|
|
; IN:
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; MODIFIED REGISTERS: r17, r22, X
|
|
|
|
com2WaitForAttnHigh:
|
|
ldi r17, COM2_MAXWAIT_US
|
|
|
|
com2WaitForAttnHigh_loop:
|
|
sbic COM_PIN_ATTN, COM_PINNUM_ATTN
|
|
rjmp com2WaitForAttnHigh_done
|
|
rcall com2WaitFor1000ns
|
|
dec r17
|
|
brne com2WaitForAttnHigh_loop
|
|
clc ; timeout
|
|
ret
|
|
|
|
com2WaitForAttnHigh_done:
|
|
sec ; ok
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; comWaitFor100ns
|
|
;
|
|
; Waits for 100 nanoseconds.
|
|
; IN:
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; REGS: r17, r22
|
|
|
|
com2WaitFor1000ns:
|
|
Utils_WaitNanoSecs 1000, 7, r22 ; wait for 100 nanosecs (minus 3 cycles for rcall, 4 for ret)
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
; CRC code
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; add checksum byte to buffer
|
|
;
|
|
; IN:
|
|
; - X : pointer to packet buffer
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; MODIFIED REGS: R16, R17, R18, R19, X
|
|
|
|
com2CalcAndAddChecksumByte:
|
|
adiw xh:xl, COM2_MSG_OFFS_MSGLEN
|
|
ld r18, X ; read msg len
|
|
sbiw xh:xl, COM2_MSG_OFFS_MSGLEN
|
|
inc r18 ; account for dest address
|
|
inc r18 ; account for msg len byte
|
|
ldi r19, COM2_CRC_POLYNOMIAL
|
|
rcall com2CalcCrc8 ; (R16, R17, R18, R20, X)
|
|
st X, r16 ; add checksum byte
|
|
sec
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; check message in buffer
|
|
;
|
|
; IN:
|
|
; - X : pointer to packet buffer
|
|
; OUT:
|
|
; - CFLAG: set if okay, clear otherwise
|
|
; MODIFIED REGS: R16, R17, R18, R19, R20, X
|
|
|
|
com2CheckMessageInBuffer:
|
|
adiw xh:xl, COM2_MSG_OFFS_MSGLEN
|
|
ld r18, X ; read msg len
|
|
sbiw xh:xl, COM2_MSG_OFFS_MSGLEN
|
|
inc r18 ; account for dest address
|
|
inc r18 ; account for msg len byte
|
|
ldi r19, COM2_CRC_POLYNOMIAL
|
|
rcall com2CalcCrc8 ; (R16, R17, R18, R20, X)
|
|
ld r17, X
|
|
cp r16, r17 ; should be equal
|
|
brne com2CheckMessageInBuffer_error
|
|
sec
|
|
ret
|
|
com2CheckMessageInBuffer_error:
|
|
clc
|
|
ret
|
|
|
|
|
|
|
|
; ---------------------------------------------------------------------------
|
|
; calc crc8 checksum using given polynomial
|
|
;
|
|
; IN:
|
|
; - X : pointer to data to calc crc8 for
|
|
; - r18: number of bytes to calc crc8 for
|
|
; - r19: polynomial to use (e.g. 0x97: HD=4 up to 119 bytes, e.g. detects all 1 to 3 bit errors)
|
|
; OUT:
|
|
; - r16: crc8 checksum
|
|
; - X : point directly behind the checked area
|
|
; MODIFIED REGS: R16, R17, R18, R20, X
|
|
|
|
com2CalcCrc8:
|
|
ldi r16, 0xff ; crc
|
|
|
|
com2CalcCrc8_loop1:
|
|
ld r17, X+ ; running var
|
|
eor r16, r17
|
|
ldi r20, 8 ; counter for loop2
|
|
com2CalcCrc8_loop2:
|
|
lsl r16
|
|
brcc com2CalcCrc8_l1
|
|
eor r16, r19
|
|
com2CalcCrc8_l1:
|
|
dec r20
|
|
brne com2CalcCrc8_loop2
|
|
dec r18
|
|
brne com2CalcCrc8_loop1
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
; 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
|
|
|