; ***************************************************************************
; 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_UARTFD_UARTFD0_ASM
#define AVR_MODULES_UARTFD_UARTFD0_ASM




.dseg

uartFd0_iface: .byte UARTFD_IFACE_SIZE



.cseg

; ---------------------------------------------------------------------------
; @routine UARTFD_Init @global
;
; @clobbers

UARTFD0_Init:
  ldi yl, LOW(uartFd0_iface)
  ldi yh, HIGH(uartFd0_iface)
  rcall NET_Interface_Init                ; (R16, R17, X)

  ldi r16, 0xff
  std Y+UARTFD_IFACE_OFFS_WBUFNUM, r16
  clr r16
  std Y+UARTFD_IFACE_OFFS_WBUFLEFT, r16

  std Y+UARTFD_IFACE_OFFS_RBUFUSED, r16
  std Y+UARTFD_IFACE_OFFS_RBUFLEFT, r16
  std Y+UARTFD_IFACE_OFFS_STATUS, r16

  M_UARTFD_SET_BAUDRATE 0
  M_UARTFD_SET_CHARFORMAT 0

  ; always receive!
  rcall uartFd0StartReading
  rcall uartFd0StartRx
  
  ret
; @end



; ---------------------------------------------------------------------------
; @routine UARTFD0_Every100ms @global
;
; @clobbers 

UARTFD0_Every100ms:
  ldi yl, LOW(uartFd0_iface)
  ldi yh, HIGH(uartFd0_iface)
  rcall NET_Interface_Periodically
  ret
; @end



; ---------------------------------------------------------------------------
; @routine UARTFD0_Run @global
;
; @return CFLAG set if something done
; @clobbers r22, (r16, r17, r18, r19, r20, r24, r25, X, Y, Z)

UARTFD0_Run:
  ldi yl, LOW(uartFd0_iface)
  ldi yh, HIGH(uartFd0_iface)
  clr r22

  push r15
    in r15, SREG
    
    cli                            ; disable irqs
    rcall uartFd0RunRead           ; (r16, r17, r18, r19, r20, r24, r25, X, Z)
    sbci r22, 0
    out SREG, r15                  ; probably enable irqs
  
    cli                            ; disable irqs
    rcall uartFd0RunWrite          ; (r16, r17, r18, r19, r24, r25, X)
    sbci r22, 0

    out SREG, r15                  ; probably enable irqs
  pop r15

  tst r22
  clc
  breq UARTFD0_Run_ret
  sec
UARTFD0_Run_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0RunWrite
;
; @param Y pointer to interface data in SRAM
; @return CFLAG set if something done
; @clobbers r19 (r16, r18, r24, r25, X)

uartFd0RunWrite:
  clr r19
  rcall uartFd0CheckWriting   ; (r16, X)
  rcall uartFd0CheckMsgSent   ; (r16, r24, r25)
  sbci r19, 0
  rcall uartFd0CheckSendMsg   ; (r16, r17, r18, X)
  sbci r19, 0
  tst r19
  clc
  breq uartFd0RunWrite_ret
  sec
uartFd0RunWrite_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckWriting
;
; @param Y pointer to interface data in SRAM
; @clobbers r16 (X)

uartFd0CheckWriting:
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  breq uartFd0CheckWriting_ret
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFLEFT
  tst r16
  breq uartFd0CheckWriting_ret
  ldd r16, Y+NET_IFACE_OFFS_WRITETIMER
  cpi r16, UARTFD_WRITE_TIMEOUT
  brcs uartFd0CheckWriting_ret
  rcall uartFd0RestartWriting  ; (r16, X)
uartFd0CheckWriting_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckSendMsg
;
; @param Y pointer to interface data in SRAM
; @return CFLAG set if something done
; @clobbers r16 (r17, r18, X)

uartFd0CheckSendMsg:
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  brne uartFd0CheckSendMsg_clcRet
  rcall NET_Interface_GetNextOutgoingMsgNum    ; (R17, R18, X)
  brcc uartFd0CheckSendMsg_ret
  rcall uartFd0StartWriting                    ; (R16, R17, X)
  sec
  rjmp uartFd0CheckSendMsg_ret
uartFd0CheckSendMsg_clcRet:
  clc
uartFd0CheckSendMsg_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckMsgSent
;
; @param Y pointer to interface data in SRAM
; @return CFLAG set if something done
; @clobbers r16 (r24, r25)

uartFd0CheckMsgSent:
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  breq uartFd0CheckMsgSent_clcRet
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFLEFT
  tst r16
  brne uartFd0CheckMsgSent_clcRet
  rcall uartFd0FinishWriting             ; (r16, r24, r25)
  sec
  rjmp uartFd0CheckMsgSent_ret
uartFd0CheckMsgSent_clcRet:
  clc
