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



; ***************************************************************************
; code


.cseg



; ---------------------------------------------------------------------------
; @routine main @global

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)

  bigcall systemSetSpeed
  bigcall systemInitHardware
  bigcall Utils_Init
  bigcall Utils_SetupUid
  bigcall initModules
  bigcall initApps
  bigcall Utils_InitialWait
  sei ; Enable interrupts

  bigcall 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

main_loop:
    bigcall systemSleep   ; system-dependant

    ; run loop as long as some run function returns with CFLAG set
    clr r17
main_runLoop:
    push r17
      clr r16
      push r16
        bigcall runModules
      pop r16
      sbci r16, 0
      
      push r16
        bigcall runApps
      pop r16
      sbci r16, 0
    
      push r16
        bigcall onEveryLoop   ; call into main app
      pop r16
    pop r17

    tst r16
    breq main_endRunLoop
    inc r17
    cpi r17, 10
    brcc main_endRunLoop
  brne main_runLoop
main_endRunLoop:

#ifdef MODULES_NETWORK
  #ifndef MAIN_WITHOUT_MSG_HANDLING
    rcall mainHandleMessages
  #endif
#endif

  rjmp main_loop

main_loop_reboot:
  cli
  bigjmp BOOTLOADER_ADDR
; @end



; ---------------------------------------------------------------------------
; @routine onSystemTimerTick
;
; Called every 100ms. No arguments, no results.

onSystemTimerTick:
  bigcall onEvery100ms

#ifdef MODULES_CLOCK
  bigcall Clock_Every100ms           ; generates calls to onEverySecond/Minute/Hour/Day
#endif

#ifdef MODULES_LED_SIMPLE
  bigcall LedSimple_Every100ms
#endif

#ifdef MODULES_LED_SIGNAL
  bigcall LedSignal_Every100ms
#endif


#ifdef MODULES_LED_ACTIVITY
  bigcall LedActivity_Every100ms
#endif


#ifdef MODULES_UART_BITBANG
  bigcall UART_BitBang_Every100ms
#endif

#ifdef MODULES_UART_HW
  bigcall NET_Uart_Every100ms
#endif

#ifdef MODULES_TTYONUART1
  bigcall TtyOnUart1_Periodically
#endif

#ifdef MODULES_UARTFD0
  bigcall UARTFD0_Every100ms
#endif

#ifdef MODULES_UARTFD1
  bigcall UARTFD1_Every100ms
#endif

#ifdef MODULES_COMONUART0
  bigcall ComOnUart0_Periodically
#endif

#ifdef MODULES_COMONUART1
  bigcall ComOnUart1_Periodically
#endif

#ifdef MODULES_COM2W
  bigcall COM2W_Every100ms
#endif


#ifdef MODULES_COM2W0
  bigcall COM2W0_Periodically
#endif

#ifdef MODULES_COM2W1
  bigcall COM2W1_Periodically
#endif

#ifdef MODULES_COM2WN
  bigcall COM2WN_Periodically
#endif


#ifdef MODULES_TCRT1000
  bigcall TCRT1K_Every100ms
#endif

#ifdef MODULES_BRIGHTNESS
  bigcall Brightness_Every100ms
#endif

#ifdef MODULES_GUI
  bigcall GUI_Every100ms
#endif

#ifdef MODULES_XPT2046
  bigcall XPT2046_Every100ms
#endif


#ifdef APPS_NETWORK
  ldi yl, LOW(netInterfaceData)
  ldi yh, HIGH(netInterfaceData)
  bigcall AppNetwork_Every100ms
#endif

#ifdef APPS_MOTION
  bigcall AppMotion_Every100ms
#endif

#ifdef APPS_DOOR
  bigcall AppDoor_Every100ms
#endif

#ifdef APPS_MA_LIGHT
  bigcall AppMotionLight_Every100ms
#endif

  ret
; @end



#ifdef MODULES_CLOCK

sysOnEverySecond:
#ifdef MODULES_DS18B20
  bigcall Ds18b20_OnEverySecond
#endif

#ifdef APPS_REPORTSENSORS
  bigcall AppReportSensors_OnEverySecond
#endif

#ifdef MODULES_SGP30
  bigcall SGP30_EverySecond
#endif

  bigjmp onEverySecond
; @end


sysOnEveryMinute:
#ifdef APPS_STATS
  bigcall AppStats_OnEveryMinute
#endif

#ifdef MODULES_CCS811
  bigcall CCS811_OnEveryMinute
#endif

  bigjmp onEveryMinute
; @end


sysOnEveryHour:
  bigjmp onEveryHour
; @end


sysOnEveryDay:
#ifdef APPS_NETWORK
  ldi yl, LOW(netInterfaceData)
  ldi yh, HIGH(netInterfaceData)
  bigcall AppNetwork_EveryDay
#endif

#ifdef APPS_ROUTER
  bigcall AppRouter_EveryDay
#endif

#ifdef APPS_FORWARDER
  bigcall AppForwarder_EveryDay
#endif

  bigjmp onEveryDay
; @end

#endif



#ifdef MODULES_NETWORK
; ---------------------------------------------------------------------------
; @routine mainHandleMessages

mainHandleMessages:
  bigcall NET_GetNextIncomingMsgNum           ; R16=msg num
  brcc mainHandleMessages_end
  bigcall NET_Buffer_Locate                   ; X=buffer addr (R17)
  adiw xh:xl, 1

  push r16
    bigcall onMessageReceived
    bigcall mainModulesOnPacketReceived
    bigcall mainAppsOnPacketReceived
  pop r16
  bigcall NET_Buffer_ReleaseByNum
  sec
mainHandleMessages_end:
  ret
; @end
#endif





