/*******************************************************************************
 *  metrology-template.h - MSP430i2041 distribution version
 *
 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ 
 *  ALL RIGHTS RESERVED
 * 
*/
//Venkat
#define OSR 128
#define AVERAGE 3
#define IEC_THD_SUPPORT
//#undef IEC_THD_SUPPORT


/*! This selects the number of phases. */
#define i2040_EVM_VERSION_2
#define CONFIGURATION_NEUTRAL_MONITORING//CONFIGURATION_TWO_PHASES

#define TOTAL_ACTIVE_ENERGY_PULSES_PER_KW_HOUR            6400
/*! Tiny power levels should not record energy at all, as they may just be rounding
    errors, noise, or the consumption of the meter itself. This value is the cutoff level,
    in 10 milliwatts. */
#define TOTAL_RESIDUAL_POWER_CUTOFF                       250

#define TOTAL_REACTIVE_ENERGY_PULSES_PER_KVAR_HOUR              6400

#define TOTAL_ACTIVE_ENERGY_SUPPORT

#define TOTAL_REACTIVE_ENERGY_SUPPORT

#define TOTAL_APPARENT_ENERGY_SUPPORT           

//Change this value to the phase to be tied with the active and reactive leds. 
//This macro has potential values of 0, 1, and 2 to correspond to phases A, B, and C
//This macro is used for CONFIGURATION_COMMON_VOLTAGE. 
#define CURRENT_CHANNEL_PULSE_OUTPUT    0    


//MM i2040 V1 EVM
#if defined(i2040_EVM_VERSION_1)
  #if defined(CONFIGURATION_NEUTRAL_MONITORING)
    #define NUM_PHASES 1
    #undef VOLTAGE_SIGNAL_IS_COMMON
    #define NEUTRAL_MONITOR_SUPPORT
    #define SELECTED_STRUCT_CONFIGURATION        0xA5
    #undef TOTAL_ACTIVE_ENERGY_PULSES_PER_KW_HOUR            
    #undef TOTAL_RESIDUAL_POWER_CUTOFF                       
    #undef TOTAL_REACTIVE_ENERGY_PULSES_PER_KVAR_HOUR              
    #undef TOTAL_ACTIVE_ENERGY_SUPPORT
    #undef TOTAL_REACTIVE_ENERGY_SUPPORT
    #undef TOTAL_APPARENT_ENERGY_SUPPORT
    #define PHASE_1_VOLTAGE_ADC_CHANNEL                 0
    #define PHASE_1_CURRENT_ADC_CHANNEL                 1
    #undef PHASE_2_VOLTAGE_ADC_CHANNEL                  //1
    #undef PHASE_2_CURRENT_ADC_CHANNEL                      
    #undef PHASE_3_CURRENT_ADC_CHANNEL                      
    #undef  CURRENT_CHANNEL_PULSE_OUTPUT
    #define NEUTRAL_CURRENT_ADC_CHANNEL                 3
    #define UNUSED_ADC_CHANNEL                          2
    #define PHASE_1_DELAY_SPLIT     167
    #define NEUTRAL_DELAY_SPLIT     167
  #elif defined(CONFIGURATION_COMMON_VOLTAGE)
    #define NUM_PHASES 3
    #define VOLTAGE_SIGNAL_IS_COMMON
    #undef NEUTRAL_MONITOR_SUPPORT
    #define SELECTED_STRUCT_CONFIGURATION        0xA6
    #define PHASE_1_VOLTAGE_ADC_CHANNEL                 0
    #define PHASE_1_CURRENT_ADC_CHANNEL                 1
    #undef PHASE_2_VOLTAGE_ADC_CHANNEL                  //1
    #define PHASE_2_CURRENT_ADC_CHANNEL                 2     
    #define PHASE_3_CURRENT_ADC_CHANNEL                 3      
    #undef NEUTRAL_CURRENT_ADC_CHANNEL               
    #undef UNUSED_ADC_CHANNEL
    #define PHASE_1_DELAY_SPLIT     84
    #define PHASE_2_DELAY_SPLIT     84
    #define PHASE_3_DELAY_SPLIT     84
  #else //CONFIGURATION_TWO_PHASES will be the default configuration
    #define NUM_PHASES 2
    #undef VOLTAGE_SIGNAL_IS_COMMON
    #undef NEUTRAL_MONITOR_SUPPORT
    #define SELECTED_STRUCT_CONFIGURATION        0xA7
    #define PHASE_1_VOLTAGE_ADC_CHANNEL                 0
    #define PHASE_1_CURRENT_ADC_CHANNEL                 1
    #define PHASE_2_VOLTAGE_ADC_CHANNEL                 2 //1
    #define PHASE_2_CURRENT_ADC_CHANNEL                 3     
    #undef PHASE_3_CURRENT_ADC_CHANNEL                      
    #undef CURRENT_CHANNEL_PULSE_OUTPUT  
    #undef NEUTRAL_CURRENT_ADC_CHANNEL                 
    #undef UNUSED_ADC_CHANNEL
    #define PHASE_1_DELAY_SPLIT     126
    #define PHASE_2_DELAY_SPLIT     126
  #endif
  /*! ADC channel allocation */

