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



; ---------------------------------------------------------------------------
; wait indefinately until ATTN line is high, send packet over wire, retry until successfull
;
; IN:
; - x  : ptr to buffer to send
; OUT:
; - nothing
; REGS: (R16, R17, R18, R21, R22, R24, R25, X)

flashSendPacketUntilSuccess:
    push xl
      push xh
        rcall COM2_SendPacketWithAttn ; (R16, R17, R21, R22, X)
      pop xh
    pop xl
    brcc flashSendPacket_error
    ret
flashSendPacket_error:
    rcall flashWaitForAttnHigh        ; (R16, R17, R18, R22, R24, R25, X)
  rjmp flashSendPacketUntilSuccess



; ---------------------------------------------------------------------------
; wait indefinately for free ATTN line
;
; IN:
; - nothing
; OUT:
; - nothing
; REGS: (R16, R17, R18, R22, R24, R25, X)

flashWaitForAttnHigh:
    rcall com2WaitForAttnHigh             ; (r17, r22, X)
    brcc flashWaitForAttnHigh_stillLow
    ret
flashWaitForAttnHigh_stillLow:
    rcall flashWaitDependingOnUid         ; (R16, R18, R22, R24, R25)
  rjmp flashWaitForAttnHigh




