From 7bae2072aa57e9122f9a1865331001eca9653b49 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Fri, 7 Aug 2026 00:42:45 +0200 Subject: [PATCH] avr: more code sharing. --- avr/apps/reportsensors/main.asm | 45 +------------ avr/apps/stats/main.asm | 116 +++++++++++++++++--------------- avr/common/walktable.asm | 82 ++++++++++++++++++++++ 3 files changed, 145 insertions(+), 98 deletions(-) create mode 100644 avr/common/walktable.asm diff --git a/avr/apps/reportsensors/main.asm b/avr/apps/reportsensors/main.asm index 6f4b3fe3..e5dc9294 100644 --- a/avr/apps/reportsensors/main.asm +++ b/avr/apps/reportsensors/main.asm @@ -82,54 +82,13 @@ AppReportSensors_OnEverySecond_store: appReportSensorsWalkTable: ldi zl, LOW(appReportSensorsTable*2) ldi zh, HIGH(appReportSensorsTable*2) - lds r22, reportSensorTimer -appReportSensorsWalkTable_loop: - lpm r16, Z+ - adiw zh:zl, 1 - lpm r18, Z+ - lpm r19, Z+ - mov r17, r18 ; routine==0? - or r17, r19 - breq appReportSensorsWalkTable_loopEnd ; yes, end of table - tst r16 ; timer==0? - breq appReportSensorsWalkTable_loopCall ; yes, match any value - cp r22, r16 - brne appReportSensorsWalkTable_loopNext ; skip -appReportSensorsWalkTable_loopCall: - push r22 - push zl - push zh - push yl - push yh - rcall appReportSensorsCallR19R18 - pop yh - pop yl - pop zh - pop zl - pop r22 -appReportSensorsWalkTable_loopNext: - rjmp appReportSensorsWalkTable_loop -appReportSensorsWalkTable_loopEnd: + lds r16, reportSensorTimer + bigcall WalkTable ret ; @end -; --------------------------------------------------------------------------- -; @routine appReportSensorsCallR19R18 -; Call route at address r19:r18 - -appReportSensorsCallR19R18: - push r18 - push r19 - ret -; @end - - - - - - #ifdef MODULES_SI7021 appReportSensorsSi7021MeasureValue1: rjmp SI7021_MeasureTemp diff --git a/avr/apps/stats/main.asm b/avr/apps/stats/main.asm index 273aa1a6..2e0e4381 100644 --- a/avr/apps/stats/main.asm +++ b/avr/apps/stats/main.asm @@ -71,55 +71,34 @@ AppStats_OnEveryMinute_noIrq: AppStats_OnEveryMinute_store: sts appStatsTimer, r16 - mov r17, r16 - andi r17, 3 - brne AppStats_OnEveryMinute_sendStats - ; send device announcement - push r16 - push yl - push yh - ldi yl, LOW(netInterfaceData) - ldi yh, HIGH(netInterfaceData) - rcall AppNetwork_SendDevice -#ifdef APP_STATS_NETDEV2 - ldi yl, LOW(netInterfaceData2) - ldi yh, HIGH(netInterfaceData2) - rcall AppNetwork_SendDevice -#endif - pop yh - pop yl - pop r16 -AppStats_OnEveryMinute_sendStats: -#if 1 -; debug - push r16 - push yl - push yh - rcall AppStats_OnEveryMinute_sendMemStats - pop yh - pop yl - pop r16 -#else - cpi r16, 2 - breq AppStats_OnEveryMinute_sendMemStats -#endif - - cpi r16, 3 - breq AppStats_OnEveryMinute_sendRecvStats1 - - cpi r16, 5 - breq AppStats_OnEveryMinute_sendSendStats1 - -#ifdef APP_STATS_NETDEV2 - cpi r16, 6 - breq AppStats_OnEveryMinute_sendRecvStats2 - - cpi r16, 7 - breq AppStats_OnEveryMinute_sendSendStats2 -#endif + rcall appStatsWalkTable ; add more here ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine appStatsTable +; +; @clobbers r16, r18, r19, r22, Z, (any) + +appStatsWalkTable: + ldi zl, LOW(appStatsTable*2) + ldi zh, HIGH(appStatsTable*2) + lds r16, appStatsTimer + bigcall WalkTable + ret +; @end + + + +AppStats_OnEveryMinute_sendDeviceDev1: + ldi yl, LOW(netInterfaceData) + ldi yh, HIGH(netInterfaceData) + rjmp AppNetwork_SendDevice + AppStats_OnEveryMinute_sendMemStats: ldi yl, LOW(netInterfaceData) ldi yh, HIGH(netInterfaceData) @@ -135,19 +114,46 @@ AppStats_OnEveryMinute_sendSendStats1: ldi yh, HIGH(netInterfaceData) rjmp AppNetwork_SendTxdStats -#ifdef APP_STATS_NETDEV2 -AppStats_OnEveryMinute_sendRecvStats2: - ldi yl, LOW(netInterfaceData2) - ldi yh, HIGH(netInterfaceData2) - rjmp AppNetwork_SendRxdStats -AppStats_OnEveryMinute_sendSendStats2: - ldi yl, LOW(netInterfaceData2) - ldi yh, HIGH(netInterfaceData2) - rjmp AppNetwork_SendTxdStats +#ifdef APP_STATS_NETDEV2 + AppStats_OnEveryMinute_sendDeviceDev2: + ldi yl, LOW(netInterfaceData2) + ldi yh, HIGH(netInterfaceData2) + rjmp AppNetwork_SendDevice + + AppStats_OnEveryMinute_sendRecvStats2: + ldi yl, LOW(netInterfaceData2) + ldi yh, HIGH(netInterfaceData2) + rjmp AppNetwork_SendRxdStats + + AppStats_OnEveryMinute_sendSendStats2: + ldi yl, LOW(netInterfaceData2) + ldi yh, HIGH(netInterfaceData2) + rjmp AppNetwork_SendTxdStats #endif ; @end +; --------------------------------------------------------------------------- +; table +; time routine (word address) + +appStatsTable: + .dw 1, AppStats_OnEveryMinute_sendMemStats + .dw 2, AppStats_OnEveryMinute_sendDeviceDev1 + .dw 3, AppStats_OnEveryMinute_sendRecvStats1 + .dw 4, AppStats_OnEveryMinute_sendSendStats1 + .dw 5, AppStats_OnEveryMinute_sendDeviceDev1 +#ifdef APP_STATS_NETDEV2 + .dw 6, AppStats_OnEveryMinute_sendDeviceDev2 + .dw 7, AppStats_OnEveryMinute_sendRecvStats2 + .dw 8, AppStats_OnEveryMinute_sendSendStats2 +#endif + + ; end of table + .dw 0, 0 + + + diff --git a/avr/common/walktable.asm b/avr/common/walktable.asm new file mode 100644 index 00000000..634876d4 --- /dev/null +++ b/avr/common/walktable.asm @@ -0,0 +1,82 @@ +; *************************************************************************** +; copyright : (C) 2026 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. * +; *************************************************************************** + +#ifndef AQH_AVR_COMMON_WALKTABLE_ASM +#define AQH_AVR_COMMON_WALKTABLE_ASM + + + +; *************************************************************************** +; code + +.cseg + + + +; --------------------------------------------------------------------------- +; @routine WalkTable +; +; Walks a given table and calls every routine whose table entry +; holds a value equal to the given one. +; +; The loop uses r16-r19 and Z. +; +; @param Z byte pointer to table (for LPM!) +; @param r16 value +; @clobbers (any) + +WalkTable: + mov r22, r16 +WalkTable_loop: + lpm r16, Z+ + adiw zh:zl, 1 + lpm r18, Z+ + lpm r19, Z+ + mov r17, r18 ; routine==0? + or r17, r19 + breq WalkTable_loopEnd ; yes, end of table + tst r16 ; timer==0? + breq WalkTable_loopCall ; yes, match any value + cp r22, r16 + brne WalkTable_loopNext ; skip +WalkTable_loopCall: + push r22 + push zl + push zh + push yl + push yh + rcall walkTableCallR19R18 + pop yh + pop yl + pop zh + pop zl + pop r22 +WalkTable_loopNext: + rjmp WalkTable_loop +WalkTable_loopEnd: + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine walkTableCallR19R18 +; Call route at address r19:r18 + +walkTableCallR19R18: + push r18 + push r19 + ret +; @end + + + +#endif + +