reworked com stack.

- prepared for use of CRC8
- organized code in more files
- recv stats message now contains crc errors and io errors
This commit is contained in:
Martin Preuss
2023-04-07 19:13:54 +02:00
parent 3bb867ed21
commit 4e1f08b567
22 changed files with 1326 additions and 1161 deletions

View File

@@ -333,23 +333,23 @@ void _processRecvStatsMessage(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *nodeMsg)
packetsInInt=AQH_RecvStatsMsg_GetPacketsIn(nodeMsg);
if (packetsInInt) {
double packetsIn;
double errors;
double handled;
double errorsPercentage=0.0;
double handledPercentage=0.0;
double crcErrors;
double ioErrors;
double crcErrorsPercentage=0.0;
double ioErrorsPercentage=0.0;
packetsIn=(double) packetsInInt;
errors=(double)AQH_RecvStatsMsg_GetErrors(nodeMsg);
handled=(double)AQH_RecvStatsMsg_GetHandled(nodeMsg);
crcErrors=(double)AQH_RecvStatsMsg_GetCrcErrors(nodeMsg);
ioErrors=(double)AQH_RecvStatsMsg_GetIoErrors(nodeMsg);
errorsPercentage=errors*100.0/packetsIn;
handledPercentage=handled*100.0/packetsIn;
crcErrorsPercentage=crcErrors*100.0/packetsIn;
ioErrorsPercentage=ioErrors*100.0/packetsIn;
_publishInt(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/packetsIn", packetsInInt);
_publishInt(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/errors", (int) AQH_RecvStatsMsg_GetErrors(nodeMsg));
_publishInt(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/handled", (int) AQH_RecvStatsMsg_GetHandled(nodeMsg));
_publishDouble(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/errorsPercent", errorsPercentage);
_publishDouble(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/handledPercent", handledPercentage);
_publishInt(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/crcerrors", (int) AQH_RecvStatsMsg_GetCrcErrors(nodeMsg));
_publishInt(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/ioerrors", (int) AQH_RecvStatsMsg_GetIoErrors(nodeMsg));
_publishDouble(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/crcerrorsPercent", crcErrorsPercentage);
_publishDouble(ep, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/ioerrorsPercent", ioErrorsPercentage);
}
}

View File

@@ -17,7 +17,7 @@
static uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len);
static uint8_t _calcXorChecksum(const uint8_t *ptr, uint8_t len);
@@ -105,7 +105,7 @@ int AQH_NodeMsg_IsMsgComplete(const GWEN_MSG *msg)
int AQH_NodeMsg_IsChecksumValid(const GWEN_MSG *msg)
{
if (msg && AQH_NodeMsg_IsMsgComplete(msg))
return (_calcChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))==0)?1:0;
return (_calcXorChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg))==0)?1:0;
return 0;
}
@@ -116,7 +116,7 @@ int AQH_NodeMsg_AddChecksum(GWEN_MSG *msg)
if (msg) {
int rv;
rv=GWEN_Msg_AddByte(msg, _calcChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg)));
rv=GWEN_Msg_AddByte(msg, _calcXorChecksum(GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg)));
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
@@ -171,7 +171,7 @@ uint32_t AQH_NodeMsg_GetMsgGroup(uint8_t msgType)
uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len)
uint8_t _calcXorChecksum(const uint8_t *ptr, uint8_t len)
{
int i;
uint8_t x=0;
@@ -185,4 +185,26 @@ uint8_t _calcChecksum(const uint8_t *ptr, uint8_t len)
uint8_t _calcCrc8Checksum(const uint8_t *ptr, uint8_t len)
{
int i;
uint8_t x=0xff;
for (i=0; i<len; i++, ptr++) {
int j;
x^=*ptr;
for (j=0; j<8; j++) {
if (x & 0x80)
x=(uint8_t) (x<<1)^0x97;
else
x<<=1;
}
}
return x;
}

View File

@@ -21,10 +21,10 @@
#define AQH_MSG_OFFS_RECVSTATS_UID 0
#define AQH_MSG_OFFS_RECVSTATS_PACKETSIN 4
#define AQH_MSG_OFFS_RECVSTATS_ERRORS 6
#define AQH_MSG_OFFS_RECVSTATS_HANDLED 8
#define AQH_MSG_OFFS_RECVSTATS_CRCERRORS 6
#define AQH_MSG_OFFS_RECVSTATS_IOERRORS 8
#define AQH_MSG_RECVSTATS_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_HANDLED+2)
#define AQH_MSG_RECVSTATS_MINSIZE (AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_IOERRORS+2)
@@ -54,12 +54,12 @@ uint16_t AQH_RecvStatsMsg_GetPacketsIn(const GWEN_MSG *msg)
uint16_t AQH_RecvStatsMsg_GetErrors(const GWEN_MSG *msg)
uint16_t AQH_RecvStatsMsg_GetCrcErrors(const GWEN_MSG *msg)
{
if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_RECVSTATS_MINSIZE) {
const uint8_t *ptr;
ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_ERRORS;
ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_CRCERRORS;
return (uint16_t)(ptr[0])+(ptr[1]<<8);
}
return 0;
@@ -67,12 +67,12 @@ uint16_t AQH_RecvStatsMsg_GetErrors(const GWEN_MSG *msg)
uint16_t AQH_RecvStatsMsg_GetHandled(const GWEN_MSG *msg)
uint16_t AQH_RecvStatsMsg_GetIoErrors(const GWEN_MSG *msg)
{
if (GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_RECVSTATS_MINSIZE) {
const uint8_t *ptr;
ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_HANDLED;
ptr=GWEN_Msg_GetConstBuffer(msg)+AQH_MSG_OFFS_ALL_DATA_BEGIN+AQH_MSG_OFFS_RECVSTATS_IOERRORS;
return (uint16_t)(ptr[0])+(ptr[1]<<8);
}
return 0;
@@ -85,14 +85,14 @@ void AQH_RecvStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const
if ((AQH_NodeMsg_GetMsgType(msg)==AQH_MSG_TYPE_COMRECVSTATS) &&
(GWEN_Msg_GetBytesInBuffer(msg)>=AQH_MSG_RECVSTATS_MINSIZE)) {
GWEN_Buffer_AppendArgs(dbuf,
"0x%02x->0x%02x: RECVSTATS %s (uid=0x%08x, in=%d, errors=%d, handled=%d)\n",
"0x%02x->0x%02x: RECVSTATS %s (uid=0x%08x, in=%d, crc errors=%d, io errors=%d)\n",
AQH_NodeMsg_GetSourceAddress(msg),
AQH_NodeMsg_GetDestAddress(msg),
sText,
(unsigned int) AQH_RecvStatsMsg_GetUid(msg),
AQH_RecvStatsMsg_GetPacketsIn(msg),
AQH_RecvStatsMsg_GetErrors(msg),
AQH_RecvStatsMsg_GetHandled(msg));
AQH_RecvStatsMsg_GetCrcErrors(msg),
AQH_RecvStatsMsg_GetIoErrors(msg));
}
}

View File

