; program skeleton for ATtiny2313
; Copyright 2008 Stefan Schuermans <stefan@blinkenarea.org>
; Copyleft: GNU public license V2 - http://www.gnu.org/copyleft/gpl.html
; a BlinkenArea project - http://www.blinkenarea.org/

; fuse bits: low=0x64 high=0xD9:m upper=0xFF
; clock frequency: 8 MHz (internal RC oscillator)

; PA0..1: unused
; PA2: reset
; PB0..2: unused
; PB3: input from start button (low active)
; PB4: input from turn off button (low active)
; PB5: programming input
; PB6: programming output
; PB7: programming clock
; PD0..6: output to LED1..7



.INCLUDE        "tn2313def.inc"



; IO pins
.equ    PIN_BUTTONS     =       PINB            ; button inputs
.equ    BIT_BTN_START   =       3               ;  start button
.equ    BIT_BTN_OFF     =       4               ;  turn off button
.equ    PORT_LEDS       =       PORTD           ; LED outputs



; general purpose registers
.def    TMP             =       r16             ; register for temporary storage
.def    STATE           =       r0              ; register for state of button



.DSEG
.ORG    0x060

; variables
MYVAR:          .BYTE   1



.CSEG
.ORG    0x000
        rjmp    ENTRY                           ; RESET
        reti                                    ; INT0
        reti                                    ; INT1
        reti                                    ; TIMER1_CAPT
        reti                                    ; TIMER1_COMPA
        reti                                    ; TIMER1_OVF
        reti                                    ; TIMER0_OVF
        reti                                    ; USART0_RX
        reti                                    ; USART0_UDRE
        reti                                    ; USART0_TX
        reti                                    ; ANALOG_COMP
        reti                                    ; PC_INT
        reti                                    ; TIMER1_COMPB
        reti                                    ; TIMER0_COMPA
        reti                                    ; TIMER0_COMPB
        reti                                    ; USI_START
        reti                                    ; USI_OVERFLOW
        reti                                    ; EE_READY
        reti                                    ; WDT



; code entry point
ENTRY:
; initialize stack pointer
        ldi     TMP,RAMEND
        out     SPL,TMP
; set clock prescaler to 1:1
        ldi     TMP,1<<CLKPCE
        out     CLKPR,TMP
        ldi     TMP,0
        out     CLKPR,TMP
; set PA0..1 to output and low, PA2 is disconnected by fuse bits
; TODO
; set PB0..2 and PB6 to output and low, PB3..4 to input without pull-up, PB5 and PB7 to input with pull-up
; TODO
; set PD0..6 to output and low
; TODO
; disable anlaog comparator
        ldi     TMP,0x80
        out     ACSR,TMP
; jump to main program
        rjmp    MAIN



; main program - initialization
MAIN:
; turn on all LEDs
        ldi     TMP,0x7F
        out     PORT_LEDS,TMP



; main program - main loop
MAIN_LOOP:

; read state of start button
; TODO
; turn on LED1 if button is pressed
; TODO
; turn off LED1 if button is released
; TODO

; read state of turn off button
; TODO
; turn on LED2 if button is pressed
; TODO
; turn off LED2 if button is released
; TODO

; bottom of main loop
        rjmp    MAIN_LOOP

