improved "run" code.

This commit is contained in:
Martin Preuss
2025-07-07 21:44:40 +02:00
parent 4f497fc41a
commit 245d44c05d
7 changed files with 126 additions and 72 deletions

View File

@@ -64,6 +64,10 @@ AppRouter_Init:
; set interface number for NETDEV1 ; set interface number for NETDEV1
ldi r16, NETDEV1_IFACENUM ldi r16, NETDEV1_IFACENUM
sts netInterfaceData2+NET_IFACE_OFFS_IFACENUM, r16 sts netInterfaceData2+NET_IFACE_OFFS_IFACENUM, r16
ldi r18, NETMSG_CMD_ADDRESS_RANGE
rcall appRouterSendRangeMsgToDev1
ret ret
; @end ; @end
@@ -128,6 +132,7 @@ appRouterHandleMsgAnyDev_handleRebootMsg:
ret ret
appRouterHandleMsgAnyDev_handlePingMsg: appRouterHandleMsgAnyDev_handlePingMsg:
rcall appRouterHandlePingRequest rcall appRouterHandlePingRequest
clc
ret ret
appRouterHandleMsgAnyDev_handleSetValue: appRouterHandleMsgAnyDev_handleSetValue:
rcall NETMSG_ValueRead ; (none) rcall NETMSG_ValueRead ; (none)
@@ -174,7 +179,7 @@ appRouterHandleDev1Msg:
pop xl pop xl
rjmp appRouterHandleDev1Msg_end rjmp appRouterHandleDev1Msg_end
appRouterHandleDev1Msg_savedX: appRouterHandleDev1Msg_savedX:
adiw xh:xl, NETMSG_OFFS_CMD ; maybe move ping/reboot handling to all/main.asm? adiw xh:xl, NETMSG_OFFS_CMD
ld r16, X ld r16, X
sbiw xh:xl, NETMSG_OFFS_CMD sbiw xh:xl, NETMSG_OFFS_CMD
cpi r16, NETMSG_CMD_NEED_ADDRESS cpi r16, NETMSG_CMD_NEED_ADDRESS
@@ -182,6 +187,7 @@ appRouterHandleDev1Msg_savedX:
cpi r16, NETMSG_CMD_CLAIM_ADDRESS cpi r16, NETMSG_CMD_CLAIM_ADDRESS
breq appRouterHandleDev1Msg_handleClaimAddr breq appRouterHandleDev1Msg_handleClaimAddr
; add more here ; add more here
ldi r16, 0
rjmp appRouterHandleDev1Msg_clcRet rjmp appRouterHandleDev1Msg_clcRet
appRouterHandleDev1Msg_handleNeedAddr: appRouterHandleDev1Msg_handleNeedAddr:
ldi r18, NETMSG_CMD_ADDRESS_RANGE ldi r18, NETMSG_CMD_ADDRESS_RANGE
@@ -255,7 +261,7 @@ appRouterSendAddrMsgToDev1:
bigcall NETMSG_Address_Write ; (R16, R17, R18, R19, R20, R21) bigcall NETMSG_Address_Write ; (R16, R17, R18, R19, R20, R21)
sbiw xh:xl, 1 sbiw xh:xl, 1
pop r16 pop r16
rcall appRouterSendMsgToDev1 ; (R16, R17, R18, X, Y) rcall appRouterSendMsgToDev1 ; (R16, R17, R18, R24, R25, X, Y)
appRouterSendAddrMsgToDev1_end: appRouterSendAddrMsgToDev1_end:
ret ret
; @end ; @end
@@ -266,7 +272,8 @@ appRouterSendAddrMsgToDev1_end:
; @routine appRouterSendRangeMsgToDev1 ; @routine appRouterSendRangeMsgToDev1
; ;
; @param R18 msg code ; @param R18 msg code
; @clobbers (R16, R17, R18, R19, R20, R21, X, Y) ; @return CFLAG set if message enqueued, cleared on error
; @clobbers (R16, R17, R18, R19, R20, R21, R24, R25, X, Y)
appRouterSendRangeMsgToDev1: appRouterSendRangeMsgToDev1:
bigcall NET_Buffer_Alloc ; (R16, R17, X) bigcall NET_Buffer_Alloc ; (R16, R17, X)
@@ -278,7 +285,7 @@ appRouterSendRangeMsgToDev1:
bigcall NETMSG_Range_Write ; (R16, R17, R18, R19, R20, R21) bigcall NETMSG_Range_Write ; (R16, R17, R18, R19, R20, R21)
sbiw xh:xl, 1 sbiw xh:xl, 1
pop r16 pop r16
rcall appRouterSendMsgToDev1 ; (R16, R17, R18, X, Y) rcall appRouterSendMsgToDev1 ; (R16, R17, R18, R24, R25, X, Y)
appRouterSendRangeMsgToDev1_end: appRouterSendRangeMsgToDev1_end:
ret ret
; @end ; @end
@@ -290,7 +297,8 @@ appRouterSendRangeMsgToDev1_end:
; ;
; @param R16 num of allocated buffer ; @param R16 num of allocated buffer
; @param X msg to send (points to start of allocated buffer, e.g. buffer header) ; @param X msg to send (points to start of allocated buffer, e.g. buffer header)
; @clobbers Y (R16, R17, R18, X) ; @return CFLAG set if message enqueued, cleared on error
; @clobbers Y (R16, R17, R18, R24, R25, X)
appRouterSendMsgToDev1: appRouterSendMsgToDev1:
ldi yl, LOW(netInterfaceData2) ldi yl, LOW(netInterfaceData2)
@@ -298,6 +306,8 @@ appRouterSendMsgToDev1:
bigcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X) bigcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X)
brcs appRouterSendMsgToDev1_end brcs appRouterSendMsgToDev1_end
bigcall NET_Buffer_ReleaseByNum ; (R16, X) bigcall NET_Buffer_ReleaseByNum ; (R16, X)
ldi r16, NET_IFACE_OFFS_ERR_NOBUF_LOW
rcall NET_Interface_IncCounter16 ; (R24, R25)
clc clc
appRouterSendMsgToDev1_end: appRouterSendMsgToDev1_end:
ret ret
@@ -354,27 +364,29 @@ appRouterHandlePingRequest_end:
; @routine appRouterCheckRecvdMsg ; @routine appRouterCheckRecvdMsg
; ;
; Read messages from either interface and forward to the other one. ; Read messages from either interface and forward to the other one.
;
; @return CFLAG set if something done, cleared otherwise
appRouterCheckRecvdMsg: appRouterCheckRecvdMsg:
rcall NET_PeekNextIncomingMsgNum ; check read queue (bufNum->r16) rcall NET_PeekNextIncomingMsgNum ; check read queue (bufNum->r16)
brcc appRouterCheckRecvdMsg_end ; no msg, jmp brcc appRouterCheckRecvdMsg_end ; no msg, jmp
rcall NET_Buffer_Locate ; (R17) rcall NET_Buffer_Locate ; (R17)
rcall appRouterHandleRouterMsgWithHdr ; filter out router msgs rcall appRouterHandleRouterMsgWithHdr ; filter out router msgs
brcs appRouterCheckRecvdMsg_removeMsg ; handled by router code, don't forward brcs appRouterCheckRecvdMsg_removeMsg ; handled by router code, don't forward
; let system handle incoming messages ; let system handle incoming messages
adiw xh:xl, 1 adiw xh:xl, 1
rcall appRouterLetSysHandleMsg rcall appRouterLetSysHandleMsg
sbiw xh:xl, 1 sbiw xh:xl, 1
; forward to other interface ; forward to other interface
ld r17, X ld r17, X
andi r17, (1<<NET_IFACE_BUFFER_IFACENUM1_BIT) | (1<<NET_IFACE_BUFFER_IFACENUM0_BIT) andi r17, (1<<NET_IFACE_BUFFER_IFACENUM1_BIT) | (1<<NET_IFACE_BUFFER_IFACENUM0_BIT)
rcall appRouterReverseInterfaceNum ; (R16, R17) rcall appRouterReverseInterfaceNum ; (R16, R17)
rcall appRouterAddMsgToInterface rcall appRouterAddMsgToInterface
brcc appRouterCheckRecvdMsg_end ; could not add, jmp brcc appRouterCheckRecvdMsg_end ; could not add, jmp
rcall NET_GetNextIncomingMsgNum ; take off the queue rcall NET_GetNextIncomingMsgNum ; take off the queue
rjmp appRouterCheckRecvdMsg sec
ret
appRouterCheckRecvdMsg_removeMsg: appRouterCheckRecvdMsg_removeMsg:
rcall NET_GetNextIncomingMsgNum ; take off the queue rcall NET_GetNextIncomingMsgNum ; take off the queue
rcall NET_Buffer_ReleaseByNum ; (R16, X) rcall NET_Buffer_ReleaseByNum ; (R16, X)
@@ -392,8 +404,8 @@ appRouterCheckRecvdMsg_end:
appRouterHandleRouterMsgWithHdr: appRouterHandleRouterMsgWithHdr:
ld r17, X ld r17, X
andi r17, (1<<NET_IFACE_BUFFER_IFACENUM1_BIT) | (1<<NET_IFACE_BUFFER_IFACENUM0_BIT)
adiw xh:xl, 1 adiw xh:xl, 1
andi r17, (1<<NET_IFACE_BUFFER_IFACENUM1_BIT) | (1<<NET_IFACE_BUFFER_IFACENUM0_BIT)
cpi r17, NETDEV1_IFACENUM cpi r17, NETDEV1_IFACENUM
brne appRouterHandleRouterMsgWithHdr_any brne appRouterHandleRouterMsgWithHdr_any
rcall appRouterHandleDev1Msg ; handle messages from controlled subnet rcall appRouterHandleDev1Msg ; handle messages from controlled subnet

