
 /*******************************************************************************
 * (c) Copyright 2011 Microsemi Corporation.  All rights reserved.
 *
 *  Sample test program for the SmartFusion ACE.  TM0 is used to monitor the
 *  voltage across the potentiometer.  The UART is used to send the ADC results
 *  to a terminal program.  The hardware configuration has four flags:
 *   - over 1.0v
 *   - over 1.5v
 *   - over 2.0v
 *   - over 2.5v
 *  The flag values are displayed on the SmartFusion Evaluation Kit board LEDs.
 *  Author: Amit Garg
 *  Last modified : Nov 16th, 2011
 *  Tested with : Keil uvision v4.21
 *  Last modification : Updated while updating the tutorials to Libero SoC IDE flow.
 ******************************************************************************/

#include "mss_uart.h"
#include "mss_ace.h"
#include "mss_gpio.h"
#include "ace_handles.h"
#include <stdio.h>

int main()
{
	size_t rx_size;
	uint8_t rx_buff[1];
	const uint8_t greeting[] = "Welcome to Microsemi's SmartFusion Voltage Monitor\n\r";
	const uint8_t instruction[] = "\n\rPress Any Key\n\r";

    int32_t flag_status_2p5v,  flag_status_2p0v, flag_status_1p5v, flag_status_1p0v;
	uint32_t gpio_output;

/*Initialize and Configure GPIO*/
MSS_GPIO_init();
MSS_GPIO_config( MSS_GPIO_31 , MSS_GPIO_OUTPUT_MODE );
MSS_GPIO_config( MSS_GPIO_30 , MSS_GPIO_OUTPUT_MODE );
MSS_GPIO_config( MSS_GPIO_29 , MSS_GPIO_OUTPUT_MODE );
MSS_GPIO_config( MSS_GPIO_28 , MSS_GPIO_OUTPUT_MODE );

/*Initialize UART_0*/
MSS_UART_init
(
&g_mss_uart0,
MSS_UART_57600_BAUD,
MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY | MSS_UART_ONE_STOP_BIT
);

    /*Initialize ACE*/
    ACE_init( );

    MSS_UART_polled_tx( &g_mss_uart0, greeting, sizeof(greeting) );
    MSS_UART_polled_tx( &g_mss_uart0, instruction, sizeof(instruction) );

   for (;;)
    {
        rx_size = MSS_UART_get_rx( &g_mss_uart0, rx_buff, sizeof(rx_buff));
        if (rx_size > 0)
        {
            uint8_t display_buffer[32];
            uint16_t adc_result;
            int32_t adc_value_mv;

            adc_result   = ACE_get_ppe_sample( TM0_Voltage );
            adc_value_mv = ACE_convert_to_mV( TM0_Voltage, adc_result );

            if ( adc_value_mv < 0 )
            {
                snprintf( display_buffer, sizeof(display_buffer),
                    "-%d.%d V\n\r", -adc_value_mv / 1000, -adc_value_mv % 1000);
            }
            else
            {
                snprintf( display_buffer, sizeof(display_buffer),
                    "%d.%d V\n\r", adc_value_mv / 1000, adc_value_mv % 1000);
            }
            MSS_UART_polled_tx_string( &g_mss_uart0, display_buffer );

     		/* Checking the status of Voltage flags */

     		flag_status_2p5v = ACE_get_flag_status(TM0_Voltage_over_2p5v);
     		flag_status_2p0v = ACE_get_flag_status(TM0_Voltage_over_2p0v);
         	flag_status_1p5v = ACE_get_flag_status(TM0_Voltage_over_1p5v);
     		flag_status_1p0v = ACE_get_flag_status(TM0_Voltage_over_1p0v);

     		/* Voltage flags are displayed on the LEDs through GPIO */

            if ( flag_status_2p5v == FLAG_ASSERTED )
            	gpio_output = ~(
            			MSS_GPIO_28_MASK |
            			MSS_GPIO_29_MASK |
            			MSS_GPIO_30_MASK |
            			MSS_GPIO_31_MASK );
            else
            if ( flag_status_2p0v == FLAG_ASSERTED )
            	gpio_output = ~(
            			MSS_GPIO_28_MASK |
            			MSS_GPIO_29_MASK |
            			MSS_GPIO_30_MASK );
            else
            if ( flag_status_1p5v == FLAG_ASSERTED )
            	gpio_output = ~(
            			MSS_GPIO_28_MASK |
            			MSS_GPIO_29_MASK );
            else
            if ( flag_status_1p0v == FLAG_ASSERTED )
            	gpio_output = ~(
            			MSS_GPIO_28_MASK );
            else
            	gpio_output = (
            			MSS_GPIO_28_MASK |
            			MSS_GPIO_29_MASK |
            			MSS_GPIO_30_MASK |
            			MSS_GPIO_31_MASK );

            MSS_GPIO_set_outputs( gpio_output );

    	    MSS_UART_polled_tx( &g_mss_uart0, instruction, sizeof(instruction) );
        }
    }
    return 0;
}
/***************************************************************************/
