avr: use new code in latest nodes.

This commit is contained in:
Martin Preuss
2025-04-29 00:35:45 +02:00
parent 2357b63b42
commit 1e90682605
22 changed files with 1194 additions and 1304 deletions

View File

@@ -22,6 +22,8 @@
<subdirs> <subdirs>
all
n21
n23 n23
</subdirs> </subdirs>

18
avr/devices/all/0BUILD Normal file
View File

@@ -0,0 +1,18 @@
<?xml?>
<gwbuild>
<extradist>
apps.asm
data.asm
defs.asm
includes.asm
main.asm
modules.asm
sendvalue.asm
</extradist>
</gwbuild>

59
avr/devices/all/apps.asm Normal file
View File

@@ -0,0 +1,59 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; @routine initApps
;
; Call init functions of the used apps. Add your routine calls here.
initApps:
#ifdef APPS_NETWORK
ldi yl, LOW(netInterfaceData)
ldi yh, HIGH(netInterfaceData)
rcall AppNetwork_Init
#endif
#ifdef APPS_MOTION
rcall AppMotion_Init
#endif
#ifdef APPS_DOOR
rcall AppDoor_Init
#endif
; done
ret
; @end
; ---------------------------------------------------------------------------
; @routine runApps
;
; Call run functions of the used modules. Add your routine calls here.
;
runApps:
; add more modules here
ret

14
avr/devices/all/data.asm Normal file
View File

@@ -0,0 +1,14 @@
; ***************************************************************************
; 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

45
avr/devices/all/defs.asm Normal file
View File

@@ -0,0 +1,45 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
.equ AQHOME_VALUETYPE_UNKNOWN = 0
.equ AQHOME_VALUETYPE_TEMP = 1
.equ AQHOME_VALUETYPE_HUMIDITY = 2
.equ AQHOME_VALUETYPE_DOOR = 3
.equ AQHOME_VALUETYPE_RGB = 4
.equ AQHOME_VALUETYPE_RGBW = 5
.equ AQHOME_VALUETYPE_MOTION = 6
.equ AQHOME_VALUETYPE_CO2 = 7
.equ AQHOME_VALUETYPE_TVOC = 8
.equ AQHOME_FW_TYPE_ATT84_BASE = 0
.equ AQHOME_FW_TYPE_ATT84_TEMP1 = 1
; ---------------------------------------------------------------------------
; EEPROM positions
.equ EEPROM_OFFS_UUID = 0 ; 4 bytes (occupy total of 8 bytes for extensibility)
.equ EEPROM_OFFS_COMADDR = 8 ; 1 byte (plus one byte reserved)
.equ EEPROM_OFFS_SEED = 10 ; 2 bytes
.equ EEPROM_OFFS_REED_CONF = 12 ; 1 byte (plus one byte reserved)
.equ EEPROM_OFFS_MAL_CONF_ONTIME = 14 ; 2 bytes
.equ EEPROM_OFFS_MAL_CONF_SRC1_ADDR = 16 ; 1 byte
.equ EEPROM_OFFS_MAL_CONF_SRC1_VALUEID = 17 ; 1 byte
.equ EEPROM_OFFS_MAL_CONF_SRC2_ADDR = 18 ; 1 byte
.equ EEPROM_OFFS_MAL_CONF_SRC2_VALUEID = 19 ; 1 byte
; next is 20

View File

@@ -0,0 +1,108 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
.include "devices/all/main.asm"
.include "devices/all/apps.asm"
.include "devices/all/modules.asm"
.include "devices/all/sendvalue.asm"
.include "devices/all/data.asm"
.include "common/utils.asm"
.include "common/utils_initial_wait.asm"
.include "common/utils_wait_fixed.asm"
.include "common/utils_copy_from_flash.asm"
.include "common/utils_copy_sdram.asm"
.include "modules/basetimer/main.asm"
#ifdef MODULES_NETWORK
.include "common/crc8.asm"
.include "common/m_fixedbuffers.asm"
.include "common/m_ringbuffer_y.asm"
.include "common/ringbuffer_y.asm"
.include "modules/network/defs.asm"
.include "modules/network/data.asm"
.include "modules/network/iface.asm"
.include "modules/network/main.asm"
.include "modules/network/buffer.asm"
.include "modules/network/msg/defs.asm"
.include "modules/network/msg/common.asm"
.include "modules/network/msg/crc.asm"
.include "modules/network/msg/value-w.asm"
.include "modules/network/msg/addr-r.asm"
.include "modules/network/msg/addr-w.asm"
#endif
#ifdef MODULES_UART_BITBANG
.include "modules/uart_bitbang2/defs.asm"
.include "modules/uart_bitbang2/iface.asm"
.include "modules/uart_bitbang2/lowlevel.asm"
#endif
#ifdef MODULES_CLOCK
.include "modules/clock/main.asm"
#endif
#ifdef MODULES_LED_SIMPLE
.include "modules/led_simple/main.asm"
#endif
#ifdef MODULES_TWI_MASTER
.include "modules/twimaster/main.asm"
#endif
#ifdef MODULES_SI7021
.include "modules/si7021/main2.asm"
#ifdef MODULES_NETWORK
.include "modules/si7021/send.asm"
#endif
#endif
#ifdef MODULES_STATS
; .include "modules/stats/main.asm"
#endif
#ifdef MODULES_MOTION
.include "modules/motion/main2.asm"
#endif
#ifdef MODULES_CCS811
.include "modules/ccs811/main.asm"
#endif
#ifdef MODULES_TCRT1000
.include "modules/tcrt1000/main2.asm"
#endif
#ifdef APPS_MOTION
.include "modules/f_keepup/main.asm"
.include "modules/valsched/main.asm"
.include "apps/motion/main.asm"
#endif
#ifdef APPS_DOOR
#ifndef APPS_MOTION
.include "modules/f_keepup/main.asm"
.include "modules/valsched/main.asm"
#endif
.include "apps/door/main.asm"
#endif
#ifdef APPS_NETWORK
.include "apps/network/main.asm"
.include "modules/network/msg/reboot-d.asm"
.include "modules/network/msg/reboot-r.asm"
#endif

