; ***************************************************************************
; copyright   : (C) 2024 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.   *
; ***************************************************************************



; ---------------------------------------------------------------------------
; @macro UART_BB_M_WAIT_FOR_PIN_LOW IN_REG_DATA, IN_PINNUM
;                                 0            1
; Wait for a pin to become low
; @param %0 DATA register for input pin (e.g. PINB)
; @param %1 pin number for input (e.g. PORTB1)
; @return CFLAG set if okay, clear otherwise
; @clobbers R17, R22

.macro UART_BB_M_WAIT_FOR_PIN_LOW
  ldi r17, 200
l_loop:
    sbis @0, @1
      rjmp l_reached
    Utils_WaitNanoSecs 5000, 0, r22                ; wait for 5us
    dec r17
  brne l_loop
  clc
  rjmp l_end
l_reached:
  sec
l_end:
.endmacro
; @end



; ---------------------------------------------------------------------------
; @macro UART_BB_M_WAIT_FOR_PIN_HIGH IN_REG_DATA, IN_PINNUM
;                                 0            1
; Wait for a pin to become high (up to 1ms)
; @param %0 DATA register for input pin (e.g. PINB)
; @param %1 pin number for input (e.g. PORTB1)
; @return CFLAG set if okay, clear otherwise
; @clobbers R17, R22

.macro UART_BB_M_WAIT_FOR_PIN_HIGH
  ldi r17, 200
l_loop:
    sbic @0, @1
      rjmp l_reached
    Utils_WaitNanoSecs 5000, 0, r22                ; wait for 5us
    dec r17
  brne l_loop
  clc
  rjmp l_end
l_reached:
  sec
l_end:
.endmacro
; @end



; ---------------------------------------------------------------------------
; @routine uartBitbang_SendByte
;
; Send a byte.
; We only set the data pin to low at the beginning for the startbit. After that
; we only change the pin direction (e.g. input vs output):
; - for 0 bit: set DDR to output, forcing the data line low
; - for 1 bit: set DDR to input, letting the external pullup R pull the data line to HIGH
;              since the output pin is still set to 0 the internal pullup is disabled
; Expects interrupts to be disabled.
;
; @param R16 byte to send
; @return CFLAG set if okay, clear otherwise
; @clobbers R16, R21, R22

uartBitbang_SendByte:
  cbi COM_DATA_DDR, COM_DATA_PIN                   ; set DATA port as input
  cbi COM_DATA_OUTPUT, COM_DATA_PIN                ; disable internal pullup for DATA
  ldi r21, 8                                       ; +1 bits left
  ; send startbit
  sbi COM_DATA_DDR, COM_DATA_PIN                   ; +2 set DATA as output
  cbi COM_DATA_OUTPUT, COM_DATA_PIN                ; +2 set DATA low
  Utils_WaitNanoSecs COM_BIT_LENGTH, 1, r22        ; wait for one bit duration
  ; send data bits
uartBitbang_SendByte_loop:                         ; 9 for low bit
    lsr r16                                        ; 1+ bit to send -> CARRY
    brcs uartBitbang_SendByte_setHigh              ; HI: +2, LO: +1
uartBitbang_SendByte_setLow:
    sbi COM_DATA_DDR, COM_DATA_PIN                 ; +2 set DATA as output
    cbi COM_DATA_OUTPUT, COM_DATA_PIN              ; +2 set DATA low
    Utils_WaitNanoSecs COM_BIT_LENGTH, 11, r22
    rjmp uartBitbang_SendByte_loopEnd              ; +2
uartBitbang_SendByte_setHigh:
    cbi COM_DATA_DDR, COM_DATA_PIN                 ; +2 set DATA as input, pullup R makes it ONE
    nop                                            ; +1 (to make pin change available)
    Utils_WaitNanoSecs COM_HALFBIT_LENGTH, 0, r22  ; wait for half a bit length for line to safely settle
    sbis COM_DATA_INPUT, COM_DATA_PIN              ; +1 if no skip, +2 if skipped
      rjmp uartBitbang_SendByte_error              ; +2 if error (collision: we wanted line to be high but it is low)
    Utils_WaitNanoSecs COM_HALFBIT_LENGTH, 11, r22
