Files
aqhomecontrol/avr/main.asm
2024-09-29 21:03:12 +02:00

244 lines
4.5 KiB
NASM

; ***************************************************************************
; copyright : (C) 2024 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. *
; ***************************************************************************
; ***************************************************************************
; code
.cseg
; ---------------------------------------------------------------------------
; main
;
main:
cli
; setup stack
.ifdef SPH ; if SPH is defined
ldi r16, High(RAMEND)
out SPH, r16 ; init MSB stack pointer
.endif
ldi r16, Low(RAMEND)
out SPL, r16 ; init LSB stack pointer
; rcall watchdogOff ; turn off watchdog timer (sometimes it stays on after reboot)
rcall systemSetSpeed
rcall initModules
rcall Utils_SetupUid
rcall initialWait
sei ; Enable interrupts
rcall onSystemStart
#ifdef MODULES_LED
ldi xl, LOW(blinkPattern) ; debug: set blink pattern
ldi xh, HIGH(blinkPattern)
ldi zl, LOW(ledA3Flash)
ldi zh, HIGH(ledA3Flash)
ldi yl, LOW(ledA3Sram)
ldi yh, HIGH(ledA3Sram)
rcall Led_SetPattern
#endif
; sbi DDRA, PORTA2 ; debug
; sbi PINA, PORTA2 ; debug (toggle)
; cbi PORTA, PORTA2 ; debug (on)
; sbi PORTA, PORTA2 ; debug (off)
; ldi r16, 1
; sts twiMasterScanEnabled, r16
main_loop:
rcall runModules
; only modify SE, SM1 and SM0
cli
in r16, MCUCR
ldi r17, (1<<SE) | (1<<SM1) | (1<<SM0)
neg r17
and r16, r17
ori r16, (1<<SE) ; sleep mode "idle", enable
out MCUCR, r16
sei ; make sure interrupts really are enabled
sleep ; sleep, wait for interrupt
rjmp main_loop
; ---------------------------------------------------------------------------
; initModules
;
; Call init functions of the used modules. Add your routine calls here.
;
; IN:
; - nothing
; OUT:
; - nothing
; USED: depending on called routines
initModules:
rcall Utils_Init
rcall BaseTimer_Init ; unconditionally call this
#ifdef MODULES_TIMER
rcall Timer_Init
#endif
#ifdef MODULES_LED
ldi zl, LOW(ledA3Flash)
ldi zh, HIGH(ledA3Flash)
ldi yl, LOW(ledA3Sram)
ldi yh, HIGH(ledA3Sram)
rcall Led_Init
#endif
#ifdef MODULES_LED_SIMPLE
rcall LedSimple_Init
#endif
#ifdef MODULES_COM
rcall Com2_Init ; init COM module
rcall CPRO_Init ; init COM protocol module
#endif
#ifdef MODULES_TWI_MASTER
rcall TWI_Master_Init
#endif
#ifdef MODULES_OWI_MASTER
rcall OwiMaster_Init
#endif
#ifdef MODULES_LCD
rcall LCD_Init
#endif
#ifdef MODULES_BMP280
rcall BMP280_Init
#endif
#ifdef MODULES_SI7021
rcall SI7021_Init
#endif
#ifdef MODULES_DS18B20
rcall Ds18b20_Init
#endif
#ifdef MODULES_STATS
rcall Stats_Init
#endif
#ifdef MODULES_CNY70
rcall CNY70_Init
#endif
#ifdef MODULES_REED
rcall REED_Init
#endif
#ifdef MODULES_SK6812
rcall SK6812_Init
#endif
; done
ret
; ---------------------------------------------------------------------------
; runModules
;
; Call run functions of the used modules. Add your routine calls here.
;
; IN:
; - nothing
; OUT:
; - nothing
; USED: depending on called routines
runModules:
rcall BaseTimer_Run
#ifdef MODULES_COM
; COM module (call until carry flag cleared but at most 10 times to not starve other modules)
ldi r16, 10
runModules_Com:
push r16
rcall Com2_Run
pop r16
brcc runModules_ComEnd
dec r16
brne runModules_Com
runModules_ComEnd:
#endif
#ifdef MODULES_STATS
rcall Stats_Run
#endif
#ifdef MODULES_REED
rcall REED_Run
#endif
#ifdef MODULES_CNY70
rcall CNY70_Run
#endif
#ifdef MODULES_DS18B20
rcall Ds18b20_Run
#endif
; add more modules here
ret
; ---------------------------------------------------------------------------
; initialWait
;
; Initial wait to desync nodes.
;
; This routine waits for between 10 to 2560 milliseconds (derived from UID)
;
; IN:
; - nothing
; OUT:
; - nothing
; USED: r16, r17, r18, r19, r20, r21, r22, X
initialWait:
; setup initial wait loop
rcall Utils_ReadUid ; (R16, X)
clr r16
eor r16, r18
eor r16, r19
eor r16, r20
eor r16, r21
initialWait_l1: ; wait R16 x 10 milliseconds
ldi r17, 200
initialWait_l2: ; wait for 10ms
rcall Utils_WaitFor50MicroSecs ; (R22)
dec r17
brne initialWait_l2
dec r16
brne initialWait_l1
ret