uartFd0CheckMsgSent_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0RunRead
;
; @param Y pointer to interface data in SRAM
; @return CFLAG set if something done
; @clobbers r16 (r17, r18, r19, r20, r24, r25, X, Z)

uartFd0RunRead:
  ; make sure received messages are stored ASAP
  rcall uartFd0CheckMsgReceived     ; (r16, r17, r18, r19, r20, r24, r25, X, Z)
  push r15
    in r15, SREG                    ; save CFLAG

    ; handle errors
    rcall uartFd0CheckOverrun       ; (r16, r24, r25)
    rcall uartFd0CheckHwErr         ; (r16, r24, r25)
    rcall uartFd0CheckBadMsgSize    ; (r16, r24, r25)
    rcall uartFd0CheckSkipping      ; (r16, X)
    rcall uartFd0SetSkippingSignal  ; (r16, r17)

    ; clear error flags
    ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
    cbr r16, (1<<UARTFD_IFACE_STATUS_BIT_OVERRUN) | (1<<UARTFD_IFACE_STATUS_BIT_HWERR) | (1<<UARTFD_IFACE_STATUS_BIT_BADMSGSIZE)
    std Y+UARTFD_IFACE_OFFS_STATUS, r16

    ; check timeout for reading
    rcall uartFd0CheckReading
    out SREG, r15                   ; restore CFLAG from uartFd0CheckMsgReceived
  pop r15
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0SetSkippingSignal
;
; @param Y pointer to interface data in SRAM
; @clobbers r16 (r17)

uartFd0SetSkippingSignal:
#ifdef MODULES_LED_SIGNAL
  ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
  andi r16, (1<<UARTFD_IFACE_STATUS_BIT_SKIPPING)
  brne uartFd0SetSkippingSignal_skipping
  ldi r16, T03_FLAGS_I1_SKIPPING
  rcall LedSignal_ClrFlag                          ; (r17)
  rjmp uartFd0SetSkippingSignal_ret
uartFd0SetSkippingSignal_skipping:
  ldi r16, T03_FLAGS_I1_SKIPPING
  rcall LedSignal_SetFlag                          ; (r17)
#endif
uartFd0SetSkippingSignal_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckSkipping
;
; @param Y pointer to interface data in SRAM
; @clobbers r16 (X)

uartFd0CheckSkipping:
  ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
  andi r16, (1<<UARTFD_IFACE_STATUS_BIT_SKIPPING)
  breq uartFd0CheckSkipping_ret

  ldd r16, Y+NET_IFACE_OFFS_READTIMER
  cpi r16, UARTFD_SKIPPING_TIME
  brcs uartFd0CheckSkipping_ret
  ; leave skipping mode
  push r15
    in r15, SREG
    cli
    ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
    cbr r16, (1<<UARTFD_IFACE_STATUS_BIT_SKIPPING)
    std Y+UARTFD_IFACE_OFFS_STATUS, r16

    rcall uartFd0RestartReading                      ; (R16, X)
    out SREG, r15
  pop r15
uartFd0CheckSkipping_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckReading
;
; @param Y pointer to interface data in SRAM
; @clobbers r16 (X)

uartFd0CheckReading:
  ldd r16, Y+UARTFD_IFACE_OFFS_RBUFLEFT
  tst r16
  breq uartFd0CheckReading_ret
  ldd r16, Y+UARTFD_IFACE_OFFS_RBUFUSED
  tst r16
  brne uartFd0CheckReading_ret
  ldd r16, Y+NET_IFACE_OFFS_READTIMER
  cpi r16, UARTFD_READ_TIMEOUT
  brcs uartFd0CheckReading_ret
  rcall uartFd0RestartReading  ; (r16, X)
uartFd0CheckReading_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckMsgReceived
;
; @param Y pointer to interface data in SRAM
; @return CFLAG set if something done
; @clobbers r16 (r17, r18, r19, r20, r24, r25, X, Z)

uartFd0CheckMsgReceived:
  ldd r16, Y+UARTFD_IFACE_OFFS_RBUFLEFT
  tst r16
  brne uartFd0CheckMsgReceived_clcRet
  ldd r16, Y+UARTFD_IFACE_OFFS_RBUFUSED
  tst r16
  breq uartFd0CheckMsgReceived_clcRet
  rcall uartFd0FinishReading             ; (r16, r17, r18, r19, r20, r24, r25, X, Z)
  rcall uartFd0StartReading              ; (R16, X)
  sec
  rjmp uartFd0CheckMsgReceived_ret
uartFd0CheckMsgReceived_clcRet:
  clc
uartFd0CheckMsgReceived_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckOverrun
;
; @param Y pointer to interface data in SRAM
; @clobbers r16, r24, r25

