; ***************************************************************************
; copyright   : (C) 2023 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.
;
;
;                                 AtTiny84
;                                 --------
;                            VCC  1     14  GND
;                            PB0  2     13  PA0
;                            PB1  3     12  PA1  COM-DATA
;                  /RESET    PB3  4     11  PA2  [OWI]
;                  [KEY1]    PB2  5     10  PA3  LED
;                  COM_ATTN  PA7  6      9  PA4  TWI-SCL
;                  TWI-SDA   PA6  7      8  PA5
;                                 --------
;
; ***************************************************************************



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

.include "defs.asm"



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

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

.equ clock=1000000 ; Define the clock frequency



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

#define FW_TYPE AQHOME_FW_TYPE_ATT84_TEMP1
#define FW_MAIN_VERSION_HIGH 0
#define FW_MAIN_VERSION_LOW  1


#define MODULES_TIMER
#define MODULES_COM
#define MODULES_LED
#define MODULES_TWI_MASTER
;#define MODULES_LCD
#define MODULES_SI7021



.set MODULES_MASK = 0
#ifdef MODULES_TIMER
.set MODULES_MASK = MODULES_MASK | (1<<AQHOME_FW_MODULE_TIMER)
#endif
#ifdef MODULES_COM
.set MODULES_MASK = MODULES_MASK | (1<<AQHOME_FW_MODULE_COM)
#endif
#ifdef MODULES_LED
.set MODULES_MASK = MODULES_MASK | (1<<AQHOME_FW_MODULE_LED)
#endif
#ifdef MODULES_TWI_MASTER
.set MODULES_MASK = MODULES_MASK | (1<<AQHOME_FW_MODULE_TWIMASTER)
#endif
#ifdef MODULES_LCD
.set MODULES_MASK = MODULES_MASK | (1<<AQHOME_FW_MODULE_LCD)
#endif
#ifdef MODULES_SI7021
.set MODULES_MASK = MODULES_MASK | (1<<AQHOME_FW_MODULE_SI7021)
#endif



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

#define LCD_MINIMAL_FONT


.equ VALUE_ID_TEMP1 = 0x01
.equ VALUE_ID_HUM1  = 0x02



; ---------------------------------------------------------------------------
; COM module

.equ COM_BIT_LENGTH      = 52000 ; 104000=9600, 52000=19200, 26000=38400

.equ COM_DDR_DATA        = DDRA
.equ COM_PORT_DATA       = PORTA
.equ COM_PIN_DATA        = PINA
.equ COM_PINNUM_DATA     = PORTA1

.equ COM_DDR_ATTN        = DDRA
.equ COM_PORT_ATTN       = PORTA
.equ COM_PIN_ATTN        = PINA
.equ COM_PINNUM_ATTN     = PORTA7

.equ COM_IRQ_ADDR_ATTN   = PCMSK0
.equ COM_IRQ_BIT_ATTN    = 7              ; bit 7 in PCMSK0
.equ COM_IRQ_GIFR_ATTN   = PCIF0
.equ COM_IRQ_GIMSK_ATTN  = PCIE0



; ---------------------------------------------------------------------------
; TWI master module

.equ TWI_DDR_SCL          = DDRA
.equ TWI_PORT_SCL         = PORTA
.equ TWI_PIN_SCL          = PINA
.equ TWI_PINNUM_SCL       = PORTA4

.equ TWI_DDR_SDA          = DDRA
.equ TWI_PORT_SDA         = PORTA
.equ TWI_PIN_SDA          = PINA
.equ TWI_PINNUM_SDA       = PORTA6



; ---------------------------------------------------------------------------
; LCD module

.equ LCD_TWI_ADDRESS      = 0x3c



; ---------------------------------------------------------------------------
; BMP 280

.equ BMP280_ADDR          = 0x76



; ---------------------------------------------------------------------------
; SI 7021

.equ SI7021_ADDR          = 0x40





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

.cseg
.org 000000



; ---------------------------------------------------------------------------
; Reset and interrupt vectors (will be removed as soon as we can flash data over COM)

	rjmp PC+0x500 ; Reset vector
	rjmp PC+0x500 ; EXT_INT0
	rjmp PC+0x500 ; PCI0
	rjmp PC+0x500 ; PCI1
	rjmp PC+0x500 ; WATCHDOG
        rjmp PC+0x500 ; ICP1
        rjmp PC+0x500 ; OC1A
	rjmp PC+0x500 ; OC1B
	rjmp PC+0x500 ; OVF1
        rjmp PC+0x500 ; OC0A
	rjmp PC+0x500 ; OC0B
	rjmp PC+0x500 ; OVF0
	rjmp PC+0x500 ; ACI
	rjmp PC+0x500 ; ADCC
	rjmp PC+0x500 ; ERDY
	rjmp PC+0x500 ; USI_STR
	rjmp PC+0x500 ; USI_OVF


; ---------------------------------------------------------------------------
; working system starts here.

