/***********************************************************************************************
* Company: Microsemi Corporation
*
* File: main.c
* File history:
*      Revision: 1.0 Date: November 8, 2012
*
* Description:
*
*	Program to blink the LED's on the SmartFusion2 Development Kit. It also prints the status
*	messages on the Serial Console. This is intended for creating the sample application image  
*	to use in the Code Shadowing Demo's
*
* Author: Corporate Applications Engineering
*
************************************************************************************************/

#include "drivers/mss_gpio/mss_gpio.h"
#include "drivers/mss_uart/mss_uart.h"
#include "drivers/mss_timer/mss_timer.h"
#include "CMSIS/m2sxxx.h"
extern uint32_t g_FrequencyPCLK0;

void Timer1_IRQHandler( void )
{
	int temp = 0;
	printf("########DDR timer1 hit %08x #########\n",&temp);

	MSS_TIM1_clear_irq();
}
void GPIO11_IRQHandler(void)
{

	MSS_UART_polled_tx_string(&g_mss_uart1,(const uint8_t *)"@@@@@@@ MSS_GPIO_11 interrupt@@@@@@@@@\n");
	MSS_GPIO_clear_irq(MSS_GPIO_11);
}
void GPIO12_IRQHandler(void)
{

	MSS_UART_polled_tx_string(&g_mss_uart1,(const uint8_t *)"******MSS_GPIO_12 interrupt *******\n");
	MSS_GPIO_clear_irq(MSS_GPIO_12);
}

void delay(int n);

/*-------------------------------------------------------------------------*//**
 * main() function.
 */
int main()
{
     uint32_t count,MDDR_status=0;
     delay(2000);
     MSS_UART_init( &g_mss_uart1,MSS_UART_115200_BAUD,MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT );
    printf("**********Welcome to SmartFusion2**********\n");

    /*
     * Initialize MSS GPIOs.
     */
    MSS_GPIO_init();
    MSS_TIM1_init(MSS_TIMER_PERIODIC_MODE);
    	 MSS_TIM1_load_immediate(g_FrequencyPCLK0*5);
    	MSS_TIM1_start();
    	MSS_TIM1_enable_irq();
    /*
     * Configure MSS GPIOs.
   */
    MSS_GPIO_config( MSS_GPIO_0 , MSS_GPIO_OUTPUT_MODE );
    MSS_GPIO_config( MSS_GPIO_1 , MSS_GPIO_OUTPUT_MODE );
    MSS_GPIO_config( MSS_GPIO_2 , MSS_GPIO_OUTPUT_MODE );
    MSS_GPIO_config( MSS_GPIO_3 , MSS_GPIO_OUTPUT_MODE );
    MSS_GPIO_config( MSS_GPIO_4 , MSS_GPIO_OUTPUT_MODE );
    MSS_GPIO_config( MSS_GPIO_8 , MSS_GPIO_OUTPUT_MODE );
    MSS_GPIO_config( MSS_GPIO_9 , MSS_GPIO_OUTPUT_MODE );
    MSS_GPIO_config( MSS_GPIO_10 , MSS_GPIO_OUTPUT_MODE );
    MSS_GPIO_config( MSS_GPIO_11 , MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_POSITIVE );
    MSS_GPIO_config( MSS_GPIO_12 , MSS_GPIO_INPUT_MODE | MSS_GPIO_IRQ_EDGE_NEGATIVE );

    delay(100);
    MSS_GPIO_enable_irq(MSS_GPIO_11);
    MSS_GPIO_enable_irq(MSS_GPIO_12);
    delay(100);
    MDDR_status = SYSREG->MDDR_CR;
    if((MDDR_status&0x2) == 0x2)
    {
    	printf("code is running from SDR SDRAM memory\n");
        printf("SDR SDRAM memory is re mapped to the 0x00000000 address of MSS memory space\n");
    }
        else

        	{
        	printf("code is running from DDR SDRAM memory\n");
        	printf("DDR SDRAM memory is re mapped to the 0x00000000 address of MSS memory space\n");
        	}
    /*
     * Set initial delay used to blink the LED.
     */
    count=0;
    printf("LED pattern counter is started\n");
    while(1)
    {
      printf("count value = %d,stored at 0x%08x address\n",count,&count);
      count++;
      MSS_GPIO_set_output(MSS_GPIO_0, 0);
      MSS_GPIO_set_output(MSS_GPIO_10, 0);
      delay(50000);
      MSS_GPIO_set_output(MSS_GPIO_0, 1);
      MSS_GPIO_set_output(MSS_GPIO_10, 1);
      delay(50000);
      MSS_GPIO_set_output(MSS_GPIO_1, 0);
      MSS_GPIO_set_output(MSS_GPIO_9, 0);
      delay(50000);
      MSS_GPIO_set_output(MSS_GPIO_1, 1);
      MSS_GPIO_set_output(MSS_GPIO_9, 1);
      delay(50000);
      MSS_GPIO_set_output(MSS_GPIO_2, 0);
      MSS_GPIO_set_output(MSS_GPIO_8, 0);
      delay(50000);
      MSS_GPIO_set_output(MSS_GPIO_2, 1);
      MSS_GPIO_set_output(MSS_GPIO_8, 1);
      delay(50000);
      MSS_GPIO_set_output(MSS_GPIO_3, 0);
      MSS_GPIO_set_output(MSS_GPIO_4, 0);
      delay(50000);
      MSS_GPIO_set_output(MSS_GPIO_3, 1);
      MSS_GPIO_set_output(MSS_GPIO_4, 1);
    }
    return 0;
}
void delay(int n)
{
  while(n>0)
    n--;
}
