; ***************************************************************************
; 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_INIT_H
#define AVR_MODULES_COM2W_COM2WN_INIT_H


; WORK IN PROGRESS


; ---------------------------------------------------------------------------
; @macro mCOM2WN_INIT
;
; @param @0 interface number (beginning with 0)

.macro mCOM2WN_INIT
  ldi yl, LOW(com2w@0_iface)
  ldi yh, HIGH(com2w@0_iface)
  rcall com2wnInit
  ldi r16, COM_MASK_CLK@0
  std Y+COM2W_IFACE_OFFS_PINMASK_CLK, r16
  ldi r16, COM_MASK_DATA@0
  std Y+COM2W_IFACE_OFFS_PINMASK_DATA, r16
  ldi r16, COM_MASK_IRQ@0
  std Y+COM2W_IFACE_OFFS_PINMASK_IRQ, r16
.endmacro
; @end





.cseg



; ---------------------------------------------------------------------------
; @routine COM2WN_Init
;

COM2WN_Init:
  ldi r16, 0xff                                 ; start with all clock lines hi
  sts com2wnIoCurrentClockStates, r16
  sts com2wnIoLastClockStates, r16

  clr r16
  sts com2wnIoFlags, r16
  sts com2wnIoDisabledClockMask, r16

  ldi yl, LOW(com2wnIoRingBuffer)
  ldi yh, HIGH(com2wnIoRingBuffer)
  ldi r16, COM2WN_IO_RINGBUFFER_SIZE
  rcall RingBufferY_Init                        ; (R17)
  
.if COM_PORTS >0
  mCOM2WN_INIT 0
.endif
.if COM_PORTS >1
  mCOM2WN_INIT 1
.endif

.if COM_PORTS >2
  mCOM2WN_INIT 2
.endif
.if COM_PORTS >3
  mCOM2WN_INIT 3
.endif

.if COM_PORTS >4
  mCOM2WN_INIT 4
.endif
.if COM_PORTS >5
  mCOM2WN_INIT 5
.endif

.if COM_PORTS >6
  mCOM2WN_INIT 6
.endif
.if COM_PORTS >7
  mCOM2WN_INIT 7
.endif

  rcall com2wnSetupIrq

  ret
; @end



; ---------------------------------------------------------------------------
; @routine com2wInit
;

com2wnInit:
  rcall NET_Interface_Init                 ; (R16, R17, X)
  ldi r16, COM2W_MODE_IDLE
  rcall com2wSetMode                       ; (R17)

  rcall com2wSetupLines
  rcall com2wnEnableClkIrq
  ret
; @end



#endif ; AVR_MODULES_COM2W_COM2WN_INIT_H


