diff --git a/avr/devices/t03/main.asm b/avr/devices/t03/main.asm index 4497448..f570ca5 100644 --- a/avr/devices/t03/main.asm +++ b/avr/devices/t03/main.asm @@ -170,7 +170,9 @@ main_loop: rcall BaseTimer_Run rcall TtyOnUart1_Run - + + ; check incoming msg + rcall checkRecvdMsg rjmp main_loop @@ -258,18 +260,16 @@ initModules: .include "common/m_ringbuffer_y.asm" .include "common/ringbuffer_y.asm" .include "common/m_fixedbuffers.asm" -;.include "common/crc8.asm" -.include "modules/flash/wait.asm" .include "modules/basetimer/main.asm" .include "modules/led_simple/main.asm" -.include "modules/com2/defs.asm" -.include "modules/com2/crc.asm" -.include "modules/comproto/defs.asm" .include "modules/network/defs.asm" .include "modules/network/buffer.asm" .include "modules/network/data.asm" .include "modules/network/main.asm" .include "modules/network/iface.asm" +.include "modules/network/msg/defs.asm" +.include "modules/network/msg/common.asm" +.include "modules/network/msg/device-w.asm" .include "modules/uart_hw/defs.asm" .include "modules/uart_hw/lowlevel.asm" @@ -291,18 +291,18 @@ maybeSendDeviceMsg: sbiw r25:r24, 1 brne maybeSendDeviceMsg_storeCounter ; send device msg - rcall NET_Buffer_Alloc ; (R16, R17, X) + rcall NET_Buffer_Alloc ; (R16, R17, X) brcc maybeSendDeviceMsg_resetCounter push r16 adiw xh:xl, 1 - rcall writeDeviceMsg + rcall NETMSG_Device_Write ; (R16, R17, R18, R19, R20, R21, Z) sbiw xh:xl, 1 pop r16 push r16 - rcall TtyOnUart1_SendBuffer + rcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X) pop r16 brcs maybeSendDeviceMsg_resetCounter - rcall NET_Buffer_ReleaseByNum ; (R16, X) + rcall NET_Buffer_ReleaseByNum ; (R16, X) rjmp maybeSendDeviceMsg_end ; reset counter maybeSendDeviceMsg_resetCounter: @@ -316,36 +316,16 @@ maybeSendDeviceMsg_end: -writeDeviceMsg: - ldi r16, 0xff - st X+, r16 ; dest address - ldi r16, 18 ; msg code+src address+12 payload bytes - st X+, r16 ; msg len - ldi r16, CPRO_CMD_DEVICE - st X+, r16 ; msg code - clr r16 - st X+, r16 ; src address - rcall addUidToBuffer ; (r16, r18, r19, r20, r21) - ldi zh, HIGH(devInfoBlock*2) ; 6-17: devInfoBlock - ldi zl, LOW(devInfoBlock*2) - ldi r18, 12 - rcall Utils_CopyFromFlash ; (R17, R18, X, Y) - sbiw xh:xl, 20 ; go back to beginning of message (1 byte dst addr, 1 byte length, 18 bytes payload) - rcall com2CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X) - sbiw xh:xl, 21 ; go back to beginning of message (1 byte dst addr, 1 byte length, 18 bytes payload, 1 byte crc) - ret +checkRecvdMsg: + ldi yl, LOW(ttyOnUart1_iface) + ldi yh, HIGH(ttyOnUart1_iface) - -addUidToBuffer: - push xh - push xl - rcall Utils_ReadUid ; (R16, X) - pop xl - pop xh - st X+, r18 - st X+, r19 - st X+, r20 - st X+, r21 + rcall NET_PeekNextIncomingMsgNum ; check read queue + brcc checkRecvdMsg_end ; no msg, jmp + rcall NET_Interface_AddOutgoingMsgNum ; try to add msg to interface + brcc checkRecvdMsg_end ; could not add, jmp + rcall NET_GetNextIncomingMsgNum ; take off the queue +checkRecvdMsg_end: ret diff --git a/avr/modules/network/buffer.asm b/avr/modules/network/buffer.asm index c5e6692..34eaf84 100644 --- a/avr/modules/network/buffer.asm +++ b/avr/modules/network/buffer.asm @@ -97,7 +97,7 @@ NET_Buffer_ReleaseByNum_end: ; @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 +; @clobbers r17 NET_Buffer_Locate: cpi r16, NET_BUFFERS_NUM @@ -110,10 +110,10 @@ NET_Buffer_Locate: ror xl lsr xh ; *32 ror xl - ldi r16, LOW(netBuffers) - add xl, r16 + ldi r17, LOW(netBuffers) + add xl, r17 ldi r16, HIGH(netBuffers) - adc xh, r16 + adc xh, r17 sec NET_Buffer_Locate_end: ret diff --git a/avr/modules/network/main.asm b/avr/modules/network/main.asm index b681029..59511e6 100644 --- a/avr/modules/network/main.asm +++ b/avr/modules/network/main.asm @@ -80,3 +80,24 @@ NET_GetNextIncomingMsgNum: +; --------------------------------------------------------------------------- +; @routine NET_GetPeekIncomingMsgNum @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_PeekNextIncomingMsgNum: + push yl + push yh + ldi yl, LOW(netRingBufferMsgNumIn) + ldi yh, HIGH(netRingBufferMsgNumIn) + rcall RingBufferY_PeekByteGuarded ; R17, R18, X + pop yh + pop yl + ret +; @end + + + diff --git a/avr/modules/network/msg/0BUILD b/avr/modules/network/msg/0BUILD new file mode 100644 index 0000000..e71ddb7 --- /dev/null +++ b/avr/modules/network/msg/0BUILD @@ -0,0 +1,11 @@ + + + + + + device-w.asm + + + + + diff --git a/avr/modules/network/msg/common.asm b/avr/modules/network/msg/common.asm new file mode 100644 index 0000000..543193b --- /dev/null +++ b/avr/modules/network/msg/common.asm @@ -0,0 +1,126 @@ +; *************************************************************************** +; 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. * +; *************************************************************************** + + +; +; --------------------------------------------------------------------------- +; @routine NETMSG_Common_AddUidToBuffer @global +; +; @return X points directly behind the UID in buffer +; @param X current buffer position to write UID to +; @clobbers R16, R18, R19, R20, R21 + +NETMSG_Common_AddUidToBuffer: + push xh + push xl + rcall Utils_ReadUid ; (R16, X) + pop xl + pop xh + st X+, r18 + st X+, r19 + st X+, r20 + st X+, r21 + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine NETMSG_CalcAndAddChecksumByte @global +; +; @return CFLAG set if okay, clear otherwise +; @return X points behind checksum byte pos upon return +; @param X pointer to packet buffer (points behind checksum upon return) +; @clobbers R16, R17, R18, R19, R20, X + +NETMSG_CalcAndAddChecksumByte: + rcall netMsgCalcMsgChecksum ; (R16, R17, R18, R19, R20, X) + st X+, r16 ; add checksum byte + sec + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine netMsgCheckMessageInBuffer +; +; check message in buffer +; +; @return CFLAG set if okay, clear otherwise +; @param X pointer to packet buffer +; @clobbers R16, R17 (R18, R19, R20, X) + +netMsgCheckMessageInBuffer: + rcall netMsgCalcMsgChecksum ; (R16, R17, R18, R19, R20, X) + ld r17, X + cp r16, r17 ; should be equal + brne netMsgCheckMessageInBuffer_error + sec + ret +netMsgCheckMessageInBuffer_error: + clc + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine netMsgCalcMsgChecksum +; +; calc checksum from msg buffer (except crc byte) +; +; @return r16 crc8 checksum +; @return X points to position of checksum byte +; @param X pointer to packet buffer (points to checksum byte upon return) +; @clobbers R18 (R16, R17, R19, R20, X) + +netMsgCalcMsgChecksum: + adiw xh:xl, NETMSG_OFFS_MSGLEN + ld r18, X ; read msg len + sbiw xh:xl, NETMSG_OFFS_MSGLEN + inc r18 ; account for dest address + inc r18 ; account for msg len byte + rjmp netMsgCrcCalc ; (R16, R17, R18, R19, R20, X) +; @end + + + +; --------------------------------------------------------------------------- +; @routine netMsgCrcCalc @global +; calc checksum using given polynomial +; +; @return r16 calculated checksum +; @return X points directly after last checked byte +; @param X pointer to data to calc crc8 for +; @param r18 number of bytes to calc crc8 for +; @clobbers R16, R17, R18, R19, R20, X + +netMsgCrcCalc: + ldi r16, 0xff ; start crc + ldi r19, NETMSG_CRC8_POLYNOMIAL + +netMsgCrcCalc_loop1: + ld r17, X+ ; running var + eor r16, r17 + ldi r20, 8 ; counter for loop2 +netMsgCrcCalc_loop2: + lsl r16 + brcc netMsgCrcCalc_l1 + eor r16, r19 +netMsgCrcCalc_l1: + dec r20 + brne netMsgCrcCalc_loop2 + dec r18 + brne netMsgCrcCalc_loop1 + ret +; @end + + + diff --git a/avr/modules/network/msg/defs.asm b/avr/modules/network/msg/defs.asm new file mode 100644 index 0000000..89d978e --- /dev/null +++ b/avr/modules/network/msg/defs.asm @@ -0,0 +1,68 @@ +; *************************************************************************** +; 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. * +; *************************************************************************** + + + +; --------------------------------------------------------------------------- +; command values + +.equ NETMSG_CMD_PING = 10 +.equ NETMSG_CMD_PONG = 11 +.equ NETMSG_CMD_SENDSTATS = 20 +.equ NETMSG_CMD_RECVSTATS = 21 +.equ NETMSG_CMD_TWIBUSMEMBER = 30 +.equ NETMSG_CMD_DEBUG = 40 + +.equ NETMSG_CMD_RESULT = 50 + +.equ NETMSG_CMD_NEED_ADDRESS = 60 +.equ NETMSG_CMD_HAVE_ADDRESS = 61 +.equ NETMSG_CMD_CLAIM_ADDRESS = 62 +.equ NETMSG_CMD_DENY_ADDRESS = 63 +.equ NETMSG_CMD_ADDRESS_RANGE = 64 + +.equ NETMSG_CMD_FLASH_START = 70 +.equ NETMSG_CMD_FLASH_END = 71 +.equ NETMSG_CMD_FLASH_READY = 72 +.equ NETMSG_CMD_FLASH_DATA = 73 +.equ NETMSG_CMD_FLASH_RSP = 74 + +.equ NETMSG_CMD_DEVICE = 80 +.equ NETMSG_CMD_MEMSTATS = 81 +.equ NETMSG_CMD_SYSSTATS = 82 + +.equ NETMSG_CMD_REBOOT_REQUEST = 90 +.equ NETMSG_CMD_REBOOT_RESPONSE = 91 + +.equ NETMSG_CMD_VALUE_REPORT = 100 +.equ NETMSG_CMD_VALUE_SET = 101 +.equ NETMSG_CMD_VALUE_SET_ACK = 102 +.equ NETMSG_CMD_VALUE_SET_NACK = 103 + +.equ NETMSG_CMD_DATA = 110 + + + +; --------------------------------------------------------------------------- +; position definitions for all messages + +.equ NETMSG_OFFS_DESTADDR = 0 +.equ NETMSG_OFFS_MSGLEN = 1 +.equ NETMSG_OFFS_MSGDATA = 2 +.equ NETMSG_OFFS_CMD = 2 ; first at NETMSG_OFFS_MSGDATA +.equ NETMSG_OFFS_SRCADDR = 3 +.equ NETMSG_OFFS_PAYLOAD = 4 ; payload for the cmd follows here + +.equ NETMSG_CRC8_POLYNOMIAL = 0x97 ; HD=4 up to 119 bytes, e.g. detects all 1 to 3 bit errors + + + + + + diff --git a/avr/modules/network/msg/device-w.asm b/avr/modules/network/msg/device-w.asm new file mode 100644 index 0000000..56549c1 --- /dev/null +++ b/avr/modules/network/msg/device-w.asm @@ -0,0 +1,39 @@ +; *************************************************************************** +; 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. * +; *************************************************************************** + + + +; --------------------------------------------------------------------------- +; @routine NETMSG_Device_Write +; +; @param Y pointer to device to write msg for +; @clobbers R16, R18 (R17, R19, R20, R21, Z) + +NETMSG_Device_Write: + ldi r16, 0xff + st X+, r16 ; dest address + ldi r16, 18 ; msg code+src address+12 payload bytes + st X+, r16 ; msg len + ldi r16, NETMSG_CMD_DEVICE + st X+, r16 ; msg code + ldd r16, Y+NET_IFACE_OFFS_ADDRESS + st X+, r16 ; src address + rcall NETMSG_Common_AddUidToBuffer ; (R16, R18, R19, R20, R21) + ldi zh, HIGH(devInfoBlock*2) ; 6-17: devInfoBlock + ldi zl, LOW(devInfoBlock*2) + ldi r18, 12 + rcall Utils_CopyFromFlash ; (R17, R18, X, Z) + sbiw xh:xl, 20 ; go back to beginning of message (1 byte dst addr, 1 byte length, 18 bytes payload) + rcall NETMSG_CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X) + sbiw xh:xl, 21 ; go back to beginning of message (1 byte dst addr, 1 byte length, 18 bytes payload, 1 byte crc) + ret +; @end + + + diff --git a/avr/modules/network/msg/sendstats-w.asm b/avr/modules/network/msg/sendstats-w.asm new file mode 100644 index 0000000..b11eff7 --- /dev/null +++ b/avr/modules/network/msg/sendstats-w.asm @@ -0,0 +1,40 @@ +; *************************************************************************** +; 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. * +; *************************************************************************** + + + +; --------------------------------------------------------------------------- +; @routine NETMSG_SendStats_Write @global +; +; @param Y pointer to device to write msg for and to +; @clobbers R16, R18 (R17, R19, R20, R21, Z) + +NETMSG_SendStats_Write: + ldi r16, 0xff + st X+, r16 ; dest address + ldi r16, 12 ; msg code+src address+10 payload bytes + st X+, r16 ; msg len + ldi r16, NETMSG_CMD_SENDSTATS + st X+, r16 ; msg code + ldd r16, Y+NET_IFACE_OFFS_ADDRESS + st X+, r16 ; src address + rcall NETMSG_Common_AddUidToBuffer ; (R16, R18, R19, R20, R21) + adiw yh:yl, NET_IFACE_OFFS_PACKETSOUT_LOW + ldi r18, 6 + rcall Utils_Copy_SDRAM + sbiw yh:yl, NET_IFACE_OFFS_PACKETSOUT_LOW+6 + + sbiw xh:xl, 13 ; go back to beginning of message (1 byte dst addr, 1 byte length, 12 bytes payload) + rcall NETMSG_CalcAndAddChecksumByte ; (R16, R17, R18, R19, R20, X) + sbiw xh:xl, 14 ; go back to beginning of message (1 byte dst addr, 1 byte length, 12 bytes payload, 1 byte crc) + ret +; @end + + + diff --git a/avr/modules/uart_hw/defs.asm b/avr/modules/uart_hw/defs.asm index 7acf9ec..befd725 100644 --- a/avr/modules/uart_hw/defs.asm +++ b/avr/modules/uart_hw/defs.asm @@ -17,12 +17,13 @@ .equ UART_HW_READMODE_IDLE = 1 .equ UART_HW_READMODE_READING = 2 .equ UART_HW_READMODE_SKIPPING = 3 +.equ UART_HW_READMODE_MSGRECEIVED = 4 .equ UART_HW_WRITEMODE_OFF = 0 -.equ UART_HW_WRITEMODE_IDLE = 1 -.equ UART_HW_WRITEMODE_WRITING = 2 -.equ UART_HW_WRITEMODE_WAITBUFFEREMPTY = 3 -.equ UART_HW_WRITEMODE_WRITEBUFFEREMPTY = 4 +.equ UART_HW_WRITEMODE_IDLE = 16 +.equ UART_HW_WRITEMODE_WRITING = 17 +.equ UART_HW_WRITEMODE_WAITBUFFEREMPTY = 18 +.equ UART_HW_WRITEMODE_WRITEBUFFEREMPTY = 19 .equ UART_HW_STATUS_UNDERRUN_BIT = 0 diff --git a/avr/modules/uart_hw/lowlevel.asm b/avr/modules/uart_hw/lowlevel.asm index 9d2dc14..33412ec 100644 --- a/avr/modules/uart_hw/lowlevel.asm +++ b/avr/modules/uart_hw/lowlevel.asm @@ -35,7 +35,7 @@ UART_HW_Interface_Init: ; --------------------------------------------------------------------------- ; @routine UART_HW_Interface_SetReadBuffer @global ; -; @param Y pointer to interface data in SRAM (see @ref UART_HW_IFACE_OFFS_STATE) +; @param Y pointer to interface data in SRAM ; @param r16 read buffer number ; @param X pointer to read buffer ; @clobbers R17 @@ -48,6 +48,7 @@ UART_HW_Interface_SetReadBuffer: sbiw xh:xl, 1 clr r17 std Y+UART_HW_IFACE_OFFS_READBUFUSED, r17 + ldi r17, NET_BUFFERS_SIZE-1 std Y+UART_HW_IFACE_OFFS_READBUFLEFT, r17 ret ; @end @@ -57,7 +58,7 @@ UART_HW_Interface_SetReadBuffer: ; --------------------------------------------------------------------------- ; @routine UART_HW_Interface_SetWriteBuffer @global ; -; @param Y pointer to interface data in SRAM (see @ref UART_HW_IFACE_OFFS_STATE) +; @param Y pointer to interface data in SRAM ; @param r16 write buffer number ; @param X pointer to write buffer ; @clobbers r17 diff --git a/avr/modules/uart_hw/lowlevel_uart1.asm b/avr/modules/uart_hw/lowlevel_uart1.asm index 826704d..5176001 100644 --- a/avr/modules/uart_hw/lowlevel_uart1.asm +++ b/avr/modules/uart_hw/lowlevel_uart1.asm @@ -29,7 +29,7 @@ UART_HW_Uart1_Init: ; --------------------------------------------------------------------------- ; @routine UART_HW_Uart1_StartRx @global ; -; @clobbers none +; @clobbers R16 UART_HW_Uart1_StartRx: M_UART_HW_Uart_StartRx 1 @@ -94,7 +94,7 @@ UART_HW_Uart1_Flush: ; @routine UART_HW_Uart1_RxCharIsr @global ; ; @param Y pointer to interface data in SRAM (see @ref UART_HW_IFACE_OFFS_MODE) -; @clobbers R16 (R17, R18, X) +; @clobbers R16 (R17, R18, R24, R25, X) UART_HW_Uart1_RxCharIsr: M_UART_HW_Uart_RxCharIsr 1 diff --git a/avr/modules/uart_hw/m_lowlevel_uart.asm b/avr/modules/uart_hw/m_lowlevel_uart.asm index a1c3cc1..2968c67 100644 --- a/avr/modules/uart_hw/m_lowlevel_uart.asm +++ b/avr/modules/uart_hw/m_lowlevel_uart.asm @@ -135,33 +135,91 @@ l_end_%: ; ; @param %0 UART number ("0" for UART0) ; @param Y pointer to interface data in SRAM -; @clobbers R16 (R17, R18, X) +; @clobbers R16 (R17, R18, R24, R25, X) .macro M_UART_HW_Uart_RxCharIsr -#if 0 + ; check for errors lds r16, UCSR@0A ; check for errors andi r16, (1< HWERR - rjmp l_setStatusAndEnd_% -l_recv_%: + brne l_hwerr_% + ; read char lds r16, UCSR@0A sbrs r16, RXC@0 rjmp l_end_% ; no data - lds r16, UDR@0 - rcall UART_HW_InterfaceWriteToReadBuffer ; (R17, R18, X) - brcc l_overrun_% + lds r16, UDR@0 ; r16=received char + ; check read mode + ldd r17, Y+UART_HW_IFACE_OFFS_READMODE + cpi r17, UART_HW_READMODE_READING + breq l_storeChar_% + cpi r17, UART_HW_READMODE_SKIPPING + breq l_skipChar_% + rjmp l_overrun_% ; neither read nor skip mode +l_skipChar_%: clr r16 - std Y+NET_IFACE_OFFS_READTIMER, r16 ; reset read timer + std Y+NET_IFACE_OFFS_READTIMER, r16 ; reset read timer rjmp l_end_% +l_storeChar_%: + mov r18, r16 ; r18=received char + ; check for buffer overrun + ldd r17, Y+UART_HW_IFACE_OFFS_READBUFLEFT ; r17=bytes left + tst r17 + breq l_econtent_% ; msg too long + ; actually store byte, increment/decrement counters and pos + ldd xl, Y+UART_HW_IFACE_OFFS_READBUFPOS_LOW + ldd xh, Y+UART_HW_IFACE_OFFS_READBUFPOS_HIGH + st X+, r18 ; r18=byte to store + clr r16 + std Y+NET_IFACE_OFFS_READTIMER, r16 ; reset read timer + std Y+UART_HW_IFACE_OFFS_READBUFPOS_LOW, xl + std Y+UART_HW_IFACE_OFFS_READBUFPOS_HIGH, xh + ldd r18, Y+UART_HW_IFACE_OFFS_READBUFUSED ; r18=bytes in buffer + inc r18 + std Y+UART_HW_IFACE_OFFS_READBUFUSED, r18 + dec r17 + std Y+UART_HW_IFACE_OFFS_READBUFLEFT, r17 + breq l_msgFinished_% + ; check msg size + cpi r18, 2 ; bytes in buffer, exactly 2? + brne l_end_% ; nope, done + sbiw xh:xl, 1 ; yes, determine message length (msgLen at previous pos) + ld r16, X+ ; read payload length byte + subi r16, -3 ; add 3 (dest addr, length, crc byte) + cpi r16, (NET_BUFFERS_SIZE-1) ; total msg length ok? + brcc l_econtent_% ; content error (msg too long) + subi r16, 2 ; subtract bytes already received + std Y+UART_HW_IFACE_OFFS_READBUFLEFT, r16 ; set new number of bytes left + brne l_end_% ; jmp if still bytes left to received +l_msgFinished_%: + ldd r16, Y+UART_HW_IFACE_OFFS_READBUFNUM + rcall NET_AddIncomingMsgNum ; (R17, R18, X) + brcs l_msgStored_% + ; msg complete but could not store + rcall NET_Buffer_Locate + rcall UART_HW_Interface_SetReadBuffer ; reset/reuse current buffer + ldi r16, NET_IFACE_OFFS_ERR_NOBUF_LOW + ldi r17, UART_HW_READMODE_READING + rjmp l_incCounterAndEnterMode_% +l_msgStored_%: + ldi r16, NET_IFACE_OFFS_PACKETSIN_LOW + ldi r17, UART_HW_READMODE_MSGRECEIVED + rjmp l_incCounterAndEnterMode_% +l_hwerr_%: + ldi r16, NET_IFACE_OFFS_ERR_IO_LOW + rcall NET_Interface_IncCounter16 + ldi r16, UART_HW_READMODE_SKIPPING + std Y+UART_HW_IFACE_OFFS_READMODE, r16 ; set read mode + rjmp l_end_% +l_econtent_%: + ldi r16, NET_IFACE_OFFS_ERR_CONTENT_LOW + rjmp l_incCounterAndEnterSkipping_% l_overrun_%: - ldd r16, Y+NET_IFACE_OFFS_STATUS ; set overrun error - ori r16, (1< OVERRUN -l_setStatusAndEnd_%: - std Y+NET_IFACE_OFFS_STATUS, r16 + ldi r16, NET_IFACE_OFFS_ERR_NOBUF_LOW +l_incCounterAndEnterSkipping_%: + ldi r17, UART_HW_READMODE_SKIPPING +l_incCounterAndEnterMode_%: + rcall NET_Interface_IncCounter16 ; (R24, R25) + std Y+UART_HW_IFACE_OFFS_READMODE, r17 ; set read mode l_end_%: -#endif .endmacro ; @end diff --git a/avr/modules/uart_hw/ttyonuart1.asm b/avr/modules/uart_hw/ttyonuart1.asm index 38cc6b8..93f710d 100644 --- a/avr/modules/uart_hw/ttyonuart1.asm +++ b/avr/modules/uart_hw/ttyonuart1.asm @@ -73,17 +73,21 @@ TtyOnUart1_RxCharIsr: push r16 push r17 push r18 - push xl - push xh - push yl - push yh - ldi yl, LOW(ttyOnUart1_iface) - ldi yh, HIGH(ttyOnUart1_iface) - rcall UART_HW_Uart1_RxCharIsr ; (R16, R17, R18, X) - pop yh - pop yl - pop xh - pop xl + push r24 + push r25 + push xl + push xh + push yl + push yh + ldi yl, LOW(ttyOnUart1_iface) + ldi yh, HIGH(ttyOnUart1_iface) + rcall UART_HW_Uart1_RxCharIsr ; (R16, R17, R18, R24, R25, X) + pop yh + pop yl + pop xh + pop xl + pop r25 + pop r24 pop r18 pop r17 pop r16 @@ -159,7 +163,7 @@ TtyOnUart1_TxCharIsr: ; --------------------------------------------------------------------------- ; @routine TtyOnUart1_SendBuffer @global ; -; @clobbers R17 +; @clobbers R16, R17 TtyOnUart1_SendBuffer: push r15 @@ -198,7 +202,7 @@ TtyOnUart1_Run: ldi yh, HIGH(ttyOnUart1_iface) rcall ttyOnUart1RunWriteModes -; rcall ttyOnUart1RunReadModes + rcall ttyOnUart1RunReadModes pop r15 out SREG, r15 ret @@ -213,6 +217,8 @@ TtyOnUart1_Run: ttyOnUart1RunWriteModes: ldd r16, Y+UART_HW_IFACE_OFFS_WRITEMODE ; handle write functions + cpi r16, UART_HW_WRITEMODE_IDLE + breq ttyOnUart1RunIdle cpi r16, UART_HW_WRITEMODE_WRITING breq ttyOnUart1RunWriting cpi r16, UART_HW_WRITEMODE_WAITBUFFEREMPTY @@ -224,6 +230,21 @@ ttyOnUart1RunWriteModes: +; --------------------------------------------------------------------------- +; @routine ttyOnUart1RunIdle +; +; @clobbers + +ttyOnUart1RunIdle: + rcall NET_Interface_GetNextOutgoingMsgNum ; (R17, R18, X) + brcc ttyOnUart1RunIdle_end + rcall NET_Buffer_Locate ; (R17) + brcc ttyOnUart1RunIdle_end + rcall TtyOnUart1_SendBuffer ; (R16, R17) +ttyOnUart1RunIdle_end: + ret + + ; --------------------------------------------------------------------------- ; @routine ttyOnUart1RunWriting ; @@ -264,6 +285,8 @@ ttyOnUart1RunWriteBufferEmpty_setIdle: rcall UART_HW_Uart1_StopTx ; disable transceiver and interrupts (R16) ldi r16, UART_HW_WRITEMODE_IDLE std Y+UART_HW_IFACE_OFFS_WRITEMODE, r16 + ldi r16, NET_IFACE_OFFS_PACKETSOUT_LOW ; increment packets counter + rcall NET_Interface_IncCounter16 ; (R24, R25) ret ; @end @@ -275,6 +298,95 @@ ttyOnUart1RunWriteBufferEmpty_setIdle: ; @clobbers all, !Y ttyOnUart1RunReadModes: + ldd r16, Y+UART_HW_IFACE_OFFS_READMODE ; handle read functions + cpi r16, UART_HW_READMODE_IDLE + breq ttyOnUart1RunReadIdle ; (R16, R17, R24, R25, X) + cpi r16, UART_HW_READMODE_READING + breq ttyOnUart1RunReading ; (none) + cpi r16, UART_HW_READMODE_SKIPPING + breq ttyOnUart1RunSkipping ; (R16) + cpi r16, UART_HW_READMODE_MSGRECEIVED + breq ttyOnUart1RunMsgReceived ; (R16, R17, R18, R24, R25) + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine ttyOnUart1RunReadIdle +; +; @clobbers R16 (R17, R24, R25, X) + +ttyOnUart1RunReadIdle: + ldd r16, Y+UART_HW_IFACE_OFFS_READBUFNUM + cpi r16, 0xff + brne ttyOnUart1RunReadIdle_enterReading + rcall NET_Buffer_Alloc ; (R16, R17, X) + brcc ttyOnUart1RunReadIdle_noBuf + rcall UART_HW_Interface_SetReadBuffer ; (R17) +ttyOnUart1RunReadIdle_enterReading: + ldi r16, UART_HW_READMODE_READING + std Y+UART_HW_IFACE_OFFS_READMODE, r16 + rcall UART_HW_Uart1_StartRx ; R16 + ret +ttyOnUart1RunReadIdle_noBuf: + ldi r16, NET_IFACE_OFFS_ERR_NOBUF_LOW + rcall NET_Interface_IncCounter16 ; (R24, R25) + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine ttyOnUart1RunReading +; +; @clobbers none + +ttyOnUart1RunReading: + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine ttyOnUart1RunSkipping +; +; @clobbers R16 + +ttyOnUart1RunSkipping: + ldd r16, Y+NET_IFACE_OFFS_READTIMER + cpi r16, 2 + brcs ttyOnUart1RunSkipping_end + ldi r16, UART_HW_READMODE_IDLE + std Y+UART_HW_IFACE_OFFS_READMODE, r16 +ttyOnUart1RunSkipping_end: + ret +; @end + + + +; --------------------------------------------------------------------------- +; @routine ttyOnUart1RunMsgReceived +; +; @clobbers R16 (R17, R18, R24, R25) + +ttyOnUart1RunMsgReceived: + ldd r16, Y+UART_HW_IFACE_OFFS_READBUFNUM + cpi r16, 0xff + breq ttyOnUart1RunMsgReceived_end + rcall NET_AddIncomingMsgNum ; (R17, R18, X) + brcs ttyOnUart1RunMsgReceived_enterIdle +ttyOnUart1RunMsgReceived_overrun: ; reset/reuse current buffer + rcall NET_Buffer_Locate + rcall UART_HW_Interface_SetReadBuffer ; (R17) + ldi r16, NET_IFACE_OFFS_ERR_NOBUF_LOW + rcall NET_Interface_IncCounter16 ; (R24, R25) +ttyOnUart1RunMsgReceived_enterIdle: + ldi r16, 0xff + std Y+UART_HW_IFACE_OFFS_READBUFNUM, r16 + ldi r16, UART_HW_READMODE_IDLE + std Y+UART_HW_IFACE_OFFS_READMODE, r16 +ttyOnUart1RunMsgReceived_end: ret ; @end