//sendbyte.c
//
//Program to send one byte via the external USART
//
//Derek Marston and Peter Susi
//4/26/03

#include "common.h"
#include "iob32.h" //all IO and memory names

//	Authorize interrupts.

#define cli()	_asm("andcc #$EF")

#define TDRE	0x80	/* transmit ready bit */
//#define TransmitRdy  PortA.b.b4


void main(void)
{

//load stack pointer...can be done in linker file

	ConfigPorts();
	ConfigUSART();
	ConfigSCI();
	cli();


	for(;;)
	{
		Transmit(0xAA);		//SendFree();
			//infinite loop
	}
}

//Configure Direction Registers
void ConfigPorts()
{
	DDRA = 0x8F;
	/*
	pin 0 = USART reset
	pin 1 = Read'
	pin 2 = Write'
	pin 3 = command/data'

	pin 4 = TxRDY
	pin 5 = RxRDY
	pin 6 = unused

	pin 7 = output square wave for debug purposes

	*/

	DDRB = 0xFF; //PORTB is command/status/data register of USART
	//set to output to initialize USART

}

/*Configure 8251 USART for:
;		Asyncronous serial data
;		Baud Rate Factor = 16
;		8Bit data, no parity
;		1 stop bit
;		Enable transmitter
*/
void ConfigUSART()
{
	PORTB = 0x4E; //send initialization code to USART

	PORTA = 0x07; //set pin 0 high for USART hardware reset

	wait();

	PORTA = 0x06; //end reset pulse

	PORTA = 0x0E; //set USART ready for COMMAND
	PORTA = 0x0A; //Set USART ready for WRITE 
	PORTA = 0x0E; //end write pulse

	wait();

	PORTB = 0x05; //send second byte of initialization code

	PORTA = 0x0E; //set USART ready for COMMAND
	PORTA = 0x0A; //Set USART ready for WRITE 
	PORTA = 0x0E; //end write pulse

	wait();

	PORTA = 0x06; //set USART to idle mode}
}

//delay a few clock cycles
//
void wait(void)
{
	int count;
	for (count = 0; count < 4; count++)
	{}
}


//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(char tosend)
{
//	BIT_CHAR temp;
//	temp.byte = SC0SR1;
//	while (!temp.b.b7)
//	{}

	while (!(SC0SR1 & TDRE))  //TDRE is defined above
	{}	

	SC0DRL = tosend;
}	

// Reads last byte from the Data register and returns it.

char GetKey()
{
	return SC0DRL;
}

//Transmit toTx via external USART (connected to PortB)
void Transmit(char toTx)
{

	while (!(PORTA & BIT4))  //waits until USART is ready to transmit
	{
	}

	DDRB = 0xFF;  //set PORTB to output on all pins

	PORTB = toTx;  //set data on USART's data bus via portB
	

	PORTA = 0x02; //Set USART ready for WRITE data
	PORTA = 0x06; //end write pulse


/*
	PORTA = (PORTA | ~BIT2);//activates Write signal on USART 

	PORTA = (PORTA & BIT2);  //deactivates write signal
*/
//	wait();		//kills time to let USART use data

}

//send a Free Token (AA FF AA)
void SendFree(void)
{
	Transmit(0xAA);
	Transmit(0xFF);
	Transmit(0xAA);
}

// 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;
*/


}


//Interrupt Routine triggered on RxRdy (tied to IRQ, or PE1)
// to receive network data.
//
@interrupt void _IRQ_ISR(void)
{
	// generate square wave on interrupt

	square();


/*
	BIT_CHAR nextByte;

	recFlag = 0x0;
	nextByte = Receive();
	if (nextByte == 0xAA)    //can C do "if ((nextByte = Receive()) == 0xAA)?
	{
		
	}


*/


}

//Generates a square wave on all pins of Port A.  Use for debugging.
//
void square(void)
{
	DDRA = 0xFF;

	for(;;) //generate a square wave on all pins of Port A
	{
		PORTA = 0x00;	//turn bit off
		wait();

		PORTA = 0xFF; //(PORTA & BIT7);	//turn bit on
		wait();
	}	

}