View File

@@ -66,11 +66,21 @@ initApps:
; ;
runApps: runApps:
clr r16
#ifdef APPS_ROUTER #ifdef APPS_ROUTER
bigcall AppRouter_Run push r16
bigcall AppRouter_Run
pop r16
sbci r16, 0 ; decrease r16 only if CFLAG set
#endif #endif
; add more modules here ; add more modules here
tst r16
clc
breq runApps_end
sec
runApps_end:
ret ret
; @end ; @end

View File

@@ -241,6 +241,8 @@
.include "modules/network/msg/pong-w.asm" .include "modules/network/msg/pong-w.asm"
.include "modules/network/msg/range-d.asm" .include "modules/network/msg/range-d.asm"
.include "modules/network/msg/range-r.asm" .include "modules/network/msg/range-r.asm"
.include "common/eeprom-r.asm"
.include "common/eeprom-w.asm"
#endif #endif

View File

@@ -55,10 +55,26 @@ main:
main_loop: main_loop:
bigcall systemSleep ; system-dependant bigcall systemSleep ; system-dependant
bigcall runModules
bigcall runApps ; run loop as long as some run function returns with CFLAG set
main_runLoop:
clr r16
push r16
bigcall runModules
pop r16
sbci r16, 0
push r16
bigcall runApps
pop r16
sbci r16, 0
bigcall onEveryLoop ; call into main app push r16
bigcall onEveryLoop ; call into main app
pop r16
tst r16
brne main_runLoop
#ifdef MODULES_NETWORK #ifdef MODULES_NETWORK
#ifndef MAIN_WITHOUT_MSG_HANDLING #ifndef MAIN_WITHOUT_MSG_HANDLING

