From abe218e7b0a9b311850bda6a92bc7af2b85375f3 Mon Sep 17 00:00:00 2001 From: Martin Preuss Date: Mon, 20 Apr 2026 23:56:52 +0200 Subject: [PATCH] avr: reorganized common code. --- avr/apps/network/main.asm | 2 +- avr/common/0BUILD | 23 +++-- avr/common/devuid.asm | 112 ++++++++++++++++++++++++ avr/common/eeprom-r.asm | 25 +++++- avr/common/eeprom-w.asm | 24 ++++++ avr/common/inccounter16.asm | 47 +++++++++++ avr/common/inccounter32.asm | 51 +++++++++++ avr/common/list.asm | 7 +- avr/common/utils.asm | 136 ------------------------------ avr/common/utils_initial_wait.asm | 2 +- avr/devices/all/includes.asm | 5 +- avr/devices/all/main.asm | 2 +- 12 files changed, 281 insertions(+), 155 deletions(-) create mode 100644 avr/common/devuid.asm create mode 100644 avr/common/inccounter16.asm create mode 100644 avr/common/inccounter32.asm diff --git a/avr/apps/network/main.asm b/avr/apps/network/main.asm index 2f3c3e3..6e47485 100644 --- a/avr/apps/network/main.asm +++ b/avr/apps/network/main.asm @@ -224,7 +224,7 @@ appNetworkHandlePingRequest_end: appNetworkHandleReeunumRequest: push xl push xh - bigcall Utils_ReadUid ; r21:r20:r19:r18=uid (r16, X) + bigcall DevUid_ReadFromEeprom ; r21:r20:r19:r18=uid (r16, X) pop xh pop xl rcall NETMSG_Range_Read ; r20=range begin, r21=range end (none) diff --git a/avr/common/0BUILD b/avr/common/0BUILD index 838a3ed..5247bd2 100644 --- a/avr/common/0BUILD +++ b/avr/common/0BUILD @@ -3,16 +3,31 @@ + calls.asm crc8.asm debug.asm + devuid.asm divide.asm + divide8.asm + eeprom-r.asm + eeprom-w.asm + eeprom_tlv.asm + inccounter16.asm + inccounter32.asm + itoa.asm + list.asm + list_t.asm m_fixedbuffers.asm m_ringbuffer.asm m_ringbuffer_y.asm multiply.asm + random.asm + ressource.asm ringbuffer.asm ringbuffer_y.asm shared.asm + tree.asm + tree_t.asm utils.asm utils_copy_from_flash.asm utils_copy_sdram.asm @@ -26,14 +41,6 @@ wait_1ms.asm wait_50us.asm watchdog.asm - list.asm - list_t.asm - tree.asm - tree_t.asm - eeprom-r.asm - eeprom-w.asm - ressource.asm - itoa.asm diff --git a/avr/common/devuid.asm b/avr/common/devuid.asm new file mode 100644 index 0000000..36effda --- /dev/null +++ b/avr/common/devuid.asm @@ -0,0 +1,112 @@ +; *************************************************************************** +; 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_DEVUID_ASM +#define AQH_AVR_COMMON_DEVUID_ASM + + + +; *************************************************************************** +; code + + +.cseg + + + +; --------------------------------------------------------------------------- +; @routine DevUid_Read + +; Read UID from EEPROM. +; +; @return r21:r20:r19:r18 UID +; @clobbers R16, X + +DevUid_ReadFromEeprom: + ldi xl, LOW(EEPROM_OFFS_UUID) + ldi xh, HIGH(EEPROM_OFFS_UUID) + rcall Eeprom_ReadByte ; r16=byte read (none) + brcc DevUid_ReadFromEeprom_ret + mov r18, r16 + adiw xh:xl, 1 + + rcall Eeprom_ReadByte ; r16=byte read (none) + brcc DevUid_ReadFromEeprom_ret + mov r19, r16 + adiw xh:xl, 1 + + rcall Eeprom_ReadByte ; r16=byte read (none) + brcc DevUid_ReadFromEeprom_ret + mov r20, r16 + adiw xh:xl, 1 + + rcall Eeprom_ReadByte ; r16=byte read (none) + brcc DevUid_ReadFromEeprom_ret + mov r21, r16 + +DevUid_ReadFromEeprom_ret: + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine DevUid_Setup @global +; +; Reads UID from EEPROM. If not set generate a new one and store it in EEPROM. +; +; @return CFLAG set if new generated, cleared if there already was one. +; @clobbers R16, R18, R19, R20, R21, X (R17) + +DevUid_Setup: + rcall DevUid_ReadFromEeprom ; (R16, X) + cp r18, r19 ; all the same? + brne DevUid_Setup_uidOkay ; different, jmp + cp r18, r20 + brne DevUid_Setup_uidOkay ; different, jmp + cp r18, r21 + brne DevUid_Setup_uidOkay ; different, jmp + + ldi xl, LOW(EEPROM_OFFS_UUID) ; all the same, generate new uid + ldi xh, HIGH(EEPROM_OFFS_UUID) + rcall RAND_PseudoRandom ; byte 0 (R16, R17, R18, R19) + inc r16 + rcall Eeprom_WriteByteIfChanged ; (r17) + brcc DevUid_Setup_ret + adiw xh:xl, 1 + + rcall RAND_PseudoRandom ; byte 1 + rcall Eeprom_WriteByteIfChanged ; (r17) + brcc DevUid_Setup_ret + adiw xh:xl, 1 + + rcall RAND_PseudoRandom ; byte 2 + rcall Eeprom_WriteByteIfChanged ; (r17) + brcc DevUid_Setup_ret + adiw xh:xl, 1 + + rcall RAND_PseudoRandom ; byte 3 + rcall Eeprom_WriteByteIfChanged ; (r17) + brcc DevUid_Setup_ret + + rcall RAND_UpdateSeedInEeprom ; (R16, R17, R18, R19, X) + +DevUid_Setup_uidOkay: + sec +DevUid_Setup_ret: + ret +; @end + + + + + + +#endif + diff --git a/avr/common/eeprom-r.asm b/avr/common/eeprom-r.asm index a9e36aa..c6f0036 100644 --- a/avr/common/eeprom-r.asm +++ b/avr/common/eeprom-r.asm @@ -60,7 +60,6 @@ Eeprom_ReadByte_ret: ; --------------------------------------------------------------------------- ; @routine Eeprom_CheckAddr ; -; ; @param X EEPROM Address to validate ; @return CFLAG set if address okay, cleared if out of range ; @clobbers r16 @@ -75,6 +74,30 @@ Eeprom_CheckAddr: +; --------------------------------------------------------------------------- +; @routine Eeprom_ReadBytes +; +; @param X EEPROM Address to read from +; @param Y address in SDRAM to write to +; @param R17 number of bytes to copy +; @clobbers r16, r17, X, Y + +Eeprom_ReadBytes: +Eeprom_ReadBytes_loop: + rcall Eeprom_ReadByte ; r16=byte read (none) + brcc Eeprom_ReadBytes_done + st Y+, r16 + adiw xh:xl, 1 + dec r17 + brne Eeprom_ReadBytes_loop + sec +Eeprom_ReadBytes_done: + ret +; @end + + + + #endif ; AQH_AVR_COMMON_EEPROM_R_H diff --git a/avr/common/eeprom-w.asm b/avr/common/eeprom-w.asm index 88f6ffa..1416f65 100644 --- a/avr/common/eeprom-w.asm +++ b/avr/common/eeprom-w.asm @@ -101,6 +101,30 @@ Eeprom_WriteByteIfChanged_end: +; --------------------------------------------------------------------------- +; @routine Eeprom_WriteBytes @global +; +; Write bytes into EEPROM (if changed, thus saving on write cycles) +; +; @param X eeprom address to write to +; @param Y address in SDRAM to read from +; @param R17 number of bytes to copy +; @clobbers r16, r17, r18, X, Y + +Eeprom_WriteBytes: + mov r18, r17 +Eeprom_WriteBytes_loop: + ld r16, Y+ + rcall Eeprom_WriteByteIfChanged + brcc Eeprom_WriteBytes_ret + adiw xh:xl, 1 + dec r18 + brne Eeprom_WriteBytes_loop +Eeprom_WriteBytes_ret: + ret +; @end + + #endif ; AQH_AVR_COMMON_EEPROM_W_H diff --git a/avr/common/inccounter16.asm b/avr/common/inccounter16.asm new file mode 100644 index 0000000..9480718 --- /dev/null +++ b/avr/common/inccounter16.asm @@ -0,0 +1,47 @@ +; *************************************************************************** +; 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_INCCOUNTER16_ASM +#define AQH_AVR_COMMON_INCCOUNTER16_ASM + + + +; *************************************************************************** +; code + + +.cseg + + + +; --------------------------------------------------------------------------- +; @routine Utils_IncrementCounter16 @global +; +; Increment a 16 bit counter at the address given by X. +; +; @param X Address of the 2 byte counter (1. byte is LSB) +; @clobbers r18, r19, r22 + +Utils_IncrementCounter16: + ld r18, x+ + ld r19, x + ldi r22, 1 + add r18, r22 + clr r22 ; doesn't affect carry flag + adc r19, r22 + st x, r19 + st -x, r18 + ret +; @end + + + + +#endif + diff --git a/avr/common/inccounter32.asm b/avr/common/inccounter32.asm new file mode 100644 index 0000000..2a2e353 --- /dev/null +++ b/avr/common/inccounter32.asm @@ -0,0 +1,51 @@ +; *************************************************************************** +; 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_INCCOUNTER32_ASM +#define AQH_AVR_COMMON_INCCOUNTER32_ASM + + + +; *************************************************************************** +; code + + +.cseg + + +; --------------------------------------------------------------------------- +; @routine Utils_IncrementCounter32 @global +; +; Increment a 32 bit counter at the address given by X. +; +; @param X Address of the 4 byte counter (1. byte is LSB) +; @clobbers r18, r19, r20, r21, 22 + +Utils_IncrementCounter32: + ld r18, x+ + ld r19, x+ + ld r20, x+ + ld r21, x + ldi r22, 1 + add r18, r22 + clr r22 ; doesn't affect carry flag + adc r19, r22 + adc r20, r22 + adc r21, r22 + st x, r21 + st -x, r20 + st -x, r19 + st -x, r18 + ret +; @end + + + +#endif + diff --git a/avr/common/list.asm b/avr/common/list.asm index 2afa294..65bed7d 100644 --- a/avr/common/list.asm +++ b/avr/common/list.asm @@ -212,8 +212,8 @@ List_UnlinkAllObjects_loop: ; Calls the given function for every object until it ; returns with a set carry flag or until the full list ; is handled. -; Registers that can be used by the given function are all -; except R16, R18 and R19 (those are used by this routine). +; All registers that can be used by the given function, however +; R16, R18, R19 and Y are modified between function calls. ; ; @param Y pointer to first object in a list ; @param Z routine to call for every object @@ -223,9 +223,6 @@ List_ForEveryObject: List_ForEveryObject_loop: ldd r18, Y+LIST_OFFS_NEXT_LO ; next ldd r19, Y+LIST_OFFS_NEXT_HI ; next - clr r16 - std Y+LIST_OFFS_NEXT_LO, r16 - std Y+LIST_OFFS_NEXT_HI, r16 push r18 ; next push r19 ; next rcall List_ForEveryObject_callZ diff --git a/avr/common/utils.asm b/avr/common/utils.asm index d50ad0a..aacf067 100644 --- a/avr/common/utils.asm +++ b/avr/common/utils.asm @@ -25,7 +25,6 @@ utilsDateString: .db "%YEAR%-%MONTH%-%DAY%-%HOUR%:%MINUTE%", 0, 0 - ; --------------------------------------------------------------------------- ; @routine Utils_FillSram @global ; @@ -49,141 +48,6 @@ Utils_FillSram_end: -; --------------------------------------------------------------------------- -; Increment a 32 bit counter at the address given by X. -; IN: -; - X: Address of the 4 byte counter (1. byte is LSB) -; OUT: -; - nothing -; MODIFIED REGISTERS: r18, r19, r20, r21, 22 - -Utils_IncrementCounter32: - ld r18, x+ - ld r19, x+ - ld r20, x+ - ld r21, x - ldi r22, 1 - add r18, r22 - clr r22 ; doesn't affect carry flag - adc r19, r22 - adc r20, r22 - adc r21, r22 - st x, r21 - st -x, r20 - st -x, r19 - st -x, r18 - ret - - - -; --------------------------------------------------------------------------- -; Increment a 16 bit counter at the address given by X. -; IN: -; - X: Address of the 2 byte counter (1. byte is LSB) -; OUT: -; - nothing -; MODIFIED REGISTERS: r18, r19, 22 - -Utils_IncrementCounter16: - ld r18, x+ - ld r19, x - ldi r22, 1 - add r18, r22 - clr r22 ; doesn't affect carry flag - adc r19, r22 - st x, r19 - st -x, r18 - ret -; @end - - - -; --------------------------------------------------------------------------- -; @routine Utils_ReadUid - -; Read UID from EEPROM. -; -; @return r21:r20:r19:r18 UID -; @clobbers R16, X - -Utils_ReadUid: - ldi xl, LOW(EEPROM_OFFS_UUID) - ldi xh, HIGH(EEPROM_OFFS_UUID) - rcall Eeprom_ReadByte ; r16=byte read (none) - brcc Utils_ReadUid_ret - mov r18, r16 - adiw xh:xl, 1 - - rcall Eeprom_ReadByte ; r16=byte read (none) - brcc Utils_ReadUid_ret - mov r19, r16 - adiw xh:xl, 1 - - rcall Eeprom_ReadByte ; r16=byte read (none) - brcc Utils_ReadUid_ret - mov r20, r16 - adiw xh:xl, 1 - - rcall Eeprom_ReadByte ; r16=byte read (none) - brcc Utils_ReadUid_ret - mov r21, r16 - -Utils_ReadUid_ret: - ret -; @end - - - -; --------------------------------------------------------------------------- -; @routine Utils_SetupUid @global -; -; Reads UID from EEPROM. If not set generate a new one and store it in EEPROM. -; -; @return CFLAG set if new generated, cleared if there already was one. -; @clobbers R16, R18, R19, R20, R21, X (R17) - -Utils_SetupUid: - rcall Utils_ReadUid ; (R16, X) - cp r18, r19 ; all the same? - brne Utils_SetupUid_uidOkay ; different, jmp - cp r18, r20 - brne Utils_SetupUid_uidOkay ; different, jmp - cp r18, r21 - brne Utils_SetupUid_uidOkay ; different, jmp - - ldi xl, LOW(EEPROM_OFFS_UUID) ; all the same, generate new uid - ldi xh, HIGH(EEPROM_OFFS_UUID) - rcall RAND_PseudoRandom ; byte 0 (R16, R17, R18, R19) - inc r16 - rcall Eeprom_WriteByteIfChanged ; (r17) - brcc Utils_SetupUid_ret - adiw xh:xl, 1 - - rcall RAND_PseudoRandom ; byte 1 - rcall Eeprom_WriteByteIfChanged ; (r17) - brcc Utils_SetupUid_ret - adiw xh:xl, 1 - - rcall RAND_PseudoRandom ; byte 2 - rcall Eeprom_WriteByteIfChanged ; (r17) - brcc Utils_SetupUid_ret - adiw xh:xl, 1 - - rcall RAND_PseudoRandom ; byte 3 - rcall Eeprom_WriteByteIfChanged ; (r17) - brcc Utils_SetupUid_ret - - rcall RAND_UpdateSeedInEeprom ; (R16, R17, R18, R19, X) - -Utils_SetupUid_uidOkay: - sec -Utils_SetupUid_ret: - ret -; @end - - - - UTILS_END: .equ MODULE_SIZE_UTILS = UTILS_END-UTILS_BEGIN diff --git a/avr/common/utils_initial_wait.asm b/avr/common/utils_initial_wait.asm index 31c9a97..4e89655 100644 --- a/avr/common/utils_initial_wait.asm +++ b/avr/common/utils_initial_wait.asm @@ -21,7 +21,7 @@ Utils_InitialWait: ; setup initial wait loop - rcall Utils_ReadUid ; (R16, X) + rcall DevUid_ReadFromEeprom ; (R16, X) clr r16 eor r16, r18 eor r16, r19 diff --git a/avr/devices/all/includes.asm b/avr/devices/all/includes.asm index ef8bd4e..939a880 100644 --- a/avr/devices/all/includes.asm +++ b/avr/devices/all/includes.asm @@ -16,17 +16,18 @@ .include "devices/all/apps_include.asm" #ifdef MODULES_NETWORK .include "devices/all/sendvalue.asm" +.include "common/utils_copy_from_flash.asm" #endif .include "devices/all/data.asm" .include "common/utils.asm" +.include "common/devuid.asm" .include "common/random.asm" .include "common/eeprom-r.asm" .include "common/eeprom-w.asm" .include "common/utils_initial_wait.asm" .include "common/utils_wait_fixed.asm" -.include "common/utils_copy_from_flash.asm" -.include "common/utils_copy_sdram.asm" +;.include "common/utils_copy_sdram.asm" .include "modules/basetimer/main.asm" diff --git a/avr/devices/all/main.asm b/avr/devices/all/main.asm index 8417709..17fd183 100644 --- a/avr/devices/all/main.asm +++ b/avr/devices/all/main.asm @@ -35,7 +35,7 @@ main: bigcall systemSetSpeed bigcall systemInitHardware bigcall RAND_SetupSeed - bigcall Utils_SetupUid + bigcall DevUid_Setup bigcall modulesInit bigcall appsInit bigcall Utils_InitialWait