126
avr/devices/all/main.asm Normal file
View File

@@ -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. *
; ***************************************************************************
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; @routine main @global
main:
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 watchdogOff ; turn off watchdog timer (sometimes it stays on after reboot)
rcall systemSetSpeed
rcall systemInitHardware
rcall Utils_Init
rcall Utils_SetupUid
rcall initModules
rcall initApps
rcall Utils_InitialWait
sei ; Enable interrupts
rcall onSystemStart
#ifdef MODULES_LED
ldi xl, LOW(blinkPattern) ; debug: set blink pattern
ldi xh, HIGH(blinkPattern)
ldi zl, LOW(ledA3Flash)
ldi zh, HIGH(ledA3Flash)
ldi yl, LOW(ledA3Sram)
ldi yh, HIGH(ledA3Sram)
rcall Led_SetPattern
#endif
main_loop:
rcall runModules
rcall runApps
#ifdef MODULES_NETWORK
rcall handleMessages
#endif
; 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
rjmp main_loop
main_loop_reboot:
cli
rjmp BOOTLOADER_ADDR
; @end
; ---------------------------------------------------------------------------
; @routine onSystemTimerTick
;
; Called every 100ms. No arguments, no results.
onSystemTimerTick:
rcall onEvery100ms
#ifdef MODULES_CLOCK
rcall Clock_Every100ms ; generates calls to onEverySecond/Minute/Hour/Day
#endif
#ifdef MODULES_LED_SIMPLE
rcall LedSimple_Every100ms
#endif
#ifdef MODULES_UART_BITBANG
rcall UART_BitBang_Every100ms
#endif
#ifdef MODULES_TCRT1000
rcall TCRT1K_Every100ms
#endif
#ifdef APPS_NETWORK
ldi yl, LOW(netInterfaceData)
ldi yh, HIGH(netInterfaceData)
rcall AppNetwork_Every100ms
#endif
#ifdef APPS_MOTION
rcall AppMotion_Every100ms
#endif
#ifdef APPS_DOOR
rcall AppDoor_Every100ms
#endif
ret
; @end

210
avr/devices/all/modules.asm Normal file
View File

@@ -0,0 +1,210 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; @routine initModules
;
; Call init functions of the used modules. Add your routine calls here.
initModules:
rcall BaseTimer_Init ; unconditionally call this
#ifdef MODULES_CLOCK
rcall Clock_Init
#endif
#ifdef MODULES_TIMER
rcall Timer_Init
#endif
#ifdef MODULES_LED
ldi zl, LOW(ledA3Flash)
ldi zh, HIGH(ledA3Flash)
ldi yl, LOW(ledA3Sram)
ldi yh, HIGH(ledA3Sram)
rcall Led_Init
#endif
#ifdef MODULES_LED_SIMPLE
rcall LedSimple_Init
#endif
#ifdef MODULES_COM
rcall Com2_Init ; init COM module
rcall CPRO_Init ; init COM protocol module
#endif
#ifdef MODULES_NETWORK
rcall NET_Init
#endif
#ifdef MODULES_UART_BITBANG
rcall UART_BitBang_Init
#endif
#ifdef MODULES_MOTION
rcall Motion_Init
#endif
#ifdef MODULES_TWI_MASTER
rcall TWI_Master_Init
#endif
#ifdef MODULES_OWI_MASTER
rcall OwiMaster_Init
#endif
#ifdef MODULES_LCD
rcall LCD_Init
#endif
#ifdef MODULES_BMP280
rcall BMP280_Init
#endif
#ifdef MODULES_SI7021
rcall SI7021_Init
#endif
#ifdef MODULES_DS18B20
rcall Ds18b20_Init
#endif
#ifdef MODULES_STATS
rcall Stats_Init
#endif
#ifdef MODULES_CNY70
rcall CNY70_Init
#endif
#ifdef MODULES_REED
rcall REED_Init
#endif
#ifdef MODULES_SK6812
rcall SK6812_Init
#endif
#ifdef MODULES_MOTION_LIGHT
rcall MotionLight_Init
#endif
#ifdef MODULES_TCRT1000
rcall TCRT1K_Init
#endif
#ifdef MODULES_CCS811
rcall CCS811_Init
#endif
; done
ret
; @end
; ---------------------------------------------------------------------------
; runModules
;
; Call run functions of the used modules. Add your routine calls here.
;
; IN:
; - nothing
; OUT:
; - nothing
; USED: depending on called routines
runModules:
rcall BaseTimer_Run
#ifdef MODULES_COM
; COM module (call until carry flag cleared but at most 10 times to not starve other modules)
ldi r16, 10
runModules_Com:
push r16
rcall Com2_Run
pop r16
brcc runModules_ComEnd
dec r16
brne runModules_Com
runModules_ComEnd:
#endif
#ifdef MODULES_STATS
rcall Stats_Run
#endif
#ifdef MODULES_REED
rcall REED_Run
#endif
#ifdef MODULES_CNY70
rcall CNY70_Run
#endif
#ifdef MODULES_DS18B20
rcall Ds18b20_Run
#endif
#ifdef MODULES_MOTION_LIGHT
; rcall MotionLight_Run
#endif
#ifdef MODULES_TCRT1000
; rcall TCRT1K_Run
#endif
; add more modules here
ret
#ifdef MODULES_NETWORK
; ---------------------------------------------------------------------------
; @routine handleMessages
handleMessages:
rcall NET_GetNextIncomingMsgNum ; R16=msg num
brcc handleMessages_end
rcall NET_Buffer_Locate ; X=buffer addr (R17)
adiw xh:xl, 1
push r16
rcall onMessageReceived
#ifdef APPS_NETWORK
; handle messages
ldi yl, LOW(netInterfaceData)
ldi yh, HIGH(netInterfaceData)
rcall AppNetwork_HandleMsg
#endif
; add more here
pop r16
rcall NET_Buffer_ReleaseByNum
sec
handleMessages_end:
ret
; @end
#endif

