/*********************************************************************
 *                    Flowcode EEPROM Component Code
 *
 * File: PIC_EEPROM.c
 *
 * (c) 2009 Matrix Multimedia Ltd.
 * http://www.matrixmultimedia.com
 *
 * Software License Agreement
 *
 * The software supplied herewith by Matrix Multimedia Ltd (the
 * “Company”) for its Flowcode graphical programming language is
 * intended and supplied to you, the Company’s customer, for use
 * solely and exclusively on the Company's products. The software
 * is owned by the Company, and is protected under applicable
 * copyright laws. All rights are reserved. Any use in violation
 * of the foregoing restrictions may subject the user to criminal
 * sanctions under applicable laws, as well as to civil liability
 * for the breach of the terms and conditions of this licence.
 *
 * THIS SOFTWARE IS PROVIDED IN AN “AS IS” CONDITION. NO WARRANTIES,
 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED
 * TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
 * PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,
 * IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR
 * CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 * Changelog:
 *
 *  date  | by | description
 * -------+----+-----------------------------------------------------
 * 020407 | ST | Created
 * 121207 | BR | Added possibility of eedat & eeaddr register names
 * 130509 | BR | Added % definitions to DefinesCode section
 * 131109 | BR | Fixed #pragma error HITECH problem
 * 230311 | BR | Changed code to work for hitech and boostc without ifdefs
 * 280711 | BR | Fixed a #if problem where MX_EE were getting defined multiple times
 ********************************************************************/

/*********************************************************************

 Return & parameter types:
   void
   char
   short
   char*

 Pin directions:
   0 = OUTPUT
   2 = INPUT
   3 = BIDIRECTIONAL

**********************************************************************

[Settings]
CLSID={73CDBD68-192D-4830-95BF-F55A31E923EF}
IsAnalogue=0
MultipleAllowed=0
Description=Internal EEPROM Flowcode Component

**********************************************************************

[Port]
DesiredPort=0
PortMandatory=0
SamePortAsID=0

[Pins]
Count=0

[PinPort]
# PORTA = 0
# PORTB = 1
# PORTC = 2
# PORTD = 3
# PORTE = 4

[PinDesiredBit]

[PinDirection]
# DIR_INPUT = 2
# DIR_OUTPUT = 0
# DIR_BIDIRECTIONAL = 3

[PinMustUsePort]
# boolean

[SamePortAsPinX]
# -1 = any pin
# otherwise, the pin number

[PinMustUsePin]
# boolean

[PinValue]
# boolean (0 = off, 1 = on)

**********************************************************************

[MacroNames]
Count=2
1=EEPROMRead
2=WriteEEPROM

[VisibleMacroNames]
1=Read
2=Write

[MacroReturns]
1=char
2=void

[MacroIsPrivate]
1=0
2=0

[MacroParameters_EEPROMRead]
Count=1
1=addr

[MacroParamTypes_EEPROMRead]
1=short

[MacroParameters_WriteEEPROM]
Count=2
1=addr
2=data

[MacroParamTypes_WriteEEPROM]
1=short
2=char

 ********************************************************************/

/********************************************************************
 * ADDITIONAL CODE
 ********************************************************************/

/*DefinesCode_Start*/

/**** Macro Substitutions ****
******************************/

/*DefinesCode_End*/

/*InitialisationCode_Start*/
/*InitialisationCode_End*/

/*InterruptCode_Start*/
/*InterruptCode_End*/

/********************************************************************
 * FUNCTIONS
 ********************************************************************/

