/*********************************************************************
 *                  Flowcode Internals Header File
 *
 * File: internals.h
 *
 * (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
 * -------+----+-----------------------------------------------------
 * 010107 | ST | Created
 * 010108 | SK | Edited for ICD Operation
 * 031108 | BR | Edited for integration with PPP
 * 130309 | BR | Added Floating point, Hex and Decimal String Coversion Functions
 * 160309 | BR | Added ICD Port Definitions
 * 160309 | BR | Added Watchdog Delay routines
 * 090409 | BR | Fixed minor definitions bug
 * 170409 | BR | Optimised Floating / String conversions
 * 210509 | BR | Fixed ICD Stop and wait functionality
 * 110809 | BR | Changed the amount of delay in the watchdog ms delay routine
 * 160909 | BR | Added BoostC and HiTECH definitions
 * 290909 | BR | Altered the Test_bit function and ICD arrays for HITECH
 * 161009 | BR | Added a missing semi colon and added the math.h library for 16F devices
 * 221009 | BR | Removed the FCI_TMP_STR and FCI_TMP_INT variables
 * 101209 | BR | Altered timeout val and in <dbg_hold> routine got rid of initial delay and comms now wait forever
 * 181209 | BR | Edited reset function for use with the HITECH compiler.
 * 230210 | BR | Fixed a bug with multiple string concats requiring surplus variables.
 * 230710 | BR | Fixed an overflow bug in the Float to string and string to int functions
 * 270511 | ST | Added CAN fucntions for the MIAC_SYSTEM (from SK)
 * 020611 | BR | Disabled ICD functions from running when an interrupt is active
 * 220611 | BR | Modified the watchdog delays to try to provide better delay accuracy
 * 300611 | SK | Added MIAC Add-on Float Union Code
 * 110711 | SK | Duplicated MIAC I/O Functions - System Components
 * 010911 | BR | Moved the majority of the internals code into the CAL C files
 * 270912 | BR | Mod to PIC16BIT test bit function to allow it to work in 16-bit registers
 * 210313 | JW | Added v6 defines to get code working - may need to be done more elegantly in future
 * 110713 | BR | Added FCV_PRECISION definition to allow float to string function to work correctly
 * 250913 | LM | Removed FCP_GET/FCP_SET macro defs
 ********************************************************************/

#ifdef MX_PIC
  #include <system.h>
#endif


