/***********************************************************************************************
* Company: Microsemi Corporation
*
* File: main.c
* File history:
*      Revision: 1.0 Date: November 8, 2012
*
* Description:
*
*	SmartFusion2: Initializing SRAM from eNVM using Cortex-M3
*
* Author: Corporate Applications Engineering
*
************************************************************************************************/
#include <stdio.h>
#include "drivers/mss_uart/mss_uart.h"

#define SRAM_BASE_ADDR 0x30000000
#define eNVM_BASE_ADDR 0x60000000
#define no_of_data_words 16
#define SRAM_depth 64

void nvm_access (void);

/*-------------------------------------------------------------------------*//**
 * main() function.
 */
int main()
{
  const uint8_t greating1[] = "\r\n*****SmartFusion2: Start SRAM Initialization*****\r\n";
  const uint8_t greating2[] = "\r\n*****SmartFusion2: SRAM Initialization done*****\r\n";
  const uint8_t seperator[] = "\r\n***************************************\r\n";

	  MSS_UART_init( &g_mss_uart0, MSS_UART_57600_BAUD, MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT );
	  MSS_UART_polled_tx( &g_mss_uart0, greating1, sizeof(greating1) );
	  MSS_UART_polled_tx( &g_mss_uart0, seperator, sizeof(seperator) );
	  nvm_access ();
	  MSS_UART_polled_tx( &g_mss_uart0, seperator, sizeof(seperator) );
	  MSS_UART_polled_tx( &g_mss_uart0, greating2, sizeof(greating2) );
	  while (1)
	    {
	    }

}
/*-------------------------------------------------------------------------*/

//***************************************************************
void nvm_access (void)
{
  uint32_t *nvm_word_ptr, read_data;
  uint32_t *fab_sram_word_ptr;
  volatile int i, k;

  uint8_t fab_sram_word[4] = "0000";
  const uint8_t greating6[] = "\r\n Reading from eNVM...\r\n";
  const uint8_t greating7[] = "\r\n Writing to SRAM...\r\n";
  const uint8_t greating8[] = "\r\n Reading SRAM...\r\n";

  uint8_t tim_val[] = "00000000";
  int j;

  		  nvm_word_ptr = (uint32_t *)eNVM_BASE_ADDR;
  		  fab_sram_word_ptr = (uint32_t *)SRAM_BASE_ADDR;

		  for( i = 0; i< (no_of_data_words); i++)
		  {
			//Read the eNVM content
			read_data = *nvm_word_ptr;

			MSS_UART_polled_tx( &g_mss_uart0, greating6, sizeof(greating6) );
			j = sprintf(tim_val, "%8X", nvm_word_ptr);
			MSS_UART_polled_tx( &g_mss_uart0, tim_val, sizeof(tim_val) );
		    j = sprintf(tim_val, "%8X", read_data);
		    MSS_UART_polled_tx( &g_mss_uart0, tim_val, sizeof(tim_val) );

		    //convert 32 bit to 8 bit
		    fab_sram_word[0] = read_data;
		    fab_sram_word[1] = read_data >> 8;
		    fab_sram_word[2] = read_data >> 16;
		    fab_sram_word[3] = read_data >> 24;

				for	( k = 0; k< 4; k++)
					  {
						//Write each 8 bit to SRAM
						*fab_sram_word_ptr = fab_sram_word[k];

					    MSS_UART_polled_tx( &g_mss_uart0, greating7, sizeof(greating7) );
					    j = sprintf(tim_val, "%8X", fab_sram_word_ptr);
					    MSS_UART_polled_tx( &g_mss_uart0, tim_val, sizeof(tim_val) );
					    j = sprintf(tim_val, "%8X", fab_sram_word[k]);
					    MSS_UART_polled_tx( &g_mss_uart0, tim_val, sizeof(tim_val) );
					    MSS_UART_polled_tx( &g_mss_uart0, greating8, sizeof(greating8) );
					    read_data = *fab_sram_word_ptr;
					    j = sprintf(tim_val, "%8X", fab_sram_word_ptr);
					   	MSS_UART_polled_tx( &g_mss_uart0, tim_val, sizeof(tim_val) );
					   	j = sprintf(tim_val, "%8X", read_data);
					   	MSS_UART_polled_tx( &g_mss_uart0, tim_val, sizeof(tim_val) );

					    fab_sram_word_ptr = fab_sram_word_ptr + 1;
		  		}

		    nvm_word_ptr = nvm_word_ptr + 1;
		  }
}