View File

@@ -0,0 +1,67 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; @routine Main_SendValue
;
; @param R17 value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22 value type
Main_SendValue:
push r17
rcall NET_Buffer_Alloc ; (R16, R17, X)
pop r17
brcc sendValue_end ; jmp on error
push r16 ; buffer num
ldi r16, 0xff ; DEST addr
adiw xh:xl, 1
ldi yl, LOW(netInterfaceData)
ldi yh, HIGH(netInterfaceData)
rcall NETMSG_ValueWriteReport ; (R16, R17, R18, R19, R20, R21, R23, R24, R25)
sbiw xh:xl, 1
pop r16 ; buffer num
rcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X)
brcs sendValue_end ; jump if okay
rcall NET_Buffer_ReleaseByNum ; otherwise release buffer
clc
sendValue_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine Main_Send8BitValue
;
; Send 8 bit value with denominator 1.
;
; @param R16 value
; @param R17 value id
; @param R22 value type
Main_Send8BitValue:
mov r18, r16 ; R19:R18 = value
clr r19
ldi r20, 1 ; R21:R20 = denom
clr r21
rjmp Main_SendValue
; @end

View File

@@ -2,44 +2,9 @@
<gwbuild> <gwbuild>
<target type="AvrHexFile" name="n21_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="n21_boot" >
<includes type="avrasm" >
-I $(builddir)
-I $(srcdir)
-I $(topsrcdir)/avr
-I $(topbuilddir)/avr
</includes>
<sources type="avrasm" >
boot.asm
</sources>
</target>
<subdirs> <subdirs>
boot
main
</subdirs> </subdirs>
<extradist> <extradist>

View File

@@ -0,0 +1,32 @@
<?xml?>
<gwbuild>
<target type="AvrHexFile" name="n21_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>

View File

@@ -12,8 +12,8 @@
.include "include/tn84def.inc" ; Define device ATtiny84 .include "include/tn84def.inc" ; Define device ATtiny84
.list .list
.include "./defs.asm" .include "../defs.asm"
.include "defs_all.asm" .include "devices/all/defs.asm"

View File

