; ***************************************************************************
; copyright   : (C) 2026 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_MOD_COM2WROUTER_INIT_ASM
#define AVR_MOD_COM2WROUTER_INIT_ASM



; ***************************************************************************
; defines



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

.cseg

; ---------------------------------------------------------------------------
; @routine COM2WR_Router_Init @global
;
; @clobbers r16

COM2WR_Router_Init:
  ; setup data
  clr r16
  sts com2wAllCurrentPinMask, r16
  dec r16
  sts com2wAllLastClock, r16      ; preset last clock with 1

  bigcall COM2WR_Ringbuffer_Init

  ; TODO: setup all sub-interfaces

  ; setup lines
  clr r16
  outr COM_CLK_DDR, r16           ; make all CLK pins inputs
  outr COM_DATA_DDR, r16          ; make all DATA pins inputs

.ifdef COM_CLK_PUE
  outr COM_CLK_PUE, r16           ; disable pullup resistors on CLK lines
.endif

.ifdef COM_DATA_PUE
  outr COM_DATA_PUE, r16          ; disable pullup resistors on DATA lines
.endif

  ; enable pin change irqs for all pins of given port
  ldi r16, 0xff
  outr COM_IRQ_ADDR_CLK, r16
  
  ; enable PCIEn irq
  inr r16, COM_IRQ_ADDR_M_CLK     ; enable pin change irq PCIEn
  sbr r16, (1<<COM_IRQ_BIT_M_CLK)
  outr COM_IRQ_ADDR_M_CLK, r16

  ; clear PCIEn interrupt flag
  inr r16, COM_IRQ_ADDR_F_CLK     ; enable pin change irq PCIEn
  sbr r16, (1<<COM_IRQ_BIT_F_CLK)
  outr COM_IRQ_ADDR_F_CLK, r16

  ret
; @end





#endif ; AVR_MOD_COM2WROUTER_INIT_ASM

