Files
aqhomecontrol/avr/modules/uart_hw/buffers.asm
2025-01-27 00:20:45 +01:00

205 lines
5.0 KiB
NASM

; ***************************************************************************
; 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. *
; ***************************************************************************
.dseg
uartHwDataBegin:
; fixed buffers for incoming and outgoing messages
uartHw_buffers: .byte UART_HW_FIXEDBUFFERS_NUM*UART_HW_FIXEDBUFFERS_SIZE
uartHw_ringBufferMsgNumIn: .byte UART_HW_MSGNUMINBUF_SIZE
uartHw_ringBufferMsgNumOut: .byte UART_HW_MSGNUMOUTBUF_SIZE
uartHwDataEnd:
.cseg
; ---------------------------------------------------------------------------
; @routine UART_HW_FixedBuffers_Init @global
;
; @clobbers R16, R17, X
UART_HW_FixedBuffers_Init:
ldi xl, LOW(uartHw_buffers)
ldi xh, HIGH(uartHw_buffers)
m_fixedbuf_init UART_HW_FIXEDBUFFERS_SIZE, UART_HW_FIXEDBUFFERS_NUM
ret
; @end
; ---------------------------------------------------------------------------
; @routine UART_HW_FixedBuffers_Alloc @global
;
; @return CFLAG set if buffer available, cleared otherwise
; @return r16 buffer num
; @return X pointer to start of buffer
; @clobbers R16, R17, X
UART_HW_FixedBuffers_Alloc:
ldi xl, LOW(uartHw_buffers)
ldi xh, HIGH(uartHw_buffers)
m_fixedbuf_reserve UART_HW_FIXEDBUFFERS_SIZE, UART_HW_FIXEDBUFFERS_NUM
ret
; @end
; ---------------------------------------------------------------------------
; @routine UART_HW_FixedBuffers_ReleaseByAddr @global
;
; @param X pointer to start of buffers
; @clobbers R16
UART_HW_FixedBuffers_ReleaseByAddr:
m_fixedbuf_release
ret
; @end
; ---------------------------------------------------------------------------
; @routine UART_HW_FixedBuffers_ReleaseByNum @global
;
; @param r16 buffer number
; @clobbers X (R16)
UART_HW_FixedBuffers_ReleaseByNum:
rcall UART_HW_FixedBuffers_Locate ; (R16)
brcc UART_HW_FixedBuffers_ReleaseByNum_end
rcall UART_HW_FixedBuffers_ReleaseByAddr ; (R16)
UART_HW_FixedBuffers_ReleaseByNum_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine UART_HW_FixedBuffers_Locate
;
; Get position of a given buffer.
;
; CAVE: need to change this routine if UART_HW_FIXEDBUFFERS_SIZE is changed!
;
; @return CFLAG set if okay, cleared on error
; @return X points to start of buffer with the given num
; @param r16 buffer num (0-max)
; @clobbers r16
UART_HW_FixedBuffers_Locate:
cpi r16, UART_HW_FIXEDBUFFERS_NUM
brcc UART_HW_FixedBuffers_Locate_end ; out of range
mov xh, r16 ; * 256
clr xl
lsr xh ; *128
ror xl
lsr xh ; *64
ror xl
lsr xh ; *32
ror xl
ldi r16, LOW(uartHw_buffers)
add xl, r16
ldi r16, HIGH(uartHw_buffers)
adc xh, r16
sec
UART_HW_FixedBuffers_Locate_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine UART_HW_AddIncomingMsg @global
;
; @return CFLAG on success, cleared on error
; @param R16 buffer number of the next incoming message
; @clobbers R17, R18, X
UART_HW_AddIncomingMsgNum:
push yl
push yh
ldi yl, LOW(uartHw_ringBufferMsgNumIn)
ldi yh, HIGH(uartHw_ringBufferMsgNumIn)
rcall RingBufferY_WriteByte ; R17, R18, X
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine UART_HW_GetNextIncomingMsgNum @global
;
; @return CFLAG on success, cleared on error
; @return R16 buffer number of the next incoming message
; @param Y pointer to start of interface data
; @clobbers R17, R18, X
UART_HW_GetNextIncomingMsgNum:
push yl
push yh
ldi yl, LOW(uartHw_ringBufferMsgNumIn)
ldi yh, HIGH(uartHw_ringBufferMsgNumIn)
rcall RingBufferY_ReadByte ; R17, R18, X
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine UART_HW_AddOutgoingMsg @global
;
; @return CFLAG on success, cleared on error
; @param R16 buffer number of the next incoming message
; @clobbers R17, R18, X
UART_HW_AddOutgoingMsgNum:
push yl
push yh
ldi yl, LOW(uartHw_ringBufferMsgNumOut)
ldi yh, HIGH(uartHw_ringBufferMsgNumOut)
rcall RingBufferY_WriteByte ; R17, R18, X
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine UART_HW_GetNextOutgoingMsgNum @global
;
; @return CFLAG on success, cleared on error
; @return R16 buffer number of the next incoming message
; @param Y pointer to start of interface data
; @clobbers R17, R18, X
UART_HW_GetNextOutgoingMsgNum:
push yl
push yh
ldi yl, LOW(uartHw_ringBufferMsgNumOut)
ldi yh, HIGH(uartHw_ringBufferMsgNumOut)
rcall RingBufferY_ReadByte ; R17, R18, X
pop yh
pop yl
ret
; @end