#include "m2sxxx.h"
#include "drivers/mss_uart/mss_uart.h"

#define ENVM_DEMO_IMAGE_STORED_ADDR 0x20000

/**************************************************************************/
/* printing number on Serial Terminal                                     */
/**************************************************************************/
int printnumber(int n)
    {
    	char ret[100];
    	    int numChars = 0;
    	    int isNegative = 0;
    	    int temp;
    	    // Determine if integer is negative

    	    if (n < 0) {
    	        n = -n;
    	        isNegative = 1;
    	        numChars++;
    	    }
    	    // Count how much space we will need for the string
    	    temp = n;
    	    do {
    	        numChars++;
    	        temp /= 10;
    	    } while ( temp );
    	    ret[numChars] = 0;
    	    //Add the negative sign if needed
    	    if (isNegative) ret[0] = '-';
    	    // Copy digits to string in reverse order
    	    int i = numChars - 1;
    	    do {
    	        ret[i--] = n%10 + '0';
    	        n /= 10;
    	    } while (n);
    		MSS_UART_polled_tx( &g_mss_uart1, (const uint8_t*)ret,numChars);
    		MSS_UART_polled_tx( &g_mss_uart1, (const uint8_t*)"\r\n", 2 );
    		return 0;
    }


size_t
UART_Polled_Rx
(
    mss_uart_instance_t * this_uart,
    uint8_t * rx_buff,
    size_t buff_size
)
{
    size_t rx_size = 0U;

    while( rx_size < buff_size )
    {
       while ( ((this_uart->hw_reg->LSR) & 0x1) != 0U  )
       {
           rx_buff[rx_size] = this_uart->hw_reg->RBR;
           ++rx_size;
       }
    }

    return rx_size;
}

/****************************************************************/
/* Entry to Main form user bootcode                             */
/****************************************************************/
int main()
{
    uint8_t rx_buff[1];
    unsigned long data;
    unsigned int byte[4];
	/* Initialization all necessary hardware components */
    MSS_UART_init( &g_mss_uart1,MSS_UART_57600_BAUD,MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT );
    MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"#### Welcome to SmartFusion2 ####\n\r",sizeof("#### Welcome to SmartFusion2 ####\n\r"));

  	while(1)
  	{
  	    MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"Please Select the Following option\n\r",sizeof("Please Select the Following option\n\r"));
  	    MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"1.Factorial of 2\n\r",sizeof("1.Factorial of 2\n\r"));
  	    MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"2.Factorial of 3\n\r",sizeof("2.Factorial of 3\n\r"));
  	    MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"3.Factorial of 4\n\r",sizeof("3.Factorial of 4\n\r"));

  		while( (!(UART_Polled_Rx ( &g_mss_uart1, rx_buff, 1 ))) )
			;

		data = *(unsigned int *)ENVM_DEMO_IMAGE_STORED_ADDR;

		byte[3] = data & 0xFF; /* 0 -7 */
		byte[2] = ((data >>8) & 0xFF); /* 8 -15 */
		byte[1] = ((data >>16) & 0xFF); /* 16 - 23 */
		byte[0] = ((data >>24) & 0xFF); /* 24 -31   */

  		if(rx_buff[0] == '1')
	    {
			  if (byte[3] == 0x32)
			  {
				  MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"Factorial of 2:\n\r",sizeof("Factorial of 2:\n\r"));
				  printnumber(2);
			  }
	    }
	    else if(rx_buff[0] == '2')
	    {
			  if (byte[2] == 0x36)
			  {
				  MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"Factorial of 3:\n\r",sizeof("Factorial of 3:\n\r"));
				  printnumber(6);
			  }
	    }
	    else if(rx_buff[0] == '3')
	    {
			  if ((byte[1] == 0x32) && (byte[0] == 0x34))
			  {
				  MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"Factorial of 4:\n\r",sizeof("Factorial of 4:\n\r"));
				  printnumber(24);
			  }
	    }
	    else
	    {
	  	    MSS_UART_polled_tx(&g_mss_uart1,(uint8_t *)"Incorrect option\n\r",sizeof("Incorrect option"));
	    }
  	}

    return 0;
}

