 include "lcd_io.h"
 
; base frequencies, rates, etc
#define	XTAL_FREQ	D'20000000'	; OSC freq in Hz
#define BAUD_RATE	38400		; Desired baud rate
#define	I2CClock	D'100000'	; define I2C bite rate
#define RX_BUFFER_SIZE	32		; serial recieve buffer allocation
#define TX_BUFFER_SIZE	32		; serial transmit buffer allocation
#define	WAITING_DELAY	d'130'		; Delay between '.'s in "Waiting" stuffs

; caulculates baudrate when BRGH = 1, adjust for rounding errors
#define CALC_HIGH_BAUD(BaudRate)        (((10*XTAL_FREQ/(16*BaudRate))+5)/10)-1
; caulculates baudrate when BRGH = 0, adjust for rounding errors
#define CALC_LOW_BAUD(BaudRate)         (((10*XTAL_FREQ/(64*BaudRate))+5)/10)-1

; used for I2C calculations
#define  I2C_ClockValue  (((XTAL_FREQ/I2CClock)/4) -1) ; 


;*****
;I2C addresses
;*****
#define LED_ADDR	b'01000010'	; [0100]xxx0
#define FBUT_ADDR	b'01001001'	; [0100]xxx1
#define MBUT_ADDR	b'00000001'	; Set me
#define	TEMP_ADDR_R	b'10010001'	; [1001]xxx1
#define	TEMP_ADDR_W	b'10010000'	; [1001]xxx0
#define	EEPRM_ADDR_R	b'10100001'	; [1010]xxx1
#define	EEPRM_ADDR_W	b'10100000'	; [1010]xxx0
#define	RTC_ADDR_R	b'11010001'	; [1101000]1
#define RTC_ADDR_W	b'11010000'	; [1101000]0

;*****
; Power Controll
;*****
#define POWER_PRT	PORTC
#define POWER_CTRL	PORTC, 5
#define POWER_ON	bsf	POWER_CTRL
#define POWER_OFF	bcf	POWER_CTRL

;*****
; EXT. Reset line
;*****
#define RESET_PRT	PORTC
#define RESET_CTRL	PORTC, 0
#define RESET_ON	bcf	RESET_CTRL
#define RESET_OFF	bsf	RESET_CTRL

;*****
; Fan crap.
;*****
#define FAN_PRT		PORTE
#define	FAN_CTRL	PORTE, 2

;*****
;LED Crap
;*****
#define LED_PWR		Led_Data, 7
#define LED_ACT		Led_Data, 6
#define LED_RX		Led_Data, 1
#define LED_TX		Led_Data, 0

;*****
; Pizeo beeper crap
;*****
#define PIEZO_PRT	PORTC
#define PIEZO_TRIS	TRISC
#define PIEZO_BIT	2
#define BEEPER		PIEZO_PRT, PIEZO_BIT

;*****
; Digital temperature chip stuff
;*****
#define TEMP_RT		0xAA		; Read Temperature register
#define TEMP_ATH	0xA1		; Access Temp High trip point
#define	TEMP_ATL	0xA2		; Access Temp Low trip point
#define	TEMP_AC		0xAC		; Access Configuration register
#define TEMP_STC	0x51		; StarT Conversion 
#define	TEMP_SPC	0x22		; StoP Conversion

;*****
; 24C64 EEPROM chip info
; Stores startup logo
;*****
#define	EEPRM_SIZE	2000h		; Size of memmory
#define	EEPRM_PAGE_SIZE	30		; 30 bytes per page

;*****
;LCD port setup
;*****
#define LCD_BIAS_PRT	PORTE
#define	LCD_BIAS_CTRL	PORTE, 0
#define	LCD_BIAS_ADJ	PORTE, 1
#define	LCD_BIAS_ON	bsf	LCD_BIAS_PRT, LCD_BIAS_CTRL
#define	LCD_BIAS_OFF	bcf	LCD_BIAS_PRT, LCD_BIAS_CTRL

#define	LCD_DATA	PORTD
#define	LCD_DATA_TRIS	TRISD
#define	LCD_CTRL	PORTA