View File

@@ -175,56 +175,46 @@ initModules:
; USED: depending on called routines ; USED: depending on called routines
runModules: runModules:
bigcall BaseTimer_Run clr r16
push r16
bigcall BaseTimer_Run
pop r16
#ifdef MODULES_COM
; COM module (call until carry flag cleared but at most 10 times to not starve other modules)
ldi r16, 10
runModules_Com:
push r16
bigcall Com2_Run
pop r16
brcc runModules_ComEnd
dec r16
brne runModules_Com
runModules_ComEnd:
#endif
#ifdef MODULES_TTYONUART1 #ifdef MODULES_TTYONUART1
bigcall TtyOnUart1_Run push r16
bigcall TtyOnUart1_Run
pop r16
#endif #endif
#ifdef MODULES_COMONUART0 #ifdef MODULES_COMONUART0
bigcall ComOnUart0_Run push r16
bigcall ComOnUart0_Run
pop r16
sbci r16, 0
#endif #endif
#ifdef MODULES_COMONUART1 #ifdef MODULES_COMONUART1
bigcall ComOnUart1_Run push r16
bigcall ComOnUart1_Run
pop r16
sbci r16, 0
#endif #endif
#ifdef MODULES_STATS #ifdef MODULES_STATS
bigcall Stats_Run push r16
bigcall Stats_Run
pop r16
#endif #endif
#ifdef MODULES_REED
bigcall REED_Run
#endif
#ifdef MODULES_CNY70
bigcall CNY70_Run
#endif
#ifdef MODULES_MOTION_LIGHT
; rcall MotionLight_Run
#endif
#ifdef MODULES_TCRT1000
; rcall TCRT1K_Run
#endif
; add more modules here ; add more modules here
; check for repeat request
tst r16
clc
breq runModules_end
sec
runModules_end:
ret ret