//MM i2040 V2 EVM
#elif defined(i2040_EVM_VERSION_2)
  #if defined(CONFIGURATION_NEUTRAL_MONITORING)
    #define NUM_PHASES 1
    #undef VOLTAGE_SIGNAL_IS_COMMON
    #undef NEUTRAL_MONITOR_SUPPORT
    #define SELECTED_STRUCT_CONFIGURATION        0xA5
    #define PHASE_1_VOLTAGE_ADC_CHANNEL                 0
    #define PHASE_1_CURRENT_ADC_CHANNEL                 1   
    #undef NEUTRAL_CURRENT_ADC_CHANNEL                 

//    #define NEUTRAL_CURRENT_ADC_CHANNEL                 3
    #undef  CURRENT_CHANNEL_PULSE_OUTPUT 
    #define UNUSED_ADC_CHANNEL                          2
 //   #define UNUSED_ADC_CHANNEL                          1  
    #undef PHASE_2_VOLTAGE_ADC_CHANNEL                  //1
    #undef PHASE_2_CURRENT_ADC_CHANNEL                      
    #undef PHASE_3_CURRENT_ADC_CHANNEL                      
    #undef TOTAL_ACTIVE_ENERGY_PULSES_PER_KW_HOUR            
    #undef TOTAL_RESIDUAL_POWER_CUTOFF                       
    #undef TOTAL_REACTIVE_ENERGY_PULSES_PER_KVAR_HOUR              
    #undef TOTAL_ACTIVE_ENERGY_SUPPORT
    #undef TOTAL_REACTIVE_ENERGY_SUPPORT
    #undef TOTAL_APPARENT_ENERGY_SUPPORT
    #define PHASE_1_DELAY_SPLIT     167
    #define NEUTRAL_DELAY_SPLIT     167
  #elif defined(CONFIGURATION_COMMON_VOLTAGE)
    #define NUM_PHASES 3
    #define VOLTAGE_SIGNAL_IS_COMMON
    #undef NEUTRAL_MONITOR_SUPPORT
    #define SELECTED_STRUCT_CONFIGURATION        0xA6
    #define PHASE_1_VOLTAGE_ADC_CHANNEL                 0
    #define PHASE_1_CURRENT_ADC_CHANNEL                 1
    #undef PHASE_2_VOLTAGE_ADC_CHANNEL                  //1
    #define PHASE_2_CURRENT_ADC_CHANNEL                 2     
    #define PHASE_3_CURRENT_ADC_CHANNEL                 3   
    #undef NEUTRAL_CURRENT_ADC_CHANNEL                 
    #undef UNUSED_ADC_CHANNEL
    #define PHASE_1_DELAY_SPLIT     84
    #define PHASE_2_DELAY_SPLIT     84
    #define PHASE_3_DELAY_SPLIT     84
  #else //CONFIGURATION_TWO_PHASES will be the default configuration
    #define NUM_PHASES 2
    #undef VOLTAGE_SIGNAL_IS_COMMON
    #undef NEUTRAL_MONITOR_SUPPORT
    #define SELECTED_STRUCT_CONFIGURATION        0xA7
    #define PHASE_1_VOLTAGE_ADC_CHANNEL                 0
    #define PHASE_1_CURRENT_ADC_CHANNEL                 1
    #define PHASE_2_VOLTAGE_ADC_CHANNEL                 2 //1
    #define PHASE_2_CURRENT_ADC_CHANNEL                 3     
    #undef PHASE_3_CURRENT_ADC_CHANNEL                      
    #undef  CURRENT_CHANNEL_PULSE_OUTPUT  
    #undef NEUTRAL_CURRENT_ADC_CHANNEL              
    #undef UNUSED_ADC_CHANNEL
    #define PHASE_1_DELAY_SPLIT     126
    #define PHASE_2_DELAY_SPLIT     126
  #endif