#ifndef internals_h
#define internals_h

	#define CAL_APD(a, b) 		a##b
	#define CAL_APPEND(a, b) 	CAL_APD(a, b)

	//BR 23/02/10 - These are needed by multiple concat statements?
	//However 10F devices could not allocate enough memory to create array
	#ifndef MX_10F_TRIS
		unsigned char FCI_TMP_STR[20];
		int FCI_TMP_INT;
	#endif

	//Code for variable manager port code
	#ifndef FCP_USE_TRIS
	  #define FCP_USE_TRIS 1
	#endif

	//Code for float to string
	#ifndef FCV_PRECISION
	  #define FCV_PRECISION 6
	#endif

	#ifdef MX_CAL_PIC
		#if 0
		#if (FCP_USE_TRIS == 1)
			#define FCP_GET(p, m, b) FC_CAL_Port_In_DDR__x((&FCP_PORT_##p), (&FCP_TRIS_##p), (m), (b)) 	/* Gets the value of a port variable */
			#define FCP_SET(p, m, b, v) clear_bit(FCP_TRIS_##p, b); if (v) set_bit(FCP_LAT_##p, b); else clear_bit(FCP_LAT_##p, b);
		#else
			#define FCP_GET(p, m, b) test_bit(FCP_PORT_##p, b); 										/* Gets the value of a port variable */
			#define FCP_SET(p, m, b, v) if (v) set_bit(FCP_LAT_##p, b); else clear_bit(FCP_LAT_##p, b); /* Sets the value of a port variable */
		#endif
		#endif
		#include "PIC\PIC_CAL_TypeDefs.c"	//Variable Type Definitions

		/******************** BOOST C SPECIFICS *****************/
		#ifdef _BOOSTC

			#include <float.h>				//This may have an impact on code size but is needed for floats to work
			#include <rand.h>				//This may have an impact on code size but is needed for rand() to work

			#pragma CLOCK_FREQ 				MX_CLK_SPEED
			#define MX_INTERRUPT_MACRO		interrupt
			#define MX_CLEAR_WATCHDOG		clear_wdt()
			#define ROMARRAY_S				rom MX_STRING
			#define ROMARRAY_(t)			rom t*
			#define ROMARRAY_E

			#define cr_bit					clear_bit
			#define st_bit					set_bit
			#define ts_bit					test_bit

		#endif

		/******************* HITECH C SPECIFICS *****************/
		#ifdef HI_TECH_C

			#include <htc.h> 				//Delay Params
			#include "definitions.h"
			#include <math.h>

			#define _XTAL_FREQ 				MX_CLK_SPEED
			#define MX_INTERRUPT_MACRO		interrupt myisr
			#define MX_CLEAR_WATCHDOG		CLRWDT()
			#define ROMARRAY_S				const MX_UINT8
			#define ROMARRAY_(t)			const t*
			#define ROMARRAY_E				[]

			MX_UINT8 ts_bit(MX_UINT8 reg, MX_UINT8 bt);
			MX_UINT8 test_bit(MX_UINT8 reg, MX_UINT8 bt);

			#define INTE					INT0IE
			#define INTF					INT0IF

			#define st_bit(var,bitno) 		bitno = 1
			#define cr_bit(var,bitno) 		bitno = 0
			#define set_bit(var,bitno) 		var |= (1 << bitno)
			#define clear_bit(var,bitno) 	var &= ~(1 << bitno)

			MX_UINT8 ts_bit(MX_UINT8 reg, MX_UINT8 bt)
			{
				if (bt)
					return 1;
				return 0;
			}

			MX_UINT8 test_bit(MX_UINT8 reg, MX_UINT8 bt)
			{
				if (reg & (1 << bt))
					return (1 << bt);
				return 0;
			}

		#endif

		#include "PIC\PIC_CAL_IO.c"		//I/O Definitions and Functions
		#include "PIC\PIC_CAL_Delay.c"	//Delay Definitions
		#include "PIC\PIC_CAL_String.c"	//String Definitions
		#include "PIC\PIC_CAL_Float.c"	//Float Definitions

		#ifdef USE_FLOWCODE_ICD			//ICD SPECIFICS
			#include "PIC\PIC_CAL_ICD.c"
		#endif

		#ifdef USE_FLOWCODE_ICD2		//ICD2 SPECIFICS
			#include "PIC\PIC_CAL_ICD2.c"
		#endif

		#ifdef MX_MIAC_SYSTEM		//Automatic CAN implementation
			#include "PIC\PIC_CAL_MIAC_SYSTEM.c"
		#endif
	#endif


	#ifdef MX_CAL_PIC16
		#if 0
		#if (FCP_USE_TRIS == 1)
			#define FCP_GET(p, m, b) FC_CAL_Port_In_DDR__x((&FCP_PORT_##p), (&FCP_TRIS_##p), (m), (b)) /* Gets the value of a port variable */
			#define FCP_SET(p, m, b, v) FC_CAL_Port_Out_DDR__x((&FCP_PORT_##p), (&FCP_TRIS_##p), (m), (v) << (b)) /* Sets the value of a port variable */
		#else
			#define FCP_GET(p, m, b) FC_CAL_Port_In__x((&FCP_PORT_##p), (m), (b)) /* Gets the value of a port variable */
			#define FCP_SET(p, m, b, v) FC_CAL_Port_Out__x((&FCP_PORT_##p), (m), (v) << (b)) /* Sets the value of a port variable */
		#endif
		#endif

		#ifndef MX_CLKS_PER_INST
			#define MX_CLKS_PER_INST 2
		#endif

		#include "PIC16BIT\PIC16BIT_CAL_TypeDefs.c"	//Variable Type Definitions

		#define MX_CLEAR_WATCHDOG		asm("clrwdt")

		#define MX_Fosc 	MX_CLK_SPEED

		#define set_bit(var,bitno) 		var |= (1 << bitno)
		#define clear_bit(var,bitno) 	var &= ~(1 << bitno)
		#define test_bit(var, bitno)    (var & (1 << bitno))

		#define cr_bit					clear_bit
		#define st_bit					set_bit
		#define ts_bit					test_bit

		#define ROMARRAY_S				const MX_UINT8
		#define ROMARRAY_(t)			const t
		#define ROMARRAY_E				[]

		#if defined(__PIC24FV32KA301__)
			#define _EEDATA(N)   __attribute__((space(eedata),aligned(N)))
		#endif


		#include "PIC16BIT\PIC16BIT_CAL_IO.c"		//I/O Definitions and Functions
		#include "PIC16BIT\PIC16BIT_CAL_Delay.c"	//Delay Definitions
		#include "PIC16BIT\PIC16BIT_CAL_String.c"	//String Definitions
		#include "PIC16BIT\PIC16BIT_CAL_Float.c"	//Float Definitions

		#ifdef USE_FLOWCODE_ICD			//ICD SPECIFICS
			#include "PIC16BIT\PIC16BIT_CAL_ICD.c"
		#endif
		#ifdef USE_FLOWCODE_ICD2		//ICD2 SPECIFICS
			#include "PIC16BIT\PIC16BIT_CAL_ICD2.c"
		#endif
	#endif


	#ifdef MX_CAL_AVR
		#if 0
		#if (FCP_USE_TRIS == 1)
			#define FCP_GET(p, m, b) FC_CAL_Port_In_DDR__x((&FCP_PIN_##p), (&FCP_TRIS_##p), (m), (b)) 	/* Gets the value of a port variable */
			#define FCP_SET(p, m, b, v) set_bit(FCP_TRIS_##p, b); if (v) set_bit(FCP_PORT_##p, b); else clear_bit(FCP_PORT_##p, b);
		#else
			#define FCP_GET(p, m, b) test_bit(FCP_PIN_##p, b);											/* Gets the value of a port variable */
			#define FCP_SET(p, m, b, v) if (v) set_bit(FCP_PORT_##p, b); else clear_bit(FCP_PORT_##p, b); /* Sets the value of a port variable */
		#endif
		#endif
		#include <stdlib.h>

		#define MX_CLEAR_WATCHDOG	wdt_reset()
		#define RECIP3 1.0/3.0

		#define F_CPU	MX_CLK_SPEED

		#define clear_bit( reg, bitNumb )	((reg) &= ~(1 << (bitNumb)))
		#define set_bit( reg, bitNumb )		((reg) |= (1 << (bitNumb)))
		#define test_bit( reg, bitNumb )	((reg) & (1 << (bitNumb)))

		#define cr_bit					clear_bit
		#define st_bit					set_bit
		#define ts_bit					test_bit

		#define ROMARRAY_S				const MX_UINT8
		#define ROMARRAY_(t)			const t
		#define ROMARRAY_E				[]

		#ifdef SPIF
			#define BF 	(1 << SPIF)
		#endif

		#include "AVR\AVR_CAL_TypeDefs.c"	//Variable Type Definitions
		#include "AVR\AVR_CAL_IO.c"		//I/O Definitions and Functions
		#include "AVR\AVR_CAL_Delay.c"	//Delay Definitions
		#include "AVR\AVR_CAL_String.c"	//String Definitions
		#include "AVR\AVR_CAL_Float.c"	//Float Definitions

		#ifdef USE_FLOWCODE_ICD			//ICD SPECIFICS
			#include "AVR\AVR_CAL_ICD.c"
		#endif
		#ifdef USE_FLOWCODE_ICD2		//ICD2 SPECIFICS
			#include "AVR\AVR_CAL_ICD2.c"
		#endif

	#endif


	#ifdef MX_CAL_ARM

		#define FC_CAL_Bit_Call1(Apre, Alett, Apost, B)				Apre##Alett##_##Apost(B)
		#define FC_CAL_Bit_Call2(Apre, Alett, Apost, B, C)			Apre##Alett##_##Apost(C, B)
		#if 0
		#define FCP_GET(p, m, b)									(FC_CAL_Bit_Call1(port, p, in, m) >> (b))
		#define FCP_SET(p, m, b, v)									FC_CAL_Bit_Call2(port, p, out, m, (v) << (b))
		#endif
		#include <stdlib.h>
		#include <math.h>

		#define MX_CLEAR_WATCHDOG

		// Bit manipulation
		#define clear_bit( reg, bitNumb )	((reg) &= ~(1 << (bitNumb)))
		#define set_bit( reg, bitNumb )		((reg) |= (1 << (bitNumb)))
		#define test_bit( reg, bitNumb )	((reg) & (1 << (bitNumb)))

		#define cr_bit						clear_bit
		#define st_bit						set_bit
		#define ts_bit						test_bit

		#define ROMARRAY_S					const MX_UINT8
		#define ROMARRAY_(t)				const t
		#define ROMARRAY_E					[]

		#include "ARM\ARM_CAL_TypeDefs.c"	//Variable Type Definitions
		#include "ARM\ARM_CAL_IO.c"		//I/O Definitions and Functions
		#include "ARM\ARM_CAL_Delay.c"	//Delay Definitions
		#include "ARM\ARM_CAL_String.c"	//String Definitions
		#include "ARM\ARM_CAL_Float.c"	//Float Definitions

		#ifdef USE_FLOWCODE_ICD			//ICD SPECIFICS
			#include "ARM\ARM_CAL_ICD.c"
		#endif
		#ifdef USE_FLOWCODE_ICD2		//ICD2 SPECIFICS
			#include "ARM\ARM_CAL_ICD2.c"
		#endif

	#endif

#endif		//End of internals_h

