Files
aqhomecontrol/avr/common/utils.asm
2026-04-14 23:45:32 +02:00

196 lines
4.2 KiB
NASM

; ***************************************************************************
; 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_UTILS_ASM
#define AQH_AVR_COMMON_UTILS_ASM
; ***************************************************************************
; code
.cseg
UTILS_BEGIN:
utilsDateString: .db "%YEAR%-%MONTH%-%DAY%-%HOUR%:%MINUTE%", 0, 0
; ---------------------------------------------------------------------------
; @routine Utils_FillSram @global
;
; @return X points directly behind filled memory
; @param X pointer to SRAM to fill
; @param r16 value to fill the SRAM with
; @param r17 size of area to fill
; @clobbers r17, X
Utils_FillSram:
tst r17
breq Utils_FillSram_end
Utils_FillSram_loop:
st x+, r16
dec r17
brne Utils_FillSram_loop
Utils_FillSram_end:
ret
; @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
#endif