@@ -1,471 +0,0 @@
; ***************************************************************************
; 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
.include "common/utils_wait.asm"
; ---------------------------------------------------------------------------
; 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_COM
#define MODULES_COM_WITH_ADDR_PROTO
#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_TCRT1000
; ---------------------------------------------------------------------------
; 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_TCRT1K = 0x08
;.equ VALUE_ID_REED_CONF = 0x81
; ***************************************************************************
; 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:
rjmp main
; ***************************************************************************
; 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/basetimer/main.asm"
#ifdef MODULES_TIMER
.include "modules/timer/main.asm"
#endif
#ifdef MODULES_LED
.include "modules/led/main.asm"
#endif
#ifdef MODULES_LED_SIMPLE
.include "modules/led_simple/main.asm"
#endif
#ifdef MODULES_COM
.include "modules/com2/defs.asm"
.include "modules/com2/main.asm"
.include "modules/com2/buffer.asm"
.include "modules/uart_bitbang/defs.asm"
.include "modules/uart_bitbang/main.asm"
.include "modules/uart_bitbang/bytelevel.asm"
.include "modules/uart_bitbang/packetlevel.asm"
#ifdef MODULES_COM_WITH_ADDR_PROTO
.include "modules/comproto/defs.asm"
.include "modules/comproto/main.asm"
.include "modules/comproto/addr.asm"
.include "modules/comproto/msg_recvstats.asm"
.include "modules/comproto/msg_sendstats.asm"
.include "modules/comproto/msg_sysstats.asm"
.include "modules/comproto/msg_memstats.asm"
.include "modules/comproto/msg_pong.asm"
.include "modules/comproto/msg_value.asm"
.include "modules/comproto/msg_device.asm"
.include "modules/comproto/msg_reboot.asm"
#endif
#ifdef MODULES_LCD
.include "modules/com2/screen.asm"
.include "modules/comproto/screen.asm"
#endif
#endif
#ifdef MODULES_TWI_MASTER
.include "modules/twimaster/main.asm"
#endif
#ifdef MODULES_OWI_MASTER
.include "modules/owimaster/main.asm"
#endif
#ifdef MODULES_LCD
.include "modules/lcd/main.asm"
#endif
#ifdef MODULES_SI7021
.include "modules/si7021/main.asm"
#endif
#ifdef MODULES_DS18B20
.include "modules/ds18b20/main.asm"
#endif
#ifdef MODULES_STATS
.include "modules/stats/main.asm"
#endif
#ifdef MODULES_CNY70
.include "modules/cny70/main.asm"
#endif
#ifdef MODULES_REED
.include "modules/reed/main.asm"
#endif
#ifdef MODULES_MOTION
.include "modules/motion/main.asm"
#endif
#ifdef MODULES_TCRT1000
.include "modules/tcrt1000/main.asm"
#endif
; test
;#include "modules/uart_irq/defs.asm"
;#include "modules/uart_irq/iface.asm"
;#include "modules/uart_irq/iface1.asm"
; ***************************************************************************
; data in SRAM
.dseg
programRamBegin:
#ifdef MODULES_LCD
screenCounter: .byte 1
#endif
programRamEnd:
#ifdef MODULES_LCD
sramTimerWriteStats: .byte 2
sramTimerScreen: .byte 2
#endif
#ifdef MODULES_SI7021
sramTimerSI7021Measure: .byte 2
sramTimerSI7021SendTemp: .byte 2
sramTimerSI7021SendHumidity: .byte 2
#endif
#ifdef MODULES_CNY70
sramTimerCny70SendAdc: .byte 2
#endif
#ifdef MODULES_LCD
sramPeriodicalLcdMark: .byte 2
#endif
#ifdef MODULES_DS18B20
sramDs18b20Timer: .byte 2
sramSendDs18b20TempTimer: .byte 2
#endif
; ***************************************************************************
; data in FLASH
.cseg
; ---------------------------------------------------------------------------
; timer list
timerList:
; SRAM variable/counter routine flags secs (0=don't start or restart)
#ifdef MODULES_COM_WITH_ADDR_PROTO
.dw cproAddresModeTimer, CPRO_Address_OnTimer, 0, 0 ; (no restart)
#endif
#ifdef MODULES_STATS
.dw statsSendTimer, Stats_Timer, TIMER_FLAGS_IF_ADDR, 9000 ; every 15m
#endif
#ifdef MODULES_LCD
; .dw sramPeriodicalLcdMark, periodicalLcdMark, 0, 20 ; every 2s
; .dw sramTimerWriteStats, writeStats, 0, 100
.dw sramTimerScreen, printScreen, TIMER_FLAGS_IF_ADDR, 50 ; every 5s
#endif
#ifdef MODULES_SI7021
.dw sramTimerSI7021Measure, SI7021_OnTimer, 0, 300 ; every 30s
.dw sramTimerSI7021SendTemp, sendSI7021Temp, TIMER_FLAGS_IF_ADDR, 600 ; every 60s
.dw sramTimerSI7021SendHumidity, sendSI7021Humidity, TIMER_FLAGS_IF_ADDR, 600 ; every 60s
#endif
#ifdef MODULES_CNY70
.dw sramTimerCny70SendAdc, CNY70_OnTimer, TIMER_FLAGS_IF_ADDR, 50 ; every 5s
#endif
#ifdef MODULES_DS18B20
.dw sramDs18b20Timer, Ds18b20_OnTimer, 0, 300 ; every 30s
.dw sramSendDs18b20TempTimer, sendDs18b20Temp, TIMER_FLAGS_IF_ADDR, 600 ; every 60s
#endif
.dw 0 ; end of list
.include "main_all.asm"
systemSetSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, 0
out CLKPR, r16
out CLKPR, r17
.endif
ret
systemSetBootSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, (1<<CLKPS1) | (1<<CLKPS0)
out CLKPR, r16
out CLKPR, r17
.endif
ret
; ---------------------------------------------------------------------------
; Called on first time run, i.e. on system start. No arguments, no results.
onSystemStart:
ldi xh, HIGH(programRamBegin)
ldi xl, LOW(programRamBegin)
clr r16
ldi r17, (programRamEnd-programRamBegin)
rcall Utils_FillSram
ret
#ifdef MODULES_LCD
printScreen:
lds r16, screenCounter
tst r16
brne printScreen_l1
rcall CPRO_Screen
rjmp printScreen_counter
printScreen_l1:
cpi r16, 1
brne printScreen_l2
rcall COM2_Screen_RecvStats
rjmp printScreen_counter
printScreen_l2:
; cpi r16, 2
; brne printScreen_l3
; rcall COM2_Screen_SendStats
; rjmp printScreen_counter
printScreen_l3:
; add more screens here
printScreen_counter:
lds r16, screenCounter
inc r16
cpi r16, 2 ; number of screens
brcs printScreen_store
clr r16
printScreen_store:
sts screenCounter, r16
printScreen_end:
ret
;periodicalLcdMark:
; rcall printTimerMark
; ret
;writeStats:
; rcall printSendStats
; ret
#endif
#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
; ---------------------------------------------------------------------------
; Called every 100ms. Add your routine calls here. No arguments, no results.
onEvery100ms:
#ifdef MODULES_LED_SIMPLE
rcall LedSimple_Every100ms
#endif
#ifdef MODULES_REED
rcall REED_Every100ms
#endif
#ifdef MODULES_MOTION
rcall Motion_Every100ms
#endif
#ifdef MODULES_TCRT1000
rcall TCRT1K_Every100ms
#endif
ret
; ---------------------------------------------------------------------------
; @routine onPacketReceived:
;
; Called after a packet was received via COM module. Add your routine calls here.
;
; The packet will be released in any case after return from this call.
;
; @return CFLAG set if message handled, cleared otherwise
; @param X pointer to received buffer
; @clobbers all
onPacketReceived:
#ifdef MODULES_MOTION_LIGHT
rcall MotionLight_OnPacketReceived
brcs onPacketReceived_end
#endif
#ifdef MODULES_SK6812
rcall SK6812_OnPacketReceived
brcs onPacketReceived_end
#endif
#ifdef MODULES_REED
rcall REED_OnPacketReceived
brcs onPacketReceived_end
#endif
#ifdef MODULES_COM
rcall CPRO_OnPacketReceived
brcs onPacketReceived_end
#endif
clc
onPacketReceived_end:
ret
; @end

View File

@@ -0,0 +1,34 @@
<?xml?>
<gwbuild>
<target type="AvrHexFile" name="n21_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>
data.asm
</extradist>
</gwbuild>

View File

@@ -0,0 +1,19 @@
; ***************************************************************************
; 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
programRamBegin:
programRamSensorTimer: .byte 1 ; seconds counter
programRamStatsTimer: .byte 1 ; minutes counter
programRamEnd:

View File

@@ -0,0 +1,323 @@
; ***************************************************************************
; 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. *
; ***************************************************************************
; ***************************************************************************
; 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 "./data.asm"
.include "devices/all/defs.asm"
.include "common/utils_wait.asm"
; ***************************************************************************
; defines
; ---------------------------------------------------------------------------
; generic
.equ NET_BUFFERS_NUM = 6
.equ NET_BUFFERS_SIZE = 28
.equ PROGRAM_SENSOR_INTERVAL_SECS = 60
.equ PROGRAM_STATS_INTERVAL_MINS = 10
; ---------------------------------------------------------------------------
; 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_CLOCK
#define MODULES_LED_SIMPLE
#define MODULES_NETWORK
#define MODULES_UART_BITBANG
#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_TCRT1000
;#define MODULES_CCS811
#define APPS_NETWORK
#define APPS_MOTION
#define APPS_DOOR
; ---------------------------------------------------------------------------
; 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_TCRT1K = 0x08
;.equ VALUE_ID_REED_CONF = 0x81
; ***************************************************************************
; 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 UART_BitBang_PcintIsr ; 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
; ---------------------------------------------------------------------------
; @routine firmwareStart @global
firmwareStart:
rjmp main
; @end
; ---------------------------------------------------------------------------
; @routine onSystemStart
onSystemStart:
ret
; @end
; ---------------------------------------------------------------------------
; @routine systemSetSpeed
;
; Called every 100ms. Add your routine calls here. No arguments, no results.
systemSetSpeed:
.if clock == 8000000
ldi r16, (1<<CLKPCE)
ldi r17, 0
out CLKPR, r16
out CLKPR, r17
.endif
.if clock == 1000000
ldi r16, (1<<CLKPCE)
ldi r17, (1<<CLKPS1) | (1<<CLKPS0)
out CLKPR, r16
out CLKPR, r17
.endif
ret
; @end
; ---------------------------------------------------------------------------
; @routine systemInitHardware
;
systemInitHardware:
; set all ports as inputs and enable internal pull-up resistors
ldi r16, 0xff
clr r17
.ifdef PORTA
out DDRA, r17 ; all input
out PORTA, r16 ; enable pull-up on all
.endif
.ifdef PORTB
out DDRB, r17 ; all input
out PORTB, r16 ; enable pull-up on all
.endif
.ifdef PORTC
out DDRC, r17 ; all input
out PORTC, r16 ; enable pull-up on all
.endif
ret
; @end
; ---------------------------------------------------------------------------
; @routine onMessageReceived
;
; Called on every message received
onMessageReceived:
clc
ret
; @end
; ---------------------------------------------------------------------------
; @routine onEvery100ms
;
; Called every 100ms. Add your routine calls here. No arguments, no results.
onEvery100ms:
; nothing to do here (all done in devices/all/main.asm)
ret
; @end
; ---------------------------------------------------------------------------
; @routine onEverySecond
onEverySecond:
lds r16, programRamSensorTimer
inc r16
cpi r16, PROGRAM_SENSOR_INTERVAL_SECS
brcs onEverySecond_store
clr r16
onEverySecond_store:
sts programRamSensorTimer, r16
cpi r16, 1
breq onEverySecond_measureValue1
cpi r16, 19
breq onEverySecond_measureValue2
cpi r16, 39
breq onEverySecond_sendValue1
cpi r16, 49
breq onEverySecond_sendValue2
ret
onEverySecond_measureValue1:
rjmp SI7021_MeasureTemp
onEverySecond_measureValue2:
rjmp SI7021_MeasureHumidity
onEverySecond_sendValue1:
rjmp SI7021_SendTemperature
onEverySecond_sendValue2:
rjmp SI7021_SendHumidity
; @end
; ---------------------------------------------------------------------------
; @routine onEveryMinute
onEveryMinute:
rcall checkSendStats
ret
; @end
onEveryHour:
onEveryDay:
ret
checkSendStats:
lds r16, programRamStatsTimer
inc r16
cpi r16, PROGRAM_STATS_INTERVAL_MINS
brcs checkSendStats_store
clr r16
checkSendStats_store:
sts programRamStatsTimer, r16
ldi yl, LOW(netInterfaceData)
ldi yh, HIGH(netInterfaceData)
cpi r16, 1
breq checkSendStats_sendDevice
cpi r16, 2
breq checkSendStats_sendTxdStats
cpi r16, 3
breq checkSendStats_sendRxdStats
ret
checkSendStats_sendTxdStats:
rjmp AppNetwork_SendTxdStats
checkSendStats_sendRxdStats:
rjmp AppNetwork_SendRxdStats
checkSendStats_sendDevice:
rjmp AppNetwork_SendDevice
; ***************************************************************************
; includes
.include "devices/all/includes.asm"
.include "apps/network/stats.asm"
.include "modules/network/msg/sendstats-w.asm"
.include "modules/network/msg/recvstats-w.asm"
.include "modules/network/msg/device-w.asm"
; ---------------------------------------------------------------------------
; defines for network interface
.equ netInterfaceData = uart_bitbang_iface

View File

@@ -25,6 +25,7 @@
</subdirs> </subdirs>
<extradist> <extradist>
data.asm
</extradist> </extradist>

View File

@@ -13,6 +13,7 @@
programRamBegin: programRamBegin:
programRamSensorTimer: .byte 1 programRamSensorTimer: .byte 1 ; seconds counter
programRamStatsTimer: .byte 1 ; minutes counter
programRamEnd: programRamEnd:

View File

@@ -33,7 +33,7 @@
.include "../defs.asm" .include "../defs.asm"
.include "./data.asm" .include "./data.asm"
.include "defs_all.asm" .include "devices/all/defs.asm"
.include "common/utils_wait.asm" .include "common/utils_wait.asm"
@@ -44,10 +44,12 @@
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; generic ; generic
.equ NET_BUFFERS_NUM = 6 .equ NET_BUFFERS_NUM = 6
.equ NET_BUFFERS_SIZE = 28 .equ NET_BUFFERS_SIZE = 28
.equ PROGRAM_SENSOR_INTERVAL_SECS = 60
.equ PROGRAM_STATS_INTERVAL_MINS = 10
.equ PROGRAM_TIMER_VALUE = 60
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
@@ -58,18 +60,25 @@
.equ FIRMWARE_VERSION_PATCHLEVEL = 1 .equ FIRMWARE_VERSION_PATCHLEVEL = 1
#define MODULES_TIMER ; #define MODULES_TIMER
#define MODULES_CLOCK
#define MODULES_LED_SIMPLE #define MODULES_LED_SIMPLE
#define MODULES_NETWORK
#define MODULES_UART_BITBANG
#define MODULES_TWI_MASTER #define MODULES_TWI_MASTER
;#define MODULES_LCD ;#define MODULES_LCD
#define LCD_MINIMAL_FONT ;#define LCD_MINIMAL_FONT
#define MODULES_SI7021 #define MODULES_SI7021
#define MODULES_STATS ;#define MODULES_STATS
;#define MODULES_OWI_MASTER ;#define MODULES_OWI_MASTER
;#define MODULES_DS18B20 ;#define MODULES_DS18B20
#define MODULES_MOTION #define MODULES_MOTION
;#define MODULES_CCS811 ;#define MODULES_CCS811
#define APPS_NETWORK
#define APPS_MOTION
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; defines for values ; defines for values
@@ -91,6 +100,8 @@
; *************************************************************************** ; ***************************************************************************
; code segment ; code segment
@@ -129,49 +140,21 @@ devInfoVersion: .db DEVICEINFO_VERSION, DEVICEINFO_REVISION ; v
firmwareVersion: .db FIRMWARE_VARIANT_TEMP_WINDOW, FIRMWARE_VERSION_MAJOR firmwareVersion: .db FIRMWARE_VARIANT_TEMP_WINDOW, FIRMWARE_VERSION_MAJOR
.db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL .db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL
; ---------------------------------------------------------------------------
; @routine firmwareStart @global
firmwareStart: firmwareStart:
cli rjmp main
; setup stack ; @end
.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
ldi r16, PROGRAM_TIMER_VALUE ; ---------------------------------------------------------------------------
sts programRamSensorTimer, r16 ; @routine onSystemStart
rcall initModules onSystemStart:
ret
sbi LED_SIMPLE_DDR, LED_SIMPLE_PINNUM ; out ; @end
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 ; let basetimer run
rcall handleMessages ; handle incoming messages
; rcall ComOnUart0_Run
rjmp main_loop
@@ -196,14 +179,15 @@ systemSetSpeed:
.endif .endif
ret ret
; @end
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; @routine initHardware ; @routine systemInitHardware
; ;
initHardware: systemInitHardware:
; set all ports as inputs and enable internal pull-up resistors ; set all ports as inputs and enable internal pull-up resistors
ldi r16, 0xff ldi r16, 0xff
clr r17 clr r17
@@ -227,46 +211,24 @@ initHardware:
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; @routine initModules ; @routine onMessageReceived
; ;
; Called on every message received
initModules: onMessageReceived:
; init drivers clc
rcall BaseTimer_Init
rcall Clock_Init
rcall LedSimple_Init
rcall NET_Init
rcall UART_BitBang_Init
rcall Motion_Init
rcall SI7021_Init
; init apps
ldi yl, LOW(uart_bitbang_iface)
ldi yh, HIGH(uart_bitbang_iface)
rcall AppNetwork_Init
rcall AppMotion_Init
ret ret
; @end ; @end
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; @routine onSystemTimerTick ; @routine onEvery100ms
; ;
; Called every 100ms. Add your routine calls here. No arguments, no results. ; Called every 100ms. Add your routine calls here. No arguments, no results.
onSystemTimerTick: onEvery100ms:
rcall Clock_Every100ms ; generates calls to onEverySecond/Minute/Hour/Day ; nothing to do here (all done in devices/all/main.asm)
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 ret
; @end ; @end
@@ -277,18 +239,19 @@ onSystemTimerTick:
onEverySecond: onEverySecond:
lds r16, programRamSensorTimer lds r16, programRamSensorTimer
dec r16 inc r16
brne onEverySecond_store cpi r16, PROGRAM_SENSOR_INTERVAL_SECS
ldi r16, PROGRAM_TIMER_VALUE brcs onEverySecond_store
clr r16
onEverySecond_store: onEverySecond_store:
sts programRamSensorTimer, r16 sts programRamSensorTimer, r16
cpi r16, 59 cpi r16, 1
breq onEverySecond_measureValue1 breq onEverySecond_measureValue1
cpi r16, 29
breq onEverySecond_measureValue2
cpi r16, 19 cpi r16, 19
breq onEverySecond_measureValue2
cpi r16, 39
breq onEverySecond_sendValue1 breq onEverySecond_sendValue1
cpi r16, 9 cpi r16, 49
breq onEverySecond_sendValue2 breq onEverySecond_sendValue2
ret ret
onEverySecond_measureValue1: onEverySecond_measureValue1:
@@ -296,152 +259,63 @@ onEverySecond_measureValue1:
onEverySecond_measureValue2: onEverySecond_measureValue2:
rjmp SI7021_MeasureHumidity rjmp SI7021_MeasureHumidity
onEverySecond_sendValue1: onEverySecond_sendValue1:
rjmp sendTemperature rjmp SI7021_SendTemperature
onEverySecond_sendValue2: onEverySecond_sendValue2:
rjmp sendHumidity rjmp SI7021_SendHumidity
; @end ; @end
; ---------------------------------------------------------------------------
; @routine onEveryMinute
onEveryMinute: onEveryMinute:
rcall checkSendStats
ret
; @end
onEveryHour: onEveryHour:
onEveryDay: onEveryDay:
ret ret
; --------------------------------------------------------------------------- checkSendStats:
; @routine handleMessages lds r16, programRamStatsTimer
inc r16
handleMessages: cpi r16, PROGRAM_STATS_INTERVAL_MINS
rcall NET_GetNextIncomingMsgNum ; R16=msg num brcs checkSendStats_store
brcc handleMessages_end clr r16
rcall NET_Buffer_Locate ; X=buffer addr (R17) checkSendStats_store:
push r16 sts programRamStatsTimer, r16
; handle messages ldi yl, LOW(netInterfaceData)
ldi yl, LOW(uart_bitbang_iface) ldi yh, HIGH(netInterfaceData)
ldi yh, HIGH(uart_bitbang_iface) cpi r16, 1
rcall AppNetwork_HandleMsg breq checkSendStats_sendDevice
; add more here cpi r16, 2
pop r16 breq checkSendStats_sendTxdStats
rcall NET_Buffer_ReleaseByNum cpi r16, 3
sec breq checkSendStats_sendRxdStats
handleMessages_end:
ret ret
; @end checkSendStats_sendTxdStats:
rjmp AppNetwork_SendTxdStats
checkSendStats_sendRxdStats:
rjmp AppNetwork_SendRxdStats
; --------------------------------------------------------------------------- checkSendStats_sendDevice:
; @routine sendTemperature rjmp AppNetwork_SendDevice
sendTemperature:
ldi r16, SI7021_VALUE_TEMP
rcall SI7021_GetValue
brcc sendTemperature_end
ldi r17, VALUE_ID_SI7021_TEMP ; VALUE ID
ldi r22, AQHOME_VALUETYPE_TEMP ; VALUE TYPE
rcall sendValue
sendTemperature_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine sendHumidity
sendHumidity:
ldi r16, SI7021_VALUE_HUMIDITY
rcall SI7021_GetValue
brcc sendHumidity_end
ldi r17, VALUE_ID_SI7021_HUM ; VALUE ID
ldi r22, AQHOME_VALUETYPE_HUMIDITY ; VALUE TYPE
rcall sendValue
sendHumidity_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine sendValue
;
; @param R17 value id
; @param R19:R18 value
; @param R21:R20 denom (e.g. 100, meaning value must be divided by 100)
; @param R22 value type
sendValue:
push r17
rcall NET_Buffer_Alloc ; (R16, R17, X)
pop r17
brcc sendValue_end ; jmp on error
push r16 ; buffer num
ldi r16, 0xff ; DEST addr
adiw xh:xl, 1
ldi yl, LOW(netInterfaceData)
ldi yh, HIGH(netInterfaceData)
rcall NETMSG_ValueWriteReport ; (R16, R17, R18, R19, R20, R21, R23, R24, R25)
sbiw xh:xl, 1
pop r16 ; buffer num
rcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X)
brcs sendValue_end ; jump if okay
rcall NET_Buffer_ReleaseByNum ; otherwise release buffer
clc
sendValue_end:
ret
; @end
; *************************************************************************** ; ***************************************************************************
; includes ; includes
.include "common/utils.asm" .include "devices/all/includes.asm"
.include "common/utils_wait_fixed.asm" .include "apps/network/stats.asm"
.include "common/utils_copy_from_flash.asm" .include "modules/network/msg/sendstats-w.asm"
.include "common/utils_copy_sdram.asm" .include "modules/network/msg/recvstats-w.asm"
.include "common/crc8.asm" .include "modules/network/msg/device-w.asm"
.include "common/m_fixedbuffers.asm"
.include "common/m_ringbuffer_y.asm"
.include "common/ringbuffer_y.asm"
.include "modules/network/defs.asm"
.include "modules/network/data.asm"
.include "modules/network/iface.asm"
.include "modules/network/main.asm"
.include "modules/network/buffer.asm"
.include "modules/network/msg/defs.asm"
.include "modules/network/msg/common.asm"
.include "modules/network/msg/crc.asm"
.include "modules/network/msg/value-w.asm"
.include "modules/network/msg/addr-r.asm"
.include "modules/network/msg/addr-w.asm"
.include "modules/uart_bitbang2/defs.asm"
.include "modules/uart_bitbang2/iface.asm"
.include "modules/uart_bitbang2/lowlevel.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/main2.asm"
#ifdef MODULES_STATS
; .include "modules/stats/main.asm"
#endif
.include "modules/motion/main2.asm"
#ifdef MODULES_CCS811
.include "modules/ccs811/main.asm"
#endif
.include "modules/f_keepup/main.asm"
.include "modules/valsched/main.asm"
.include "./motion.asm"
.include "./network.asm"

