started working on new n23 code.
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
<gwbuild>
|
||||
|
||||
<subdirs>
|
||||
<subdirs-inactive>
|
||||
n00
|
||||
n06
|
||||
n11
|
||||
@@ -18,6 +18,11 @@
|
||||
n22
|
||||
n23
|
||||
t03
|
||||
</subdirs-inactive>
|
||||
|
||||
|
||||
<subdirs>
|
||||
n23
|
||||
</subdirs>
|
||||
|
||||
</gwbuild>
|
||||
|
||||
@@ -2,44 +2,9 @@
|
||||
|
||||
<gwbuild>
|
||||
|
||||
<target type="AvrHexFile" name="n23_firmware" >
|
||||
|
||||
<includes type="avrasm" >
|
||||
-I $(builddir)
|
||||
-I $(srcdir)
|
||||
-I $(topsrcdir)/avr
|
||||
-I $(topbuilddir)/avr
|
||||
</includes>
|
||||
|
||||
|
||||
<sources type="avrasm" >
|
||||
main.asm
|
||||
</sources>
|
||||
|
||||
|
||||
</target>
|
||||
|
||||
|
||||
|
||||
<target type="AvrHexFile" name="n23_boot" >
|
||||
|
||||
<includes type="avrasm" >
|
||||
-I $(builddir)
|
||||
-I $(srcdir)
|
||||
-I $(topsrcdir)/avr
|
||||
-I $(topbuilddir)/avr
|
||||
</includes>
|
||||
|
||||
|
||||
<sources type="avrasm" >
|
||||
boot.asm
|
||||
</sources>
|
||||
|
||||
|
||||
</target>
|
||||
|
||||
|
||||
<subdirs>
|
||||
boot
|
||||
main
|
||||
</subdirs>
|
||||
|
||||
<extradist>
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
|
||||
N20
|
||||
N23
|
||||
===
|
||||
|
||||
- Role: Air quality and climate sensors
|
||||
|
||||
32
avr/devices/n23/boot/0BUILD
Normal file
32
avr/devices/n23/boot/0BUILD
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml?>
|
||||
|
||||
<gwbuild>
|
||||
|
||||
<target type="AvrHexFile" name="n23_boot" >
|
||||
|
||||
<includes type="avrasm" >
|
||||
-I $(builddir)
|
||||
-I $(srcdir)
|
||||
-I $(topsrcdir)/avr
|
||||
-I $(topbuilddir)/avr
|
||||
</includes>
|
||||
|
||||
|
||||
<sources type="avrasm" >
|
||||
boot.asm
|
||||
</sources>
|
||||
|
||||
|
||||
</target>
|
||||
|
||||
|
||||
<subdirs>
|
||||
</subdirs>
|
||||
|
||||
<extradist>
|
||||
</extradist>
|
||||
|
||||
|
||||
</gwbuild>
|
||||
|
||||
|
||||
@@ -12,10 +12,13 @@
|
||||
.include "include/tn84def.inc" ; Define device ATtiny84
|
||||
.list
|
||||
|
||||
.include "./defs.asm"
|
||||
.include "../defs.asm"
|
||||
.include "defs_all.asm"
|
||||
|
||||
|
||||
#define COM_ACCEPT_ALL_DEST
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; defines
|
||||
@@ -119,6 +122,8 @@ main:
|
||||
.include "modules/network/msg/defs.asm"
|
||||
.include "modules/network/msg/crc.asm"
|
||||
|
||||
.include "common/debug.asm"
|
||||
|
||||
|
||||
|
||||
systemSetSpeed:
|
||||
33
avr/devices/n23/main/0BUILD
Normal file
33
avr/devices/n23/main/0BUILD
Normal file
@@ -0,0 +1,33 @@
|
||||
<?xml?>
|
||||
|
||||
<gwbuild>
|
||||
|
||||
<target type="AvrHexFile" name="n23_firmware" >
|
||||
|
||||
<includes type="avrasm" >
|
||||
-I $(builddir)
|
||||
-I $(srcdir)
|
||||
-I $(topsrcdir)/avr
|
||||
-I $(topbuilddir)/avr
|
||||
</includes>
|
||||
|
||||
|
||||
<sources type="avrasm" >
|
||||
main.asm
|
||||
</sources>
|
||||
|
||||
|
||||
</target>
|
||||
|
||||
|
||||
|
||||
<subdirs>
|
||||
</subdirs>
|
||||
|
||||
<extradist>
|
||||
</extradist>
|
||||
|
||||
|
||||
</gwbuild>
|
||||
|
||||
|
||||
@@ -30,7 +30,7 @@
|
||||
.include "include/tn84def.inc" ; Define device ATtiny84
|
||||
.list
|
||||
|
||||
.include "./defs.asm"
|
||||
.include "../defs.asm"
|
||||
.include "defs_all.asm"
|
||||
|
||||
|
||||
364
avr/devices/n23/main/main.asm
Normal file
364
avr/devices/n23/main/main.asm
Normal file
@@ -0,0 +1,364 @@
|
||||
; ***************************************************************************
|
||||
; 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. *
|
||||
; ***************************************************************************
|
||||
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; Source file for temperature sensor node on AtTiny 84
|
||||
;
|
||||
; This is for the full system (i.e. not the boot loader).
|
||||
;
|
||||
; All definitions and changes should go into this file.
|
||||
;
|
||||
;
|
||||
; ***************************************************************************
|
||||
|
||||
|
||||
.equ clock=1000000 ; Define the clock frequency
|
||||
;.equ clock=8000000 ; Define the clock frequency
|
||||
|
||||
|
||||
|
||||
.nolist
|
||||
.include "include/tn84def.inc" ; Define device ATtiny84
|
||||
.list
|
||||
|
||||
.include "../defs.asm"
|
||||
.include "defs_all.asm"
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; defines
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; generic
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; firmware settings including list of modules used
|
||||
|
||||
.equ FIRMWARE_VERSION_MAJOR = 0
|
||||
.equ FIRMWARE_VERSION_MINOR = 0
|
||||
.equ FIRMWARE_VERSION_PATCHLEVEL = 1
|
||||
|
||||
|
||||
#define MODULES_TIMER
|
||||
#define MODULES_LED_SIMPLE
|
||||
#define MODULES_TWI_MASTER
|
||||
;#define MODULES_LCD
|
||||
#define LCD_MINIMAL_FONT
|
||||
#define MODULES_SI7021
|
||||
#define MODULES_STATS
|
||||
;#define MODULES_OWI_MASTER
|
||||
;#define MODULES_DS18B20
|
||||
#define MODULES_MOTION
|
||||
;#define MODULES_CCS811
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; defines for values
|
||||
|
||||
.equ VALUE_ID_SI7021_TEMP = 0x01
|
||||
.equ VALUE_ID_SI7021_HUM = 0x02
|
||||
|
||||
.equ VALUE_ID_ADC = 0x03
|
||||
;.equ VALUE_ID_REED1 = 0x04
|
||||
;.equ VALUE_ID_REED2 = 0x05
|
||||
;.equ VALUE_ID_DS18B20_TEMP = 0x06
|
||||
.equ VALUE_ID_MOTION = 0x07
|
||||
|
||||
.equ VALUE_ID_CO2 = 0x08
|
||||
.equ VALUE_ID_TVOC = 0x09
|
||||
|
||||
;.equ VALUE_ID_REED_CONF = 0x81
|
||||
|
||||
.equ VALUE_ID_DEBUG = 0x7f
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; code segment
|
||||
|
||||
.cseg
|
||||
.org 000000
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; Reset and interrupt vectors (will be removed as soon as we can flash data over COM)
|
||||
|
||||
; rjmp main ; Reset vector
|
||||
rjmp BOOTLOADER_ADDR ; Reset vector ; use this for flashed system
|
||||
reti ; EXT_INT0
|
||||
rjmp uartBitbangIsrPcint0 ; PCI0
|
||||
reti ; PCI1
|
||||
reti ; WATCHDOG
|
||||
reti ; ICP1
|
||||
reti ; OC1A
|
||||
reti ; OC1B
|
||||
reti ; OVF1
|
||||
rjmp baseTimerIrqOC0A ; OC0A
|
||||
reti ; OC0B
|
||||
reti ; OVF0
|
||||
reti ; ACI
|
||||
reti ; ADCC
|
||||
reti ; ERDY
|
||||
reti ; USI_STR
|
||||
reti ; USI_OVF
|
||||
|
||||
|
||||
devInfoBlock: ; 12 bytes
|
||||
devInfoManufacturer: .db 'A', 'Q', 'U', 'A'
|
||||
devInfoId: .db DEVICEINFO_ID, 0
|
||||
devInfoVersion: .db DEVICEINFO_VERSION, DEVICEINFO_REVISION ; version, revision
|
||||
firmwareVersion: .db FIRMWARE_VARIANT_TEMP_WINDOW, FIRMWARE_VERSION_MAJOR
|
||||
.db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL
|
||||
|
||||
firmwareStart:
|
||||
cli
|
||||
; setup stack
|
||||
.ifdef SPH ; if SPH is defined
|
||||
ldi r16, High(RAMEND)
|
||||
out SPH, r16 ; init MSB stack pointer
|
||||
.endif
|
||||
ldi r16, Low(RAMEND)
|
||||
out SPL, r16 ; init LSB stack pointer
|
||||
|
||||
rcall systemSetSpeed
|
||||
rcall initHardware
|
||||
; rcall watchdogOff ; turn off watchdog timer (sometimes it stays on after reboot)
|
||||
|
||||
rcall Utils_Init
|
||||
rcall Utils_SetupUid
|
||||
|
||||
rcall initModules
|
||||
|
||||
sbi LED_SIMPLE_DDR, LED_SIMPLE_PINNUM ; out
|
||||
sbi LED_SIMPLE_PORT, LED_SIMPLE_PINNUM ; off
|
||||
|
||||
sei
|
||||
main_loop:
|
||||
; only modify SE, SM1 and SM0
|
||||
cli
|
||||
in r16, MCUCR
|
||||
ldi r17, (1<<SE) | (1<<SM1) | (1<<SM0)
|
||||
neg r17
|
||||
and r16, r17
|
||||
ori r16, (1<<SE) ; sleep mode "idle", enable
|
||||
out MCUCR, r16
|
||||
sei ; make sure interrupts really are enabled
|
||||
sleep ; sleep, wait for interrupt
|
||||
|
||||
rcall BaseTimer_Run
|
||||
; rcall ComOnUart0_Run
|
||||
|
||||
; TODO: read recved messages
|
||||
|
||||
rjmp main_loop
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine systemSetSpeed
|
||||
;
|
||||
; Called every 100ms. Add your routine calls here. No arguments, no results.
|
||||
|
||||
systemSetSpeed:
|
||||
.if clock == 1000000
|
||||
ldi r17, 0xd8
|
||||
ldi r16, (1<<CLKPS1) | (1<<CLKPS0) ; SUT=0, CLKPS=0011b
|
||||
sts CCP, r17
|
||||
sts CLKPR, r16
|
||||
.endif
|
||||
|
||||
.if clock == 8000000
|
||||
ldi r17, 0xd8
|
||||
clr r16 ; SUT=0, CLKPS=0
|
||||
sts CCP, r17
|
||||
sts CLKPR, r16
|
||||
.endif
|
||||
ret
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine initHardware
|
||||
;
|
||||
|
||||
initHardware:
|
||||
; set all ports as inputs and enable internal pull-up resistors
|
||||
ldi r16, 0xff
|
||||
clr r17
|
||||
.ifdef PORTA
|
||||
out DDRA, r17 ; all input
|
||||
sts PUEA, r16 ; enable pull-up on all
|
||||
.endif
|
||||
|
||||
.ifdef PORTB
|
||||
out DDRB, r17 ; all input
|
||||
sts PUEB, r16 ; enable pull-up on all
|
||||
.endif
|
||||
|
||||
.ifdef PORTC
|
||||
out DDRC, r17 ; all input
|
||||
sts PUEC, r16 ; enable pull-up on all
|
||||
.endif
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine initModules
|
||||
;
|
||||
|
||||
initModules:
|
||||
; init drivers
|
||||
rcall BaseTimer_Init
|
||||
rcall Clock_Init
|
||||
rcall LedSimple_Init
|
||||
rcall NET_Init
|
||||
rcall UART_BitBang_Init
|
||||
|
||||
; init apps
|
||||
ldi yl, LOW(uart_bitbang_iface)
|
||||
ldi yh, HIGH(uart_bitbang_iface)
|
||||
rcall AppNetwork_Init
|
||||
rcall AppMotion_Init
|
||||
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine onSystemTimerTick
|
||||
;
|
||||
; Called every 100ms. Add your routine calls here. No arguments, no results.
|
||||
|
||||
onSystemTimerTick:
|
||||
rcall Clock_Every100ms ; generates calls to onEverySecond/Minute/Hour/Day
|
||||
rcall LedSimple_Every100ms
|
||||
rcall UART_BitBang_Every100ms
|
||||
|
||||
ldi yl, LOW(uart_bitbang_iface)
|
||||
ldi yh, HIGH(uart_bitbang_iface)
|
||||
rcall AppNetwork_Every100ms
|
||||
|
||||
rcall AppMotion_Every100ms
|
||||
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
onEverySecond:
|
||||
onEveryMinute:
|
||||
onEveryHour:
|
||||
onEveryDay:
|
||||
ret
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; includes
|
||||
|
||||
.include "common/utils.asm"
|
||||
.include "common/utils_wait_fixed.asm"
|
||||
.include "common/utils_copy_from_flash.asm"
|
||||
.include "common/utils_copy_sdram.asm"
|
||||
.include "common/crc8.asm"
|
||||
|
||||
.include "modules/uart_bitbang2/main.asm"
|
||||
.include "modules/basetimer/main.asm"
|
||||
.include "modules/clock/main.asm"
|
||||
.include "modules/led_simple/main.asm"
|
||||
.include "modules/twimaster/main.asm"
|
||||
.include "modules/si7021/main.asm"
|
||||
|
||||
#ifdef MODULES_STATS
|
||||
.include "modules/stats/main.asm"
|
||||
#endif
|
||||
|
||||
.include "modules/motion/main.asm"
|
||||
|
||||
#ifdef MODULES_CCS811
|
||||
.include "modules/ccs811/main.asm"
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; defines for network interface
|
||||
|
||||
.equ netInterfaceData = uart_bitbang_iface
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; data in SRAM
|
||||
|
||||
.dseg
|
||||
|
||||
programRamBegin:
|
||||
;
|
||||
programRamEnd:
|
||||
|
||||
|
||||
|
||||
.cseg
|
||||
|
||||
|
||||
|
||||
#ifdef MODULES_SI7021
|
||||
|
||||
sendSI7021Humidity:
|
||||
rcall SI7021_SendHumidity
|
||||
brcs sendSI7021Humidity_okay
|
||||
; set timer to 1s to retry later
|
||||
ldi xl, LOW(sramTimerSI7021SendHumidity)
|
||||
ldi xh, HIGH(sramTimerSI7021SendHumidity)
|
||||
rjmp Timer_SetValueTo1s
|
||||
sendSI7021Humidity_okay:
|
||||
ret
|
||||
|
||||
|
||||
sendSI7021Temp:
|
||||
rcall SI7021_SendTemp
|
||||
brcs sendSI7021Temp_okay
|
||||
; set timer to 1s to retry later
|
||||
ldi xl, LOW(sramTimerSI7021SendTemp)
|
||||
ldi xh, HIGH(sramTimerSI7021SendTemp)
|
||||
rjmp Timer_SetValueTo1s
|
||||
sendSI7021Temp_okay:
|
||||
ret
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef MODULES_DS18B20
|
||||
sendDs18b20Temp:
|
||||
rcall Ds18b20_SendTemp
|
||||
brcs sendDs18b20Temp_okay
|
||||
; set timer to 1s to retry later
|
||||
ldi xl, LOW(sramSendDs18b20TempTimer)
|
||||
ldi xh, HIGH(sramSendDs18b20TempTimer)
|
||||
rjmp Timer_SetValueTo1s
|
||||
sendDs18b20Temp_okay:
|
||||
ret
|
||||
#endif
|
||||
|
||||
|
||||
108
avr/devices/n23/main/motion.asm
Normal file
108
avr/devices/n23/main/motion.asm
Normal file
@@ -0,0 +1,108 @@
|
||||
; ***************************************************************************
|
||||
; 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. *
|
||||
; ***************************************************************************
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; defines
|
||||
|
||||
.equ APP_MOTION_KEEPUPTIME = 100 ; 10s
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; data
|
||||
|
||||
.dseg
|
||||
|
||||
appMotionKeepupData: .byte FILTER_KEEPUP_DATA_SIZE
|
||||
appMotionValSchedData: .byte VALSCHED_DATA_SIZE
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; code
|
||||
|
||||
.cseg
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine AppMotion_Init @global
|
||||
;
|
||||
|
||||
AppMotion_Init:
|
||||
rcall Motion_Init
|
||||
|
||||
ldi yl, LOW(appMotionKeepupData)
|
||||
ldi yh, HIGH(appMotionKeepupData)
|
||||
rcall FilterKeepUp_Init
|
||||
ldi r16, APP_MOTION_KEEPUPTIME
|
||||
std Y+FILTER_KEEPUP_OFFS_RESTARTVALUE, r16
|
||||
|
||||
ldi yl, LOW(appMotionValSchedData)
|
||||
ldi yh, HIGH(appMotionValSchedData)
|
||||
rcall ValueScheduler_Init
|
||||
ldi r16, (1<<VALSCHED_FLAGS_REPEAT1_BIT) ; only repeat report interval for active motion
|
||||
std Y+VALSCHED_OFFS_FLAGS, r16
|
||||
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine AppMotion_Every100ms @global
|
||||
;
|
||||
|
||||
AppMotion_Every100ms:
|
||||
rcall Motion_GetValue ; get value into R16
|
||||
|
||||
ldi yl, LOW(appMotionKeepupData)
|
||||
ldi yh, HIGH(appMotionKeepupData)
|
||||
rcall FilterKeepUp_SetValue ; (R17)
|
||||
rcall FilterKeepUp_Every100ms
|
||||
rcall FilterKeepUp_GetValue ; get value into R16
|
||||
|
||||
ldi yl, LOW(appMotionValSchedData)
|
||||
ldi yh, HIGH(appMotionValSchedData)
|
||||
rcall FilterKeepUp_SetValue ; (R17)
|
||||
rcall ValueScheduler_Every100ms ; (R16, R17)
|
||||
rcall ValueScheduler_CheckSend ; (R16)
|
||||
brcc AppMotion_Every100ms_end
|
||||
rcall NET_Buffer_Alloc
|
||||
brcc AppMotion_Every100ms_end ; jmp on error
|
||||
push r16 ; buffer num
|
||||
rcall ValueScheduler_GetValue ; get value to r16
|
||||
mov r18, r16 ; R19:R18 = value
|
||||
clr r19
|
||||
ldi r16, 0xff ; DEST addr
|
||||
ldi r17, VALUE_ID_MOTION ; VALUE ID
|
||||
ldi r20, 1 ; R21:R20 = denom
|
||||
clr r21
|
||||
ldi r22, AQHOME_VALUETYPE_MOTION ; VALUE TYPE
|
||||
adiw xh:xl, 1
|
||||
rcall NETMSG_ValueWriteReport ; (R16, R17, R18, R19, R20, R21, R23, R24, R25)
|
||||
sbiw xh:xl, 1
|
||||
pop r16 ; buffer num
|
||||
ldi yl, LOW(netInterfaceData)
|
||||
ldi yh, HIGH(netInterfaceData)
|
||||
rcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X)
|
||||
brcs AppMotion_Every100ms_markSent
|
||||
rcall NET_Buffer_ReleaseByNum
|
||||
rjmp AppMotion_Every100ms_end
|
||||
AppMotion_Every100ms_markSent:
|
||||
ldi yl, LOW(appMotionValSchedData)
|
||||
ldi yh, HIGH(appMotionValSchedData)
|
||||
rcall ValueScheduler_MarkSent ; (R16)
|
||||
AppMotion_Every100ms_end:
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
|
||||
361
avr/devices/n23/main/network.asm
Normal file
361
avr/devices/n23/main/network.asm
Normal file
@@ -0,0 +1,361 @@
|
||||
; ***************************************************************************
|
||||
; 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. *
|
||||
; ***************************************************************************
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; defines
|
||||
|
||||
.equ APP_NETWORK_STATE_INITIALWAIT = 0 ; initially wait
|
||||
.equ APP_NETWORK_STATE_NEEDADDRESS = 1 ; wait for range msgs after sending "NEEDADDRESS"
|
||||
.equ APP_NETWORK_STATE_CLAIMADDRESS1 = 2 ; wait for "DENYADDRESS"
|
||||
.equ APP_NETWORK_STATE_CLAIMADDRESS2 = 3 ; wait for "DENYADDRESS"
|
||||
.equ APP_NETWORK_STATE_CLAIMADDRESS3 = 4 ; wait for "DENYADDRESS"
|
||||
.equ APP_NETWORK_STATE_HAVEADDRESS1 = 5 ; just notify (and handle "DENYADDRESS")
|
||||
.equ APP_NETWORK_STATE_HAVEADDRESS2 = 6 ; just notify (and handle "DENYADDRESS")
|
||||
.equ APP_NETWORK_STATE_UP = 7
|
||||
.equ APP_NETWORK_STATE_COUNT = 8
|
||||
|
||||
.equ APP_NETWORK_TIMER_100MS = 50
|
||||
|
||||
|
||||
.equ APP_NETWORK_ADDRESS_RANGE_BEGIN = 1
|
||||
.equ APP_NETWORK_ADDRESS_RANGE_END = 192
|
||||
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; data
|
||||
|
||||
.dseg
|
||||
|
||||
|
||||
|
||||
|
||||
; ***************************************************************************
|
||||
; code
|
||||
|
||||
.cseg
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine AppNetwork_Init @global
|
||||
;
|
||||
|
||||
AppNetwork_Init:
|
||||
rcall appNetworkResetState
|
||||
sec
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine AppNetwork_Fini @global
|
||||
;
|
||||
|
||||
AppNetwork_Fini:
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine AppNetwork_Every100ms @global
|
||||
;
|
||||
|
||||
AppNetwork_Every100ms:
|
||||
ldd r16, Y+NET_IFACE_OFFS_STATETIMER
|
||||
tst r16
|
||||
brne AppNetwork_Every100ms_decAndJump
|
||||
ret
|
||||
AppNetwork_Every100ms_decAndJump:
|
||||
dec r16
|
||||
std Y+NET_IFACE_OFFS_STATETIMER, r16
|
||||
brne AppNetwork_Every100ms_end
|
||||
ldd r16, Y+NET_IFACE_OFFS_STATUS
|
||||
cpi r16, APP_NETWORK_STATE_COUNT
|
||||
brcs AppNetwork_Every100ms_jump
|
||||
AppNetwork_Every100ms_end:
|
||||
ret
|
||||
AppNetwork_Every100ms_jump:
|
||||
ldi zl, LOW(appNetworkTimerTable)
|
||||
ldi zh, HIGH(appNetworkTimerTable)
|
||||
add zl, r16
|
||||
adc zh, r16
|
||||
sub zh, r16
|
||||
ijmp
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine AppNetwork_HandleMsg @global
|
||||
;
|
||||
; @param X pointer to received message
|
||||
|
||||
AppNetwork_HandleMsg:
|
||||
adiw xh:xl, NETMSG_OFFS_CMD
|
||||
ld r16, X
|
||||
sbiw xh:xl, NETMSG_OFFS_CMD
|
||||
cpi r16, NETMSG_CMD_HAVE_ADDRESS
|
||||
brcs AppNetwork_HandleMsg_clcRet ; lower than "HAVE_ADDR"
|
||||
cpi r16, NETMSG_CMD_ADDRESS_RANGE
|
||||
breq AppNetwork_HandleMsg_handleRangeMsg
|
||||
brcc AppNetwork_HandleMsg_clcRet ; higher or equal to "ADDR_RANGE"
|
||||
rjmp AppNetwork_HandleMsg_handleAddrMsg
|
||||
AppNetwork_HandleMsg_handleRangeMsg:
|
||||
; TODO
|
||||
rjmp AppNetwork_HandleMsg_clcRet
|
||||
|
||||
AppNetwork_HandleMsg_handleAddrMsg:
|
||||
rcall NETMSG_Address_Read ; (R18, R19)
|
||||
mov r16, r18
|
||||
subi r16, NETMSG_CMD_HAVE_ADDRESS
|
||||
ldi zl, LOW(appNetworkMsgTable)
|
||||
ldi zh, HIGH(appNetworkMsgTable)
|
||||
add zl, r16
|
||||
adc zh, r16
|
||||
sub zh, r16
|
||||
ijmp
|
||||
|
||||
AppNetwork_HandleMsg_clcRet:
|
||||
clc
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
appNetworkTimerTable:
|
||||
rjmp appNetworkHandleStateInitialWait
|
||||
rjmp appNetworkHandleStateNeedAddress
|
||||
rjmp appNetworkHandleStateClaimAddress1
|
||||
rjmp appNetworkHandleStateClaimAddress2
|
||||
rjmp appNetworkHandleStateClaimAddress3
|
||||
rjmp appNetworkHandleStateHaveAddress1
|
||||
rjmp appNetworkHandleStateHaveAddress2
|
||||
rjmp appNetworkHandleStateUp
|
||||
|
||||
|
||||
appNetworkHandleStateInitialWait:
|
||||
ldi r18, NETMSG_CMD_NEED_ADDRESS
|
||||
rjmp appNetworkSendMsgNextState
|
||||
|
||||
|
||||
appNetworkHandleStateNeedAddress:
|
||||
ldi r18, NETMSG_CMD_CLAIM_ADDRESS
|
||||
rjmp appNetworkSendMsgNextState
|
||||
|
||||
|
||||
appNetworkHandleStateClaimAddress1:
|
||||
ldi r18, NETMSG_CMD_CLAIM_ADDRESS
|
||||
rjmp appNetworkSendMsgNextState
|
||||
|
||||
|
||||
appNetworkHandleStateClaimAddress2:
|
||||
ldi r18, NETMSG_CMD_CLAIM_ADDRESS
|
||||
rjmp appNetworkSendMsgNextState
|
||||
|
||||
|
||||
appNetworkHandleStateClaimAddress3:
|
||||
ldi r18, NETMSG_CMD_HAVE_ADDRESS
|
||||
rjmp appNetworkSendMsgNextState
|
||||
|
||||
|
||||
appNetworkHandleStateHaveAddress1:
|
||||
ldi r18, NETMSG_CMD_HAVE_ADDRESS
|
||||
rjmp appNetworkSendMsgNextState
|
||||
|
||||
|
||||
appNetworkHandleStateHaveAddress2:
|
||||
ldi r16, APP_NETWORK_STATE_UP
|
||||
std Y+NET_IFACE_OFFS_STATUS, r16
|
||||
ldd r16, Y+NET_IFACE_OFFS_RANGE_BEGIN ; set interface address
|
||||
std Y+NET_IFACE_OFFS_ADDRESS, r16
|
||||
ret
|
||||
|
||||
|
||||
appNetworkHandleStateUp:
|
||||
ldi r16, 200 ; come again after 20s (nothing to do for now, maybe add some cleanup later)
|
||||
std Y+NET_IFACE_OFFS_STATETIMER, r16
|
||||
ret
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine appNetworkSendMsgNextState
|
||||
;
|
||||
; @param R18 msg type to send
|
||||
; @clobbers R16, R19 (R17, R18, R20, R21, X)
|
||||
|
||||
appNetworkSendMsgNextState:
|
||||
ldd r19, NET_IFACE_OFFS_RANGE_BEGIN
|
||||
rcall appNetworkSendAddrMsg ; (R16, R17, R18, R19, R20, R21, X)
|
||||
brcc appNetworkSendMsgNextState_end
|
||||
ldd r16, Y+NET_IFACE_OFFS_STATUS
|
||||
inc r16
|
||||
std Y+NET_IFACE_OFFS_STATUS, r16
|
||||
ldi r16, APP_NETWORK_TIMER_100MS
|
||||
std Y+NET_IFACE_OFFS_STATETIMER, r16
|
||||
appNetworkSendMsgNextState_end:
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
appNetworkMsgTable:
|
||||
rjmp appNetworkHandleMsgNeedAddr
|
||||
rjmp appNetworkHandleMsgHaveAddr
|
||||
rjmp appNetworkHandleMsgClaimAddr
|
||||
rjmp appNetworkHandleMsgDenyAddr
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine appNetworkHandleMsgNeedAddr
|
||||
;
|
||||
; @param R18 command
|
||||
; @param R19 address to send
|
||||
|
||||
appNetworkHandleMsgNeedAddr:
|
||||
clc
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine appNetworkHandleMsgHaveAddr
|
||||
;
|
||||
; @param R18 command
|
||||
; @param R19 address to send
|
||||
|
||||
appNetworkHandleMsgHaveAddr:
|
||||
clc
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine appNetworkHandleMsgClaimAddr
|
||||
;
|
||||
; @param R18 command
|
||||
; @param R19 address from CLAIM_ADDRESS message
|
||||
|
||||
appNetworkHandleMsgClaimAddr:
|
||||
ldd r16, Y+NET_IFACE_OFFS_ADDRESS
|
||||
cp r19, r16
|
||||
brne appNetworkHandleMsgClaimAddr_end ; not our address
|
||||
ldd r16, Y+NET_IFACE_OFFS_STATUS
|
||||
cpi r16, APP_NETWORK_STATE_UP ; up?
|
||||
brne appNetworkHandleMsgClaimAddr_end ; nope, ignore
|
||||
; network is up, someone claimed our address, deny it
|
||||
ldi r18, NETMSG_CMD_DENY_ADDRESS ; deny our addr
|
||||
ldd r19, Y+NET_IFACE_OFFS_ADDRESS
|
||||
rjmp appNetworkSendAddrMsg
|
||||
appNetworkHandleMsgClaimAddr_end:
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine appNetworkHandleMsgDenyAddr
|
||||
;
|
||||
; @param R18 command
|
||||
; @param R19 address from DENY_ADDR message
|
||||
|
||||
appNetworkHandleMsgDenyAddr:
|
||||
; check state
|
||||
ldd r16, Y+NET_IFACE_OFFS_STATUS
|
||||
cpi r16, APP_NETWORK_STATE_UP
|
||||
breq appNetworkHandleMsgDenyAddr_end ; ignore (our network stack is up)
|
||||
; still setting up address, check whether the last one is denied now
|
||||
ldd r16, Y+NET_IFACE_OFFS_RANGE_BEGIN
|
||||
cp r19, r16 ; our claimed address?
|
||||
brne appNetworkHandleMsgDenyAddr_end ; nope, jump
|
||||
; try next address (if any left)
|
||||
ldd r17, Y+NET_IFACE_OFFS_RANGE_END
|
||||
inc r16
|
||||
cp r16, r17
|
||||
brcs appNetworkHandleMsgDenyAddr_claimNext
|
||||
; out of addresses, start completely new after some waiting time
|
||||
rcall appNetworkResetState
|
||||
ldi r16, 200 ; wait for 20s before trying whole process again
|
||||
std Y+NET_IFACE_OFFS_STATETIMER, r16
|
||||
rjmp appNetworkHandleMsgClaimAddr_end
|
||||
appNetworkHandleMsgDenyAddr_claimNext:
|
||||
; send CLAIM_ADDR for next address (new state: APP_NETWORK_STATE_NEEDADDRESS+1)
|
||||
std Y+NET_IFACE_OFFS_RANGE_BEGIN, r16
|
||||
ldi r16, APP_NETWORK_STATE_NEEDADDRESS
|
||||
std Y+NET_IFACE_OFFS_STATUS, r16
|
||||
ldi r18, NETMSG_CMD_CLAIM_ADDRESS
|
||||
rjmp appNetworkSendMsgNextState
|
||||
appNetworkHandleMsgClaimAddr_end:
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine appNetworkSendAddrMsg
|
||||
;
|
||||
; @param Y pointer to device to write msg for
|
||||
; @param R18 command
|
||||
; @param R19 address to send
|
||||
; @clobbers R16 (R17, R18, R19, R20, R21, X)
|
||||
|
||||
appNetworkSendAddrMsg:
|
||||
rcall NET_Buffer_Alloc ; (R16, R17, X)
|
||||
brcc appNetworkSendAddrMsg_end
|
||||
adiw xh:xl, 1
|
||||
push r16
|
||||
rcall NETMSG_Address_Write ; (R16, R17, R18, R19, R20, R21)
|
||||
pop r16
|
||||
sbiw xh:xl, 1
|
||||
rcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X)
|
||||
brcs appNetworkSendAddrMsg_end
|
||||
rcall NET_Buffer_ReleaseByNum ; (R16, X)
|
||||
clc
|
||||
appNetworkSendAddrMsg_end:
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
|
||||
|
||||
; ---------------------------------------------------------------------------
|
||||
; @routine appNetworkResetState
|
||||
;
|
||||
; @param Y pointer to device to write msg for
|
||||
; @clobbers R16
|
||||
|
||||
appNetworkResetState:
|
||||
ldi r16, APP_NETWORK_STATE_INITIALWAIT
|
||||
std Y+NET_IFACE_OFFS_STATUS, r16
|
||||
ldi r16, APP_NETWORK_TIMER_100MS
|
||||
std Y+NET_IFACE_OFFS_STATETIMER, r16
|
||||
ldi r16, APP_NETWORK_ADDRESS_RANGE_BEGIN
|
||||
std Y+NET_IFACE_OFFS_RANGE_BEGIN, r16
|
||||
ldi r16, APP_NETWORK_ADDRESS_RANGE_END ; last possible address+1
|
||||
std Y+NET_IFACE_OFFS_RANGE_END, r16
|
||||
ret
|
||||
; @end
|
||||
|
||||
|
||||
Reference in New Issue
Block a user