; ***************************************************************************
; 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 LED controller node on AtTiny 84
;
; This is for the full system (i.e. not the boot loader).
; ***************************************************************************

.equ clock=8000000 ; Define the clock frequency



.nolist
.include "include/tn84def.inc" ; Define device ATtiny84
.list

.include "version.asm"
.include "../defs.asm"
.include "../eeprom.asm"
;.include "./data.asm"

.include "devices/all/defs.asm"

.include "common/calls.asm"
.include "common/utils_io.asm"
.include "common/utils_wait.asm"



; ***************************************************************************
; defines

; ---------------------------------------------------------------------------
; generic

.equ NET_BUFFERS_NUM            =  6
.equ NET_MSGNUMINBUF_SIZE       =  8 ; max buffer nums in ringbuffer (global incoming)
.equ NET_IFACE_OUTMSGBUF_SIZE   =  8 ; max buffer nums in ringbuffer (per interface outbound)



; ---------------------------------------------------------------------------
; firmware settings including list of modules used


#define MODULES_CLOCK
#define MODULES_LED1
#define MODULES_NETWORK
#define MODULES_COM2W
#define MODULES_OWI_MASTER
#define MODULES_DS18B20
#define MODULES_SK6812

#define APPS_NETWORK
#define APPS_REPORTSENSORS
#define APPS_STATS
#define APPS_LIGHTCONTROL



; ---------------------------------------------------------------------------
; defines for modules

.equ VALUE_ID_DS18B20_TEMP          = 0x06

.equ VALUE_ID_LIGHTCONTROL_STATE    = 0x07
.equ VALUE_ID_LIGHTCTRL_NUMLEDS     = 0x82
.equ VALUE_ID_LIGHTCTRL_VALUE       = 0x84
.equ VALUE_ID_LIGHTCTRL_HOLDTIME    = 0x85
.equ VALUE_ID_LIGHTCTRL_TRIGGER1SRC = 0x86
.equ VALUE_ID_LIGHTCTRL_TRIGGER2SRC = 0x87
.equ VALUE_ID_LIGHTCTRL_GUARDSRC    = 0x89
.equ VALUE_ID_LIGHTCTRL_MODE        = 0x8b
.equ VALUE_ID_LIGHTCTRL_GUARDLIM    = 0x8c
.equ VALUE_ID_LIGHTCTRL_TRIGGER1LIM = 0x8d
.equ VALUE_ID_LIGHTCTRL_TRIGGER2LIM = 0x8e



; ***************************************************************************
; code segment

.cseg
.org 000000



; ---------------------------------------------------------------------------
; Reset and interrupt vectors

  rjmp BOOTLOADER_ADDR        ;  1: Reset vector  ; use this for flashed system
  reti                        ;  2: EXT_INT0
  rjmp com2wPcintIsr          ;  3: PCI0
  reti                        ;  4: PCI1
  reti                        ;  5: WDT
  reti                        ;  6: TIM1_CAPT
  reti                        ;  7: TIM1_COMPA
  reti                        ;  8: TIM1_COMPB
  reti                        ;  9: TIM1_OVF
  rjmp baseTimerIrqOC0A       ; 10: TIM0_COMPA
  reti                        ; 11: TIM0_COMPB
  reti                        ; 12: TIM0_OVF
  reti                        ; 13: ANA_COMP
  reti                        ; 14: ADC
  reti                        ; 15: EE_RDY
  reti                        ; 16: USI_STR
  reti                        ; 17: 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_LEDSTRIPS, FIRMWARE_VERSION_MAJOR
                     .db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL

; ---------------------------------------------------------------------------
; @routine firmwareStart @global

firmwareStart:
  rjmp main
; @end



onSystemStart:
onMessageReceived:
onEvery100ms:
onEverySecond:
onEveryMinute:
onEveryHour:
onEveryDay:
onEveryLoop:
  clc
  ret



; ***************************************************************************
; includes

.include "devices/all/hw_tn84.asm"
.include "devices/all/includes.asm"
.include "common/eeprom_tlv.asm"
;.include "common/debug.asm"



; ---------------------------------------------------------------------------
; defines for network interface

.equ netInterfaceData = com2w_iface



deviceCodeEnd:
  .if deviceCodeEnd >= BOOTLOADER_ADDR
    .warning "Code reaches into boot loader!"
  .endif