View File

@@ -1,126 +0,0 @@
; ***************************************************************************
; 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 ValueScheduler_SetValue ; (R17)
rcall ValueScheduler_Every100ms ; (R16, R17)
rcall ValueScheduler_CheckSend ; (R16)
brcc AppMotion_Every100ms_end
; prepare and send value message
rcall appMotionSendValue
brcc AppMotion_Every100ms_end ; jmp on error
ldi yl, LOW(appMotionValSchedData)
ldi yh, HIGH(appMotionValSchedData)
rcall ValueScheduler_MarkSent ; (R16)
AppMotion_Every100ms_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine appMotionSendValue
;
; @return CFLAGS set if message enqueued, cleared on error
; @clobbers R17, R18, R19, R20, R21, R22, Y (R16, R23, R24, R25)
appMotionSendValue:
rcall NET_Buffer_Alloc ; (R16, R17, X)
brcc appMotionSendValue_end ; jmp on error
push r16 ; buffer num
ldi yl, LOW(appMotionValSchedData)
ldi yh, HIGH(appMotionValSchedData)
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
ldi yl, LOW(netInterfaceData)
ldi yh, HIGH(netInterfaceData)
rcall NETMSG_ValueWriteReport ; (R16, R17, R18, R19, R20, R21, R23, R24, R25)
sbiw xh:xl, 1
pop r16 ; buffer num
rcall NET_Interface_AddOutgoingMsgNum ; (R17, R18, X)
brcs appMotionSendValue_end ; jump if okay
rcall NET_Buffer_ReleaseByNum ; otherwise release buffer
clc
appMotionSendValue_end:
ret
; @end

