Files
aqhomecontrol/avr/devices/n23/main/main.asm
2025-04-26 11:04:01 +02:00

455 lines
11 KiB
NASM

; ***************************************************************************
; 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 "./data.asm"
.include "defs_all.asm"
.include "common/utils_wait.asm"
; ***************************************************************************
; defines
; ---------------------------------------------------------------------------
; generic
.equ NET_BUFFERS_NUM = 6
.equ NET_BUFFERS_SIZE = 28
.equ PROGRAM_TIMER_VALUE = 60
; ---------------------------------------------------------------------------
; 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 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
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
ldi r16, PROGRAM_TIMER_VALUE
sts programRamSensorTimer, r16
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 ; let basetimer run
rcall handleMessages ; handle incoming messages
; rcall ComOnUart0_Run
rjmp main_loop
; ---------------------------------------------------------------------------
; @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
; ---------------------------------------------------------------------------
; @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
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 initModules
;
initModules:
; init drivers
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
; @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
; ---------------------------------------------------------------------------
; @routine onEverySecond
onEverySecond:
lds r16, programRamSensorTimer
dec r16
brne onEverySecond_store
ldi r16, PROGRAM_TIMER_VALUE
onEverySecond_store:
sts programRamSensorTimer, r16
cpi r16, 59
breq onEverySecond_measureValue1
cpi r16, 29
breq onEverySecond_measureValue2
cpi r16, 19
breq onEverySecond_sendValue1
cpi r16, 9
breq onEverySecond_sendValue2
ret
onEverySecond_measureValue1:
rjmp SI7021_MeasureTemp
onEverySecond_measureValue2:
rjmp SI7021_MeasureHumidity
onEverySecond_sendValue1:
rjmp sendTemperature
onEverySecond_sendValue2:
rjmp sendHumidity
; @end
onEveryMinute:
onEveryHour:
onEveryDay:
ret
; ---------------------------------------------------------------------------
; @routine handleMessages
handleMessages:
rcall NET_GetNextIncomingMsgNum ; R16=msg num
brcc handleMessages_end
rcall NET_Buffer_Locate ; X=buffer addr (R17)
push r16
; handle messages
ldi yl, LOW(uart_bitbang_iface)
ldi yh, HIGH(uart_bitbang_iface)
rcall AppNetwork_HandleMsg
; add more here
pop r16
rcall NET_Buffer_ReleaseByNum
sec
handleMessages_end:
ret
; @end
; ---------------------------------------------------------------------------
; @routine sendTemperature
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
.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 "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"
; ---------------------------------------------------------------------------
; defines for network interface
.equ netInterfaceData = uart_bitbang_iface