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


; ***************************************************************************
;
;                                 AtMega644
;                                 --------
;                        LED PB0  1     40 PA0 DEV0
;                        DC  PB1  2     39 PA1 DEV1
;                      INT2  PB2  3     38 PA2 DEV2
;                    DSPRES  PB3  4     37 PA3 PA3
;                        SS  PB4  5     36 PA4 LUM
;                      MOSI  PB5  6     35 PA5
;                      MISO  PB6  7     34 PA6
;                       SCK  PB7  8     33 PA7
;                         /RESET  9     32 AREF
;                            VCC 10     31 GND
;                            GND 11     30 AVCC
;                          XTAL2 12     29 PC7
;                          XTAL1 13     28 PC6
;                       RXD  PD0 14     27 PC5
;                       TXD  PD1 15     26 PC4
;                            PD2 16     25 PC3
;                      INT1  PD3 17     24 PC2
;                   AUX_PD4  PD4 18     23 PC1
;                    DSPLED  PD5 19     22 PC0
;                            PD6 20     21 PD7
;                                 --------
;
; ***************************************************************************



.equ BOOTLOADER_ADDR = 0x7c00 ; word address (=0xf800 in byte address)

.equ RESSOURCE_ADDR  = 0x4000 ; word address (=0x8000 in byte address)

.equ FIRMWARE_VARIANT_BOOT        = 0
.equ FIRMWARE_VARIANT_TEMP_WINDOW = 1

.equ DEVICEINFO_ID       = 'C'
.equ DEVICEINFO_VERSION  = 3
.equ DEVICEINFO_REVISION = 0



; ---------------------------------------------------------------------------
; EEPROM TLV ids

.equ C03_EEID_SENSOR_CO2  = 0x10
.equ C03_EEID_SENSOR_TEMP = 0x11
.equ C03_EEID_SENSOR_HUM  = 0x12

.equ EEID_SCREENSAVER     = 0x20



; ---------------------------------------------------------------------------
; Beeper module

.equ BEEPER_SIMPLE_ONTIME  = 1     ; shorter
.equ BEEPER_SIMPLE_OFFTIME = 0     ; longer
.equ BEEPER_SIMPLE_DDR     = DDRD
.equ BEEPER_SIMPLE_PORT    = PORTD
.equ BEEPER_SIMPLE_PORTIN  = PIND
.equ BEEPER_SIMPLE_PINNUM  = PORTD7



; ---------------------------------------------------------------------------
; LED module

.equ LED1_DEFAULT_ONTIME  = 1     ; shorter
.equ LED1_DEFAULT_OFFTIME = 99    ; longer
.equ LED1_DEFAULT_REPEATS = 0     ; continous
.equ LED1_DDR     = DDRB
.equ LED1_PORT    = PORTB
.equ LED1_PORTIN  = PINB
.equ LED1_PINNUM  = PORTB0



; ---------------------------------------------------------------------------
; COM module

.equ COM_BIT_LENGTH      = 52000 ; 104000ns=9600, 52000ns=19200, 26000ns=38400
.equ COM_HALFBIT_LENGTH  = 26000 ; see https://de.wikipedia.org/wiki/Universal_Asynchronous_Receiver_Transmitter

.equ COM_DATA_DDR       = DDRA
.equ COM_DATA_INPUT     = PINA
.equ COM_DATA_OUTPUT    = PORTA
.equ COM_DATA_PIN       = PORTA6

.equ COM_CLK_DDR       = DDRA
.equ COM_CLK_INPUT     = PINA
.equ COM_CLK_OUTPUT    = PORTA
.equ COM_CLK_PIN       = PORTA7

; pin change interrupt register and bit
.equ COM_IRQ_ADDR_CLK    = PCMSK0
.equ COM_IRQ_BIT_CLK     = PCINT7  ; PA7

; interrupt control register
.equ COM_IRQ_ICR_ADDR    = PCICR   ; PCICR on AtMega644P, GIMSK on AtTiny
.equ COM_IRQ_ICR_BIT     = PCIE0

; interrupt flag/mask register
.equ COM_IRQ_IFR_ADDR    = PCIFR   ; PCIFR on AtMega644P, GIFR on AtTiny
.equ COM_IRQ_IFR_BIT     = PCIF0

.equ COM_IRQ_GIFR_CLK    = INTF0
;.equ COM_IRQ_GIMSK_CLK   = PCIE0