View File

@@ -147,20 +147,32 @@ comOnUart0StartReading:
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; @routine ComOnUart0_Run @global ; @routine ComOnUart0_Run @global
; ;
; @return CFLAG set if something done, cleared otherwise
; @clobbers all ; @clobbers all
ComOnUart0_Run: ComOnUart0_Run:
ldi yl, LOW(comOnUart0_iface) ldi yl, LOW(comOnUart0_iface)
ldi yh, HIGH(comOnUart0_iface) ldi yh, HIGH(comOnUart0_iface)
ldd r16, Y+UART_HW2_IFACE_OFFS_MODE ldd r16, Y+UART_HW2_IFACE_OFFS_MODE
clr r18
ComOnUart0_Run_loop: ComOnUart0_Run_loop:
push r16 ; current state push r18
rcall comOnUart0RunMode ; (all but Y) push r16 ; current state
pop r17 ; previous state (pop from r16 into r17) rcall comOnUart0RunMode ; (all but Y)
pop r17 ; previous state (pop from r16 into r17)
pop r18
; read new state ; read new state
ldd r16, Y+UART_HW2_IFACE_OFFS_MODE ldd r16, Y+UART_HW2_IFACE_OFFS_MODE
cp r16, r17 cp r16, r17
brne ComOnUart0_Run_loop ; state changed, run again breq ComOnUart0_Run_loopEnd
ldi r18, 1
rjmp ComOnUart0_Run_loop ; state changed, run again
ComOnUart0_Run_loopEnd:
tst r18
clc
breq ComOnUart0_Run_end
sec
ComOnUart0_Run_end:
ret ret
; @end ; @end

View File

@@ -147,20 +147,32 @@ comOnUart1StartReading:
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; @routine ComOnUart1_Run @global ; @routine ComOnUart1_Run @global
; ;
; @return CFLAG set if something done, cleared otherwise
; @clobbers all ; @clobbers all
ComOnUart1_Run: ComOnUart1_Run:
ldi yl, LOW(comOnUart1_iface) ldi yl, LOW(comOnUart1_iface)
ldi yh, HIGH(comOnUart1_iface) ldi yh, HIGH(comOnUart1_iface)
ldd r16, Y+UART_HW2_IFACE_OFFS_MODE ldd r16, Y+UART_HW2_IFACE_OFFS_MODE
clr r18
ComOnUart1_Run_loop: ComOnUart1_Run_loop:
push r16 ; current state push r18
rcall comOnUart1RunMode ; (all but Y) push r16 ; current state
pop r17 ; previous state (pop from r16 into r17) rcall comOnUart1RunMode ; (all but Y)
pop r17 ; previous state (pop from r16 into r17)
pop r18
; read new state ; read new state
ldd r16, Y+UART_HW2_IFACE_OFFS_MODE ldd r16, Y+UART_HW2_IFACE_OFFS_MODE
cp r16, r17 cp r16, r17
brne ComOnUart1_Run_loop ; state changed, run again breq ComOnUart1_Run_loopEnd
ldi r18, 1
rjmp ComOnUart1_Run_loop ; state changed, run again
ComOnUart1_Run_loopEnd:
tst r18
clc
breq ComOnUart1_Run_end
sec
ComOnUart1_Run_end:
ret ret
; @end ; @end