; SLdt  2405061640  PIT-Interrupt auf AVR64EA28
; avrasm2

.include   "AVR64EA28def.inc"

/*
.include   "AVR128DA28def.inc"
.include   "AVR64EA28def.inc"
.include   "AVR16EB28def.inc"
.include   "AVR32DD28def.inc"
.include   "m4809def.inc"
*/

.equ    DIR_LED         = VPORTA_DIR
.equ    IN_LED          = VPORTA_IN
.equ    LEDpit          = 7
.equ    LEDlive         = 6

.def    tmp0            = r16
;===============

.macro  rtcstatus
;/* geht offenbar auch ohne
rtcstatus_:
     lds    tmp0,RTC_STATUS
     tst    tmp0
    brne    rtcstatus_
pitstatus_:
     lds    tmp0,RTC_PITSTATUS
     tst    tmp0
    brne    pitstatus_
;*/
.endmacro
;===============

.org    0
    rjmp        reset

.org    RTC_PIT_vect
    ldi     tmp0,$FF
    sts     RTC_PITINTFLAGS,tmp0
    sbi     IN_LED,LEDpit
    reti
;===============

reset:
    sbi     DIR_LED,LEDpit
;    sbi     DIR_LED,LEDlive

    rtcstatus   

    ldi     tmp0,CPU_CCP_IOREG_gc
    out     CPU_CCP,tmp0
    ldi     tmp0,0b10000000             ; RUNSTDBY Run Standby
    sts     CLKCTRL_OSC32KCTRLA,tmp0    ; noetig?

; To operate the PIT, follow these steps:

; 1. Configure the RTC clock CLK_RTC as described in section 24.4.1.1. Configure the Clock CLK_RTC.
; Reset-Wert von RTC_CLKSEL passt

; 2. Enable the interrupt by writing a ‘1’ to the Periodic Interrupt (PI) bit in the PIT Interrupt Control (RTC.PITINTCTRL) register.
    ldi     tmp0,1
    sts     RTC_PITINTCTRL,tmp0

; 3. Select the period for the interrupt by writing the desired value to the Period (PERIOD) bit field in the Periodic Interrupt Timer Control A (RTC.PITCTRLA) register.
    rtcstatus
    ldi     tmp0,0b0_1101_00_0          ; /16384
    sts     RTC_PITCTRLA,tmp0

; 4. Enable the PIT by writing a ‘1’ to the Periodic Interrupt Timer Enable (PITEN) bit in the RTC.PITCTRLA register.
    rtcstatus
    lds     tmp0,RTC_PITCTRLA
    ori     tmp0,0b0_0000_00_1          ; enable
    sts     RTC_PITCTRLA,tmp0

    rtcstatus

    sei
main_loop:
;    sbi     IN_LED,LEDlive
 rjmp       main_loop

.exit