uartFd0CheckOverrun:
  ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
  andi r16, (1<<UARTFD_IFACE_STATUS_BIT_OVERRUN)
  breq uartFd0CheckOverrun_end
  ldi r16, NET_IFACE_OFFS_ERR_MISSED_LOW
  rcall NET_Interface_IncCounter16              ; (R24, R25)
uartFd0CheckOverrun_end:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckHwErr
;
; @param Y pointer to interface data in SRAM
; @clobbers r16, r24, r25

uartFd0CheckHwErr:
  ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
  andi r16, (1<<UARTFD_IFACE_STATUS_BIT_HWERR)
  breq uartFd0CheckHwErr_end
  ldi r16, NET_IFACE_OFFS_ERR_IO_LOW
  rcall NET_Interface_IncCounter16              ; (R24, R25)
uartFd0CheckHwErr_end:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0CheckBadMsgSize
;
; @param Y pointer to interface data in SRAM
; @clobbers r16, r24, r25

uartFd0CheckBadMsgSize:
  ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
  andi r16, (1<<UARTFD_IFACE_STATUS_BIT_BADMSGSIZE)
  breq uartFd0CheckBadMsgSize_end
  ldi r16, NET_IFACE_OFFS_ERR_MSGSIZE_LOW
  rcall NET_Interface_IncCounter16              ; (R24, R25)
uartFd0CheckBadMsgSize_end:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0StartReading
;
; @param Y pointer to interface data in SRAM
; @clobbers R16, X

uartFd0StartReading:
  rjmp uartFd0RestartReading ; (R16, X)
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0RestartReading
;
; @param Y pointer to interface data in SRAM
; @clobbers R16, X

uartFd0RestartReading:
  mov xl, yl
  mov xh, yh
  adiw xh:xl, UARTFD_IFACE_OFFS_RBUFFER
  std Y+UARTFD_IFACE_OFFS_RPOS_LOW, xl
  std Y+UARTFD_IFACE_OFFS_RPOS_HIGH, xh
  clr r16
  std Y+UARTFD_IFACE_OFFS_RBUFUSED, r16
  std Y+NET_IFACE_OFFS_READTIMER, r16
  ldi r16, UARTFD_IFACE_SIZE_BUFFER
  std Y+UARTFD_IFACE_OFFS_RBUFLEFT, r16
  ; clear error flags
  ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
  cbr r16, (1<<UARTFD_IFACE_STATUS_BIT_OVERRUN) | (1<<UARTFD_IFACE_STATUS_BIT_HWERR) | (1<<UARTFD_IFACE_STATUS_BIT_BADMSGSIZE)
  std Y+UARTFD_IFACE_OFFS_STATUS, r16
  
  push r15
    in r15, SREG
    
    cli                            ; disable irqs
    M_UARTFD_FLUSH_IN 0

    out SREG, r15                  ; probably enable irqs
  pop r15

;  rcall uartFd0StopRx
;  nop
;  rcall uartFd0StartRx
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0FinishReading
;
; @param Y pointer to interface data in SRAM
; @clobbers r16, r17, r18, X, Z (r19, r20, r24, r25)

uartFd0FinishReading:
  ; check message
  mov xl, yl
  mov xh, yh
  adiw xh:xl, UARTFD_IFACE_OFFS_RBUFFER
  rcall NETMSG_CheckMessageInBuffer       ; (R16, R17, R18, R19, R20, X)
  brcc uartFd0FinishReading_errorCrc
  ; allocate buffer for storage
  rcall NET_Buffer_Alloc                  ; R16=number, X=ptr (R16, R17, X)
  brcc uartFd0FinishReading_errorMem
  ldd r18, Y+NET_IFACE_OFFS_IFACENUM
  andi r18, 0x0f
  ld r17, X
  andi r17, 0xf0
  or r17, r18
  st X+, r17                              ; store interface number
  ; copy to newly allocated buffer
  mov zl, yl
  mov zh, yh
  adiw zh:zl, UARTFD_IFACE_OFFS_RBUFFER
  ldd r17, Y+UARTFD_IFACE_OFFS_RBUFUSED
uartFd0FinishReading_copyLoop:
    ld r18, Z+
    st X+, r18
    dec r17
  brne uartFd0FinishReading_copyLoop
  ; add new buffer to incoming list
  rcall NET_AddIncomingMsgNum
  brcs uartFd0FinishReading_msgSaved
  rcall NET_Buffer_ReleaseByNum           ; (R16, X)
  rjmp uartFd0FinishReading_errorMem
uartFd0FinishReading_msgSaved:
#ifdef LED1_NETWORK_ACTIVITY
  rcall LED1_SetActivityTiming           ; (r18-r20)
#endif
  ldi r16, NET_IFACE_OFFS_PACKETSIN_LOW
  rjmp uartFd0FinishReading_incCounter
uartFd0FinishReading_errorCrc:
  ldi r16, NET_IFACE_OFFS_ERR_CONTENT_LOW
  rjmp uartFd0FinishReading_incCounter
