avr: reorganized common code.

This commit is contained in:
Martin Preuss
2026-04-20 23:56:52 +02:00
parent 2d2105ae00
commit abe218e7b0
12 changed files with 281 additions and 155 deletions

View File

@@ -224,7 +224,7 @@ appNetworkHandlePingRequest_end:
appNetworkHandleReeunumRequest: appNetworkHandleReeunumRequest:
push xl push xl
push xh 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 xh
pop xl pop xl
rcall NETMSG_Range_Read ; r20=range begin, r21=range end (none) rcall NETMSG_Range_Read ; r20=range begin, r21=range end (none)

View File

@@ -3,16 +3,31 @@
<gwbuild> <gwbuild>
<extradist> <extradist>
calls.asm
crc8.asm crc8.asm
debug.asm debug.asm
devuid.asm
divide.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_fixedbuffers.asm
m_ringbuffer.asm m_ringbuffer.asm
m_ringbuffer_y.asm m_ringbuffer_y.asm
multiply.asm multiply.asm
random.asm
ressource.asm
ringbuffer.asm ringbuffer.asm
ringbuffer_y.asm ringbuffer_y.asm
shared.asm shared.asm
tree.asm
tree_t.asm
utils.asm utils.asm
utils_copy_from_flash.asm utils_copy_from_flash.asm
utils_copy_sdram.asm utils_copy_sdram.asm
@@ -26,14 +41,6 @@
wait_1ms.asm wait_1ms.asm
wait_50us.asm wait_50us.asm
watchdog.asm watchdog.asm
list.asm
list_t.asm
tree.asm
tree_t.asm
eeprom-r.asm
eeprom-w.asm
ressource.asm
itoa.asm
</extradist> </extradist>
</gwbuild> </gwbuild>

112
avr/common/devuid.asm Normal file
View File

@@ -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

View File

@@ -60,7 +60,6 @@ Eeprom_ReadByte_ret:
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; @routine Eeprom_CheckAddr ; @routine Eeprom_CheckAddr
; ;
;
; @param X EEPROM Address to validate ; @param X EEPROM Address to validate
; @return CFLAG set if address okay, cleared if out of range ; @return CFLAG set if address okay, cleared if out of range
; @clobbers r16 ; @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 #endif ; AQH_AVR_COMMON_EEPROM_R_H

View File

@@ -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 #endif ; AQH_AVR_COMMON_EEPROM_W_H

View File

@@ -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

View File

@@ -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

View File

@@ -212,8 +212,8 @@ List_UnlinkAllObjects_loop:
; Calls the given function for every object until it ; Calls the given function for every object until it
; returns with a set carry flag or until the full list ; returns with a set carry flag or until the full list
; is handled. ; is handled.
; Registers that can be used by the given function are all ; All registers that can be used by the given function, however
; except R16, R18 and R19 (those are used by this routine). ; R16, R18, R19 and Y are modified between function calls.
; ;
; @param Y pointer to first object in a list ; @param Y pointer to first object in a list
; @param Z routine to call for every object ; @param Z routine to call for every object
@@ -223,9 +223,6 @@ List_ForEveryObject:
List_ForEveryObject_loop: List_ForEveryObject_loop:
ldd r18, Y+LIST_OFFS_NEXT_LO ; next ldd r18, Y+LIST_OFFS_NEXT_LO ; next
ldd r19, Y+LIST_OFFS_NEXT_HI ; 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 r18 ; next
push r19 ; next push r19 ; next
rcall List_ForEveryObject_callZ rcall List_ForEveryObject_callZ

View File

@@ -25,7 +25,6 @@ utilsDateString: .db "%YEAR%-%MONTH%-%DAY%-%HOUR%:%MINUTE%", 0, 0
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; @routine Utils_FillSram @global ; @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: UTILS_END:
.equ MODULE_SIZE_UTILS = UTILS_END-UTILS_BEGIN .equ MODULE_SIZE_UTILS = UTILS_END-UTILS_BEGIN

View File

@@ -21,7 +21,7 @@
Utils_InitialWait: Utils_InitialWait:
; setup initial wait loop ; setup initial wait loop
rcall Utils_ReadUid ; (R16, X) rcall DevUid_ReadFromEeprom ; (R16, X)
clr r16 clr r16
eor r16, r18 eor r16, r18
eor r16, r19 eor r16, r19

View File

@@ -16,17 +16,18 @@
.include "devices/all/apps_include.asm" .include "devices/all/apps_include.asm"
#ifdef MODULES_NETWORK #ifdef MODULES_NETWORK
.include "devices/all/sendvalue.asm" .include "devices/all/sendvalue.asm"
.include "common/utils_copy_from_flash.asm"
#endif #endif
.include "devices/all/data.asm" .include "devices/all/data.asm"
.include "common/utils.asm" .include "common/utils.asm"
.include "common/devuid.asm"
.include "common/random.asm" .include "common/random.asm"
.include "common/eeprom-r.asm" .include "common/eeprom-r.asm"
.include "common/eeprom-w.asm" .include "common/eeprom-w.asm"
.include "common/utils_initial_wait.asm" .include "common/utils_initial_wait.asm"
.include "common/utils_wait_fixed.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" .include "modules/basetimer/main.asm"

View File

@@ -35,7 +35,7 @@ main:
bigcall systemSetSpeed bigcall systemSetSpeed
bigcall systemInitHardware bigcall systemInitHardware
bigcall RAND_SetupSeed bigcall RAND_SetupSeed
bigcall Utils_SetupUid bigcall DevUid_Setup
bigcall modulesInit bigcall modulesInit
bigcall appsInit bigcall appsInit
bigcall Utils_InitialWait bigcall Utils_InitialWait