; ***************************************************************************
; 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_COM2W_COM2WN_IO_H
#define AVR_MODULES_COM2W_COM2WN_IO_H


; WORK IN PROGRESS


.cseg



; ---------------------------------------------------------------------------
; @routine com2wSetupLines

com2wSetupLines:
  ; setup CLK line (as input, disable internal pull-up resistor)
  rcall com2wClkSetHigh
  
.ifdef COM_CLK_PUE
  ldd r16, Y+COM2W_IFACE_OFFS_PINMASK_CLK
  com r16
  inr r17, COM_CLK_PUE
  and r17, r16
  outr COM_CLK_PUE, r17
.endif

  ; setup DATA line (as input, disable internal pull-up resistor)
  rcall com2wnDataSetHigh
.ifdef COM_DATA_PUE
  ldd r16, Y+COM2W_IFACE_OFFS_PINMASK_DATA
  com r16
  inr r17, COM_DATA_PUE
  and r17, r16
  outr COM_DATA_PUE, r17
.endif

  ret
; @end



; ---------------------------------------------------------------------------
; @routine com2wClkSetHigh
;
; @clobbers none

com2wClkSetHigh:
  push r16
    push r17
      ldd r16, Y+COM2W_IFACE_OFFS_PINMASK_CLK
      ; clear bit in io reg (AND with complement)
      com r16                        
      inr r17, COM_CLK_DDR
      and r17, r16
      outr COM_CLK_DDR, r17          ; make pin input
.ifndef COM_CLK_PUE
      inr r17, COM_CLK_OUTPUT
      and r17, r16                   ; disable pullup
      outr COM_CLK_OUTPUT, r17
.endif
    pop r17
  pop r16
  ret
; @end



; ---------------------------------------------------------------------------
; @routine com2wnClkSetLow
;
; @param Y pointer to interface data in SRAM
; @clobbers none

com2wnClkSetLow:
  push r16
    push r17
      ldd r16, Y+COM2W_IFACE_OFFS_PINMASK_CLK
      ; set bit in io reg
      inr r17, COM_CLK_DDR
      or r17, r16
      outr COM_CLK_DDR, r17          ; make pin output

      com r16
      inr r17, COM_CLK_OUTPUT
      and r17, r16
      outr COM_CLK_OUTPUT, r17        ; set pin low

    pop r17
  pop r16
  ret
; @end



; ---------------------------------------------------------------------------
; @routine com2wnDataSetHigh
;
; @param Y pointer to interface data in SRAM
; @clobbers none

com2wnDataSetHigh:
  push r16
    push r17
      ldd r16, Y+COM2W_IFACE_OFFS_PINMASK_DATA
      ; clear bit in io reg (AND with complement)
      com r16                        
      inr r17, COM_DATA_DDR
      and r17, r16
      outr COM_DATA_DDR, r17         ; make pin input
.ifndef COM_DATA_PUE
      inr r17, COM_DATA_OUTPUT
      and r17, r16                   ; disable pullup
      outr COM_DATA_OUTPUT, r17
.endif
    pop r17
  pop r16
  ret
; @end



; ---------------------------------------------------------------------------
; @routine com2wnDataSetLow
;
; @param Y pointer to interface data in SRAM
; @clobbers none

com2wnDataSetLow:
  push r16
    push r17
      ldd r16, Y+COM2W_IFACE_OFFS_PINMASK_DATA
      ; set bit in io reg
      inr r17, COM_DATA_DDR
      or r17, r16
      outr COM_DATA_DDR, r17          ; make pin output

      com r16
      inr r17, COM_DATA_OUTPUT
      and r17, r16
      outr COM_DATA_OUTPUT, r17        ; set pin low

    pop r17
  pop r16
  ret
; @end









#endif ; AVR_MODULES_COM2W_COM2WN_IO_H

