; ***************************************************************************
; 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_UARTFD1_ASM
#define AVR_MODULES_UARTFD_UARTFD1_ASM

; this is the tested version!



.dseg

uartFd1_iface: .byte UARTFD_IFACE_SIZE



.cseg

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

UARTFD1_Init:
  ldi yl, LOW(uartFd1_iface)
  ldi yh, HIGH(uartFd1_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 1
  M_UARTFD_SET_CHARFORMAT 1

  ; always receive!
  rcall uartFd1StartReading
  rcall uartFd1StartRx
  
  ret
; @end



; ---------------------------------------------------------------------------
; @routine UARTFD1_Every100ms @global
;
; @clobbers 

UARTFD1_Every100ms:
  ldi yl, LOW(uartFd1_iface)
  ldi yh, HIGH(uartFd1_iface)
  rcall NET_Interface_Periodically
  ret
; @end



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

UARTFD1_Run:
  ldi yl, LOW(uartFd1_iface)
  ldi yh, HIGH(uartFd1_iface)
  clr r22

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

    out SREG, r15                  ; probably enable irqs
  pop r15

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



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

uartFd1RunWrite:
  clr r19
  rcall uartFd1CheckWriting   ; (r16, X)
  rcall uartFd1CheckMsgSent   ; (r16, r24, r25)
  sbci r19, 0
  rcall uartFd1CheckSendMsg   ; (r16, r17, r18, X)
  sbci r19, 0
  tst r19
  clc
  breq uartFd1RunWrite_ret
  sec
uartFd1RunWrite_ret:
  ret
; @end



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

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



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

uartFd1CheckSendMsg:
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  brne uartFd1CheckSendMsg_clcRet
  rcall NET_Interface_GetNextOutgoingMsgNum    ; (R17, R18, X)
  brcc uartFd1CheckSendMsg_ret
  rcall uartFd1StartWriting                    ; (R16, R17, X)
  sec
  rjmp uartFd1CheckSendMsg_ret
uartFd1CheckSendMsg_clcRet:
  clc
uartFd1CheckSendMsg_ret:
  ret
; @end



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

uartFd1CheckMsgSent:
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  breq uartFd1CheckMsgSent_clcRet
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFLEFT
  tst r16
  brne uartFd1CheckMsgSent_clcRet
  rcall uartFd1FinishWriting             ; (r16, r24, r25)
  sec
  rjmp uartFd1CheckMsgSent_ret
uartFd1CheckMsgSent_clcRet:
  clc
uartFd1CheckMsgSent_ret:
  ret
; @end



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

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

    ; handle errors
    rcall uartFd1CheckOverrun       ; (r16, r24, r25)
    rcall uartFd1CheckHwErr         ; (r16, r24, r25)
    rcall uartFd1CheckBadMsgSize    ; (r16, r24, r25)
    rcall uartFd1CheckSkipping      ; (r16, X)
    rcall uartFd1SetSkippingSignal  ; (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 uartFd1CheckReading
    out SREG, r15                   ; restore CFLAG from uartFd1CheckMsgReceived
  pop r15
  ret
; @end



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

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



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

uartFd1CheckSkipping:
  ldd r16, Y+UARTFD_IFACE_OFFS_STATUS
  andi r16, (1<<UARTFD_IFACE_STATUS_BIT_SKIPPING)
  breq uartFd1CheckSkipping_ret

  ldd r16, Y+NET_IFACE_OFFS_READTIMER
  cpi r16, UARTFD_SKIPPING_TIME
  brcs uartFd1CheckSkipping_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 uartFd1RestartReading                      ; (R16, X)
    out SREG, r15
  pop r15
uartFd1CheckSkipping_ret:
  ret
; @end



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

uartFd1CheckReading:
  ldd r16, Y+UARTFD_IFACE_OFFS_RBUFLEFT
  tst r16
  breq uartFd1CheckReading_ret
  ldd r16, Y+UARTFD_IFACE_OFFS_RBUFUSED
  tst r16
  brne uartFd1CheckReading_ret
  ldd r16, Y+NET_IFACE_OFFS_READTIMER
  cpi r16, UARTFD_READ_TIMEOUT
  brcs uartFd1CheckReading_ret
  rcall uartFd1RestartReading  ; (r16, X)
uartFd1CheckReading_ret:
  ret
; @end



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

uartFd1CheckMsgReceived:
  ldd r16, Y+UARTFD_IFACE_OFFS_RBUFLEFT
  tst r16
  brne uartFd1CheckMsgReceived_clcRet
  ldd r16, Y+UARTFD_IFACE_OFFS_RBUFUSED
  tst r16
  breq uartFd1CheckMsgReceived_clcRet
  rcall uartFd1FinishReading             ; (r16, r17, r18, r19, r20, r24, r25, X, Z)
  rcall uartFd1StartReading              ; (R16, X)
  sec
  rjmp uartFd1CheckMsgReceived_ret
uartFd1CheckMsgReceived_clcRet:
  clc
uartFd1CheckMsgReceived_ret:
  ret
; @end



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

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



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

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



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

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



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

uartFd1StartReading:
  rjmp uartFd1RestartReading ; (R16, X)
; @end



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

uartFd1RestartReading:
  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 1

    out SREG, r15                  ; probably enable irqs
  pop r15

;  rcall uartFd1StopRx
;  nop
;  rcall uartFd1StartRx
  ret
; @end



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

uartFd1FinishReading:
  ; 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 uartFd1FinishReading_errorCrc
  ; allocate buffer for storage
  rcall NET_Buffer_Alloc                  ; R16=number, X=ptr (R16, R17, X)
  brcc uartFd1FinishReading_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
uartFd1FinishReading_copyLoop:
    ld r18, Z+
    st X+, r18
    dec r17
  brne uartFd1FinishReading_copyLoop
  ; add new buffer to incoming list
  rcall NET_AddIncomingMsgNum
  brcs uartFd1FinishReading_msgSaved
  rcall NET_Buffer_ReleaseByNum           ; (R16, X)
  rjmp uartFd1FinishReading_errorMem
uartFd1FinishReading_msgSaved:
#ifdef LED1_NETWORK_ACTIVITY
  rcall LED1_SetActivityTiming           ; (r18-r20)
#endif
  ldi r16, NET_IFACE_OFFS_PACKETSIN_LOW
  rjmp uartFd1FinishReading_incCounter
uartFd1FinishReading_errorCrc:
  ldi r16, NET_IFACE_OFFS_ERR_CONTENT_LOW
  rjmp uartFd1FinishReading_incCounter
uartFd1FinishReading_errorMem:
  ldi r16, NET_IFACE_OFFS_ERR_NOBUF_LOW
uartFd1FinishReading_incCounter:
  rjmp NET_Interface_IncCounter16        ; (R24, R25)
; @end



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

uartFd1StartWriting:
  std Y+UARTFD_IFACE_OFFS_WBUFNUM, r16
  rjmp uartFd1RestartWriting
; @end



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

uartFd1RestartWriting:
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  breq uartFd1RestartWriting_ret
  rcall NET_Buffer_Locate                 ; (R17)
  brcc uartFd1RestartWriting_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 uartFd1StartTx                    ; (r16)
uartFd1RestartWriting_ret:
  ret
; @end



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

uartFd1FinishWriting:
  rcall uartFd1StopTx                    ; (r16)
  ldd r16, Y+UARTFD_IFACE_OFFS_WBUFNUM
  cpi r16, 0xff
  breq uartFd1FinishWriting_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)
uartFd1FinishWriting_ret:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd1StartRx
;
; @clobbers R16

uartFd1StartRx:
  M_UARTFD_START_RX 1
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd1StopRx
;
; @clobbers R16

uartFd1StopRx:
  M_UARTFD_STOP_RX 1
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd1StartTx
;
; @clobbers R16

uartFd1StartTx:
  M_UARTFD_START_TX 1
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd1StopTx
;
; @clobbers R16

uartFd1StopTx:
;  M_UARTFD_STOP_TX 1
  M_UARTFD_STOP_TXIRQS 1
  ret
; @end



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

uartFd1RxChar:
  M_UARTFD_RXCHAR_ISR 1
  ret
; @end



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

uartFd1TxUdre:
  M_UARTFD_TXUDRE_ISR 1
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartFd1TxChar
;
; @clobbers 

uartFd1TxChar:
  M_UARTFD_TXCHAR_ISR 1
  ret
; @end



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

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



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

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



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

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










#endif



