//	AD7716 interface
//		These routines are used to interface to the AD7716 A/D converter.
//		The converter uses the TT8 QSPI interface, and requires that the
//		12 bit MAX186 be disabled during operation.  The MAX186 can be
//		briefly re-enabled by using the pause and resume functions.
//
//		The AD7716 interface uses the following hardware:
//			IRQ4 = Port F4 is connected to the Data Ready line, and generates
//				an interrupt which then uses the QSPI to collect data
//			REVIEW - update this PCS documentation to properly reflect the lines used
//			PCS (peipheral chip select) lines are used as control lines
//				to the AD7716.  PCSX is connected to Transmit Frame
//				Sync (TFS); PCSY is connected to Receive Frame Sync (RFS);
//				PCSZ is connected to the Reset line.
//			The QSPI interrupt (bit7 of SPCR3) is used to determine when new
//				data has been received in QSPI Receive RAM
//
//  All user access of the AD7716 should be through these routines.  Those which are
//	intended for users are declared below.
//
//	AD7716_power_on_init (void):
//		Called when power is first turned on.  Initializes various internal
//		data structures
//
//	AD7716_open  (long n_buffer_points):
//		Allocates data buffer.  Does not mark any channels for saving, and
//		does not began data collection.  Sets default conversion frequency.
//		The user must call the AD7716_set_channels() routine before
//		starting data flow.
//
//	AD7716_close (void):
//		Stops data collection and frees data buffers.
//
//	AD7716_set_frequency (float freq):
//		Sets conversion frequency.  The requested freq must fall within
//		approximately 1% of a known frequency to be accepted
//
//	AD7716_get_frequency (void):
//		Returns current conversion frequency in samples/second.
//
//  AD7716_set_channels (int channels):
//		Marks which channels to record
//
//	AD7716_get_channels (void):
//		Returns bit pattern specifiying which channels are being recorded
//
//	AD7716_start_data (void):
//		Starts collection of data
//
//	AD7716_stop_data (void):
//		Stops data collection and clears the data buffer
//
//	AD7716_pause_data (void):
//		Temporarily suspends data collection so that other routines 
//		can use the SPI interface
//
//	AD7716_resume_data (void):
//		Sets the SPI interface back up and resumes data collection; 
//		if called quickly enough after AD7716_pause, no data will
//		be lost.
//
//	AD7716_get_data_point (long* value):
//		Returns the oldest data point, if one is available.
//
//	AD7716_get_data_point_time (void):
//		Returns the time at which the most recently returned data point
//		(via AD7716_get_data_point) was read.  Data collection must be
//		active for this to work properly.
//
//	AD7716_get_channel_num (long value):
//		Takes a value returned by the get_data_point routine and returns
//		the channel number from which this value was converted.  This
//		routine uses information imbedded in the lowest two bits of the
//		value to determine the channel number.
//
//	Debugging routines: compiled only if DEBUG is defined:
//	AD7716_critical_data_dump (void):
//		Prints the data structures used by these routines; not 
//		compiled in release version
//
#ifndef AD7716_DEFINED
#define AD7716_DEFINED	0x7F01

#include	<tt8lib.h>

void AD7716_power_on_init (void);
void AD7716_turn_power_off (void);
void AD7716_turn_power_on (void);
int AD7716_open (long n_buffer_points);
void AD7716_close (void);
double AD7716_set_conversion_rate (float f);
short AD7716_set_averaging (short n_to_average);
double AD7716_get_conversion_rate (void);
void AD7716_set_channels (short channels);
short AD7716_get_channels (void);
short AD7716_get_n_channels (void);
void AD7716_start_data (void);
void AD7716_stop_data (void);
void AD7716_pause_data (void);
void AD7716_resume_data (void);
int AD7716_get_data_point (long* value);
int AD7716_get_channel_num (long value);
time_tt AD7716_get_data_point_time (void);
short AD7716_check_for_overflow (void);
void AD7716_clear_overflow (void);
unsigned long AD7716_get_icount (void);
long AD7716_get_n_waiting (void);
#ifdef DEBUG
	void AD7716_critical_data_dump (void);
	void AD7716_VALIDITY_FAILURE (void* exp, unsigned line1, unsigned line2);
	void AD7716_assert_valid (unsigned calling_line);
	//  temporary debugging variable - delete it
	extern short SPI_copy[];
#else
	#define AD7716_assert_valid(exp) ((void)0)
#endif
#ifdef SIMULATE_DATA
	unsigned long AD7716_get_sim_count (void);
	void AD7716_reset_sim_count (void);
#endif

void AD7716_icount_print (void);

//  appropriate for a 7.3728e6 crystal
#define		DEFAULT_CONVERSION_RATE		129.0


#endif
