; ***************************************************************************
; copyright   : (C) 2026 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 "version.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_TIMER
#define MODULES_CLOCK
#define MODULES_LED1
#define MODULES_NETWORK
#define MODULES_COM2W
#define MODULES_SPI_SW
#define MODULES_RAM_23LC512
#define MODULES_EE_25LC256
;#define MODULES_TWI_MASTER
;#define MODULES_LCD
;#define LCD_MINIMAL_FONT
;#define MODULES_DS3231

#define APPS_NETWORK
#define APPS_STATS



; ---------------------------------------------------------------------------
; defines for values





; ***************************************************************************
; 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 com2wPcintIsr         ; 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:
  ; external SRAM check
  bigcall RAM_23LC512_WritePattern
  nop
  bigcall RAM_23LC512_ReadPattern
  brcs onSystemStart_done
  bigcall DEBUG1

onSystemStart_done:
  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:
onEverySecond:
onEveryMinute:
onEveryHour:
onEveryDay:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine onEveryLoop
;
; Called on every loop (i.e. after awakening from sleep).
;
onEveryLoop:
  ret
; @end





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

.include "devices/all/hw_tn84.asm"
.include "devices/all/includes.asm"

.include "common/debug.asm"
;.include "modules/network/msg/debug-w.asm"




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

.equ netInterfaceData = com2w_iface



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


