/*******************************************************************************
**	max3100.c
*******************************************************************************/

// Thomas P. Sullivan
// Enertech Consultants
// 11-18-98

// This software demonstrates using the Maxim 3100 UART with the QSPI of the 68338
//	found on the Persistor Instruments Inc. CF1.  The software is provided for demonstration
// purposes ONLY and no warranties are expressed or implied.  The software is provide AS IS. 
// Having said that, I hope you find it useful.

//	The hardware needed to support this application is a CF1 and it's prototyping board.
// Also, wired to the prototyping board are two MAX3100 UARTS (I used DIPS).  Each is connected
//	to all the QSPI connections (i.e. MISO,MOSI,SCK).  For the chip selects, one MAX3100 is connected to
// PCS0 (and we'll call it Port 0) and the other is connected to PCS1 (and we'll call it Port 1).
//	The -IRQ line from both MAX3100's is connected to the -IRQ2 line on the CF1 and pulled up 
//	to VREG through a 1 megOhm resistor.  The -SHDN line from both MAX3100's is connected to the 
//	CF1 pin CTS14A (if you need this pin then change it to another).  The -SHDN line from
//	the MAX3100's can also be simply pulled-up if you do not need any control over this signal.
//	The crystal frequency used for the MAX3100's is 3.6864 MHz.  A slower crystal can also be
//	used.  If a slower crystal is used then the configuration word will have to be modified.
//	For this application using a 3.6864 MHz crystal, the baud rate for both parts is set at
//	9600 (8N1).  To use this software, wire the board as described and connect TX from one 
//	MAX3100 to the RX of the other (2 jumpers).  THIS IS JUST FOR TESTING USING THIS SOFTWARE.

// The output of the MAX3100 is a digital signal.  To change these signal to and from RS-232
//	levels, use a chip like the MAX3223 (the MAX3100 is JUST A UART).

// Compile, load and run this software.  The software sends a byte to port 1 using port 0 and
//	sends a different byte to port 0 using port 1 (they play catch).  The software supports interrupt
// driven buffered receive and polled transmit.


//
// This schematic is probably viewed best with a fixed font.
//
//
//
//	                                         +------+-+------+
//	                                        1|      | |      |14
//	       MOSI  ---------------*------------+ Din   -   Vcc +----  VREG
//	                            |           2|               |13
//	       MISO  ------------*--[------------+ Dout       Tx +-----------------------+
//	                         |  |           3|               |12                     |
//	        SCK  ---------*--[--[------------+ SCLK       Rx +--------------------+  |
//	                      |  |  |           4|               |11                  |  |
//	       PCS0  ---------[--[--[------------+ -CS      -RTS +                    |  |
//	                      |  |  |           5|               |10                  |  |
//	      -IRQ2  ------*--[--[--[------------+ -IRQ     -CTS +----- VREG          |  |
//	                   |  |  |  |           6|               |9          22pf     |  |
//	                   |  |  |  |  *---------+ -SHDN      X1 +-----+-----||--+    |  |
//	                   |  |  |  |  |        7|               |8   ###        |    |  |
//	                   |  |  |  |  |   +-----+ GND        X2 +-----+-----||--+    |  |
//	                   |  |  |  |  |   |     |               |  Crystal  22pf|    |  |
//	                   |  |  |  |  |   |     +---------------+   3.6864      |    |  |
//	                   |  |  |  |  |  GND         MAX3100         MHz       GND   |  |
//	                   |  |  |  |  |                                              |  |
//	                   |  |  |  |  |                                              |  |
//	                   |  |  |  |  |                                              |  |
//	                   |  |  |  |  |         +------+-+------+                    |  |
//	                   |  |  |  |  |        1|      | |      |14                  |  |
//	                   |  |  |  +--[---------+ Din   -   Vcc +----  VREG          |  |
//	                   |  |  |     |        2|               |13                  |  |
//	                   |  |  +-----[---------+ Dout       Tx +--------------------+  |
//	                   |  |        |        3|               |12                     |
//	                   |  *--------[---------+ SCLK       Rx +-----------------------+
//	                   |           |        4|               |11
//	       PCS1  ------[-----------[---------+ -CS      -RTS +
//	                   |           |        5|               |10
//	                +--*-----------[---------+ -IRQ     -CTS +----- VREG
//	                |              |        6|               |9          22pf
//	                /           +--+---------+ -SHDN      X1 +-----+-----||--+
//	                \           |           7|               |8   ###        |
//	                /           /      +-----+ GND        X2 +-----+-----||--+
//	        1 MEG   \           \      |     |               |  Crystal  22pf|
//	                /           /      |     +---------------+   3.6864      |
//	                \           \     GND         MAX3100         MHz       GND
//                 |           /
//	       VREG  ---+           \
//	                            |
//	                    VREG ---+




