avr: introduced network module

this will be the base module for network modules.
This commit is contained in:
Martin Preuss
2025-02-13 01:12:29 +01:00
parent c5ab06b6d0
commit bf61be029e
16 changed files with 501 additions and 838 deletions

View File

@@ -0,0 +1,12 @@
<?xml?>
<gwbuild>
<extradist>
buffer.asm
defs.asm
</extradist>
</gwbuild>

View File

@@ -0,0 +1,125 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
; ===========================================================================
; defs
.equ NET_BUFFER_INUSE_BIT = 7
.equ NET_BUFFER_IFACENUM1_BIT = 1
.equ NET_BUFFER_IFACENUM0_BIT = 0
; ===========================================================================
; code segment
.cseg
; ---------------------------------------------------------------------------
; @routine NET_Buffer_Init @global
;
; @clobbers R16, R17, X
NET_Buffer_Init:
ldi xl, LOW(netBuffers)
ldi xh, HIGH(netBuffers)
m_fixedbuf_init NET_BUFFERS_SIZE, NET_BUFFERS_NUM
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_Buffer_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
NET_Buffer_Alloc:
ldi xl, LOW(netBuffers)
ldi xh, HIGH(netBuffers)
m_fixedbuf_reserve NET_BUFFERS_SIZE, NET_BUFFERS_NUM
brcc NET_Buffer_Alloc_end
ldi r17, (1<<NET_BUFFER_INUSE_BIT)
st X, r17
NET_Buffer_Alloc_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_Buffer_ReleaseByAddr @global
;
; @param X pointer to start of buffer
; @clobbers R16
NET_Buffer_ReleaseByAddr:
m_fixedbuf_release
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_Buffer_ReleaseByNum @global
;
; @param r16 buffer number
; @clobbers X (R16)
NET_Buffer_ReleaseByNum:
rcall NET_Buffer_Locate ; (R16)
brcc NET_Buffer_ReleaseByNum_end
rcall NET_Buffer_ReleaseByAddr ; (R16)
NET_Buffer_ReleaseByNum_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_Buffer_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
NET_Buffer_Locate:
cpi r16, NET_BUFFERS_NUM
brcc NET_Buffer_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(netBuffers)
add xl, r16
ldi r16, HIGH(netBuffers)
adc xh, r16
sec
NET_Buffer_Locate_end:
ret
; @end

View File

@@ -0,0 +1,20 @@
; ***************************************************************************
; 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
networkDataBegin:
; buffers for incoming and outgoing messages
netBuffers: .byte NET_BUFFERS_NUM*NET_BUFFERS_SIZE
netRingBufferMsgNumIn: .byte NET_MSGNUMINBUF_SIZE
networkDataEnd:

View File

@@ -0,0 +1,50 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
; defs
.equ NET_MSGNUMINBUF_SIZE = 4 ; max buffer nums in ringbuffer (global incoming)
.equ NET_IFACE_OUTMSGBUF_SIZE = 4 ; max buffer nums in ringbuffer (per interface outbound)
; interface generic data
.equ NET_IFACE_OFFS_IFACENUM = 0 ; interface number (put into received messages)
.equ NET_IFACE_OFFS_ADDRESS = 1
.equ NET_IFACE_OFFS_STATUS = 2
.equ NET_IFACE_OFFS_READTIMER = 3
.equ NET_IFACE_OFFS_WRITETIMER = 4
; receiption stats
.equ NET_IFACE_OFFS_PACKETSIN_LOW = 8
.equ NET_IFACE_OFFS_PACKETSIN_HIGH = 9
.equ NET_IFACE_OFFS_ERR_CONTENT_LOW = 10
.equ NET_IFACE_OFFS_ERR_CONTENT_HIGH = 11
.equ NET_IFACE_OFFS_ERR_IO_LOW = 12
.equ NET_IFACE_OFFS_ERR_IO_HIGH = 13
.equ NET_IFACE_OFFS_ERR_NOBUF_LOW = 14
.equ NET_IFACE_OFFS_ERR_NOBUF_HIGH = 15
.equ NET_IFACE_OFFS_HANDLED_LOW = 16
.equ NET_IFACE_OFFS_HANDLED_HIGH = 17
.equ NET_IFACE_OFFS_ERR_MISSED_LOW = 18
.equ NET_IFACE_OFFS_ERR_MISSED_HIGH = 19
; send stats
.equ NET_IFACE_OFFS_PACKETSOUT_LOW = 20
.equ NET_IFACE_OFFS_PACKETSOUT_HIGH = 21
.equ NET_IFACE_OFFS_ERR_COLLISIONS_LOW = 22
.equ NET_IFACE_OFFS_ERR_COLLISIONS_HIGH = 23
.equ NET_IFACE_OFFS_ERR_BUSY_LOW = 24
.equ NET_IFACE_OFFS_ERR_BUSY_HIGH = 25
.equ NET_IFACE_OFFS_OUTMSGRINGBUF = 26 ; RINGBUFFERY_SIZE+UART_HW_IFACE_OUTMSGBUF_SIZE
.equ NET_IFACE_SIZE = NET_IFACE_OFFS_OUTMSGRINGBUF+RINGBUFFERY_SIZE+NET_IFACE_OUTMSGBUF_SIZE

