Files
aqhomecontrol/avr/timer.asm
2023-02-20 23:47:56 +01:00

370 lines
9.0 KiB
NASM

; ***************************************************************************
; defs
.equ TIMER_FLAGS_100MS = 1
.equ TIMER_FLAGS_1S = 2
.equ TIMER_CALLBACK_10S = 0x01
.equ TIMER_CALLBACK_30S = 0x02
.equ TIMER_CALLBACK_1M = 0x04
.equ TIMER_CALLBACK_15M = 0x08
.equ TIMER_CALLBACK_30M = 0x10
.equ TIMER_CALLBACK_1H = 0x20
.equ TIMER_CALLBACK_12H = 0x40
.equ TIMER_CALLBACK_1D = 0x80
; ***************************************************************************
; data
.dseg
timerModuleData:
timerModuleTickCounter: .byte 1 ; only low byte used
timerModuleFlags: .byte 1
timerModuleCounterSecs: .byte 4
timerModuleDerivedCounters: ; order of the following vars matters!!
timerModuleCounterFor10s: .byte 1 ; change Timer_IncrementCounters when changing the
timerModuleCounterFor1m: .byte 1 ; order here!
timerModuleCounterFor15m: .byte 1
timerModuleCounterFor30m: .byte 1
timerModuleCounterFor1h: .byte 1
timerModuleCounterFor12h: .byte 1
timerModuleCounterFor1d: .byte 1
timerModuleData_end:
; ***************************************************************************
; code
.cseg
; ***************************************************************************
; Timer_Init
;
; IN:
; - nothing
; OUT:
; - nothing
; USED: r16, r17, x
Timer_Init: ; setup timer for 15.2588 interrupts per second (e.g. every 65.5 ms)
; reset data in SDRAM
ldi xh, HIGH(timerModuleData)
ldi xl, LOW(timerModuleData)
ldi r16, 0
ldi r17, (timerModuleData_end-timerModuleData)
rcall Utils_FillSram
; CTC mode (WGM2:0=2, OCR0A=value, OCF0A Flag =1, -> IRQ_OC0A
; CMP-A interrupt about every 100ms
ldi r16, (1<<CS02) | (1<<CS00) ; Prescaler 1024
out TCCR0B, r16
ldi r16, (1<<WGM01) ; CTC mode
out TCCR0A, r16
ldi r16, 98-1 ; (1,000,000/1024)/10 = 97.65625
out OCR0A, r16
ldi r16, (1<<OCF0A) ; clear pending interrupts
out TIFR0, r16
ldi r16, (1<<OCIE0A)
out TIMSK0, r16
ret
; ---------------------------------------------------------------------------
; Timer_Fini
;
; IN:
; - nothing
; OUT:
; - nothing
; USED:
Timer_Fini:
ret
; ---------------------------------------------------------------------------
; Timer_Run
;
; IN:
; - nothing
; OUT:
; - CFLAG: set if something done, cleared otherwise
; USED: r15, r16, r17, R19 (more depending on called routines)
Timer_Run:
in r15, SREG
cli
lds r17, timerModuleFlags ; get timer flags
clr r16 ; replace with 0 for next IRQ
sts timerModuleFlags, r16
mov r16, r17
andi r16, TIMER_FLAGS_100MS ; timer irq occurred?
brne Timer_Run_100ms_passed ; yes, go handle
out SREG, r15 ; restore global IRQ flag
clc ; nothing done
ret
Timer_Run_100ms_passed:
mov r16, r17
andi r16, TIMER_FLAGS_1S
brne Timer_Run_1s_passed
out SREG, r15 ; restore global IRQ flag
rcall onEvery100ms ; only call this one
sec
ret
Timer_Run_1s_passed:
clr r19 ; flags for additional callbacks to be called
rcall Timer_IncrementCounters ; 1s passed, handle depending counters, sample callback flags
out SREG, r15 ; restore global IRQ flag
mov r16, r19
rcall Timer_CallOnEveryHandlers
sec
ret
; ---------------------------------------------------------------------------
; Call "onEvery*" handlers according to given flags.
;
; IN:
; - R16: flags
; OUT:
; - nothing
; REGS: depending on called routines, expect clobbering of all registers
Timer_CallOnEveryHandlers:
push r16
rcall onEvery100ms ; always call this one
rcall onEverySecond ; and this
pop r16
tst r16 ; any flag set?
breq Timer_CallOnEveryHandlers_done ; nope, nothing more to call
mov r17, r16
andi r17, TIMER_CALLBACK_10S
breq Timer_CallOnEveryHandlers_l1
push r16
rcall onEvery10s
pop r16
Timer_CallOnEveryHandlers_l1:
mov r17, r16
andi r17, TIMER_CALLBACK_30S
breq Timer_CallOnEveryHandlers_l2
push r16
rcall onEvery30s
pop r16
Timer_CallOnEveryHandlers_l2:
mov r17, r16
andi r17, TIMER_CALLBACK_1M
breq Timer_CallOnEveryHandlers_l3
push r16
rcall onEveryMinute
pop r16
Timer_CallOnEveryHandlers_l3:
mov r17, r16
andi r17, TIMER_CALLBACK_15M
breq Timer_CallOnEveryHandlers_l4
push r16
rcall onEvery15m
pop r16
Timer_CallOnEveryHandlers_l4:
mov r17, r16
andi r17, TIMER_CALLBACK_30M
breq Timer_CallOnEveryHandlers_l5
push r16
rcall onEvery30m
pop r16
Timer_CallOnEveryHandlers_l5:
mov r17, r16
andi r17, TIMER_CALLBACK_1H
breq Timer_CallOnEveryHandlers_l6
push r16
rcall onEvery1h
pop r16
Timer_CallOnEveryHandlers_l6:
mov r17, r16
andi r17, TIMER_CALLBACK_12H
breq Timer_CallOnEveryHandlers_l7
push r16
rcall onEvery12h
pop r16
Timer_CallOnEveryHandlers_l7:
mov r17, r16
andi r17, TIMER_CALLBACK_1D
breq Timer_CallOnEveryHandlers_done
push r16
rcall onEvery1d
pop r16
Timer_CallOnEveryHandlers_done:
ret
; ---------------------------------------------------------------------------
; Increments the list of dependent counters and sets flag in R19 accordingly.
;
; IN:
; - R19: flags to add to
; OUT:
; - R19: flags for callback handlers to be called
; REGS: R16, R19, X
Timer_IncrementCounters:
ldi xl, LOW(timerModuleDerivedCounters) ; points to first counter: timerModuleCounterFor10s
ldi xh, HIGH(timerModuleDerivedCounters)
clr r19
ldi r17, 10 ; overflow after 10s
ldi r18, TIMER_CALLBACK_10S
rcall timerInc8CheckVar
brcc Timer_IncrementCounters_done
ldi r17, 3 ; overflow after 3*10s
ldi r18, TIMER_CALLBACK_30S
rcall timerInc8CheckVar
brcc Timer_IncrementCounters_done
ldi r17, 2 ; overflow after 2*30s
ldi r18, TIMER_CALLBACK_1M
rcall timerInc8CheckVar
brcc Timer_IncrementCounters_done
ldi r17, 30 ; overflow after 30*1m
ldi r18, TIMER_CALLBACK_30M
rcall timerInc8CheckVar
brcc Timer_IncrementCounters_done
ldi r17, 2 ; overflow after 2*30m
ldi r18, TIMER_CALLBACK_1H
rcall timerInc8CheckVar
brcc Timer_IncrementCounters_done
ldi r17, 12 ; overflow after 12*1h
ldi r18, TIMER_CALLBACK_12H
rcall timerInc8CheckVar
brcc Timer_IncrementCounters_done
ldi r17, 2 ; overflow after 2*12h
ldi r18, TIMER_CALLBACK_1D
rcall timerInc8CheckVar
brcc Timer_IncrementCounters_done
Timer_IncrementCounters_done:
ret
; ---------------------------------------------------------------------------
; Increment an 8 bit counter and check result.
;
; If the incremented counter is equal or greater than the given value the counter
; is reset, the given flag is ORed to the flags and carry flag is set on return.
; Otherwise the counter is just incremented and carry flag cleared.
;
; IN:
; - X: ptr to counter
; - R17: value to compare against
; - R18: flag to add on overflow
; - R19: flags to add to
; OUT:
; - CFLAG set if overflow occurred, cleared otherwise
; - X: points to next counter (advanced by one byte)
; REGS: R16, R19, X
timerInc8CheckVar:
ld r16, X
inc r16
cp r16, r17
brcs timerInc8CheckVar_stillSmaller
clr r16
st X+, r16
or r19, r18
sec
ret
timerInc8CheckVar_stillSmaller:
st X+, r16
clc
ret
; ---------------------------------------------------------------------------
; OC0A interrupt handler
;
; Called every 100 milliseconds, sets flag TIMER_FLAGS_100MS.
; Increments an internal counter. If that counter reaches 10 the seconds counter is incremented and a the flag
; TIMER_FLAGS_1S is set.
timerIrqOC0A:
push r15
in r15, SREG
push r16
push r17
lds r17, timerModuleFlags
ori r17, TIMER_FLAGS_100MS
lds r16, timerModuleTickCounter
inc r16
cpi r16, 10
brcc timerIrq2_l1
sts timerModuleTickCounter, r16
rjmp timerIrq2_l2
timerIrq2_l1: ; increment seconds
; reset 100ms counter
clr r16
sts timerModuleTickCounter, r16
; increment seconds
push xh
push xl
push r18
ldi xh, HIGH(timerModuleCounterSecs)
ldi xl, LOW(timerModuleCounterSecs)
ldi r18, 1
ld r16, X ; byte 0
adc r16, r18 ; inc by 1
dec r18 ; doesn't affect carry flag! (r18 becomes 0 for following ADC)
st X+, r16
ld r16, X ; byte 1
adc r16, r18
st X+, r16
ld r16, X ; byte 2
adc r16, r18
st X+, r16
ld r16, X ; byte 3
adc r16, r18
st X+, r16
pop r18
pop xl
pop xh
ori r17, TIMER_FLAGS_1S
timerIrq2_l2:
sts timerModuleFlags, r17 ; store new timer flags
pop r17
pop r16
out SREG, r15
pop r15
reti