#include	<cf1bios.h>		// Persistor CF1 System and I/O Definitions
#include	<cf1pico.h>		// Persistor CF1 PicoDOS Definitions  

#include	<assert.h>
#include	<ctype.h>
#include	<errno.h>
#include	<float.h>
#include	<limits.h>
#include	<locale.h>
#include	<math.h>
#include	<setjmp.h>
#include	<signal.h>
#include	<stdarg.h>
#include	<stddef.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<string.h>
#include	<time.h>

#include "Scan.h"

//	Global variables (well, global in scope right John!?)
static QPB	*QPB_Slot0 = NULL;	//NULL to start
static QPB	*QPB_Slot1 = NULL;	//NULL to start

ushort ReadData = 0x0000;
ushort ReadConfig = 0x4000;
ushort Config = 0xC40B;				//Just receive interupts on
ushort ConfigTRIntON = 0xCC0B;	//Receive AND transmit interupts on
ushort WriteData;

int GnMoreToSend0;
int GnMoreToSend1;

unsigned char *TopOfRecBuffer0, *HeadRecBuffer0, *TailRecBuffer0;
unsigned char *TopOfRecBuffer1, *HeadRecBuffer1, *TailRecBuffer1;

unsigned char *TopOfXmitBuffer0, *PtrXmitBuffer0;
unsigned char *TopOfXmitBuffer1, *PtrXmitBuffer1;

//	Lets create a device driver specification for our part
static QPBDev Max3100DevStruct0 = 
	{
	COM2_SLOT,				// qslot				our qspi slot (non-multiplexed connection to PCS0 -- tps)
	"Max3100_0\0",			// devName			C string with device name (15 max)
	42500000,				// maxBaud			maximum baud rate in Hz for device
	16,						// bits				Bits Per Transfer
	iaLowSCK,				// clockPolar		SPI Clock Polarity (iaHighSCK,iaLowSCK)
	captLead,				// clockPhase		SPI Clock Phase (captLead/captFall)
	true,						// contCSMulti		Continue CS assert between mult xfrs
	true,						// autoTiming		Auto adjust timing to clock flag
	0,							// psDelaySCK		Min. Delay Before SCK (picoSecs)
	0,							// psDelayTXFR		Min. Delay After Transfer
	0,							// *rcvData			pointer to received data buffer
	0							// xfrCount			words transferred
	};

static QPBDev Max3100DevStruct1 = 
	{
	COM3_SLOT,				// qslot				our qspi slot (non-multiplexed connection to PCS0 -- tps)
	"Max3100_1\0",			// devName			C string with device name (15 max)
	42500000,				// maxBaud			maximum baud rate in Hz for device
	16,						// bits				Bits Per Transfer
	iaLowSCK,				// clockPolar		SPI Clock Polarity (iaHighSCK,iaLowSCK)
	captLead,				// clockPhase		SPI Clock Phase (captLead/captFall)
	true,						// contCSMulti		Continue CS assert between mult xfrs
	true,						// autoTiming		Auto adjust timing to clock flag
	0,							// psDelaySCK		Min. Delay Before SCK (picoSecs)
	0,							// psDelayTXFR		Min. Delay After Transfer
	0,							// *rcvData			pointer to received data buffer
	0							// xfrCount			words transferred
	};


