avr: moved COM and COMPROTO defs and routines into dedicated files.
This commit is contained in:
663
avr/com2.asm
663
avr/com2.asm
@@ -12,33 +12,8 @@
|
||||
; ***************************************************************************
|
||||
; defines
|
||||
|
||||
.equ COM2_BUFFER_SIZE = 24 ; CAVE: must change code in COM2_BufferPosToX when changing this!
|
||||
.equ COM2_BUFFER_NUM = 4
|
||||
.include "com2_defs.asm"
|
||||
|
||||
.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
|
||||
|
||||
|
||||
|
||||
@@ -317,82 +292,44 @@ com2ReceivePacket:
|
||||
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
|
||||
clc
|
||||
ret
|
||||
|
||||
com2ReceivePacket_bufferAvailable:
|
||||
push xl
|
||||
push xh
|
||||
rcall com2ReceivePacketIntoX
|
||||
lds r16, com2Address
|
||||
rcall com2ReceivePacketRaw
|
||||
pop xh
|
||||
pop xl
|
||||
brcs com2ReceivePacket_received
|
||||
rcall COM2_BufferDeallocBack
|
||||
clc
|
||||
ret
|
||||
com2ReceivePacket_received:
|
||||
brcc com2ReceivePacket_error
|
||||
|
||||
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:
|
||||
brcc com2ReceivePacket_dataError
|
||||
|
||||
; everything okay
|
||||
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_error:
|
||||
cpi r16, COM2_ERROR_IOERROR
|
||||
breq com2ReceivePacket_ioError
|
||||
cpi r16, COM2_ERROR_DATAERROR
|
||||
breq com2ReceivePacket_dataError
|
||||
rjmp com2ReceivePacket_retnc
|
||||
com2ReceivePacket_ioError:
|
||||
ldi xl, LOW(com2StatsIoError)
|
||||
ldi xh, HIGH(com2StatsIoError)
|
||||
rjmp com2ReceivePacket_incCounter
|
||||
com2ReceivePacket_contentError:
|
||||
rjmp com2ReceivePacket_incCounterDeallocNc
|
||||
com2ReceivePacket_dataError:
|
||||
ldi xl, LOW(com2StatsContentError)
|
||||
ldi xh, HIGH(com2StatsContentError)
|
||||
rjmp com2ReceivePacket_incCounter
|
||||
com2ReceivePacket_incCounter:
|
||||
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
|
||||
rjmp com2ReceivePacket_incCounterDeallocNc
|
||||
com2ReceivePacket_incCounterDeallocNc:
|
||||
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
|
||||
com2ReceivePacket_deallocRetnc:
|
||||
rcall COM2_BufferDeallocBack
|
||||
com2ReceivePacket_retnc:
|
||||
clc
|
||||
ret
|
||||
|
||||
@@ -412,85 +349,31 @@ COM2_SendPacket:
|
||||
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
|
||||
rcall COM2_SendPacketWithAttn
|
||||
brcs COM2_SendPacket_okay
|
||||
|
||||
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
|
||||
cpi r16, COM2_ERROR_BUSY
|
||||
breq COM2_SendPacket_busyError
|
||||
cpi r16, COM2_ERROR_COLLISION
|
||||
breq COM2_SendPacket_collError
|
||||
|
||||
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
|
||||
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
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; 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
|
||||
COM2_SendPacket_okay:
|
||||
ldi xl, LOW(com2StatsPacketsOut)
|
||||
ldi xh, HIGH(com2StatsPacketsOut)
|
||||
rcall Utils_IncrementCounter16 ; (r18, r19, r22)
|
||||
sec
|
||||
ret
|
||||
com2SendPacket_ioError:
|
||||
clc
|
||||
ret
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -544,466 +427,10 @@ com2IsrPcint0_end:
|
||||
|
||||
|
||||
|
||||
|
||||
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
; 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
|
||||
|
||||
|
||||
.include "com2_packets.asm"
|
||||
.include "com2_lowlevel.asm"
|
||||
.include "com2_buffer.asm"
|
||||
.include "com2_crc.asm"
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user