/*
	XM_RECV.C

	XModem-CRC file transfer example using the SCC chip.

	Data is transferred from a remote device that supports XModem-CRC to
	SCC channel A. Configure the remote device for 9600 bps at 8N1 without
	RTS/CTS flow control.

	Note that any port may be used by this program by changing from SCC
	channel A to the desired port. To use port z1, change aascscc.lib to
	aascz1.lib and then change the parameters in aascOpen() to reflect
	those used for the z1 port.
*/

#use vdriver.lib
#use aasc.lib
#use aascscc.lib

#define BUFSIZE 2048	// Circular buffer size
#define BAUDMULT 8	// x 1200 bits per second ( eg: 8 x 1200 = 9600 baud )

struct _Channel *aascChannel;
char circBufIn[BUFSIZE],circBufOut[BUFSIZE],aascBuffer[BUFSIZE];

int aascInit(void);

void main(void)
{
	// Initialize the aasc framework.
	
	if( !aascInit() ) exit(-1);

	// Process the data transfer.
	
	while(1) {

		hitwd();

		printf("Press any key to initiate XModem Device-to-Controller transfer.\r");
		hitwd();

		if( kbhit() ) {

			getchar();
			printf("\n\nXmodem transfer initiated...\n");
			hitwd();

			// Setup XModem transfer to maximum amount of logical memory.

			aascXMRdInitLog( (unsigned) aascBuffer, 2048);

			// Initialize XModem.
			
			aascReadXModem( aascChannel, aascRdCBackLocLg, aascRdCBackCfmLg, 1 );

			// Perform the XModem transfer.
			
			while( !aascReadXModem( aascChannel, aascRdCBackLocLg, aascRdCBackCfmLg, 0 ) )
				hitwd();

			printf("\n\nXmodem transfer finished...\n\n");
			hitwd();
		}
	}
}

int aascInit(void)
{
	// Initialize the virtual driver

	VdInit();
	
	// Open channel A of the SCC at 8N1
	
	aascChannel = aascOpen( DEV_SCC, 0,
                 SCC_A | SCC_1STOP | SCC_NOPARITY | SCC_8DATA | SCC_1200*BAUDMULT,
                 NULL);

	if(aascChannel==NULL) {
		printf("SCC channel A not available.");
		return;
	}
	
   // Setup the circular buffers.
   
	aascSetReadBuf( aascChannel, circBufIn, sizeof( circBufIn));
	aascSetWriteBuf( aascChannel, circBufOut, sizeof( circBufOut));
}