; ---------------------------------------------------------------------------
; SPI hardware module

.equ SPIHW_SS_DDR      = DDRB
.equ SPIHW_SS_INPUT    = PINB
.equ SPIHW_SS_OUTPUT   = PORTB
.equ SPIHW_SS_PIN      = PORTB4

.equ SPIHW_MOSI_DDR    = DDRB
.equ SPIHW_MOSI_INPUT  = PINB
.equ SPIHW_MOSI_OUTPUT = PORTB
.equ SPIHW_MOSI_PIN    = PORTB5

.equ SPIHW_MISO_DDR    = DDRB
.equ SPIHW_MISO_INPUT  = PINB
.equ SPIHW_MISO_OUTPUT = PORTB
.equ SPIHW_MISO_PIN    = PORTB6

.equ SPIHW_SCK_DDR    = DDRB
.equ SPIHW_SCK_INPUT  = PINB
.equ SPIHW_SCK_OUTPUT = PORTB
.equ SPIHW_SCK_PIN    = PORTB7

.equ SPIHW_SS0_DDR    = DDRA
.equ SPIHW_SS0_OUTPUT = PORTA
.equ SPIHW_SS0_INPUT  = PORTA
.equ SPIHW_SS0_PIN    = PORTA0

.equ SPIHW_SS1_DDR    = DDRA
.equ SPIHW_SS1_OUTPUT = PORTA
.equ SPIHW_SS1_INPUT  = PORTA
.equ SPIHW_SS1_PIN    = PORTA1

.equ SPIHW_SS2_DDR    = DDRA
.equ SPIHW_SS2_OUTPUT = PORTA
.equ SPIHW_SS2_INPUT  = PORTA
.equ SPIHW_SS2_PIN    = PORTA2



; ---------------------------------------------------------------------------
; ILI9341 module

.equ DISPLAY_WIDTH        = 320
.equ DISPLAY_HEIGHT       = 240

.equ ILI9341_DEVICENUM    = 0
.equ ILI9341_DSP_WIDTH    = DISPLAY_WIDTH
.equ ILI9341_DSP_HEIGHT   = DISPLAY_HEIGHT

.equ ILI9341_RESET_DDR    = DDRB
.equ ILI9341_RESET_OUTPUT = PORTB
.equ ILI9341_RESET_INPUT  = PORTB
.equ ILI9341_RESET_PIN    = PORTB3

.equ ILI9341_DC_DDR       = DDRB
.equ ILI9341_DC_OUTPUT    = PORTB
.equ ILI9341_DC_INPUT     = PORTB
.equ ILI9341_DC_PIN       = PORTB1

.equ ILI9341_LED_DDR      = DDRD
.equ ILI9341_LED_OUTPUT   = PORTD
.equ ILI9341_LED_INPUT    = PORTD
.equ ILI9341_LED_PIN      = PORTD5

.equ ILI9341_BACKLIGHT_DDR       = DDRD
.equ ILI9341_BACKLIGHT_OUTPUT    = PORTD
.equ ILI9341_BACKLIGHT_INPUT     = PORTD
.equ ILI9341_BACKLIGHT_PIN       = PORTD5



; ---------------------------------------------------------------------------
; XPT2046 module

.equ XPT2046_DEVICENUM    = 1



; ---------------------------------------------------------------------------
; ComOnUart module

;.equ USART0_DATAREG = UDR
;.equ UCSR0A = UCSRA
;.equ UCSR0B = UCSRB
;.equ UCSR0C = UCSRC
;.equ UBRR0L = UBRRL
;.equ UBRR0H = UBRRH

;.equ UCSZ00 = UCSZ0
;.equ UCSZ01 = UCSZ1
;.equ UDRE0  = UDRE
;.equ RXC0   = RXC
;.equ TXC0   = TXC
;.equ FE0    = FE
;.equ DOR0   = DOR
;.equ UPE0   = UPE
;.equ RXEN0  = RXEN
;.equ TXEN0  = TXEN
;.equ USBS0  = USBS
;.equ RXCIE0 = RXCIE
;.equ UDRIE0 = UDRIE


; ---------------------------------------------------------------------------
; Brightness


.equ BRIGHTNESS_ADC_PORT    = PORTA ; adc4
.equ BRIGHTNESS_ADC_DDR     = DDRA
.equ BRIGHTNESS_ADC_PIN     = PORTA4
.equ BRIGHTNESS_ADC_MUX     = 4