@@ -20,8 +20,8 @@
AQHOME_API uint32_t AQH_RecvStatsMsg_GetUid(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_RecvStatsMsg_GetPacketsIn(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_RecvStatsMsg_GetErrors(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_RecvStatsMsg_GetHandled(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_RecvStatsMsg_GetCrcErrors(const GWEN_MSG *msg);
AQHOME_API uint16_t AQH_RecvStatsMsg_GetIoErrors(const GWEN_MSG *msg);
AQHOME_API void AQH_RecvStatsMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText);

View File

@@ -348,8 +348,8 @@ void _handleMsgComRecvStat(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *ep, c
DBG_INFO(AQH_LOGDOMAIN, "Error handling message");
}
AQH_NodeInfo_SetStatsPacketsIn(ni, AQH_RecvStatsMsg_GetPacketsIn(msg));
AQH_NodeInfo_SetStatsErrors(ni, AQH_RecvStatsMsg_GetErrors(msg));
AQH_NodeInfo_SetStatsHandled(ni, AQH_RecvStatsMsg_GetHandled(msg));
AQH_NodeInfo_SetStatsCrcErrors(ni, AQH_RecvStatsMsg_GetCrcErrors(msg));
AQH_NodeInfo_SetStatsIoErrors(ni, AQH_RecvStatsMsg_GetIoErrors(msg));
AQH_NodeDb_SetModified(xmgr->nodeDb);
}
}

View File

@@ -97,7 +97,14 @@
<flags></flags>
</member>
<member name="statsErrors" type="uint32_t" maxlen="4">
<member name="statsCrcErrors" type="uint32_t" maxlen="4">
<default>0</default>
<preset>0</preset>
<access>public</access>
<flags></flags>
</member>
<member name="statsIoErrors" type="uint32_t" maxlen="4">
<default>0</default>
<preset>0</preset>
<access>public</access>

View File

@@ -213,7 +213,7 @@ Offset Length Meaning
---------------------------------------------------------
4 4 UID of the sending node
8 2 packets in
10 2 errors
12 2 packets handled
10 2 crc errors
12 2 io errors

View File

@@ -221,6 +221,11 @@
.include "com.asm"
.include "comproto.asm"
.include "comproto_addr.asm"
.include "comproto_stats.asm"
.include "comproto_device.asm"
.include "comproto_values.asm"
;.include "comproto_debug.asm"
;.include "comproto_twi.asm"
.include "twimaster.asm"
.include "lcd.asm"
.include "si7021.asm"
@@ -328,12 +333,13 @@ onEveryMinute:
breq onEveryMinute_l1 ; no, do nothing
; will later send this only every hour or so
ldi r16, 0xff ; send stats to everybody
rcall CPRO_EnqueueComSendStats
ldi r16, 0xff ; send recv to everybody
rcall CPRO_EnqueueComRecvStats
ldi r16, 0xff ; send device info to everybody
rcall CPRO_EnqueueDevice
onEveryMinute_l1:

View File

@@ -62,11 +62,14 @@ comDataBegin:
comStatsPacketsOut: .byte 2
comStatsRecvErrs: .byte 2
comStatsRecvCrcErrs: .byte 2
comStatsCollisions: .byte 2
comStatsMissed: .byte 2
comStatsAborted: .byte 2
comStatsIgnored: .byte 2
comStatsHandled: .byte 2
comStatsNoBuffer: .byte 2
comRecvBuffersReadPos: .byte 1
comRecvBuffersWritePos: .byte 1
@@ -151,8 +154,8 @@ Com_Init:
in r16, GIMSK ; enable pin change irq PCIE0 or PCIE1
ori r16, (1<<COM_IRQ_GIMSK_ATTN)
out GIMSK, R16
in r16, GIFR ; clear pending irq
andi r16, ~(1<<COM_IRQ_GIFR_ATTN)
ldi r16, (1<<COM_IRQ_GIFR_ATTN) ; clear pending irq by writing 1 to ATTN bit
out GIFR, r16
sec
@@ -212,14 +215,11 @@ COM_EnqueuePacket:
; MODIFIED REGS: R16, R17 (R21)
COM_AllocBufferAndGetXY:
rcall COM_BufferAlloc ; (r16, r17, r21)
rcall COM_BufferAlloc ; (r16, r17, r21)
brcc COM_AllocBufferAndGetXY_error
mov xl, yl
mov xh, yh
ldi r16, COM_BUFFER_OFFS_DATA
clr r17
add xl, r16
adc xh, r17
adiw xh:xl, COM_BUFFER_OFFS_DATA
sec
ret
COM_AllocBufferAndGetXY_error:
@@ -304,659 +304,12 @@ comHandleReceivedPacket_l2:
; ---------------------------------------------------------------------------
; comSendPacketHandleRepeat
;
; IN:
; - Y: pointer to current buffer (pointed to by comRecvBuffersReadPos)
; OUT:
; - CFLAG: set if something done, can be called again immediately (otherwise: wait for timer interrupt and retry)
; MODIFIED REGS: R15, R17, R22, X (R16, R17, R18, R21, R22)
comSendPacketHandleRepeat:
in r15, SREG
cli
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 comSendPacketHandleRepeat_adjustRepeat ; jump if it is
ldd r16, y+COM_BUFFER_OFFS_FLAGS
rcall comSetupRepeat ; setup comRepeatCount if not already done (R16, R17)
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
rcall comSendPacketHandleBuffer ; (R16, R17, R21, R22)
cbi COM_DDR_ATTN, COM_PINNUM_ATTN ; release ATTN line (by setting direction to IN)
brcc comSendPacketHandleRepeat_collision
; packet sent, adjust stats, release buffer
ldi xl, LOW(comStatsPacketsOut)
ldi xh, HIGH(comStatsPacketsOut)
rcall comDeallocReadBufAndIncrCounter ; (r16, r17, r18)
rjmp comSendPacketHandleRepeat_retC
comSendPacketHandleRepeat_collision:
; increment collisions counter
ldi xl, LOW(comStatsCollisions)
ldi xh, HIGH(comStatsCollisions)
ldi r22, 1
ld r16, x+
ld r17, x
add r16, r22
dec r22
adc r17, r22
st x, r17
st -x, r16
comSendPacketHandleRepeat_adjustRepeat:
; decrement repeat counter (except for vital messages)
lds r17, comRepeatCount
cpi r17, COM_REPEAT_VITAL
breq comSendPacketHandleRepeat_retNC ; vital message, repeat forever
dec r17
sts comRepeatCount, r17
brne comSendPacketHandleRepeat_retNC
; dealloc buffer, inc abort counter
ldi xl, LOW(comStatsAborted)
ldi xh, HIGH(comStatsAborted)
rcall comDeallocReadBufAndIncrCounter
rjmp comSendPacketHandleRepeat_retC
.include "com_lowlevel.asm"
.include "com_buffer.asm"
.include "com_crc.asm"
.include "com_send.asm"
.include "com_recv.asm"
comSendPacketHandleRepeat_retNC:
out SREG, r15
clc
ret
comSendPacketHandleRepeat_retC:
out SREG, r15
sec
ret
; ---------------------------------------------------------------------------
; comSetupRepeat
;
; IN:
; - R16: priority
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17
comSetupRepeat:
lds r17, comRepeatCount
tst r17
brne comSetupRepeat_l99 ; comRepeatCount already setup
; set comRepeatCount according to priority
andi r16, (COM_BUFFER_FLAGS_PRIO1 | COM_BUFFER_FLAGS_PRIO0) ; r16: flags
cpi r16, COM_REPEAT_INFO
brne comSetupRepeat_l1
ldi r17, COM_REPEAT_INFO
rjmp comSetupRepeat_l98
comSetupRepeat_l1:
cpi r16, COM_REPEAT_NORMAL
brne comSetupRepeat_l2
ldi r17, COM_REPEAT_NORMAL
rjmp comSetupRepeat_l98
comSetupRepeat_l2:
cpi r16, COM_REPEAT_IMPORTANT
brne comSetupRepeat_l3
ldi r17, COM_REPEAT_IMPORTANT
rjmp comSetupRepeat_l98
comSetupRepeat_l3:
ldi r17, COM_REPEAT_VITAL
comSetupRepeat_l98:
sts comRepeatCount, r17
comSetupRepeat_l99:
ret
; ---------------------------------------------------------------------------
; comDeallocReadBufAndIncrCounter
;
; IN:
; - X : pointer to counter
; OUT:
; - nothing
; REGS: r16, r17, r21
comDeallocReadBufAndIncrCounter:
rcall COM_BufferDeallocFront ; (r16, r17, r21)
ldi r21, 1
ld r16, x+
ld r17, x
add r16, r21
dec r21
adc r17, r21
st x, r17
st -x, r16
sts comRepeatCount, r21 ; set comRepeatCount to zero
ret
; ---------------------------------------------------------------------------
; comSendPacketHandleBuffer
;
; Send a packet from the current read buffer (pointed to by comRecvBuffersReadPos).
; On success the flag COM_BUFFER_FLAGS_DONE is set in the buffer.
; The buffer is not released.
;
; CAVE: Expects interrupts to be disabled!
;
; IN:
; - Y : pointer to current read buffer
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17 (R21, R22)
comSendPacketHandleBuffer:
mov xl, yl
mov xh, yh
ldi r16, COM_BUFFER_OFFS_DATA
clr r17
add xl, r16
adc xh, r17
ldd r16, y+(COM_BUFFER_OFFS_DATA+COM_MSG_OFFS_MSGLEN) ; get msg payload length
ldi r17, 3
add r16, r17 ; add dest addr, msg len and XOR byte
rcall comSendPacketFromSram ; send all that
brcc comSendPacketHandleBuffer_error
ldd r16, y+COM_BUFFER_OFFS_FLAGS
ori r16, COM_BUFFER_FLAGS_DONE
std y+COM_BUFFER_OFFS_FLAGS, r16
sec
ret
comSendPacketHandleBuffer_error:
clc
ret
; ---------------------------------------------------------------------------
; comSendPacketFromSram
;
; Send a packet from SRAM
;
; IN:
; - R16: number of bytes to send
; - X: pointer to buffer to read from
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17 (R21, R22)
comSendPacketFromSram:
mov r17, r16
comSendPacketFromSram_loop:
ld r16, X+
rcall comSendByte ; send byte (R16, R21, R22)
brcc comSendPacketFromSram_error
dec r17
brne comSendPacketFromSram_loop
sec
ret
comSendPacketFromSram_error:
clc
ret
; ---------------------------------------------------------------------------
; comReceivePacketHandleBuffer
;
; Allocate a buffer and receive a packet into it.
; On success the buffer flags will have COM_BUFFER_FLAGS_RECEIVED set.
; On error the buffer will be deallocated.
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, cleared otherwise
; MODIFIED REGISTERS: R16, R17, X, Y (R1, R18, R19, R20, R21, R22)
comReceivePacketHandleBuffer:
rcall COM_BufferAlloc
brcc comReceivePacketHandleBuffer_error
; get pos of data portion for the allocated buffer
mov xl, yl
mov xh, yh
ldi r16, COM_BUFFER_OFFS_DATA
clr r17
add xl, r16
adc xh, r17
rcall comReceivePacketToSram ; (r1, r16, r17, R20, R21, R22, X)
brcc comReceivePacketHandleBuffer_dealloc
; handle buffer flags
ldi r16, COM_BUFFER_FLAGS_RECEIVED
std y+COM_BUFFER_OFFS_FLAGS, r16
ldi xl, LOW(comStatsPacketsIn)
ldi xh, HIGH(comStatsPacketsIn)
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
sec
ret
comReceivePacketHandleBuffer_dealloc:
push r16
rcall COM_BufferDeallocBack ; (r16, r17, r21)
pop r16
cpi r16, COM_ERR_NOTFORME ; packet just not for me?
breq comReceivePacketHandleBuffer_notforme ; correct, don't count as error
ldi xl, LOW(comStatsRecvErrs)
ldi xh, HIGH(comStatsRecvErrs)
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
comReceivePacketHandleBuffer_error:
clc
ret
comReceivePacketHandleBuffer_notforme: ; TODO: may count packets not for me?
clc
ret
; ---------------------------------------------------------------------------
; comReceivePacketToSram
;
; Receive a packet to the given SRAM position.
; IN:
; - X: pointer to start of buffer to receive bytes
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r1, r16, r17, X (R20, R21, R22)
comReceivePacketToSram:
clr r1 ; r1: checksum
; read destination address
rcall comReceiveByte ; read byte (R16, R17, R20, R21, R22)
brcc comReceivePacketToSram_error
eor r1, r16
; compare destination address (accept "0" and own address)
tst r16
breq comReceivePacketToSram_acceptAddr
cpi r16, 0xff
breq comReceivePacketToSram_acceptAddr
lds r17, comAddress
cp r16, r17
breq comReceivePacketToSram_acceptAddr
ldi r16, COM_ERR_NOTFORME
clc ; not for me
ret
comReceivePacketToSram_acceptAddr:
st X+, r16
; read msg length
rcall comReceiveByte ; read packet length (R16, R17, R20, R21, R22)
brcc comReceivePacketToSram_error
eor r1, r16
st X+, r16
cpi r16, (COM_BUFFER_SIZE-3-COM_BUFFER_OFFS_DATA)+1
brcc comReceivePacketToSram_error ; packet too long (TODO: count overruns)
tst r16
breq comReceivePacketToSram_readXOR
mov r17, r16
comReceivePacketToSram_loop:
push r17
rcall comReceiveByte ; read byte (R16, R17, R20, R21, R22)
pop r17
brcc comReceivePacketToSram_error
st X+, r16
eor r1, r16
dec r17
brne comReceivePacketToSram_loop
comReceivePacketToSram_readXOR:
rcall comReceiveByte ; XOR byte (R16, R17, R20, R21, R22)
brcc comReceivePacketToSram_error
st X+, r16
eor r1, r16
brne comReceivePacketToSram_error
sec
ret
comReceivePacketToSram_crcError:
ldi r16, COM_ERR_CHECKSUM
clc
ret
comReceivePacketToSram_error:
ldi r16, COM_ERR_IO
clc
ret
; ***************************************************************************
; lowlevel pin functions
; ---------------------------------------------------------------------------
; comSendByte
;
; 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
comSendByte:
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
comSendByte_loop: ; 9 for low bit
lsr r16 ; 1+ bit to send -> CARRY
brcs comSendByte_setHigh ; HI: +2, LO: +1
comSendByte_setLow:
sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output
Utils_WaitNanoSecs COM_BIT_LENGTH, 9, r22
rjmp comSendByte_loopEnd ; +2
comSendByte_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 comSendByte_error ; +2 if error (collision: we wanted line to be high but it is low)
Utils_WaitNanoSecs COM_BIT_LENGTH/2, 0, r22
comSendByte_loopEnd:
dec r21 ; +1
brne comSendByte_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
comSendByte_error:
clc
ret
; ---------------------------------------------------------------------------
; comReceiveByte
;
; 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)
comReceiveByte:
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 comWaitForDataLow ; (R17)
brcc comReceiveByte_error
Utils_WaitNanoSecs COM_BIT_LENGTH/2, 5, r22 ; goto middle of startbit to maximize sync stability
comReceiveByte_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 comReceiveByte_shiftIn ; HIGH: +2, rjmp, use set CFLAG
clc ; LOW: +1
comReceiveByte_shiftIn:
ror r20 ; +1
dec r21 ; +1
brne comReceiveByte_loop ; +2, sum per loop: 8 cycles
rcall comWaitForDataHigh ; wait for start of stopbit
brcc comReceiveByte_error
mov r16, r20
sec
ret
comReceiveByte_error:
clc
ret
; ---------------------------------------------------------------------------
; comWaitForDataLow
;
; Waits up to COM_MAXWAIT loops for low data line
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22
comWaitForDataLow:
ldi r17, COM_MAXWAIT
comWaitForDataLow_loop:
sbis COM_PIN_DATA, COM_PINNUM_DATA
rjmp comWaitForDataLow_done
Utils_WaitNanoSecs 100, 0, r22 ; wait for 100 nanosecs
dec r17
brne comWaitForDataLow_loop
clc ; timeout
ret
comWaitForDataLow_done:
sec ; ok
ret
; ---------------------------------------------------------------------------
; comWaitForDataHigh
;
; Waits up to COM_MAXWAIT loops for high data line
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22, X
comWaitForDataHigh:
ldi r17, COM_MAXWAIT
comWaitForDataHigh_loop:
sbic COM_PIN_DATA, COM_PINNUM_DATA
rjmp comWaitForDataHigh_done
Utils_WaitNanoSecs 100, 0, r22 ; wait for 100 nanosecs
dec r17
brne comWaitForDataHigh_loop
clc ; timeout
ret
comWaitForDataHigh_done:
sec ; ok
ret
; ---------------------------------------------------------------------------
; comWaitForAttnHigh
;
; Waits up to COM_MAXWAIT loops for high ATTN line
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22, X
comWaitForAttnHigh:
ldi r17, COM_MAXWAIT
comWaitForAttnHigh_loop:
sbic COM_PIN_ATTN, COM_PINNUM_ATTN
rjmp comWaitForAttnHigh_done
Utils_WaitNanoSecs 100, 0, r22 ; wait for 100 nanosecs
dec r17
brne comWaitForAttnHigh_loop
clc ; timeout
ret
comWaitForAttnHigh_done:
sec ; ok
ret
; ***************************************************************************
; buffer functions
; ---------------------------------------------------------------------------
; COM_BufferAlloc
;
; Allocate a transfer buffer.
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, clear otherwise
; - Y: pointer to allocated buffer in SRAM
; MODIFIED REGISTERS: r16, r17, r21
COM_BufferAlloc:
in r21, SREG ; save global interrupt enable bit from SREG
cli
lds r17, comRecvBuffersUsed
cpi r17, COM_BUFFER_NUM
brcc COM_AllocBuffer_error ; no buffer available
inc r17 ; increment number of buffers used
sts comRecvBuffersUsed, r17 ; store new value
lds r16, comRecvBuffersWritePos ; get current write pos
mov r17, r16 ; increment for next call
inc r17
cpi r17, COM_BUFFER_NUM ; CF set if COM_BUFFER_NUM > R17
brcs COM_AllocBuffer_l1
clr r17 ; wraparound
COM_AllocBuffer_l1:
sts comRecvBuffersWritePos, r17 ; store new writepos for next caller
rcall COM_BufferPosToY ; (R16, R17)
clr r17
std y+COM_BUFFER_OFFS_FLAGS, r17 ; preset flags
out SREG, r21 ; restore global interrupt enable bit in SREG
sec
ret
COM_AllocBuffer_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM_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
COM_BufferDeallocBack:
in r21, SREG ; save global interrupt enable bit from SREG
cli
lds r17, comRecvBuffersUsed
tst r17
breq COM_BufferDeallocBack_error ; no buffer allocated, nothing to release
dec r17
sts comRecvBuffersUsed, r17 ; store new value
lds r17, comRecvBuffersWritePos
tst r17 ; 0?
brne COM_BufferDeallocBack_l1 ; nope go directly decrement r17
ldi r17, COM_BUFFER_NUM ; wrap-around
COM_BufferDeallocBack_l1:
dec r17
sts comRecvBuffersWritePos, r17
out SREG, r21
sec
ret
COM_BufferDeallocBack_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM_BufferDeallocFront
;
; Release a transfer buffer by increasing the read pos.
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r16, r17, r21
COM_BufferDeallocFront:
in r21, SREG ; save global interrupt enable bit from SREG
cli
lds r17, comRecvBuffersUsed
tst r17
breq COM_BufferDeallocFront_error ; no buffer allocated, nothing to release
dec r17
sts comRecvBuffersUsed, r17 ; store new value
lds r17, comRecvBuffersReadPos
inc r17
cpi r17, COM_BUFFER_NUM
brcs COM_BufferDeallocFront_l1
clr r17 ; wrap-around
COM_BufferDeallocFront_l1:
sts comRecvBuffersReadPos, r17
out SREG, r21
sec
ret
COM_BufferDeallocFront_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM_BufferPosToY
;
; Get a pointer to the SRAM position of the given buffer.
; CAVE: Code must correspond to COM_BUFFER_SIZE!!
; IN:
; - R16: buffer number (starting with 0)
; OUT:
; - Y: pointer to buffer in SRAM
; MODIFIED REGISTERS: R16, R17
COM_BufferPosToY:
; calculate offset
clr r17
mov yl, r16
clr yh
lsl yl
rol yh ; *2
add yl, r16
adc yh, r17 ; *3
lsl yl
rol yh ; *6
lsl yl
rol yh ; *12
lsl yl
rol yh ; *24
; add base position of buffers
ldi r16, LOW(comRecvBuffers)
ldi r17, HIGH(comRecvBuffers)
add yl, r16
adc yh, r17
ret

