/*
 * functions.h
 *
 *  Created on: Sep 7, 2010
 *      Author: karugantip
 */

#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_

/* Function to insert delay between each sample */
/*void delay(int i)
{
	int temp = 0;
	while (temp < i)
	temp++;
}*/

void delay(int i)
{
	int j;
for( j = 0; j < i; j++){
	__ASM volatile ("NOP");
	}
}

/*Function to convert ASCII to HEX*/
unsigned char ascii_to_hex (unsigned char ascii_input)
{
    unsigned char hex_output;

    if ( (ascii_input >= 0x30) && (ascii_input <= 0x39) )
    {
          hex_output = ascii_input - 0x30;
    }

    else
       if ( (ascii_input >= 0x41) && (ascii_input <= 0x46) )
       {
          hex_output = ascii_input - 0x37;
       }
       else
          if ( (ascii_input >= 0x61) && (ascii_input <= 0x66) )
          {
              hex_output = ascii_input - 0x57;
          }
          else
              hex_output = 0xff;


    return (hex_output);
}
/* Function to read the input character */
uint8_t readchar (void)
{
	uint8_t rx_size = 0;
	uint8_t rx_char;
	do {
		rx_size = MSS_UART_get_rx( &g_mss_uart0, &rx_char, sizeof(rx_char) );
		}while ( rx_size == 0);
	return(rx_char);
}

uint8_t readbyte (void)
{
   unsigned char write_data;
   uint8_t write_data_int;
   write_data = readchar();

   write_data_int = 0;
     write_data_int = (uint8_t) (ascii_to_hex (write_data));
   return(write_data_int);
}

uint16_t readhword (void)
{
   unsigned char write_data[2];
   uint16_t write_data_int;
   write_data[0] = readchar();
   write_data[1] = readchar();

   write_data_int = 0;
     write_data_int = (uint16_t) (ascii_to_hex (write_data[0]));
     write_data_int = write_data_int << 4;

   write_data_int = (uint16_t) (ascii_to_hex (write_data[1]) + write_data_int);

   return(write_data_int);
}
uint32_t readword (void)
{
  int i = 0;
   unsigned char write_data[4];
   uint32_t write_data_int;
   write_data[0] = readchar();
   write_data[1] = readchar();
   write_data[2] = readchar();
   write_data[3] = readchar();

   write_data_int = 0;

   for (i = 0; i<3; i++) {
     write_data_int = (uint32_t) (ascii_to_hex (write_data[i]) + write_data_int);
     write_data_int = write_data_int << 4;
   }

   write_data_int = (uint32_t) (ascii_to_hex (write_data[3]) + write_data_int);

   return(write_data_int);
}
/* Function to send character */
// UART0 - convert byte into the 1-hexadecimal character and send
void sendnibble (unsigned char txdata)
{
	unsigned char txdata_nibble;
		// convert nibble
		txdata_nibble = txdata & 0x0f;
		if(txdata_nibble<=9)
		txdata_nibble =	(txdata_nibble|0x30);
		else
		txdata_nibble = ((txdata_nibble-9) | 0x40);
		MSS_UART_polled_tx(&g_mss_uart0, &txdata_nibble, sizeof(txdata_nibble));
}
// UART0 - convert byte into the 2-hexadecimal characters and send
void sendbyte (unsigned char txdata)
{
	unsigned char txdata_nibble;
		// most significant nibble
		txdata_nibble = txdata & 0xf0;
		txdata_nibble = (txdata_nibble >> 4);
		sendnibble(txdata_nibble);
		// least significant nibble
		txdata_nibble = txdata & 0x0f;
		sendnibble(txdata_nibble);
}
void senddouble (uint16_t doub)
{
		// next MSB
		sendbyte (doub>>8 & 0x00FF);
		// next MSB
		sendbyte (doub & 0x00FF);
}
// UART0 - convert word into the 4-hexadecimal characters and send
void sendword (uint32_t word)
{
		// MSB
		sendbyte (word>>24 & 0x000000FF);
		// next MSB
		sendbyte (word>>16 & 0x000000FF);
		// next MSB
		sendbyte (word>>8 & 0x000000FF);
		// next MSB
		sendbyte (word & 0x000000FF);
}
#endif /* FUNCTIONS_H_ */
