/**********************************************************
	hdlc_test.c
 	Z-World, 2002

	This program is used with Rabbit 3000 based controllers.
	
	Description
	===========
	This program demonstrates a simple loopback test on
	serial port E in HDLC mode.

	Instructions
	============
	1. Simply connect PG6 (TXE) to PG7 (RXE).
	2. Compile and run this program.
	3. Packets are sent out	and received on the same port.

*************************************************************/
#use hdlc_packet.lib

#define BUFFER_COUNT 4
#define BUFFER_SIZE 100
#define BAUD_RATE 100000L

main()
{
	char tx_packet[100];
	char rx_packet[100];
	int packet_size;
	unsigned long xbuffers;
	char errors;
	int counter;
	unsigned long t;
	int temp_flags;
	int temp_length;
	int i;
	int recv_count;
	unsigned long peekaddr;
	int peeklen;
				
	xbuffers = xalloc(BUFFER_COUNT * (BUFFER_SIZE + 4));
	HDLCopenE(	BAUD_RATE,
					HDLC_NRZ,
					xbuffers,
					BUFFER_COUNT,
					BUFFER_SIZE );
	counter = 1;
	recv_count = 0;

	while(1)
	{
		if(HDLCsendingE() == 0)
		{
			for(i = 0;i < 10;i++)
			{
				tx_packet[i] = '0' + (counter % 10);
			}
			tx_packet[10] = 0;

			strcpy(tx_packet, "CatMouse987654321");
			printf("Sending packet '%s', length:%d\n", tx_packet, strlen(tx_packet));
			
			if(HDLCsendE(tx_packet, strlen(tx_packet)) == 1)
			{
				counter++;
			}
			else
			{
				printf("Packet not sent\n");
			}
		}
				
		errors = HDLCerrorE();
		if(errors)
		{
			printf("ERROR 0x%x\n", errors);
		}

		//use the more efficient peek method
		if(HDLCpeekE(&peekaddr, &peeklen))
		{
			xmem2root(rx_packet, peekaddr, peeklen);
			packet_size = peeklen;
			HDLCdropE();
		}
		else
		{
			packet_size = 0;
		}
							
		if(packet_size > 0)
		{
			rx_packet[packet_size] = 0;
			printf("Got packet(%d): '%s'\n", packet_size, rx_packet);	 	
		}
	}
}
						

