/*******************************************************************************
 *  emeter-template.h - MSP430i2041 distribution version
 *
 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/ 
 *  ALL RIGHTS RESERVED
 * 
*/

#include <emeter-metrology.h>

/* Define the following to build the software such that the info memory is overwritten
   each time new code is loaded into the MCU. */
//#define OVERWRITE_INFOMEM

/*! This switch, in combination with the calibrator switch, enables calibration
    with the meter cooperating with an external reference, through a UART port. */
#define SERIAL_CALIBRATION_SUPPORT
#define SERIAL_CALIBRATION_PASSWORD_1               0x1234
#define SERIAL_CALIBRATION_PASSWORD_2               0x5678
#define SERIAL_CALIBRATION_PASSWORD_3               0x9ABC
#define SERIAL_CALIBRATION_PASSWORD_4               0xDEF0


/*! This switch enables the sending of the current readings, through a UART port,
    for use in cooperative calibration with other meters. */
#define SERIAL_CALIBRATION_REF_SUPPORT

#undef RTC_SUPPORT

/* This switch enables power down to battery backup status on loss of line
   power. */
#undef POWER_DOWN_SUPPORT

/* These switches select a method of detecting when power is restored, if
   power down mode is supported. */
/* This method assumes the voltage waveform is being turned into simple
   digital pulses into an I/O pin. If this feature is used, POWER_UP_VOLTAGE_PULSE
   must define the way a voltage pulse is sensed. */
#undef POWER_UP_BY_VOLTAGE_PULSES

/* This method assumes the pre-regulator power supply voltage is being
   monitored by Comparator A. This method is suitable for meters which
   support a live/neutral only mode, for tamper resistance. */
#undef POWER_UP_BY_SUPPLY_SENSING

/* Many data logging requirements can be met by using only the MSP430's
   info memory. If an external serial EEPROM is needed for more
   complex requirements, this switch will enable an interface to
   I2C type serial EEPROMs. Basic routines to driver these EEPROMs are
   included in the toolkit. Routines to actually store and retrieve
   information are left to the meter designer. */
#undef EXTERNAL_EEPROM_SUPPORT

/*! This defines the speed of eUSCI 0 */
#define UART_0_SUPPORT
#define UART_0_BAUD_RATE                            9600
#define UART_0_DLT645_SUPPORT

/*! This switch enables multi-rate tariff features */
#undef MULTI_RATE_SUPPORT

/*! This switch selects where a backup battery's condition is monitored. */
#undef BATTERY_MONITOR_SUPPORT

#define custom_set_consumption(x,y)                 /**/

//#if defined(LIERDA_BOARD)
//#define custom_active_energy_pulse_start() P1OUT |= BIT1
//#define custom_active_energy_pulse_end() P1OUT &= ~BIT1
//#else
//#define custom_active_energy_pulse_start() P1OUT &= ~BIT4
//#define custom_active_energy_pulse_end() P1OUT |= BIT4
//#endif

#if defined(CONFIGURATION_NEUTRAL_MONITORING)
    #define custom_active_energy_pulse_start() \
    { \
                P2OUT |= BIT1; \
    }

    #define custom_active_energy_pulse_end() \
    { \
            P2OUT |= BIT1; \
    }

    #define custom_reactive_energy_pulse_start() \
    { \
            P2OUT |= BIT2; \
    }

    #define custom_reactive_energy_pulse_end() \
    { \
            P2OUT |= BIT2; \
    }
#elif defined(CONFIGURATION_COMMON_VOLTAGE)
    #define custom_active_energy_pulse_start(ph) \
    { \
        if (ph == CURRENT_CHANNEL_PULSE_OUTPUT) \
            P2OUT |= BIT1; \
    }

    #define custom_active_energy_pulse_end(ph) \
    { \
        if (ph == CURRENT_CHANNEL_PULSE_OUTPUT) \
            P2OUT |= BIT1; \
    }

    #define custom_reactive_energy_pulse_start(ph) \
    { \
        if (ph == CURRENT_CHANNEL_PULSE_OUTPUT) \
            P2OUT |= BIT2; \
    }

    #define custom_reactive_energy_pulse_end(ph) \
    { \
        if (ph == CURRENT_CHANNEL_PULSE_OUTPUT) \
            P2OUT |= BIT2; \
    }

