; ***************************************************************************
; copyright   : (C) 2025 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.   *
; ***************************************************************************


; ---------------------------------------------------------------------------
; flashReadEepromIncr
;
; Read a byte from EEPROM (see example in ATtiny24/44/84 manual p.19).
;
; IN:
; - X: EEPROM Address to read from
; OUT:
; - R16: byte read
; - X: EEPROM Address incremented
; REGS: R16

flashReadEepromIncr:
.ifdef EEPE
  sbic EECR, EEPE          ; wait for previous write to complete (if any)
.else
  sbic EECR, EEWE          ; wait for previous write to complete (if any)
.endif
    rjmp flashReadEepromIncr
  out EEARH, xh            ; set EEPROM address
  out EEARL, xl
  sbi EECR, EERE           ; start EEPROM read by writing EERE
  in r16, EEDR             ; read data from data register
  adiw xh:xl, 1
  ret



; ---------------------------------------------------------------------------
; @routine flashReadUidIntoSdram
;
; Read UID from EEPROM.
;
; @clobbers R16, X, Y

flashReadUidIntoSdram:
  ldi yh, HIGH(flashUid)
  ldi yl, LOW(flashUid)
  push r15
    in r15, SREG
    cli
    ldi xl, LOW(EEPROM_OFFS_UUID)
    ldi xh, HIGH(EEPROM_OFFS_UUID)
    rcall flashReadEepromIncr ; (R16)
    st Y+, r16
    rcall flashReadEepromIncr ; (R16)
    st Y+, r16
    rcall flashReadEepromIncr ; (R16)
    st Y+, r16
    rcall flashReadEepromIncr ; (R16)
    st Y+, r16
    out SREG, r15
  pop r15
  ret
; @end