163
avr/com_buffer.asm Normal file
View File

@@ -0,0 +1,163 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; COM_BufferAlloc
;
; Allocate a transfer buffer.
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, clear otherwise
; - Y: pointer to allocated buffer in SRAM
; MODIFIED REGISTERS: r16, r17, r21
COM_BufferAlloc:
in r21, SREG ; save global interrupt enable bit from SREG
cli
lds r17, comRecvBuffersUsed
cpi r17, COM_BUFFER_NUM
brcc COM_AllocBuffer_error ; no buffer available
inc r17 ; increment number of buffers used
sts comRecvBuffersUsed, r17 ; store new value
lds r16, comRecvBuffersWritePos ; get current write pos
mov r17, r16 ; increment for next call
inc r17
cpi r17, COM_BUFFER_NUM ; CF set if COM_BUFFER_NUM > R17
brcs COM_AllocBuffer_l1
clr r17 ; wraparound
COM_AllocBuffer_l1:
sts comRecvBuffersWritePos, r17 ; store new writepos for next caller
rcall COM_BufferPosToY ; (R16, R17)
clr r17
std y+COM_BUFFER_OFFS_FLAGS, r17 ; preset flags
out SREG, r21 ; restore global interrupt enable bit in SREG
sec
ret
COM_AllocBuffer_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM_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
COM_BufferDeallocBack:
in r21, SREG ; save global interrupt enable bit from SREG
cli
lds r17, comRecvBuffersUsed
tst r17
breq COM_BufferDeallocBack_error ; no buffer allocated, nothing to release
dec r17
sts comRecvBuffersUsed, r17 ; store new value
lds r17, comRecvBuffersWritePos
tst r17 ; 0?
brne COM_BufferDeallocBack_l1 ; nope go directly decrement r17
ldi r17, COM_BUFFER_NUM ; wrap-around
COM_BufferDeallocBack_l1:
dec r17
sts comRecvBuffersWritePos, r17
out SREG, r21
sec
ret
COM_BufferDeallocBack_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM_BufferDeallocFront
;
; Release a transfer buffer by increasing the read pos.
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r16, r17, r21
COM_BufferDeallocFront:
in r21, SREG ; save global interrupt enable bit from SREG
cli
lds r17, comRecvBuffersUsed
tst r17
breq COM_BufferDeallocFront_error ; no buffer allocated, nothing to release
dec r17
sts comRecvBuffersUsed, r17 ; store new value
lds r17, comRecvBuffersReadPos
inc r17
cpi r17, COM_BUFFER_NUM
brcs COM_BufferDeallocFront_l1
clr r17 ; wrap-around
COM_BufferDeallocFront_l1:
sts comRecvBuffersReadPos, r17
out SREG, r21
sec
ret
COM_BufferDeallocFront_error:
out SREG, r21
clc
ret
; ---------------------------------------------------------------------------
; COM_BufferPosToY
;
; Get a pointer to the SRAM position of the given buffer.
; CAVE: Code must correspond to COM_BUFFER_SIZE!!
; IN:
; - R16: buffer number (starting with 0)
; OUT:
; - Y: pointer to buffer in SRAM
; MODIFIED REGISTERS: R16, R17
COM_BufferPosToY:
; calculate offset
clr r17
mov yl, r16
clr yh
lsl yl
rol yh ; *2
add yl, r16
adc yh, r17 ; *3
lsl yl
rol yh ; *6
lsl yl
rol yh ; *12
lsl yl
rol yh ; *24
; add base position of buffers
ldi r16, LOW(comRecvBuffers)
ldi r17, HIGH(comRecvBuffers)
add yl, r16
adc yh, r17
ret

