/*
	BL17SCC.C
	
	Use with BL1700 boards.

	This sample program will send bytes to another controller via RS485,
	or it can receive bytes from another controller via RS485 and echo
	them out via RS232.

	If receiving bytes by RS485 from SCC port B, characters are echoed
	to a terminal program (eg: Windows(R) Terminal) through RS232
	SCC port A.

	Instructions:
	Install jumpers on J2, pins 8-10; J7, pins 5-6, 7-8; J8, pins 3-5.
	Connect H15 (Port B) 485+ and 485- to another controller.  Connect 485+
	terminals together and connect 485- terminals together.
	If you are using two BL1700's, make one the sender by defining
	SENDRECV as SEND. Compile and run this program. Pull the program jumper.

	Connect a serial cable to H14 (Port A) to an unused Com Port on your PC.
	Establish communications with Windows Terminal with settings 9600,N,8,1
	and local echo disabled.

	Program the other board by defining SENDRECV as RECV. Compile and run
	this program. Reset the other board.

	You should see the alphabet continuously displayed	in Windows Terminal.
*/

#use aasc.lib
#use aascscc.lib
#use eziobl17.lib

#define BUFSIZE  32	// Circular buffer size
#define BAUDMULT 8	// x 1200 bits per second ( eg: 8 x 1200 = 9600 baud )
#define SEND	1
#define RECV	0
#define ENABLE	1
#define DISABLE 0

#define SENDRECV SEND
#define SCC_CH SCC_B

int i, j;
char readBuffer1[BUFSIZE],writeBuffer1[BUFSIZE],workBuffer[BUFSIZE];
char readBuffer2[BUFSIZE],writeBuffer2[BUFSIZE];
char alphabet[] = "abcdefghijklmnopqrstuvwxyz\n\r";

struct _Channel *aascChannel1,*aascChannel2;

main()
{
	_GLOBAL_INIT(); 			// This must be the first action performed in main().
	switchLED(ENABLE);		//turn on LED
	
	// Open a channel to the SCC ( scc channel B at 8N1 with rs485 driver ).
	aascChannel1 = aascOpen( DEV_SCC, 0,
                 		SCC_B |
                 		SCC_1STOP |
                 		SCC_NOPARITY |
                 		SCC_8DATA |
                 		SCC_1200*BAUDMULT,
                 		NULL);

	if( aascChannel1==NULL ) return -1;
	
	// Open a channel to the SCC ( scc channel A at 8N1 with rs232 driver ).
	aascChannel2 = aascOpen( DEV_SCC, 0,
                 		SCC_A |
                 		SCC_1STOP |
                 		SCC_NOPARITY |
                 		SCC_8DATA |
                 		SCC_1200*BAUDMULT,
                 		NULL);

	if( aascChannel2==NULL ) return -1;

   // Setup the circular buffers.
	aascSetReadBuf( aascChannel1, readBuffer1, sizeof( readBuffer1));
	aascSetWriteBuf( aascChannel1, writeBuffer1, sizeof( writeBuffer1));

	aascSetReadBuf( aascChannel2, readBuffer2, sizeof( readBuffer2));
	aascSetWriteBuf( aascChannel2, writeBuffer2, sizeof( writeBuffer2));

	// Enable or disable the rs485 transmitter.
	if( SENDRECV == SEND )
		sccSw485(SCC_CH, ENABLE); 			// Enable the 485 transmitter.
	else
		sccSw485(SCC_CH, DISABLE); 		// Disable the 485 transmitter (to receive only).

	// Process the data transfer.
	i = 0;
	while(1) {
		hitwd();
		if( SENDRECV ) {
			//send one character at a time
			while( !aascWriteChar( aascChannel1, alphabet[i]) ) {
				hitwd();
			}
			++i;
			if( i>27 ) i=0;
		}
		else {		
			//read the character
			if( aascReadBlk( aascChannel1, workBuffer, 26, 1) ) {
				//echo it to the other port
				while( !aascWriteBlk( aascChannel2, workBuffer, 26, 1) ) {
					hitwd();
				}
			}
		}
	}
}

