/*******************************************************************************
* Company: Actel Corporation
*
* File: main.c
* File history:
*      Revision: 1.0 Date: June 07, 2010
*
* Description:
*
* This sample program is targeted to the SmartFusion Development Kit. It fills
* the External SRAM with known pattern at 0x70000000 and reads back the pattern
* on HyperTerminal.
*
* Author: Upender Cherukupally
*         Upender.Cherukupally@actel.com
*         Corporate Applications Engineering
*
*******************************************************************************/

#include <stdio.h>

#include "drivers/mss_uart/mss_uart.h"
#include "drivers/mss_watchdog/mss_watchdog.h"
#include "BSP/emc_driver/nor.h"

/* Setting the base address for EMC SRAM*/
#define EXT_RAM_BASE_ADDR    0x70000000

/* Setting the base address for EMC NOR Flash*/
#define EXT_FLASH_BASE_ADDR  0x74000000

static void emc_sram_wr(void);
static void emc_nor_wr(void);


int main()
{
    /* Watchdog Disabling function*/
    MSS_WD_disable();
    
    emc_sram_wr();
    emc_nor_wr();
	return 0;
}

static void emc_sram_wr(void)
{
	unsigned long * buff = (unsigned long *)EXT_RAM_BASE_ADDR;
    int size;

    printf("********************************************\n\r");
    printf("     Accessing External SRAM Using EMC      \n\r");
    printf("********************************************\n\r\n\r");

    /* Writing 0xFFFFFFFF to first 16 Memory locations*/
    for( size = 0; size < 16; size++)
    {
    	buff[size] = 0xFFFFFFFF;
    }

    /* Writing 0,4,8,C,10.. pattern to first 10 Memory locations*/
	for( size = 0; size < 10; size++)
	{
		buff[size] = size*4;
	}

	printf("  ADDRESS          DATA\n\r");
	printf("--------------------------------------------\n\r");
	
	/* Reading data from first 16 Memory locations and prints on HyperTerminal*/
	for( size = 0; size < 16; size++)
    {
    	printf("%p  ->   0x%08x \n\r", buff+size, ( unsigned int)buff[size]);
    }
    return;
}


static void emc_nor_wr(void)
{
	uint8_t Writebuff[64];
	uint8_t Readbuff[64];
	int32_t size;
	
    printf("\n\r\n\r********************************************\n\r");
    printf("   Accessing External NOR Flash Using EMC   \n\r");
    printf("********************************************\n\r\n\r");
 
    /* EMC Initialization*/
    emc_init();
    
    printf("Erasing the content of NOR Flash ..\n\r");   
    
    /*Erases the content of EMC NOR Flash */
    emc_flash_chip_erase(EXT_FLASH_BASE_ADDR);

    /* Writing 0,4,8,C,10.. pattern to Writebuff buffer*/
	for( size = 0; size < 64; size++)
	{
		Writebuff[size] = size;
	}
    
	/* Writing the content of Writebuff buffer into NOR Flash */
	emc_flash_write(EXT_FLASH_BASE_ADDR,Writebuff,size);
	
	/* Reading from NOR Flash and storing into Readbuff buffer  */
	emc_flash_read(EXT_FLASH_BASE_ADDR,Readbuff,size);
	
	printf("\n\r  ADDRESS        DATA\n\r");
	printf("--------------------------------------------\n\r");
	
	/* Reading data from Readbuff buffer and prints on HyperTerminal*/
	for( size = 0; size < 64; size++)
    {
    	printf("%p  ->   0x%x \n\r", (void *)(EXT_FLASH_BASE_ADDR+size),Readbuff[size]);
    }
    return;
}



