Implemented address assignment protocol.

This commit is contained in:
Martin Preuss
2023-02-05 23:42:59 +01:00
parent c1aee1e2c5
commit 716248c4e1
6 changed files with 655 additions and 46 deletions

View File

@@ -14,6 +14,9 @@
.equ CPRO_CMD_VALUE = 50
.equ CPRO_CMD_NEED_ADDRESS = 60
.equ CPRO_CMD_HAVE_ADDRESS = 61
.equ CPRO_CMD_CLAIM_ADDRESS = 62
.equ CPRO_CMD_DENY_ADDRESS = 63
.equ CPRO_CMD_ADDRESS_RANGE = 64
; flags for variable payload enqueue function
@@ -28,6 +31,28 @@
.equ CPRO_PAYLOAD_FLAGS_SHIFT_NUM = 5
.equ CPRO_PACKET_OFFS_DESTADDR = 0
.equ CPRO_PACKET_OFFS_MSGLEN = 1
.equ CPRO_PACKET_OFFS_CMD = 2
.equ CPRO_PACKET_OFFS_SRCADDR = 3
.equ CPRO_PACKET_OFFS_PAYLOAD = 4
.equ CPRO_PACKET_HAVEADDR_OFFS_ADDRESS = 8
.equ CPRO_PACKET_CLAIMADDR_OFFS_ADDRESS = 8
.equ CPRO_PACKET_DENYADDR_OFFS_ADDRESS = 8
.equ CPRO_WAITTIME_GETADDR = 130
.equ CPRO_WAITTIME_CLAIMADDR = 30
; current mode of operation
.equ CPRO_MODE_NORMAL = 0 ; normal operation
.equ CPRO_MODE_GETADDRSTARTED = 10 ; waiting for HAVE_ADDRESS and ADDRESS_RANGE packets to arrive
.equ CPRO_MODE_CLAIMING_ADDR1 = 20 ; CLAIM_ADDRESS sent, waiting for HAVE_ADDRESS packet to reject the claim
.equ CPRO_MODE_CLAIMING_ADDR2 = 21 ; CLAIM_ADDRESS sent, 2nd try
.equ CPRO_MODE_CLAIMING_ADDR3 = 22 ; CLAIM_ADDRESS sent, 3rd try
.equ CPRO_MODE_SENDING_HAVE_ADDRESS = 30 ; waiting for our turn to send HAVE_ADDRESS packet
; ***************************************************************************
; data
@@ -35,6 +60,14 @@
.dseg
cproDataBegin:
cproMode: .byte 1 ; "normal", "waitForHaveAddress", "samplingAddresses", "claimAddress"
cproAddrRangeBegin: .byte 1
cproAddrRangeEnd: .byte 1
cproAddressWaitCounter: .byte 1 ; counter for seconds to wait for all nodes to respond
cproUsedAddresses: .byte 16 ; one bit per address known to b in use
cproDataEnd:
; ***************************************************************************
@@ -44,6 +77,104 @@
CPRO_Init:
; preset SRAM data area
ldi xh, HIGH(cproDataBegin)
ldi xl, LOW(cproDataBegin)
clr r16
ldi r17, (cproDataEnd-cproDataBegin)
rcall Utils_FillSram
sec
ret
CPRO_OnEverySecond:
lds r17, cproMode
cpi r17, CPRO_MODE_NORMAL
breq CPRO_OnEverySecond_done
CPRO_OnEverySecond_l1:
cpi r17, CPRO_MODE_GETADDRSTARTED
brne CPRO_OnEverySecond_l2
rjmp cproHandle1sGetAddrStarted
CPRO_OnEverySecond_l2:
cpi r17, CPRO_MODE_SENDING_HAVE_ADDRESS
brne CPRO_OnEverySecond_l3
rjmp cproHandle1sSendingHaveAddress
CPRO_OnEverySecond_l3:
cpi r17, CPRO_MODE_CLAIMING_ADDR1
brne CPRO_OnEverySecond_l4
rjmp cproHandle1sClaimingAddr12
CPRO_OnEverySecond_l4:
cpi r17, CPRO_MODE_CLAIMING_ADDR2
brne CPRO_OnEverySecond_l5
rjmp cproHandle1sClaimingAddr12
CPRO_OnEverySecond_l5:
cpi r17, CPRO_MODE_CLAIMING_ADDR3
brne CPRO_OnEverySecond_done
rjmp cproHandle1sClaimingAddr3
CPRO_OnEverySecond_done:
ret
; ---------------------------------------------------------------------------
; CPRO_OnPacketReceived:
;
; Try to handle the given packet.
;
; IN:
; - Y : pointer to received buffer
; OUT:
; - CFLAG: set if handled, cleared otherwise
; USED: depending on called routines
CPRO_OnPacketReceived:
ldd r16, y+(COM_BUFFER_OFFS_DATA+CPRO_PACKET_OFFS_CMD)
cpi r16, CPRO_CMD_PING
brne CPRO_OnPacketReceived_l1
rjmp cproHandlePing
CPRO_OnPacketReceived_l1:
cpi r16, CPRO_CMD_NEED_ADDRESS
brne CPRO_OnPacketReceived_l2
rjmp cproHandlePckNeedAddr
CPRO_OnPacketReceived_l2:
cpi r16, CPRO_CMD_HAVE_ADDRESS
brne CPRO_OnPacketReceived_l3
rjmp cproHandlePckHaveAddr
CPRO_OnPacketReceived_l3:
cpi r16, CPRO_CMD_ADDRESS_RANGE
brne CPRO_OnPacketReceived_l4
rjmp cproHandleAddrRange
CPRO_OnPacketReceived_l4:
cpi r16, CPRO_CMD_DENY_ADDRESS
brne CPRO_OnPacketReceived_l5
rjmp cproHandleDenyAddr
CPRO_OnPacketReceived_l5:
cpi r16, CPRO_CMD_CLAIM_ADDRESS
brne CPRO_OnPacketReceived_l6
rjmp cproHandlePckClaimAddr
CPRO_OnPacketReceived_l6:
clc
ret
cproHandlePing:
ldd r16, y+(COM_BUFFER_OFFS_DATA+CPRO_PACKET_OFFS_SRCADDR)
tst r16
breq cproHandlePing_notHandled
rcall CPRO_EnqueuePong
ret ; use carry flag from previous call
cproHandlePing_notHandled:
clc
ret
; ---------------------------------------------------------------------------
; Enqueue a PING packet.
;
@@ -286,21 +417,85 @@ CPRO_EnqueueValue_error:
; Enqueue a NEEDADDRESS packet.
;
; IN:
; - R16: destination address
; - nothing
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R18 (R3, R4, R15, R16, R17, R20, R21, X, Y (R18, R19)
CPRO_EnqueueNeedAddress:
ldi r18, CPRO_CMD_NEED_ADDRESS
rjmp cproEnqueueAddressPacket
; ---------------------------------------------------------------------------
; Enqueue a HAVE_ADDRESS packet.
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R18 (R3, R4, R15, R16, R17, R18, R19, R20, R21, X, Y)
CPRO_EnqueueHaveAddress:
ldi r18, CPRO_CMD_HAVE_ADDRESS
lds r19, comAddress
rjmp cproEnqueueAddressPacket
; ---------------------------------------------------------------------------
; Enqueue a CLAIM_ADDRESS packet.
;
; IN:
; - R19: claimed address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R18 (R3, R4, R15, R16, R17, R18, R19, R20, R21, X, Y)
CPRO_EnqueueClaimAddress:
ldi r18, CPRO_CMD_CLAIM_ADDRESS
rjmp cproEnqueueAddressPacket
; ---------------------------------------------------------------------------
; Enqueue a DENY_ADDRESS packet.
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R18, R19 (R3, R4, R15, R16, R17, R20, R21, X, Y)
CPRO_EnqueueDenyAddress:
ldi r18, CPRO_CMD_DENY_ADDRESS
lds r19, comAddress
rjmp cproEnqueueAddressPacket
; ---------------------------------------------------------------------------
; cproEnqueueAddressPacket
; Enqueue a NEED/HAVE/CLAIM ADDRESS packet.
;
; IN:
; - R18: command (either CPRO_CMD_NEED_ADDRESS, CPRO_CMD_HAVE_ADDRESS or CPRO_CMD_CLAIM_ADDRESS)
; - R19: address to send (claim, have)
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, R21, X, Y (R3, R4, R15, R16, R17, R18, R19, R21, X)
CPRO_EnqueueNeedAddress:
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
pop r16
brcc CPRO_EnqueueNeedAddress_error
ldi r17, CPRO_PAYLOAD_FLAGS_UID
ldi r18, CPRO_CMD_NEED_ADDRESS
cproEnqueueAddressPacket:
mov r6, r19
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
brcc cproEnqueueAddressPacket_error
ldi r16, 0xff
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (1<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM)
push xh
push xl
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
st X+, r6 ; 5: value id
pop xl
pop xh
rcall cproCalcAndAddChecksumByte
@@ -308,10 +503,10 @@ CPRO_EnqueueNeedAddress:
; mark buffer as enqueued with PRIO "important" (higher retry count)
ldi r20, COM_BUFFER_PRIO_IMPORTANT
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueNeedAddress_error
brcc cproEnqueueAddressPacket_error
sec
ret
CPRO_EnqueueNeedAddress_error:
cproEnqueueAddressPacket_error:
clc
ret