View File

@@ -0,0 +1,130 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
.cseg
; ---------------------------------------------------------------------------
; @routine NET_Interface_Init @global
;
; @param Y pointer to interface data in SRAM
; @clobbers R16, R17, X
NET_Interface_Init:
mov xl, yl
mov xh, yh
ldi r17, NET_IFACE_SIZE
clr r16
rcall Utils_FillSram ; (R17, X)
push yl
push yh
adiw yh:yl, NET_IFACE_OFFS_OUTMSGRINGBUF
ldi r16, NET_IFACE_OUTMSGBUF_SIZE
rcall RingBufferY_Init
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_Interface_AddOutgoingMsgNum @global
;
; @return CFLAG on success, cleared on error
; @param r16 byte to write
; @param Y pointer to start of interface data
; @clobbers R17, R18, X
NET_Interface_AddOutgoingMsgNum:
push yl
push yh
adiw yh:yl, NET_IFACE_OFFS_OUTMSGRINGBUF
rcall RingBufferY_WriteByteGuarded ; R17, R18, X
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_Interface_GetNextOutgoingMsgNum @global
;
; @return CFLAG on success, cleared on error
; @return R16 byte read
; @param Y pointer to start of interface data
; @clobbers R17, R18, X
NET_Interface_GetNextOutgoingMsgNum:
push yl
push yh
adiw yh:yl, NET_IFACE_OFFS_OUTMSGRINGBUF
rcall RingBufferY_ReadByteGuarded ; R17, R18, X
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_Interface_PeekNextOutgoingMsgNum @global
;
; @return CFLAG on success, cleared on error
; @return R16 byte read
; @param Y pointer to start of interface data
; @clobbers R17, R18, X
NET_Interface_PeekNextOutgoingMsgNum:
push yl
push yh
adiw yh:yl, NET_IFACE_OFFS_OUTMSGRINGBUF
rcall RingBufferY_PeekByteGuarded ; R17, R18, X
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_Interface_IncCounter16 @global
;
; @param Y pointer to start of interface data
; @param R16 offset to Y for counter to increment
; @clobbers R24, R25
NET_Interface_IncCounter16:
add yl, r16
adc yh, r16
sub yh, r16
ld r24, Y+
ld r25, Y
sbiw yh:yl, 1
adiw r25:r24, 1
breq NET_Interface_IncCounter16_end
st Y+, r24
st Y, r25
sbiw yh:yl, 1
NET_Interface_IncCounter16_end:
sub yl, r16
sbc yh, r16
add yh, r16
ret
; @end

View File

@@ -0,0 +1,82 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
.cseg
; ---------------------------------------------------------------------------
; @routine NET_Init
;
; Initializes buffers.
;
; @clobbers R16, R17, X
NET_Init:
ldi xh, HIGH(networkDataBegin)
ldi xl, LOW(networkDataBegin)
clr r16
ldi r17, (networkDataEnd-networkDataBegin)
rcall Utils_FillSram ; (R17, X)
rcall NET_Buffer_Init ; (R16, R17, X)
ldi r16, NET_MSGNUMINBUF_SIZE
ldi yl, LOW(netRingBufferMsgNumIn)
ldi yh, HIGH(netRingBufferMsgNumIn)
rcall RingBufferY_Init ; (R17)
sec
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_AddIncomingMsgNum @global
;
; @return CFLAG on success, cleared on error
; @param R16 buffer number of the next incoming message
; @clobbers R17, R18, X
NET_AddIncomingMsgNum:
push yl
push yh
ldi yl, LOW(netRingBufferMsgNumIn)
ldi yh, HIGH(netRingBufferMsgNumIn)
rcall RingBufferY_WriteByteGuarded ; R17, R18, X
pop yh
pop yl
ret
; @end
; ---------------------------------------------------------------------------
; @routine NET_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
NET_GetNextIncomingMsgNum:
push yl
push yh
ldi yl, LOW(netRingBufferMsgNumIn)
ldi yh, HIGH(netRingBufferMsgNumIn)
rcall RingBufferY_ReadByteGuarded ; R17, R18, X
pop yh
pop yl
ret
; @end