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



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

.cseg


; ---------------------------------------------------------------------------
; @routine ATTN_Init @global
;
; @clobbers R16

ATTN_Init:
.ifdef INT0
  .if COM_IRQ_BIT_ATTN == INT0
    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
  .endif
.endif
  
  rcall ATTN_SetHighEnableIrq
  
  ret
; @end




; ---------------------------------------------------------------------------
; @routine ATTN_SetLowDisableIrq @global
;
; @clobbers R16

ATTN_SetLowDisableIrq:
.ifdef INT0
  .if COM_IRQ_BIT_ATTN == INT0
    inr r16, COM_IRQ_ADDR_ATTN        ; disable irq for ATTN line
    cbr r16, (1<<COM_IRQ_BIT_ATTN)
    outr COM_IRQ_ADDR_ATTN, r16
  .endif
.endif

  sbi COM_ATTN_DDR, COM_ATTN_PIN            ; set ATTN as output
  cbi COM_ATTN_OUTPUT, COM_ATTN_PIN         ; set ATTN low
  
  ret
; @end



; ---------------------------------------------------------------------------
; @routine ATTN_SetHighEnableIrq @global
;
; @clobbers R16

ATTN_SetHighEnableIrq:
  cbi COM_ATTN_DDR, COM_ATTN_PIN           ; set ATTN as input
.ifdef COM_ATTN_PUE
  cbi COM_ATTN_PUE, COM_ATTN_PIN           ; disable pullup on ATTN
.else
  cbi COM_ATTN_OUTPUT, COM_ATTN_PIN        ; disable pullup on ATTN
.endif

.ifdef INT0
  .if COM_IRQ_BIT_ATTN == INT0
    inr r16, COM_IRQ_ADDR_ATTN             ; enable irq for ATTN line
    sbr r16, (1<<COM_IRQ_BIT_ATTN)
    outr COM_IRQ_ADDR_ATTN, r16
  .endif
.endif

  ret
; @end



; ---------------------------------------------------------------------------
; @routine ATTN_IsHigh @global
;
; @return CFLAG set if ATTN is high
; @clobbers none

ATTN_IsHigh:
  clc
  sbic COM_ATTN_INPUT, COM_ATTN_PIN     ; ATTN low?
    sec                                 ; yes, set CF
  ret
; @end




