//hyperterm.c
//
//Program to communicate with Hyperterminal
//
//Derek Marston and Peter Susi
//4/04/03

#include "common.h"
#include "iob32.h" //all IO and memory names

/*	Authorize interrupts.
 */
#define cli()	_asm("andcc #$EF")

//#define SIZE	512	/* buffer size */
#define TDRE	0x80	/* transmit ready bit */


//char buffer[SIZE];	/* reception buffer */
//char *ptlec;		/* read pointer */
//char *ptecr;		/* write pointer */


void main(void)
{

//load stack pointer...can be done in linker file

	ConfigSCI();
	cli();

	for (;;)
	{}
	
}

//Configures port for Asynchronous serial 8-bit data, no parity, 1 stop bit
//enable the transmitter and receiver
void ConfigSCI()
{
	
	SC0CR1 = 0x00; 	
 		// enable Tx, Rx and RxInterrupt

	SC0CR2 = 0x2C;

	SC0BDL = 0x34;	//BR divisor for 9600 BPS

}

//Waits for the transmitter to be ready (until TDRE bit in the Status Register is 1)
//then writes the contents of the passed parameter (one byte) to the Data Register
void CharOut(BIT_CHAR tosend)
{
//	BIT_CHAR temp;
//	temp.byte = SC0SR1;
//	while (!temp.b.b7)
//	{}

	while (!(SC0SR1 & TDRE))  //TDRE is defined above
	{}	

	SC0DRL = tosend.byte;
}	

// Reads last byte from the Data register and returns it.

BIT_CHAR GetKey()
{
	BIT_CHAR temp;
	temp.byte = SC0DRL;
	return temp;
}


// Interrupt Routine for recieving Hyperterminal bytes
//


@interrupt void _SCIRx_ISR(void)
	{
          SC0SR1; // read Status Reg to clear interrupt 	

	  SC0DRL = SC0DRL;    // Echo back byte
/*
	*ptecr++ = SC0DRL;		// get the char 
	if (ptecr >= &buffer[SIZE])	// put it in buffer
		ptecr = buffer;
*/

	}


