; ***************************************************************************
; 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_MODULES_COM2W2_MACROS_H
#define AVR_MODULES_COM2W2_MACROS_H



.macro M_COM2WCLKSETLOW
  sbi COM_CLK_DDR, COM_CLK_PIN          ; set CLK as output
  cbi COM_CLK_OUTPUT, COM_CLK_PIN       ; set CLK low
.endmacro



.macro M_COM2WCLKSETHIGH
  cbi COM_CLK_DDR, COM_CLK_PIN           ; set CLK as input
.ifndef COM_CLK_PUE
  cbi COM_CLK_OUTPUT, COM_CLK_PIN        ; disable pullup on CLK
.endif
.endmacro



.macro M_COM2WDATASETLOW
  sbi COM_DATA_DDR, COM_DATA_PIN          ; set DATA as output
  cbi COM_DATA_OUTPUT, COM_DATA_PIN       ; set DATA low
.endmacro



.macro M_COM2WDATASETHIGH
  cbi COM_DATA_DDR, COM_DATA_PIN           ; set DATA as input
.ifndef COM_DATA_PUE
  cbi COM_DATA_OUTPUT, COM_DATA_PIN        ; disable pullup on DATA
.endif
.endmacro



.macro M_COM2WSETUPINT0
  inr r16, MCUCR
  cbr r16, (1<<ISC01) | (1<<ISC00)
  sbr r16, (1<<ISC01) | (0<<ISC00)        ; falling edge of ATTN
  outr MCUCR, r16
;  sbr r16, (0<<ISC01) | (0<<ISC00)         ; low level triggers
  inr r16, COM_IRQ_ADDR_CLK               ; enable irq for ATTN line
  sbr r16, (1<<COM_IRQ_BIT_CLK)
  outr COM_IRQ_ADDR_CLK, r16
.endmacro



.macro M_COM2WSETUPPCI
  inr r16, COM_IRQ_ADDR_CLK               ; enable pin change irq for CLK line
  ori r16, (1<<COM_IRQ_BIT_CLK)
  outr COM_IRQ_ADDR_CLK, r16
;  sbi COM_IRQ_ADDR_CLK, COM_IRQ_BIT_CLK   ; enable pin change irq for CLK line
  in r16, GIMSK                           ; enable pin change irq PCIE0 or PCIE1
  ori r16, (1<<COM_IRQ_GIMSK_CLK)
  out GIMSK, R16
  ldi r16, (1<<COM_IRQ_GIFR_CLK)          ; clear pending irq by writing 1 to CLK bit
  out GIFR, r16
.endmacro



.macro M_COM2WSETUPPCICR
  inr r16, COM_IRQ_ADDR_CLK               ; enable pin change irq for CLK line
  ori r16, (1<<COM_IRQ_BIT_CLK)
  outr COM_IRQ_ADDR_CLK, r16

  inr r16, COM_IRQ_ICR_ADDR               ; enable pin change irq in PCICR
  ori r16, (1<<COM_IRQ_ICR_BIT)
  outr COM_IRQ_ICR_ADDR, R16
  ldi r16, (1<<COM_IRQ_IFR_BIT)           ; clear pending irq by writing 1 to CLK bit
  out COM_IRQ_IFR_ADDR, r16
.endmacro




#endif

