; ***************************************************************************
; Source file for base system node on AtMega 644P
;
; This is for the maintenance system (i.e. the flash loader).
;
; All definitions and changes should go into this file.
; ***************************************************************************

.equ clock=16000000 ; Define the clock frequency

.nolist
.include "include/m644Pdef.inc" ; Define device ATmega644P
.list

.include "../defs.asm"
.include "devices/all/defs.asm"

.include "common/calls.asm"
.include "common/utils_wait.asm"
.include "common/utils_io.asm"



; ***************************************************************************
; defines

; ---------------------------------------------------------------------------
; generic


.equ NET_BUFFERS_NUM            = 6
.equ NET_BUFFERS_SIZE           = 32



; ---------------------------------------------------------------------------
; firmware settings

.equ FIRMWARE_VERSION_MAJOR       = 0
.equ FIRMWARE_VERSION_MINOR       = 0
.equ FIRMWARE_VERSION_PATCHLEVEL  = 1



; ---------------------------------------------------------------------------
; LED

.equ LED_DDR             = DDRB
.equ LED_PORT            = PORTB
.equ LED_PIN             = PINB
.equ LED_PINNUM          = PORTB0





; ***************************************************************************
; code segment

.cseg
.org 0x0000



; ---------------------------------------------------------------------------
; Reset and interrupt vectors
  jmp main                      ;  1: Reset vector   RESET
  jmp irqNotSet                 ;  2: INT0           External Interrupt Request 0
  jmp irqNotSet                 ;  3: INT1           External Interrupt Request 1
  jmp irqNotSet                 ;  4: INT2           External Interrupt Request 2
  jmp irqNotSet                 ;  5: PCINT0         Pin Change Interrupt Request 0
  jmp irqNotSet                 ;  6: PCINT1         Pin Change Interrupt Request 1
  jmp irqNotSet                 ;  7: PCINT2         Pin Change Interrupt Request 2
  jmp irqNotSet                 ;  8: PCINT3         Pin Change Interrupt Request 3
  jmp irqNotSet                 ;  9: WDT            Watchdog Time-out Interrupt
  jmp irqNotSet                 ; 10: TIMER2_COMPA   Timer/Counter2 Compare Match A
  jmp irqNotSet                 ; 11: TIMER2_COMPB   Timer/Counter2 Compare Match B
  jmp irqNotSet                 ; 12: TIMER2_OVF     Timer/Counter2 Overflow
  jmp irqNotSet                 ; 13: TIMER1_CAPT    Timer/Counter1 Capture Event
  jmp irqNotSet                 ; 14: TIMER1_COMPA   Timer/Counter1 Compare Match A
  jmp irqNotSet                 ; 15: TIMER1_COMPB   Timer/Counter1 Compare Match B
  jmp irqNotSet                 ; 16: TIMER1_OVF     Timer/Counter1 Overflow
  jmp irqNotSet                 ; 17: TIMER0_COMPA   Timer/Counter0 Compare Match A
  jmp irqNotSet                 ; 18: TIMER0_COMPB   Timer/Counter0 Compare Match B
  jmp irqNotSet                 ; 19: TIMER0_OVF     Timer/Counter0 Overflow
  jmp irqNotSet                 ; 20: SPI_STC        Serial Transfer Complete
  jmp irqNotSet                 ; 21: USART0_RXC     USART0 Rx Complete
  jmp irqNotSet                 ; 22: USART0_UDRE    USART0 Data Register Empty
  jmp irqNotSet                 ; 23: USART0_TXC     USART0 Tx Complete
  jmp irqNotSet                 ; 24: ANA_COMP       Analog Comparator
  jmp irqNotSet                 ; 25: ADC            ADC Conversion Complete
  jmp irqNotSet                 ; 26: EE_RDY         EEPROM Ready
  jmp irqNotSet                 ; 27: TWI            2-Wire Interface
  jmp irqNotSet                 ; 28: SPM_RDY        Store Program Memory Ready



; ---------------------------------------------------------------------------
; Device Info Block

devInfoBlock:        ; 12 bytes
devInfoManufacturer: .db 'A', 'Q', 'U', 'A'
devInfoId:           .db DEVICEINFO_ID, 0
devInfoVersion:      .db DEVICEINFO_VERSION, DEVICEINFO_REVISION             ; version, revision
firmwareVersion:     .db FIRMWARE_VARIANT_BOOT, FIRMWARE_VERSION_MAJOR
                     .db FIRMWARE_VERSION_MINOR, FIRMWARE_VERSION_PATCHLEVEL

firmwareStart:
  jmp main        ; will be overwritten when flashing

irqNotSet:
  reti



; ***************************************************************************
; main code


.org BOOTLOADER_ADDR


main:
;  ldi r16, 0xb0   ; orig: a0
;  out OSCCAL, r16
  jmp bootLoader ; this routine is in modules/bootloader/main.asm



; ***************************************************************************
; includes

.include "common/wait_10us.asm"
.include "common/utils_copy_from_flash.asm"
.include "common/utils_copy_sdram.asm"

.include "modules/flash/defs.asm"
.include "modules/flash/eeprom.asm"
.include "modules/flash/io.asm"
.include "modules/flash/io_com2w.asm"
.include "modules/flash/flash1pmega.asm"
.include "modules/flash/flashxp.asm"
.include "modules/flash/flashprocess.asm"
.include "modules/flash/wait.asm"
.include "modules/bootloader/main.asm"
.include "modules/network/msg/defs.asm"
.include "modules/network/msg/crc.asm"

;.include "common/debug.asm"



systemSetSpeed:
  ; speed not changeable at runtime on this device
  ret