110
avr/com_crc.asm Normal file
View File

@@ -0,0 +1,110 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; add checksum byte to buffer
;
; IN:
; - X : pointer to packet buffer
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R18, X
comCalcAndAddChecksumByte:
adiw xh:xl, COM_MSG_OFFS_MSGLEN
ld r18, X ; read msg len
inc r18 ; account for dest address
inc r18 ; account for msg len bytes
sbiw xh:xl, COM_MSG_OFFS_MSGLEN
rcall cproCalcXor ; (R16, R17, R18, 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, X
cproCheckMessageInBuffer:
adiw xh:xl, COM_MSG_OFFS_MSGLEN
ld r18, X ; read msg len
inc r18 ; account for dest address
inc r18 ; account for msg len bytes
sbiw xh:xl, COM_MSG_OFFS_MSGLEN
rcall cproCalcXor ; (R16, R17, R18, X)
ld r17, X
cp r16,r17 ; should be equal
brne cproCheckMessageInBuffer_error
sec
ret
cproCheckMessageInBuffer_error:
clc
ret
; ---------------------------------------------------------------------------
; calc xor checksum
;
; IN:
; - X : pointer to data to calc crc8 for
; - r18: number of bytes to calc crc8 for
; OUT:
; - r16: xor checksum
; - X : point directly behind the checked area
; MODIFIED REGS: R16, R17, R18, X
cproCalcXor:
clr r16
cproCalcXor_loop1:
ld r17, X+
eor r16, r17
dec r18
brne cproCalcXor_loop1
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
cproCalcCrc8:
ldi r16, 0xff ; crc
cproCalcCrc8_loop1:
ld r17, X+ ; running var
eor r16, r17
ldi r20, 8 ; counter for loop2
cproCalcCrc8_loop2:
lsl r16
brcc cproCalcCrc8_l1
eor r16, r19
cproCalcCrc8_l1:
dec r20
brne cproCalcCrc8_loop2
dec r18
brne cproCalcCrc8_loop1
ret

183
avr/com_lowlevel.asm Normal file
View File

@@ -0,0 +1,183 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; comSendByte
;
; 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
comSendByte:
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
comSendByte_loop: ; 9 for low bit
lsr r16 ; 1+ bit to send -> CARRY
brcs comSendByte_setHigh ; HI: +2, LO: +1
comSendByte_setLow:
sbi COM_DDR_DATA, COM_PINNUM_DATA ; +2 set DATA as output
Utils_WaitNanoSecs COM_BIT_LENGTH, 9, r22
rjmp comSendByte_loopEnd ; +2
comSendByte_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 comSendByte_error ; +2 if error (collision: we wanted line to be high but it is low)
Utils_WaitNanoSecs COM_BIT_LENGTH/2, 0, r22
comSendByte_loopEnd:
dec r21 ; +1
brne comSendByte_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
comSendByte_error:
clc
ret
; ---------------------------------------------------------------------------
; comReceiveByte
;
; 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)
comReceiveByte:
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 comWaitForDataLow ; (R17)
brcc comReceiveByte_error
Utils_WaitNanoSecs COM_BIT_LENGTH/2, 5, r22 ; goto middle of startbit to maximize sync stability
comReceiveByte_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 comReceiveByte_shiftIn ; HIGH: +2, rjmp, use set CFLAG
clc ; LOW: +1
comReceiveByte_shiftIn:
ror r20 ; +1
dec r21 ; +1
brne comReceiveByte_loop ; +2, sum per loop: 8 cycles
rcall comWaitForDataHigh ; wait for start of stopbit
brcc comReceiveByte_error
mov r16, r20
sec
ret
comReceiveByte_error:
clc
ret
; ---------------------------------------------------------------------------
; comWaitForDataLow
;
; Waits up to COM_MAXWAIT loops for low data line
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22
comWaitForDataLow:
ldi r17, COM_MAXWAIT
comWaitForDataLow_loop:
sbis COM_PIN_DATA, COM_PINNUM_DATA
rjmp comWaitForDataLow_done
Utils_WaitNanoSecs 100, 0, r22 ; wait for 100 nanosecs
dec r17
brne comWaitForDataLow_loop
clc ; timeout
ret
comWaitForDataLow_done:
sec ; ok
ret
; ---------------------------------------------------------------------------
; comWaitForDataHigh
;
; Waits up to COM_MAXWAIT loops for high data line
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22, X
comWaitForDataHigh:
ldi r17, COM_MAXWAIT
comWaitForDataHigh_loop:
sbic COM_PIN_DATA, COM_PINNUM_DATA
rjmp comWaitForDataHigh_done
Utils_WaitNanoSecs 100, 0, r22 ; wait for 100 nanosecs
dec r17
brne comWaitForDataHigh_loop
clc ; timeout
ret
comWaitForDataHigh_done:
sec ; ok
ret
; ---------------------------------------------------------------------------
; comWaitForAttnHigh
;
; Waits up to COM_MAXWAIT loops for high ATTN line
; IN:
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r17, r22, X
comWaitForAttnHigh:
ldi r17, COM_MAXWAIT
comWaitForAttnHigh_loop:
sbic COM_PIN_ATTN, COM_PINNUM_ATTN
rjmp comWaitForAttnHigh_done
Utils_WaitNanoSecs 100, 0, r22 ; wait for 100 nanosecs
dec r17
brne comWaitForAttnHigh_loop
clc ; timeout
ret
comWaitForAttnHigh_done:
sec ; ok
ret

