; ***************************************************************************
; 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.   *
; ***************************************************************************

; Hardware routine  for AtTiny 84 devices


.cseg


; ---------------------------------------------------------------------------
; @routine systemInitHardware
;

systemInitHardware:
  ; 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

.ifdef PORTD
  out DDRD, r17  ; all input
  out PORTD, r16 ; enable pull-up on all
.endif

.ifdef PORTE
  out DDRE, r17  ; all input
  out PORTE, r16 ; enable pull-up on all
.endif

  ret
; @end



; ---------------------------------------------------------------------------
; @routine systemSetSpeed
;

systemSetSpeed:
  ret
; @end



; ---------------------------------------------------------------------------
; @routine systemSleep
;

systemSleep:
  ; only modify SE, SM2, SM1 and SM0
  cli

  inr r16, MCUCR
  cbr r16, (1<<SE) | (1<<SM2) | (1<<SM1)
  outr MCUCR, r16

  inr r16, EMCUCR
  cbr r16, (1<<SM0)
  outr EMCUCR, r16

  sei                        ; make sure interrupts really are enabled

  inr r16, MCUCR       ; enable sleep mode
  sbr r16, (1<<SE)
  outr MCUCR, r16

  sleep                      ; sleep, wait for interrupt

  inr r16, MCUCR       ; disable sleep mode
  cbr r16, (1<<SE)
  outr MCUCR, r16

  ret
; @end





; ---------------------------------------------------------------------------
; @routine systemSetupTimer0
;

systemSetupTimer0:            ; setup timer for IRQ every 100ms
  ldi r16, (1<<CS02) | (0<<CS01) | (1<<CS00) | (1<<WGM01) | (0<<WGM00)  ; Prescaler 1024, CTC mode
  out TCCR0, r16

;
; Settings for clock 1Mhz (default)
; use timer0 with OCR0A=98-1 (irq every 97.65625 millisecs), baseTimerModuleReloadValue 1
;
.if clock == 1000000
  ; CMP-A interrupt about every 100ms
  ldi r16, 98-1      ; (1,000,000/1024)/10 = 97.65625
  out OCR0, r16

  ldi r16, 1
  sts baseTimerModuleReloadValue, r16
  sts baseTimerModuleTickCounter, r16
.endif

;
; Settings for clock 8Mhz
; use timer0 with OCR0=78 (irq every 9.984 millisecs), baseTimerModuleReloadValue 10
;
.if clock == 8000000
  ; CMP interrupt about every 10ms
  ldi r16, 78-1
  out OCR0, r16

  ldi r16, 10
  sts baseTimerModuleReloadValue, r16
  sts baseTimerModuleTickCounter, r16
.endif

  ldi r16, (1<<OCF0)              ; clear pending interrupts
.ifdef TIFR0
  out TIFR0, r16
.else
  out TIFR, r16
.endif
  
.ifdef TIMSK0
  inr r16, TIMSK0
  sbr r16, (1<<OCIE0)        ; Timer/Counter0 Output Compare Match A Interrupt Enable
  outr TIMSK0, r16
.else
  inr r16, TIMSK
  sbr r16, (1<<OCIE0)        ; Timer/Counter0 Output Compare Match A Interrupt Enable
  outr TIMSK, r16
.endif

  sec
  ret
; @end




