// CRC: 289BEDF10BA76345B6BC99ACD51A6F5BD4021537C1260D6E07B77238C0FEE01B66D7CDAF564C713C51E0A8FAEFAD2EFB2703EDFB697E8A1F2A3B2E8C646EF11D78E226430338D9D83BB30BD44376A2E95AF4DB1A854EB83629FF4680D55093BC2793A204D0A0080713F82284423AEF7CBA04AD747C7EF74EFE1D590ED1E320A99C4B49F4B7DA885CD3038E3B5AD4591F5CD9410FD5CCAD310362748FD5F352A1330E1BCA78884EE28DCFF29235443E5CE5402CE8A53EFBC278BE3B663ED3C3019AC639F53E5FAB72D20C52081B5E9CB4535C05561C3D5FBC
// REVISION: 1.0
// GUID: 85F665AC-116A-4A6D-94EE-B05B1F6B26D8
// DATE: 25\04\2018
// DIR: CAL\PIC\PIC_CAL_EEPROM.c
/*********************************************************************
 *                  Flowcode CAL EEPROM File
 *
 * File: PIC_CAL_EEPROM.c
 *
 * (c) 2011 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
 * -------+----+-----------------------------------------------------
 * 210911 | BR | Created
 * 010513 | LM | V6 EEPROM CAL standardised API to 16 bit data word size
 * 240517 | BR | Added Type 4 for new 18F devices
 * 300517 | LM | Additional mods to support 16F18 (compiles but untested)
 */




#ifdef _NVMCON1_WR_POSN

	MX_UINT16 FC_CAL_EE_Read (MX_UINT16 Address)
	{
		MX_UINT16 data = 0;

		#ifdef MX_EE

			#if defined _NVMCON1_NVMREG_POSN
				NVMCON1bits.NVMREG = 0;					//EEPROM Access
			#elif defined _NVMCON1_NVMREGS_POSN
				NVMCON1bits.NVMREGS = 1;				//EEPROM Access
			#endif

			NVMADRL = Address & 0xff;

			#if (MX_EE_SIZE > 256)
				NVMADRH = (Address & 0xff00) >> 8;
			#endif

			#if defined (MX_EE_TYPE4)					//EE Memory starts at address 0xF000
				NVMADRH |= 0xF0;
			#endif

			NVMCON1bits.RD = 1;
			#if defined _NVMDAT_NVMDAT0_POSN
				data = NVMDAT;
			#elif defined _NVMDATL_NVMDAT0_POSN
				data = NVMDATL;
			#endif

			#if defined _NVMCON1_NVMREG_POSN
				NVMCON1bits.NVMREG = 2;					//FLASH Access
			#elif defined _NVMCON1_NVMREGS_POSN
				NVMCON1bits.NVMREGS = 0;				//FLASH Access
			#endif

		#else
			#error "Chip does not have EEPROM memory"
		#endif
		return (data);
	}

	void FC_CAL_EE_Write (MX_UINT16 Address, MX_UINT16 Data)
	{
		#ifdef MX_EE

			char bInterruptsEnabled;

			//wait for previous EE writes to complete...
			while (ts_bit(NVMCON1, WR));

			#if defined _NVMCON1_NVMREG_POSN
				NVMCON1bits.NVMREG = 0;					//EEPROM Access
			#elif defined _NVMCON1_NVMREGS_POSN
				NVMCON1bits.NVMREGS = 1;				//EEPROM Access
			#endif

			NVMADRL = Address & 0xff;

			#if (MX_EE_SIZE > 256)
				NVMADRH = (Address & 0xff00) >> 8;
			#endif

			#if defined (MX_EE_TYPE4)					//EE Memory starts at address 0xF000
				NVMADRH |= 0xF0;
			#endif

			#if defined _NVMDAT_NVMDAT0_POSN
				NVMDAT = Data;
			#elif defined _NVMDATL_NVMDAT0_POSN
				NVMDATL = Data;
			#endif

			NVMCON1bits.WREN = 1;

			bInterruptsEnabled = ts_bit(INTCON, GIE);
			cr_bit(INTCON, GIE);			//Disable Interrupts

			NVMCON2 = 0x55;
			NVMCON2 = 0xAA;

			NVMCON1bits.WR = 1;

			while (ts_bit(NVMCON1, WR));		//wait for EE write to complete...

			if (bInterruptsEnabled)
				st_bit(INTCON, GIE);		//Re-enable Interrupts

			NVMCON1bits.WREN = 0;
			
			#if defined _NVMCON1_NVMREG_POSN
				NVMCON1bits.NVMREG = 2;					//FLASH Access
			#elif defined _NVMCON1_NVMREGS_POSN
				NVMCON1bits.NVMREGS = 0;				//FLASH Access
			#endif
			
		#else
			#error "Chip does not have EEPROM memory"
		#endif
	}

#else


	MX_UINT16 FC_CAL_EE_Read (MX_UINT16 Address)
	{
		MX_UINT16 data = 0;

		#ifdef MX_EE

			EEADR = Address & 0xff;
			#if (MX_EE_SIZE > 256)
				EEADRH = (Address & 0xff00) >> 8;
			#endif
			#ifdef MX_EE_TYPE2
				cr_bit(EECON1, EEPGD);
			#endif
			#ifdef MX_EE_TYPE3
				cr_bit(EECON1, EEPGD);
				cr_bit(EECON1, CFGS);
			#endif
			st_bit(EECON1, RD);
			data = EEDATA;

		#else
			#error "Chip does not have EEPROM memory"
		#endif
		return (data);
	}

	void FC_CAL_EE_Write (MX_UINT16 Address, MX_UINT16 Data)
	{
		#ifdef MX_EE

			char bInterruptsEnabled;
			//wait for previous EE writes to complete...
			while (ts_bit(EECON1, WR));
			EEADR = Address & 0xff;
			#if (MX_EE_SIZE > 256)
				EEADRH = (Address & 0xff00) >> 8;
			#endif
			EEDATA = Data;
			#ifdef MX_EE_TYPE2
				cr_bit(EECON1, EEPGD);
			#endif
			#ifdef MX_EE_TYPE3
				cr_bit(EECON1, EEPGD);
				cr_bit(EECON1, CFGS);
			#endif
			st_bit(EECON1, WREN);
			bInterruptsEnabled = ts_bit(INTCON, GIE);
			cr_bit(INTCON, GIE);			//Disable Interrupts
			EECON2 = 0x55;
			EECON2 = 0xAA;
			st_bit(EECON1, WR);
			while (ts_bit(EECON1, WR));	//wait for EE write to complete...
			if (bInterruptsEnabled)
				st_bit(INTCON, GIE);		//Re-enable Interrupts
			cr_bit(EECON1, WREN);

		#else
			#error "Chip does not have EEPROM memory"
		#endif
	}

#endif