147
avr/com_recv.asm Normal file
View File

@@ -0,0 +1,147 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; comReceivePacketHandleBuffer
;
; Allocate a buffer and receive a packet into it.
; On success the buffer flags will have COM_BUFFER_FLAGS_RECEIVED set.
; On error the buffer will be deallocated.
; IN:
; - nothing
; OUT:
; - CFLAG: set if okay, cleared otherwise
; MODIFIED REGISTERS: R16, R17, X, Y (R1, R18, R19, R20, R21, R22)
comReceivePacketHandleBuffer:
rcall COM_BufferAlloc
brcs comReceivePacketHandleBuffer_haveBuffer
ldi xl, LOW(comStatsNoBuffer)
ldi xh, HIGH(comStatsNoBuffer)
rjmp comReceivePacketHandleBuffer_errorWithCounter
comReceivePacketHandleBuffer_haveBuffer:
; get pos of data portion for the allocated buffer
mov xl, yl
mov xh, yh
adiw xh:xl, COM_BUFFER_OFFS_DATA
rcall comReceivePacketToXandCheck ; (r16, r17, r18, r20, r21, r22, X)
brcs comReceivePacketHandleBuffer_haveMessage
push r16
rcall COM_BufferDeallocBack ; (r16, r17, r21)
pop r16
cpi r16, COM_ERR_NOTFORME ; packet just not for me?
breq comReceivePacketHandleBuffer_notforme ; correct, don't count as error
ldi xl, LOW(comStatsRecvCrcErrs)
ldi xh, HIGH(comStatsRecvCrcErrs)
cpi r16, COM_ERR_CHECKSUM
breq comReceivePacketHandleBuffer_errorWithCounter
ldi xl, LOW(comStatsRecvErrs) ; generic error
ldi xh, HIGH(comStatsRecvErrs)
rjmp comReceivePacketHandleBuffer_errorWithCounter
comReceivePacketHandleBuffer_haveMessage:
; handle buffer flags
ldi r16, COM_BUFFER_FLAGS_RECEIVED
std y+COM_BUFFER_OFFS_FLAGS, r16
ldi xl, LOW(comStatsPacketsIn)
ldi xh, HIGH(comStatsPacketsIn)
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
sec
ret
comReceivePacketHandleBuffer_errorWithCounter:
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
comReceivePacketHandleBuffer_notforme:
clc
ret
; ---------------------------------------------------------------------------
; comReceivePacketToXandCheck
;
; Receive a packet to the given SRAM position.
; IN:
; - X: pointer to start of buffer to receive bytes
; OUT:
; - CFLAG: set if okay, clear otherwise
; - R16: error code if CFLAG cleared
; MODIFIED REGISTERS: r16, r17, r18, X (r20, r21, r22)
comReceivePacketToXandCheck:
push xh
push xl
rcall comReceivePacketToSram ; (r16, r17, R20, R21, R22, X)
pop xl
pop xh
brcc comReceivePacketToXandCheck_error
rcall cproCheckMessageInBuffer ; (R16, R17, R18, X)
ldi r16, COM_ERR_CHECKSUM
brcc comReceivePacketToXandCheck_error
sec
ret
comReceivePacketToXandCheck_error:
clc
ret
; ---------------------------------------------------------------------------
; comReceivePacketToSram
;
; Receive a packet to the given SRAM position.
; IN:
; - X: pointer to start of buffer to receive bytes
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGISTERS: r16, r17, X (R20, R21, R22)
comReceivePacketToSram:
; read destination address
rcall comReceiveByte ; read byte (R16, R17, R20, R21, R22)
brcc comReceivePacketToSram_error
; compare destination address (accept "00", "FF" and own address)
tst r16
breq comReceivePacketToSram_acceptAddr
cpi r16, 0xff
breq comReceivePacketToSram_acceptAddr
lds r17, comAddress
cp r16, r17
breq comReceivePacketToSram_acceptAddr
ldi r16, COM_ERR_NOTFORME
clc ; not for me
ret
comReceivePacketToSram_acceptAddr:
st X+, r16 ; store dest address
; read msg length
rcall comReceiveByte ; read packet length (R16, R17, R20, R21, R22)
brcc comReceivePacketToSram_error
st X+, r16
cpi r16, (COM_BUFFER_SIZE-3-COM_BUFFER_OFFS_DATA)+1
brcc comReceivePacketToSram_error ; packet too long (TODO: count overruns)
inc r16 ; account for checksum byte
mov r17, r16
comReceivePacketToSram_loop:
push r17
rcall comReceiveByte ; read byte (R16, R17, R20, R21, R22)
pop r17
brcc comReceivePacketToSram_error
st X+, r16
dec r17
brne comReceivePacketToSram_loop
sec
ret
comReceivePacketToSram_error:
ldi r16, COM_ERR_IO
clc
ret

