; *************************************************************************** ; copyright : (C) 2023 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. * ; *************************************************************************** ; *************************************************************************** ; defines .equ CPRO_CMD_PING = 10 .equ CPRO_CMD_PONG = 11 .equ CPRO_CMD_COMSENDSTATS = 20 .equ CPRO_CMD_COMRECVSTATS = 21 .equ CPRO_CMD_TWIBUSMEMBER = 30 .equ CPRO_CMD_DEBUG = 40 .equ CPRO_CMD_VALUE = 51 ; was 50 when sending timestamp instead of uid .equ CPRO_CMD_NEED_ADDRESS = 60 .equ CPRO_CMD_HAVE_ADDRESS = 61 .equ CPRO_CMD_CLAIM_ADDRESS = 62 .equ CPRO_CMD_DENY_ADDRESS = 63 .equ CPRO_CMD_ADDRESS_RANGE = 64 .equ CPRO_CMD_FLASH_START = 70 .equ CPRO_CMD_FLASH_END = 71 .equ CPRO_CMD_FLASH_ADDR = 72 .equ CPRO_CMD_FLASH_DATA = 73 .equ CPRO_CMD_FLASH_RSP = 74 .equ CPRO_CMD_DEVICE = 80 .equ CPRO_CMD_MEMSTATS = 81 .equ CPRO_CMD_SYSSTATS = 82 .equ CPRO_PACKET_HAVEADDR_OFFS_ADDRESS = COM2_MSG_OFFS_PAYLOAD+4 .equ CPRO_PACKET_CLAIMADDR_OFFS_ADDRESS = COM2_MSG_OFFS_PAYLOAD+4 .equ CPRO_PACKET_DENYADDR_OFFS_ADDRESS = COM2_MSG_OFFS_PAYLOAD+4 .equ CPRO_WAITTIME_INITIAL = 10 .equ CPRO_WAITTIME_GETADDR = 130 .equ CPRO_WAITTIME_CLAIMADDR = 17 .equ CPRO_WAITTIME_RECLAIMADDR = 10 ; current mode of operation .equ CPRO_MODE_NOADDRESS = 0x00 ; no address, yet .equ CPRO_MODE_NORMAL = 0x01 ; normal operation .equ CPRO_MODE_SEND_NEED_ADDR = 0x02 ; wait to send need address .equ CPRO_MODE_GETADDRSTARTED = 0x03 ; waiting for HAVE_ADDRESS and ADDRESS_RANGE packets to arrive .equ CPRO_MODE_SEND_CLAIM_ADDR1 = 0x04 ; send CLAIM_ADDRESS as part of reclaiming procedure .equ CPRO_MODE_CLAIMING_ADDR1 = 0x05 ; CLAIM_ADDRESS sent, waiting for HAVE_ADDRESS packet to reject the claim .equ CPRO_MODE_CLAIMING_ADDR2 = 0x06 ; CLAIM_ADDRESS sent, 2nd try .equ CPRO_MODE_CLAIMING_ADDR3 = 0x07 ; CLAIM_ADDRESS sent, 3rd try .equ CPRO_MODE_SENDING_HAVE_ADDR = 0x08 ; waiting for our turn to send HAVE_ADDRESS packet .equ CPRO_MODE_SEND_RECLAIM_ADDR = 0x09 ; send CLAIM_ADDRESS as part of reclaiming procedure .equ CPRO_MODE_RECLAIMING_ADDR = 0x0a ; CLAIM_ADDRESS with the previously used address sent after bootup .equ CPRO_MODE_SEND_DENY_ADDR = 0x0b ; someone claimed our address, send a DENY_ADDR message .equ CPRO_MODE_NEXT_FREE = 0x0c ; next free mode ; *************************************************************************** ; data .dseg cproDataBegin: cproMode: .byte 1 cproDataEnd: ; *************************************************************************** ; code .cseg CPRO_BEGIN: CPRO_Init: ; preset SRAM data area ldi xh, HIGH(cproDataBegin) ldi xl, LOW(cproDataBegin) clr r16 ldi r17, (cproDataEnd-cproDataBegin) rcall Utils_FillSram #ifdef MODULES_COM_WITH_ADDR_PROTO rcall CPRO_Address_Init #endif sec ret ; --------------------------------------------------------------------------- ; CPRO_OnPacketReceived: ; ; Try to handle the given packet. ; ; IN: ; - X : pointer to received buffer ; OUT: ; - CFLAG: set if handled, cleared otherwise ; USED: depending on called routines CPRO_OnPacketReceived: adiw xh:xl, COM2_MSG_OFFS_CMD ld r16, x sbiw xh:xl, COM2_MSG_OFFS_CMD cpi r16, CPRO_CMD_PING brne CPRO_OnPacketReceived_l1 rjmp cproHandlePing CPRO_OnPacketReceived_l1: #ifdef MODULES_COM_WITH_ADDR_PROTO rjmp CPRO_Address_OnPacketReceived #else clc ret #endif cproHandlePing: adiw xh:xl, COM2_MSG_OFFS_SRCADDR ld r16, x tst r16 ; dont handle src address 0 breq cproHandlePing_notHandled inc r16 breq cproHandlePing_notHandled ; dont handle src address 255 rcall CPRO_WritePong rjmp COM2_SendPacket ; use carry flag from this routine cproHandlePing_notHandled: clc ret CPRO_END: .equ MODULE_SIZE_CPRO = CPRO_END-CPRO_BEGIN