; ***************************************************************************
; 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


; ---------------------------------------------------------------------------
; Create and send a FLASH RESPONSE packet
;
; IN:
; - R16: response code to send
; OUT:
; - nothing
; REGS: r16, X (R15, R17, R18, R19, R20, R21, R22)

flashSendFlashResponse:
  ; send flash ready message
  ldi xl, LOW(flashSendBuffer)
  ldi xh, HIGH(flashSendBuffer)
  clr r16
  rcall flashWriteFlashRsp         ; (R16, R17, R18, R19, R20)
  rjmp flashSendPacketUntilSuccess ; (R15, R16, R17, R21, R22, X)




; ---------------------------------------------------------------------------
; Write a FLASH_RESPONSE packet.
;
; IN:
; - R16: response code (0 if ok, error code otherwise)
; - X  : buffer to write to
; OUT:
; - nothing
; MODIFIED REGS: R16, R17 (R18, R19, R20)

flashWriteFlashRsp:
  clr r18
  st X+, r18 ; dest address (unused)
  ldi r18, 3 ; msg code+src address+one payload byte
  st X+, r18 ; msg len
  ldi r17, CPRO_CMD_FLASH_RSP
  st X+, r17 ; msg code
  clr r17
  st X+, r17 ; src address (not used)
  st X, r16  ; payload byte
  sbiw xh:xl, 4
  rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X)
  sbiw xh:xl, 5
  ret