197
avr/com_send.asm Normal file
View File

@@ -0,0 +1,197 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; comSendPacketHandleRepeat
;
; IN:
; - Y: pointer to current buffer (pointed to by comRecvBuffersReadPos)
; OUT:
; - CFLAG: set if something done, can be called again immediately (otherwise: wait for timer interrupt and retry)
; MODIFIED REGS: R15, R17, R22, X (R16, R17, R18, R21, R22)
comSendPacketHandleRepeat:
in r15, SREG
cli
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 comSendPacketHandleRepeat_adjustRepeat ; jump if it is
ldd r16, y+COM_BUFFER_OFFS_FLAGS
rcall comSetupRepeat ; setup comRepeatCount if not already done (R16, R17)
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
rcall comSendPacketHandleBuffer ; (R16, R17, R21, R22)
cbi COM_DDR_ATTN, COM_PINNUM_ATTN ; release ATTN line (by setting direction to IN)
brcc comSendPacketHandleRepeat_collision
; packet sent, adjust stats, release buffer
ldi xl, LOW(comStatsPacketsOut)
ldi xh, HIGH(comStatsPacketsOut)
rcall comDeallocReadBufAndIncrCounter ; (r16, r17, r18)
rjmp comSendPacketHandleRepeat_retC
comSendPacketHandleRepeat_collision:
; increment collisions counter
ldi xl, LOW(comStatsCollisions)
ldi xh, HIGH(comStatsCollisions)
rcall Utils_IncrementCounter16 ; (r18, r19, 22)
comSendPacketHandleRepeat_adjustRepeat:
; decrement repeat counter (except for vital messages)
lds r17, comRepeatCount
cpi r17, COM_REPEAT_VITAL
breq comSendPacketHandleRepeat_retNC ; vital message, repeat forever
dec r17
sts comRepeatCount, r17
brne comSendPacketHandleRepeat_retNC
; dealloc buffer, inc abort counter
ldi xl, LOW(comStatsAborted)
ldi xh, HIGH(comStatsAborted)
rcall comDeallocReadBufAndIncrCounter
rjmp comSendPacketHandleRepeat_retC
comSendPacketHandleRepeat_retNC:
out SREG, r15
clc
ret
comSendPacketHandleRepeat_retC:
out SREG, r15
sec
ret
; ---------------------------------------------------------------------------
; comSetupRepeat
;
; IN:
; - R16: priority
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17
comSetupRepeat:
lds r17, comRepeatCount
tst r17
brne comSetupRepeat_l99 ; comRepeatCount already setup
; set comRepeatCount according to priority
andi r16, (COM_BUFFER_FLAGS_PRIO1 | COM_BUFFER_FLAGS_PRIO0) ; r16: flags
cpi r16, COM_REPEAT_INFO
brne comSetupRepeat_l1
ldi r17, COM_REPEAT_INFO
rjmp comSetupRepeat_l98
comSetupRepeat_l1:
cpi r16, COM_REPEAT_NORMAL
brne comSetupRepeat_l2
ldi r17, COM_REPEAT_NORMAL
rjmp comSetupRepeat_l98
comSetupRepeat_l2:
cpi r16, COM_REPEAT_IMPORTANT
brne comSetupRepeat_l3
ldi r17, COM_REPEAT_IMPORTANT
rjmp comSetupRepeat_l98
comSetupRepeat_l3:
ldi r17, COM_REPEAT_VITAL
comSetupRepeat_l98:
sts comRepeatCount, r17
comSetupRepeat_l99:
ret
; ---------------------------------------------------------------------------
; comSendPacketHandleBuffer
;
; Send a packet from the current read buffer (pointed to by comRecvBuffersReadPos).
; On success the flag COM_BUFFER_FLAGS_DONE is set in the buffer.
; The buffer is not released.
;
; CAVE: Expects interrupts to be disabled!
;
; IN:
; - Y : pointer to current read buffer
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17 (R21, R22)
comSendPacketHandleBuffer:
mov xl, yl
mov xh, yh
ldi r16, COM_BUFFER_OFFS_DATA
clr r17
add xl, r16
adc xh, r17
ldd r16, y+(COM_BUFFER_OFFS_DATA+COM_MSG_OFFS_MSGLEN) ; get msg payload length
ldi r17, 3
add r16, r17 ; add dest addr, msg len and XOR byte
rcall comSendPacketFromSram ; send all that
brcc comSendPacketHandleBuffer_error
ldd r16, y+COM_BUFFER_OFFS_FLAGS
ori r16, COM_BUFFER_FLAGS_DONE
std y+COM_BUFFER_OFFS_FLAGS, r16
sec
ret
comSendPacketHandleBuffer_error:
clc
ret
; ---------------------------------------------------------------------------
; comSendPacketFromSram
;
; Send a packet from SRAM
;
; IN:
; - R16: number of bytes to send
; - X: pointer to buffer to read from
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17 (R21, R22)
comSendPacketFromSram:
mov r17, r16
comSendPacketFromSram_loop:
ld r16, X+
rcall comSendByte ; send byte (R16, R21, R22)
brcc comSendPacketFromSram_error
dec r17
brne comSendPacketFromSram_loop
sec
ret
comSendPacketFromSram_error:
clc
ret
; ---------------------------------------------------------------------------
; comDeallocReadBufAndIncrCounter
;
; IN:
; - X : pointer to counter
; OUT:
; - nothing
; REGS: r16, r17, r21
comDeallocReadBufAndIncrCounter:
rcall COM_BufferDeallocFront ; (r16, r17, r21)
ldi r21, 1
ld r16, x+
ld r17, x
add r16, r21
dec r21
adc r17, r21
st x, r17
st -x, r16
sts comRepeatCount, r21 ; set comRepeatCount to zero
ret

56
avr/com_twi.asm Normal file
View File

@@ -0,0 +1,56 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; Enqueue a TWI Bus Member packet.
;
; IN:
; - R16: destination address
; - R1 : Address of the bus member
; - R2 : availability (0=not available, 1=available)
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)
CPRO_EnqueueTwiBusMember:
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
pop r16
brcc CPRO_EnqueueTwiBusMember_error
clr r17 ; r17: XOR byte
; write header (dest address, msg length)
st X+, r16 ; destination address
eor r17, r16
ldi r16, 4 ; 4 bytes payload
st X+, r16
eor r17, r16
; write payload
ldi r16, CPRO_CMD_TWIBUSMEMBER ; send command
st X+, r16
eor r17, r16
lds r16, comAddress ; send source address
st X+, r16
eor r17, r16
mov r16, r1 ; send i2c bus member address
st X+, r16
eor r17, r16
mov r16, r2 ; send i2c bus member availability
st X+, r16
eor r17, r16
; store XOR byte
st X+, r17
; mark buffer as enqueued with PRIO "info" (limited amount of retries)
ldi r20, COM_BUFFER_PRIO_INFO
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueTwiBusMember_error
sec
ret
CPRO_EnqueueTwiBusMember_error:
clc
ret