View File

@@ -1,401 +0,0 @@
; ***************************************************************************
; 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
in r15, SREG
push r15
cli
ldi xl, LOW(EEPROM_OFFS_COMADDR)
ldi xh, HIGH(EEPROM_OFFS_COMADDR)
rcall Utils_WriteEepromIncr ; write address to EEPROM
pop r15
out SREG, r15
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, Y+NET_IFACE_OFFS_RANGE_BEGIN
rcall appNetworkSendAddrMsg ; (R16, R17, R18, R19, R20, R21, X)
brcc appNetworkSendMsgNextState_retry
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
ret
appNetworkSendMsgNextState_retry:
ldi r16, 1
std Y+NET_IFACE_OFFS_STATETIMER, r16
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 appNetworkHandleMsgDenyAddr_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
appNetworkHandleMsgDenyAddr_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
rcall appNetworkGetAddressFromEeprom ; R16=addr (R15, X)
tst r16
breq appNetworkResetState_setRangeEnd
cpi r16, 0xff
breq appNetworkResetState_setRangeEnd
std Y+NET_IFACE_OFFS_RANGE_BEGIN, r16
appNetworkResetState_setRangeEnd:
ldi r16, APP_NETWORK_ADDRESS_RANGE_END ; last possible address+1
std Y+NET_IFACE_OFFS_RANGE_END, r16
ret
; @end
; ---------------------------------------------------------------------------
; @routine appNetworkGetAddressFromEeprom
;
; @return R16 address from EEPROM
; @clobbers R15, X
appNetworkGetAddressFromEeprom:
ldi xl, LOW(EEPROM_OFFS_COMADDR)
ldi xh, HIGH(EEPROM_OFFS_COMADDR)
in r15, SREG
push r15
cli
rcall Utils_ReadEepromIncr ; (R16)
pop r15
out SREG, r15
ret
; @end