uartBitbang_SendByte_loopEnd:
    dec r21                                        ; +1
  brne uartBitbang_SendByte_loop                   ; +2, sum per loop: 10 cycles
  ; send stopbit
  cbi COM_DATA_DDR, COM_DATA_PIN                   ; +2 set DATA as input, pullup R makes it ONE
  Utils_WaitNanoSecs COM_BIT_LENGTH, 0, r22        ; wait for one bit length
  sec
  ret

uartBitbang_SendByte_error:
  clc
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartBitbangReceiveByte
;
; Read a byte.
; Expects interrupts to be disabled.
;
; @return CFLAG set if okay, clear otherwise
; @return R16 byte received
; @clobbers R16, R20, R21, R22 (R17)

uartBitbang_ReceiveByte:
  cbi COM_DATA_DDR, COM_DATA_PIN               ; set DATA port as input
  cbi COM_DATA_OUTPUT, COM_DATA_PIN            ; disable internal pullup for RXD
  ldi r21, 8                                   ; bits left
  clr r20                                      ; byte currently receiving
  ; wait for startbit
  rcall uartBitbang_WaitForDataLow             ; (R17, R22)
  brcc uartBitbang_ReceiveByte_error
  Utils_WaitNanoSecs COM_HALFBIT_LENGTH, 10, r22  ; goto middle of startbit to maximize sync stability
uartBitbang_ReceiveByte_loop:
    Utils_WaitNanoSecs COM_BIT_LENGTH, 8, r22  ; 8 cycles used in the complete loop between waits
    sec                                        ; +1
    sbic COM_DATA_INPUT, COM_DATA_PIN          ; LOW: +2, HIGH: +1
      rjmp uartBitbang_ReceiveByte_shiftIn     ; HIGH: +2, rjmp, use set CFLAG
    clc                                        ; LOW: +1
uartBitbang_ReceiveByte_shiftIn:
    ror r20                                    ; +1
    dec r21                                    ; +1
  brne uartBitbang_ReceiveByte_loop                                  ; +2, sum per loop: 8 cycles
  rcall uartBitbang_WaitForDataHigh            ; wait for start of stopbit
  brcc uartBitbang_ReceiveByte_error
  mov r16, r20
  sec
  ret
uartBitbang_ReceiveByte_error:
  clc
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartBitbang_WaitForDataLow
;
; Wait up to 1ms for data pin to become low
; @return CFLAG set if okay, clear otherwise
; @clobbers R17, R22

uartBitbang_WaitForDataLow:
  cbi COM_DATA_DDR, COM_DATA_PIN          ; set DATA port as input
  cbi COM_DATA_OUTPUT, COM_DATA_PIN       ; disable internal pullup for TXD
  UART_BB_M_WAIT_FOR_PIN_LOW COM_DATA_INPUT, COM_DATA_PIN
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartBitbang_WaitForDataHigh
;
; Wait up to 1ms for data pin to become high
; @return CFLAG set if okay, clear otherwise
; @clobbers R17, R22

uartBitbang_WaitForDataHigh:
  cbi COM_DATA_DDR, COM_DATA_PIN          ; set DATA port as input
  cbi COM_DATA_OUTPUT, COM_DATA_PIN       ; disable internal pullup for TXD
  UART_BB_M_WAIT_FOR_PIN_HIGH COM_DATA_INPUT, COM_DATA_PIN
  ret
; @end



; ---------------------------------------------------------------------------
; @routine uartBitbang_WaitForAttnHigh
;
; Wait up to 1ms for data pin to become high
; @return CFLAG set if okay, clear otherwise
; @clobbers R17, R22

uartBitbang_WaitForAttnHigh:
  cbi COM_ATTN_DDR, COM_ATTN_PIN          ; set ATTN port as input
  cbi COM_ATTN_OUTPUT, COM_ATTN_PIN       ; disable internal pullup for ATTN
  UART_BB_M_WAIT_FOR_PIN_HIGH COM_ATTN_INPUT, COM_ATTN_PIN
  ret
; @end