View File

@@ -187,10 +187,6 @@ cproHandlePing_notHandled:
; ---------------------------------------------------------------------------
; Enqueue a PING packet.
;
@@ -223,443 +219,6 @@ CPRO_EnqueuePong:
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; Enqueue a COM Send stats packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)
CPRO_EnqueueComSendStats:
push r15
in r15, SREG
cli
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
pop r16
brcc CPRO_EnqueueComSendStats_error
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM) ; seconds + 6 bytes payload
ldi r18, CPRO_CMD_COMSENDSTATS
push xh
push xl
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
lds r16, comStatsPacketsOut ; 6: packets out
st X+, r16
lds r16, comStatsPacketsOut+1
st X+, r16
lds r16, comStatsCollisions ; 8: collisions
st X+, r16
lds r16, comStatsCollisions+1
st X+, r16
lds r16, comStatsAborted ; 10: aborted
st X+, r16
lds r16, comStatsAborted+1
st X+, r16
pop xl
pop xh
rcall cproCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "important" (higher retry count)
ldi r20, COM_BUFFER_PRIO_IMPORTANT
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueComSendStats_error
pop r15
out SREG, r15
sec
ret
CPRO_EnqueueComSendStats_error:
pop r15
out SREG, r15
clc
ret
#endif
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; Enqueue a COM reception stats packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)
CPRO_EnqueueComRecvStats:
push r15
in r15, SREG
cli
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
pop r16
brcc CPRO_EnqueueComRecvStats_error
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM) ; seconds + 6 bytes payload
ldi r18, CPRO_CMD_COMRECVSTATS
push xh
push xl
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
lds r16, comStatsPacketsIn ; 6: packets in
st X+, r16
lds r16, comStatsPacketsIn+1
st X+, r16
lds r16, comStatsRecvErrs ; 8: errors
st X+, r16
lds r16, comStatsRecvErrs+1
st X+, r16
lds r16, comStatsHandled ; 10: handled
st X+, r16
lds r16, comStatsHandled+1
st X+, r16
pop xl
pop xh
rcall cproCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "important" (higher retry count)
ldi r20, COM_BUFFER_PRIO_IMPORTANT
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueComRecvStats_error
pop r15
out SREG, r15
sec
ret
CPRO_EnqueueComRecvStats_error:
pop r15
out SREG, r15
clc
ret
#endif
#ifdef WITH_DEBUG
# ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; Enqueue a TWI Bus Member packet.
;
; IN:
; - R16: destination address
; - R1 : Address of the bus member
; - R2 : availability (0=not available, 1=available)
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)
CPRO_EnqueueTwiBusMember:
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
pop r16
brcc CPRO_EnqueueTwiBusMember_error
clr r17 ; r17: XOR byte
; write header (dest address, msg length)
st X+, r16 ; destination address
eor r17, r16
ldi r16, 4 ; 4 bytes payload
st X+, r16
eor r17, r16
; write payload
ldi r16, CPRO_CMD_TWIBUSMEMBER ; send command
st X+, r16
eor r17, r16
lds r16, comAddress ; send source address
st X+, r16
eor r17, r16
mov r16, r1 ; send i2c bus member address
st X+, r16
eor r17, r16
mov r16, r2 ; send i2c bus member availability
st X+, r16
eor r17, r16
; store XOR byte
st X+, r17
; mark buffer as enqueued with PRIO "info" (limited amount of retries)
ldi r20, COM_BUFFER_PRIO_INFO
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueTwiBusMember_error
sec
ret
CPRO_EnqueueTwiBusMember_error:
clc
ret
# endif
#endif
#ifdef WITH_DEBUG
# ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; Enqueue a DEBUG packet.
;
; IN:
; - R16: destination address
; - R1: debug value 1
; - R2: debug value 2
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)
CPRO_EnqueueDebug:
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
pop r16
brcc CPRO_EnqueueDebug_error
clr r17 ; r17: XOR byte
; write header (dest address, msg length)
st X+, r16 ; destination address
eor r17, r16
ldi r16, 4 ; 2 bytes payload
st X+, r16
eor r17, r16
; write payload
ldi r16, CPRO_CMD_DEBUG
st X+, r16
eor r17, r16
lds r16, comAddress
st X+, r16
eor r17, r16
mov r16, r1 ; debug 1
st X+, r16
eor r17, r16
mov r16, r2 ; debug 2
st X+, r16
eor r17, r16
; store XOR byte
st X+, r17
; mark buffer as enqueued with PRIO "info" (limited amount of retries)
ldi r20, COM_BUFFER_PRIO_INFO
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueDebug_error
sec
ret
CPRO_EnqueueDebug_error:
clc
ret
# endif
#endif
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; Enqueue a DEVICE packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R6, R16, R17, R18, R20 (R3, R4, R15, R19, R21, X)
CPRO_EnqueueDevice:
mov r6, r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
brcc CPRO_EnqueueDevice_error
push xh
push xl
mov r16, r6
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM)
ldi r18, CPRO_CMD_DEVICE
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
ldi r17, LOW(FW_TYPE)
st X+, r17 ; 6: firmware type low
ldi r17, HIGH(FW_TYPE)
st X+, r17 ; 7: firmware type high
ldi r17, FW_MAIN_VERSION_LOW
st X+, r17 ; 8: version low
ldi r17, FW_MAIN_VERSION_HIGH
st X+, r17 ; 9: version high
ldi r17, LOW(MODULES_MASK)
st X+, r17 ; 10: modules mask low
ldi r17, HIGH(MODULES_MASK)
st X+, r17 ; 11: modules mask low
pop xl
pop xh
rcall cproCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "normal" (slightly limited number of retries)
ldi r20, COM_BUFFER_PRIO_NORMAL
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueDevice_error
sec
ret
CPRO_EnqueueDevice_error:
clc
ret
#endif
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; Enqueue a VALUE packet.
;
; IN:
; - R16: destination address
; - R17: value id
; - R19:R18: value
; - R21:R20: denom (e.g. 100, meaning value must be divided by 100)
; - R22: value type
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R6, R7, R8, R9, R10, R11, R12, R16, R17, X, Y (R3, R4, R15, R16, R17, R18, R19, R20, R21)
CPRO_EnqueueValue:
mov r6, r16
mov r7, r17
mov r8, r18
mov r9, r19
mov r10, r20
mov r11, r21
mov r12, r22
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
brcc CPRO_EnqueueValue_error
push xh
push xl
mov r16, r6
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM)
ldi r18, CPRO_CMD_VALUE
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
st X+, r7 ; 6: value id
st X+, r12 ; 7: value type
st X+, r8 ; 8: low value
st X+, r9 ; 9: high value
st X+, r10 ; 10: low denom
st X+, r11 ; 11: high denom
pop xl
pop xh
rcall cproCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "normal" (slightly limited number of retries)
ldi r20, COM_BUFFER_PRIO_NORMAL
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueValue_error
sec
ret
CPRO_EnqueueValue_error:
clc
ret
#endif
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; Enqueue a NEEDADDRESS packet.
;
; IN:
; - 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
#endif
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; 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
#endif
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; 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
#endif
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; 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
#endif
#ifndef BASE_SYSTEM
; ---------------------------------------------------------------------------
; 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)
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
; mark buffer as enqueued with PRIO "important" (higher retry count)
ldi r20, COM_BUFFER_PRIO_IMPORTANT
rcall COM_EnqueuePacket ; (R15, R16)
brcc cproEnqueueAddressPacket_error
sec
ret
cproEnqueueAddressPacket_error:
clc
ret
#endif
; ---------------------------------------------------------------------------
; Enqueue a simple packet with payload only CMD and source address.
;
@@ -685,7 +244,7 @@ cproEnqueueMsgWithCmdAndSrcAddr:
pop r20
pop xl
pop xh
rcall cproCalcAndAddChecksumByte
rcall comCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO given in R20
rcall COM_EnqueuePacket ; (R15, R16)
@@ -750,31 +309,6 @@ cproBeginMsgWithVariablePayload_l2:
; ---------------------------------------------------------------------------
; add checksum byte to buffer
;
; IN:
; - X : pointer to packet buffer
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R18, X
cproCalcAndAddChecksumByte:
clr r17
ld r16, X+ ; dest addr
eor r17, r16
ld r18, X+ ; msg len
eor r17, r18
cproCalcAndAddChecksumByte_loop:
ld r16, X+
eor r17, r16
dec r18
brne cproCalcAndAddChecksumByte_loop
st X+, r17
ret
; ---------------------------------------------------------------------------
; cproCalcPayloadSize
;