#endif

#if defined(__HAS_SD_ADC__)
    #if defined(VOLTAGE_SIGNAL_IS_COMMON)
#define VOLTAGE_CHANNELS 1
    #else
#define VOLTAGE_CHANNELS NUM_PHASES
    #endif
    #if defined(NEUTRAL_MONITOR_SUPPORT)
#define CURRENT_CHANNELS (NUM_PHASES + 1)
    #else
#define CURRENT_CHANNELS NUM_PHASES
    #endif
#endif      
      
/*! This switch selects the use of 24 bit values for the current signal, where the
    ADC supports this. If this symbol is not defined, 16 bit values are used.
    There is nothing to be gained by using more than 16 bits for the the voltage
    signal, so we don't. */
#define TWENTYFOUR_BIT


//#define SD_LIVE_CURRENT_GAIN                        SD24GAIN_4
#define SD_LIVE_CURRENT_GAIN                        SD24GAIN_16

/*! The gain setting for the voltage channel of the SD16, for devices using the
    SD16 sigma-delta ADC. This is usually GAIN_1 (i.e. x1 gain). */
#define SD_VOLTAGE_GAIN                             SD24GAIN_1

/*! The gain setting for the first current channel channel of the SD16,*/

#define SD_NEUTRAL_CURRENT_GAIN                        SD24GAIN_1


#undef TRNG_SUPPORT
#undef LIMP_MODE_SUPPORT

/*! This switch enables use of the MSP430's internal temperature diode to
    measure the meter's temperature. */
#undef TEMPERATURE_SUPPORT

/*! This switch enables temperature correction of the metrology. */
#undef TEMPERATURE_CORRECTION_SUPPORT



/*! The nominal mains frequency, in Hz. This is used to prime the mains frequency
    measurement, and make its initial value settle quickly. It is not currently used after
    reset. */
#define MAINS_NOMINAL_FREQUENCY                     50

/*! This selects the nominal voltage used for sag/swell detection, and power calculations
    in limp mode, in volts */
#define MAINS_NOMINAL_VOLTAGE                       254

/*! This selects the basis current */
#define MAINS_BASIS_CURRENT                         10

/*! This selects the maximum operating current */
#define MAINS_MAXIMUM_CURRENT                       100

/*! This switch enables mains frequency measurement. This may be used as a
    meter feature. It may be a requirement, if non-linear CT sensors are used. */
#define MAINS_FREQUENCY_SUPPORT

/*! This selects support for RMS voltage measurement. */
#define VRMS_SUPPORT

#define SAG_SWELL_SUPPORT

//*! This selects support for sag and swell detection. */
#define SAG_SWELL_SUPPORT
/*! This selects the number of mains cycles over which sag and swell detection works. */
#define SAG_SWELL_WINDOW_LEN                        3
/*! This selects the percentage fall from the nominal voltage for sag detection. */
#define SAG_THRESHOLD                               61/* sag at 100 at nominal voltage(254) */ 
/*! This selects the percentage rise above the nominal voltage for swell detection. */
#define SWELL_THRESHOLD                             20/* swell at 304.8 at nominal voltage(254) */ 