uartFd0FinishReading_errorMem:
  ldi r16, NET_IFACE_OFFS_ERR_NOBUF_LOW
uartFd0FinishReading_incCounter:
  rjmp NET_Interface_IncCounter16        ; (R24, R25)
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0StartWriting
;
; @param R16 number of buffer to write
; @param Y pointer to interface data in SRAM
; @clobbers R16, X (R17)

uartFd0StartWriting:
  std Y+UARTFD_IFACE_OFFS_WBUFNUM, r16
  rjmp uartFd0RestartWriting
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0RestartWriting
;
; @param Y pointer to interface data in SRAM
; @clobbers R16, X (R17)

uartFd0RestartWriting:
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  breq uartFd0RestartWriting_ret
  rcall NET_Buffer_Locate                 ; (R17)
  brcc uartFd0RestartWriting_ret
  adiw xh:xl, 1
  adiw xh:xl, NETMSG_OFFS_MSGLEN
  ld r16, X
  sbiw xh:xl, NETMSG_OFFS_MSGLEN
  subi r16, -3                             ; add destaddr, length, crc
  std Y+UARTFD_IFACE_OFFS_WBUFLEFT, r16
  std Y+UARTFD_IFACE_OFFS_WPOS_LOW, xl
  std Y+UARTFD_IFACE_OFFS_WPOS_HIGH, xh
  rcall uartFd0StartTx                    ; (r16)
uartFd0RestartWriting_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0FinishWriting
;
; @param R16 number of buffer to write
; @param Y pointer to interface data in SRAM
; @clobbers r16 (r24, r25)

uartFd0FinishWriting:
  rcall uartFd0StopTx                    ; (r16)
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  breq uartFd0FinishWriting_ret
  rcall NET_Buffer_ReleaseByNum          ; (r16, X)
  ldi r16, 0xff
  std Y+UARTFD_IFACE_OFFS_WBUFNUM, r16
  ldi r16, NET_IFACE_OFFS_PACKETSOUT_LOW
  rcall NET_Interface_IncCounter16       ; (R24, R25)
uartFd0FinishWriting_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0StartRx
;
; @clobbers R16

uartFd0StartRx:
  M_UARTFD_START_RX 0
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0StopRx
;
; @clobbers R16

uartFd0StopRx:
  M_UARTFD_STOP_RX 0
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0StartTx
;
; @clobbers R16

uartFd0StartTx:
  M_UARTFD_START_TX 0
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0StopTx
;
; @clobbers R16

uartFd0StopTx:
;  M_UARTFD_STOP_TX 0
  M_UARTFD_STOP_TXIRQS 0
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0RxChar
;
; @clobbers r16, r17, X

uartFd0RxChar:
  M_UARTFD_RXCHAR_ISR 0
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0TxUdre
;
; @clobbers r16, r17, X

uartFd0TxUdre:
  M_UARTFD_TXUDRE_ISR 0
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd0TxChar
;
; @clobbers 

uartFd0TxChar:
  M_UARTFD_TXCHAR_ISR 0
  ret
; @end



; ---------------------------------------------------------------------------
; @routine UARTFD0_RxCharIsr @global @isr
;
; @clobbers none

UARTFD0_RxCharIsr:
  push r15
    in r15, SREG
    push yl
      push yh
        ldi yl, LOW(uartFd0_iface)
        ldi yh, HIGH(uartFd0_iface)
        push r16
          push r17
            push xl
              push xh
                rcall uartFd0RxChar
              pop xh
            pop xl
          pop r17
        pop r16
      pop yh
    pop yl
    out SREG, r15
  pop r15
  reti
; @end



; ---------------------------------------------------------------------------
; @routine UARTFD0_TxUdreIsr @global @isr
;
; @clobbers none

UARTFD0_TxUdreIsr:
  push r15
    in r15, SREG
    push yl
      push yh
        ldi yl, LOW(uartFd0_iface)
        ldi yh, HIGH(uartFd0_iface)
        push r16
          push r17
            push xl
              push xh
                rcall uartFd0TxUdre
              pop xh
            pop xl
          pop r17
        pop r16
      pop yh
    pop yl
    out SREG, r15
  pop r15
  reti
; @end



; ---------------------------------------------------------------------------
; @routine UARTFD0_TxCharIsr @global @isr
;
; @clobbers none

UARTFD0_TxCharIsr:
  push r15
    in r15, SREG
    push yl
      push yh
        ldi yl, LOW(uartFd0_iface)
        ldi yh, HIGH(uartFd0_iface)
        push r16
          push r17
            push xl
              push xh
                rcall uartFd0TxChar
              pop xh
            pop xl
          pop r17
        pop r16
      pop yh
    pop yl
    out SREG, r15
  pop r15
  reti
; @end










#endif