char EEPROMRead(short addr)
{
/*Macro_EEPROMRead_Start*/
	char data = 0;

	#ifdef MX_EE
		#ifdef _BOOSTC

			#ifndef MX_EEADR
				#ifdef EEADR
					#define MX_EEADR eeadr
				#else
					#ifdef EEADRL
						#define MX_EEADR eeadrl
					#endif
				#endif
			#endif
			#ifndef MX_EEDATA
				#ifdef EEDATA
					#define MX_EEDATA eedata
				#else
					#ifdef EEDATAL
						#define MX_EEDATA eedatal
					#else
						#ifdef EEDATL
							#define MX_EEDATA eedatl
						#endif
					#endif
				#endif
			#endif

			#ifndef MX_EEADR	//Catch Error
				#error "EEPROM error - MX_EEADR not set"
			#endif

			#ifndef MX_EEDATA
				#error "EEPROM error - MX_EEDATA not set"
			#endif

			MX_EEADR = addr & 0xff;
			#if (MX_EE_SIZE > 256)
				eeadrh = (addr & 0xff00) >> 8;
			#endif
			#ifdef MX_EE_TYPE2
				clear_bit(eecon1, EEPGD);
			#endif
			#ifdef MX_EE_TYPE3
				clear_bit(eecon1, EEPGD);
				clear_bit(eecon1, CFGS);
			#endif
			set_bit(eecon1, RD);
			data = MX_EEDATA;

		#endif

		#ifdef HI_TECH_C

			data = eeprom_read(addr);

		#endif
	#else
		#ifdef MX_EE_TYPE67X
			#error "FlowCode does not support EEPROM for this device"
		#else
			#ifdef MX_EE_TYPE62X
				#error "FlowCode does not support EEPROM for this device"
			#else
				#error "Chip does not have EEPROM memory"
			#endif
		#endif
	#endif
	return (data);

/*Macro_EEPROMRead_End*/
}


void WriteEEPROM(short addr, char data)
{
/*Macro_WriteEEPROM_Start*/

	#ifdef MX_EE
		#ifdef _BOOSTC

			#ifndef MX_EEADR
				#ifdef EEADR
					#define MX_EEADR eeadr
				#else
					#ifdef EEADRL
						#define MX_EEADR eeadrl
					#endif
				#endif
			#endif
			#ifndef MX_EEDATA
				#ifdef EEDATA
					#define MX_EEDATA eedata
				#else
					#ifdef EEDATAL
						#define MX_EEDATA eedatal
					#else
						#ifdef EEDATL
							#define MX_EEDATA eedatl
						#endif
					#endif
				#endif
			#endif

			#ifndef MX_EEADR	//Catch Error
				#error "EEPROM error - MX_EEADR not set"
			#endif

			#ifndef MX_EEDATA
				#error "EEPROM error - MX_EEDATA not set"
			#endif

			char bInterruptsEnabled;

			//wait for previous EE writes to complete...
			while (test_bit(eecon1, WR));

			MX_EEADR = addr & 0xff;
			#if (MX_EE_SIZE > 256)
				eeadrh = (addr & 0xff00) >> 8;
			#endif
			MX_EEDATA = data;
			#ifdef MX_EE_TYPE2
				clear_bit(eecon1, EEPGD);
			#endif
			#ifdef MX_EE_TYPE3
				clear_bit(eecon1, EEPGD);
				clear_bit(eecon1, CFGS);
			#endif
			set_bit(eecon1, WREN);
			bInterruptsEnabled = test_bit(intcon, GIE);
			clear_bit(intcon, GIE);			//Disable Interrupts

			eecon2 = 0x55;
			eecon2 = 0xAA;
			set_bit(eecon1, WR);
			while (test_bit(eecon1, WR));	//wait for EE write to complete...

			if (bInterruptsEnabled)
				set_bit(intcon, GIE);		//Re-enable Interrupts
			clear_bit(eecon1, WREN);

		#endif
		#ifdef HI_TECH_C

			eeprom_write(addr, data);

		#endif
	#else
		#ifdef MX_EE_TYPE67X
			#error "FlowCode does not support EEPROM for this device"
		#else
			#ifdef MX_EE_TYPE62X
				#error "FlowCode does not support EEPROM for this device"
			#else
				#error "Chip does not have EEPROM memory"
			#endif
		#endif
	#endif
/*Macro_WriteEEPROM_End*/
}