#undef FUNDAMENTAL_VRMS_SUPPORT

#undef VOLTAGE_THD_SUPPORT

/*! This selects support for RMS current measurement. */
#define IRMS_SUPPORT

#undef FUNDAMENTAL_IRMS_SUPPORT

#undef CURRENT_THD_SUPPORT

#undef FUNDAMENTAL_ACTIVE_POWER_SUPPORT

#define REACTIVE_POWER_SUPPORT

#define REACTIVE_POWER_BY_QUADRATURE_SUPPORT

#undef FUNDAMENTAL_REACTIVE_POWER_SUPPORT

#define APPARENT_POWER_SUPPORT

#define POWER_FACTOR_SUPPORT

#undef RESIDUAL_IRMS_SUPPORT

/*! Tiny power levels should not record energy at all, as they may just be rounding
    errors, noise, or the consumption of the meter itself. This value is the cutoff level,
    in milliwatts. */
#define RESIDUAL_POWER_CUTOFF                       250



/*! The duration of the LED on time for an energy pulse. This is measured in
    ADC samples (i.e. increments 1/3276.8s). The maximum allowed is 255, giving a
    pulse of about 78ms. 163 gives a 50ms pulse. */
#define ENERGY_PULSE_DURATION                       20

/*! This switch selects support for measuring the active energy consumption. */
#define ACTIVE_ENERGY_SUPPORT

/*! This sets the number of pulses per kilo-watt hour the meter will produce at
    its active energy pulse LED/opto-coupler/LCD segment. It does not affect the
    energy accumulation process. */
#define ACTIVE_ENERGY_PULSES_PER_KW_HOUR            6400

/*! This sets the number of pulses per kilo-watt hour the meter will produce at
    its active energy pulse LED/opto-coupler/LCD segment. It does not affect the
    energy accumulation process. */


#define REACTIVE_ENERGY_PULSES_PER_KVAR_HOUR              6400



#define REACTIVE_ENERGY_SUPPORT

#define APPARENT_ENERGY_SUPPORT


/*! Normally the meter software only calculates the properly scaled values
    for voltage, current, etc. as these values are needed. This define
    enables additional global parameters, which are regularly updated with
    all the metrics gathered by the meter. This is generally less efficient,
    as it means calculating things more often than necessary. However, some
    may find this easier to use, so it is offered as a choice for the meter
    designer. */
#define PRECALCULATED_PARAMETER_SUPPORT

#define PER_SENSOR_PRECALCULATED_PARAMETER_SUPPORT

#undef PHASE_UNBALANCED_DETECTION_SUPPORT
#undef ON_UNBALANCED_SELECT_HIGHER_READING
#undef PHASE_REVERSED_DETECTION_SUPPORT
//#define PHASE_REVERSED_DETECTION_SUPPORT
//#undef ON_REVERSED_SELECT_POSITIVE_READING
#define ON_REVERSED_SELECT_POSITIVE_READING

/*! This is the number of successive measurement blocks which must agree the
    reversed current status has changed before we accept it. */
#define PHASE_REVERSED_PERSISTENCE_CHECK            5

/*! This is the minimum power level at which we will make checks for the reverse
    condition. Very small readings are not a reliable indicator, due to noise. */
#define PHASE_REVERSED_THRESHOLD_POWER              2000
/*! This is called every ADC interrupt, after the main DSP work has finished.
    It can be used for things like custom keypad operations. It is important
    this is a very short routine, as it is called from the main ADC interrupt. */
#define custom_adc_interrupt()                      /**/

#undef DYNAMIC_CURRENT_RELATED_CORRECTION_SUPPORT
#undef DYNAMIC_FREQUENCY_RELATED_CORRECTION_SUPPORT