View File

@@ -36,12 +36,11 @@ main:
rcall Utils_Init rcall Utils_Init
rcall Utils_SetupUid rcall Utils_SetupUid
rcall initModules rcall initModules
rcall initialWait rcall Utils_InitialWait
sei ; Enable interrupts sei ; Enable interrupts
rcall onSystemStart rcall onSystemStart
#ifdef MODULES_LED #ifdef MODULES_LED
ldi xl, LOW(blinkPattern) ; debug: set blink pattern ldi xl, LOW(blinkPattern) ; debug: set blink pattern
ldi xh, HIGH(blinkPattern) ldi xh, HIGH(blinkPattern)
@@ -52,17 +51,13 @@ main:
rcall Led_SetPattern rcall Led_SetPattern
#endif #endif
; sbi DDRA, PORTA2 ; debug
; sbi PINA, PORTA2 ; debug (toggle)
; cbi PORTA, PORTA2 ; debug (on)
; sbi PORTA, PORTA2 ; debug (off)
; ldi r16, 1
; sts twiMasterScanEnabled, r16
main_loop: main_loop:
rcall runModules rcall runModules
#ifdef MODULES_NETWORK
rcall handleMessages
#endif
; only modify SE, SM1 and SM0 ; only modify SE, SM1 and SM0
cli cli
in r16, MCUCR in r16, MCUCR
@@ -71,7 +66,7 @@ main_loop:
and r16, r17 and r16, r17
ori r16, (1<<SE) ; sleep mode "idle", enable ori r16, (1<<SE) ; sleep mode "idle", enable
out MCUCR, r16 out MCUCR, r16
sei ; make sure interrupts really are enabled sei ; make sure interrupts really are enabled
sleep ; sleep, wait for interrupt sleep ; sleep, wait for interrupt
rjmp main_loop rjmp main_loop
@@ -79,20 +74,17 @@ main_loop:
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; initModules ; @routine initModules
; ;
; Call init functions of the used modules. Add your routine calls here. ; Call init functions of the used modules. Add your routine calls here.
;
; IN:
; - nothing
; OUT:
; - nothing
; USED: depending on called routines
initModules: initModules:
; rcall Utils_Init
rcall BaseTimer_Init ; unconditionally call this rcall BaseTimer_Init ; unconditionally call this
#ifdef MODULES_CLOCK
rcall Clock_Init
#endif
#ifdef MODULES_TIMER #ifdef MODULES_TIMER
rcall Timer_Init rcall Timer_Init
#endif #endif
@@ -113,6 +105,19 @@ initModules:
rcall Com2_Init ; init COM module rcall Com2_Init ; init COM module
rcall CPRO_Init ; init COM protocol module rcall CPRO_Init ; init COM protocol module
#endif #endif
#ifdef MODULES_NETWORK
rcall NET_Init
#endif
#ifdef MODULES_UART_BITBANG
rcall UART_BitBang_Init
#endif
#ifdef MODULES_MOTION
rcall Motion_Init
#endif
#ifdef MODULES_TWI_MASTER #ifdef MODULES_TWI_MASTER
rcall TWI_Master_Init rcall TWI_Master_Init
#endif #endif
@@ -151,10 +156,6 @@ initModules:
rcall SK6812_Init rcall SK6812_Init
#endif #endif
#ifdef MODULES_MOTION
rcall Motion_Init
#endif
#ifdef MODULES_MOTION_LIGHT #ifdef MODULES_MOTION_LIGHT
rcall MotionLight_Init rcall MotionLight_Init
#endif #endif
@@ -169,6 +170,7 @@ initModules:
; done ; done
ret ret
; @end
@@ -216,11 +218,6 @@ runModules_ComEnd:
rcall Ds18b20_Run rcall Ds18b20_Run
#endif #endif
#ifdef MODULES_MOTION
rcall Motion_Run
#endif
#ifdef MODULES_MOTION_LIGHT #ifdef MODULES_MOTION_LIGHT
; rcall MotionLight_Run ; rcall MotionLight_Run
#endif #endif
@@ -235,36 +232,29 @@ runModules_ComEnd:
#ifdef MODULES_NETWORK
; --------------------------------------------------------------------------- ; ---------------------------------------------------------------------------
; initialWait ; @routine handleMessages
;
; Initial wait to desync nodes.
;
; This routine waits for between 10 to 2560 milliseconds (derived from UID)
;
; IN:
; - nothing
; OUT:
; - nothing
; USED: r16, r17, r18, r19, r20, r21, r22, X
initialWait: handleMessages:
; setup initial wait loop rcall NET_GetNextIncomingMsgNum ; R16=msg num
rcall Utils_ReadUid ; (R16, X) brcc handleMessages_end
clr r16 rcall NET_Buffer_Locate ; X=buffer addr (R17)
eor r16, r18 push r16
eor r16, r19 ; handle messages
eor r16, r20 ldi yl, LOW(uart_bitbang_iface)
eor r16, r21 ldi yh, HIGH(uart_bitbang_iface)
rcall AppNetwork_HandleMsg
initialWait_l1: ; wait R16 x 10 milliseconds ; add more here
ldi r17, 200 pop r16
initialWait_l2: ; wait for 10ms rcall NET_Buffer_ReleaseByNum
rcall Utils_WaitFor50MicroSecs ; (R22) sec
dec r17 handleMessages_end:
brne initialWait_l2
dec r16
brne initialWait_l1
ret ret
; @end
#endif