.org 0x0500


	rjmp main ; Reset vector
	reti ; EXT_INT0
	rjmp comIsrPcint0 ; PCI0
	reti ; PCI1
	reti ; WATCHDOG
	reti ; ICP1
	reti ; OC1A
	reti ; OC1B
	reti ; OVF1
        rjmp timerIrqOC0A ; OC0A
	reti ; OC0B
	reti ; OVF0
	reti ; ACI
	reti ; ADCC
	reti ; ERDY
	reti ; USI_STR
	reti ; USI_OVF


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

.include "utils.asm"
.include "timer.asm"
.include "led.asm"
.include "com.asm"
.include "comproto.asm"
.include "comproto_addr.asm"
.include "comproto_stats.asm"
.include "comproto_device.asm"
.include "comproto_values.asm"
;.include "comproto_debug.asm"
;.include "comproto_twi.asm"
.include "twimaster.asm"
;.include "lcd.asm"
.include "si7021.asm"



; ***************************************************************************
; data in SRAM

.dseg




; ***************************************************************************
; data in FLASH

.cseg

ledA3Flash:    .db DDRA+0x20, PORTA+0x20, PINA+0x20, (1<<PORTA3)
blinkPattern:  .db 2, 50, 0xff, 0xff   ; 1 short blink, 2s pause, restart
;blinkPattern2: .db 10, 20, 0xff, 0xff   ; 1 long blink, 2s pause, restart



.include "main.asm"


; ---------------------------------------------------------------------------
; Called on first time run, i.e. on system start. No arguments, no results.

onSystemStart:

#ifdef MODULES_COM
  rcall CPRO_StartReclaimAddrProcedure
#endif

  ret



; ---------------------------------------------------------------------------
; Called every 100ms. Add your routine calls here. No arguments, no results.

onEvery100ms:
#ifdef MODULES_LED
  ; ticker for LED module
  ldi zl, LOW(ledA3Flash)
  ldi zh, HIGH(ledA3Flash)
  ldi yl, LOW(ledA3Sram)
  ldi yh, HIGH(ledA3Sram)
  rcall Led_Tick
#endif
  ret



; ---------------------------------------------------------------------------
; Called every second. Add your routine calls here. No arguments, no results.

onEverySecond:
;  rcall TWI_Master_ScanNext
  rcall CPRO_OnEverySecond
  ret



; ---------------------------------------------------------------------------
; Called every 5 seconds. Add your routine calls here.  No arguments, no results.

onEvery5s:
  ret



; ---------------------------------------------------------------------------
; Called every 10 seconds. Add your routine calls here.  No arguments, no results.

onEvery10s:
#ifdef MODULES_LCD
  rcall printSendStats
#endif
  ret



; ---------------------------------------------------------------------------
; Called every 30 seconds. Add your routine calls here. No arguments, no results.

onEvery30s:
#ifdef MODULES_SI7021
  in r15, SREG
  push r15
    cli
    rcall SI7021_PeriodicMeasurement
    rcall Main_SendValueMsg
  pop r15
  out SREG, r15
#endif
  ret



; ---------------------------------------------------------------------------
; Called every minute. Add your routine calls here. No arguments, no results.

onEvery1m:
#ifdef MODULES_COM
    lds r16, comAddress               ; do we have an address assigned?
    tst r16
    breq onEvery1m_l1                 ; no, do nothing

    ; will later send this only every hour or so
    ldi r16, 0xff                     ; send device info to everybody
    rcall CPRO_EnqueueDevice
onEvery1m_l1:
#endif

  ret


; ---------------------------------------------------------------------------
; Called every 5 minutes. Add your routine calls here. No arguments, no results.

onEvery5m:

#ifdef MODULES_COM
    ; will later send these only every hour or so
    ldi r16, 0xff                     ; send stats to everybody
    rcall CPRO_EnqueueComSendStats

    ldi r16, 0xff                     ; send recv to everybody
    rcall CPRO_EnqueueComRecvStats
#endif

  ret



; ---------------------------------------------------------------------------
; Called every 15 minutes. Add your routine calls here. No arguments, no results.

onEvery15m:
  ret



; ---------------------------------------------------------------------------
; Called every 30 minutes. Add your routine calls here. No arguments, no results.

onEvery30m:
  ret



; ---------------------------------------------------------------------------
; Called every hour. Add your routine calls here. No arguments, no results.

onEvery1h:
  ret



; ---------------------------------------------------------------------------
; Called every 12 hours. Add your routine calls here. No arguments, no results.

onEvery12h:
  ret



; ---------------------------------------------------------------------------
; Called every day. Add your routine calls here. No arguments, no results.

onEvery1d:
  ret



; ---------------------------------------------------------------------------
; onPacketReceived:
;
; Called after a packet was received via COM module. Add your routine calls here.
;
; The packet will be removed from buffer in any case after return from this call.
; IN:
; - Y  : pointer to received buffer
; OUT:
; - CFLAG: set if handled, cleared otherwise
; USED: depending on called routines

onPacketReceived:
  rcall CPRO_OnPacketReceived
  ret