;LCD Control Signal names.
#define LCD_CE		LCD_CTRL,3		; LCD Enable control line
#define LCD_RD		LCD_CTRL,1		; LCD Read control line
#define LCD_WR		LCD_CTRL,2		; LCD Write control line
#define LCD_C_D		LCD_CTRL,0		; LCD Control/data Select line

; SYS_OPTS
; System options register, defines, etc.
#define	SOPT_BUFREDY	7	; Serial buffer ready?
#define SOPT_BUFOVER	6	; Serial buffer overflow!
#define SOPT_BUTDN	5	; Button down
#define SOPT_OK2I2C	4	; It ok for ISR i2c routines?
#define SOPT_NADA3	3
#define SOPT_NADA2	2
#define SOPT_CONNECTED	1
#define SOPT_LCDCOL	0	; LCD color when blanking
#define _BufferOverrun  SYS_OPTS,SOPT_BUFOVER	; buffer overrun

;***** RAM VARIABLES DEFINITIONS

        ; *** Bank0 *** 80 bytes
        CBLOCK  0x020
f_but_fifo:13
SYS_OPTS:1			; System internal variables...
Led_Data:1			; LED display data
FBut_State:1			; Function Button state
MBut_State:1			; Menu button state
Piezo_Cntr:1			; Beeper countdown counter
Local_TempMSB:1			; Board temperature MSB
Local_TempLSB:1			; and LSB
EEPROM_Addr_H:1			; 24C64 EEPROM Address high byte
EEPROM_Addr_L:1			; 24C64 EEPROM Address low byte
EEPROM_Data:1			; 24C64 EEPROM Data
RTC_Addr:1			; RTC Adder
RTC_Data:1			; RTC Data

; LCD command and data buffers
COMMANDBUF:1
DATABUFA:1
DATABUFB:1

; Temporary storage registers..area specific..
tmp_int:1
tmp_but:1
tmp_main:1
tmp_lcd1:1
tmp_lcd2:1
tmp_temp:1
tmp_cmds:1
tmp_i2c1:1
tmp_i2c2:1
tmp_ser:1
tmp_out1:1
tmp_out2:1

; Used in temp conversion
Scratch:1
MpyDiv1:1
MpyDiv2:1
MpyDiv3:1
MpyDiv4:1
Count:1
bin:1
hundreds:1
tens_and_ones:1

logo_txt_cntr:1
logo_txt_pos:1

tempone:1			; Delay var
temptwo:1			; Delay var
tempthree:1
OffsetH:1
OffsetL:1

; serial buffer RX and TX  pointers, buffers located in bank1
RX_Buffer_InPtr:1       	; where to put next incomming byte
RX_Buffer_OutPtr:1      	; where to get next ( first ) byte
RX_Buffer_Count:1       	; how many we have in buffer

TX_Buffer_InPtr:1       	; where to put next outgoing byte
TX_Buffer_OutPtr:1      	; whre to get next byte 	
TX_Buffer_Count:1       	; how many we have in buffer
TX_Temp:1               	; temporary reg used while sending

rx_data:1
tx_data:1

        ENDC
        
        ; *** Bank0/1/2/3 mirrored in all banks 0x70, 0xF0, 0x170, 0x1F0, 16 bytes
        ; ram variables accesible from all banks mainly used for context saving
        ; ( ram area above 0x70 are mirrored in all banks )
        CBLOCK  0x070
        ICD_Reserved1:1 	; for icd
        Saved_W:1       	; variable used for context saving 
        Saved_Status:1  	; variable used for context saving
        Saved_Pclath:1  	; 
        Saved_Fsr:1     	;
        Table_Temp:1    	; table lookup temp variable
        ENDC
        
        ; *** Bank1 *** 80 bytes
        CBLOCK  0x0A0
RX_Buffer:RX_BUFFER_SIZE        ; buffer for receving
TX_Buffer:TX_BUFFER_SIZE        ; buffer for sending
        ENDC

        ; *** Bank2 *** extra ram 16 bytes
        CBLOCK  0x110
        
        ENDC

        ; *** Bank2 *** 80 Bytes
        CBLOCK  0x120
        ENDC

        ; *** Bank3 *** extra ram 16 bytes
        CBLOCK  0x190

        ENDC
        ; *** Bank3 *** 80 bytes
        CBLOCK  0x1A0

        ENDC

        CBLOCK  0x1EB
        ENDC