View File

@@ -429,5 +429,103 @@ cproSetBitInBitfield:
; ---------------------------------------------------------------------------
; Enqueue a NEEDADDRESS packet.
;
; IN:
; - 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)
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 comCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "important" (higher retry count)
ldi r20, COM_BUFFER_PRIO_IMPORTANT
rcall COM_EnqueuePacket ; (R15, R16)
brcc cproEnqueueAddressPacket_error
sec
ret
cproEnqueueAddressPacket_error:
clc
ret

56
avr/comproto_debug.asm Normal file
View File

@@ -0,0 +1,56 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; Enqueue a DEBUG packet.
;
; IN:
; - R16: destination address
; - R1: debug value 1
; - R2: debug value 2
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)
CPRO_EnqueueDebug:
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
pop r16
brcc CPRO_EnqueueDebug_error
clr r17 ; r17: XOR byte
; write header (dest address, msg length)
st X+, r16 ; destination address
eor r17, r16
ldi r16, 4 ; 2 bytes payload
st X+, r16
eor r17, r16
; write payload
ldi r16, CPRO_CMD_DEBUG
st X+, r16
eor r17, r16
lds r16, comAddress
st X+, r16
eor r17, r16
mov r16, r1 ; debug 1
st X+, r16
eor r17, r16
mov r16, r2 ; debug 2
st X+, r16
eor r17, r16
; store XOR byte
st X+, r17
; mark buffer as enqueued with PRIO "info" (limited amount of retries)
ldi r20, COM_BUFFER_PRIO_INFO
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueDebug_error
sec
ret
CPRO_EnqueueDebug_error:
clc
ret

56
avr/comproto_device.asm Normal file
View File

@@ -0,0 +1,56 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; Enqueue a DEVICE packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R6, R16, R17, R18, R20 (R3, R4, R15, R19, R21, X)
CPRO_EnqueueDevice:
mov r6, r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
brcc CPRO_EnqueueDevice_error
push xh
push xl
mov r16, r6
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM)
ldi r18, CPRO_CMD_DEVICE
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
ldi r17, LOW(FW_TYPE)
st X+, r17 ; 6: firmware type low
ldi r17, HIGH(FW_TYPE)
st X+, r17 ; 7: firmware type high
ldi r17, FW_MAIN_VERSION_LOW
st X+, r17 ; 8: version low
ldi r17, FW_MAIN_VERSION_HIGH
st X+, r17 ; 9: version high
ldi r17, LOW(MODULES_MASK)
st X+, r17 ; 10: modules mask low
ldi r17, HIGH(MODULES_MASK)
st X+, r17 ; 11: modules mask low
pop xl
pop xh
rcall comCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "normal" (slightly limited number of retries)
ldi r20, COM_BUFFER_PRIO_NORMAL
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueDevice_error
sec
ret
CPRO_EnqueueDevice_error:
clc
ret

117
avr/comproto_stats.asm Normal file
View File

@@ -0,0 +1,117 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; Enqueue a COM Send stats packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)
CPRO_EnqueueComSendStats:
push r15
in r15, SREG
cli
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21) -> Y=buffer, X=msg
pop r16
brcc CPRO_EnqueueComSendStats_error
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM) ; seconds + 6 bytes payload
ldi r18, CPRO_CMD_COMSENDSTATS
push xh
push xl
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
lds r16, comStatsPacketsOut ; 6: packets out
st X+, r16
lds r16, comStatsPacketsOut+1
st X+, r16
lds r16, comStatsCollisions ; 8: collisions
st X+, r16
lds r16, comStatsCollisions+1
st X+, r16
lds r16, comStatsAborted ; 10: aborted
st X+, r16
lds r16, comStatsAborted+1
st X+, r16
pop xl
pop xh
rcall comCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "important" (higher retry count)
ldi r20, COM_BUFFER_PRIO_IMPORTANT
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueComSendStats_error
pop r15
out SREG, r15
sec
ret
CPRO_EnqueueComSendStats_error:
pop r15
out SREG, r15
clc
ret
; ---------------------------------------------------------------------------
; Enqueue a COM reception stats packet.
;
; IN:
; - R16: destination address
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R16, R17, R20, X (R15, Y)
CPRO_EnqueueComRecvStats:
push r15
in r15, SREG
cli
push r16
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
pop r16
brcc CPRO_EnqueueComRecvStats_error
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM) ; seconds + 6 bytes payload
ldi r18, CPRO_CMD_COMRECVSTATS
push xh
push xl
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
lds r16, comStatsPacketsIn ; 6: packets in
st X+, r16
lds r16, comStatsPacketsIn+1
st X+, r16
lds r16, comStatsRecvCrcErrs ; 8: CRC errors
st X+, r16
lds r16, comStatsRecvCrcErrs+1
st X+, r16
lds r16, comStatsRecvErrs ; 10: IO errors
st X+, r16
lds r16, comStatsRecvErrs+1
st X+, r16
pop xl
pop xh
rcall comCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "important" (higher retry count)
ldi r20, COM_BUFFER_PRIO_IMPORTANT
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueComRecvStats_error
pop r15
out SREG, r15
sec
ret
CPRO_EnqueueComRecvStats_error:
pop r15
out SREG, r15
clc
ret

60
avr/comproto_values.asm Normal file
View File

@@ -0,0 +1,60 @@
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; Enqueue a VALUE packet.
;
; IN:
; - R16: destination address
; - R17: value id
; - R19:R18: value
; - R21:R20: denom (e.g. 100, meaning value must be divided by 100)
; - R22: value type
; OUT:
; - CFLAG: set if okay, clear otherwise
; MODIFIED REGS: R6, R7, R8, R9, R10, R11, R12, R16, R17, X, Y (R3, R4, R15, R16, R17, R18, R19, R20, R21)
CPRO_EnqueueValue:
mov r6, r16
mov r7, r17
mov r8, r18
mov r9, r19
mov r10, r20
mov r11, r21
mov r12, r22
rcall COM_AllocBufferAndGetXY ; (r16, r17, r21)
brcc CPRO_EnqueueValue_error
push xh
push xl
mov r16, r6
ldi r17, CPRO_PAYLOAD_FLAGS_UID | (6<<CPRO_PAYLOAD_FLAGS_SHIFT_NUM)
ldi r18, CPRO_CMD_VALUE
rcall cproBeginMsgWithVariablePayload ; (R3, R4, R16, R17, R18, R19, R20, R21, X)
st X+, r7 ; 6: value id
st X+, r12 ; 7: value type
st X+, r8 ; 8: low value
st X+, r9 ; 9: high value
st X+, r10 ; 10: low denom
st X+, r11 ; 11: high denom
pop xl
pop xh
rcall comCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "normal" (slightly limited number of retries)
ldi r20, COM_BUFFER_PRIO_NORMAL
rcall COM_EnqueuePacket ; (R15, R16)
brcc CPRO_EnqueueValue_error
sec
ret
CPRO_EnqueueValue_error:
clc
ret

View File

@@ -70,7 +70,7 @@ CPRO_EnqueueFlashRsp:
st X+, r7 ; 6: ACK or NAK
pop xl
pop xh
rcall cproCalcAndAddChecksumByte
rcall comCalcAndAddChecksumByte
; mark buffer as enqueued with PRIO "important" (higher number of retries)
ldi r20, COM_BUFFER_PRIO_IMPORTANT