; ***************************************************************************
; 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 base system node on AtTiny 84
;
; This is for the maintenance system (i.e. the flash loader).
;
; All definitions and changes should go into this file.
; ***************************************************************************

.equ clock=1000000 ; Define the clock frequency

.nolist
.include "include/tn861Adef.inc" ; Define device ATtiny861A
.list

.include "common/calls.asm"
.include "common/utils_io.asm"
.include "devices/all/io_1.asm"

.include "version.asm"
.include "../defs.asm"

.include "devices/all/defs.asm"


#define COM_ACCEPT_ALL_DEST



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

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

.include "common/utils_wait.asm"



; ---------------------------------------------------------------------------
; firmware settings



; ---------------------------------------------------------------------------
; LED

.equ LED_DDR             = DDRA
.equ LED_PORT            = PORTA
.equ LED_PIN             = PINA
.equ LED_PINNUM          = PORTA3


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

.cseg
.org 0x0000



; ---------------------------------------------------------------------------
; Reset and interrupt vectors
  rjmp main ; 0x00: Reset vector  Reset Vector
  reti      ; 0x01: INT0          External Interrupt 0
  reti      ; 0x02: PCI           Pin Change Interrupt
  reti      ; 0x03: OC1A          Timer/Counter1 Compare Match 1A
  reti      ; 0x04: OC1B          Timer/Counter1 Compare Match 1B
  reti      ; 0x05: OVF1          Timer/Counter1 Overflow
  reti      ; 0x06: OVF0          Timer/Counter0 Overflow
  reti      ; 0x07: USI_START     USI Start
  reti      ; 0x08: USI_OVF       USI Overflow
  reti      ; 0x09: ERDY          EEPROM Ready
  reti      ; 0x0a: ACI           Analog Comparator
  reti      ; 0x0b: ADCC          ADC Conversion Complete
  reti      ; 0x0c: WDT           Watchdog Time-Out
  reti      ; 0x0d: INT1          External Interrupt 1
  reti      ; 0x0e: OC0A          Timer/Counter0 Compare Match A
  reti      ; 0x0f: OC0B          Timer/Counter0 Compare Match B
  reti      ; 0x10: ICP0          Interrupt Capture Event for Timer/Counter0
  reti      ; 0x11: OC1D          Timer/Counter1 Compare Match D
  reti      ; 0x12: FAULT_PROT    Timer/Counter1 Fault Protection

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_BOOT, FIRMWARE_VERSION_MAJOR
                     .db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL

firmwareStart:   rjmp main ; will be overwritten when flashing



; ***************************************************************************
; main code


.org BOOTLOADER_ADDR


main:
  rjmp bootLoader ; this routine is in modules/bootloader/main.asm



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

.include "common/utils_wait_fixed.asm"
.include "common/utils_copy_from_flash.asm"
.include "common/utils_copy_sdram.asm"

.include "modules/flash/defs.asm"
.include "modules/flash/eeprom.asm"
.include "modules/flash/io.asm"
.include "modules/flash/io_com2w.asm"
.include "modules/flash/flash1p.asm"
.include "modules/flash/flashxp.asm"
.include "modules/flash/flashprocess.asm"
.include "modules/flash/wait.asm"
.include "modules/bootloader/main.asm"
.include "modules/network/msg/defs.asm"
.include "modules/network/msg/crc.asm"

;.include "common/debug.asm"



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



