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

#ifndef AVR_MODULES_UART_HW2_SYNC_H
#define AVR_MODULES_UART_HW2_SYNC_H


.dseg


.cseg




; sync length is 1000 us, so this should return:
; -  125 for 1 MHz
; - 1000 for 8 MHz

syncRecv:
syncRecv_waitForHigh:
    sbis COM_DATA0_INPUT, COM_DATA0_PIN
  rjmp syncRecv_waitForHigh

  clr r24
  clr r25

syncRecv_waitForLow:
    sbic COM_DATA0_INPUT, COM_DATA0_PIN
  rjmp syncRecv_waitForLow

syncRecv_countLow:                            ; total: +8 per loop
    sbic COM_DATA0_INPUT, COM_DATA0_PIN       ; +1 if noskip, +2 if skip
      rjmp syncRecv_countDone                 ; +2, total: +2 every loop, +3 in last loop
    adiw r25:r24, 1                           ; +1
    breq syncRecv_countError                  ; +1 if not 0, +2 if 0
    nop                                       ; +1
    nop                                       ; +1
  rjmp syncRecv_countLow                      ; +2

syncRecv_countError:
  clr r24
  clr r25
syncRecv_countDone:
  ret
; @end




#endif