#else
    #define custom_active_energy_pulse_start(ph) \
    { \
        if (ph == FAKE_PHASE_TOTAL) \
            P2OUT |= BIT1; \
    }

    #define custom_active_energy_pulse_end(ph) \
    { \
        if (ph == FAKE_PHASE_TOTAL) \
            P2OUT |= BIT1; \
    }

    #define custom_reactive_energy_pulse_start(ph) \
    { \
        if (ph == FAKE_PHASE_TOTAL) \
             P2OUT |= BIT2; \
    }

    #define custom_reactive_energy_pulse_end(ph) \
    { \
        if (ph == FAKE_PHASE_TOTAL) \
            P2OUT |= BIT2; \
    }
#endif

/* This custom function must check the conditions, such as passswords and shorting link tests,
   appropriate to the meter's "enable calibration" condition. */
#define custom_is_calibration_enabled() true

#define USE_WATCHDOG  


#undef USE_EXTERNAL_CLOCK
#undef USE_DUMMY_CALIBRATION
#undef OUTPUT_CLOCK

#define P1DIR_INIT      (BIT0 | BIT1 | BIT6 )
#define P1SEL0_INIT      (BIT2 | BIT3)
#define P1SEL1_INIT      (0)
#define P1OUT_INIT      (BIT0 )

#define P2DIR_INIT      ( BIT1 | BIT2 )
#define P2SEL0_INIT      (0)//UTXD0, URXD0
#define P2SEL1_INIT      (0)//UTXD0, URXD0
#define P2OUT_INIT      (BIT1 | BIT2)

#define custom_active_energy_pulse_start()      LED7_ON()
#define custom_active_energy_pulse_end()        LED7_OFF()

#define custom_reactive_energy_pulse_start()    //P2OUT &= ~BIT1
#define custom_reactive_energy_pulse_end()      //P2OUT |= BIT1

#define LED1_ON()                         // P2OUT &= ~BIT2
#define LED1_OFF()                        // P2OUT |= BIT2
#define LED2_ON()                         // P2OUT &= ~BIT3
#define LED2_OFF()                        // P2OUT |= BIT3
#define LED3_ON()                         // P2OUT &= ~BIT1
#define LED3_OFF()                        // P2OUT |= BIT1
#define LED7_ON()                          P2OUT &= ~BIT0
#define LED7_OFF()                         P2OUT |= BIT0
#define LED5_ON()                         // P1OUT &= ~BIT5
#define LED5_OFF()                        // P1OUT |= BIT5
#define LED6_ON()                         // P1OUT &= ~BIT4
#define LED6_OFF()                        // P1OUT |= BIT4
#define LED4_ON()                         // P1OUT &= ~BIT0
#define LED4_OFF()                        // P1OUT |= BIT0


/*
    P1.0 == LED 7
    P1.1 == 
    P1.2 == RXD 
    P1.3 == TxD
    P1.4 == LED 6
    P1.5 == LED 5
    P1.6 == 
    P1.7 == 
 */
#define P1DIR_INIT      (BIT5 | BIT4 | BIT3 | BIT0)
#define P1SEL0_INIT     (BIT3 | BIT2)
#define P1SEL1_INIT     (0x00)
#define P1OUT_INIT      (0xff)
/*
   P2.0 == LED 4
   P2.1 == LED 3
   P2.2 == LED 1
   P2.3 == LED 2
   P2.4 ==
   P2.5 ==
   P2.6 == 
   P2.7 == 
 */
#define P2DIR_INIT      (BIT0)//(BIT3 | BIT2 | BIT1 | BIT0)
//#define P2SEL_INIT      (BIT7 | BIT6)
#define P2OUT_INIT      (BIT3 | BIT2 | BIT1 | BIT0)
