/***********************************************************************************************
* Company: Microsemi Corporation
*
* File: main.c
* File history:
*      Revision: 1.0 Date: April 19, 2010
*
* Description:
*
* Application program to capture the interrupts from the High-Speed Timer
*
* Author: Sathish Odiga
*         sathish.odiga@microsemi.com
*         Corporate Applications Engineering
*
************************************************************************************************/

#include <stdio.h>

#include "mss_uart.h"

/* High-Speed Timer Registers Addresses */
#define TIMERLOADA ((volatile unsigned long *)(0x40050000))
#define TIMERVALUEA ((volatile unsigned long *)(0x40050004))
#define TIMERCONTROLA ((volatile unsigned long *)(0x40050008))
#define TIMERPRESCALEA ((volatile unsigned long *)(0x4005000C))
#define TIMERINTCLRA ((volatile unsigned long *)(0x40050010))
#define TIMERTIMINT1 ((volatile unsigned long *)(0x40050014))
#define TIMERTIMINT2 ((volatile unsigned long *)(0x40050018))

volatile int timer_intr_flag;

/* Function to read the character */
char readchar (void)
{
	uint8_t rx_size = 0;
	unsigned char rx_char;
	do {
		rx_size = MSS_UART_get_rx( &g_mss_uart0, &rx_char, sizeof(rx_char) );
		}while ( rx_size == 0);
	return(rx_char);
}

/* Fabric Interrupt Handler */
void Fabric_IRQHandler( void )
{
	timer_intr_flag = 1;

	/* Clearing the pending Interrupt by
	 * writing to Timer Interrupt Clear Register*/
	*TIMERINTCLRA = 0x1;

	NVIC_ClearPendingIRQ( Fabric_IRQn );
}

/*==============================================================================
 * main function.
 */
int main()
{
	int get_val = 0;
	volatile char key;
	const uint8_t pattern[] = "\r1-One-shot mode\n\r2-Continuous mode\n\r3-Switch Start\n\r"
								  "Choose a mode (1/2/3): ";

    /*--------------------------------------------------------------------------
     * Disable watchdog.*/
    MSS_WD_disable();

    /*--------------------------------------------------------------------------
     * Initialize and configure UART0 and greeting message over the UART.*/
    MSS_UART_init( &g_mss_uart0, MSS_UART_57600_BAUD, MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT );
    printf("\n\r/***********SmartFusion High-Speed Timer***********/\n\r");

    NVIC_EnableIRQ(Fabric_IRQn);

    /* Read Initial Values.*/
	get_val = (uint32_t) *TIMERVALUEA;
    printf("Timer Initial Value : %x\n\r", get_val);

    /*--------------------------------------------------------------------------
      * Set up Fab Timer registers.*/
    *TIMERLOADA      = 0x007FFFFF;
	*TIMERPRESCALEA  = 0x00000000;

	/* Read timer Loaded Values.*/
	get_val = *TIMERVALUEA;
    printf("Timer Loaded Value : %x\n\r", get_val);

    /* Clear the pending timer interrupts */
    *TIMERINTCLRA = 0x1;

    MSS_UART_polled_tx_string(&g_mss_uart0, pattern);
	key = readchar();
    switch(key)
    {
    case ('1'):
    	// Start the timer in one-shot mode
    	*TIMERCONTROLA   = 0x00000007;
    break;
    case ('2'):
    	// Start the timer in Continuous mode
    	*TIMERCONTROLA   = 0x00000003;
    break;
    case ('3'):
    	// Start the timer using Switch (SW1)
    	*TIMERCONTROLA   = 0x00000002;
    	printf("\rPress SW1 to start the Timer\n\r");
    break;
    }

    while (1)
    {
    	if (timer_intr_flag == 1)
    	{
    		printf("High-Speed Timer Interrupt captured by MSS \n\r");
    		timer_intr_flag = 0;
    	}
    }

}


