From 6e25647c0a8fc3aae0d596d806c9582b760daa9c Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Thu, 13 Feb 2025 23:52:55 +0100 Subject: [PATCH] avr: echoing messages via tty now basically works. --- avr/devices/t03/main.asm | 112 ++++++++++++++++++++++-- avr/modules/network/msg/common.asm | 4 +- avr/modules/network/msg/device-w.asm | 1 + avr/modules/network/msg/recvstats-w.asm | 41 +++++++++ avr/modules/network/msg/sendstats-w.asm | 7 +- avr/modules/uart_hw/ttyonuart1.asm | 16 ++-- 6 files changed, 163 insertions(+), 18 deletions(-) create mode 100644 avr/modules/network/msg/recvstats-w.asm diff --git a/avr/devices/t03/main.asm b/avr/devices/t03/main.asm index f570ca5..46a3853 100644 --- a/avr/devices/t03/main.asm +++ b/avr/devices/t03/main.asm @@ -24,9 +24,9 @@ ;.equ clock=1000000 ; Define the clock frequency .equ clock=8000000 ; Define the clock frequency -; .equ SEND_DEVICE_EVERY = 3000 -.equ SEND_DEVICE_EVERY = 100 ; every 10s - +;.equ SEND_DEVICE_EVERY = 3000 +.equ SEND_DEVICE_EVERY = 350 ; every 30s +.equ SEND_STATS_EVERY = 600 ; every minute .nolist .include "include/tn841def.inc" ; Define device ATtiny841 @@ -204,6 +204,8 @@ onSystemTimerTick: rcall LedSimple_Every100ms #endif rcall maybeSendDeviceMsg + rcall maybeSendTStatsMsg + rcall maybeSendRStatsMsg rcall TtyOnUart1_Periodically ret @@ -242,6 +244,17 @@ initModules: sts deviceCounter, r16 ldi r16, HIGH(SEND_DEVICE_EVERY) sts deviceCounter+1, r16 + + ldi r16, LOW(SEND_STATS_EVERY) + sts sendTStatsCounter, r16 + ldi r16, HIGH(SEND_STATS_EVERY) + sts sendTStatsCounter+1, r16 + + ldi r16, LOW(SEND_STATS_EVERY) + sts sendRStatsCounter, r16 + ldi r16, HIGH(SEND_STATS_EVERY) + sts sendRStatsCounter+1, r16 + ret ; @end @@ -270,6 +283,8 @@ initModules: .include "modules/network/msg/defs.asm" .include "modules/network/msg/common.asm" .include "modules/network/msg/device-w.asm" +.include "modules/network/msg/sendstats-w.asm" +.include "modules/network/msg/recvstats-w.asm" .include "modules/uart_hw/defs.asm" .include "modules/uart_hw/lowlevel.asm" @@ -292,7 +307,7 @@ maybeSendDeviceMsg: brne maybeSendDeviceMsg_storeCounter ; send device msg rcall NET_Buffer_Alloc ; (R16, R17, X) - brcc maybeSendDeviceMsg_resetCounter + brcc maybeSendDeviceMsg_end push r16 adiw xh:xl, 1 rcall NETMSG_Device_Write ; (R16, R17, R18, R19, R20, R21, Z) @@ -316,12 +331,94 @@ maybeSendDeviceMsg_end: +maybeSendTStatsMsg: + ldi yl, LOW(ttyOnUart1_iface) + ldi yh, HIGH(ttyOnUart1_iface) + + lds r24, sendTStatsCounter + lds r25, sendTStatsCounter+1 + sbiw r25:r24, 1 + brne maybeSendTStatsMsg_storeCounter + ; send device msg + rcall NET_Buffer_Alloc ; (R16, R17, X) +; brcc maybeSendTStatsMsg_resetCounter + brcc maybeSendTStatsMsg_end + push r16 + adiw xh:xl, 1 + rcall NETMSG_SendStats_Write ; (R16, R17, R18, R19, R20, R21, Z) + sbiw xh:xl, 1 + pop r16 + push r16 + rcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X) + pop r16 + brcs maybeSendTStatsMsg_resetCounter + rcall NET_Buffer_ReleaseByNum ; (R16, X) + rjmp maybeSendTStatsMsg_end + ; reset counter +maybeSendTStatsMsg_resetCounter: + ldi r24, LOW(SEND_STATS_EVERY) + ldi r25, HIGH(SEND_STATS_EVERY) +maybeSendTStatsMsg_storeCounter: + sts sendTStatsCounter, r24 + sts sendTStatsCounter+1, r25 +maybeSendTStatsMsg_end: + ret + + + +maybeSendRStatsMsg: + ldi yl, LOW(ttyOnUart1_iface) + ldi yh, HIGH(ttyOnUart1_iface) + + lds r24, sendRStatsCounter + lds r25, sendRStatsCounter+1 + sbiw r25:r24, 1 + brne maybeSendRStatsMsg_storeCounter + ; send device msg + rcall NET_Buffer_Alloc ; (R16, R17, X) +; brcc maybeSendTStatsMsg_resetCounter + brcc maybeSendRStatsMsg_end + push r16 + adiw xh:xl, 1 + rcall NETMSG_RecvStats_Write ; (R16, R17, R18, R19, R20, R21, Z) + sbiw xh:xl, 1 + pop r16 + push r16 + rcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X) + pop r16 + brcs maybeSendRStatsMsg_resetCounter + rcall NET_Buffer_ReleaseByNum ; (R16, X) + rjmp maybeSendRStatsMsg_end + ; reset counter +maybeSendRStatsMsg_resetCounter: + ldi r24, LOW(SEND_STATS_EVERY) + ldi r25, HIGH(SEND_STATS_EVERY) +maybeSendRStatsMsg_storeCounter: + sts sendRStatsCounter, r24 + sts sendRStatsCounter+1, r25 +maybeSendRStatsMsg_end: + ret + + + checkRecvdMsg: ldi yl, LOW(ttyOnUart1_iface) ldi yh, HIGH(ttyOnUart1_iface) rcall NET_PeekNextIncomingMsgNum ; check read queue brcc checkRecvdMsg_end ; no msg, jmp + rcall NET_Buffer_Locate ; (R17) + adiw xh:xl, 1 + push r16 + rcall NETMSG_CheckMessageInBuffer ; (R16, R17, R18, R19, R20, X) + pop r16 + brcs checkRecvdMsg_sendMsg + rcall NET_GetNextIncomingMsgNum ; take off the queue + rcall NET_Buffer_ReleaseByNum ; delete + ldi r16, NET_IFACE_OFFS_ERR_CONTENT_LOW + rcall NET_Interface_IncCounter16 ; (R24, R25) + rjmp checkRecvdMsg_end +checkRecvdMsg_sendMsg: rcall NET_Interface_AddOutgoingMsgNum ; try to add msg to interface brcc checkRecvdMsg_end ; could not add, jmp rcall NET_GetNextIncomingMsgNum ; take off the queue @@ -402,9 +499,10 @@ waitFor10ms_loop: .dseg programRamBegin: -; nothing for now -flashUid: .byte 4 -deviceCounter: .byte 2 + flashUid: .byte 4 + deviceCounter: .byte 2 + sendTStatsCounter: .byte 2 + sendRStatsCounter: .byte 2 programRamEnd: diff --git a/avr/modules/network/msg/common.asm b/avr/modules/network/msg/common.asm index 543193b..23767f3 100644 --- a/avr/modules/network/msg/common.asm +++ b/avr/modules/network/msg/common.asm @@ -49,7 +49,7 @@ NETMSG_CalcAndAddChecksumByte: ; --------------------------------------------------------------------------- -; @routine netMsgCheckMessageInBuffer +; @routine NETMSG_CheckMessageInBuffer ; ; check message in buffer ; @@ -57,7 +57,7 @@ NETMSG_CalcAndAddChecksumByte: ; @param X pointer to packet buffer ; @clobbers R16, R17 (R18, R19, R20, X) -netMsgCheckMessageInBuffer: +NETMSG_CheckMessageInBuffer: rcall netMsgCalcMsgChecksum ; (R16, R17, R18, R19, R20, X) ld r17, X cp r16, r17 ; should be equal diff --git a/avr/modules/network/msg/device-w.asm b/avr/modules/network/msg/device-w.asm index 56549c1..b41c66c 100644 --- a/avr/modules/network/msg/device-w.asm +++ b/avr/modules/network/msg/device-w.asm @@ -13,6 +13,7 @@ ; @routine NETMSG_Device_Write ; ; @param Y pointer to device to write msg for +; @param X pointer to buffer to write to ; @clobbers R16, R18 (R17, R19, R20, R21, Z) NETMSG_Device_Write: diff --git a/avr/modules/network/msg/recvstats-w.asm b/avr/modules/network/msg/recvstats-w.asm new file mode 100644 index 0000000..60ab75f --- /dev/null +++ b/avr/modules/network/msg/recvstats-w.asm @@ -0,0 +1,41 @@ +; *************************************************************************** +; copyright : (C) 2025 by Martin Preuss +; email : martin@libchipcard.de +; +; *************************************************************************** +; * This file is part of the project "AqHome". * +; * Please see toplevel file COPYING of that project for license details. * +; *************************************************************************** + + + +; --------------------------------------------------------------------------- +; @routine NETMSG_RecvStats_Write @global +; +; @param Y pointer to device to write msg for and to +; @param X pointer to buffer to write to +; @clobbers R16, R18 (R17, R19, R20, R21, Z) + +NETMSG_RecvStats_Write: + ldi r16, 0xff + st X+, r16 ; dest address + ldi r16, 18 ; msg code+src address+10 payload bytes + st X+, r16 ; msg len + ldi r16, NETMSG_CMD_RECVSTATS + st X+, r16 ; msg code + ldd r16, Y+NET_IFACE_OFFS_ADDRESS + st X+, r16 ; src address + rcall NETMSG_Common_AddUidToBuffer ; (R16, R18, R19, R20, R21) + adiw yh:yl, NET_IFACE_OFFS_PACKETSIN_LOW + ldi r18, 12 + rcall Utils_Copy_SDRAM ; (R17, R18, X, Y) + sbiw yh:yl, NET_IFACE_OFFS_PACKETSIN_LOW+12 + + sbiw xh:xl, 20 ; go back to beginning of message (1 byte dst addr, 1 byte length, 18 bytes payload) + rcall NETMSG_CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X) + sbiw xh:xl, 21 ; go back to beginning of message (1 byte dst addr, 1 byte length, 18 bytes payload, 1 byte crc) + ret +; @end + + + diff --git a/avr/modules/network/msg/sendstats-w.asm b/avr/modules/network/msg/sendstats-w.asm index b11eff7..5ad7d9f 100644 --- a/avr/modules/network/msg/sendstats-w.asm +++ b/avr/modules/network/msg/sendstats-w.asm @@ -13,6 +13,7 @@ ; @routine NETMSG_SendStats_Write @global ; ; @param Y pointer to device to write msg for and to +; @param X pointer to buffer to write to ; @clobbers R16, R18 (R17, R19, R20, R21, Z) NETMSG_SendStats_Write: @@ -27,12 +28,12 @@ NETMSG_SendStats_Write: rcall NETMSG_Common_AddUidToBuffer ; (R16, R18, R19, R20, R21) adiw yh:yl, NET_IFACE_OFFS_PACKETSOUT_LOW ldi r18, 6 - rcall Utils_Copy_SDRAM + rcall Utils_Copy_SDRAM ; (R17, R18, X, Y) sbiw yh:yl, NET_IFACE_OFFS_PACKETSOUT_LOW+6 - sbiw xh:xl, 13 ; go back to beginning of message (1 byte dst addr, 1 byte length, 12 bytes payload) + sbiw xh:xl, 14 ; go back to beginning of message (1 byte dst addr, 1 byte length, 12 bytes payload) rcall NETMSG_CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X) - sbiw xh:xl, 14 ; go back to beginning of message (1 byte dst addr, 1 byte length, 12 bytes payload, 1 byte crc) + sbiw xh:xl, 15 ; go back to beginning of message (1 byte dst addr, 1 byte length, 12 bytes payload, 1 byte crc) ret ; @end diff --git a/avr/modules/uart_hw/ttyonuart1.asm b/avr/modules/uart_hw/ttyonuart1.asm index 93f710d..1d6d4e9 100644 --- a/avr/modules/uart_hw/ttyonuart1.asm +++ b/avr/modules/uart_hw/ttyonuart1.asm @@ -9,6 +9,7 @@ .equ TTYONUART1_SKIPTIME = 2 +.equ TTYONUART1_MSG_INTERVAL = 2 .equ TTYONUART1_IFACENUM = 2 @@ -218,7 +219,7 @@ TtyOnUart1_Run: ttyOnUart1RunWriteModes: ldd r16, Y+UART_HW_IFACE_OFFS_WRITEMODE ; handle write functions cpi r16, UART_HW_WRITEMODE_IDLE - breq ttyOnUart1RunIdle + breq ttyOnUart1RunWriteIdle cpi r16, UART_HW_WRITEMODE_WRITING breq ttyOnUart1RunWriting cpi r16, UART_HW_WRITEMODE_WAITBUFFEREMPTY @@ -235,13 +236,16 @@ ttyOnUart1RunWriteModes: ; ; @clobbers -ttyOnUart1RunIdle: +ttyOnUart1RunWriteIdle: + ldd r16, Y+NET_IFACE_OFFS_WRITETIMER + cpi r16, TTYONUART1_MSG_INTERVAL ; wait a bit between messages + brcs ttyOnUart1RunWriteIdle_end rcall NET_Interface_GetNextOutgoingMsgNum ; (R17, R18, X) - brcc ttyOnUart1RunIdle_end + brcc ttyOnUart1RunWriteIdle_end rcall NET_Buffer_Locate ; (R17) - brcc ttyOnUart1RunIdle_end + brcc ttyOnUart1RunWriteIdle_end rcall TtyOnUart1_SendBuffer ; (R16, R17) -ttyOnUart1RunIdle_end: +ttyOnUart1RunWriteIdle_end: ret @@ -355,7 +359,7 @@ ttyOnUart1RunReading: ttyOnUart1RunSkipping: ldd r16, Y+NET_IFACE_OFFS_READTIMER - cpi r16, 2 + cpi r16, TTYONUART1_SKIPTIME brcs ttyOnUart1RunSkipping_end ldi r16, UART_HW_READMODE_IDLE std Y+UART_HW_IFACE_OFFS_READMODE, r16