/****************************************************************************
* Company: Microsemi Corporation
*
* File: main.c
* File history:
*     Revision: Initial version   Date: May 04, 2010
*
* Description:
*             This main functions is used to provide the user interface
*             to enter the data, which needs to be written to EEPROM
* Author: Pavan Marisetti
*         pavan.marisetti@microsemi.com
*         Corporate Applications Engineering
****************************************************************************/

#include "drivers/mss_watchdog/mss_watchdog.h"
#include "drivers/mss_uart/mss_uart.h"
#include "BSP/EEPROM_Driver/EEPROM.h"


/*==============================================================================
 * main function.
 */
int main()
{
    uint8_t intial_text[] = "This Design Example demonstrates Simple EEPROM Write/Read \n\r";
    uint8_t init_write_text[] = "Please type the data to be written on to the EEPROM and press Enter\n\r";
    uint8_t write_success_text[] = "Data is successfully written into EEPROM\n\r";
    uint8_t read_text[] = "Data read from EEPROM: ";
    uint8_t rx_buff[1]={0};
    uint8_t rx_size = 0;
    uint16_t size = 0;
    uint16_t start_address=0;
    uint8_t write_data[1024];
    uint8_t read_data[1024];
    MSS_WD_disable();
    /* Initialize and configure UART0. */
    MSS_UART_init( &g_mss_uart0,
                    MSS_UART_57600_BAUD,
                    MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT );

    /* Printing Initial text to UART 0*/
    MSS_UART_polled_tx( &g_mss_uart0, intial_text, sizeof(intial_text) );
    while(1)
    {
        /* Initialisation of EEPROM(I2C 1 Initialisation)*/
        EEPROM_init();
        MSS_UART_polled_tx( &g_mss_uart0,init_write_text , sizeof(init_write_text) );
        /* Receiving the data from UART terminal*/
        while(*rx_buff!= '\n')
        {
            rx_size = MSS_UART_get_rx ( &g_mss_uart0, rx_buff, sizeof(rx_buff) );
            if((rx_size > 0))
            {
                write_data[size] = *rx_buff;
                size++;
            }
        }
        /* Wrting data to EEPROM */
        EEPROM_write(start_address,size,write_data);
        MSS_UART_polled_tx( &g_mss_uart0,write_success_text , sizeof(write_success_text));
        /* reading data from EEPROM */
        EEPROM_read(start_address,size,read_data);
        MSS_UART_polled_tx( &g_mss_uart0,read_text , sizeof(read_text));
        MSS_UART_polled_tx( &g_mss_uart0,read_data , size);
        *rx_buff = NULL;
        size = 0;
    }
    return(0);
}
