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



; ***************************************************************************
; code


.cseg



; ---------------------------------------------------------------------------
; Write a FLASH_READY packet.
;
; IN:
; - X  : buffer to write to
; OUT:
; - nothing
; MODIFIED REGS: R16, Z (R17, R18, R19, R20)

flashWriteFlashReady:
  ldi r16, 0xff
  st X+, r16 ; dest address (unused)
  ldi r16, 12 ; msg code+src address+ten payload bytes
  st X+, r16 ; msg len
  ldi r16, CPRO_CMD_FLASH_READY
  st X+, r16 ; msg code
  ldi r16, COM2_MAINTENANCE_ADDR
  st X+, r16 ; src address (not used)
  ; payload
  lds r16, flashUid
  st X+, r16
  lds r16, flashUid+1
  st X+, r16
  lds r16, flashUid+2
  st X+, r16
  lds r16, flashUid+3
  st X+, r16
  ldi zl, low(firmwareType*2)
  ldi zh, HIGH(firmwareType*2)
  lpm r16, Z+
  st X+, r16     ; firmwareType (low)
  lpm r16, Z+
  st X+, r16     ; firmwareType (high)
  lpm r16, Z+ 
  st X+, r16     ; firmwareVersion (low)
  lpm r16, Z+ 
  st X+, r16     ; firmwareVersion (high)
  ldi r16, LOW(PAGESIZE*2)
  st X+, r16
  ldi r16, HIGH(PAGESIZE*2)
  st X, r16
  sbiw xh:xl, 13
  rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X)
  sbiw xh:xl, 14
  ret



