This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TMS320F28035: Printing debug info through the Serial Communications Interface (SCI)

Part Number: TMS320F28035

I am trying to print values of C language variables through the Serial Communications Interface (SCI). These values are IQ15 (IQMath) values, but the values that are being printed are incorrect. I suspect it maybe a formatting issue.  I convert the IQ15 values into floating point values by using the _IQtoF function and then print them. Below is a code snippet of how I am doing it,

    float f1 = 0, f2 = 0;

    f1 = _IQtoF(ledOpenVoltage);
    f2 = _IQtoF(ledVoltage);

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
    sprintf((char *)serialTxBytes, "ledOpenVoltage: %.2f, ledOVoltage: %.2f, ledVoltage: %i \r\n", f1, f2, ledVoltage);
    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

Both ledOpenVoltage and ledVoltage are declared as long. But they get assigned _IQ15 values. The above code prints the f1 value correctly, but not the f2 value.

  • Hi Indika,

    The above code prints the f1 value correctly, but not the f2 value.

    Can you give me an example please of what you are expecting and what you are actually seeing? 

    In the past I've seen that breaking up the sprintf statement into multiple parts and transmitting the individual segments is needed instead of doing it all in one statement.

    Best Regards,

    Marlyn

  • I Marlyn,

    Thank you for the response.  Per your suggestion, changed the code so that it prints only one value at a time.  I am expecting to see value of around 30 for the leldVoltage(f2), but instead I see a value around 122.

  • Here is a screen shot of what I am seeing.

  • Hi Indika,

    Can I please see how you've split the code to transmit only one value at a time?

    Since f1 is correct I don't believe its an issue with the conversion, its probably the output formatting like you said. If you can also send me a project file or more complete code I can test on my side I will try to debug.

    This is a different issue but here is an example of splitting the output into multiple segments: https://e2e.ti.com/support/microcontrollers/c2000/f/c2000-microcontrollers-forum/964271/tms320f28035-ltoa-function-and-scia_msg-stuck-sending-characters

    Best Regards,

    Marlyn

  • Hi Marlyn,

    Here's the code that splits the transmission to one value at time.

    float f1 = 0, f2 = 0;

    f1 = _IQtoF(ledOpenVoltage);

    f2 = _IQtoF(ledVoltage);

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "ledOpenVoltage(f1): %.2f \r\n", f1);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "ledOVoltage(f2): %.2f \r\n", f2);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    I am not sure how I can send an my project as an attachment.  Can you please let me know how I can do it?

    Thank you

    Indika

  • Hi Indika,

    You should be able to send a zipped folder when you reply through the 'Insert' option, select "Image/Video/File". then click on "Upload".

    Best Regards,

    Marlyn

  • Hi Marlyn,

    I did as you suggested, but I am not sure if it worked. Did you get the file?

    Regards,

    Indika

  • Hi Indika,

    I did not receive it, another alternative is if you could post what your c file code contains.

    Best Regards,

    Marlyn

  • Hello Marlyn,

    I am posting the code below. This project has C code and Assembly code. The variable 'ledVoltage' is a shared variable between the C code and the Assembly code. So I am posting both code files.

    The C code (Main.c)

    //----------------------------------------------------------------------------------

    //

    // Product: Universal LED Power Supply

    //

    // File: ULEDPS-Main.c

    //

    // Description: Main function, state machine, and background functions

    //

    // Version: L

    //

    // Target: TMS320F28035 (Piccolo)

    //

    //----------------------------------------------------------------------------------

    // Copyright Eaton Corporation © 2014

    //----------------------------------------------------------------------------------

    // $Author: tom.reed $

    // $Date: 2014-12-15 17:31:09 -0500 (Mon, 15 Dec 2014) $

    // $Id: ULEDPS-Main.c 340 2014-12-15 22:31:09Z tom.reed $

    // $Rev: 340 $

    // $URL: 10.91.161.17/.../ULEDPS-Main.c $

    //

    const int svnRev = 1207; // SVN VERSION AND DATE VARIABLES MUST BE MANUALLY UPDATED WHEN A NEW VERSION IS RELEASED.

    const long svnRevStamp = 20200807;

    const char firmwareRev = 'P'; // Letter version of production release.

    // Header Includes

    #include <string.h>

    #include <stdio.h>

    #include <stdint.h>

    #include "ULEDPS-Settings.h" // User settings and power supply parameters.

    #include "DSP2803x_Device.h" // Device register structures from TI.

    #include "Flash2803x_API_Library.h" // On-Chip flash functionality.

    #include "DSP2803x_EPWM_defines.h" // Defines for PWM settings.

    #include "IQmathLib.h" // TIs built-in IQ math functions.

    #include <math.h> // Standard math functions.

    #include "SFO_V6.h" // High resolution PWM controller calibration algorithm.

    #include "CLAShared.h" // Functions for CLA subprocessor.

    #include "DSP2803x_Cla_defines.h" // Defines for CLA subprocessor.

    #include "DPlib.h" // TIs digital power library functions.

    // Function Prototypes (in this file)

    int tempConvert(int adcval);

    void init_cla(void);

    void runtimeErrorChecking(void);

    void ledCurrentErrorChecking(void);

    long sciMessageRx(void);

    void sciMessageTx(int data[], int length);

    long parseBytesToData(int data[], int offset, int length);

    void parseDataToBytes(long value, int data[], int offset, int length);

    void processEEPROMread(int byteAddress, int data[]);

    void processEEPROMwrite(int byteAddress, int data[]);

    void initEEPROMpage(int byteAddress);

    void sendSciData(int dataID, long dataValue, int length);

    void sendSciAdData(int dataID, char data[], int length);

    long getHeaterTemperature(long adcValue);

    void configFunction(void);

    void diagFunction(void);

    int ledCurrentRamp(volatile float *refPoint, float setPoint);

    // Control interrupt functions located in ULEDPS-28x-ISR.asm

    extern void CTRL_INIT(void);

    extern interrupt void CTRL_ISR(void);

    // CLA functions located in ULEDPS-CLA-ISR.asm

    extern void CLA_CTRL_INIT(void);

    // Microsecond delay located in DSP2803x_usDelay.asm

    extern void DSP2803x_usDelay(Uint32 Count);

    extern void ADCinit(void);

    extern void DeviceInit(void);

    // EPWM functions located in ULEDPS-ePWM_Config.c

    extern void ledBuckConfig(int16 pwmNumber, int16 period);

    extern void ledBoostConfig(int16 pwmNumber, int16 period);

    extern void ledDimmerConfig(int16 x, Uint16 period);

    extern void ledDelayDimmerConfig(int16 x, Uint16 period);

    extern void inputPfcConfig(int16 pwmNumber, int16 period, int16 compNumber);

    extern void interrupt28xConfig(int16 pwmNumber, int16 period);

    extern void interruptCLAConfig(int16 pwmNumber, int16 period);

    extern void heaterDriveConfig(int16 pwmNumber, int16 period);

    extern void syncDriveConfig(int16 pwmNumber, int16 period);

    extern int i2cNackPoll(int slaveAddress);

    // EEPROM functions located in ULEDPS-EEEPROM.c

    extern int receiveI2cData(int slaveAddress, int byteAddress, int data[]);

    extern int transmitI2cData(int slaveAddress, int byteAddress, int data[]);

    extern void I2Cinit(void);

    extern void i2cRestart(void);

    // Misc MCU Functions.

    void InitFlash();

    void MemCopy();

    // Serial function in ULEDPS-Sci.c

    extern void scia_init(void);

    // Addresses for relocating timing critical functions in RAM

    extern Uint16 *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;

    // Addresses for relocating CLA program code to CLA accessible RAM.

    extern Uint16 Cla1funcsLoadStart, Cla1funcsLoadEnd, Cla1funcsRunStart;

    // Externally defined variables in 28x assembly routine.

    extern long *CNTL_2P2Z_Ref1;

    extern long *CNTL_2P2Z_Fdbk1;

    extern long *CNTL_2P2Z_Out1;

    extern long *CNTL_2P2Z_Coef1;

    extern long *PWMDRV_1chHiRes_Duty1;

    extern long pfcOVH;

    extern long pfcOVL;

    extern long pfcUVH;

    extern long pfcUVL;

    extern int pfcOCH;

    extern int pfcOCL;

    extern unsigned int inputCurrentOffset;

    extern int syncCurrentH;

    extern int syncCurrentL;

    extern long pfcVoltageAdc;

    extern long pfcVoltage;

    extern long pfcSenseGain;

    extern unsigned int pfcOVErr;

    extern unsigned int pfcOCErr;

    extern unsigned int dimDisable;

    extern unsigned int dimOverride;

    extern unsigned int dimRampVal;

    extern unsigned int blinkDisable;

    extern unsigned int blinkState;

    extern unsigned int blinkTimer;

    extern unsigned int ledFastOVDisable;

    extern unsigned int ledFastOV;

    extern unsigned int linePeriod;

    // Externally defined variables in CLA assembly routine.

    extern volatile float *ADCDRV_1ch_CLA_Rlt1;

    extern volatile float *ADCDRV_1ch_CLA_Rlt6;

    extern volatile float *CNTL_2P2Z_CLA_Ref1;

    extern volatile float *CNTL_2P2Z_CLA_Fdbk1;

    extern volatile float *CNTL_2P2Z_CLA_Out1;

    extern volatile float *CNTL_2P2Z_CLA_Coef1;

    extern volatile float *PWMDRV_1chHiRes_CLA_Duty6;

    extern volatile float *Kff;

    extern volatile float vFFvMin;

    extern volatile float lineCurrentGain;

    extern volatile float lineCurrentOffset;

    extern float ledCurrentSet;

    extern float ledCurrentRef;

    extern float ledCurrentRampVal;

    extern int ledMinDuty;

    extern int ledRampSpeed;

    extern float ledCurrentSetVal;

    //extern float ledDimSet;

    extern volatile int syncDisableFlag;

    extern volatile int ledCurrentCLA;

    extern volatile int ADC2_pfcVoltage;

    extern volatile int ADC4_lineVoltage;

    extern volatile int ADC5_heaterTemperature;

    extern volatile int ADC6_ledVoltage;

    extern volatile int ADC7_hk12Voltage;

    extern unsigned int ledVoltageCLA;

    extern volatile long currentFastIQ;

    extern volatile long currentFiltIQ;

    extern volatile long currentDimmingRms;

    // Locating pragmas for CLA variables.

    #pragma DATA_SECTION(CNTL_2P2Z_CoefStruct1, "CNTL_2P2Z_Coef");

    struct CNTL_2P2Z_CoefStruct CNTL_2P2Z_CoefStruct1;

    #pragma DATA_SECTION(CNTL_2P2Z_CLACoefStruct_LED, "CNTL_2P2Z_CLA_Coef");

    struct CNTL_2P2Z_CLACoefStruct CNTL_2P2Z_CLACoefStruct_LED;

    #pragma DATA_SECTION(ledControlOut, "Cla1ToCpuMsgRAM");

    volatile float ledControlOut;

    #pragma DATA_SECTION(ledFFgain, "CpuToCla1MsgRAM");

    volatile float ledFFgain;

    //#pragma DATA_SECTION(ledCurrentRef, "CpuToCla1MsgRAM");

    //volatile float ledCurrentRef;

    #pragma DATA_SECTION(ledCurrentAdc, "Cla1ToCpuMsgRAM");

    volatile float ledCurrentAdc;

    #pragma DATA_SECTION(ledDuty, "Cla1ToCpuMsgRAM");

    volatile float ledDuty;

    #pragma DATA_SECTION(vOutAdc, "Cla1ToCpuMsgRAM");

    volatile float vOutAdc;

    //Slope of temperature sensor (deg. C / ADC code, fixed point Q15 format)

    #define getTempSlope() (*(int (*)(void))0x3D7E82)()

    //ADC code corresponding to temperature sensor output at 0-degreesC

    #define getTempOffset() (*(int (*)(void))0x3D7E85)()

    // PFC control loop gains

    long pfcPgain;

    long pfcIgain;

    long pfcDgain;

    long pfcFastPgain;

    long pfcFastIgain;

    // LED control loop gains

    float ledPgain;

    float ledIgain;

    float ledDgain;

    // State machine variables

    int previousState = MCU_RESET;

    int currentState = PFC_BOOT;

    int statusLedBlink = 0;

    // Timer variables

    volatile unsigned int timer10ms = 0;

    volatile unsigned int canTimer = 0;

    volatile unsigned long i2cTimer = 0;

    volatile unsigned int sfoTimer = 0;

    volatile unsigned int sciTimer = 0;

    volatile unsigned int adTimer = 0;

    volatile unsigned int configExitTimer = 0;

    volatile unsigned int connectTimer = 0;

    volatile unsigned int errorDelay = 0;

    volatile unsigned int warmUpDelayTimer = 0;

    // Operating time counters

    unsigned long opSeconds = 0;

    unsigned long opSecondsB1 = 0;

    unsigned long opSecondsB2 = 0;

    unsigned long opSecondsB3 = 0;

    unsigned long opSecondsB10 = 0;

    unsigned long opSecondsB4 = 0;

    unsigned long opSecondsB30 = 0;

    unsigned long opSecondsB5 = 0;

    // Error counting and logging.

    int runtimeErrorsEnable = 0;

    unsigned int pfcOCErrorLog = 0;

    unsigned int pfcOCErrorCount = 0;

    unsigned int pfcOVErrorLog = 0;

    unsigned int pfcOVErrorCount = 0;

    unsigned int pfcUVErrorLog = 0;

    unsigned int pfcUVErrorCount = 0;

    unsigned int v12ErrorLog = 0;

    unsigned int v12ErrorCount = 0;

    unsigned int ledVErrorLog = 0;

    int ledOVErrorCount = 0;

    unsigned int ledUVErrorCount = 0;

    unsigned int ledMaxOVErrors = LED_MAX_OV_ERRORS;

    unsigned int ledRestartMax = LED_RESTART_MAX;

    unsigned int ledOCErrorLog = 0;

    unsigned int ledOCErrorCount = 0;

    unsigned int ledPeakOCErrorLog = 0;

    unsigned int ledPeakOCErrorCount = 0;

    unsigned int ledMaxUVErrors = LED_MAX_UV_ERRORS;

    unsigned int ledRestart = 0;

    unsigned int ledRestartCounter = 0;

    unsigned int ledRestartVoltage = 0;

    unsigned int ledOCWaitCounter = 0;

    unsigned int ledOCErrorReset = LED_OC_RESET;

    unsigned int warmUpDelayComplete = 0;

    unsigned int startWarmUpDelay = 0;

    float ledCurrentSetValHalf = 0;

    // PFC protection and regulation.

    long pfcVoltageRef = 0;

    long pfcSetRef = 0;

    long pfcDuty = 0;

    long pfcControlOut = 0;

    long pfcUVHRun = 0;

    long pfcUVLRun = 0;

    #define GUARD_SYNC_TIMEOUT 150

    #define GUARD_SYNC_ACTIVE_TIME 10

    int guardSyncMasterFlag = 0;

    int guardSyncIn = 0;

    int guardSyncTimer = GUARD_SYNC_TIMEOUT;

    int guardSyncOddEven = 0;

    int guardSyncActiveDelay = 0;

    int guardSyncActive = 0;

    #define L852G_TEST 0

    // LED protection and regulation.

    float ledCurrentSetSave = 0;

    unsigned int ledOverCurrentSet = 0;

    long ledShortVoltage = 0;

    float ledRampVal = 0.0;

    float ledRestartVal = 0.000005;

    long ledOpenVoltage = 0;

    int runLedCal = 0;

    unsigned int ledCalFinished = 0;

    unsigned int ledCalNotifyTimer = 0;

    unsigned long ledCalTimer = 0;

    unsigned int ledOnTimer = 0;

    unsigned int timerOn = 0;

    int turnLedOn = 0;

    unsigned int bleLedOnTimer = 0;

    unsigned int bleTimerOn = 0;

    // Common loop counters;

    volatile int i;

    volatile int j;

    volatile int k;

    // Hi-res ePWM scale factor.

    int MEP_ScaleFactor = 115;

    int sfoStatus = 0;

    // Operating point variables.

    long v12Hk = 0;

    long ledCurrent = 0;

    long ledVoltage = 0;

    int currentStep = B0;

    long lineFrequency = 0;

    // Temperature and related variables.

    int tempFiltCount = 0;

    long heaterCelsius = _IQ15(25.0);

    long mcuCelsius = _IQ15(25.0);

    long heatTempFilter = 0;

    int heaterMaxTempRun = 0;

    int mcuMaxTemp = 0;

    int mcuMaxTempRun = 0;

    long heatOnTemp = 12.4;

    long heatOffTemp = 34.0;

    // CCR amp control

    int ccrCntlTimer = 0;

    int ccrCntlStep = 0;

    int ccrCntlStepPrev = 0;

    int ccrCntlWord = 0;

    int ccrMsgHeader = 0;

    int ccrMsgData = 0;

    int ccrMsgPending = 0;

    int ccrMsgBitCount = 0;

    int ccrMsgReceived = 0;

    int heatAmpCntl = 0;

    // I2C and EEPROM variables.

    int i2cErrorCount = 0;

    int i2cRetries = 0;

    int i2cStatus = 0;

    int eepromData[16];

    int blockPointer = 0;

    int bytePointer = 16;

    int eepromInitFlag = 0;

    int currentSendTimer = 0;

    // ERGL variables.

    int erglCounter = 0;

    int erglLimit = 0;

    int erglFlag = 0;

    int erglState = ERGL_LEFT_ON;

    int erglStatePrev = ERGL_LEFT_ON;

    int erglNextState = ERGL_RIGHT_ON;

    int erglRightFault = 0;

    int erglLeftFault = 0;

    int erglRightOVErrorCount = 0;

    int erglRightUVErrorCount = 0;

    int erglLeftOVErrorCount = 0;

    int erglLeftUVErrorCount = 0;

    int guardFlag = 0;

    extern int currentCalFlag;

    int currentCalTimer = 5;

    int ledManualBlinkTimer = 0;

    int failOpenFlag = 1;

    int failOpenOnTimer = 0;

    int failOpenOffTimer = 0;

    // Bluetooth (serial message) variables.

    long bleMessage = 0;

    int bluetoothMode = 0;

    int bleCode = 0;

    int bleCommand = 0;

    int bleDataID = 0;

    int bleData = 0;

    int bleNotify = 0;

    int bleNotifyData = 0;

    int configOnEntry = 1;

    int diagOnEntry = 1;

    int connect = 0;

    int sendAdName = 0;

    char adName[12] = {'C','R','O','U','S','E','-','H','I','N','D','S'};

    //int serialTxBytes[16]; // For Bluetooth

    int serialTxBytes[200]; // For serial communication

     

    // Configuration variables.

    unsigned int oddEvenConfigValid = 0xFF;

    unsigned int oddEvenConfig = 0xFF;

    unsigned int phaseConfigValid = 0xFF;

    unsigned int phaseConfig = 0xFF;

    unsigned int dimRampConfigValid = 0xFF;

    unsigned int dimRampConfig = 0xFF;

    unsigned int hardwareConfigValid = 0xFF;

    unsigned int hardwareConfig = 0xFF;

    unsigned int ledCalValid = 0xFF;

    long ledShortConfig = _IQ15(0.0);

    // Jumper settings.

    unsigned int configJumpers = 0;

    unsigned int signJumper = 1;

    unsigned int boostJumper = 1;

    unsigned int monitorJumper = 1;

    unsigned int hirlJumper = 1;

    unsigned int test_timer = 0;

    unsigned int reCalibrate = 0;

    unsigned int toggleFlag = 0;

    // Flash variables.

    extern Uint32 Flash_CPUScaleFactor;

    typedef struct {

    Uint16 *StartAddr;

    Uint16 *EndAddr;

    } SECTOR;

    #define FLASH_START_ADDR 0x3E8000

    SECTOR Sector[8]= {

    (Uint16 *) 0x3E8000,(Uint16 *) 0x3E9FFF,

    (Uint16 *) 0x3EA000,(Uint16 *) 0x3EBFFF,

    (Uint16 *) 0x3EC000,(Uint16 *) 0x3EDFFF,

    (Uint16 *) 0x3EE000,(Uint16 *) 0x3EFFFF,

    (Uint16 *) 0x3F0000,(Uint16 *) 0x3F1FFF,

    (Uint16 *) 0x3F2000,(Uint16 *) 0x3F3FFF,

    (Uint16 *) 0x3F4000,(Uint16 *) 0x3F5FFF,

    (Uint16 *) 0x3F6000,(Uint16 *) 0x3F7FFF,

    };

    #define GPI31Value GpioDataRegs.GPADAT.bit.GPIO31

    #define GPI41Value GpioDataRegs.GPBDAT.bit.GPIO41

    int ledCalNotifyTime = 1000;

    long ledAutoCalTime = 359000;

    int ManualBlinkTime = 100;

    //unsigned int FirstSetGpio;

    //unsigned int SecondSetGpio;

    //unsigned int FirstOneSet;

    //#define GPIO22Value GpioDataRegs.GPADAT.bit.GPIO22

    //#define GPIO23Value GpioDataRegs.GPADAT.bit.GPIO23

    //unsigned int toggle;

    void main(void)

    {

    GpioDataRegs.GPBSET.bit.GPIO34 = 1;

    // FirstSetGpio = 0;

    // SecondSetGpio = 0;

    // FirstOneSet = 0;

    // toggle = 1;

    //=================================================================================

    // MCU INITIALIZATION

    //=================================================================================

    IER = 0x0000; // Disable interrupts.

    DeviceInit(); // Setup clocks, GPIOs, etc. (ULEDPS-F2803xInit.c

    MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart); // Copy timing critical functions to RAM.

    ADCinit(); // Initialize the ADC module..

    I2Cinit(); // Initialize I2C module.

    scia_init(); // Initialize the serial port.

    CpuTimer0Regs.PRD.all = 600000; // Set timer 0 the count down to 0 in 10mS (600000 clocks at 60mHz).

    CpuTimer1Regs.PRD.all = 6000000; // Set timer 1 for I2C error detection.

    //====================================================================================

    // INTERRUPTS & PWM INITIALIZATION

    //====================================================================================

    // Both the 28x core and CLA core have assembly interrupt routines that run in parallel.

    // Both are triggered at 100kHz by different PWMs to allow phasing. The ADC is triggered by

    // a compare action so conversions may be placed anywhere within the PWM cycle.

    boostJumper = GpioDataRegs.GPADAT.bit.GPIO17; // Read PCB jumper resistors to determine hardware type.

    signJumper = GpioDataRegs.GPADAT.bit.GPIO18;

    monitorJumper = GpioDataRegs.GPADAT.bit.GPIO20;

    hirlJumper = GpioDataRegs.GPADAT.bit.GPIO21;

    // hirlJumper = 0;

    dimOverride = 0; // Initialize dimming override for DC output units and elevated guard light

    dimRampVal = 5;

    EALLOW; // Many of the following registers are EALLOW protected.

    #if DEBUG

    EPwm1Regs.TBCTL.bit.FREE_SOFT = 2; // Set PWM emulation behavior to free-run

    EPwm2Regs.TBCTL.bit.FREE_SOFT = 2; // Set PWM emulation behavior to free-run

    EPwm3Regs.TBCTL.bit.FREE_SOFT = 2; // Set PWM emulation behavior to free-run

    EPwm4Regs.TBCTL.bit.FREE_SOFT = 2; // Set PWM emulation behavior to free-run

    EPwm5Regs.TBCTL.bit.FREE_SOFT = 2; // Set PWM emulation behavior to free-run

    EPwm6Regs.TBCTL.bit.FREE_SOFT = 2; // Set PWM emulation behavior to free-run

    EPwm7Regs.TBCTL.bit.FREE_SOFT = 2; // Set PWM emulation behavior to free-run

    #endif

    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0; // Disable TBCLK during setup

    if(boostJumper == 0)

    {

    ledBoostConfig(LED_EPWM, LED_PERIOD); // Set up LED converter PWM. Boost topology for ERGL and IRGL,

    } // Buck for all others.

    else

    {

    ledBuckConfig(LED_EPWM, LED_PERIOD);

    }

    inputPfcConfig(INPUT_EPWM, PFC_PERIOD, INPUT_COMPARATOR); // Set up input PFC PWM.

    EPwm1Regs.CMPA.half.CMPA = 50;

    ledDimmerConfig(LED_DIM_EPWM, LED_DIM_PERIOD); // Set up LED dimmer PWM.

    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD); // Set up delayed LED dimmer PWM.

    interrupt28xConfig(INT28X_EPWM, INTERRUPT_PERIOD); // Set up 28x interrupt timer.

    interruptCLAConfig(INTCLA_EPWM, INTERRUPT_PERIOD); // Set up CLA interrupt timer.

    heaterDriveConfig(HEAT_EPWM, HEATER_PERIOD); // Set up PWM to activate heater.

    PieVectTable.EPWM2_INT = &CTRL_ISR; // Point to 28x assembly interrupt vector.

    PieCtrlRegs.PIEIER3.bit.INTx2 = 1; // Enable the 28x interrupt in the interrupt controller.

    EPwm2Regs.ETSEL.bit.INTEN = 1; // Enable the interrupt signal in the PWM module.

    EPwm2Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // Trigger on TBCTR=0.

    EPwm2Regs.ETPS.bit.INTPRD = ET_1ST; // Interrupt triggered every cycle.

    EPwm2Regs.ETSEL.bit.SOCAEN = 1; // Enable start of conversion signal.

    EPwm2Regs.ETSEL.bit.SOCASEL = ET_CTRU_CMPB; // Trigger ADC conversions on PWM4 when CMPB is reached.

    EPwm2Regs.ETPS.bit.SOCAPRD = ET_1ST; // Trigger ADC every cycle.

    EPwm2Regs.CMPB = 520; // Set timing for ADC start of conversion.

    EPwm4Regs.ETSEL.bit.INTEN = 1; // Enable the interrupt signal for the CLA in the PWM module.

    EPwm4Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO; // Trigger on TBCTR=0.

    EPwm4Regs.ETPS.bit.INTPRD = ET_1ST; // Interrupt triggered every cycle.

    EPwm1Regs.HRMSTEP=MEP_ScaleFactor; // Set initial hi-res PWM scale factor.

    IER |= M_INT3; // Enable the MCU interrupts for PWM INT.

    SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Sync PWM time-base clocks

    EDIS;

    InitFlash(); // Set up internal flash functions.

    Flash_CallbackPtr = 0; // Flash callback set to 0 for now;

    CTRL_INIT(); // Initialize assembly variables (28x core) see ULEDPS-28x-ISR.asm

    CLA_CTRL_INIT(); // Initialize assembly variables (CLA core) see ULEDPS-CLA-ISR.asm

    currentCalFlag = 0;

     

    // Read configuration settings from EEPROM for initial setup.

    i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, CONFIGDATA0, eepromData); // Read EEPROM data.

    if(i2cStatus == I2C_ERROR) // Check for errors and timeout to prevent getting

    { // stuck on a bus hang-up.

    i2cErrorCount++;

    }

    if(eepromData[0] != EEPROM_DATA_VALID) // Check if the data just read is valid or an erased EEPROM.

    {

    i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK1, CONFIGDATA0, eepromData); // Attempt to read EEPROM from secondary config block.

    if(i2cStatus == I2C_ERROR)

    {

    i2cErrorCount++;

    }

    if(eepromData[0] != EEPROM_DATA_VALID)

    {

    initEEPROMpage(CONFIGDATA0); // If secondary block was invalid, set config data to factory values.

    }

    else

    {

    processEEPROMread(CONFIGDATA0, eepromData); // If secondary block was valid, read it in as config data.

    }

    }

    else

    {

    processEEPROMread(CONFIGDATA0, eepromData); // If primary block was valid, read it in as config data.

    }

    // Read configuration settings from jumpers (on JTAG header).

    configJumpers = 0;

    int config = 0;

    config = GpioDataRegs.GPBDAT.bit.GPIO35;

    configJumpers = configJumpers|(config<<4);

    config = GpioDataRegs.GPBDAT.bit.GPIO38;

    configJumpers = configJumpers|(config<<3);

    config = GpioDataRegs.GPADAT.bit.GPIO1;

    configJumpers = configJumpers|(config<<2);

    config = GpioDataRegs.GPADAT.bit.GPIO2;

    configJumpers = configJumpers|(config<<1);

    config = GpioDataRegs.GPADAT.bit.GPIO3;

    configJumpers = configJumpers|config;

    if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)

    {

    ledCalNotifyTime = 500;

    ledAutoCalTime = 3000;

    ManualBlinkTime = 200;

    }

    else

    {

    ledCalNotifyTime = 1000;

    ledAutoCalTime = 359000;

    ManualBlinkTime = 100;

    }

    if (monitorJumper == 0)

    {

    failOpenFlag = 0;

    }

    if(hardwareConfigValid != 0xFF)

    {

    if(hardwareConfigValid == hardwareConfig)

    {

    configJumpers = hardwareConfig;

    }

    }

    #if JUMPER_PRESET // Override the jumpers settings (see settings file).

    configJumpers = CODE_JUMPER_SETTING;

    #endif

    // if((boostJumper == 1) && (signJumper == 1))

    if(signJumper == 1) // Fixes the problem with the HIRL. May create a problem with 804 and 852G. SAMPLE 1

    {

    if(ledCalValid == EEPROM_DATA_VALID)

    {

    ledShortVoltage = ledShortConfig;

    }

    else

    {

    runLedCal = 1;

    }

    reCalibrate = 1;

    if (boostJumper == 0)

    { // Don't re-calibrate if L852G or L804

    if (configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS || configJumpers == L804 || configJumpers == L804_LEFT_FULL_ON || configJumpers == L804_RIGHT_FULL_ON)

    {

    reCalibrate = 0;

    }

    }

    if (hirlJumper == 0)

    { // Don't re-calibrate if L852G/S

    if (configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON)

    {

    reCalibrate = 0;

    }

    }

    if (reCalibrate == 1)

    {

    if (hirlJumper == 0 && configJumpers == NO_JUMPERS) // If a HIRL and configJumpers is equal to NO_JUMPERS, then don't look at GPIO41

    {

    oddEvenConfigValid = 0xFF;

    oddEvenConfig = 0xFF;

    phaseConfigValid = 0xFF;

    phaseConfig = 0xFF;

    dimRampConfigValid = 0xFF;

    dimRampConfig = 0xFF;

    ledCalValid = 0xFF;

    ledShortConfig = _IQ15(0.0);

    hardwareConfigValid = 0xFF;

    hardwareConfig = 0xFF;

    ledShortVoltage = 0;

    ledCalTimer = 0; // Was 'LED_AUTO_CAL_TIME - 1000'. Changed to 0 to stop calibration after 10 seconds

    runLedCal = 1;

    processEEPROMwrite(CONFIGDATA0, eepromData);

    i2cStatus = transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);

    if(i2cStatus == I2C_ERROR)

    {

    i2cErrorCount++;

    }

    }

    else

    {

    #if HARDWARE_21696

    // if(GpioDataRegs.GPBDAT.bit.GPIO40 == 0)

    if(GPI41Value == 0) // GPIO40 and GPIO41 pulled low by bluetooth board always, so GPIO40 can't be used with a bluetooth board

    {

    #else

    // if(GpioDataRegs.GPADAT.bit.GPIO31 == 0)

    if (GPI35Value == 0)

    {

    #endif

    ledCalValid = 0xFF;

    ledShortConfig = 0;

    ledShortVoltage = 0;

    ledCalTimer = 0; // Was 'LED_AUTO_CAL_TIME - 1000'. Changed to 0 to stop calibration after 10 seconds

    runLedCal = 1;

    processEEPROMwrite(CONFIGDATA0, eepromData);

    i2cStatus = transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);

    if(i2cStatus == I2C_ERROR)

    {

    i2cErrorCount++;

    }

    // i2cNackPoll(EEPROM_SLAVE_ADDR);

    }

    }

    }

    }

    if((dimRampConfig >= 1)&&(dimRampConfig <= 5)) // If dim ramp config is valid, set the dimming ramp rate.

    {

    dimRampVal = dimRampConfig;

    }

    else

    {

    dimRampConfig = dimRampVal|0xF0;

    }

    /*

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "-------------------------------------------- \r\n");

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    long l1 = 0, l2 = 0;

    l1 = _IQ15(73.76);

    l2 = _IQ15(-56.2314);

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "\r\n l1: %d, l2: %d \r\n", l1, l2);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    */

    float f1 = 0, f2 = 0;

    /*

    f1 = _IQtoF(l1);

    f2 = _IQtoF(l2);

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "f1: %.2f, f2: %.4f \r\n", f1, f2);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    */

    if(boostJumper == 0)

    {

    // Common for boost units.

    // PFC controller set point and protection limits.

    pfcSetRef = _IQ12mpy(PFC_SET_12V_852G, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.

    pfcOVH = PFC_OVH_12V_852G;

    pfcOVL = PFC_OVL_12V_852G;

    pfcUVHRun = PFC_UVH_12V_852G;

    pfcUVLRun = PFC_UVL_12V_852G;

    pfcUVH = PFC_UVH_STARTUP;

    pfcUVL = PFC_UVL_STARTUP;

    // PFC controller coefficients.

    pfcPgain = KP_PFC;

    pfcIgain = KI_PFC;

    pfcDgain = KD_PFC;

    pfcFastPgain = KP_PFC_FAST;

    pfcFastIgain = KI_PFC_FAST;

    CNTL_2P2Z_CLA_Fdbk1 = &ledCurrentAdc; // LED feedback current.

    // LED controller coefficients.

    ledPgain = KP_LED;

    ledIgain = KI_LED;

    ledDgain = KD_LED;

    ledFFgain = 1.8;//KFF_LED;

    Kff = &ledFFgain; // LED feed-forward variables.

    vFFvMin = VFF_MIN40;

    syncDisableFlag = 0; // Enable dimming and sync of LED.

    dimDisable = 0;

    ledFastOVDisable = 0;

    ledMaxOVErrors = BOOST_MAX_OV_ERRORS;

    ledRestartMax = BOOST_RESTART_MAX;

    ledMinDuty = COLOR_DIM_MIN;

    dimOverride = 0;

    blinkTimer = 0;

    switch(configJumpers) // LED controller set point and protection limits set by jumpers.

    {

    case HI450MA_WR_FAA: // HIRL L862 White - Red

    pfcSetRef = _IQ12mpy(PFC_SET_862_WR, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.

    ledCurrentSetVal = ((float)((0.471 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(81.0);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(35.0);

    }

    EALLOW;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // Activate GPIO40 as epwm7A to follow dimming

    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD); // Set up Halo dimmer PWM.

    EDIS;

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking

    ledMinDuty = WHITE_DIM_MIN;

    break;

    case HI470MA_WG_FAA: // HIRL L862 White - Green

    pfcSetRef = _IQ12mpy(PFC_SET_862_WG, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.

    ledCurrentSetVal = ((float)((0.483 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(81.0);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(35.0);

    }

    EALLOW;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // Activate GPIO40 as epwm7A to follow dimming

    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD); // Set up Halo dimmer PWM.

    EDIS;

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking

    ledMinDuty = WHITE_DIM_MIN;

    break;

    case HI440MA_WW_FAA: // HIRL L862 White - White

    /* SAMPLE 1 ledCurrentSetVal = ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(90.0);

    ledShortVoltage = _IQ15(60.0);

    */

    ledCurrentSetVal = ((float)((0.442 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0)); // SAMPLE 1

    ledOverCurrentSet = ((int)(0.675 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN)); // SAMPLE 1

    ledFastOV = (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN); // SAMPLE 1

    ledOpenVoltage = _IQ15(81.0); // SAMPLE 1 - Need to address cold temp variations

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(40); // SAMPLE 1 - Need to address cold temp variations

    }

    EALLOW;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; //Activate GPIO40 as epwm7A to follow dimming

    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD); // Set up Halo dimmer PWM.

    EDIS;

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking

    ledMinDuty = WHITE_DIM_MIN;

    break;

    case HI460MA_210_WW: // HIRL L862-WR; Fig2.10-WW

    pfcSetRef = _IQ12mpy(PFC_SET_12V_210_WW, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.

    ledCurrentSetVal = ((float)((0.470 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledCurrentSetValHalf = ((float)((0.235 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(90.0);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(50.0);

    }

    pfcUVHRun = PFC_UVH_12V_852G_NEW;

    pfcUVLRun = PFC_UVL_12V_852G_NEW;

    // ledPgain = KP_LED_1;

    // ledIgain = KI_LED_1;

    // ledDgain = KD_LED_1;

    EALLOW;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; //Activate GPIO40 as epwm7A to follow dimming

    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD); // Set up Halo dimmer PWM. Only used on Fig2.10 WW

    EDIS;

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking

    ledMinDuty = WHITE_DIM_MIN;

    break;

    case HI460MA_210_WY: // HIRL L862-WR; Fig2.10-WY, WR

    pfcSetRef = _IQ12mpy(PFC_SET_12V_210_WY, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.

    ledCurrentSetVal = ((float)((0.470 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(90.0);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(40.0);

    }

    pfcUVHRun = PFC_UVH_12V_852G_NEW;

    pfcUVLRun = PFC_UVL_12V_852G_NEW;

    EALLOW;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; //Activate GPIO40 as epwm7A to follow dimming

    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD); // Set up Halo dimmer PWM. Only used on Fig2.10 WW

    EDIS;

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking

    ledMinDuty = WHITE_DIM_MIN;

    break;

    case HI440MA_WY_FAA: // HIRL L862-WY

    ledCurrentSetVal = ((float)((0.442 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(81.0);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(40.0);

    }

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking

    ledMinDuty = WHITE_DIM_MIN;

    break;

    /* case HI470MA_BST: // HIRL L862-WG NON-FAA

    ledCurrentSetVal = ((float)((0.480 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    // ledOverCurrentSet = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOverCurrentSet = ((int)(0.720 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(65.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(90.0);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(25.0);

    }

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking

    break;

    */

    case HI500mA_BST: // L850CD, IREL White

    ledCurrentSetVal = ((float)((0.502 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(81.0);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(40.0);

    }

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking

    ledMinDuty = WHITE_DIM_MIN;

    break;

    case L804: // L804 configuration.

    ledCurrentSetVal = ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));//TOM

    ledOverCurrentSet = ((int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(VMAX_GUARD);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(VMIN_GUARD);

    }

    erglFlag = 1;

    blinkDisable = 1; // L804 alternates, so blinking is disabled.

    dimOverride = 0; // Dimming override set to 0 for wig/wag transition

    ledMaxUVErrors = ERGL_LED_MAX_UV_ERRORS;

    EALLOW;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 0;

    GpioCtrlRegs.GPBDIR.bit.GPIO40 = 1;

    GpioDataRegs.GPBCLEAR.bit.GPIO40 = 1;

    GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0; //TR 11-10-14 was GPIO34

    GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1; //TR 11-10-14 was GPIO34

    GpioDataRegs.GPBSET.bit.GPIO41 = 1; //TR 11-10-14 was GPIO34

    EDIS;

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '0';

    adName[3] = '4';

    adName[4] = '-';

    adName[5] = 'E';

    adName[6] = 'R';

    adName[7] = 'G';

    adName[8] = 'L';

    adName[9] = ' ';

    adName[10] = ' ';

    adName[11] = ' ';

    break;

    case L804_LEFT_FULL_ON: // L804 Photo Test

    ledCurrentSetVal = ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(VMAX_GUARD);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(VMIN_GUARD);

    }

    erglFlag = 0;

    blinkDisable = 1;

    ledMaxUVErrors = ERGL_LED_MAX_UV_ERRORS;

    EALLOW;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 0;

    GpioCtrlRegs.GPBDIR.bit.GPIO40 = 1;

    GpioDataRegs.GPBSET.bit.GPIO40 = 1;

    GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0; //TR 11-10-14 was GPIO34

    GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1; //TR 11-10-14 was GPIO34

    GpioDataRegs.GPBCLEAR.bit.GPIO41 = 1; //TR 11-10-14 was GPIO34

    EDIS;

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '0';

    adName[3] = '4';

    adName[4] = '-';

    adName[5] = 'E';

    adName[6] = 'R';

    adName[7] = 'G';

    adName[8] = 'L';

    adName[9] = ' ';

    adName[10] = ' ';

    adName[11] = ' ';

    break;

    case L804_RIGHT_FULL_ON: // L804 Photo Test

    ledCurrentSetVal = ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(VMAX_GUARD);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(VMIN_GUARD);

    }

    erglFlag = 0;

    blinkDisable = 1;

    ledMaxUVErrors = ERGL_LED_MAX_UV_ERRORS;

    EALLOW;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 0;

    GpioCtrlRegs.GPBDIR.bit.GPIO40 = 1;

    GpioDataRegs.GPBCLEAR.bit.GPIO40 = 1;

    GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0; //TR 11-10-14 was GPIO34

    GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1; //TR 11-10-14 was GPIO34

    GpioDataRegs.GPBSET.bit.GPIO41 = 1; //TR 11-10-14 was GPIO34

    EDIS;

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '0';

    adName[3] = '4';

    adName[4] = '-';

    adName[5] = 'E';

    adName[6] = 'R';

    adName[7] = 'G';

    adName[8] = 'L';

    adName[9] = ' ';

    adName[10] = ' ';

    adName[11] = ' ';

    break;

    case L852G_FULL_ON: // L852g Photo Test

    ledCurrentSetVal = ((float)((0.490 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(VMAX_GUARD);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(VMIN_GUARD);

    }

    erglFlag = 0;

    blinkDisable = 1; // Disable blinking for photo test.

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '5';

    adName[3] = '2';

    adName[4] = 'G';

    adName[5] = '-';

    adName[6] = 'I';

    adName[7] = 'R';

    adName[8] = 'G';

    adName[9] = 'L';

    adName[10] = ' ';

    adName[11] = ' ';

    break;

    case L852G_ODD: // L852G First OFF configuration

    ledCurrentSetVal = ((float)((0.490 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(VMAX_GUARD);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(VMIN_GUARD);

    }

    erglFlag = 0;

    guardFlag = 1;

    blinkDisable = 0;

    blinkState = 0;

    guardSyncOddEven = 0;

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '5';

    adName[3] = '2';

    adName[4] = 'G';

    adName[5] = '-';

    adName[6] = 'I';

    adName[7] = 'R';

    adName[8] = 'G';

    adName[9] = 'L';

    adName[10] = ' ';

    adName[11] = ' ';

    break;

    default: // L852G First ON configuration.

    ledCurrentSetVal = ((float)((0.490 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledFastOV = (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);

    ledOpenVoltage = _IQ15(VMAX_GUARD);

    if(ledCalValid != EEPROM_DATA_VALID)

    {

    ledShortVoltage = _IQ15(VMIN_GUARD);

    }

    erglFlag = 0;

    guardFlag = 1;

    blinkDisable = 0;

    blinkState = 1;

    guardSyncOddEven = 1;

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '5';

    adName[3] = '2';

    adName[4] = 'G';

    adName[5] = '-';

    adName[6] = 'I';

    adName[7] = 'R';

    adName[8] = 'G';

    adName[9] = 'L';

    adName[10] = ' ';

    adName[11] = ' ';

    break;

    }

     

    if(guardFlag == 1)

    {

    EALLOW;

    syncDriveConfig(7, HEATER_PERIOD); // Set up PWM to sync.

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // Sync pwm output

     

    #if HARDWARE_21696

    GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBDIR.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;

    #else

    GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;

    GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;

    #endif

    EDIS;

    }

    if(oddEvenConfigValid == EEPROM_DATA_VALID)

    {

    if(oddEvenConfig == ODD) // If blink config is saved in EEPROM, it overrides the jumper setting.

    {

    blinkState = 0;

    guardSyncOddEven = 0;

    }

    else if (oddEvenConfig == EVEN)

    {

    blinkState = 1;

    guardSyncOddEven = 1;

    }

    }

    else

    {

    oddEvenConfig = blinkState|0xF0;

    }

    if(phaseConfigValid == EEPROM_DATA_VALID)

    {

    if(phaseConfig == DEFAULT_PHASE) // If phase config is saved in EEPROM, use alternate phase.

    {

    blinkTimer = 0;

    }

    else if(phaseConfig == ALTERNATE_PHASE)

    {

    blinkTimer = 116;

    }

    }

    else

    {

    phaseConfig = 2;

    }

    }

    else // ELEVATED TAXIWAY AND INSET FIXTURES

    {

    // Common settings for TEL and insets:

    if(hirlJumper == 0)

    {

    // PFC controller set point and protection limits.

    pfcSetRef = _IQ12mpy(PFC_SET_HIRL, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    pfcOVH = PFC_OVH_HIRL;

    pfcOVL = PFC_OVL_HIRL;

    pfcUVHRun = PFC_UVH_HIRL;

    pfcUVLRun = PFC_UVL_HIRL;

    pfcUVH = PFC_UVH_STARTUP;

    pfcUVL = PFC_UVL_STARTUP;

    // PFC controller coefficients.

    pfcPgain = KP_PFC;

    pfcIgain = KI_PFC;

    pfcDgain = KD_PFC;

    pfcFastPgain = KP_PFC_FAST;

    pfcFastIgain = KI_PFC_FAST;

    CNTL_2P2Z_CLA_Fdbk1 = &ledCurrentAdc; // LED feedback current.

    // LED controller coefficients.

    ledPgain = KP_LED;

    ledIgain = KI_LED;

    ledDgain = KD_LED;

    ledFFgain = KFF_LED;

    Kff = &ledFFgain; // LED feed-forward variables.

    vFFvMin = VFF_MIN55; //was 32

    ledOpenVoltage = _IQ15(VMAX_HIRL);

    syncDisableFlag = 2; // Enable dimming and auto sync of LED.

    dimDisable = 0;

    blinkDisable = 1; // Disable blinking for these models

    ledFastOVDisable = 1;

    ledMinDuty = WHITE_DIM_MIN;

    ledFastOV = (unsigned int)(60.0 * LED_VINPUT_GAIN * ADC_GAIN);

    heatOnTemp = 12.4;

    heatOffTemp = 34.0;

    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL = DC_COMP3OUT; // LED Buck. TR 2-23-17: Added to protect LED driver during oupt transients

    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL = DC_COMP3OUT; // Dimmer.

     

    switch(configJumpers) // Fixture specific set point and protection limits set by jumpers.

    {

    case TEST_MODE:

    ledCurrentSetVal = ((float)((0.900 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.5 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL = DC_COMP3OUT; // LED Buck.

    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL = DC_COMP3OUT; // Dimmer.

    dimDisable = 1;

    dimOverride = 37501;

    test_timer = TEST_CYCLE_TIME;

    break;

    case I630MA: // 2.8-R, 2.3-G, 2.3/8-GR Jumper Diagram 4

    ledCurrentSetVal = ((float)((0.650 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    /* case I450MA: // L862-YR, 2.10-YR

    ledCurrentSetVal = ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    /* case I475MA: // FAA yellow-green

    ledCurrentSetVal = ((float)((0.475 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    case HI475MA_YG_FAA: // L862 YG Jumper Diagram 29

    pfcSetRef = _IQ12mpy(PFC_SET_HIRL_50, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.475 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(45.0);

    break;

    case HI450MA_YR_FAA: // L862 YR Jumper Diagram 2

    pfcSetRef = _IQ12mpy(PFC_SET_HIRL_50, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(45.0);

    break;

     

    case I525MA_852G_H_OFF: // L852G/S First OFF Jumper Diagram 7

    pfcSetRef = _IQ12mpy(PFC_SET_HIRL_852G_S_SIGN, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    pfcOVH = PFC_OVH_HIRL_852G_S_SIGN;

    pfcOVL = PFC_OVL_HIRL_852G_S_SIGN;

    pfcUVHRun = PFC_UVH_HIRL_852G_S_SIGN;

    pfcUVLRun = PFC_UVL_HIRL_852G_S_SIGN;

    pfcUVH = PFC_UVH_STARTUP_852G_S_SIGN;

    pfcUVL = PFC_UVL_STARTUP_852G_S_SIGN;

    // PFC controller coefficients.

    pfcPgain = KP_PFC_SIGN;

    pfcIgain = KI_PFC_SIGN;

    pfcDgain = KD_PFC_SIGN;

    pfcFastPgain = KP_PFC_FAST_SIGN;

    pfcFastIgain = KI_PFC_FAST_SIGN;

    ledPgain = KP_SIGN;

    ledIgain = KI_SIGN;

    ledDgain = KD_SIGN;

    ledFFgain = KFF_SIGN;

    ledCurrentSetVal = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0)); // This particular unit was 6/5% low, so the current had to be set at 586 mA

    ledOverCurrentSet = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(VMAX_HIRL_2_852G_S_SIGN);

    erglFlag = 0;

    guardFlag = 1;

    blinkDisable = 0;

    blinkState = 0;

    blinkTimer = 0;

    dimOverride = 0;

    guardSyncOddEven = 1;

    syncDisableFlag = 0;

    ledRestartMax = BOOST_RESTART_MAX;

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '5';

    adName[3] = '2';

    adName[4] = 'G';

    adName[5] = ' ';

    adName[6] = ' ';

    adName[7] = ' ';

    adName[8] = ' ';

    adName[9] = ' ';

    adName[10] = ' ';

    adName[11] = ' ';

    /* if(guardFlag == 1)

    {

    EALLOW;

    syncDriveConfig(7, HEATER_PERIOD); // Set up PWM to sync.

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // Sync pwm output

    #if HARDWARE_21696

    GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBDIR.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;

    #else

    GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;

    GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;

    #endif

    EDIS;

    }

    */

    break;

    case I525MA_852G_H_ON: // L852G/S First ON Jumper Diagram 5

    pfcSetRef = _IQ12mpy(PFC_SET_HIRL_852G_S_SIGN, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    pfcOVH = PFC_OVH_HIRL_852G_S_SIGN;

    pfcOVL = PFC_OVL_HIRL_852G_S_SIGN;

    pfcUVHRun = PFC_UVH_HIRL_852G_S_SIGN;

    pfcUVLRun = PFC_UVL_HIRL_852G_S_SIGN;

    pfcUVH = PFC_UVH_STARTUP_852G_S_SIGN;

    pfcUVL = PFC_UVL_STARTUP_852G_S_SIGN;

    // PFC controller coefficients.

    pfcPgain = KP_PFC_SIGN;

    pfcIgain = KI_PFC_SIGN;

    pfcDgain = KD_PFC_SIGN;

    pfcFastPgain = KP_PFC_FAST_SIGN;

    pfcFastIgain = KI_PFC_FAST_SIGN;

    ledPgain = KP_SIGN;

    ledIgain = KI_SIGN;

    ledDgain = KD_SIGN;

    ledFFgain = KFF_SIGN;

    ledCurrentSetVal = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0)); // This particular unit was 6/5% low, so the current had to be set at 586 mA

    ledOverCurrentSet = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(VMAX_HIRL_2_852G_S_SIGN);

    f1 = _IQtoF(ledOpenVoltage);

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "------- ledOpenVoltage: %.2f \r\n", f1);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    erglFlag = 0;

    guardFlag = 1;

    blinkDisable = 0;

    blinkState = 1;

    blinkTimer = 0;

    dimOverride = 0;

    guardSyncOddEven = 1;

    syncDisableFlag = 0;

    ledRestartMax = BOOST_RESTART_MAX;

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '5';

    adName[3] = '2';

    adName[4] = 'G';

    adName[5] = ' ';

    adName[6] = ' ';

    adName[7] = ' ';

    adName[8] = ' ';

    adName[9] = ' ';

    adName[10] = ' ';

    adName[11] = ' ';

    /* if(guardFlag == 1)

    {

    EALLOW;

    syncDriveConfig(7, HEATER_PERIOD); // Set up PWM to sync.

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // Sync pwm output

    #if HARDWARE_21696

    GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1;

    GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;

    #else

    GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;

    GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;

    #endif

    EDIS;

    }

    */

    break;

    case I525MA_852G_H_FULL_ON: // L852G/S Full ON Jumper Diagram 6

    pfcSetRef = _IQ12mpy(PFC_SET_HIRL_852G_S_SIGN, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    pfcOVH = PFC_OVH_HIRL_852G_S_SIGN;

    pfcOVL = PFC_OVL_HIRL_852G_S_SIGN;

    pfcUVHRun = PFC_UVH_HIRL_852G_S_SIGN;

    pfcUVLRun = PFC_UVL_HIRL_852G_S_SIGN;

    pfcUVH = PFC_UVH_STARTUP_852G_S_SIGN;

    pfcUVL = PFC_UVL_STARTUP_852G_S_SIGN;

    // PFC controller coefficients.

    pfcPgain = KP_PFC_SIGN;

    pfcIgain = KI_PFC_SIGN;

    pfcDgain = KD_PFC_SIGN;

    pfcFastPgain = KP_PFC_FAST_SIGN;

    pfcFastIgain = KI_PFC_FAST_SIGN;

    ledPgain = KP_SIGN;

    ledIgain = KI_SIGN;

    ledDgain = KD_SIGN;

    ledFFgain = KFF_SIGN;

    ledCurrentSetVal = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(VMAX_HIRL_2_852G_S_SIGN);

    f1 = _IQtoF(ledOpenVoltage);

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "------- ledOpenVoltage: %.2f \r\n", f1);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    erglFlag = 0;

    guardFlag = 1;

    blinkDisable = 1;

    guardSyncOddEven = 1;

    syncDisableFlag = 0;

    ledRestartMax = BOOST_RESTART_MAX;

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '5';

    adName[3] = '2';

    adName[4] = 'G';

    adName[5] = ' ';

    adName[6] = ' ';

    adName[7] = ' ';

    adName[8] = ' ';

    adName[9] = ' ';

    adName[10] = ' ';

    adName[11] = ' ';

    /* if(guardFlag == 1)

    {

    EALLOW;

    syncDriveConfig(7, HEATER_PERIOD); // Set up PWM to sync.

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // Sync pwm output

    #if HARDWARE_21696

    GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBDIR.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;

    #else

    GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;

    GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;

    #endif

    EDIS;

    }

    */

    break;

    case NO_JUMPERS: // L852G/S Calibration mode Jumper Diagram 1

    pfcSetRef = _IQ12mpy(PFC_SET_HIRL_852G_S_SIGN, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    pfcOVH = PFC_OVH_HIRL_852G_S_SIGN;

    pfcOVL = PFC_OVL_HIRL_852G_S_SIGN;

    pfcUVHRun = PFC_UVH_HIRL_852G_S_SIGN;

    pfcUVLRun = PFC_UVL_HIRL_852G_S_SIGN;

    pfcUVH = PFC_UVH_STARTUP_852G_S_SIGN;

    pfcUVL = PFC_UVL_STARTUP_852G_S_SIGN;

    // PFC controller coefficients.

    pfcPgain = KP_PFC_SIGN;

    pfcIgain = KI_PFC_SIGN;

    pfcDgain = KD_PFC_SIGN;

    pfcFastPgain = KP_PFC_FAST_SIGN;

    pfcFastIgain = KI_PFC_FAST_SIGN;

    ledPgain = KP_SIGN;

    ledIgain = KI_SIGN;

    ledDgain = KD_SIGN;

    ledFFgain = KFF_SIGN;

    ledCurrentSetVal = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(VMAX_HIRL_2_852G_S_SIGN);

    erglFlag = 0;

    guardFlag = 1;

    blinkDisable = 0;

    guardSyncOddEven = 1;

    syncDisableFlag = 0;

    ledRestartMax = BOOST_RESTART_MAX;

    ledShortVoltage = 0;

    ledCalTimer = 0;

    runLedCal = 1;

    oddEvenConfigValid = 0xFF;

    oddEvenConfig = 0xFF;

    phaseConfigValid = 0xFF;

    phaseConfig = 0xFF;

    dimRampConfigValid = 0xFF;

    dimRampConfig = 0xFF;

    ledCalValid = 0xFF;

    ledShortConfig = _IQ15(0.0);

    hardwareConfigValid = 0xFF;

    hardwareConfig = 0xFF;

    processEEPROMwrite(CONFIGDATA0, eepromData);

    i2cStatus = transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);

    if(i2cStatus == I2C_ERROR)

    {

    i2cErrorCount++;

    }

    adName[0] = 'L';

    adName[1] = '8';

    adName[2] = '5';

    adName[3] = '2';

    adName[4] = 'G';

    adName[5] = ' ';

    adName[6] = ' ';

    adName[7] = ' ';

    adName[8] = ' ';

    adName[9] = ' ';

    adName[10] = ' ';

    adName[11] = ' ';

    /* if(guardFlag == 1)

    {

    EALLOW;

    syncDriveConfig(7, HEATER_PERIOD); // Set up PWM to sync.

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // Sync pwm output

    #if HARDWARE_21696

    GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBDIR.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;

    GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;

    #else

    GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;

    GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;

    GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;

    #endif

    EDIS;

    }

    */

    break;

    default: // 350 mA

    ledCurrentSetVal = ((float)((0.350 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.650 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    } // END switch(configJumpers) (HIRL fixture configuration)

    // Read configuration settings from EEPROM for initial setup.

    i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, CONFIGDATA0, eepromData); // Read EEPROM data.

    if(i2cStatus == I2C_ERROR) // Check for errors and timeout to prevent getting

    { // stuck on a bus hang-up.

    i2cErrorCount++;

    }

    if(eepromData[0] != EEPROM_DATA_VALID) // Check if the data just read is valid or an erased EEPROM.

    {

    i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK1, CONFIGDATA0, eepromData); // Attempt to read EEPROM from secondary config block.

    if(i2cStatus == I2C_ERROR)

    {

    i2cErrorCount++;

    }

    if(eepromData[0] != EEPROM_DATA_VALID)

    {

    initEEPROMpage(CONFIGDATA0); // If secondary block was invalid, set config data to factory values.

    }

    else

    {

    processEEPROMread(CONFIGDATA0, eepromData); // If secondary block was valid, read it in as config data.

    }

    }

    else

    {

    processEEPROMread(CONFIGDATA0, eepromData); // If primary block was valid, read it in as config data.

    }

     

    if(oddEvenConfigValid == EEPROM_DATA_VALID)

    {

    if(oddEvenConfig == ODD) // If blink config is saved in EEPROM, it overrides the jumper setting.

    {

    blinkState = 0;

    guardSyncOddEven = 0;

    }

    else if (oddEvenConfig == EVEN)

    {

    blinkState = 1;

    guardSyncOddEven = 1;

    }

    }

    else

    {

    oddEvenConfig = blinkState|0xF0;

    }

    if(phaseConfigValid == EEPROM_DATA_VALID)

    {

    if(phaseConfig == DEFAULT_PHASE) // If phase config is saved in EEPROM, use alternate phase.

    {

    blinkTimer = 0;

    }

    else if(phaseConfig == ALTERNATE_PHASE)

    {

    blinkTimer = 116;

    }

    }

    else

    {

    phaseConfig = 2;

    }

    }

    else

    {

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "Buck Non-HIRL \r\n");

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    // PFC controller set point and protection limits.

    pfcSetRef = _IQ12mpy(PFC_SET_TEL_INSET, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    pfcOVH = PFC_OVH_TEL_INSET;

    pfcOVL = PFC_OVL_TEL_INSET;

    pfcUVHRun = PFC_UVH_TEL_INSET;

    pfcUVLRun = PFC_UVL_TEL_INSET;

    pfcUVH = PFC_UVH_STARTUP;

    pfcUVL = PFC_UVL_STARTUP;

    // PFC controller coefficients.

    pfcPgain = KP_PFC;

    pfcIgain = KI_PFC;

    pfcDgain = KD_PFC;

    pfcFastPgain = KP_PFC_FAST;

    pfcFastIgain = KI_PFC_FAST;

    CNTL_2P2Z_CLA_Fdbk1 = &ledCurrentAdc; // LED feedback current.

    // LED controller coefficients.

    ledPgain = KP_LED;

    ledIgain = KI_LED;

    ledDgain = KD_LED;

    ledFFgain = KFF_LED;

    Kff = &ledFFgain; // LED feed-forward variables.

    vFFvMin = VFF_MIN32;

    ledOpenVoltage = _IQ15(VMAX_INSET);

    syncDisableFlag = 2; // Enable dimming and auto sync of LED.

    dimDisable = 0;

    blinkDisable = 1; // Disable blinking for these models

    // blinkDisable = 0; // Enabled blinking for S-609 (Special)

    // blinkTimer = 0; // Enabled blinking for S-609 (Special)

    ledFastOVDisable = 1;

    ledMinDuty = WHITE_DIM_MIN;

    ledFastOV = (unsigned int)(28.0 * LED_VINPUT_GAIN * ADC_GAIN);

    if(GpioDataRegs.GPBDAT.bit.GPIO41 == 0)

    {

    failOpenFlag = 0;

    }

    switch(configJumpers) // Fixture specific set point and protection limits set by jumpers.

    {

    case TEST_MODE:

    ledCurrentSetVal = ((float)((0.900 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.5 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL = DC_COMP3OUT; // LED Buck.

    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL = DC_COMP3OUT; // Dimmer.

    dimDisable = 1;

    dimOverride = 37501;

    test_timer = TEST_CYCLE_TIME;

    break;

    case I925MA:

    ledCurrentSetVal = ((float)((0.925 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.5 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    case I875MA:

    ledCurrentSetVal = ((float)((0.875 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.5 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    case I525MA:

    ledCurrentSetVal = ((float)((0.525 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.800 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    /* case I475MA:

    ledCurrentSetVal = ((float)((0.475 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.0 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    case I425MA:

    ledCurrentSetVal = ((float)((0.425 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    /* case I100MA:

    ledCurrentSetVal = ((float)((0.100 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    case I950MA:

    ledCurrentSetVal = ((float)((0.950 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.500 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL = DC_COMP3OUT; // LED Buck.

    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL = DC_COMP3OUT; // Dimmer.

    break;

    /* case I200MA:

    ledCurrentSetVal = ((float)((0.200 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.550 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    /* case I300MA:

    ledCurrentSetVal = ((float)((0.300 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.600 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    /* case I750MA:

    ledCurrentSetVal = ((float)((0.750 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    case I900MA:

    ledCurrentSetVal = ((float)((0.900 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.500 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    heatOnTemp = 12.4;

    heatOffTemp = 34.0;

    break;

    case I800MA:

    ledCurrentSetVal = ((float)((0.800 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.300 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    case I400MA:

    ledCurrentSetVal = ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    case I550MA:

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    ledCurrentSetVal = ((float)((0.558 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    case I350MA_HTR:

    ledCurrentSetVal = ((float)((0.350 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    heatOnTemp = 12.4;

    heatOffTemp = 34.0;

    break;

    case I600MA:

    ledCurrentSetVal = ((float)((0.600 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.930 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    /* case I450MA_HTR:

    ledCurrentSetVal = ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    heatOnTemp = 12.4;

    heatOffTemp = 25.0;

    break;

    */

    case I400MA_HTR:

    ledCurrentSetVal = ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    heatOnTemp = 12.4;

    heatOffTemp = 34.0;

    break;

    case I500MA:

    ledCurrentSetVal = ((float)((0.500 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.800 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    heatOnTemp = 12.4;

    heatOffTemp = 34.0;

    break;

    case I1050MA:

    ledCurrentSetVal = ((float)((1.050 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.50 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL = DC_COMP3OUT; // LED Buck.

    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL = DC_COMP3OUT; // Dimmer.

    break;

    case I700MA:

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    ledCurrentSetVal = ((float)((0.607 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    heatOnTemp = 12.4;

    heatOffTemp = 34.0;

    break;

    case I225MA:

    ledCurrentSetVal = ((float)((0.225 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.525 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    case I060MA:

    ledCurrentSetVal = ((float)((0.060 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.150 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    /* case I1000MA:

    ledCurrentSetVal = ((float)((1.000 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.50 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL = DC_COMP3OUT; // LED Buck.

    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL = DC_COMP3OUT; // Dimmer.

    break;

    */

    case I150MA:

    ledCurrentSetVal = ((float)((0.150 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.450 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    case I250MA:

    ledCurrentSetVal = ((float)((0.250 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.550 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    case HI700MA_R_FAA:

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledOpenVoltage = _IQ15(25.00);

    ledCurrentSetVal = ((float)((0.638 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.930 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    /* case I085MA:

    ledCurrentSetVal = ((float)((0.085 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.150 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    case I850MA:

    pfcSetRef = _IQ12mpy(PFC_SET_SIGN_2, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.850 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.20 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    /* case I450MA:

    ledCurrentSetVal = ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    break;

    */

    case HI700MA_G_FAA:

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.708 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    break;

    case HI630MA_R_FAA:

    GpioDataRegs.GPBCLEAR.bit.GPIO34 = 1;

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.304 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    break;

    case HI700MA_GR_FAA:

    ledCurrentSetVal = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    break;

    case HI630MA_RR_FAA:

    ledCurrentSetVal = ((float)((0.630 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    break;

    case HI700MA_L862_GR:

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.708 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    break;

    case HI630MA_L862_RR:

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.638 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    break;

    case HI450MA_L850C_G:

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.458 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(25.0);

    break;

    /* case HI450MA_L850D_G: // HI450MA_L850C_G and HI450MA_L850D_G are the same

    pfcSetRef = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));

    ledCurrentSetVal = ((float)((0.458 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ledOpenVoltage = _IQ15(25.0);

     

    break;

    */

    default: //I350MA_1LED

    ledCurrentSetVal = ((float)((0.350 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));

    ledOverCurrentSet = ((unsigned int)(0.650 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));

    ;

    break;

    } // END switch(configJumpers) (Inset fixture configuration)

    } // END hirl settings

    } // END else

    //====================================================================================

    // ASSEMBLY VARIABLE LINKS

    //====================================================================================

    // The following variables are shared between the assembly and C environment. Pointer links assign

    // TIs DpLib functions to "net" variables used for various control algorithms.

    // PFC controller assignment (28x core)

    CNTL_2P2Z_Ref1 = &pfcVoltageRef; // PFC controller reference input.

    CNTL_2P2Z_Fdbk1 = &pfcVoltageAdc; // PFC feedback voltage.

    CNTL_2P2Z_Coef1 = &CNTL_2P2Z_CoefStruct1.vcntl; // Pointer to the PFC control coefficients.

    CNTL_2P2Z_Out1 = &pfcControlOut; // PFC controller output

    PWMDRV_1chHiRes_Duty1 = &pfcDuty; // PFC PWM driver.

    pfcSenseGain = PFC_SENSE_GAIN; // Circuit gains for sensing PFC voltage.

    syncCurrentH = SYNC_THRESHOLD_REF_H; // Sets current threshold used to detect AC input edges.

    syncCurrentL = SYNC_THRESHOLD_REF_L;

    // PFC controller equation is reduced to PID by converting coefficients.

    CNTL_2P2Z_CoefStruct1.vcntl = PFC_CNTL_REF;

    CNTL_2P2Z_CoefStruct1.b2 = _IQ6(pfcDgain);

    CNTL_2P2Z_CoefStruct1.b1 = _IQ6(pfcIgain - pfcPgain - pfcDgain - pfcDgain);

    CNTL_2P2Z_CoefStruct1.b0 = _IQ6(pfcPgain + pfcIgain + pfcDgain);

    CNTL_2P2Z_CoefStruct1.b1_fast = _IQ6(pfcFastIgain - pfcFastPgain - pfcDgain - pfcDgain);

    CNTL_2P2Z_CoefStruct1.b0_fast = _IQ6(pfcFastPgain + pfcFastIgain + pfcDgain);

    CNTL_2P2Z_CoefStruct1.a2 = _IQ26(0.0);

    CNTL_2P2Z_CoefStruct1.a1 = _IQ26(1.0);

    CNTL_2P2Z_CoefStruct1.max = 0x10000000;//0x02000000;

    CNTL_2P2Z_CoefStruct1.min = _IQ24(0.0);

    // LED controller assignment (CLA core)

    ADCDRV_1ch_CLA_Rlt1 = &ledCurrentAdc; // LED current ADC read driver.

    ADCDRV_1ch_CLA_Rlt6 = &vOutAdc; // LED anode voltage (used to regulate in voltage mode devices).

    CNTL_2P2Z_CLA_Ref1 = &ledCurrentRef; // LED set current.

    CNTL_2P2Z_CLA_Coef1 = &CNTL_2P2Z_CLACoefStruct_LED.b2; // Pointer to the LED control coefficients.

    CNTL_2P2Z_CLA_Out1 = &ledDuty; // LED controller output

    PWMDRV_1chHiRes_CLA_Duty6 = &ledDuty; // LED PWM driver.

    // LED controller equation is reduced to PID by converting coefficients.

    CNTL_2P2Z_CLACoefStruct_LED.b2 = (ledDgain);

    CNTL_2P2Z_CLACoefStruct_LED.b1 = (ledIgain - ledPgain - ledDgain - ledDgain);

    CNTL_2P2Z_CLACoefStruct_LED.b0 = (ledPgain + ledIgain + ledDgain);

    CNTL_2P2Z_CLACoefStruct_LED.a2 = (float)0.0;

    CNTL_2P2Z_CLACoefStruct_LED.a1 = (float)1.0;

    CNTL_2P2Z_CLACoefStruct_LED.max =(float)0.945;

    CNTL_2P2Z_CLACoefStruct_LED.min =(float)0.05;

    ledCurrentSet = 0;

    Kff = &ledFFgain; // LED voltage feedforward pointer.

    // Enable shunt or hall ic current monitoring to support legacy hardware (see settings file).

    #if SHUNT_IC

    lineCurrentGain = LINE_CURRENT_GAIN_SHUNT;

    lineCurrentOffset = LINE_CURRENT_OFFSET_SHUNT;

    inputCurrentOffset = LINE_CURRENT_OFFSET_SHUNT_FP;

    pfcOCH = (int)(PFC_OC_H * SHUNT_IC_GAIN * ADC_GAIN * NUM_CURRENT_SAMPLES);

    pfcOCL = (int)(PFC_OC_L * SHUNT_IC_GAIN * ADC_GAIN * NUM_CURRENT_SAMPLES);

    #else

    lineCurrentGain = LINE_CURRENT_GAIN_HALL;

    lineCurrentOffset = LINE_CURRENT_OFFSET_HALL;

    inputCurrentOffset = LINE_CURRENT_OFFSET_HALL_FP;

    pfcOCH = (int)(PFC_OC_H * HALL_IC_GAIN * LINE_CURRENT_DIVIDER * ADC_GAIN * NUM_CURRENT_SAMPLES);

    pfcOCL = (int)(PFC_OC_L * HALL_IC_GAIN * LINE_CURRENT_DIVIDER * ADC_GAIN * NUM_CURRENT_SAMPLES);

    #endif

    // lineSenseGain = LINE_SENSE_GAIN;

    init_cla(); // Initialize CLA core and setup CLA memory.

    Comp1Regs.DACVAL.bit.DACVAL = ledOverCurrentSet; // Set DAC for LED 1 DC over-current trip point for <950mA versions.

    Comp3Regs.DACVAL.bit.DACVAL = ledOverCurrentSet; // Set DAC for LED 1 DC over-current trip point for =>950mA versions.

    EALLOW;

    EPwm5Regs.TZCLR.all = 0x007F; // Clear errors caused by 0A current limit.

    EPwm6Regs.TZCLR.all = 0x007F;

    EPwm4Regs.ETCLR.bit.INT = 1; // Clear any interrupts that may have occurred

    PieCtrlRegs.PIEACK.all = 0x0FFF; // at startup.

    EDIS;

    EINT; // Enable global interrupts.

    ERTM;

    EALLOW;

    // GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // Enable PFC PWM pins (left until now to prevent startup glitches

    GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1; // while PWMs are not configured).

    GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 1;

    GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 1;

    GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 1;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;

    EDIS;

    //-------------------------------------------------------------------------

    // Background Infinite Loop

    //-------------------------------------------------------------------------

    EALLOW;

    SysCtrlRegs.WDCR = 0x002F; // Activate watchdog.

    EDIS;

    for(;;) // Infinite background loop.

    {

    EALLOW;

    SysCtrlRegs.WDKEY = 0x0055; // Manage watchdog timer.

    SysCtrlRegs.WDKEY = 0x00AA;

    EDIS;

    MONITOR1_TOGGLE = 1; // Toggle monitoring signal. This signal and

    // another toggled in the interrupt routine give

    // an indication that the processor is running and

    // no errors are detected.

    //-------------------------------------------------------------------------

    // Background State Machine

    //-------------------------------------------------------------------------

    switch(currentState)

    {

    //-------------------------------------------------------------------------

    case PFC_BOOT: // STATE = "0"

    //-------------------------------------------------------------------------

    if(previousState != PFC_BOOT)

    {

    ledCurrentSet = 0;

    runtimeErrorsEnable = 0;

    pfcUVH = PFC_UVH_STARTUP;

    pfcUVL = PFC_UVL_STARTUP;

    #if HARDWARE_21696

    EALLOW;

    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0;

    EDIS;

    #endif

    GpioDataRegs.GPACLEAR.bit.GPIO25 = 1;

    pfcVoltageRef = PFC_START_REF;

    PCB_LED_OFF = 1;

    previousState = PFC_BOOT;

    }

    if(pfcVoltage > PFC_STARTUP_LEVEL) // When input current reaches correct level, begin startup.

    {

    EALLOW;

    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;

    EDIS;

    GpioDataRegs.GPASET.bit.GPIO25 = 1;

    PCB_LED_OFF = 1;

    currentState = CURRENT_LOW;

    }

    break;

    //-------------------------------------------------------------------------

    case CURRENT_LOW: // STATE = "0"

    //-------------------------------------------------------------------------

    // State entry

    if(previousState != CURRENT_LOW)

    {

    ledCurrentSet = 0;

    runtimeErrorsEnable = 0;

    pfcUVH = PFC_UVH_STARTUP;

    pfcUVL = PFC_UVL_STARTUP;

    pfcVoltageRef = PFC_START_REF;

    PCB_LED_OFF = 1;

    previousState = CURRENT_LOW;

    }

    // State exit

    if(currentFastIQ > _IQ26(2.0)) // When input current reaches correct level, begin startup.

    {

    currentState = INPUT_RAMP;

    }

    break;

    //-------------------------------------------------------------------------

    case INPUT_RAMP: // STATE = "1"

    //-------------------------------------------------------------------------

    // State entry

    if(previousState != INPUT_RAMP)

    {

    previousState = INPUT_RAMP;

    }

    // State operations

    pfcVoltageRef = pfcVoltageRef + 0x00008000; // Ramp PFC voltage reference to produce soft-start.

     

    // State exit

    if(pfcVoltageRef >= pfcSetRef) // If PFC voltage has reached its operating setpoint,

    { // move to the next state.

    pfcVoltageRef = pfcSetRef;

    currentState = LED_SOFT_START;

    }

    break;

    //-------------------------------------------------------------------------

    case LED_SOFT_START: // STATE = "2"

    //-------------------------------------------------------------------------

    // State entry

    if(previousState != LED_SOFT_START)

    {

    runtimeErrorsEnable = 0;

    ledCurrentSet = 0;

    ledCurrentRampVal = 0.00002;

    errorDelay = 0;

    previousState = LED_SOFT_START;

    PCB_LED_OFF = 1;

    warmUpDelayTimer = 0;

    warmUpDelayComplete = 0;

    startWarmUpDelay = 0;

    }

    if(ledRestart == 1)

    {

    ledCurrentRampVal = ledRestartVal;

    ledRestartCounter++;

    if(ledRestartCounter > LED_RESTART_WAIT)

    {

    EALLOW;

    EPwm6Regs.TZCLR.all = 0x2C;

    EDIS;

    ledRestartCounter = 0;

    ledRestart = 0;

    }

    }

    else

    {

    if (boostJumper == 0 && configJumpers == HI460MA_210_WW) // For FAA HIRL WW, delay startup in order to give it time to warm up

    {

    if (warmUpDelayComplete == 0)

    {

    ledCurrentSet = ledCurrentSetValHalf; // Set current to half the regular value

    }

    if(ledCurrentRef == ledCurrentSet)

    {

    startWarmUpDelay = 1;

    }

    if (startWarmUpDelay == 1 && warmUpDelayComplete == 0)

    {

    warmUpDelayTimer++; // Delay 10 seconds

    if (warmUpDelayTimer >= 1000)

    {

    ledCurrentSet = ledCurrentSetVal; // Set current to the regular value after the delay

    warmUpDelayComplete = 1;

    }

    }

    // State exit

    if(ledCurrentRef == ledCurrentSet && warmUpDelayComplete == 1)

    { // After the delay, go to RUN

    currentState = RUN;

    }

    }

    else

    {

    ledCurrentSet = ledCurrentSetVal; // Ramp LED current reference to produce soft-start.

    // State exit

    if(ledCurrentRef == ledCurrentSet) // If LED current has reached its operating setpoint,

    { // move to the next state.

    currentState = RUN;

    }

    }

    }

    break;

    //-------------------------------------------------------------------------

    case RUN: // STATE = "3"

    //-------------------------------------------------------------------------

    // if (ledShortVoltage > _IQ15(63.00) && ledShortVoltage < _IQ15(66.00))

    // {

    // GpioDataRegs.GPBSET.bit.GPIO34 = 1;

    // }

    if(previousState != RUN)

    {

    pfcUVH = pfcUVHRun;

    pfcUVL = pfcUVLRun; // Set UV points now that voltage is up.

    ledCurrentRampVal = 0.00002;

    errorDelay++;

    if(errorDelay >= 500) // Delay going into RUN to allow time for low power LEDs to ramp up.

    {

    runtimeErrorsEnable = 1;

    errorDelay = 0;

    previousState = RUN;

    PCB_LED_ON = 1; // Turn pcb indicator LED.

    }

    }

    if(runtimeErrorsEnable == 1)

    {

    runtimeErrorChecking(); // Check for various run-time errors.

    }

    // if((boostJumper == 1) && (signJumper == 1))

    /* if(signJumper == 1) // SAMPLE 1

    {

    if((runLedCal == 1) && (EPwm5Regs.CMPA.half.CMPA >= 37450))

    {

    if (ledShortVoltage > _IQ15(50) && ledShortVoltage < _IQ15(55))

    {

    GpioDataRegs.GPBCLEAR.bit.GPIO34 = 1; // uncomment if --> Set Low initially

    }

    runLedCal = 0;

    }

    */

    if((ledCalValid != EEPROM_DATA_VALID) && (bluetoothMode != CONFIG) && (runLedCal == 1))// && (EPwm5Regs.CMPA.half.CMPA >= 37450))

    {

    ledCalTimer++;

    if((ledCalTimer >= ledAutoCalTime) && (i2cTimer < I2C_AUTO_START))

    {

    if(boostJumper == 0)

    {

    if (configJumpers == HI460MA_210_WW) // WW ICAO - boost

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));

    }

    else if (configJumpers == HI460MA_210_WY) // WY ICAO - boost

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));

    }

    else if (configJumpers == HI440MA_WW_FAA) // WW FAA - boost

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.925));

    }

    else if (configJumpers == HI440MA_WY_FAA) // WY FAA - boost

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.955));

    }

    else if (configJumpers == HI450MA_WR_FAA) // WR FAA - boost

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.959));

    }

    else if (configJumpers == HI470MA_WG_FAA) // WG FAA - boost

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.961));

    }

    else if ( configJumpers == HI500mA_BST) // IREL WW 850CD

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));

    }

    else

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));

    }

    }

    else if (hirlJumper == 0)

    {

    if (configJumpers == HI450MA_YR_FAA) // YR FAA - H-buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.974));

    }

    else if (configJumpers == HI475MA_YG_FAA) // YG FAA - H-buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.972));

    }

    else if (configJumpers == I630MA) // GR ICAO - H-buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.667));

    }

    else

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));

    }

    }

    else if (hirlJumper == 1 && boostJumper == 1)

    {

    if (configJumpers == HI700MA_G_FAA) // G FAA - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));

    }

    else if (configJumpers == HI630MA_R_FAA) // R FAA - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));

    }

    else if (configJumpers == HI700MA_GR_FAA) // GR FAA - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.941));

    }

    else if (configJumpers == HI630MA_RR_FAA) // RR FAA - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.926));

    }

    else if (configJumpers == I700MA) // L850C-Y - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));

    }

    else if (configJumpers == I550MA) // L850D-R - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));

    }

    else if (configJumpers == HI700MA_R_FAA) // L862 R - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.833));

    }

    else if (configJumpers == HI700MA_L862_GR) // L862 GR - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.941));

    }

    else if (configJumpers == HI630MA_L862_RR) // L862 RR - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.926));

    }

    else if (configJumpers == HI450MA_L850C_G) // L850C G - buck

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));

    }

    // else if (configJumpers == HI450MA_L850D_G) // L850D G - buck HI450MA_L850C_G and HI450MA_L850D_G are the same

    // {

    // ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));

    // }

    else

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));

    }

    }

    else

    {

    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));

    }

    runLedCal = 0;

    ledCalValid = EEPROM_DATA_VALID;

    ledShortConfig = ledShortVoltage;

    processEEPROMwrite(CONFIGDATA0, eepromData);

    transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);

    if(i2cStatus == I2C_ERROR)

    {

    i2cErrorCount++;

    }

    ledCalTimer = 0;

    ledCalFinished = 1;

    timerOn = 0;

    ledOnTimer = 0;

    }

    }

    if(ledCalFinished == 1)

    {

    if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)

    {

    if (timerOn == 0)

    {

    if (blinkState == 1)

    {

    if (blinkTimer >= 26 && blinkTimer <= 32)

    {

    blinkDisable = 1;

    timerOn = 1;

    }

    }

    }

    else

    {

    if (ledOnTimer < 500)

    {

    ledOnTimer++;

    }

    else

    {

    if (blinkState == 1)

    {

    if (blinkTimer >= 26 && blinkTimer <= 32)

    {

    blinkDisable = 0;

    timerOn = 0;

    ledCalFinished = 0;

    }

    }

    }

    }

    }

    else

    {

    if(ledCalNotifyTimer < ledCalNotifyTime)

    {

    ledCalNotifyTimer++;

    ledManualBlinkTimer++;

    if(ledManualBlinkTimer >= 100)

    {

    if(dimDisable == 0)

    {

    dimDisable = 1;

    }

    else

    {

    dimDisable = 0;

    }

    ledManualBlinkTimer = 0;

    }

    }

    else

    {

    dimDisable = 0;

    ledManualBlinkTimer = 0;

    ledCalNotifyTimer = 0;

    ledCalFinished = 0;

    }

    }

    }

     

    // ERGL functions.

    switch(dimRampVal) // Dimming ramp timing (from off to full) determines ERGL timing.

    {

    case 1:

    erglLimit = 46;

    break;

    case 2:

    erglLimit = 46;

    break;

    case 3:

    erglLimit = 52;

    break;

    case 4:

    erglLimit = 54;

    break;

    case 5:

    erglLimit = 57;

    break;

    }

    // erglLimit = 46;

    if((erglFlag == 1)&&(connect == 0))

    {

    if(erglCounter == erglLimit)

    {

    ledCurrentSet = 0.0;

    dimDisable = 1;

    }

    if (erglCounter >= 64)

    {

    erglState = erglNextState;

    ledCurrentSet = ledCurrentSetVal;

     

    if((erglState == ERGL_LEFT_ON) || (erglState == ERGL_RIGHT_ON))

    {

    dimDisable = 0;

    }

     

    erglCounter = 0;

    }

    erglCounter++;

    switch(erglState)

    {

    case ERGL_LEFT_ON:

    ERGL_GPIO_LEFT_ON = 1;

    ERGL_GPIO_RIGHT_OFF = 1;

     

    if(erglRightFault == 1)

    {

    erglNextState = ERGL_RIGHT_FAULT;

    }

    else

    {

    erglNextState = ERGL_RIGHT_ON;

    }

    break;

    case ERGL_RIGHT_ON:

    ERGL_GPIO_RIGHT_ON = 1;

    ERGL_GPIO_LEFT_OFF = 1;

    if(erglLeftFault == 1)

    {

    erglNextState = ERGL_LEFT_FAULT;

    }

    else

    {

    erglNextState = ERGL_LEFT_ON;

    }

    break;

     

     

    case ERGL_LEFT_FAULT:

    ERGL_GPIO_LEFT_OFF = 1;

    ERGL_GPIO_RIGHT_OFF = 1;

    erglNextState = ERGL_RIGHT_ON;

    break;

     

    case ERGL_RIGHT_FAULT:

    ERGL_GPIO_RIGHT_OFF = 1;

    ERGL_GPIO_LEFT_OFF = 1;

    erglNextState = ERGL_LEFT_ON;

    break;

    } // End switch(erglState)

    if((erglCounter >= (erglLimit-15)) && (erglCounter <= erglLimit))

    {

    switch(erglState)

    {

    case ERGL_LEFT_ON:

    if(ledVoltage > ledOpenVoltage) // Check for LED open or shorted.

    {

    erglLeftOVErrorCount++;

    }

    else if(erglLeftOVErrorCount > 0)

    {

    erglLeftOVErrorCount--;

    }

    if(runtimeErrorsEnable && (ledVoltage < ledShortVoltage)) // Check for LED open or shorted.

    {

    erglLeftUVErrorCount++;

    }

    else if(erglLeftUVErrorCount > 0)

    {

    erglLeftUVErrorCount--;

    }

    if((erglLeftOVErrorCount > ledMaxOVErrors) || (erglLeftUVErrorCount > LED_MAX_UV_ERRORS)) // If errors are persistent, trigger a fault.

    {

    erglState = ERGL_LEFT_FAULT;

    erglLeftFault = 1;

    }

    break; // END case ERGL_LEFT_ON:

    case ERGL_RIGHT_ON:

    if(ledVoltage > ledOpenVoltage) // Check for LED open or shorted.

    {

    erglRightOVErrorCount++;

    }

    else if(erglRightOVErrorCount > 0)

    {

    erglRightOVErrorCount--;

    }

    if(runtimeErrorsEnable && (ledVoltage < ledShortVoltage)) // Check for LED open or shorted.

    {

    erglRightUVErrorCount++;

    }

    else if(erglRightUVErrorCount > 0)

    {

    erglRightUVErrorCount--;

    }

    if((erglRightOVErrorCount > ledMaxOVErrors) || (erglRightUVErrorCount > LED_MAX_UV_ERRORS)) // If errors are persistent, trigger a fault.

    {

    erglState = ERGL_RIGHT_FAULT;

    erglRightFault = 1;

    }

    break; // END case ERGL_RIGHT_ON:

    } // END switch(erglState) error checking

    } // END if((erglCounter >= 20) && (erglCounter <= 45))

    if((erglRightFault == 1) && (erglLeftFault == 1))

    {

    currentState = FAULT;

    ledVErrorLog++;

    }

    } // END if((erglFlag == 1)&&(connect == 0))

    static int currentSettlingTime = 0;

    // Input current self-calibration.

    long tempLong = currentFiltIQ - currentFastIQ; // Only perform current calibration if the value has

    // settled by checking filtered vs unfiltered current.

    if((tempLong > _IQ26(-0.02)) && (tempLong < _IQ26(0.02)))

    {

    currentSettlingTime++;

    if((currentFiltIQ > _IQ26(6.2)) && (currentSettlingTime > 500)) // If input current has reached 6.2A, it is assumed to

    { // to be 6.6A and an offset is added (see CLA interrupt).

    if(currentCalTimer > 0) // Allow time for the CLA interrupt to process the offset

    { // by checking a timer. In a regulator circuit that is operating

    currentCalFlag = 1; // correctly, this will eliminate variances in isolation transformers.

    currentCalTimer--;

    }

    else

    {

    currentCalFlag = 0;

    currentSettlingTime = 0;// Keep current from continuously calibrating.

    }

    }

    if(currentFiltIQ < _IQ26(5.8)) // If current value rises above 6.7A, it

    { // is recalibrated to 6.6A.

    currentCalTimer = 5;

    }

    }

    else

    {

    if(currentSettlingTime > 0)

    {

    currentSettlingTime--;

    }

    }

    if(test_timer > 0)

    {

    test_timer--;

    if((test_timer < 2000) && (test_timer >= 1000))

    {

    dimOverride = 167;

    }

    else if(test_timer < 1000)

    {

    dimOverride = 37501;

    }

    }

    if(pfcVoltage < PFC_SHUTDOWN_LEVEL)

    {

    i2cTimer = I2C_AUTO_START;

    currentState = PFC_BOOT;

    pfcUVErrorLog++;

    }

    else if(currentFastIQ < _IQ26(1.0))

    {

    i2cTimer = I2C_AUTO_START; // Attempt to save EEPROM data before shutdown

    currentState = CURRENT_LOW;

    // ledPeakOCErrorLog++; // Using ledPeakOCErrorLog to show Current is too low ((less than 1 Amp)

    }

    /* The commented out code below is to test the Watchdog. If GPIO22 is first pulled low and then

    GPIO23 is pulled low, then the code pulls GPIO27 low and goes into an infinite loop.

    if (FirstOneSet == 0) // One GPIO has been set

    {

    if ((GPIO22Value == 0 && GPIO23Value == 1 ) || (GPIO22Value == 1 && GPIO23Value == 0))

    {

    if (GPIO22Value == 0)

    {

    FirstSetGpio = 22;

    }

    else if (GPIO23Value == 0)

    {

    FirstSetGpio = 23;

    }

    FirstOneSet = 1;

    }

    }

    else

    {

    if (FirstSetGpio == 22 && GPIO23Value == 0)

    {

    SecondSetGpio = 23;

    }

    }

    if (FirstSetGpio == 22 && SecondSetGpio == 23)

    {

    for (;;)

    {

    GpioDataRegs.GPASET.bit.GPIO27 = 1;

    }

    }

    */

    break;

    //-------------------------------------------------------------------------

    case FAULT: // STATE = "4"

    //-------------------------------------------------------------------------

    if(previousState != FAULT)

    {

    ledCurrentSet = 0;

    runtimeErrorsEnable = 0;

    EALLOW;

    GpioDataRegs.GPACLEAR.bit.GPIO8 = 1; // GPIO8 is LED1_DIM

    GpioDataRegs.GPBCLEAR.bit.GPIO40 = 1; // GPIO40 is WIG_PWM

    GpioDataRegs.GPACLEAR.bit.GPIO10 = 1; // GPIO10 is LED1_PWM_H

    GpioDataRegs.GPACLEAR.bit.GPIO11 = 1; // GPIO11 is LED1_PWM_L

    GpioCtrlRegs.GPADIR.bit.GPIO8 = 1;

    GpioCtrlRegs.GPBDIR.bit.GPIO40 = 1;

    GpioCtrlRegs.GPADIR.bit.GPIO10 = 1;

    GpioCtrlRegs.GPADIR.bit.GPIO11 = 1;

    GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 0;

    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 0;

    GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 0;

    GpioCtrlRegs.GPADIR.bit.GPIO6 = 0; // Turn opto monitor to input (float).

    EDIS;

    previousState = FAULT;

    i2cTimer = I2C_AUTO_START; // Advance I2C timer to save data immediately.

    }

    if(failOpenFlag == 1)

    {

    failOpenOnTimer++;

    failOpenOffTimer++;

    if(failOpenOffTimer < FAIL_OPEN_OFF_LIMIT)

    {

    if(failOpenOnTimer < FAIL_OPEN_DUTY)

    {

    EALLOW;

    GpioDataRegs.GPACLEAR.bit.GPIO0 = 1;

    GpioCtrlRegs.GPADIR.bit.GPIO0 = 1;

    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0; // Set PFC PWM to output 0. This "opens" the

    EDIS; // front end for fail-open compatibility.

    }

    else

    {

    EALLOW;

    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;

    EDIS;

    }

    }

    else

    {

    EALLOW;

    GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;

    EDIS;

    }

    if(failOpenOnTimer > FAIL_OPEN_CYCLE)

    {

    failOpenOnTimer = 0;

    }

    if(failOpenOffTimer > FAIL_OPEN_OFF_TIME)

    {

    failOpenOffTimer = 0;

    failOpenOnTimer = 0;

    }

    }

    ledCalTimer = 0; // Prevent auto-cal if in FAULT.

    statusLedBlink++; // Blink pcb indicator LED to show fault.

    if(statusLedBlink == 75)

    {

    PCB_LED_TOGGLE = 1;

    statusLedBlink = 0;

    }

    break; //case FAULT:

    } // End switch(currentState)

    //--------------------------------------------------------------------------------

    // Common state code - the following code is executed regardless of machine state.

    //--------------------------------------------------------------------------------

    if (hirlJumper == 0 && configJumpers == NO_JUMPERS || configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == I525MA_852G_H_OFF)

    {

    // if(sendAdName < 2) // Send the name of this unit for BLE advertising

    // {

    // if(adTimer >= 5000)

    // {

    // sendSciAdData(BLE_WRITE_AD_NAME, adName, 12);

    // sendAdName++;

    // adTimer = 0;

    // }

    //

    // adTimer++;

    // }

    }

    else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)

    {

    // if(sendAdName < 2) // Send the name of this unit for BLE advertising

    // {

    // if(adTimer >= 5000)

    // {

    // sendSciAdData(BLE_WRITE_AD_NAME, adName, 12);

    // sendAdName++;

    // adTimer = 0;

    // }

    //

    // adTimer++;

    // }

    }

    else

    {

    if(sendAdName < 2) // Send the name of this unit for BLE advertising

    {

    if(adTimer >= 100)

    {

    sendSciAdData(BLE_WRITE_AD_NAME, adName, 12);

    sendAdName++;

    adTimer = 0;

    }

    adTimer++;

    }

    }

    switch(syncDisableFlag)

    {

    case 0:

    EALLOW;

    EPwm6Regs.TBCTL.bit.PHSEN = TB_ENABLE;

    EPwm6Regs.DCACTL.bit.EVT1SYNCE = 1;

    EDIS;

    break;

    case 1:

    EALLOW; // Normally, the LED PWM is synchronized to the

    EPwm6Regs.TBCTL.bit.PHSEN = TB_DISABLE; // LED dimmer. When the LED is at full constant

    EPwm6Regs.DCACTL.bit.EVT1SYNCE = 0; // output, the synchronization continues to disturb

    EDIS; // the LED PWM and therefore LED current. To cancel

    break; // this effect, the LED PWM synchronization is only

    // active while dimming and is disabled at full

    case 2: // brightness.

    if(EPwm5Regs.CMPA.half.CMPA >= 37500)

    {

    EALLOW;

    EPwm6Regs.TBCTL.bit.PHSEN = TB_DISABLE;

    EPwm6Regs.DCACTL.bit.EVT1SYNCE = 0;

    EDIS;

    }

    else

    {

    EALLOW;

    EPwm6Regs.TBCTL.bit.PHSEN = TB_ENABLE;

    EPwm6Regs.DCACTL.bit.EVT1SYNCE = 1;

    EDIS;

    }

    break;

    }

    ledCurrentErrorChecking(); // Check for LED overcurrent errors.

    // Read various operating point variables with circuit scaling.

     

    long tempShift = ledVoltageCLA;

    ledVoltage = _IQ15mpy(tempShift<<15 , _IQ15(1.0/LED_VINPUT_GAIN * 1.0/ADC_GAIN));

    tempShift = ledCurrentCLA;

    ledCurrent = _IQ15mpy(tempShift<<15 , _IQ15(1.0/LED_DC_CS_GAIN * 1.0/LED_DC_CS_RESISTOR * 1.0/ADC_GAIN));

    tempShift = ADC7_hk12Voltage;

    v12Hk = _IQ15mpy(tempShift<<15 , _IQ15(1.0/V12GAIN * 1.0/ADC_GAIN));

    tempShift = linePeriod<<4;

    lineFrequency = _IQ4div(_IQ4(100000.0),linePeriod);

     

    if(bluetoothMode != CONFIG)

    {

    if((eepromInitFlag == 0) && (i2cTimer > 0)) // Check the EEPROM for valid data. If no valid data exists, reset the EEPROM data variables.

    {

    #if DEBUG_ERASE_EEPROM

    bytePointer = 0;

    while(bytePointer <= EEPROM_MAX_ADDRESS)

    {

    initEEPROMpage(bytePointer);

    bytePointer += 16;

    }

    bytePointer = 16;

    eepromInitFlag = 1;

    i2cTimer = I2C_AUTO_START;

    #else

    i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, bytePointer, eepromData); // Read EEPROM data.

    if(i2cStatus == I2C_ERROR) // Check for errors and timeout to prevent getting

    { // stuck on a bus hang-up.

    i2cErrorCount++;

    }

    if(eepromData[0] != EEPROM_DATA_VALID) // If primary data block is invalid (or new EEPROM) try secondary.

    {

    i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK1, bytePointer, eepromData); // Read secondary data.

    if(i2cStatus == I2C_ERROR)

    {

    i2cErrorCount++;

    }

    if(eepromData[0] != EEPROM_DATA_VALID)

    {

    initEEPROMpage(bytePointer); // If secondary block was invalid, erase this data set.

    }

    else

    {

    processEEPROMread(bytePointer, eepromData); // If secondary block was valid, read in this data set.

    }

    }

    else

    {

    processEEPROMread(bytePointer, eepromData); // If primary block was valid, read in this data set.

    }

    bytePointer += 16; // Move to the next set of data.

    if(bytePointer > EEPROM_MAX_ADDRESS) // If at the end of EEPROM data, reset pointers.

    {

    bytePointer = 16;

    eepromInitFlag = 1;

    i2cTimer = I2C_AUTO_START;

    }

    #endif

    }

    else

    {

    if(i2cTimer >= I2C_AUTO_START) // Continuously sends EEPROM data during runtime. Data is

    { // stored in two data blocks in case one is corrupted.

    processEEPROMwrite(bytePointer, eepromData);

    i2cStatus = transmitI2cData(EEPROM_SLAVE_ADDR|blockPointer, bytePointer, eepromData); // Send EEPROM data.

    if(i2cStatus == I2C_ERROR)

    {

    i2cErrorCount++;

    }

    bytePointer +=16; // Move to the next set of data.

    if(bytePointer > EEPROM_MAX_ADDRESS) // If at the end of EEPROM data, reset pointers.

    {

    bytePointer = 16;

    blockPointer++;

    if(blockPointer > EEPROM_BLOCK1)

    {

    blockPointer = 0;

    i2cTimer = 0;

    }

    }

    }

    }

    }

    if(i2cErrorCount > 100) // Attempt to restart the I2C bus if there are too many communication issues.

    {

    i2cErrorCount = 0;

    i2cRestart();

    }

    if(SciaRegs.SCIFFRX.bit.RXFFST == 4) // If serial messages were received, process them.

    {

    bleMessage = sciMessageRx(); // Communication received from Bluetooth link.

    if(bleMessage==0x37ED5199) // If a valid BT link, shut off LED to indicate connection.

    { // Wireless module will send this message upon connection.

    runtimeErrorsEnable = 0;

    if (hirlJumper == 0 && configJumpers == NO_JUMPERS || configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == I525MA_852G_H_OFF)

    { // Don't blink the LED for 852G's because it will interferes with regular

    turnLedOn = 1; // blinking, instead turn off the led for short while to indicate connection

    bleTimerOn = 0;

    bleLedOnTimer = 0;

    }

    else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)

    { // Don't blink the LED for 852G's because it will interferes with regular

    turnLedOn = 1; // blinking, instead turn off the led for short while to indicate connection

    bleTimerOn = 0;

    bleLedOnTimer = 0;

    }

    else

    {

    EALLOW;

    EPwm5Regs.TZFRC.bit.OST = 1;

    EDIS;

    connectTimer = 0;

    connect = 1;

    }

    }

    if(bleMessage==0xA6ED519A) // Messages compared to enter various wireless modes.

    {

    bluetoothMode = CONFIG;

    runtimeErrorsEnable = 0;

    bleMessage = 0;

    }

    else if(bleMessage==0xA6ED519B)

    {

    bluetoothMode = DIAGNOSTIC;

    runtimeErrorsEnable = 0;

    bleMessage = 0;

    }

    else

    {

    SciaRegs.SCIFFRX.bit.RXFIFORESET = 0; // If not a valid message, clear the serial FIFO.

    SciaRegs.SCIFFRX.bit.RXFIFORESET = 1;

    }

    }

    if(turnLedOn == 1)

    {

    if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)

    {

    if (bleTimerOn == 0)

    {

    if (blinkState == 1)

    {

    if (blinkTimer >= 26 && blinkTimer <= 32)

    {

    blinkDisable = 1;

    bleTimerOn = 1;

    }

    }

    }

    else

    {

    if (bleLedOnTimer < 500)

    {

    bleLedOnTimer++;

    }

    else

    {

    if (blinkState == 1)

    {

    if (blinkTimer >= 26 && blinkTimer <= 32)

    {

    blinkDisable = 0;

    bleTimerOn = 0;

    turnLedOn = 0;

    }

    }

    }

    }

    }

    else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)

    {

    if (bleTimerOn == 0)

    {

    if (blinkState == 1)

    {

    if (blinkTimer >= 26 && blinkTimer <= 32)

    {

    blinkDisable = 1;

    bleTimerOn = 1;

    }

    }

    }

    else

    {

    if (bleLedOnTimer < 500)

    {

    bleLedOnTimer++;

    }

    else

    {

    if (blinkState == 1)

    {

    if (blinkTimer >= 26 && blinkTimer <= 32)

    {

    blinkDisable = 0;

    bleTimerOn = 0;

    turnLedOn = 0;

    }

    }

    }

    }

    }

    }

    if(bluetoothMode == CONFIG) // Run the communication routines if in the correct mode.

    {

    configFunction();

    }

    else if(bluetoothMode == DIAGNOSTIC)

    {

    diagFunction();

    }

    if (toggleFlag == 1)

    {

    if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)

    {

    if (blinkState == 1 && blinkTimer >= 26 && blinkTimer <= 32)

    {

    if(bleData == CONFIG_VALUE_SET)

    {

    blinkDisable = 1;

    dimDisable = 0;

    toggleFlag = 0;

    }

    else

    {

    blinkDisable = 0;

    toggleFlag = 0;

    }

    }

    }

    else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)

    {

    if (blinkState == 1 && blinkTimer >= 26 && blinkTimer <= 32)

    {

    if(bleData == CONFIG_VALUE_SET)

    {

    blinkDisable = 1;

    dimDisable = 0;

    toggleFlag = 0;

    }

    else

    {

    blinkDisable = 0;

    toggleFlag = 0;

    }

    }

    }

    }

    if(connect == 1) // A certain amount of time after connection, LED is re-enabled.

    {

    connectTimer++;

    if(connectTimer > 700)

    {

    connect = 0;

    connectTimer = 0;

    runtimeErrorsEnable = 1;

    }

    else if(connectTimer > 500)

    {

    EALLOW;

    EPwm5Regs.TZCLR.bit.OST = 1;

    EDIS;

    }

    }

    #if DEBUG // Allow coefficient tuning in debug mode.

    CNTL_2P2Z_CoefStruct1.b2 = _IQ6(pfcDgain);

    CNTL_2P2Z_CoefStruct1.b1 = _IQ6(pfcIgain - pfcPgain - pfcDgain - pfcDgain);

    CNTL_2P2Z_CoefStruct1.b0 = _IQ6(pfcPgain + pfcIgain + pfcDgain);

    CNTL_2P2Z_CoefStruct1.b1_fast = _IQ6(pfcFastIgain - pfcFastPgain - pfcDgain - pfcDgain);

    CNTL_2P2Z_CoefStruct1.b0_fast = _IQ6(pfcFastPgain + pfcFastIgain + pfcDgain);

     

    CNTL_2P2Z_CLACoefStruct_LED.b2 = (ledDgain);

    CNTL_2P2Z_CLACoefStruct_LED.b1 = (ledIgain - ledPgain - ledDgain - ledDgain);

    CNTL_2P2Z_CLACoefStruct_LED.b0 = (ledPgain + ledIgain + ledDgain);

    #endif

    heatTempFilter += ADC5_heaterTemperature; // Accumulate heater temp ADC value for filtering.

    tempFiltCount++; // Increment filter count.

    if(tempFiltCount == 16)

    {

    heatTempFilter = heatTempFilter<<11; // Heater temp ADC value converted to IQ15

    heaterCelsius = getHeaterTemperature(heatTempFilter); // Convert heater ADC value to Celsius temp in IQ15.

    heatTempFilter = 0; // Reset temperature filter values.

    tempFiltCount = 0;

    }

    if(heaterCelsius > _IQ15(heatOffTemp)) // Activate or deactivate heater based on temperature limits.

    {

    EPwm3Regs.CMPA.half.CMPA = 200;

    }

    if(heatAmpCntl == 0) // Only turn on heater if amp control is not active.

    {

    if(heaterCelsius < _IQ15(heatOnTemp))

    {

    EPwm3Regs.CMPA.half.CMPA = 0;

    }

    }

    int heattemp = heaterCelsius>>15;

    if(heattemp > heaterMaxTempRun) // Compare and store maximum temperatures.

    {

    heaterMaxTempRun = heattemp;

    }

    long mcutemp = AdcResult.ADCRESULT13 - getTempOffset();

    mcuCelsius = mcutemp * getTempSlope();

    mcutemp = mcuCelsius>>15;

    if(mcutemp > mcuMaxTempRun)

    {

    mcuMaxTempRun = mcutemp;

    }

    if(mcutemp > mcuMaxTemp)

    {

    mcuMaxTemp = mcutemp;

    }

     

    if(sfoTimer >= 100) // The MEP (micro-edge placement) for the high-res PWM modules is

    { // periodically calibrated with the built-in SFO() function.

    sfoTimer = 0;

    if(sfoStatus == 1)

    {

    EALLOW;

    EPwm1Regs.HRMSTEP=MEP_ScaleFactor;

    EDIS;

    }

    sfoStatus = SFO();

    }

     

    // Read input current and determine regulator step.

    if(currentFiltIQ < _IQ26(2.65))

    {

    currentStep = B0;

    }

    if((currentFiltIQ >= _IQ26(2.65))&&(currentFiltIQ <_IQ26(2.95)))

    {

    currentStep = B1;

    }

    else if((currentFiltIQ > _IQ26(3.25))&&(currentFiltIQ <_IQ26(3.55)))

    {

    currentStep = B2;

    }

    else if((currentFiltIQ > _IQ26(3.95))&&(currentFiltIQ <_IQ26(4.25)))

    {

    currentStep = B3;

    }

    else if((currentFiltIQ > _IQ26(4.65))&&(currentFiltIQ < _IQ26(4.95)))

    {

    currentStep = B10;

    }

    else if((currentFiltIQ > _IQ26(5.05))&&(currentFiltIQ < _IQ26(5.35)))

    {

    currentStep = B4;

    }

    else if((currentFiltIQ > _IQ26(5.35))&&(currentFiltIQ <_IQ26(5.65)))

    {

    currentStep = B30;

    }

    else if((currentFiltIQ > _IQ26(6.45))&&(currentFiltIQ <_IQ26(6.75)))

    {

    currentStep = B5;

    }

    // CCR amp control:

    // A certain sequence of CCR transitions will activate or de-activate the heater.

    if((ccrMsgPending == 0) && ((ccrCntlStep == B5) && ((ccrCntlStepPrev == B4) || (ccrCntlStepPrev == B30))))

    {

    ccrMsgPending = 1;

    ccrCntlWord = 0;

    }

    if(ccrMsgPending)

    {

    if((ccrCntlStep == B5) && ((ccrCntlStepPrev == B4) || (ccrCntlStepPrev == B30)))

    {

    ccrCntlWord = ccrCntlWord << 1;

    ccrCntlWord |= 0x0001;

    ccrMsgBitCount++;

    }

    else if(((ccrCntlStep == B3) || (ccrCntlStep == B10)) && ((ccrCntlStepPrev == B4) || (ccrCntlStepPrev == B30)))

    {

    ccrCntlWord = ccrCntlWord << 1;

    ccrCntlWord &= 0xFFFE;

    ccrMsgBitCount++;

    }

    if(ccrMsgBitCount == 8) // If the correct number of current transitions is received, attempt to read the message

    { // and reset counters.

    ccrMsgReceived = 1;

    ccrMsgHeader = (ccrCntlWord>>4) & 0x000F;

    ccrMsgData = ccrCntlWord & 0x000F;

    ccrCntlTimer = 0;

    ccrMsgBitCount = 0;

    ccrMsgPending = 0;

    }

    ccrCntlTimer++;

    }

    if((ccrMsgReceived == 1) && (ccrMsgHeader == 0xD))

    {

    switch(ccrMsgData)

    {

    case HEATER_ON:

    EPwm3Regs.CMPA.half.CMPA = 0;

    heatAmpCntl = 1;

    break;

    case HEATER_OFF:

    EPwm3Regs.CMPA.half.CMPA = 200;

    heatAmpCntl = 1;

    break;

    case HEATER_AUTO: // Exit manual heater control (use temp sensor).

    heatAmpCntl = 0;

    break;

    case LED_CALIBRATE: // Exit manual heater control (use temp sensor).

    runLedCal = 1;

    ledCalValid = 0xFF;

    ledShortConfig = 0;

    ledShortVoltage = 0;

    ledCalTimer = ledAutoCalTime;

    break;

    }

    ccrMsgReceived = 0;

    ccrCntlWord = 0;

    }

    ccrCntlStepPrev = ccrCntlStep;

    ccrCntlStep = currentStep;

    if(ccrCntlTimer >= 3000) // If the correct current transitions are not received in time, reset the receiver.

    {

    ccrCntlTimer = 0;

    ccrMsgBitCount = 0;

    ccrMsgPending = 0;

    ccrMsgReceived = 0;

    ccrCntlWord = 0;

    }

    while (CpuTimer0Regs.TCR.bit.TIF == 0); // Waiting here causes the background loop to cycle in set 10ms intervals.

    CpuTimer0Regs.TCR.bit.TIF = 1;

    timer10ms++; // Increment timer variables.

    i2cTimer++;

    sciTimer++;

    sfoTimer++;

    if(timer10ms >= 100) // Update total operating seconds for each step.

    {

    timer10ms = 0;

    opSeconds++;

    switch(currentStep)

    {

    case B1:

    opSecondsB1++;

    break;

    case B2:

    opSecondsB2++;

    break;

    case B3:

    opSecondsB3++;

    break;

    case B10:

    opSecondsB10++;

    break;

    case B4:

    opSecondsB4++;

    break;

    case B30:

    opSecondsB30++;

    break;

    case B5:

    opSecondsB5++;

    break;

    }

    }

    } // End for(;;) (background loop)

    } //END MAIN CODE

    long sciMessageRx(void) // Get serial data and store it in a variable.

    {

    long temp;

    long rxData = 0;

    rxData = SciaRegs.SCIRXBUF.bit.RXDT;

    temp = SciaRegs.SCIRXBUF.bit.RXDT;

    temp = temp<<8;

    rxData |=temp;

    temp = SciaRegs.SCIRXBUF.bit.RXDT;

    temp = temp<<16;

    rxData |=temp;

    temp = SciaRegs.SCIRXBUF.bit.RXDT;

    temp = temp<<24;

    rxData |=temp;

    return rxData;

    }

    void sciMessageTx(int data[], int length) // Send serial data from a byte array.

    {

    int byte = 0;

    CpuTimer1Regs.TCR.bit.TRB = 1; // Reset 10ms cpu timer.

    CpuTimer1Regs.TCR.bit.TIF = 1;

    while((byte < length) && (CpuTimer1Regs.TCR.bit.TIF != 1))

    {

    if(SciaRegs.SCIFFTX.bit.TXFFST < 4)

    {

    SciaRegs.SCITXBUF = data[byte];

    byte++;

    }

    }

    }

    //-------------------------------------------------------------------------------------------------------

    // Check for and log operating errors.

    //-------------------------------------------------------------------------------------------------------

    void ledCurrentErrorChecking(void)

    {

    ledOCErrorReset++;

    if((EPwm6Regs.TZFLG.bit.DCAEVT1 == 1)||(EPwm6Regs.TZFLG.bit.DCBEVT1 == 1)) // Check for LED peak over current.

    {

    ledOCWaitCounter++;

    // Cla1ForceTask2();

    runtimeErrorsEnable = 0;

    if(ledOCErrorCount >= ledRestartMax) // Try to restart LED

    // if(ledOCErrorCount >= 5)

    {

    Cla1ForceTask2();

    if (ledOCErrorLog <= 65000)

    {

    ledOCErrorLog++;

    }

    currentState = FAULT;

    /*

    ledOCErrorCount = 0;

    ledOCWaitCounter = 0;

    ledCurrentSet = 0;

    EALLOW;

    EPwm6Regs.TZCLR.all = 0x0028; // Clear DC events, but leave OST triggered.

    EDIS;

    if((erglFlag == 0)&&(currentState != FAULT))

    {

    currentState = LED_SOFT_START;

    ledRestart = 1;

    }

    */

    }

    else if(ledOCWaitCounter > LED_OC_WAIT)

    {

    EALLOW;

    EPwm6Regs.TZCLR.all = 0x002C;

    EDIS;

    ledOCWaitCounter = 0;

    ledOCErrorCount++;

    ledOCErrorReset = 0;

    runtimeErrorsEnable = 1;

    }

    }

    else if(ledOCErrorReset >= LED_OC_RESET)

    {

    if(ledOCErrorCount > 0)

    {

    ledOCErrorCount--;

    }

    ledOCErrorReset = LED_OC_RESET;

    }

    }

    void runtimeErrorChecking(void)

    {

    float f1 = 0, f2 = 0;

    f1 = _IQtoF(ledOpenVoltage);

    f2 = _IQtoF(ledVoltage);

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "ledOpenVoltage(f1): %.2f \r\n", f1);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "ledOVoltage(f2): %.2f \r\n", f2);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    /*

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "ledOVErrorCount: %u \r\n", ledOVErrorCount);

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

    */

    memset(serialTxBytes, 0x00, sizeof(serialTxBytes));

    sprintf((char *)serialTxBytes, "---------------------- \r\n");

    sciMessageTx(serialTxBytes, strlen(serialTxBytes));

     

     

     

    if(erglFlag == 0) // ERGL led voltages must be timed with switching.

    { // ERGL voltage checking is located in ERGL section.

    if(ledVoltage > ledOpenVoltage) // Check for LED open or shorted.

    {

    ledOVErrorCount++;

    }

    else if(ledOVErrorCount > 0)

    {

    ledOVErrorCount--;

    }

    if(ledVoltage < ledShortVoltage) // Check for LED open or shorted.

    {

    ledUVErrorCount++;

    }

    else if(ledUVErrorCount > 0)

    {

    ledUVErrorCount--;

    }

    if((ledOVErrorCount > ledMaxOVErrors) || (ledUVErrorCount > ledMaxUVErrors)) // If errors are persistent, trigger a fault.

    {

    currentState = FAULT;

    ledVErrorLog++;

    }

    } // END if(erglFlag == 0)

    if(pfcOVErr == 1) // Check for PFC over voltage.

    {

    pfcOVErrorCount++;

    pfcOVErr = 0;

    if(pfcOVErrorCount > PFC_MAX_OV_ERRORS) // If errors are persistent, trigger a fault.

    {

    currentState = FAULT;

    pfcOVErrorLog++;

    }

    }

    else if(pfcOVErrorCount > 0)

    {

    pfcOVErrorCount--;

    }

    if(pfcOCErr == 1) // Check for PFC over current.

    {

    pfcOCErrorCount++;

    pfcOCErr = 0;

    if(pfcOCErrorCount > PFC_MAX_OC_ERRORS) // If errors are persistent, trigger a fault.

    {

    currentState = FAULT;

    pfcOCErrorLog++;

    }

    }

    else if(pfcOCErrorCount > 0)

    {

    pfcOCErrorCount--;

    }

    if((ADC7_hk12Voltage > VHK12_OV_REF)||(ADC7_hk12Voltage < VHK12_UV_REF)) // Check 12V housekeeping limits.

    {

    v12ErrorCount++;

    if(v12ErrorCount > V12_MAX_ERRORS) // If errors are persistent, trigger a fault.

    {

    currentState = FAULT;

    v12ErrorLog++;

    }

    }

    else if(v12ErrorCount > 0)

    {

    v12ErrorCount--;

    }

    }

    //-------------------------------------------------------------------------------------------------------

    // Calculate heater temperature using curve fit equations

    //-------------------------------------------------------------------------------------------------------

    long getHeaterTemperature(long adcValue)

    {

    long heatTempIQ15;

    // Heater thermistor curve fit equations:

    // y = 18.999x + 1151.6 from -10 to 20C

    // y = 0.0369x - 37.587 from 20 to 75C

    // y = 0.0713x - 143.37 from 75 to 125C

    if(adcValue < _IQ15(988.0))

    {

    heatTempIQ15 = _IQ15(-10);

    }

    else if(adcValue > _IQ15(3710.0))

    {

    heatTempIQ15 = _IQ15(125.0);

    }

    else if (adcValue < _IQ15(1557.0))

    {

    heatTempIQ15 = _IQ15mpy(_IQ15(0.0522), adcValue) - _IQ15(60.032);

    }

    else if (adcValue < _IQ15(3016.0))

    {

    heatTempIQ15 = _IQ15mpy(_IQ15(0.0369), adcValue) - _IQ15(37.587);

    }

    else

    {

    heatTempIQ15 = _IQ15mpy(_IQ15(0.0713), adcValue) - _IQ15(143.37);

    }

    return heatTempIQ15;

    }

    //-------------------------------------------------------------------------------------------------------

    // Initialization for CLA subprocessor.

    //-------------------------------------------------------------------------------------------------------

    void init_cla()

    {

    EALLOW;

    Cla1Regs.MVECT2 = (Uint16) (&Cla1Task2 - &Cla1Prog_Start)*sizeof(Uint32); // CLA tasks.

    Cla1Regs.MVECT4 = (Uint16) (&Cla1Task4 - &Cla1Prog_Start)*sizeof(Uint32);

    Cla1Regs.MVECT8 = (Uint16) (&Cla1Task8 - &Cla1Prog_Start)*sizeof(Uint32);

    Cla1Regs.MPISRCSEL1.bit.PERINT2SEL = CLA_INT2_NONE;

    Cla1Regs.MPISRCSEL1.bit.PERINT4SEL = CLA_INT4_EPWM4INT; // CLA Task 4 is triggered by PWM4.

    Cla1Regs.MPISRCSEL1.bit.PERINT8SEL = CLA_INT8_NONE; // CLA Task 8 is software triggered only.

    MemCopy(&Cla1funcsLoadStart, &Cla1funcsLoadEnd, &Cla1funcsRunStart); // Copy CLA program code to CLA RAM.

    Cla1Regs.MMEMCFG.bit.PROGE = 1; // Transfer CLA memory access to CLA.

    Cla1Regs.MCTL.bit.IACKE = 1; // Enable CLA task to be triggered by software.

    Cla1Regs.MIER.all = M_INT2|M_INT4|M_INT8; // Enable CLA task interrupts.

    Cla1ForceTask8(); // Start CLA Task 8 to initialize CLA

    // accessible variables.

    EDIS;

    }

    // When the EEPROM is New

    void initEEPROMpage(int byteAddress)

    {

    switch(byteAddress)

    {

    case CONFIGDATA0:

    oddEvenConfigValid = 0xFF;

    oddEvenConfig = 0xFF;

    phaseConfigValid = 0xFF;

    phaseConfig = 0xFF;

    dimRampConfigValid = 0xFF;

    dimRampConfig = 0xFF;

    ledCalValid = 0xFF;

    ledShortConfig = _IQ15(0.0);

    hardwareConfigValid = 0xFF;

    hardwareConfig = 0xFF;

    break;

    case EEPROMDATA0:

    opSeconds = 0;

    opSecondsB1 = 0;

    opSecondsB2 = 0;

    break;

    case EEPROMDATA1:

    opSecondsB3 = 0;

    opSecondsB10 = 0;

    opSecondsB4 = 0;

    break;

    case EEPROMDATA2:

    opSecondsB30 = 0;

    opSecondsB5 = 0;

    mcuMaxTemp = 0;

    break;

    case EEPROMDATA3:

    pfcOVErrorLog = 0;

    pfcUVErrorLog = 0;

    pfcOCErrorLog = 0;

    v12ErrorLog = 0;

    ledVErrorLog = 0;

    ledOCErrorLog = 0;

    ledPeakOCErrorLog = 0;

    break;

    }

    }

    //-------------------------------------------------------------------------------------------------------

    // EEPROM data is divided into 16 byte pages that can be written to the EEPROM using a page write.

    // The following function processes the data bytes read from the EEPROM and stores them in the proper

    // data variable.

    //-------------------------------------------------------------------------------------------------------

    void processEEPROMread(int byteAddress, int data[])

    {

    switch(byteAddress)

    {

    case CONFIGDATA0:

    oddEvenConfigValid = data[1];

    oddEvenConfig = data[2];

    phaseConfigValid = data[3];

    phaseConfig = data[4];

    dimRampConfigValid = data[5];

    dimRampConfig = data[6];

    ledCalValid = data[7];

    ledShortConfig = parseBytesToData(data, 8, 4);

    hardwareConfigValid = data[12];

    hardwareConfig = data[13];

    break;

    case EEPROMDATA0:

    opSeconds = parseBytesToData(data, 1, 4);

    opSecondsB1 = parseBytesToData(data, 5, 4);

    opSecondsB2 = parseBytesToData(data, 9, 4);

    break;

    case EEPROMDATA1:

    opSecondsB3 = parseBytesToData(data, 1, 4);

    opSecondsB10 = parseBytesToData(data, 5, 4);

    opSecondsB4 = parseBytesToData(data, 9, 4);

    break;

    case EEPROMDATA2:

    opSecondsB30 = parseBytesToData(data, 1, 4);

    opSecondsB5 = parseBytesToData(data, 5, 4);

    mcuMaxTemp = (int)parseBytesToData(data, 9, 2);

    break;

    case EEPROMDATA3:

    pfcOVErrorLog = (int)parseBytesToData(data, 1, 2);

    pfcUVErrorLog = (int)parseBytesToData(data, 3, 2);

    pfcOCErrorLog = (int)parseBytesToData(data, 5, 2);

    v12ErrorLog = (int)parseBytesToData(data, 7, 2);

    ledVErrorLog = (int)parseBytesToData(data, 9, 2);

    ledOCErrorLog = (int)parseBytesToData(data, 11, 2);

    ledPeakOCErrorLog = (int)parseBytesToData(data, 13, 2);

    break;

    }

    }

    //-------------------------------------------------------------------------------------------------------

    // EEPROM data is divided into 16 byte pages that can be written to the EEPROM using a page write.

    // The following function processes the data variables and stores them in a byte array to be

    // transmitted over I2C.

    //-------------------------------------------------------------------------------------------------------

    void processEEPROMwrite(int byteAddress, int data[])

    {

    switch(byteAddress)

    {

    case CONFIGDATA0:

    data[0] = EEPROM_DATA_VALID;

    data[1] = oddEvenConfigValid;

    data[2] = oddEvenConfig;

    data[3] = phaseConfigValid;

    data[4] = phaseConfig;

    data[5] = dimRampConfigValid;

    data[6] = dimRampConfig;

    data[7] = ledCalValid;

    parseDataToBytes(ledShortConfig, data, 8, 4);

    data[12] = hardwareConfigValid;

    data[13] = hardwareConfig;

    data[14] = 0xFF;

    data[15] = 0xFF;

    break;

    case EEPROMDATA0:

    data[0] = EEPROM_DATA_VALID;

    parseDataToBytes(opSeconds, data, 1, 4);

    parseDataToBytes(opSecondsB1, data, 5, 4);

    parseDataToBytes(opSecondsB2, data, 9, 4);

    data[13] = 0xFF;

    data[14] = 0xFF;

    data[15] = 0xFF;

    break;

    case EEPROMDATA1:

    data[0] = EEPROM_DATA_VALID;

    parseDataToBytes(opSecondsB3, data, 1, 4);

    parseDataToBytes(opSecondsB10, data, 5, 4);

    parseDataToBytes(opSecondsB4, data, 9, 4);

    data[13] = 0xFF;

    data[14] = 0xFF;

    data[15] = 0xFF;

    break;

    case EEPROMDATA2:

    data[0] = EEPROM_DATA_VALID;

    parseDataToBytes(opSecondsB30, data, 1, 4);

    parseDataToBytes(opSecondsB5, data, 5, 4);

    data[9] = mcuMaxTemp;

    data[11] = 0xFF; //ledMaxTemp&0x00FF;

    data[12] = 0xFF; //(ledMaxTemp>>8)&0x00FF;

    data[13] = 0xFF;

    data[14] = 0xFF;

    data[15] = 0xFF;

    break;

    case EEPROMDATA3:

    data[0] = EEPROM_DATA_VALID;

    parseDataToBytes(pfcOVErrorLog, data, 1, 2);

    parseDataToBytes(pfcUVErrorLog, data, 3, 2);

    parseDataToBytes(pfcOCErrorLog, data, 5, 2);

    parseDataToBytes(v12ErrorLog, data, 7, 2);

    parseDataToBytes(ledVErrorLog, data, 9, 2);

    parseDataToBytes(ledOCErrorLog, data, 11, 2);

    parseDataToBytes(ledPeakOCErrorLog, data, 13, 2);

    data[15] = 0xFF;

    break;

    }

    }

    long parseBytesToData(int data[], int offset, int length)

    {

    long temp1 = 0;

    long temp2 = 0;

    int shift = 0;

    while(length > 0)

    {

    temp1 |= data[offset];

    temp1 = temp1<<shift;

    temp2 = temp2 | temp1;

    temp1 = 0;

    offset++;

    shift += 8;

    length--;

    }

    return temp2;

    }

    void parseDataToBytes(long value, int data[], int offset, int length)

    {

    long temp = 0;

    temp = value;

    while(length > 0)

    {

    data[offset] = temp&0x000000FF;

    temp = temp>>8;

    offset++;

    length--;

    }

    }

    void sendSciData(int dataID, long dataValue, int dataLength)

    {

    serialTxBytes[0] = SERIAL_MESSAGE_HEAD; // Fixed message header required by wireless IC

    serialTxBytes[1] = BLE_SEND_ATTRIBUTE;

    serialTxBytes[2] = 8;

    parseDataToBytes(dataValue, serialTxBytes, 3, 4);

    serialTxBytes[7] = 0;

    serialTxBytes[8] = 0;

    serialTxBytes[9] = dataLength;

    serialTxBytes[10] = dataID;

    sciMessageTx(serialTxBytes, 11);

    }

    void sendSciAdData(int dataID, char data[], int length) // Sends name of this device for Bluetooth advertising.

    {

    serialTxBytes[0] = SERIAL_MESSAGE_HEAD;

    serialTxBytes[1] = dataID;

    serialTxBytes[2] = length;

    int k = 0;

    int l = 3;

    while( k < length )

    {

    serialTxBytes[l] = data[k];

    k++;

    l++;

    }

     

    sciMessageTx(serialTxBytes, (length + 3));

    }

    void configFunction() // Sends and receives various configuration data over the serial/Bluetooth link.

    {

    if(configOnEntry == 1)

    {

    sendSciData(CONFIG_MODE, CONFIG_MODE_ENTER, CONFIG_MODE_LENGTH);

    configOnEntry = 0;

    }

     

    if(bleNotify == 1)

    {

    receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, CONFIGDATA0, eepromData);

    processEEPROMread(CONFIGDATA0, eepromData);

    processEEPROMwrite(CONFIGDATA0, eepromData);

    transmitI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK1, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.

    switch(bleNotifyData)

    {

    case ODDEVEN_CONFIG:

    {

    sendSciData(ODDEVEN_CONFIG, oddEvenConfig, 1);

    break;

    }

    case PHASE_CONFIG:

    {

    sendSciData(PHASE_CONFIG, phaseConfig, 1);

    break;

    }

    case DIM_RAMP_CONFIG:

    {

    sendSciData(DIM_RAMP_CONFIG, dimRampConfig, 1);

    break;

    }

    case LED_SHORT_CAL:

    {

    sendSciData(LED_SHORT_CAL, ledShortVoltage, 2);

    break;

    }

    case LED_SHORT_CAL_SAVE:

    {

    sendSciData(LED_SHORT_CAL_SAVE, ledShortConfig, 2);

    break;

    }

    case HARDWARE_CONFIG_VALID:

    {

    sendSciData(HARDWARE_CONFIG_VALID, hardwareConfigValid, 1);

    break;

    }

    case HARDWARE_CONFIG:

    {

    sendSciData(HARDWARE_CONFIG, hardwareConfig, 1);

    break;

    }

    }

    bleNotify = 0;

    bleNotifyData = 0;

    }

    if(bleMessage != 0)

    {

    if(bleMessage == 0xA6ED519C) // Exit Configuration Mode (handset app has exited).

    {

    sendSciData(CONFIG_MODE, CONFIG_MODE_EXIT, CONFIG_MODE_LENGTH);

    bluetoothMode = 0;

    configOnEntry = 1;

    bleMessage = 0;

    runtimeErrorsEnable = 1;

    }

    long tempmessage = bleMessage;

    bleData = tempmessage&0x000000FF;

    tempmessage = tempmessage>>8;

    bleDataID = tempmessage&0x000000FF;

    tempmessage = tempmessage>>8;

    bleCommand = tempmessage&0x000000FF;

    tempmessage = tempmessage>>8;

    bleCode = tempmessage&0x000000FF;

    bleMessage = 0;

    configExitTimer = 0;

    if(bleCode == 0xA6)

    {

    if(bleCommand == SERIAL_WRITE_CONFIG)

    {

    switch(bleDataID)

    {

    case ODDEVEN_CONFIG:

    {

    if((bleData == CONFIG_VALUE_CLEARED)||(bleData ==CONFIG_VALUE_SET))

    {

    if(oddEvenConfig != bleData)

    {

    if(blinkState == 1)

    {

    blinkState = 0;

    guardSyncOddEven = 0;

    }

    else

    {

    blinkState = 1;

    guardSyncOddEven = 1;

    }

    }

    oddEvenConfigValid = EEPROM_DATA_VALID;

    oddEvenConfig = bleData;

    processEEPROMwrite(CONFIGDATA0, eepromData);

    transmitI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.

    bleNotify = 1; // Set to notify

    bleNotifyData = ODDEVEN_CONFIG;

    }

    break;

    }

    case PHASE_CONFIG:

    {

     

    if((bleData == 0x51)||(bleData == 0x50))

    {

    if(phaseConfig != bleData)

    {

    if(bleData == 0x51)

    {

    int temp = blinkTimer + 116;

    if(temp > 232)

    {

    temp = temp - 232;

    }

    blinkTimer = temp;

    }

    else if (bleData == 0x50)

    {

    int temp = blinkTimer - 116;

    if(temp < 0)

    {

    temp = temp + 232;

    }

    blinkTimer = temp;

    }

    }

    phaseConfigValid = EEPROM_DATA_VALID;

    phaseConfig = bleData;

    processEEPROMwrite(CONFIGDATA0, eepromData);

    transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.

    bleNotify = 1;

    bleNotifyData = PHASE_CONFIG;

    }

    break;

    }

    case FULLON_CONFIG:

    {

    if((bleData == CONFIG_VALUE_CLEARED)||(bleData == CONFIG_VALUE_SET))

    {

    if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)

    {

    toggleFlag = 1;

    }

    else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)

    {

    toggleFlag = 1;

    }

    else

    {

    if(bleData == CONFIG_VALUE_SET)

    {

    blinkDisable = 1;

    dimDisable = 0;

    }

    else

    {

    blinkDisable = 0;

    }

    }

    }

    break;

    }

    case DIM_RAMP_CONFIG:

    {

    if((bleData >= 1)&&(bleData <= 5))

    {

    dimRampVal = bleData;

    dimRampConfigValid = EEPROM_DATA_VALID;

    dimRampConfig = bleData;

    processEEPROMwrite(CONFIGDATA0, eepromData);

    transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.

    bleNotify = 1;

    bleNotifyData = DIM_RAMP_CONFIG;

    }

     

    break;

    }

    case LED_SHORT_CAL:

    {

    runLedCal = 1;

    bleNotify = 1;

    bleNotifyData = LED_SHORT_CAL;

    break;

    }

    case LED_SHORT_CAL_SAVE:

    {

    ledCalValid = EEPROM_DATA_VALID;

    ledShortConfig = ledShortVoltage;

    processEEPROMwrite(CONFIGDATA0, eepromData);

    transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);

    bleNotify = 1;

    bleNotifyData = LED_SHORT_CAL_SAVE;

    break;

    }

    case HARDWARE_CONFIG_VALID:

    {

    if(bleData != 0xFF)

    {

    hardwareConfigValid = bleData;

    if(hardwareConfigValid == hardwareConfig)

    {

    ledCalValid = 0xFF;

    ledShortConfig = 0;

    ledShortVoltage = 0;

    }

    }

    processEEPROMwrite(CONFIGDATA0, eepromData);

    transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.

    bleNotify = 1;

    bleNotifyData = HARDWARE_CONFIG_VALID;

    break;

    }

    case HARDWARE_CONFIG:

    {

    if(bleData != 0xFF)

    {

    hardwareConfig = bleData;

    if(hardwareConfigValid == hardwareConfig)

    {

    ledCalValid = 0xFF;

    ledShortConfig = 0;

    ledShortVoltage = 0;

    }

    }

    processEEPROMwrite(CONFIGDATA0, eepromData);

    transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.

    bleNotify = 1;

    bleNotifyData = HARDWARE_CONFIG;

    break;

    }

    } // End switch(bleDataID)

    } // End if(bleCommand == SERIAL_WRITE_CONFIG)

    else if(bleCommand == SERIAL_READ_CONFIG)

    {

    switch(bleDataID)

    {

    case ODDEVEN_CONFIG:

    {

    sendSciData(ODDEVEN_CONFIG, oddEvenConfig, 1);

    break;

    }

    case PHASE_CONFIG:

    {

    sendSciData(PHASE_CONFIG, phaseConfig, 1);

    break;

    }

    case DIM_RAMP_CONFIG:

    {

    sendSciData(DIM_RAMP_CONFIG, dimRampConfig, 1);

    break;

    }

    }

    }

    } // End if(bleCode == 0xA6)

    } // End if(bleCommand != 0)

    configExitTimer++;

    if(configExitTimer > 12000)

    {

    sendSciData(CONFIG_MODE, CONFIG_MODE_EXIT, CONFIG_MODE_LENGTH);

    bluetoothMode = 0;

    configOnEntry = 1;

    configExitTimer = 0;

    runtimeErrorsEnable = 1;

    }

    } // End ConfigFunction()

    void diagFunction() // Sends and receives diagnostic data over BLE/serial link.

    {

    if(diagOnEntry == 1)

    {

    sendSciData(DIAG_MODE, DIAG_MODE_ENTER, DIAG_MODE_LENGTH);

    diagOnEntry = 0;

    }

    if(bleMessage != 0)

    {

    if(bleMessage == 0xA6ED519C) // Exit Diagnostic Mode (handset app has exited).

    {

    sendSciData(DIAG_MODE, DIAG_MODE_EXIT, DIAG_MODE_LENGTH);

    bluetoothMode = 0;

    bleMessage = 0;

    diagOnEntry = 1;

    runtimeErrorsEnable = 1;

    }

    long tempmessage = bleMessage;

    bleData = tempmessage&0x000000FF;

    tempmessage = tempmessage>>8;

    bleDataID = tempmessage&0x000000FF;

    tempmessage = tempmessage>>8;

    bleCommand = tempmessage&0x000000FF;

    tempmessage = tempmessage>>8;

    bleCode = tempmessage&0x000000FF;

    bleMessage = 0;

    configExitTimer = 0;

     

    if(bleCommand == SERIAL_READ_DIAG)

    {

    switch(bleDataID)

    {

    case INPUT_CURRENT_CHAR:

    {

    sendSciData(INPUT_CURRENT_CHAR, currentFiltIQ, 4);

    }

    break;

    case PFC_VOLTAGE_CHAR:

    {

    sendSciData(PFC_VOLTAGE_CHAR, pfcVoltage, 4);

    }

    break;

    case LED_VOLTAGE_CHAR:

    {

    sendSciData(LED_VOLTAGE_CHAR, ledVoltage, 4);

    }

    break;

     

    case LED_CURRENT_CHAR:

    {

    sendSciData(LED_CURRENT_CHAR, ledCurrent, 4);

    }

    break;

    case CURRENT_STEP_CHAR:

    {

    sendSciData(CURRENT_STEP_CHAR, currentStep, 1);

    }

    break;

    case HEATER_TEMP_CHAR:

    {

    sendSciData(HEATER_TEMP_CHAR, (heaterCelsius >> 15), 1);;

    }

    break;

    case MCU_TEMP_CHAR:

    {

    sendSciData(MCU_TEMP_CHAR, (mcuCelsius >> 15), 1);

    }

    break;

    case MCU_MAX_TEMP_CHAR:

    {

    sendSciData(MCU_MAX_TEMP_CHAR, mcuMaxTemp, 1);;

    }

    break;

    case V12_VOLTAGE_CHAR:

    {

    sendSciData(V12_VOLTAGE_CHAR, v12Hk, 4);

    }

    break;

    case OPSECONDS_CHAR:

    {

    sendSciData(OPSECONDS_CHAR, opSeconds, 4);

    }

    break;

    case OPSECONDS_B1_CHAR:

    {

    sendSciData(OPSECONDS_B1_CHAR, opSecondsB1, 4);

    }

    break;

    case OPSECONDS_B2_CHAR:

    {

    sendSciData(OPSECONDS_B2_CHAR, opSecondsB2, 4);

    }

    break;

    case OPSECONDS_B3_CHAR:

    {

    sendSciData(OPSECONDS_B3_CHAR, opSecondsB3, 4);

    }

    break;

    case OPSECONDS_B10_CHAR:

    {

    sendSciData(OPSECONDS_B10_CHAR, opSecondsB10, 4);

    }

    break;

    case OPSECONDS_B4_CHAR:

    {

    sendSciData(OPSECONDS_B4_CHAR, opSecondsB4, 4);

    }

    break;

    case OPSECONDS_B30_CHAR:

    {

    sendSciData(OPSECONDS_B30_CHAR, opSecondsB30, 4);

    }

    break;

    case OPSECONDS_B5_CHAR:

    {

    sendSciData(OPSECONDS_B5_CHAR, opSecondsB5, 4);

    }

    break;

    case PFC_OV_ERROR_LOG_CHAR:

    {

    sendSciData(PFC_OV_ERROR_LOG_CHAR, pfcOVErrorLog, 2);

    }

    break;

    case PFC_UV_ERROR_LOG_CHAR:

    {

    sendSciData(PFC_UV_ERROR_LOG_CHAR, pfcUVErrorLog, 2);

    }

    break;

    case PFC_OC_ERROR_LOG_CHAR:

    {

    sendSciData(PFC_OC_ERROR_LOG_CHAR, pfcOCErrorLog, 2);

    }

    break;

    case V12_ERROR_LOG_CHAR:

    {

    sendSciData(V12_ERROR_LOG_CHAR, v12ErrorLog, 2);

    }

    break;

    case LED_VOLT_ERROR_LOG_CHAR:

    {

    sendSciData(LED_VOLT_ERROR_LOG_CHAR, ledVErrorLog, 2);

    }

    break;

    case LED_DC_OC_ERROR_LOG_CHAR:

    {

    sendSciData(LED_DC_OC_ERROR_LOG_CHAR, ledOCErrorLog, 2);

    }

    break;

    case LED_PEAK_OC_ERROR_LOG_CHAR:

    {

    sendSciData(LED_PEAK_OC_ERROR_LOG_CHAR, ledPeakOCErrorLog, 2);

    }

    break;

    case MACHINE_STATE_CHAR:

    {

    sendSciData(MACHINE_STATE_CHAR, currentState, 1);

    }

    break;

    case SVN_REV_CHAR:

    {

    sendSciData(SVN_REV_CHAR, svnRev, 2);

    }

    break;

    case SVN_DATE_STAMP_CHAR:

    {

    sendSciData(SVN_DATE_STAMP_CHAR, svnRevStamp, 4);

    }

    break;

    case CONFIG_JUMPERS_CHAR:

    {

    sendSciData(CONFIG_JUMPERS_CHAR, configJumpers, 1);

    }

    break;

    case ONBOARD_JUMPERS_CHAR:

    {

    int onBoardJumpers = 0;

    onBoardJumpers |= signJumper;

    onBoardJumpers = onBoardJumpers<<1;

    onBoardJumpers |= boostJumper;

    sendSciData(ONBOARD_JUMPERS_CHAR, onBoardJumpers, 1);

    }

    break;

    case HEATER_MAX_TEMP_RUN_CHAR:

    {

    sendSciData(HEATER_MAX_TEMP_RUN_CHAR, heaterMaxTempRun, 1);

    }

    break;

    case MCU_MAX_TEMP_RUN_CHAR:

    {

    sendSciData(MCU_MAX_TEMP_RUN_CHAR, mcuMaxTempRun, 1);

    }

    break;

    case LINE_FREQUENCY_CHAR:

    {

    sendSciData(LINE_FREQUENCY_CHAR, lineFrequency, 4);

    }

    break;

    case FIRMWARE_LETTER_REV:

    {

    sendSciData(FIRMWARE_LETTER_REV, firmwareRev, 2);

    }

    break;

    }

    }

    }

    configExitTimer++;

    if(configExitTimer > 12000)

    {

    sendSciData(DIAG_MODE, DIAG_MODE_EXIT, DIAG_MODE_LENGTH);

    bluetoothMode = 0;

    diagOnEntry = 1;

    configExitTimer = 0;

    runtimeErrorsEnable = 1;

    }

    } // End DiagFunction

    int ledCurrentRamp(volatile float *refPoint, float setPoint)

    {

    float diff = setPoint - *refPoint;

    if (fabs(diff) <= 0.2)

    {

    *refPoint = setPoint;

    return 1;

    }

    else

    {

    if(diff < 0)

    {

    *refPoint -= 0.2;

    }

    else

    {

    *refPoint += 0.2;

    }

    return 0;

    }

    }

    The Assembly Code. File 1

    ;----------------------------------------------------------------------------------

    ; FILE: ULEDPS-CLA-ISR.C

    ;

    ; Description: CLA core interrupt routine, LED controls and protection, input current calculation.

    ;

    ; Version: 0.0

    ;

    ; Target: TMS320F28035 (Piccolo)

    ;

    ;----------------------------------------------------------------------------------

    ; Copyright Cooper Technologies Co. © 2013

    ;----------------------------------------------------------------------------------

    ; $Author: traver.gumaer $

    ; $Date: 2014-12-09 12:33:53 -0500 (Tue, 09 Dec 2014) $

    ; $Id: ULEDPS-CLA-ISR.asm 338 2014-12-09 17:33:53Z traver.gumaer $

    ; $Rev: 338 $

    ; $URL: 10.91.161.17/.../ULEDPS-CLA-ISR.asm $

    ;

    ;Gives peripheral addresses visibility in assembly

    .cdecls C,LIST,"PeripheralHeaderIncludes.h"

    .cdecls C,LIST,"ULEDPS-Settings.h"

    ;Include files for the Power Library Macro's being used by the system

    .include "ADCDRV_1ch_CLA.asm"

    .include "PWMDRV_1chHiRes_CLA.asm"

    .include "CNTL_2P2Z_CLA.asm"

    ; .include "CLAShared.h"

    .def _CLA_CTRL_INIT ; Make CLA interrupt init routine publicly visible.

    ; dummy variable for pointer initialisation

    ZeroNetCLA .usect "ZeroNetCLA_Section",2,1,1 ; output terminal 1

    .text

    ;---------------------------------------------------------

    ; ISR Initialisation

    ;---------------------------------------------------------

    _CLA_CTRL_INIT:

    ; Clear the ZeroNet

    ZAPA

    MOVL XAR0, #ZeroNetCLA

    MOVL *XAR0, ACC

    ADCDRV_1ch_CLA_INIT 1

    ADCDRV_1ch_CLA_INIT 6

    CNTL_2P2Z_CLA_INIT 1

    PWMDRV_1chHiRes_CLA_INIT 6

    LRETR

    ; CLA variable declaration

    _Kff .usect "CNTL_2P2Z_CLA_Coef",2,1,1

    _ledCurrentSet .usect "CpuToCla1MsgRAM",2,1,1

    _ledCurrentSetVal .usect "CpuToCla1MsgRAM",2,1,1

    _ledCurrentRampVal .usect "CpuToCla1MsgRAM",2,1,1

    _vFFvMin .usect "CpuToCla1MsgRAM",2,1,1

    _lineCurrentGain .usect "CpuToCla1MsgRAM",2,1,1

    _lineCurrentOffset .usect "CpuToCla1MsgRAM",2,1,1

    _syncDisableFlag .usect "CpuToCla1MsgRAM",1,1

    rmsCalcTimer .usect "CpuToCla1MsgRAM",1,1

    _ledMinDuty .usect "CpuToCla1MsgRAM",1,1

    vFFdivResult .usect "Cla1ToCpuMsgRAM",2,1,1

    _ledCurrentRef .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFastRms .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFiltRms .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFastIQ .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFiltIQ .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentDimmingRms .usect "Cla1ToCpuMsgRAM",2,1,1

    rmsTemp .usect "Cla1ToCpuMsgRAM",2,1,1

    ledDimMult .usect "Cla1ToCpuMsgRAM",2,1,1

    currentOffset .usect "Cla1ToCpuMsgRAM",2,1,1

    ctrlCurrentShift .usect "Cla1ToCpuMsgRAM",2,1,1

    currentFiltTable .usect "Cla1ToCpuMsgRAM",32,1,1

    _currentCalFlag .usect "CpuToCla1MsgRAM",1,1

    rmsCount .usect "Cla1ToCpuMsgRAM",1,1

    ledDimSet .usect "Cla1ToCpuMsgRAM",1,1

    _ledCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    rmsCalcTimerPrev .usect "Cla1ToCpuMsgRAM",1,1

    ADC1_ledCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC2_pfcVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC3_inputCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC6_ledVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC9_hk12VoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    risingDelay .usect "Cla1ToCpuMsgRAM",1,1

    _ledVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    currentFiltIndex .usect "Cla1ToCpuMsgRAM",1,1

    inHighStep .usect "Cla1ToCpuMsgRAM",1,1

    .sect "Cla1Prog"

    .align 2

    ; Make CLA variables visible to C environment

    .def _Cla1Prog_Start

    .def _Cla1Task2

    .def _Cla1Task4

    .def _Cla1Task8

    .def _Kff

    .def _currentFastIQ

    .def _currentFiltIQ

    .def _lineCurrentGain

    .def _lineCurrentOffset

    .def _syncDisableFlag

    .def ledDimSet

    .def _ledCurrentCLA

    .def _vFFvMin

    .def _ledVoltageCLA

    .def rmsCalcTimer

    .def ctrlCurrentShift

    .def _ledCurrentSet

    .def _ledCurrentSetVal

    .def _ledCurrentRampVal

    .def _ledCurrentRef

    .def _ledMinDuty

    .def _currentCalFlag

    .def _currentDimmingRms

    _Cla1Prog_Start:

    _Cla1Task1:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T1End:

    _Cla1Task2: ; Task 2 is triggered by the 28x core caused by an LED overcurrent.

    MNOP

    MNOP

    MNOP

    MSETFLG RNDF32=1

    MMOVF32 MR0, #0.0

    MMOV16 @_EPwm6Regs.CMPA.half.CMPA , MR0 ; Set LED PWM duty to 0

    MMOV16 @_EPwm6Regs.CMPA.half.CMPAHR , MR0

    MMOV16 MAR0, @_PWMDRV_1chHiRes_CLA_Duty6 ; Set address to MAR0 (takes 3 pipeline cycles to complete).

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+0, MR0 ; Zero out LED controller buffer.

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+2, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+4, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+6, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+8, MR0

    MMOV32 *MAR0, MR0 ; Write 0 to duty macro.

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T2End:

    _Cla1Task3:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T3End:

     

    _Cla1Task4: ; Task 4 is the main CLA interrupt triggered along with the 28x core interrupt.

    MNOP

    MNOP

    MNOP

    MSETFLG RNDF32=1

    ; MMOVXI MR0, #0x0002

    ; MMOV16 @_EPwm7Regs.AQCSFRC, MR0

     

     

    CLA_START

    MMOVZ16 MR0, @_AdcResult.ADCRESULT1 ; The ADC is set to auto convert all inputs at

    MMOV16 @ADC1_ledCurrentCLA, MR0 ; before during the interrupt routines. All

    MMOVZ16 MR0, @_AdcResult.ADCRESULT2 ; values are saved here so they are not being

    MMOV16 @ADC2_pfcVoltageCLA, MR0 ; read and changed at the same time later in the

    MMOVZ16 MR0, @_AdcResult.ADCRESULT3 ; cycle.

    MMOV16 @ADC3_inputCurrentCLA, MR0

    MMOVZ16 MR0, @_AdcResult.ADCRESULT6 ; cycle.

    MMOV16 @ADC6_ledVoltageCLA, MR0

    MMOVZ16 MR0, @_AdcResult.ADCRESULT9

    MMOV16 @ADC9_hk12VoltageCLA, MR0

    MMOV32 MR0, @_lineCurrentOffset

    MMOV32 MR1, @rmsTemp

    MUI16TOF32 MR2, @ADC3_inputCurrentCLA

    MSUBF32 MR2, MR2, MR0 ; Subtract the offset from the current value.

    MMPYF32 MR2, MR2, MR2 ; Square the current value.

    MADDF32 MR1, MR1, MR2 ; Add the squared value to a sum for rms calculation.

    MMOV32 @rmsTemp, MR1

    MMOVZ16 MR0, @rmsCount ; Keep count of RMS values stored and run the calculation

    MMOVIZ MR1, #0 ; when the counter is satisfied.

    MMOVXI MR1, #1

    MADD32 MR0, MR1, MR0

    MMOV16 @rmsCount, MR0

    MMOVZ16 MR0, @rmsCalcTimer

    MMOVZ16 MR1, @rmsCalcTimerPrev

    MMOV16 @rmsCalcTimerPrev, MR0

    MCMP32 MR0, MR1

    MNOP

    MNOP

    MNOP

    MBCNDD LED_REF_RAMP, GEQ ; Skip the calculation until enough values have been accumulated.

    MNOP

    MNOP

    MNOP

    ; Below is a copy TIs CLAmath divide routine. It is pasted here for clarity rather than run a macro.

    ; CLAdiv .macro Dest, Num, Den

    RMS_CALC

    MMOVIZ MR0, #0

    MMOVXI MR0, #2

    MMOVIZ MR1, #0

    MMOVXI MR1, #30

    MMOVIZ MR3, #0.0

    MMOVZ16 MR2, @currentFiltIndex ; Get index for current filter

    MMOV16 MAR0, MR2, #currentFiltTable ; MAR0 gets next location

    MADD32 MR2, MR0, MR2 ; Increment current filter index

    MCMP32 MR2, MR1 ; Check for end of table

    MMOV32 MR2, MR3, GT ; Reset index to zero if at end

    MMOV16 @currentFiltIndex, MR2 ; Store next index value

    MMOVZ16 MR1, @rmsCount ; MR1 = Den

    MI16TOF32 MR1,MR1

    MEINVF32 MR2,MR1 ; MR2 = Ye = Estimate(1/Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    || MMOV32 MR0,@rmsTemp ; MR0 = Num

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR0,MR2,MR0 ; MR0 = Y = Ye*Num

    ; Below is a copy TIs CLAmath square root routine. It is pasted here for clarity rather than run a macro.

    ; CLAsqrt .macro y, x

    MEISQRTF32 MR1,MR0 ; MR1 = Ye = Estimate(1/sqrt(X))

    MMOV32 MR1,MR0,EQ ; if(X == 0.0) Ye = 0.0

    MMPYF32 MR3,MR0,#0.5 ; MR3 = X*0.5

    MMPYF32 MR2,MR1,MR3 ; MR2 = Ye*X*0.5

    MMPYF32 MR2,MR1,MR2 ; MR2 = Ye*Ye*X*0.5

    MSUBF32 MR2,#1.5,MR2 ; MR2 = 1.5 - Ye*Ye*X*0.5

    MMPYF32 MR1,MR1,MR2 ; MR1 = Ye = Ye*(1.5 - Ye*Ye*X*0.5)

    MMPYF32 MR2,MR1,MR3 ; MR2 = Ye*X*0.5

    MMPYF32 MR2,MR1,MR2 ; MR2 = Ye*Ye*X*0.5

    MSUBF32 MR2,#1.5,MR2 ; MR2 = 1.5 - Ye*Ye*X*0.5

    MMPYF32 MR1,MR1,MR2 ; MR1 = Ye = Ye*(1.5 - Ye*Ye*X*0.5)

    MMPYF32 MR0,MR1,MR0 ; MR0 = Y = Ye*X

    MMOV32 MR1, @_lineCurrentGain ; Multiply by circuit gains

    MMPYF32 MR0, MR0, MR1

    MMOV32 @_currentFastRms, MR0 ; Store the RMS current value.

    MMOV32 *MAR0, MR0

    MMOVI16 MAR0, #currentFiltTable

    MMOVIZ MR2, #0x4C80

    MMOVXI MR2, #0x0004

    MMPYF32 MR1, MR0, MR2 ; Convert the floating point value to IQ for use by the fixed point core.

    MF32TOI32 MR1, MR1 ; The current value is post-filtered by 28x.

    MMOV32 @_currentFastIQ, MR1

    MMOVIZ MR1, #0.0

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0

    MADDF32 MR1, MR2, MR1

    MMOVF32 MR3, #0.0625

    MMPYF32 MR1, MR1, MR3

    MMOV32 @_currentFiltRms, MR1

    MMOVIZ MR2, #0x4C80

    MMOVXI MR2, #0x0004

    MMPYF32 MR3, MR2, MR1 ; Convert the floating point value to IQ for use by the fixed point core.

    MF32TOI32 MR3, MR3 ; The current value is post-filtered by 28x.

    MMOV32 @_currentFiltIQ, MR3

    MMOVIZ MR3, #0.0 ; Reset the RMS accumulator and counter.

    MMOV32 @rmsTemp, MR3

    MMOV16 @rmsCount, MR3

    ; MMOVF32 MR2, #-0.1974

    ; MMOVF32 MR3, #1.5526

    ; MMPYF32 MR2, MR2, MR1

    ; MADDF32 MR2, MR2, MR3

    ; MMINF32 MR2, #1.0

    ; MMAXF32 MR2, #0.8

    MMOVF32 MR2, #1.0

    MMOVF32 MR3, #65536.0

    MMPYF32 MR1, MR2, MR3 ; Convert the floating point value to IQ for use by the fixed point core.

    MF32TOI32 MR1, MR1 ; The current value is post-filtered by 28x.

    MMOV32 @ctrlCurrentShift, MR1

    MMOVIZ MR1, #0

    MMOVXI MR1, #1

    MMOVZ16 MR2, @_currentCalFlag ; If the current is set to be calibrated (triggered by Main()),

    MCMP32 MR2, MR1 ; the following code adds an offset value from 6.6A

    MNOP

    MNOP

    MNOP

    MBCNDD SKIP_CAL, NEQ

    MNOP

    MNOP

    MNOP

    MMOV32 MR1, @_currentFiltRms

    MMOVF32 MR0, #6.6

    MEINVF32 MR2,MR1 ; MR2 = Ye = Estimate(1/Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR0,MR2,MR0 ; MR0 = Y = Ye*Num

    MMINF32 MR0, #1.05 ; If the offset is too large, it is set to max or min.

    MMAXF32 MR0, #0.95

    MMOV32 @currentOffset, MR0

    SKIP_CAL:

    MMOV32 MR0, @currentOffset ; Add the offset to the current value and store it.

    MMOV32 MR1, @_currentFiltRms

    MMPYF32 MR0, MR1, MR0

    MMOV32 @_currentDimmingRms, MR0 ; Store the RMS value with post filtering and offset. This

    ; value is used for dimming calculation.

    LED_REF_RAMP:

    MMOV32 MR2, @_ledCurrentRef

    MMOV32 MR0, @_ledCurrentSet

    MMOV32 MR1, @_ledCurrentRampVal

    CHECK_LED_REF:

    MCMPF32 MR0, MR2

    MNOP

    MNOP

    MNOP

    MBCNDD DIM_CALC, EQ

    MNOP

    MNOP

    MNOP

    ADD_LED_REF:

    MCMPF32 MR0, MR2

    MNOP

    MNOP

    MNOP

    MBCNDD SUB_LED_REF, LT

    MNOP

    MNOP

    MNOP

    MADDF32 MR3, MR2, MR1

    MCMPF32 MR3, MR0

    MMOV32 MR3, MR0, GT

    MMOV32 @_ledCurrentRef, MR3

    MNOP

    MNOP

    MNOP

    MBCNDD DIM_CALC, UNC

    MNOP

    MNOP

    MNOP

    SUB_LED_REF:

    MSUBF32 MR3, MR2, MR1

    MCMPF32 MR3, MR0

    MMOV32 MR3, MR0, LT

    MMOV32 @_ledCurrentRef, MR3

    DIM_CALC:

    MMOV32 MR0, @_currentFiltRms

    MMOV32 MR1, @_currentFastRms

    MMOVF32 MR2, #-0.1

    MSUBF32 MR3, MR1, MR0

    MCMPF32 MR3, MR2

    MMOV32 MR0, MR1, LT

    MMOV32 @_currentFiltRms, MR0

    MMOV32 MR0, @_currentDimmingRms

    ; Store maximum dimming current limit;

    MMOVF32 MR1, #DIM_HIGH_CURRENT_L

    MMOVF32 MR2, #DIM_HIGH_CURRENT_H

    MMOVZ16 MR3, @inHighStep

    MMOV32 MR2, MR1, NEQ

    MMOVF32 MR1, #0.0

    MMOVIZ MR3, #0

    MMOVXI MR3, #1

    MCMPF32 MR0, MR2

    MMOV32 MR3, MR1, LT

    MMOV16 @inHighStep, MR3

    MMOVF32 MR1, #DIM_LOW_CURRENT

    MCMPF32 MR0, MR1 ; If below lowest dimming current, set dimming to minimum.

    MMOVF32 MR1, #DIM_MIN

    MNOP

    MNOP

    MBCNDD LOAD_DIM_VALUE, LT

    MNOP

    MNOP

    MNOP

    MCMPF32 MR0, MR2 ; If above highest dimming current, set dimming to maximum.

    MMOVIZ MR1, #DIM_MAX

    MNOP

    MNOP

    MBCNDD LOAD_DIM_VALUE, GT

    MNOP

    MNOP

    MNOP

    ; If the current falls between min and max, a series of fit equations

    MMOVF32 MR1, #DIM_HIGH_CHANGE ; is used to calculate dimming duty cycle.

    MCMPF32 MR0, MR1 ; Determine if high equation is used.

    MNOP

    MNOP

    MNOP

    MBCNDD DIM_CURVE_HIGH, GEQ

    MNOP

    MNOP

    MNOP

    MMOVF32 MR1, #DIM_MID_CHANGE

    MCMPF32 MR0, MR1 ; Determine if mid equation to used.

    MNOP

    MNOP

    MNOP

    MBCNDD DIM_CURVE_MID, GEQ

    MNOP

    MNOP

    MNOP

    DIM_CURVE_LOW: ; If mid and high equations were skipped, must use low equation.

    MMOVF32 MR1, #LOW_DIM_A ; Calculate dimming: a x + b using low dim curve-fit coefficients

    MMOVF32 MR2, #LOW_DIM_B ; set in settings file.

    MMPYF32 MR1, MR1, MR0

    MADDF32 MR1, MR1, MR2

    MBCNDD LOAD_DIM_VALUE, UNC

    MNOP

    MNOP

    MNOP

    DIM_CURVE_MID:

    MMPYF32 MR1, MR0, MR0 ; When current is between high and low changeover current, this equation is used to

    MMOVF32 MR2, #MID_DIM_A ; calculate dimming: a x^2 + b x + c using mid dim curve-fit coefficients

    MMPYF32 MR1, MR2, MR1 ; set in settings file.

    MMOVF32 MR2, #MID_DIM_B

    MMPYF32 MR3, MR2, MR0

    MADDF32 MR1, MR1, MR3

    MMOVF32 MR2, #MID_DIM_C

    MADDF32 MR1, MR1, MR2

    MBCNDD LOAD_DIM_VALUE, UNC

    MNOP

    MNOP

    MNOP

    DIM_CURVE_HIGH:

    MMPYF32 MR1, MR0, MR0 ; When current is above high changeover current, the coefficients are changed to

    MMOVF32 MR2, #HIGH_DIM_A ; high dim curve fit equation set in settings file. Equation is a x^2 + b x + c.

    MMPYF32 MR1, MR2, MR1

    MMOVF32 MR2, #HIGH_DIM_B

    MMPYF32 MR3, MR2, MR0

    MADDF32 MR1, MR1, MR3

    MMOVF32 MR2, #HIGH_DIM_C

    MADDF32 MR1, MR1, MR2

    LOAD_DIM_VALUE:

    MMOV32 @ledDimMult, MR1 ; The output of the dimming equation is a percentage and it is multiplied

    ; by the dimming PWM period to calculate dimming duty.

    MMOVF32 MR2, #DIM_MAX_DUTY

    MMPYF32 MR3, MR1, MR2

    MF32TOI32 MR0, MR3

    MMOVZ16 MR1, @_ledMinDuty

    MCMP32 MR0, MR1

    MMOV32 MR0, MR1, LT

    MMOV16 @ledDimSet, MR0 ; Convert the dimming duty and store as an integer.

    MMOVZ16 MR1, @_EPwm6Regs.TZFLG.all ; Check dimmer TZ OST. OST for the dimmer is only active in

    MMOVIZ MR2, #0 ; software. If OST is triggered, do not execute LED controls

    MMOVXI MR2, #4

    MAND32 MR1, MR2, MR1

    MNOP

    MNOP

    MNOP

    MBCNDD LED_OFF, NEQ

    MNOP

    MNOP

    MNOP

    MMOVZ16 MR1, @_EPwm6Regs.TBCTL.all ; If syncing is disabled, skip over dead band. This happens at full

    MMOVIZ MR2, #0 ; dimming duty and on voltage control units.

    MMOVXI MR2, #4

    MAND32 MR1, MR2, MR1

    MNOP

    MNOP

    MNOP

    MBCNDD LED_RUN, EQ

    MNOP

    MNOP

    MNOP

    MMOVZ16 MR0, @_EPwm5Regs.TBCTR ; Check dimming PWM time base counter. LED controls are skipped if the dimmer

    MMOVZ16 MR1, @_EPwm5Regs.CMPA.half.CMPA ; is off, and for a delay time when the dimmer has just turned on. This prevents

    MCMP32 MR0, MR1 ; executing controls when the LED is off and during the rise time of the LED current.

    MNOP

    MNOP

    MNOP

    MBCNDD LED_OFF, GT

    MNOP

    MNOP

    MNOP

    MMOVZ16 MR2, @risingDelay

    MCMP32 MR0, MR2

    MNOP

    MNOP

    MNOP

    MBCNDD LED_RUN, GT

    MNOP

    MNOP

    MNOP

    LED_OFF:

    MEALLOW

    MMOVXI MR0,#0x0005

    MMOV16 @_EPwm6Regs.TZCLR.all, MR0 ; The DC events connected to the LED PWM will automatically release the PWM when the dimmer

    ; is on, but the event flag does not automatically clear and must be cleared manually.

    ; MMOVXI MR0, #0x0001

    ; MMOV16 @_EPwm7Regs.AQCSFRC, MR0 ; Force EPWM timing signal (for debug).

    MMOVXI MR0,#0x0001

    MMOV16 @_EPwm4Regs.ETCLR.all, MR0 ; Clear interrupt to make it available for next cycle.

    MEDIS

    MSTOP

    MNOP

    MNOP

    MNOP

    LED_RUN:

    MMOV16 MAR0, @_ADCDRV_1ch_CLA_Rlt6 ; Load MAR0 with Result pointer

    MMOVZ16 MR0, @ADC6_ledVoltageCLA

    MMOV16 @_ledVoltageCLA, MR0

    MUI16TOF32 MR0, MR0

    MMPYF32 MR0,MR0,#0.000244140625 ; Multiply by 1/(2^12) to convert it to per unit float.

    MNOP

    MMOV32 *MAR0, MR0 ; Save ADC voltage result.

    MMOV16 MAR0, @_ADCDRV_1ch_CLA_Rlt1 ; Load MAR0 with Result pointer

    MUI16TOF32 MR0, @ADC1_ledCurrentCLA

    MMPYF32 MR0,MR0,#0.000244140625 ; Multiply by 1/(2^12) to convert it to per unit float.

    MNOP

    MMOV32 *MAR0, MR0 ; Save ADC current result.

    MMOVZ16 MR0, @ADC1_ledCurrentCLA

    MMOV16 @_ledCurrentCLA, MR0 ; Save LED current for use in 28x core.

    CNTL_2P2Z_CLA 1 ; Run TI control macro (LED current controller)

    MMOV16 MAR0, @_Kff ; The following routine adds input feed-forward to the LED controls. The PFC voltage

    MMOV16 MAR1, @_CNTL_2P2Z_CLA_Out1 ; is divided by its minimum value and multiplied by a feed-forward gain.

    MUI16TOF32 MR1, @ADC2_pfcVoltageCLA ; Read PFC voltage ADC

    MMPYF32 MR1,MR1,#0.000244140625 ; Multiply by ADC value 1/(2^12) to convert to per unit float.

    ; TIs CLAdiv macro included here for clarity.

    MEINVF32 MR2,MR1 ; MR2 = Ye = Estimate(1/Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    || MMOV32 MR0,@_vFFvMin ; MR0 = Num

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR0,MR2,MR0 ; MR0 = Y = Ye*Num

    MMOVD32 MR1, *MAR0

    MMOVD32 MR2, *MAR1

    MMINF32 MR0, #1.0 ; Set limits for feed-forward beteen 0 and 1.

    MMAXF32 MR0, #0.0

    MMOV16 MAR0, @_PWMDRV_1chHiRes_CLA_Duty6

    MMPYF32 MR0, MR1, MR0

    MSUBF32 MR1, MR1, MR0 ; Multiply the feed-forward calculation by the controller duty. The input (PFC) voltage

    MSUBF32 MR1, #1.0, MR1 ; directly influences the LED controller depending on Kff.

    MMPYF32 MR1, MR1, MR2

    MMOV32 *MAR0, MR1

    PWMDRV_1chHiRes_CLA 6 ; TIs duty cycle macro. Take controller output between 0 and 1 and multiplies by PWM period

    ; to calculate a duty cycle.

    EXIT_CLA:

    ; MMOVXI MR0, #0x0001

    ; MMOV16 @_EPwm7Regs.AQCSFRC, MR0 ; Uncomment for timing debug purposes.

     

    MMOVXI MR0,#0x0001

    MMOV16 @_EPwm4Regs.ETCLR.all, MR0 ; Clear interrupt to make it available for next cycle.

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T4End: ; End of CLA interrupt (Task 4).

     

    _Cla1Task5:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T5End:

    _Cla1Task6:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T6End:

    _Cla1Task7:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T7End:

    _Cla1Task8: ; Task 8 is used to initialize variables only accessible within the CLA.

    CNTL_2P2Z_DBUFF_CLA_INIT 1

     

    MMOVI16 MAR0, #currentFiltTable

    MMOVIZ MR0, #0.0

    MMOV16 @rmsCount, MR0 ; Initialize variables.

    MMOV16 @_ledVoltageCLA, MR0

    MMOV16 @rmsCalcTimerPrev, MR0

    MMOV16 @currentFiltIndex, MR0

    MMOV32 @rmsTemp , MR0

    MMOV32 @_currentFastRms, MR0

    MMOV32 @_currentFiltRms, MR0

    MMOV32 @_currentFastIQ, MR0

    MMOV32 @_currentFiltIQ, MR0

    MMOV32 @_ledCurrentRef, MR0

    MMOV32 @_currentDimmingRms, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0, MR0

    MMOVF32 MR1, #1.0

    MMOV16 @ctrlCurrentShift, MR1

    MMOV32 @currentOffset, MR1

    MMOVXI MR1, #10

    MMOV16 @risingDelay, MR1

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T8End:

    _Cla1Prog_End:

    Assembly code.  File 2

    ;----------------------------------------------------------------------------------

    ; FILE: ULEDPS-CLA-ISR.C

    ;

    ; Description: CLA core interrupt routine, LED controls and protection, input current calculation.

    ;

    ; Version: 0.0

    ;

    ; Target: TMS320F28035 (Piccolo)

    ;

    ;----------------------------------------------------------------------------------

    ; Copyright Cooper Technologies Co. © 2013

    ;----------------------------------------------------------------------------------

    ; $Author: traver.gumaer $

    ; $Date: 2014-12-09 12:33:53 -0500 (Tue, 09 Dec 2014) $

    ; $Id: ULEDPS-CLA-ISR.asm 338 2014-12-09 17:33:53Z traver.gumaer $

    ; $Rev: 338 $

    ; $URL: 10.91.161.17/.../ULEDPS-CLA-ISR.asm $

    ;

    ;Gives peripheral addresses visibility in assembly

    .cdecls C,LIST,"PeripheralHeaderIncludes.h"

    .cdecls C,LIST,"ULEDPS-Settings.h"

    ;Include files for the Power Library Macro's being used by the system

    .include "ADCDRV_1ch_CLA.asm"

    .include "PWMDRV_1chHiRes_CLA.asm"

    .include "CNTL_2P2Z_CLA.asm"

    ; .include "CLAShared.h"

    .def _CLA_CTRL_INIT ; Make CLA interrupt init routine publicly visible.

    ; dummy variable for pointer initialisation

    ZeroNetCLA .usect "ZeroNetCLA_Section",2,1,1 ; output terminal 1

    .text

    ;---------------------------------------------------------

    ; ISR Initialisation

    ;---------------------------------------------------------

    _CLA_CTRL_INIT:

    ; Clear the ZeroNet

    ZAPA

    MOVL XAR0, #ZeroNetCLA

    MOVL *XAR0, ACC

    ADCDRV_1ch_CLA_INIT 1

    ADCDRV_1ch_CLA_INIT 6

    CNTL_2P2Z_CLA_INIT 1

    PWMDRV_1chHiRes_CLA_INIT 6

    LRETR

    ; CLA variable declaration

    _Kff .usect "CNTL_2P2Z_CLA_Coef",2,1,1

    _ledCurrentSet .usect "CpuToCla1MsgRAM",2,1,1

    _ledCurrentSetVal .usect "CpuToCla1MsgRAM",2,1,1

    _ledCurrentRampVal .usect "CpuToCla1MsgRAM",2,1,1

    _vFFvMin .usect "CpuToCla1MsgRAM",2,1,1

    _lineCurrentGain .usect "CpuToCla1MsgRAM",2,1,1

    _lineCurrentOffset .usect "CpuToCla1MsgRAM",2,1,1

    _syncDisableFlag .usect "CpuToCla1MsgRAM",1,1

    rmsCalcTimer .usect "CpuToCla1MsgRAM",1,1

    _ledMinDuty .usect "CpuToCla1MsgRAM",1,1

    vFFdivResult .usect "Cla1ToCpuMsgRAM",2,1,1

    _ledCurrentRef .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFastRms .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFiltRms .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFastIQ .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFiltIQ .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentDimmingRms .usect "Cla1ToCpuMsgRAM",2,1,1

    rmsTemp .usect "Cla1ToCpuMsgRAM",2,1,1

    ledDimMult .usect "Cla1ToCpuMsgRAM",2,1,1

    currentOffset .usect "Cla1ToCpuMsgRAM",2,1,1

    ctrlCurrentShift .usect "Cla1ToCpuMsgRAM",2,1,1

    currentFiltTable .usect "Cla1ToCpuMsgRAM",32,1,1

    _currentCalFlag .usect "CpuToCla1MsgRAM",1,1

    rmsCount .usect "Cla1ToCpuMsgRAM",1,1

    ledDimSet .usect "Cla1ToCpuMsgRAM",1,1

    _ledCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    rmsCalcTimerPrev .usect "Cla1ToCpuMsgRAM",1,1

    ADC1_ledCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC2_pfcVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC3_inputCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC6_ledVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC9_hk12VoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    risingDelay .usect "Cla1ToCpuMsgRAM",1,1

    _ledVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    currentFiltIndex .usect "Cla1ToCpuMsgRAM",1,1

    inHighStep .usect "Cla1ToCpuMsgRAM",1,1

    .sect "Cla1Prog"

    .align 2

    ; Make CLA variables visible to C environment

    .def _Cla1Prog_Start

    .def _Cla1Task2

    .def _Cla1Task4

    .def _Cla1Task8

    .def _Kff

    .def _currentFastIQ

    .def _currentFiltIQ

    .def _lineCurrentGain

    .def _lineCurrentOffset

    .def _syncDisableFlag

    .def ledDimSet

    .def _ledCurrentCLA

    .def _vFFvMin

    .def _ledVoltageCLA

    .def rmsCalcTimer

    .def ctrlCurrentShift

    .def _ledCurrentSet

    .def _ledCurrentSetVal

    .def _ledCurrentRampVal

    .def _ledCurrentRef

    .def _ledMinDuty

    .def _currentCalFlag

    .def _currentDimmingRms

    _Cla1Prog_Start:

    _Cla1Task1:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T1End:

    _Cla1Task2: ; Task 2 is triggered by the 28x core caused by an LED overcurrent.

    MNOP

    MNOP

    MNOP

    MSETFLG RNDF32=1

    MMOVF32 MR0, #0.0

    MMOV16 @_EPwm6Regs.CMPA.half.CMPA , MR0 ; Set LED PWM duty to 0

    MMOV16 @_EPwm6Regs.CMPA.half.CMPAHR , MR0

    MMOV16 MAR0, @_PWMDRV_1chHiRes_CLA_Duty6 ; Set address to MAR0 (takes 3 pipeline cycles to complete).

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+0, MR0 ; Zero out LED controller buffer.

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+2, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+4, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+6, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+8, MR0

    MMOV32 *MAR0, MR0 ; Write 0 to duty macro.

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T2End:

    _Cla1Task3:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T3End:

     

    _Cla1Task4: ; Task 4 is the main CLA interrupt triggered along with the 28x core interrupt.

    MNOP

    MNOP

    MNOP

    MSETFLG RNDF32=1

    ; MMOVXI MR0, #0x0002

    ; MMOV16 @_EPwm7Regs.AQCSFRC, MR0

     

     

    CLA_START

    MMOVZ16 MR0, @_AdcResult.ADCRESULT1 ; The ADC is set to auto convert all inputs at

    MMOV16 @ADC1_ledCurrentCLA, MR0 ; before during the interrupt routines. All

    MMOVZ16 MR0, @_AdcResult.ADCRESULT2 ; values are saved here so they are not being

    MMOV16 @ADC2_pfcVoltageCLA, MR0 ; read and changed at the same time later in the

    MMOVZ16 MR0, @_AdcResult.ADCRESULT3 ; cycle.

    MMOV16 @ADC3_inputCurrentCLA, MR0

    MMOVZ16 MR0, @_AdcResult.ADCRESULT6 ; cycle.

    MMOV16 @ADC6_ledVoltageCLA, MR0

    MMOVZ16 MR0, @_AdcResult.ADCRESULT9

    MMOV16 @ADC9_hk12VoltageCLA, MR0

    MMOV32 MR0, @_lineCurrentOffset

    MMOV32 MR1, @rmsTemp

    MUI16TOF32 MR2, @ADC3_inputCurrentCLA

    MSUBF32 MR2, MR2, MR0 ; Subtract the offset from the current value.

    MMPYF32 MR2, MR2, MR2 ; Square the current value.

    MADDF32 MR1, MR1, MR2 ; Add the squared value to a sum for rms calculation.

    MMOV32 @rmsTemp, MR1

    MMOVZ16 MR0, @rmsCount ; Keep count of RMS values stored and run the calculation

    MMOVIZ MR1, #0 ; when the counter is satisfied.

    MMOVXI MR1, #1

    MADD32 MR0, MR1, MR0

    MMOV16 @rmsCount, MR0

    MMOVZ16 MR0, @rmsCalcTimer

    MMOVZ16 MR1, @rmsCalcTimerPrev

    MMOV16 @rmsCalcTimerPrev, MR0

    MCMP32 MR0, MR1

    MNOP

    MNOP

    MNOP

    MBCNDD LED_REF_RAMP, GEQ ; Skip the calculation until enough values have been accumulated.

    MNOP

    MNOP

    MNOP

    ; Below is a copy TIs CLAmath divide routine. It is pasted here for clarity rather than run a macro.

    ; CLAdiv .macro Dest, Num, Den

    RMS_CALC

    MMOVIZ MR0, #0

    MMOVXI MR0, #2

    MMOVIZ MR1, #0

    MMOVXI MR1, #30

    MMOVIZ MR3, #0.0

    MMOVZ16 MR2, @currentFiltIndex ; Get index for current filter

    MMOV16 MAR0, MR2, #currentFiltTable ; MAR0 gets next location

    MADD32 MR2, MR0, MR2 ; Increment current filter index

    MCMP32 MR2, MR1 ; Check for end of table

    MMOV32 MR2, MR3, GT ; Reset index to zero if at end

    MMOV16 @currentFiltIndex, MR2 ; Store next index value

    MMOVZ16 MR1, @rmsCount ; MR1 = Den

    MI16TOF32 MR1,MR1

    MEINVF32 MR2,MR1 ; MR2 = Ye = Estimate(1/Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    || MMOV32 MR0,@rmsTemp ; MR0 = Num

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR0,MR2,MR0 ; MR0 = Y = Ye*Num

    ; Below is a copy TIs CLAmath square root routine. It is pasted here for clarity rather than run a macro.

    ; CLAsqrt .macro y, x

    MEISQRTF32 MR1,MR0 ; MR1 = Ye = Estimate(1/sqrt(X))

    MMOV32 MR1,MR0,EQ ; if(X == 0.0) Ye = 0.0

    MMPYF32 MR3,MR0,#0.5 ; MR3 = X*0.5

    MMPYF32 MR2,MR1,MR3 ; MR2 = Ye*X*0.5

    MMPYF32 MR2,MR1,MR2 ; MR2 = Ye*Ye*X*0.5

    MSUBF32 MR2,#1.5,MR2 ; MR2 = 1.5 - Ye*Ye*X*0.5

    MMPYF32 MR1,MR1,MR2 ; MR1 = Ye = Ye*(1.5 - Ye*Ye*X*0.5)

    MMPYF32 MR2,MR1,MR3 ; MR2 = Ye*X*0.5

    MMPYF32 MR2,MR1,MR2 ; MR2 = Ye*Ye*X*0.5

    MSUBF32 MR2,#1.5,MR2 ; MR2 = 1.5 - Ye*Ye*X*0.5

    MMPYF32 MR1,MR1,MR2 ; MR1 = Ye = Ye*(1.5 - Ye*Ye*X*0.5)

    MMPYF32 MR0,MR1,MR0 ; MR0 = Y = Ye*X

    MMOV32 MR1, @_lineCurrentGain ; Multiply by circuit gains

    MMPYF32 MR0, MR0, MR1

    MMOV32 @_currentFastRms, MR0 ; Store the RMS current value.

    MMOV32 *MAR0, MR0

    MMOVI16 MAR0, #currentFiltTable

    MMOVIZ MR2, #0x4C80

    MMOVXI MR2, #0x0004

    MMPYF32 MR1, MR0, MR2 ; Convert the floating point value to IQ for use by the fixed point core.

    MF32TOI32 MR1, MR1 ; The current value is post-filtered by 28x.

    MMOV32 @_currentFastIQ, MR1

    MMOVIZ MR1, #0.0

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0[2]++

    MADDF32 MR1, MR2, MR1

    MMOV32 MR2, *MAR0

    MADDF32 MR1, MR2, MR1

    MMOVF32 MR3, #0.0625

    MMPYF32 MR1, MR1, MR3

    MMOV32 @_currentFiltRms, MR1

    MMOVIZ MR2, #0x4C80

    MMOVXI MR2, #0x0004

    MMPYF32 MR3, MR2, MR1 ; Convert the floating point value to IQ for use by the fixed point core.

    MF32TOI32 MR3, MR3 ; The current value is post-filtered by 28x.

    MMOV32 @_currentFiltIQ, MR3

    MMOVIZ MR3, #0.0 ; Reset the RMS accumulator and counter.

    MMOV32 @rmsTemp, MR3

    MMOV16 @rmsCount, MR3

    ; MMOVF32 MR2, #-0.1974

    ; MMOVF32 MR3, #1.5526

    ; MMPYF32 MR2, MR2, MR1

    ; MADDF32 MR2, MR2, MR3

    ; MMINF32 MR2, #1.0

    ; MMAXF32 MR2, #0.8

    MMOVF32 MR2, #1.0

    MMOVF32 MR3, #65536.0

    MMPYF32 MR1, MR2, MR3 ; Convert the floating point value to IQ for use by the fixed point core.

    MF32TOI32 MR1, MR1 ; The current value is post-filtered by 28x.

    MMOV32 @ctrlCurrentShift, MR1

    MMOVIZ MR1, #0

    MMOVXI MR1, #1

    MMOVZ16 MR2, @_currentCalFlag ; If the current is set to be calibrated (triggered by Main()),

    MCMP32 MR2, MR1 ; the following code adds an offset value from 6.6A

    MNOP

    MNOP

    MNOP

    MBCNDD SKIP_CAL, NEQ

    MNOP

    MNOP

    MNOP

    MMOV32 MR1, @_currentFiltRms

    MMOVF32 MR0, #6.6

    MEINVF32 MR2,MR1 ; MR2 = Ye = Estimate(1/Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR0,MR2,MR0 ; MR0 = Y = Ye*Num

    MMINF32 MR0, #1.05 ; If the offset is too large, it is set to max or min.

    MMAXF32 MR0, #0.95

    MMOV32 @currentOffset, MR0

    SKIP_CAL:

    MMOV32 MR0, @currentOffset ; Add the offset to the current value and store it.

    MMOV32 MR1, @_currentFiltRms

    MMPYF32 MR0, MR1, MR0

    MMOV32 @_currentDimmingRms, MR0 ; Store the RMS value with post filtering and offset. This

    ; value is used for dimming calculation.

    LED_REF_RAMP:

    MMOV32 MR2, @_ledCurrentRef

    MMOV32 MR0, @_ledCurrentSet

    MMOV32 MR1, @_ledCurrentRampVal

    CHECK_LED_REF:

    MCMPF32 MR0, MR2

    MNOP

    MNOP

    MNOP

    MBCNDD DIM_CALC, EQ

    MNOP

    MNOP

    MNOP

    ADD_LED_REF:

    MCMPF32 MR0, MR2

    MNOP

    MNOP

    MNOP

    MBCNDD SUB_LED_REF, LT

    MNOP

    MNOP

    MNOP

    MADDF32 MR3, MR2, MR1

    MCMPF32 MR3, MR0

    MMOV32 MR3, MR0, GT

    MMOV32 @_ledCurrentRef, MR3

    MNOP

    MNOP

    MNOP

    MBCNDD DIM_CALC, UNC

    MNOP

    MNOP

    MNOP

    SUB_LED_REF:

    MSUBF32 MR3, MR2, MR1

    MCMPF32 MR3, MR0

    MMOV32 MR3, MR0, LT

    MMOV32 @_ledCurrentRef, MR3

    DIM_CALC:

    MMOV32 MR0, @_currentFiltRms

    MMOV32 MR1, @_currentFastRms

    MMOVF32 MR2, #-0.1

    MSUBF32 MR3, MR1, MR0

    MCMPF32 MR3, MR2

    MMOV32 MR0, MR1, LT

    MMOV32 @_currentFiltRms, MR0

    MMOV32 MR0, @_currentDimmingRms

    ; Store maximum dimming current limit;

    MMOVF32 MR1, #DIM_HIGH_CURRENT_L

    MMOVF32 MR2, #DIM_HIGH_CURRENT_H

    MMOVZ16 MR3, @inHighStep

    MMOV32 MR2, MR1, NEQ

    MMOVF32 MR1, #0.0

    MMOVIZ MR3, #0

    MMOVXI MR3, #1

    MCMPF32 MR0, MR2

    MMOV32 MR3, MR1, LT

    MMOV16 @inHighStep, MR3

    MMOVF32 MR1, #DIM_LOW_CURRENT

    MCMPF32 MR0, MR1 ; If below lowest dimming current, set dimming to minimum.

    MMOVF32 MR1, #DIM_MIN

    MNOP

    MNOP

    MBCNDD LOAD_DIM_VALUE, LT

    MNOP

    MNOP

    MNOP

    MCMPF32 MR0, MR2 ; If above highest dimming current, set dimming to maximum.

    MMOVIZ MR1, #DIM_MAX

    MNOP

    MNOP

    MBCNDD LOAD_DIM_VALUE, GT

    MNOP

    MNOP

    MNOP

    ; If the current falls between min and max, a series of fit equations

    MMOVF32 MR1, #DIM_HIGH_CHANGE ; is used to calculate dimming duty cycle.

    MCMPF32 MR0, MR1 ; Determine if high equation is used.

    MNOP

    MNOP

    MNOP

    MBCNDD DIM_CURVE_HIGH, GEQ

    MNOP

    MNOP

    MNOP

    MMOVF32 MR1, #DIM_MID_CHANGE

    MCMPF32 MR0, MR1 ; Determine if mid equation to used.

    MNOP

    MNOP

    MNOP

    MBCNDD DIM_CURVE_MID, GEQ

    MNOP

    MNOP

    MNOP

    DIM_CURVE_LOW: ; If mid and high equations were skipped, must use low equation.

    MMOVF32 MR1, #LOW_DIM_A ; Calculate dimming: a x + b using low dim curve-fit coefficients

    MMOVF32 MR2, #LOW_DIM_B ; set in settings file.

    MMPYF32 MR1, MR1, MR0

    MADDF32 MR1, MR1, MR2

    MBCNDD LOAD_DIM_VALUE, UNC

    MNOP

    MNOP

    MNOP

    DIM_CURVE_MID:

    MMPYF32 MR1, MR0, MR0 ; When current is between high and low changeover current, this equation is used to

    MMOVF32 MR2, #MID_DIM_A ; calculate dimming: a x^2 + b x + c using mid dim curve-fit coefficients

    MMPYF32 MR1, MR2, MR1 ; set in settings file.

    MMOVF32 MR2, #MID_DIM_B

    MMPYF32 MR3, MR2, MR0

    MADDF32 MR1, MR1, MR3

    MMOVF32 MR2, #MID_DIM_C

    MADDF32 MR1, MR1, MR2

    MBCNDD LOAD_DIM_VALUE, UNC

    MNOP

    MNOP

    MNOP

    DIM_CURVE_HIGH:

    MMPYF32 MR1, MR0, MR0 ; When current is above high changeover current, the coefficients are changed to

    MMOVF32 MR2, #HIGH_DIM_A ; high dim curve fit equation set in settings file. Equation is a x^2 + b x + c.

    MMPYF32 MR1, MR2, MR1

    MMOVF32 MR2, #HIGH_DIM_B

    MMPYF32 MR3, MR2, MR0

    MADDF32 MR1, MR1, MR3

    MMOVF32 MR2, #HIGH_DIM_C

    MADDF32 MR1, MR1, MR2

    LOAD_DIM_VALUE:

    MMOV32 @ledDimMult, MR1 ; The output of the dimming equation is a percentage and it is multiplied

    ; by the dimming PWM period to calculate dimming duty.

    MMOVF32 MR2, #DIM_MAX_DUTY

    MMPYF32 MR3, MR1, MR2

    MF32TOI32 MR0, MR3

    MMOVZ16 MR1, @_ledMinDuty

    MCMP32 MR0, MR1

    MMOV32 MR0, MR1, LT

    MMOV16 @ledDimSet, MR0 ; Convert the dimming duty and store as an integer.

    MMOVZ16 MR1, @_EPwm6Regs.TZFLG.all ; Check dimmer TZ OST. OST for the dimmer is only active in

    MMOVIZ MR2, #0 ; software. If OST is triggered, do not execute LED controls

    MMOVXI MR2, #4

    MAND32 MR1, MR2, MR1

    MNOP

    MNOP

    MNOP

    MBCNDD LED_OFF, NEQ

    MNOP

    MNOP

    MNOP

    MMOVZ16 MR1, @_EPwm6Regs.TBCTL.all ; If syncing is disabled, skip over dead band. This happens at full

    MMOVIZ MR2, #0 ; dimming duty and on voltage control units.

    MMOVXI MR2, #4

    MAND32 MR1, MR2, MR1

    MNOP

    MNOP

    MNOP

    MBCNDD LED_RUN, EQ

    MNOP

    MNOP

    MNOP

    MMOVZ16 MR0, @_EPwm5Regs.TBCTR ; Check dimming PWM time base counter. LED controls are skipped if the dimmer

    MMOVZ16 MR1, @_EPwm5Regs.CMPA.half.CMPA ; is off, and for a delay time when the dimmer has just turned on. This prevents

    MCMP32 MR0, MR1 ; executing controls when the LED is off and during the rise time of the LED current.

    MNOP

    MNOP

    MNOP

    MBCNDD LED_OFF, GT

    MNOP

    MNOP

    MNOP

    MMOVZ16 MR2, @risingDelay

    MCMP32 MR0, MR2

    MNOP

    MNOP

    MNOP

    MBCNDD LED_RUN, GT

    MNOP

    MNOP

    MNOP

    LED_OFF:

    MEALLOW

    MMOVXI MR0,#0x0005

    MMOV16 @_EPwm6Regs.TZCLR.all, MR0 ; The DC events connected to the LED PWM will automatically release the PWM when the dimmer

    ; is on, but the event flag does not automatically clear and must be cleared manually.

    ; MMOVXI MR0, #0x0001

    ; MMOV16 @_EPwm7Regs.AQCSFRC, MR0 ; Force EPWM timing signal (for debug).

    MMOVXI MR0,#0x0001

    MMOV16 @_EPwm4Regs.ETCLR.all, MR0 ; Clear interrupt to make it available for next cycle.

    MEDIS

    MSTOP

    MNOP

    MNOP

    MNOP

    LED_RUN:

    MMOV16 MAR0, @_ADCDRV_1ch_CLA_Rlt6 ; Load MAR0 with Result pointer

    MMOVZ16 MR0, @ADC6_ledVoltageCLA

    MMOV16 @_ledVoltageCLA, MR0

    MUI16TOF32 MR0, MR0

    MMPYF32 MR0,MR0,#0.000244140625 ; Multiply by 1/(2^12) to convert it to per unit float.

    MNOP

    MMOV32 *MAR0, MR0 ; Save ADC voltage result.

    MMOV16 MAR0, @_ADCDRV_1ch_CLA_Rlt1 ; Load MAR0 with Result pointer

    MUI16TOF32 MR0, @ADC1_ledCurrentCLA

    MMPYF32 MR0,MR0,#0.000244140625 ; Multiply by 1/(2^12) to convert it to per unit float.

    MNOP

    MMOV32 *MAR0, MR0 ; Save ADC current result.

    MMOVZ16 MR0, @ADC1_ledCurrentCLA

    MMOV16 @_ledCurrentCLA, MR0 ; Save LED current for use in 28x core.

    CNTL_2P2Z_CLA 1 ; Run TI control macro (LED current controller)

    MMOV16 MAR0, @_Kff ; The following routine adds input feed-forward to the LED controls. The PFC voltage

    MMOV16 MAR1, @_CNTL_2P2Z_CLA_Out1 ; is divided by its minimum value and multiplied by a feed-forward gain.

    MUI16TOF32 MR1, @ADC2_pfcVoltageCLA ; Read PFC voltage ADC

    MMPYF32 MR1,MR1,#0.000244140625 ; Multiply by ADC value 1/(2^12) to convert to per unit float.

    ; TIs CLAdiv macro included here for clarity.

    MEINVF32 MR2,MR1 ; MR2 = Ye = Estimate(1/Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR3,MR2,MR1 ; MR3 = Ye*Den

    || MMOV32 MR0,@_vFFvMin ; MR0 = Num

    MSUBF32 MR3,#2.0,MR3 ; MR3 = 2.0 - Ye*Den

    MMPYF32 MR2,MR2,MR3 ; MR2 = Ye = Ye*(2.0 - Ye*Den)

    MMPYF32 MR0,MR2,MR0 ; MR0 = Y = Ye*Num

    MMOVD32 MR1, *MAR0

    MMOVD32 MR2, *MAR1

    MMINF32 MR0, #1.0 ; Set limits for feed-forward beteen 0 and 1.

    MMAXF32 MR0, #0.0

    MMOV16 MAR0, @_PWMDRV_1chHiRes_CLA_Duty6

    MMPYF32 MR0, MR1, MR0

    MSUBF32 MR1, MR1, MR0 ; Multiply the feed-forward calculation by the controller duty. The input (PFC) voltage

    MSUBF32 MR1, #1.0, MR1 ; directly influences the LED controller depending on Kff.

    MMPYF32 MR1, MR1, MR2

    MMOV32 *MAR0, MR1

    PWMDRV_1chHiRes_CLA 6 ; TIs duty cycle macro. Take controller output between 0 and 1 and multiplies by PWM period

    ; to calculate a duty cycle.

    EXIT_CLA:

    ; MMOVXI MR0, #0x0001

    ; MMOV16 @_EPwm7Regs.AQCSFRC, MR0 ; Uncomment for timing debug purposes.

     

    MMOVXI MR0,#0x0001

    MMOV16 @_EPwm4Regs.ETCLR.all, MR0 ; Clear interrupt to make it available for next cycle.

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T4End: ; End of CLA interrupt (Task 4).

     

    _Cla1Task5:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T5End:

    _Cla1Task6:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T6End:

    _Cla1Task7:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T7End:

    _Cla1Task8: ; Task 8 is used to initialize variables only accessible within the CLA.

    CNTL_2P2Z_DBUFF_CLA_INIT 1

     

    MMOVI16 MAR0, #currentFiltTable

    MMOVIZ MR0, #0.0

    MMOV16 @rmsCount, MR0 ; Initialize variables.

    MMOV16 @_ledVoltageCLA, MR0

    MMOV16 @rmsCalcTimerPrev, MR0

    MMOV16 @currentFiltIndex, MR0

    MMOV32 @rmsTemp , MR0

    MMOV32 @_currentFastRms, MR0

    MMOV32 @_currentFiltRms, MR0

    MMOV32 @_currentFastIQ, MR0

    MMOV32 @_currentFiltIQ, MR0

    MMOV32 @_ledCurrentRef, MR0

    MMOV32 @_currentDimmingRms, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0[2]++, MR0

    MMOV32 *MAR0, MR0

    MMOVF32 MR1, #1.0

    MMOV16 @ctrlCurrentShift, MR1

    MMOV32 @currentOffset, MR1

    MMOVXI MR1, #10

    MMOV16 @risingDelay, MR1

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T8End:

    _Cla1Prog_End:

     

     

    Regads,

    Indika

     

     

  • Hi Indika,

    Thank you for sharing your code. For future awareness there is an option as well when you reply under the "Insert" option to include code. This will format the code and include a scrollbar. 

    Unfortunately the code you have attached is very large and requires many more modules. Have you tried to step through the portion of the code that sends the f1 and f2 values? Set a breakpoint and then step through the code within CCS. Check in the expressions window of CCS to see if the values are the correct values before you transmit them through the SCI module. I would also monitor serialTxBytes. 

    Best Regards,

    Marlyn

  • //----------------------------------------------------------------------------------
    //
    //	Product:		Universal LED Power Supply
    //
    //	File:			ULEDPS-Main.c
    //
    //	Description:	Main function, state machine, and background functions
    //
    //	Version: 		L
    //
    //  Target:  		TMS320F28035 (Piccolo)
    //
    //----------------------------------------------------------------------------------
    //  Copyright Eaton Corporation © 2014
    //----------------------------------------------------------------------------------
    //  $Author: tom.reed $
    //	$Date: 2014-12-15 17:31:09 -0500 (Mon, 15 Dec 2014) $
    //	$Id: ULEDPS-Main.c 340 2014-12-15 22:31:09Z tom.reed $
    //	$Rev: 340 $
    //	$URL: http://10.91.161.17/svn-private/Firmware/ULEDPS-UNIVERSAL-CODE-E/source/ULEDPS-Main.c $
    //
    const int svnRev = 1207;				// SVN VERSION AND DATE VARIABLES MUST BE MANUALLY UPDATED WHEN A NEW VERSION IS RELEASED.
    const long svnRevStamp = 20200807;
    const char firmwareRev = 'P';			// Letter version of production release.
    
    // Header Includes
    #include <string.h>
    #include <stdio.h>
    #include <stdint.h>
    #include "ULEDPS-Settings.h"			// User settings and power supply parameters.
    #include "DSP2803x_Device.h"			// Device register structures from TI.
    #include "Flash2803x_API_Library.h"		// On-Chip flash functionality.
    #include "DSP2803x_EPWM_defines.h"		// Defines for PWM settings.
    #include "IQmathLib.h"					// TIs built-in IQ math functions.
    #include <math.h>						// Standard math functions.
    #include "SFO_V6.h"						// High resolution PWM controller calibration algorithm.
    #include "CLAShared.h"					// Functions for CLA subprocessor.
    #include "DSP2803x_Cla_defines.h"		// Defines for CLA subprocessor.
    #include "DPlib.h"						// TIs digital power library functions.
    
    // Function Prototypes (in this file)
    int		tempConvert(int adcval);
    void 	init_cla(void);
    void	runtimeErrorChecking(void);
    void	ledCurrentErrorChecking(void);
    long	sciMessageRx(void);
    void	sciMessageTx(int data[], int length);
    long	parseBytesToData(int data[], int offset, int length);
    void	parseDataToBytes(long value, int data[], int offset, int length);
    void	processEEPROMread(int byteAddress, int data[]);
    void	processEEPROMwrite(int byteAddress, int data[]);
    void	initEEPROMpage(int byteAddress);
    void	sendSciData(int dataID, long dataValue, int length);
    void 	sendSciAdData(int dataID, char data[], int length);
    long 	getHeaterTemperature(long adcValue);
    void	 configFunction(void);
    void 	diagFunction(void);
    int     ledCurrentRamp(volatile float *refPoint, float setPoint);
    
    // Control interrupt functions located in ULEDPS-28x-ISR.asm
    extern void 			CTRL_INIT(void);
    extern interrupt void 	CTRL_ISR(void);
    
    // CLA functions located in ULEDPS-CLA-ISR.asm
    extern void CLA_CTRL_INIT(void);
    
    // Microsecond delay located in DSP2803x_usDelay.asm
    extern void DSP2803x_usDelay(Uint32 Count);
    
    extern void ADCinit(void);
    extern void DeviceInit(void);
    
    // EPWM functions located in ULEDPS-ePWM_Config.c
    extern void ledBuckConfig(int16 pwmNumber, int16 period);
    extern void ledBoostConfig(int16 pwmNumber, int16 period);
    extern void ledDimmerConfig(int16 x, Uint16 period);
    extern void ledDelayDimmerConfig(int16 x, Uint16 period);
    extern void inputPfcConfig(int16 pwmNumber, int16 period, int16 compNumber);
    extern void interrupt28xConfig(int16 pwmNumber, int16 period);
    extern void interruptCLAConfig(int16 pwmNumber, int16 period);
    extern void heaterDriveConfig(int16 pwmNumber, int16 period);
    extern void syncDriveConfig(int16 pwmNumber, int16 period);
    extern int i2cNackPoll(int slaveAddress);
    
    // EEPROM functions located in ULEDPS-EEEPROM.c
    extern int	receiveI2cData(int slaveAddress, int byteAddress, int data[]);
    extern int	transmitI2cData(int slaveAddress, int byteAddress, int data[]);
    extern void I2Cinit(void);
    extern void	i2cRestart(void);
    
    // Misc MCU Functions.
    void InitFlash();
    void MemCopy();
    
    // Serial function in ULEDPS-Sci.c
    extern void scia_init(void);
    
    // Addresses for relocating timing critical functions in RAM
    extern Uint16 *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;
    
    // Addresses for relocating CLA program code to CLA accessible RAM.
    extern Uint16 Cla1funcsLoadStart, Cla1funcsLoadEnd, Cla1funcsRunStart;
    
    // Externally defined variables in 28x assembly routine.
    extern long	*CNTL_2P2Z_Ref1;
    extern long *CNTL_2P2Z_Fdbk1;
    extern long *CNTL_2P2Z_Out1;
    extern long	*CNTL_2P2Z_Coef1;
    extern long	*PWMDRV_1chHiRes_Duty1;
    
    extern long pfcOVH;
    extern long pfcOVL;
    extern long pfcUVH;
    extern long pfcUVL;
    extern int	pfcOCH;
    extern int	pfcOCL;
    
    extern unsigned int inputCurrentOffset;
    extern int syncCurrentH;
    extern int syncCurrentL;
    
    extern long pfcVoltageAdc;
    extern long pfcVoltage;
    extern long pfcSenseGain;
    
    extern unsigned int pfcOVErr;
    extern unsigned int pfcOCErr;
    
    extern unsigned int dimDisable;
    extern unsigned int dimOverride;
    extern unsigned int dimRampVal;
    
    extern unsigned int blinkDisable;
    extern unsigned int	blinkState;
    extern unsigned int blinkTimer;
    
    extern unsigned int ledFastOVDisable;
    extern unsigned int	ledFastOV;
    extern unsigned int	linePeriod;
    
    // Externally defined variables in CLA assembly routine.
    extern volatile float	*ADCDRV_1ch_CLA_Rlt1;
    extern volatile float	*ADCDRV_1ch_CLA_Rlt6;
    extern volatile float	*CNTL_2P2Z_CLA_Ref1;
    extern volatile float	*CNTL_2P2Z_CLA_Fdbk1;
    extern volatile float	*CNTL_2P2Z_CLA_Out1;
    extern volatile float	*CNTL_2P2Z_CLA_Coef1;
    extern volatile float	*PWMDRV_1chHiRes_CLA_Duty6;
    extern volatile float	*Kff;
    extern volatile float	vFFvMin;
    extern volatile float	lineCurrentGain;
    extern volatile float	lineCurrentOffset;
    extern float ledCurrentSet;
    extern float ledCurrentRef;
    extern float ledCurrentRampVal;
    extern int	ledMinDuty;
    extern int	ledRampSpeed;
    extern float ledCurrentSetVal;
    //extern float  ledDimSet;
    
    extern volatile int syncDisableFlag;
    extern volatile int	ledCurrentCLA;
    extern volatile int	ADC2_pfcVoltage;
    extern volatile int	ADC4_lineVoltage;
    extern volatile int	ADC5_heaterTemperature;
    extern volatile int	ADC6_ledVoltage;
    extern volatile int	ADC7_hk12Voltage;
    extern unsigned int	ledVoltageCLA;
    
    extern volatile long	currentFastIQ;
    extern volatile long	currentFiltIQ;
    extern volatile long	currentDimmingRms;
    
    // Locating pragmas for CLA variables.
    #pragma DATA_SECTION(CNTL_2P2Z_CoefStruct1, "CNTL_2P2Z_Coef");
    struct CNTL_2P2Z_CoefStruct CNTL_2P2Z_CoefStruct1;
    
    #pragma DATA_SECTION(CNTL_2P2Z_CLACoefStruct_LED, "CNTL_2P2Z_CLA_Coef");
    struct CNTL_2P2Z_CLACoefStruct CNTL_2P2Z_CLACoefStruct_LED;
    
    #pragma DATA_SECTION(ledControlOut, "Cla1ToCpuMsgRAM");
    volatile float ledControlOut;
    #pragma DATA_SECTION(ledFFgain, "CpuToCla1MsgRAM");
    volatile float ledFFgain;
    //#pragma DATA_SECTION(ledCurrentRef, "CpuToCla1MsgRAM");
    //volatile float ledCurrentRef;
    #pragma DATA_SECTION(ledCurrentAdc, "Cla1ToCpuMsgRAM");
    volatile float ledCurrentAdc;
    #pragma DATA_SECTION(ledDuty, "Cla1ToCpuMsgRAM");
    volatile float ledDuty;
    #pragma DATA_SECTION(vOutAdc, "Cla1ToCpuMsgRAM");
    volatile float vOutAdc;
    
    //Slope of temperature sensor (deg. C / ADC code, fixed point Q15 format)
    #define getTempSlope() (*(int (*)(void))0x3D7E82)()
    //ADC code corresponding to temperature sensor output at 0-degreesC
    #define getTempOffset() (*(int (*)(void))0x3D7E85)()
    
    // PFC control loop gains
    long pfcPgain;
    long pfcIgain;
    long pfcDgain;
    long pfcFastPgain;
    long pfcFastIgain;
    
    // LED control loop gains
    float ledPgain;
    float ledIgain;
    float ledDgain;
    
    // State machine variables
    int previousState	= MCU_RESET;
    int currentState	= PFC_BOOT;
    int statusLedBlink 	= 0;
    
    // Timer variables
    volatile unsigned int timer10ms = 0;
    volatile unsigned int canTimer	= 0;
    volatile unsigned long i2cTimer	= 0;
    volatile unsigned int sfoTimer	= 0;
    volatile unsigned int sciTimer	= 0;
    volatile unsigned int adTimer	= 0;
    volatile unsigned int configExitTimer	= 0;
    volatile unsigned int connectTimer = 0;
    volatile unsigned int errorDelay = 0;
    volatile unsigned int warmUpDelayTimer = 0;
    
    // Operating time counters
    unsigned long opSeconds		= 0;
    unsigned long opSecondsB1	= 0;
    unsigned long opSecondsB2	= 0;
    unsigned long opSecondsB3	= 0;
    unsigned long opSecondsB10	= 0;
    unsigned long opSecondsB4	= 0;
    unsigned long opSecondsB30	= 0;
    unsigned long opSecondsB5	= 0;
    
    // Error counting and logging.
    int runtimeErrorsEnable		 		= 0;
    unsigned int pfcOCErrorLog			= 0;
    unsigned int pfcOCErrorCount		= 0;
    unsigned int pfcOVErrorLog			= 0;
    unsigned int pfcOVErrorCount		= 0;
    unsigned int pfcUVErrorLog			= 0;
    unsigned int pfcUVErrorCount		= 0;
    unsigned int v12ErrorLog			= 0;
    unsigned int v12ErrorCount			= 0;
    unsigned int ledVErrorLog			= 0;
    int ledOVErrorCount		= 0;
    unsigned int ledUVErrorCount		= 0;
    unsigned int ledMaxOVErrors			= LED_MAX_OV_ERRORS;
    unsigned int ledRestartMax			= LED_RESTART_MAX;
    unsigned int ledOCErrorLog			= 0;
    unsigned int ledOCErrorCount		= 0;
    unsigned int ledPeakOCErrorLog		= 0;
    unsigned int ledPeakOCErrorCount	= 0;
    
    unsigned int ledMaxUVErrors			= LED_MAX_UV_ERRORS;
    
    unsigned int ledRestart				= 0;
    unsigned int ledRestartCounter		= 0;
    unsigned int ledRestartVoltage		= 0;
    unsigned int ledOCWaitCounter		= 0;
    unsigned int ledOCErrorReset        = LED_OC_RESET;
    
    unsigned int warmUpDelayComplete    = 0;
    unsigned int startWarmUpDelay       = 0;
    float ledCurrentSetValHalf = 0;
    
    // PFC protection and regulation.
    long pfcVoltageRef	= 0;
    long pfcSetRef		= 0;
    long pfcDuty		= 0;
    long pfcControlOut	= 0;
    long pfcUVHRun		= 0;
    long pfcUVLRun		= 0;
    
    #define GUARD_SYNC_TIMEOUT  150
    #define GUARD_SYNC_ACTIVE_TIME 10
    int guardSyncMasterFlag = 0;
    int guardSyncIn = 0;
    int guardSyncTimer = GUARD_SYNC_TIMEOUT;
    int guardSyncOddEven = 0;
    int guardSyncActiveDelay = 0;
    int guardSyncActive = 0;
    
    #define L852G_TEST   0
    
    // LED protection and regulation.
    float 			ledCurrentSetSave	= 0;
    unsigned int 	ledOverCurrentSet	= 0;
    long 			ledShortVoltage		= 0;
    float 			ledRampVal 			= 0.0;
    float 			ledRestartVal		= 0.000005;
    long 			ledOpenVoltage 		= 0;
    int				runLedCal			= 0;
    unsigned int	ledCalFinished		= 0;
    unsigned int	ledCalNotifyTimer  	= 0;
    unsigned long	ledCalTimer			= 0;
    
    unsigned int    ledOnTimer          = 0;
    unsigned int    timerOn             = 0;
    
    int             turnLedOn           = 0;
    unsigned int    bleLedOnTimer       = 0;
    unsigned int    bleTimerOn          = 0;
    
    // Common loop counters;
    volatile int i;
    volatile int j;
    volatile int k;
    
    // Hi-res ePWM scale factor.
    int MEP_ScaleFactor	= 115;
    int sfoStatus		= 0;
    
    // Operating point variables.
    long v12Hk		= 0;
    long ledCurrent = 0;
    long ledVoltage = 0;
    int currentStep	= B0;
    long lineFrequency = 0;
    
    // Temperature and related variables.
    int tempFiltCount		 = 0;
    long heaterCelsius		= _IQ15(25.0);
    long mcuCelsius			= _IQ15(25.0);
    long heatTempFilter		= 0;
    int heaterMaxTempRun	= 0;
    int	mcuMaxTemp			= 0;
    int mcuMaxTempRun		= 0;
    long heatOnTemp			= 12.4;
    long heatOffTemp		= 34.0;
    
    // CCR amp control
    int ccrCntlTimer 		= 0;
    int ccrCntlStep 		= 0;
    int ccrCntlStepPrev 	= 0;
    int ccrCntlWord 		= 0;
    int ccrMsgHeader		= 0;
    int ccrMsgData			= 0;
    int ccrMsgPending		= 0;
    int ccrMsgBitCount		= 0;
    int ccrMsgReceived		= 0;
    int heatAmpCntl 		= 0;
    
    // I2C and EEPROM variables.
    int i2cErrorCount	= 0;
    int i2cRetries		= 0;
    int i2cStatus		= 0;
    int eepromData[16];
    int blockPointer	= 0;
    int bytePointer		= 16;
    int	eepromInitFlag 	= 0;
    
    int currentSendTimer = 0;
    
    // ERGL variables.
    int erglCounter	= 0;
    int erglLimit		= 0;
    int erglFlag		= 0;
    int	erglState		= ERGL_LEFT_ON;
    int	erglStatePrev	= ERGL_LEFT_ON;
    int	erglNextState	= ERGL_RIGHT_ON;
    int erglRightFault	= 0;
    int erglLeftFault	= 0;
    int erglRightOVErrorCount	= 0;
    int erglRightUVErrorCount	= 0;
    int erglLeftOVErrorCount	= 0;
    int erglLeftUVErrorCount	= 0;
    int guardFlag				= 0;
    
    extern int currentCalFlag;
    int currentCalTimer = 5;
    
    int ledManualBlinkTimer = 0;
    
    int failOpenFlag = 1;
    int failOpenOnTimer = 0;
    int failOpenOffTimer = 0;
    
    // Bluetooth (serial message) variables.
    long 	bleMessage		= 0;
    int 	bluetoothMode	= 0;
    int		bleCode		 	= 0;
    int 	bleCommand		= 0;
    int 	bleDataID		= 0;
    int		bleData			= 0;
    int	 	bleNotify 		= 0;
    int 	bleNotifyData 	= 0;
    int 	configOnEntry 	= 1;
    int		diagOnEntry 	= 1;
    int 	connect 		= 0;
    int 	sendAdName 		= 0;
    char	adName[12] 		= {'C','R','O','U','S','E','-','H','I','N','D','S'};
    //int 	serialTxBytes[16];        // For Bluetooth
    int     serialTxBytes[200];       // For serial communication
    
    
    // Configuration variables.
    unsigned int oddEvenConfigValid = 0xFF;
    unsigned int oddEvenConfig = 0xFF;
    unsigned int phaseConfigValid = 0xFF;
    unsigned int phaseConfig = 0xFF;
    unsigned int dimRampConfigValid = 0xFF;
    unsigned int dimRampConfig = 0xFF;
    unsigned int hardwareConfigValid = 0xFF;
    unsigned int hardwareConfig = 0xFF;
    
    unsigned int ledCalValid = 0xFF;
    long ledShortConfig = _IQ15(0.0);
    
    // Jumper settings.
    unsigned int configJumpers = 0;
    unsigned int signJumper = 1;
    unsigned int boostJumper = 1;
    unsigned int monitorJumper = 1;
    unsigned int hirlJumper = 1;
    
    unsigned int test_timer = 0;
    unsigned int reCalibrate = 0;
    unsigned int toggleFlag = 0;
    
    // Flash variables.
    extern Uint32 Flash_CPUScaleFactor;
    
    typedef struct {
         Uint16 *StartAddr;
         Uint16 *EndAddr;
    } SECTOR;
    
    #define FLASH_START_ADDR  0x3E8000
    SECTOR Sector[8]= {
             (Uint16 *) 0x3E8000,(Uint16 *) 0x3E9FFF,
             (Uint16 *) 0x3EA000,(Uint16 *) 0x3EBFFF,
             (Uint16 *) 0x3EC000,(Uint16 *) 0x3EDFFF,
             (Uint16 *) 0x3EE000,(Uint16 *) 0x3EFFFF,
             (Uint16 *) 0x3F0000,(Uint16 *) 0x3F1FFF,
             (Uint16 *) 0x3F2000,(Uint16 *) 0x3F3FFF,
             (Uint16 *) 0x3F4000,(Uint16 *) 0x3F5FFF,
             (Uint16 *) 0x3F6000,(Uint16 *) 0x3F7FFF,
    };
    
    #define GPI31Value       GpioDataRegs.GPADAT.bit.GPIO31
    #define GPI41Value       GpioDataRegs.GPBDAT.bit.GPIO41
    
    int ledCalNotifyTime = 1000;
    long ledAutoCalTime = 359000;
    int ManualBlinkTime = 100;
    
    //unsigned int FirstSetGpio;
    //unsigned int SecondSetGpio;
    //unsigned int FirstOneSet;
    
    //#define GPIO22Value       GpioDataRegs.GPADAT.bit.GPIO22
    //#define GPIO23Value       GpioDataRegs.GPADAT.bit.GPIO23
    
    //unsigned int toggle;
    
    void main(void)
    {
        GpioDataRegs.GPBSET.bit.GPIO34 = 1;
    
    //    FirstSetGpio = 0;
    //    SecondSetGpio = 0;
    //    FirstOneSet = 0;
    //    toggle = 1;
    
    //=================================================================================
    //	MCU INITIALIZATION
    //=================================================================================
    	IER = 0x0000;							// Disable interrupts.
    	DeviceInit();							// Setup clocks, GPIOs, etc. (ULEDPS-F2803xInit.c
    
    	MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);	// Copy timing critical functions to RAM.
    
    	ADCinit();								// Initialize the ADC module..
    	I2Cinit();								// Initialize I2C module.
    	scia_init();							// Initialize the serial port.
    	CpuTimer0Regs.PRD.all =  600000;		// Set timer 0 the count down to 0 in 10mS (600000 clocks at 60mHz).
    	CpuTimer1Regs.PRD.all =  6000000;		// Set timer 1 for I2C error detection.
    
    //====================================================================================
    // INTERRUPTS & PWM INITIALIZATION
    //====================================================================================
    // Both the 28x core and CLA core have assembly interrupt routines that run in parallel.
    // Both are triggered at 100kHz by different PWMs to allow phasing. The ADC is triggered by
    // a compare action so conversions may be placed anywhere within the PWM cycle.
    
    	boostJumper = GpioDataRegs.GPADAT.bit.GPIO17;				// Read PCB jumper resistors to determine hardware type.
    	signJumper = GpioDataRegs.GPADAT.bit.GPIO18;
    	monitorJumper = GpioDataRegs.GPADAT.bit.GPIO20;
    	hirlJumper = GpioDataRegs.GPADAT.bit.GPIO21;
    
    //	hirlJumper = 0;
    
    	dimOverride = 0;											// Initialize dimming override for DC output units and elevated guard light
    	dimRampVal = 5;
    
    	EALLOW;														// Many of the following registers are EALLOW protected.
    
    #if	DEBUG
    	EPwm1Regs.TBCTL.bit.FREE_SOFT = 2;							// Set PWM emulation behavior to free-run
    	EPwm2Regs.TBCTL.bit.FREE_SOFT = 2;							// Set PWM emulation behavior to free-run
    	EPwm3Regs.TBCTL.bit.FREE_SOFT = 2;							// Set PWM emulation behavior to free-run
    	EPwm4Regs.TBCTL.bit.FREE_SOFT = 2;							// Set PWM emulation behavior to free-run
    	EPwm5Regs.TBCTL.bit.FREE_SOFT = 2;							// Set PWM emulation behavior to free-run
    	EPwm6Regs.TBCTL.bit.FREE_SOFT = 2;							// Set PWM emulation behavior to free-run
    	EPwm7Regs.TBCTL.bit.FREE_SOFT = 2;							// Set PWM emulation behavior to free-run
    #endif
    
    	SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;						// Disable TBCLK during setup
    
    	if(boostJumper == 0)
    	{
    		ledBoostConfig(LED_EPWM, LED_PERIOD);					// Set up LED converter PWM. Boost topology for ERGL and IRGL,
    	}															// Buck for all others.
    	else
    	{
    		ledBuckConfig(LED_EPWM, LED_PERIOD);
    	}
    
    	inputPfcConfig(INPUT_EPWM, PFC_PERIOD, INPUT_COMPARATOR);	// Set up input PFC PWM.
    	EPwm1Regs.CMPA.half.CMPA		= 50;
    
    	ledDimmerConfig(LED_DIM_EPWM, LED_DIM_PERIOD);				// Set up LED dimmer PWM.
        ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD);            // Set up delayed LED dimmer PWM.
    	interrupt28xConfig(INT28X_EPWM, INTERRUPT_PERIOD);			// Set up 28x interrupt timer.
    	interruptCLAConfig(INTCLA_EPWM, INTERRUPT_PERIOD);			// Set up CLA interrupt timer.
    	heaterDriveConfig(HEAT_EPWM, HEATER_PERIOD);				// Set up PWM to activate heater.
    
    	PieVectTable.EPWM2_INT = &CTRL_ISR;							// Point to 28x assembly interrupt vector.
    	PieCtrlRegs.PIEIER3.bit.INTx2 = 1;      					// Enable the 28x interrupt in the interrupt controller.
    	EPwm2Regs.ETSEL.bit.INTEN = 1;               				// Enable the interrupt signal in the PWM module.
    	EPwm2Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO;   				// Trigger on TBCTR=0.
    	EPwm2Regs.ETPS.bit.INTPRD = ET_1ST; 						// Interrupt triggered every cycle.
    
    	EPwm2Regs.ETSEL.bit.SOCAEN = 1;								// Enable start of conversion signal.
    	EPwm2Regs.ETSEL.bit.SOCASEL = ET_CTRU_CMPB;					// Trigger ADC conversions on PWM4 when CMPB is reached.
    	EPwm2Regs.ETPS.bit.SOCAPRD = ET_1ST;						// Trigger ADC every cycle.
    
    	EPwm2Regs.CMPB = 520;										// Set timing for ADC start of conversion.
    
    	EPwm4Regs.ETSEL.bit.INTEN = 1;               				// Enable the interrupt signal for the CLA in the PWM module.
    	EPwm4Regs.ETSEL.bit.INTSEL = ET_CTR_ZERO;   				// Trigger on TBCTR=0.
    	EPwm4Regs.ETPS.bit.INTPRD = ET_1ST;							// Interrupt triggered every cycle.
    
    	EPwm1Regs.HRMSTEP=MEP_ScaleFactor;							// Set initial hi-res PWM scale factor.
    
    	IER |= M_INT3;												// Enable the MCU interrupts for PWM INT.
    
    	SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;   					// Sync PWM time-base clocks
    
    	EDIS;
    
    	InitFlash();			// Set up internal flash functions.
    
    	Flash_CallbackPtr = 0;	// Flash callback set to 0 for now;
    
    	CTRL_INIT();			// Initialize assembly variables (28x core) see ULEDPS-28x-ISR.asm
    	CLA_CTRL_INIT();		// Initialize assembly variables (CLA core) see ULEDPS-CLA-ISR.asm
    
    	currentCalFlag = 0;
    
    
    	// Read configuration settings from EEPROM for initial setup.
    	i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, CONFIGDATA0, eepromData);	// Read EEPROM data.
    
    	if(i2cStatus == I2C_ERROR)						// Check for errors and timeout to prevent getting
    	{												// stuck on a bus hang-up.
    	   i2cErrorCount++;
    	}
    
    	if(eepromData[0] != EEPROM_DATA_VALID)			// Check if the data just read is valid or an erased EEPROM.
    	{
    		i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK1, CONFIGDATA0, eepromData);	// Attempt to read EEPROM from secondary config block.
    
    		if(i2cStatus == I2C_ERROR)
    		{
    		   i2cErrorCount++;
    		}
    
    		if(eepromData[0] != EEPROM_DATA_VALID)
    		{
    			initEEPROMpage(CONFIGDATA0);				// If secondary block was invalid, set config data to factory values.
    		}
    		else
    		{
    			processEEPROMread(CONFIGDATA0, eepromData);	// If secondary block was valid, read it in as config data.
    		}
    
    	}
    	else
    	{
    		processEEPROMread(CONFIGDATA0, eepromData);		// If primary block was valid, read it in as config data.
    	}
    
    // Read configuration settings from jumpers (on JTAG header).
    	configJumpers = 0;
    
    	int config = 0;
    
    	config = GpioDataRegs.GPBDAT.bit.GPIO35;
    	configJumpers = configJumpers|(config<<4);
    
    	config = GpioDataRegs.GPBDAT.bit.GPIO38;
    	configJumpers = configJumpers|(config<<3);
    
    	config = GpioDataRegs.GPADAT.bit.GPIO1;
    	configJumpers = configJumpers|(config<<2);
    
    	config = GpioDataRegs.GPADAT.bit.GPIO2;
    	configJumpers = configJumpers|(config<<1);
    
    	config = GpioDataRegs.GPADAT.bit.GPIO3;
    	configJumpers = configJumpers|config;
    
    	if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)
    	{
    	    ledCalNotifyTime = 500;
    	    ledAutoCalTime = 3000;
    	    ManualBlinkTime = 200;
    	}
    	else
        {
            ledCalNotifyTime = 1000;
            ledAutoCalTime = 359000;
            ManualBlinkTime = 100;
        }
    
    	if (monitorJumper == 0)
    	{
    		failOpenFlag = 0;
    	}
    	if(hardwareConfigValid != 0xFF)
    	{
    		if(hardwareConfigValid == hardwareConfig)
    		{
    			configJumpers = hardwareConfig;
    		}
    	}
    
    #if JUMPER_PRESET							// Override the jumpers settings (see settings file).
    	configJumpers = CODE_JUMPER_SETTING;
    #endif
    
    //	if((boostJumper == 1) && (signJumper == 1))
    	if(signJumper == 1)  // Fixes the problem with the HIRL.  May create a problem with 804 and 852G. SAMPLE 1
    	{
    		if(ledCalValid == EEPROM_DATA_VALID)
    		{
    			ledShortVoltage = ledShortConfig;
    		}
    		else
    		{
    			runLedCal = 1;
    		}
    
    		reCalibrate = 1;
    
    		if (boostJumper == 0)
    		{                           // Don't re-calibrate if L852G or L804
    		    if (configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS || configJumpers == L804 || configJumpers == L804_LEFT_FULL_ON || configJumpers == L804_RIGHT_FULL_ON)
    		    {
    		        reCalibrate = 0;
    		    }
    		}
    
    		if (hirlJumper == 0)
    		{                           // Don't re-calibrate if L852G/S
    		    if (configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON)
    		    {
    		        reCalibrate = 0;
    		    }
    		}
    
    		if (reCalibrate == 1)
    		{
    		    if (hirlJumper == 0 && configJumpers == NO_JUMPERS) // If a HIRL and configJumpers is equal to NO_JUMPERS, then don't look at GPIO41
    		    {
                    oddEvenConfigValid  = 0xFF;
                    oddEvenConfig       = 0xFF;
                    phaseConfigValid    = 0xFF;
                    phaseConfig         = 0xFF;
                    dimRampConfigValid  = 0xFF;
                    dimRampConfig       = 0xFF;
                    ledCalValid         = 0xFF;
                    ledShortConfig      = _IQ15(0.0);
                    hardwareConfigValid = 0xFF;
                    hardwareConfig      = 0xFF;
    
                    ledShortVoltage = 0;
                    ledCalTimer = 0;                            // Was 'LED_AUTO_CAL_TIME - 1000'. Changed to 0 to stop calibration after 10 seconds
                    runLedCal = 1;
    
                    processEEPROMwrite(CONFIGDATA0, eepromData);
                    i2cStatus = transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);
    
                    if(i2cStatus == I2C_ERROR)
                    {
                       i2cErrorCount++;
                    }
    		    }
    		    else
    		    {
    #if HARDWARE_21696
    //    		    if(GpioDataRegs.GPBDAT.bit.GPIO40 == 0)
                    if(GPI41Value == 0)                        // GPIO40 and GPIO41 pulled low by bluetooth board always, so GPIO40 can't be used with a bluetooth board
                    {
    #else
    //    		    if(GpioDataRegs.GPADAT.bit.GPIO31 == 0)
                    if (GPI35Value == 0)
                    {
    #endif
    
                        ledCalValid = 0xFF;
                        ledShortConfig = 0;
                        ledShortVoltage = 0;
                        ledCalTimer = 0;                            // Was 'LED_AUTO_CAL_TIME - 1000'. Changed to 0 to stop calibration after 10 seconds
                        runLedCal = 1;
    
                        processEEPROMwrite(CONFIGDATA0, eepromData);
                        i2cStatus = transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);
    
                        if(i2cStatus == I2C_ERROR)
                        {
                           i2cErrorCount++;
                        }
    
            //			i2cNackPoll(EEPROM_SLAVE_ADDR);
                    }
                }
    		}
    	}
    
    	if((dimRampConfig >= 1)&&(dimRampConfig <= 5))		// If dim ramp config is valid, set the dimming ramp rate.
    	{
    		dimRampVal = dimRampConfig;
    	}
    	else
    	{
    		dimRampConfig = dimRampVal|0xF0;
    	}
    
    /*
        memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
        sprintf((char *)serialTxBytes, "-------------------------------------------- \r\n");
        sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    
        long l1 = 0, l2 = 0;
    
        l1 = _IQ15(73.76);
        l2 = _IQ15(-56.2314);
    
        memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
        sprintf((char *)serialTxBytes, "\r\n l1: %d, l2: %d \r\n", l1, l2);
        sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    */
        float f1 = 0, f2 = 0;
    /*
        f1 = _IQtoF(l1);
        f2 = _IQtoF(l2);
    
        memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
        sprintf((char *)serialTxBytes, "f1: %.2f, f2: %.4f \r\n", f1, f2);
        sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    
    */
    	if(boostJumper == 0)
    	{
    		// Common for boost units.
            // PFC controller set point and protection limits.
            pfcSetRef   = _IQ12mpy(PFC_SET_12V_852G, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.
    
            pfcOVH      = PFC_OVH_12V_852G;
            pfcOVL      = PFC_OVL_12V_852G;
            pfcUVHRun   = PFC_UVH_12V_852G;
            pfcUVLRun   = PFC_UVL_12V_852G;
    
            pfcUVH = PFC_UVH_STARTUP;
            pfcUVL = PFC_UVL_STARTUP;
    
            // PFC controller coefficients.
            pfcPgain        = KP_PFC;
            pfcIgain        = KI_PFC;
            pfcDgain        = KD_PFC;
            pfcFastPgain    = KP_PFC_FAST;
            pfcFastIgain    = KI_PFC_FAST;
    
            CNTL_2P2Z_CLA_Fdbk1 = &ledCurrentAdc;                       // LED feedback current.
    
            // LED controller coefficients.
            ledPgain    = KP_LED;
            ledIgain    = KI_LED;
            ledDgain    = KD_LED;
    
            ledFFgain = 1.8;//KFF_LED;
            Kff = &ledFFgain;       // LED feed-forward variables.
            vFFvMin = VFF_MIN40;
    
            syncDisableFlag     = 0;    // Enable dimming and sync of LED.
            dimDisable          = 0;
            ledFastOVDisable    = 0;
            ledMaxOVErrors      = BOOST_MAX_OV_ERRORS;
            ledRestartMax       = BOOST_RESTART_MAX;
    
            ledMinDuty = COLOR_DIM_MIN;
    
            dimOverride = 0;
    
            blinkTimer = 0;
    
    		switch(configJumpers)	// LED controller set point and protection limits set by jumpers.
    		{
                case HI450MA_WR_FAA:        // HIRL L862 White - Red
    
                    pfcSetRef   = _IQ12mpy(PFC_SET_862_WR, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.
    
                    ledCurrentSetVal    = ((float)((0.471 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledFastOV           =  (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);
                    ledOpenVoltage      = _IQ15(81.0);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage     = _IQ15(35.0);
                    }
    
                    EALLOW;
                    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;                    // Activate GPIO40 as epwm7A to follow dimming
                    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD);        // Set up Halo dimmer PWM.
                    EDIS;
    
                    erglFlag = 0;
                    blinkDisable = 1;       // Disable blinking
                    ledMinDuty = WHITE_DIM_MIN;
    
                    break;
    
                case HI470MA_WG_FAA:       // HIRL L862 White - Green
    
                    pfcSetRef   = _IQ12mpy(PFC_SET_862_WG, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.
    
                    ledCurrentSetVal    = ((float)((0.483 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledFastOV           =  (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);
                    ledOpenVoltage      = _IQ15(81.0);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage     = _IQ15(35.0);
                    }
    
                    EALLOW;
                    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;                    // Activate GPIO40 as epwm7A to follow dimming
                    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD);        // Set up Halo dimmer PWM.
                    EDIS;
    
                    erglFlag = 0;
                    blinkDisable = 1;       // Disable blinking
                    ledMinDuty = WHITE_DIM_MIN;
    
                    break;
    
                case HI440MA_WW_FAA:        // HIRL L862 White - White
    
    /* SAMPLE 1     ledCurrentSetVal    = ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                    ledFastOV           =  (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);
                    ledOpenVoltage      = _IQ15(90.0);
                    ledShortVoltage     = _IQ15(60.0);
    */
                    ledCurrentSetVal    = ((float)((0.442 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));   // SAMPLE 1
                    ledOverCurrentSet   = ((int)(0.675 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));            // SAMPLE 1
                    ledFastOV           =  (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);                           // SAMPLE 1
                    ledOpenVoltage      = _IQ15(81.0);      // SAMPLE 1 - Need to address cold temp variations
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage     = _IQ15(40);        // SAMPLE 1 - Need to address cold temp variations
                    }
    
                    EALLOW;
                    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;                    //Activate GPIO40 as epwm7A to follow dimming
                    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD);        // Set up Halo dimmer PWM.
                    EDIS;
    
                    erglFlag = 0;
                    blinkDisable = 1;       // Disable blinking
                    ledMinDuty = WHITE_DIM_MIN;
    
                    break;
    
                case HI460MA_210_WW:       // HIRL L862-WR; Fig2.10-WW
    
                    pfcSetRef   = _IQ12mpy(PFC_SET_12V_210_WW, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.
    
                    ledCurrentSetVal        = ((float)((0.470 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledCurrentSetValHalf    = ((float)((0.235 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    
                    ledOverCurrentSet   = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledFastOV           =  (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);
                    ledOpenVoltage      = _IQ15(90.0);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage     = _IQ15(50.0);
                    }
    
                    pfcUVHRun   = PFC_UVH_12V_852G_NEW;
                    pfcUVLRun   = PFC_UVL_12V_852G_NEW;
    
    //                ledPgain    = KP_LED_1;
    //                ledIgain    = KI_LED_1;
    //                ledDgain    = KD_LED_1;
    
                    EALLOW;
                    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;                    //Activate GPIO40 as epwm7A to follow dimming
                    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD);        // Set up Halo dimmer PWM. Only used on Fig2.10 WW
                    EDIS;
    
                    erglFlag = 0;
                    blinkDisable = 1;       // Disable blinking
                    ledMinDuty = WHITE_DIM_MIN;
    
                    break;
    
                case HI460MA_210_WY:       // HIRL L862-WR; Fig2.10-WY, WR
    
                    pfcSetRef   = _IQ12mpy(PFC_SET_12V_210_WY, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN)); // Set point for PFC bus.
    
                    ledCurrentSetVal    = ((float)((0.470 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledFastOV           =  (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);
                    ledOpenVoltage      = _IQ15(90.0);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage     = _IQ15(40.0);
                    }
    
                    pfcUVHRun   = PFC_UVH_12V_852G_NEW;
                    pfcUVLRun   = PFC_UVL_12V_852G_NEW;
    
                    EALLOW;
                    GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;                    //Activate GPIO40 as epwm7A to follow dimming
                    ledDelayDimmerConfig(HALO_EPWM, LED_DIM_PERIOD);        // Set up Halo dimmer PWM. Only used on Fig2.10 WW
                    EDIS;
    
                    erglFlag = 0;
                    blinkDisable = 1;       // Disable blinking
                    ledMinDuty = WHITE_DIM_MIN;
    
                    break;
    
                case HI440MA_WY_FAA:        // HIRL L862-WY
    
                    ledCurrentSetVal        = ((float)((0.442 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledFastOV           =  (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);
                    ledOpenVoltage      = _IQ15(81.0);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage     = _IQ15(40.0);
                    }
    
                    erglFlag = 0;
                    blinkDisable = 1;       // Disable blinking
                    ledMinDuty = WHITE_DIM_MIN;
    
                    break;
    
    /*            case HI470MA_BST:       // HIRL L862-WG NON-FAA
    
                    ledCurrentSetVal        = ((float)((0.480 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    
     //             ledOverCurrentSet   = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                    ledOverCurrentSet   = ((int)(0.720 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledFastOV           =  (unsigned int)(65.0 * LED_VINPUT_GAIN * ADC_GAIN);
                    ledOpenVoltage      = _IQ15(90.0);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage     = _IQ15(25.0);
                    }
    
                    erglFlag = 0;
                    blinkDisable = 1;       // Disable blinking
    
                    break;
    */
                case HI500mA_BST:       // L850CD, IREL White
    
                    ledCurrentSetVal    = ((float)((0.502 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                    ledFastOV           =  (unsigned int)(95.0 * LED_VINPUT_GAIN * ADC_GAIN);
                    ledOpenVoltage      = _IQ15(81.0);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage     = _IQ15(40.0);
                    }
    
                    erglFlag = 0;
                    blinkDisable = 1;       // Disable blinking
                    ledMinDuty = WHITE_DIM_MIN;
    
                    break;
    
                case L804:	// L804 configuration.
    
    				ledCurrentSetVal		= ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));//TOM
    				ledOverCurrentSet	= ((int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				ledFastOV			=  (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);
    				ledOpenVoltage		= _IQ15(VMAX_GUARD);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage		= _IQ15(VMIN_GUARD);
                    }
    
    				erglFlag = 1;
    				blinkDisable = 1;	// L804 alternates, so blinking is disabled.
    				dimOverride = 0;	// Dimming override set to 0 for wig/wag transition
    
    				ledMaxUVErrors  = ERGL_LED_MAX_UV_ERRORS;
    
    				EALLOW;
    				GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 0;
    				GpioCtrlRegs.GPBDIR.bit.GPIO40 = 1;
    				GpioDataRegs.GPBCLEAR.bit.GPIO40 = 1;
    				GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;		//TR 11-10-14 was GPIO34
    				GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1;			//TR 11-10-14 was GPIO34
    				GpioDataRegs.GPBSET.bit.GPIO41 = 1;			//TR 11-10-14 was GPIO34
    				EDIS;
    
    				adName[0] = 'L';
    				adName[1] = '8';
    				adName[2] = '0';
    				adName[3] = '4';
    				adName[4] = '-';
    				adName[5] = 'E';
    				adName[6] = 'R';
    				adName[7] = 'G';
    				adName[8] = 'L';
    				adName[9] = ' ';
    				adName[10] = ' ';
    				adName[11] = ' ';
    
    				break;
    
    			case L804_LEFT_FULL_ON:		// L804 Photo Test
    
    				ledCurrentSetVal		= ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				ledFastOV			=  (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);
    				ledOpenVoltage		= _IQ15(VMAX_GUARD);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage		= _IQ15(VMIN_GUARD);
                    }
    
    				erglFlag = 0;
    				blinkDisable = 1;
    
    				ledMaxUVErrors  = ERGL_LED_MAX_UV_ERRORS;
    
    				EALLOW;
    				GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 0;
    				GpioCtrlRegs.GPBDIR.bit.GPIO40 = 1;
    				GpioDataRegs.GPBSET.bit.GPIO40 = 1;
    				GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;		//TR 11-10-14 was GPIO34
    				GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1;			//TR 11-10-14 was GPIO34
    				GpioDataRegs.GPBCLEAR.bit.GPIO41 = 1;		//TR 11-10-14 was GPIO34
    				EDIS;
    
    				adName[0] = 'L';
    				adName[1] = '8';
    				adName[2] = '0';
    				adName[3] = '4';
    				adName[4] = '-';
    				adName[5] = 'E';
    				adName[6] = 'R';
    				adName[7] = 'G';
    				adName[8] = 'L';
    				adName[9] = ' ';
    				adName[10] = ' ';
    				adName[11] = ' ';
    				break;
    
    			case L804_RIGHT_FULL_ON:		// L804 Photo Test
    
    				ledCurrentSetVal		= ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				ledFastOV			=  (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);
    				ledOpenVoltage		= _IQ15(VMAX_GUARD);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage		= _IQ15(VMIN_GUARD);
                    }
    
    				erglFlag = 0;
    				blinkDisable = 1;
    
    				ledMaxUVErrors  = ERGL_LED_MAX_UV_ERRORS;
    
    				EALLOW;
    				GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 0;
    				GpioCtrlRegs.GPBDIR.bit.GPIO40 = 1;
    				GpioDataRegs.GPBCLEAR.bit.GPIO40 = 1;
    				GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;		//TR 11-10-14 was GPIO34
    				GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1;		 	//TR 11-10-14 was GPIO34
    				GpioDataRegs.GPBSET.bit.GPIO41 = 1;			//TR 11-10-14 was GPIO34
    				EDIS;
    
    				adName[0] = 'L';
    				adName[1] = '8';
    				adName[2] = '0';
    				adName[3] = '4';
    				adName[4] = '-';
    				adName[5] = 'E';
    				adName[6] = 'R';
    				adName[7] = 'G';
    				adName[8] = 'L';
    				adName[9] = ' ';
    				adName[10] = ' ';
    				adName[11] = ' ';
    				break;
    
    			case L852G_FULL_ON:		// L852g Photo Test
    
    				ledCurrentSetVal		= ((float)((0.490 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				ledFastOV			=  (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);
    				ledOpenVoltage		= _IQ15(VMAX_GUARD);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage		= _IQ15(VMIN_GUARD);
                    }
    
    				erglFlag = 0;
    				blinkDisable = 1;		// Disable blinking for photo test.
    
    				adName[0] = 'L';
    				adName[1] = '8';
    				adName[2] = '5';
    				adName[3] = '2';
    				adName[4] = 'G';
    				adName[5] = '-';
    				adName[6] = 'I';
    				adName[7] = 'R';
    				adName[8] = 'G';
    				adName[9] = 'L';
    				adName[10] = ' ';
    				adName[11] = ' ';
    
    				break;
    
    			case L852G_ODD:		// L852G First OFF configuration
    
    				ledCurrentSetVal		= ((float)((0.490 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				ledFastOV			=  (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);
    				ledOpenVoltage		= _IQ15(VMAX_GUARD);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage		= _IQ15(VMIN_GUARD);
                    }
    
    				erglFlag = 0;
    				guardFlag = 1;
    				blinkDisable = 0;
    				blinkState = 0;
    
    				guardSyncOddEven = 0;
    
    				adName[0] = 'L';
    				adName[1] = '8';
    				adName[2] = '5';
    				adName[3] = '2';
    				adName[4] = 'G';
    				adName[5] = '-';
    				adName[6] = 'I';
    				adName[7] = 'R';
    				adName[8] = 'G';
    				adName[9] = 'L';
    				adName[10] = ' ';
    				adName[11] = ' ';
    
    				break;
    
    			default:	// L852G First ON configuration.
    
    				ledCurrentSetVal		= ((float)((0.490 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				ledFastOV			=  (unsigned int)(81.0 * LED_VINPUT_GAIN * ADC_GAIN);
    				ledOpenVoltage		= _IQ15(VMAX_GUARD);
    
                    if(ledCalValid != EEPROM_DATA_VALID)
                    {
                        ledShortVoltage		= _IQ15(VMIN_GUARD);
                    }
    
    				erglFlag = 0;
    				guardFlag = 1;
    				blinkDisable = 0;
    				blinkState = 1;
    
    				guardSyncOddEven = 1;
    
    				adName[0] = 'L';
    				adName[1] = '8';
    				adName[2] = '5';
    				adName[3] = '2';
    				adName[4] = 'G';
    				adName[5] = '-';
    				adName[6] = 'I';
    				adName[7] = 'R';
    				adName[8] = 'G';
    				adName[9] = 'L';
    				adName[10] = ' ';
    				adName[11] = ' ';
    
    				break;
    			}
    
    
    			if(guardFlag == 1)
    			{
    				EALLOW;
    
    				syncDriveConfig(7, HEATER_PERIOD);				// Set up PWM to sync.
    				GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1; // Sync pwm output
    
    
    #if HARDWARE_21696
    				GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;
    				GpioCtrlRegs.GPBDIR.bit.GPIO41 = 0;
    				GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;
    				GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;
    #else
    				GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;
    				GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;
    				GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;
    				GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;
    #endif
    				EDIS;
    			}
    
    			if(oddEvenConfigValid == EEPROM_DATA_VALID)
    			{
    				if(oddEvenConfig == ODD)				// If blink config is saved in EEPROM, it overrides the jumper setting.
    				{
    					blinkState = 0;
    					guardSyncOddEven = 0;
    				}
    				else if (oddEvenConfig == EVEN)
    				{
    					blinkState = 1;
    					guardSyncOddEven = 1;
    				}
    			}
    			else
    			{
    				oddEvenConfig = blinkState|0xF0;
    			}
    
    			if(phaseConfigValid == EEPROM_DATA_VALID)
    			{
    				if(phaseConfig == DEFAULT_PHASE)		// If phase config is saved in EEPROM, use alternate phase.
    				{
    					blinkTimer = 0;
    				}
    				else if(phaseConfig == ALTERNATE_PHASE)
    				{
    					blinkTimer = 116;
    				}
    			}
    			else
    			{
    				phaseConfig = 2;
    			}
    
    	}
    
    	else	// ELEVATED TAXIWAY AND INSET FIXTURES
    	{
    		// Common settings for TEL and insets:
    		if(hirlJumper == 0)
    		{
    			// PFC controller set point and protection limits.
    			pfcSetRef		= _IQ12mpy(PFC_SET_HIRL, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
    			pfcOVH 		= PFC_OVH_HIRL;
    		  	pfcOVL 		= PFC_OVL_HIRL;
    			pfcUVHRun	= PFC_UVH_HIRL;
    			pfcUVLRun	= PFC_UVL_HIRL;
    
    			pfcUVH = PFC_UVH_STARTUP;
    		  	pfcUVL = PFC_UVL_STARTUP;
    
    		  	// PFC controller coefficients.
    			pfcPgain		= KP_PFC;
    		  	pfcIgain		= KI_PFC;
    		  	pfcDgain		= KD_PFC;
    		  	pfcFastPgain	= KP_PFC_FAST;
    		  	pfcFastIgain	= KI_PFC_FAST;
    
    		  	CNTL_2P2Z_CLA_Fdbk1 = &ledCurrentAdc;	// LED feedback current.
    
    			// LED controller coefficients.
    		  	ledPgain	= KP_LED;
    		  	ledIgain	= KI_LED;
    		  	ledDgain	= KD_LED;
    
    		  	ledFFgain = KFF_LED;
    		  	Kff = &ledFFgain;		// LED feed-forward variables.
    			vFFvMin = VFF_MIN55;		//was 32
    
    			ledOpenVoltage = _IQ15(VMAX_HIRL);
    
    			syncDisableFlag = 2;	// Enable dimming and auto sync of LED.
    			dimDisable = 0;
    
    			blinkDisable = 1;		// Disable blinking for these models
    
    			ledFastOVDisable = 1;
    
    			ledMinDuty = WHITE_DIM_MIN;
    
    			ledFastOV		=  (unsigned int)(60.0 * LED_VINPUT_GAIN * ADC_GAIN);
    
    			heatOnTemp		=	12.4;
    			heatOffTemp		=	34.0;
    
    		    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL	= DC_COMP3OUT;	// LED Buck. TR 2-23-17: Added to protect LED driver during oupt transients
    		    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL	= DC_COMP3OUT;	// Dimmer.
    
    
                switch(configJumpers)	// Fixture specific set point and protection limits set by jumpers.
                {
    
                    case TEST_MODE:
    
                        ledCurrentSetVal		= ((float)((0.900 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet	= ((unsigned int)(1.5 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL	= DC_COMP3OUT;	// LED Buck.
                        EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL	= DC_COMP3OUT;	// Dimmer.
    
                        dimDisable = 1;
                        dimOverride = 37501;
    
                        test_timer = TEST_CYCLE_TIME;
    
                        break;
    
                    case I630MA:    // 2.8-R, 2.3-G, 2.3/8-GR        Jumper Diagram 4
    
                        ledCurrentSetVal        = ((float)((0.650 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet   = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        break;
    
     /*               case I450MA:    // L862-YR, 2.10-YR
    
                        ledCurrentSetVal        = ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet   = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        break;
    */
      /*              case I475MA:    // FAA yellow-green
    
                        ledCurrentSetVal        = ((float)((0.475 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet   = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        break;
    */
                    case HI475MA_YG_FAA:    // L862 YG       Jumper Diagram 29
    
                        pfcSetRef       = _IQ12mpy(PFC_SET_HIRL_50, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                        ledCurrentSetVal        = ((float)((0.475 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet   = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                        ledOpenVoltage  = _IQ15(45.0);
    
                        break;
    
                    case HI450MA_YR_FAA:    // L862 YR      Jumper Diagram 2
    
                        pfcSetRef       = _IQ12mpy(PFC_SET_HIRL_50, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                        ledCurrentSetVal        = ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet   = ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                        ledOpenVoltage  = _IQ15(45.0);
    
                        break;
    
    
                    case I525MA_852G_H_OFF:    // L852G/S First OFF            Jumper Diagram 7
    
                        pfcSetRef       = _IQ12mpy(PFC_SET_HIRL_852G_S_SIGN, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                        pfcOVH      = PFC_OVH_HIRL_852G_S_SIGN;
                        pfcOVL      = PFC_OVL_HIRL_852G_S_SIGN;
                        pfcUVHRun   = PFC_UVH_HIRL_852G_S_SIGN;
                        pfcUVLRun   = PFC_UVL_HIRL_852G_S_SIGN;
    
                        pfcUVH = PFC_UVH_STARTUP_852G_S_SIGN;
                        pfcUVL = PFC_UVL_STARTUP_852G_S_SIGN;
    
                        // PFC controller coefficients.
                        pfcPgain        = KP_PFC_SIGN;
                        pfcIgain        = KI_PFC_SIGN;
                        pfcDgain        = KD_PFC_SIGN;
                        pfcFastPgain    = KP_PFC_FAST_SIGN;
                        pfcFastIgain    = KI_PFC_FAST_SIGN;
    
                        ledPgain    = KP_SIGN;
                        ledIgain    = KI_SIGN;
                        ledDgain    = KD_SIGN;
                        ledFFgain   = KFF_SIGN;
    
                        ledCurrentSetVal        = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));       // This particular unit was 6/5% low, so the current had to be set at 586 mA
                        ledOverCurrentSet   = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        ledOpenVoltage = _IQ15(VMAX_HIRL_2_852G_S_SIGN);
    
                        erglFlag = 0;
                        guardFlag = 1;
                        blinkDisable = 0;
                        blinkState = 0;
                        blinkTimer = 0;
                        dimOverride = 0;
    
                        guardSyncOddEven = 1;
                        syncDisableFlag     = 0;
    
                        ledRestartMax = BOOST_RESTART_MAX;
    
                        adName[0] = 'L';
                        adName[1] = '8';
                        adName[2] = '5';
                        adName[3] = '2';
                        adName[4] = 'G';
                        adName[5] = ' ';
                        adName[6] = ' ';
                        adName[7] = ' ';
                        adName[8] = ' ';
                        adName[9] = ' ';
                        adName[10] = ' ';
                        adName[11] = ' ';
    
    /*                  if(guardFlag == 1)
                        {
                            EALLOW;
    
                            syncDriveConfig(7, HEATER_PERIOD);              // Set up PWM to sync.
                            GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;            // Sync pwm output
    
    #if HARDWARE_21696
                            GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBDIR.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;
    #else
                            GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;
    #endif
                         EDIS;
                        }
    */
                        break;
    
                    case I525MA_852G_H_ON:    // L852G/S First ON          Jumper Diagram 5
    
                        pfcSetRef       = _IQ12mpy(PFC_SET_HIRL_852G_S_SIGN, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                        pfcOVH      = PFC_OVH_HIRL_852G_S_SIGN;
                        pfcOVL      = PFC_OVL_HIRL_852G_S_SIGN;
                        pfcUVHRun   = PFC_UVH_HIRL_852G_S_SIGN;
                        pfcUVLRun   = PFC_UVL_HIRL_852G_S_SIGN;
    
                        pfcUVH = PFC_UVH_STARTUP_852G_S_SIGN;
                        pfcUVL = PFC_UVL_STARTUP_852G_S_SIGN;
    
                        // PFC controller coefficients.
                        pfcPgain        = KP_PFC_SIGN;
                        pfcIgain        = KI_PFC_SIGN;
                        pfcDgain        = KD_PFC_SIGN;
                        pfcFastPgain    = KP_PFC_FAST_SIGN;
                        pfcFastIgain    = KI_PFC_FAST_SIGN;
    
                        ledPgain    = KP_SIGN;
                        ledIgain    = KI_SIGN;
                        ledDgain    = KD_SIGN;
                        ledFFgain   = KFF_SIGN;
    
                        ledCurrentSetVal        = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));       // This particular unit was 6/5% low, so the current had to be set at 586 mA
                        ledOverCurrentSet   = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        ledOpenVoltage = _IQ15(VMAX_HIRL_2_852G_S_SIGN);
    
                        f1 = _IQtoF(ledOpenVoltage);
    
                        memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
                        sprintf((char *)serialTxBytes, "------- ledOpenVoltage: %.2f \r\n", f1);
                        sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    
                        erglFlag = 0;
                        guardFlag = 1;
                        blinkDisable = 0;
                        blinkState = 1;
                        blinkTimer = 0;
                        dimOverride = 0;
    
                        guardSyncOddEven = 1;
                        syncDisableFlag     = 0;
    
                        ledRestartMax = BOOST_RESTART_MAX;
    
                        adName[0] = 'L';
                        adName[1] = '8';
                        adName[2] = '5';
                        adName[3] = '2';
                        adName[4] = 'G';
                        adName[5] = ' ';
                        adName[6] = ' ';
                        adName[7] = ' ';
                        adName[8] = ' ';
                        adName[9] = ' ';
                        adName[10] = ' ';
                        adName[11] = ' ';
    
    /*                  if(guardFlag == 1)
                        {
                            EALLOW;
    
                            syncDriveConfig(7, HEATER_PERIOD);              // Set up PWM to sync.
                            GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;            // Sync pwm output
    
    #if HARDWARE_21696
                            GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBDIR.bit.GPIO41 = 1;
                            GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;
    #else
                            GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;
    #endif
                            EDIS;
                        }
    */
                        break;
    
                    case I525MA_852G_H_FULL_ON:    // L852G/S Full ON          Jumper Diagram 6
    
                        pfcSetRef       = _IQ12mpy(PFC_SET_HIRL_852G_S_SIGN, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                        pfcOVH      = PFC_OVH_HIRL_852G_S_SIGN;
                        pfcOVL      = PFC_OVL_HIRL_852G_S_SIGN;
                        pfcUVHRun   = PFC_UVH_HIRL_852G_S_SIGN;
                        pfcUVLRun   = PFC_UVL_HIRL_852G_S_SIGN;
    
                        pfcUVH = PFC_UVH_STARTUP_852G_S_SIGN;
                        pfcUVL = PFC_UVL_STARTUP_852G_S_SIGN;
    
                        // PFC controller coefficients.
                        pfcPgain        = KP_PFC_SIGN;
                        pfcIgain        = KI_PFC_SIGN;
                        pfcDgain        = KD_PFC_SIGN;
                        pfcFastPgain    = KP_PFC_FAST_SIGN;
                        pfcFastIgain    = KI_PFC_FAST_SIGN;
    
                        ledPgain    = KP_SIGN;
                        ledIgain    = KI_SIGN;
                        ledDgain    = KD_SIGN;
                        ledFFgain   = KFF_SIGN;
    
                        ledCurrentSetVal        = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet   = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        ledOpenVoltage = _IQ15(VMAX_HIRL_2_852G_S_SIGN);
    
                        f1 = _IQtoF(ledOpenVoltage);
    
                        memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
                        sprintf((char *)serialTxBytes, "------- ledOpenVoltage: %.2f \r\n", f1);
                        sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    
                        erglFlag = 0;
                        guardFlag = 1;
                        blinkDisable = 1;
    
                        guardSyncOddEven = 1;
                        syncDisableFlag     = 0;
    
                        ledRestartMax = BOOST_RESTART_MAX;
    
                        adName[0] = 'L';
                        adName[1] = '8';
                        adName[2] = '5';
                        adName[3] = '2';
                        adName[4] = 'G';
                        adName[5] = ' ';
                        adName[6] = ' ';
                        adName[7] = ' ';
                        adName[8] = ' ';
                        adName[9] = ' ';
                        adName[10] = ' ';
                        adName[11] = ' ';
    
    /*                  if(guardFlag == 1)
                        {
                            EALLOW;
    
                            syncDriveConfig(7, HEATER_PERIOD);              // Set up PWM to sync.
                            GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;            // Sync pwm output
    
    #if HARDWARE_21696
                            GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBDIR.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;
    #else
                            GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;
    #endif
                            EDIS;
                        }
    */
                        break;
    
                    case NO_JUMPERS:    // L852G/S Calibration mode          Jumper Diagram 1
    
                        pfcSetRef       = _IQ12mpy(PFC_SET_HIRL_852G_S_SIGN, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                        pfcOVH      = PFC_OVH_HIRL_852G_S_SIGN;
                        pfcOVL      = PFC_OVL_HIRL_852G_S_SIGN;
                        pfcUVHRun   = PFC_UVH_HIRL_852G_S_SIGN;
                        pfcUVLRun   = PFC_UVL_HIRL_852G_S_SIGN;
    
                        pfcUVH = PFC_UVH_STARTUP_852G_S_SIGN;
                        pfcUVL = PFC_UVL_STARTUP_852G_S_SIGN;
    
                        // PFC controller coefficients.
                        pfcPgain        = KP_PFC_SIGN;
                        pfcIgain        = KI_PFC_SIGN;
                        pfcDgain        = KD_PFC_SIGN;
                        pfcFastPgain    = KP_PFC_FAST_SIGN;
                        pfcFastIgain    = KI_PFC_FAST_SIGN;
    
                        ledPgain    = KP_SIGN;
                        ledIgain    = KI_SIGN;
                        ledDgain    = KD_SIGN;
                        ledFFgain   = KFF_SIGN;
    
                        ledCurrentSetVal        = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet   = ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        ledOpenVoltage = _IQ15(VMAX_HIRL_2_852G_S_SIGN);
    
                        erglFlag = 0;
                        guardFlag = 1;
                        blinkDisable = 0;
    
                        guardSyncOddEven = 1;
                        syncDisableFlag     = 0;
    
                        ledRestartMax = BOOST_RESTART_MAX;
    
                        ledShortVoltage = 0;
                        ledCalTimer = 0;
                        runLedCal = 1;
    
                        oddEvenConfigValid  = 0xFF;
                        oddEvenConfig       = 0xFF;
                        phaseConfigValid    = 0xFF;
                        phaseConfig         = 0xFF;
                        dimRampConfigValid  = 0xFF;
                        dimRampConfig       = 0xFF;
                        ledCalValid         = 0xFF;
                        ledShortConfig      = _IQ15(0.0);
                        hardwareConfigValid = 0xFF;
                        hardwareConfig      = 0xFF;
    
                        processEEPROMwrite(CONFIGDATA0, eepromData);
                        i2cStatus = transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);
    
                        if(i2cStatus == I2C_ERROR)
                        {
                           i2cErrorCount++;
                        }
    
                        adName[0] = 'L';
                        adName[1] = '8';
                        adName[2] = '5';
                        adName[3] = '2';
                        adName[4] = 'G';
                        adName[5] = ' ';
                        adName[6] = ' ';
                        adName[7] = ' ';
                        adName[8] = ' ';
                        adName[9] = ' ';
                        adName[10] = ' ';
                        adName[11] = ' ';
    
    /*                  if(guardFlag == 1)
                        {
                            EALLOW;
    
                            syncDriveConfig(7, HEATER_PERIOD);              // Set up PWM to sync.
                            GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;            // Sync pwm output
    
    #if HARDWARE_21696
                            GpioCtrlRegs.GPBMUX1.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBDIR.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBPUD.bit.GPIO41 = 0;
                            GpioCtrlRegs.GPBQSEL1.bit.GPIO41 = 3;
    #else
                            GpioCtrlRegs.GPAMUX2.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPADIR.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPAPUD.bit.GPIO31 = 0;
                            GpioCtrlRegs.GPAQSEL2.bit.GPIO31 = 3;
    #endif
                            EDIS;
                        }
    */
                        break;
    
                    default:        // 350 mA
    
                        ledCurrentSetVal        = ((float)((0.350 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                        ledOverCurrentSet   = ((unsigned int)(0.650 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
                        break;
    
    			} // END switch(configJumpers) (HIRL fixture configuration)
    
                // Read configuration settings from EEPROM for initial setup.
                i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, CONFIGDATA0, eepromData);   // Read EEPROM data.
    
                if(i2cStatus == I2C_ERROR)                      // Check for errors and timeout to prevent getting
                {                                               // stuck on a bus hang-up.
                   i2cErrorCount++;
                }
    
                if(eepromData[0] != EEPROM_DATA_VALID)          // Check if the data just read is valid or an erased EEPROM.
                {
                    i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK1, CONFIGDATA0, eepromData);   // Attempt to read EEPROM from secondary config block.
    
                    if(i2cStatus == I2C_ERROR)
                    {
                       i2cErrorCount++;
                    }
    
                    if(eepromData[0] != EEPROM_DATA_VALID)
                    {
                        initEEPROMpage(CONFIGDATA0);                // If secondary block was invalid, set config data to factory values.
                    }
                    else
                    {
                        processEEPROMread(CONFIGDATA0, eepromData); // If secondary block was valid, read it in as config data.
                    }
    
                }
                else
                {
                    processEEPROMread(CONFIGDATA0, eepromData);     // If primary block was valid, read it in as config data.
                }
    
    
                if(oddEvenConfigValid == EEPROM_DATA_VALID)
                {
                    if(oddEvenConfig == ODD)                // If blink config is saved in EEPROM, it overrides the jumper setting.
                    {
                        blinkState = 0;
                        guardSyncOddEven = 0;
                    }
                    else if (oddEvenConfig == EVEN)
                    {
                        blinkState = 1;
                        guardSyncOddEven = 1;
                    }
                }
                else
                {
                    oddEvenConfig = blinkState|0xF0;
                }
    
                if(phaseConfigValid == EEPROM_DATA_VALID)
                {
                    if(phaseConfig == DEFAULT_PHASE)        // If phase config is saved in EEPROM, use alternate phase.
                    {
                        blinkTimer = 0;
                    }
                    else if(phaseConfig == ALTERNATE_PHASE)
                    {
                        blinkTimer = 116;
                    }
                }
                else
                {
                    phaseConfig = 2;
                }
    
    		}
    		else
    		{
    
                memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
                sprintf((char *)serialTxBytes, "Buck Non-HIRL \r\n");
                sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    
    		// PFC controller set point and protection limits.
    		pfcSetRef		= _IQ12mpy(PFC_SET_TEL_INSET, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
    		pfcOVH 		= PFC_OVH_TEL_INSET;
    	  	pfcOVL 		= PFC_OVL_TEL_INSET;
    		pfcUVHRun	= PFC_UVH_TEL_INSET;
    		pfcUVLRun	= PFC_UVL_TEL_INSET;
    
    		pfcUVH = PFC_UVH_STARTUP;
    	  	pfcUVL = PFC_UVL_STARTUP;
    
    	  	// PFC controller coefficients.
    		pfcPgain		= KP_PFC;
    	  	pfcIgain		= KI_PFC;
    	  	pfcDgain		= KD_PFC;
    	  	pfcFastPgain	= KP_PFC_FAST;
    	  	pfcFastIgain	= KI_PFC_FAST;
    
    	  	CNTL_2P2Z_CLA_Fdbk1 = &ledCurrentAdc;	// LED feedback current.
    
    		// LED controller coefficients.
    	  	ledPgain	= KP_LED;
    	  	ledIgain	= KI_LED;
    	  	ledDgain	= KD_LED;
    
    	  	ledFFgain = KFF_LED;
    	  	Kff = &ledFFgain;		// LED feed-forward variables.
    		vFFvMin = VFF_MIN32;
    
    		ledOpenVoltage = _IQ15(VMAX_INSET);
    
    		syncDisableFlag = 2;	// Enable dimming and auto sync of LED.
    		dimDisable = 0;
    
    		blinkDisable = 1;		// Disable blinking for these models
    //      blinkDisable = 0;       // Enabled blinking for S-609 (Special)
    //      blinkTimer = 0;         // Enabled blinking for S-609 (Special)
    
    		ledFastOVDisable = 1;
    
    		ledMinDuty = WHITE_DIM_MIN;
    
    		ledFastOV		=  (unsigned int)(28.0 * LED_VINPUT_GAIN * ADC_GAIN);
    
    		if(GpioDataRegs.GPBDAT.bit.GPIO41 == 0)
    		{
    			failOpenFlag = 0;
    		}
    		switch(configJumpers)	// Fixture specific set point and protection limits set by jumpers.
    		{
    
    			case TEST_MODE:
    
    				ledCurrentSetVal		= ((float)((0.900 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.5 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    			    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL	= DC_COMP3OUT;	// LED Buck.
    			    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL	= DC_COMP3OUT;	// Dimmer.
    
    			    dimDisable = 1;
    			    dimOverride = 37501;
    
    			    test_timer = TEST_CYCLE_TIME;
    
    				break;
    
    			case I925MA:
    
    				ledCurrentSetVal		= ((float)((0.925 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.5 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    			case I875MA:
    
    				ledCurrentSetVal		= ((float)((0.875 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.5 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    			case I525MA:
    
    				ledCurrentSetVal		= ((float)((0.525 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.800 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    /*			case I475MA:
    
    				ledCurrentSetVal		= ((float)((0.475 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.0 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    */
    			case I425MA:
    
    				ledCurrentSetVal		= ((float)((0.425 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    /*			case I100MA:
    
    				ledCurrentSetVal		= ((float)((0.100 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    */
    			case I950MA:
    
    				ledCurrentSetVal		= ((float)((0.950 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.500 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    			    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL	= DC_COMP3OUT;	// LED Buck.
    			    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL	= DC_COMP3OUT;	// Dimmer.
    				break;
    
    /*			case I200MA:
    
    				ledCurrentSetVal		= ((float)((0.200 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.550 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    */
    /*			case I300MA:
    
    				ledCurrentSetVal		= ((float)((0.300 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.600 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    */
    /*			case I750MA:
    
    				ledCurrentSetVal		= ((float)((0.750 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.200 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    */
    			case I900MA:
    
    				ledCurrentSetVal		= ((float)((0.900 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.500 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				heatOnTemp		=	12.4;
    				heatOffTemp		=	34.0;
    				break;
    
    			case I800MA:
    
    				ledCurrentSetVal		= ((float)((0.800 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.300 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    			case I400MA:
    
    				ledCurrentSetVal		= ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    			case I550MA:
    
    			    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    			    ledOpenVoltage  = _IQ15(25.0);
    
    				ledCurrentSetVal		= ((float)((0.558 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.850 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    			case I350MA_HTR:
    
    				ledCurrentSetVal		= ((float)((0.350 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				heatOnTemp		=	12.4;
    				heatOffTemp		=	34.0;
    				break;
    
    			case I600MA:
    
    				ledCurrentSetVal		= ((float)((0.600 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.930 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    /*			case I450MA_HTR:
    
    				ledCurrentSetVal		= ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				heatOnTemp		=	12.4;
    				heatOffTemp		=	25.0;
    				break;
    */
    			case I400MA_HTR:
    
    				ledCurrentSetVal		= ((float)((0.400 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				heatOnTemp		=	12.4;
    				heatOffTemp		=	34.0;
    				break;
    
    			case I500MA:
    
    				ledCurrentSetVal		= ((float)((0.500 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.800 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				heatOnTemp		=	12.4;
    				heatOffTemp		=	34.0;
    				break;
    
    			case I1050MA:
    
    				ledCurrentSetVal		= ((float)((1.050 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.50 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    			    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL	= DC_COMP3OUT;	// LED Buck.
    			    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL	= DC_COMP3OUT;	// Dimmer.
    				break;
    
    			case I700MA:
    
    			    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    			    ledOpenVoltage  = _IQ15(25.0);
    
    				ledCurrentSetVal		= ((float)((0.607 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.000 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				heatOnTemp		=	12.4;
    				heatOffTemp		=	34.0;
    				break;
    
    			case I225MA:
    
    				ledCurrentSetVal		= ((float)((0.225 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.525 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    			case I060MA:
    
    				ledCurrentSetVal		= ((float)((0.060 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.150 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    /*			case I1000MA:
    
    				ledCurrentSetVal		= ((float)((1.000 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.50 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    			    EPwm6Regs.DCTRIPSEL.bit.DCBHCOMPSEL	= DC_COMP3OUT;	// LED Buck.
    			    EPwm5Regs.DCTRIPSEL.bit.DCAHCOMPSEL	= DC_COMP3OUT;	// Dimmer.
    				break;
    */
    			case I150MA:
    
    				ledCurrentSetVal		= ((float)((0.150 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.450 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    			case I250MA:
    
    				ledCurrentSetVal		= ((float)((0.250 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.550 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    			case HI700MA_R_FAA:
    
    			    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    			    ledOpenVoltage = _IQ15(25.00);
    
    				ledCurrentSetVal		= ((float)((0.638 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.930 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    /*			case I085MA:
    
    				ledCurrentSetVal		= ((float)((0.085 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.150 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    */
    			case I850MA:
    
    			    pfcSetRef       = _IQ12mpy(PFC_SET_SIGN_2, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
    				ledCurrentSetVal		= ((float)((0.850 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(1.20 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    
    /*			case I450MA:
    
    				ledCurrentSetVal		= ((float)((0.450 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.750 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    				break;
    */
                case HI700MA_G_FAA:
    
                    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                    ledCurrentSetVal        = ((float)((0.708 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledOpenVoltage      = _IQ15(25.0);
    
                    break;
    
                case HI630MA_R_FAA:
    
                    GpioDataRegs.GPBCLEAR.bit.GPIO34 = 1;
    
                    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                    ledCurrentSetVal        = ((float)((0.304 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledOpenVoltage      = _IQ15(25.0);
    
                    break;
    
                case HI700MA_GR_FAA:
    
                    ledCurrentSetVal        = ((float)((0.700 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledOpenVoltage      = _IQ15(25.0);
    
                    break;
    
                case HI630MA_RR_FAA:
    
                    ledCurrentSetVal        = ((float)((0.630 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledOpenVoltage      = _IQ15(25.0);
    
                    break;
                case HI700MA_L862_GR:
    
                    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                    ledCurrentSetVal        = ((float)((0.708 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledOpenVoltage      = _IQ15(25.0);
    
                    break;
    
                case HI630MA_L862_RR:
    
                    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                    ledCurrentSetVal        = ((float)((0.638 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledOpenVoltage      = _IQ15(25.0);
    
                    break;
    
                case HI450MA_L850C_G:
    
                    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                    ledCurrentSetVal        = ((float)((0.458 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledOpenVoltage      = _IQ15(25.0);
    
                    break;
    /*            case HI450MA_L850D_G:                     //   HI450MA_L850C_G and HI450MA_L850D_G are the same
    
                    pfcSetRef       = _IQ12mpy(PFC_SET_30, _IQ12(PFC_VINPUT_GAIN * ADC_GAIN));
    
                    ledCurrentSetVal        = ((float)((0.458 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
                    ledOverCurrentSet   = ((unsigned int)(1.050 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    
                    ledOpenVoltage      = _IQ15(25.0);
    
    
                    break;
    */
    			default: //I350MA_1LED
    
    				ledCurrentSetVal		= ((float)((0.350 * LED_DC_CS_RESISTOR * ADC_GAIN * LED_DC_CS_GAIN)/4096.0));
    				ledOverCurrentSet	= ((unsigned int)(0.650 * LED_DC_CS_RESISTOR * (1024/3.3) * LED_DC_CS_GAIN));
    ;
    			break;
    
    		} // END switch(configJumpers) (Inset fixture configuration)
    		} // END hirl settings
    	}	// END else
    
    	//====================================================================================
    	// ASSEMBLY VARIABLE LINKS
    	//====================================================================================
    	// The following variables are shared between the assembly and C environment. Pointer links assign
    	// TIs DpLib functions to "net" variables used for various control algorithms.
    
    	// PFC controller assignment (28x core)
    	CNTL_2P2Z_Ref1 = &pfcVoltageRef;				// PFC controller reference input.
    	CNTL_2P2Z_Fdbk1 = &pfcVoltageAdc;				// PFC feedback voltage.
    	CNTL_2P2Z_Coef1 = &CNTL_2P2Z_CoefStruct1.vcntl;	// Pointer to the PFC control coefficients.
    	CNTL_2P2Z_Out1 = &pfcControlOut;				// PFC controller output
    
    	PWMDRV_1chHiRes_Duty1 = &pfcDuty;				// PFC PWM driver.
    
    	pfcSenseGain	= PFC_SENSE_GAIN;				// Circuit gains for sensing PFC voltage.
    
    	syncCurrentH = SYNC_THRESHOLD_REF_H;		// Sets current threshold used to detect AC input edges.
    	syncCurrentL = SYNC_THRESHOLD_REF_L;
    	// PFC controller equation is reduced to PID by converting coefficients.
    	CNTL_2P2Z_CoefStruct1.vcntl = PFC_CNTL_REF;
    	CNTL_2P2Z_CoefStruct1.b2 = _IQ6(pfcDgain);
    	CNTL_2P2Z_CoefStruct1.b1 = _IQ6(pfcIgain - pfcPgain - pfcDgain - pfcDgain);
    	CNTL_2P2Z_CoefStruct1.b0 = _IQ6(pfcPgain + pfcIgain + pfcDgain);
    	CNTL_2P2Z_CoefStruct1.b1_fast = _IQ6(pfcFastIgain - pfcFastPgain - pfcDgain - pfcDgain);
    	CNTL_2P2Z_CoefStruct1.b0_fast = _IQ6(pfcFastPgain + pfcFastIgain + pfcDgain);
    	CNTL_2P2Z_CoefStruct1.a2 = _IQ26(0.0);
    	CNTL_2P2Z_CoefStruct1.a1 = _IQ26(1.0);
    	CNTL_2P2Z_CoefStruct1.max = 0x10000000;//0x02000000;
    	CNTL_2P2Z_CoefStruct1.min = _IQ24(0.0);
    
    	// LED controller assignment (CLA core)
    	ADCDRV_1ch_CLA_Rlt1 = &ledCurrentAdc;						// LED current ADC read driver.
    	ADCDRV_1ch_CLA_Rlt6 = &vOutAdc;								// LED anode voltage (used to regulate in voltage mode devices).
    	CNTL_2P2Z_CLA_Ref1 = &ledCurrentRef;						// LED set current.
    	CNTL_2P2Z_CLA_Coef1 = &CNTL_2P2Z_CLACoefStruct_LED.b2;		// Pointer to the LED control coefficients.
    	CNTL_2P2Z_CLA_Out1 = &ledDuty;								// LED controller output
    
    	PWMDRV_1chHiRes_CLA_Duty6 = &ledDuty;						// LED PWM driver.
    
    	// LED controller equation is reduced to PID by converting coefficients.
    	CNTL_2P2Z_CLACoefStruct_LED.b2 = (ledDgain);
    	CNTL_2P2Z_CLACoefStruct_LED.b1 = (ledIgain - ledPgain - ledDgain - ledDgain);
    	CNTL_2P2Z_CLACoefStruct_LED.b0 = (ledPgain + ledIgain + ledDgain);
    	CNTL_2P2Z_CLACoefStruct_LED.a2 = (float)0.0;
    	CNTL_2P2Z_CLACoefStruct_LED.a1 = (float)1.0;
    	CNTL_2P2Z_CLACoefStruct_LED.max =(float)0.945;
    	CNTL_2P2Z_CLACoefStruct_LED.min =(float)0.05;
    
    	ledCurrentSet = 0;
    	Kff	= &ledFFgain;	// LED voltage feedforward pointer.
    
    		// Enable shunt or hall ic current monitoring to support legacy hardware (see settings file).
    #if	SHUNT_IC
    	lineCurrentGain = LINE_CURRENT_GAIN_SHUNT;
    	lineCurrentOffset = LINE_CURRENT_OFFSET_SHUNT;
    	inputCurrentOffset = LINE_CURRENT_OFFSET_SHUNT_FP;
    	pfcOCH = (int)(PFC_OC_H * SHUNT_IC_GAIN * ADC_GAIN * NUM_CURRENT_SAMPLES);
    	pfcOCL = (int)(PFC_OC_L * SHUNT_IC_GAIN * ADC_GAIN * NUM_CURRENT_SAMPLES);
    #else
    	lineCurrentGain = LINE_CURRENT_GAIN_HALL;
    	lineCurrentOffset = LINE_CURRENT_OFFSET_HALL;
    	inputCurrentOffset = LINE_CURRENT_OFFSET_HALL_FP;
    	pfcOCH = (int)(PFC_OC_H * HALL_IC_GAIN * LINE_CURRENT_DIVIDER * ADC_GAIN * NUM_CURRENT_SAMPLES);
    	pfcOCL = (int)(PFC_OC_L * HALL_IC_GAIN * LINE_CURRENT_DIVIDER * ADC_GAIN * NUM_CURRENT_SAMPLES);
    #endif
    	//	lineSenseGain = LINE_SENSE_GAIN;
    
    	init_cla();								// Initialize CLA core and setup CLA memory.
    
    	Comp1Regs.DACVAL.bit.DACVAL		= ledOverCurrentSet;		// Set DAC for LED 1 DC over-current trip point for <950mA versions.
    	Comp3Regs.DACVAL.bit.DACVAL		= ledOverCurrentSet;		// Set DAC for LED 1 DC over-current trip point for =>950mA versions.
    
    	EALLOW;
    	EPwm5Regs.TZCLR.all = 0x007F;			// Clear errors caused by 0A current limit.
    	EPwm6Regs.TZCLR.all = 0x007F;
    	EPwm4Regs.ETCLR.bit.INT	= 1;			// Clear any interrupts that may have occurred
    	PieCtrlRegs.PIEACK.all = 0x0FFF;		// at startup.
    	EDIS;
    
    	EINT;									// Enable global interrupts.
    	ERTM;
    
    	EALLOW;
    //	GpioCtrlRegs.GPAMUX1.bit.GPIO0 	= 1;		// Enable PFC PWM pins (left until now to prevent startup glitches
    	GpioCtrlRegs.GPAMUX1.bit.GPIO4 	= 1;		// while PWMs are  not configured).
    	GpioCtrlRegs.GPAMUX1.bit.GPIO8 	= 1;
    	GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 1;
    	GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 1;
        GpioCtrlRegs.GPBMUX1.bit.GPIO40 = 1;
    	EDIS;
    
    //-------------------------------------------------------------------------
    //	Background Infinite Loop
    //-------------------------------------------------------------------------
    
    	EALLOW;
    	SysCtrlRegs.WDCR = 0x002F;					// Activate watchdog.
    	EDIS;
    
    	for(;;)  // Infinite background loop.
    	{
            EALLOW;
    		SysCtrlRegs.WDKEY = 0x0055;				// Manage watchdog timer.
    		SysCtrlRegs.WDKEY = 0x00AA;
            EDIS;
    
    		MONITOR1_TOGGLE = 1;					// Toggle monitoring signal. This signal and
    												// another toggled in the interrupt routine give
    												// an indication that the processor is running and
    												// no errors are detected.
    
    //-------------------------------------------------------------------------
    //	Background State Machine
    //-------------------------------------------------------------------------
    
    		switch(currentState)
    		{
    
    		//-------------------------------------------------------------------------
    			case PFC_BOOT:	// STATE = "0"
    		//-------------------------------------------------------------------------
    				if(previousState != PFC_BOOT)
    				{
    					ledCurrentSet = 0;
    
    					runtimeErrorsEnable = 0;
    
    					pfcUVH = PFC_UVH_STARTUP;
    				  	pfcUVL = PFC_UVL_STARTUP;
    
    #if	HARDWARE_21696
    				  	EALLOW;
    					GpioCtrlRegs.GPAMUX1.bit.GPIO0 	= 0;
    					EDIS;
    #endif
    
    					GpioDataRegs.GPACLEAR.bit.GPIO25 = 1;
    
    					pfcVoltageRef = PFC_START_REF;
    
    					PCB_LED_OFF = 1;
    
    					previousState = PFC_BOOT;
    				}
    
    				if(pfcVoltage > PFC_STARTUP_LEVEL)		// When input current reaches correct level, begin startup.
    				{
    
    					EALLOW;
    					GpioCtrlRegs.GPAMUX1.bit.GPIO0 	= 1;
    					EDIS;
    
    					GpioDataRegs.GPASET.bit.GPIO25 = 1;
    
    					PCB_LED_OFF = 1;
    
    					currentState = CURRENT_LOW;
    				}
    
    				break;
    //-------------------------------------------------------------------------
    			case CURRENT_LOW:	// STATE = "0"
    //-------------------------------------------------------------------------
    
    				// State entry
    				if(previousState != CURRENT_LOW)
    				{
    					ledCurrentSet = 0;
    
    					runtimeErrorsEnable = 0;
    
    					pfcUVH = PFC_UVH_STARTUP;
    				  	pfcUVL = PFC_UVL_STARTUP;
    
    					pfcVoltageRef = PFC_START_REF;
    
    					PCB_LED_OFF = 1;
    
    					previousState = CURRENT_LOW;
    				}
    
    				// State exit
    				if(currentFastIQ > _IQ26(2.0))		// When input current reaches correct level, begin startup.
    				{
    					currentState = INPUT_RAMP;
    				}
    
    				break;
    
    //-------------------------------------------------------------------------
    			case INPUT_RAMP:	// STATE = "1"
    //-------------------------------------------------------------------------
    
    				// State entry
    				if(previousState != INPUT_RAMP)
    				{
    					previousState = INPUT_RAMP;
    				}
    
    				// State operations
    				pfcVoltageRef = pfcVoltageRef + 0x00008000;		// Ramp PFC voltage reference to produce soft-start.
    
    
    				// State exit
    				if(pfcVoltageRef >= pfcSetRef)					// If PFC voltage has reached its operating setpoint,
    				{												// move to the next state.
    					pfcVoltageRef = pfcSetRef;
    					currentState = LED_SOFT_START;
    				}
    
    				break;
    
    //-------------------------------------------------------------------------
    			case LED_SOFT_START:	// STATE = "2"
    //-------------------------------------------------------------------------
    				// State entry
    				if(previousState != LED_SOFT_START)
    				{
    					runtimeErrorsEnable = 0;
    					ledCurrentSet = 0;
    					ledCurrentRampVal = 0.00002;
    					errorDelay = 0;
    					previousState = LED_SOFT_START;
    
    					PCB_LED_OFF = 1;
    
    					warmUpDelayTimer = 0;
    					warmUpDelayComplete = 0;
    					startWarmUpDelay = 0;
    				}
    
    				if(ledRestart == 1)
    				{
    					ledCurrentRampVal = ledRestartVal;
    
    					ledRestartCounter++;
    
    					if(ledRestartCounter > LED_RESTART_WAIT)
    					{
    						EALLOW;
    						EPwm6Regs.TZCLR.all = 0x2C;
    						EDIS;
    
    						ledRestartCounter = 0;
    						ledRestart = 0;
    					}
    				}
    				else
    				{
    				    if (boostJumper == 0 && configJumpers == HI460MA_210_WW)    // For FAA HIRL WW, delay startup in order to give it time to warm up
    				    {
    				        if (warmUpDelayComplete == 0)
    				        {
    				            ledCurrentSet = ledCurrentSetValHalf;		        // Set current to half the regular value
    				        }
    
                            if(ledCurrentRef == ledCurrentSet)
                            {
                                startWarmUpDelay = 1;
                            }
    
                            if (startWarmUpDelay == 1 && warmUpDelayComplete == 0)
                            {
                                warmUpDelayTimer++;                         // Delay 10 seconds
    
                                if (warmUpDelayTimer >= 1000)
                                {
                                    ledCurrentSet = ledCurrentSetVal;       // Set current to the regular value after the delay
                                    warmUpDelayComplete = 1;
                                }
                            }
    
                            // State exit
                            if(ledCurrentRef == ledCurrentSet && warmUpDelayComplete == 1)
                            {                                               // After the delay, go to RUN
                                currentState = RUN;
                            }
    				    }
    				    else
    				    {
    	                    ledCurrentSet = ledCurrentSetVal;               // Ramp LED current reference to produce soft-start.
    
    	                    // State exit
    	                    if(ledCurrentRef == ledCurrentSet)              // If LED current has reached its operating setpoint,
    	                    {                                               // move to the next state.
    	                        currentState = RUN;
    	                    }
    				    }
    				}
    
    				break;
    
    //-------------------------------------------------------------------------
    			case RUN:				//	STATE = "3"
    //-------------------------------------------------------------------------
    
    //	            if (ledShortVoltage > _IQ15(63.00) && ledShortVoltage < _IQ15(66.00))
    //	            {
    //	                GpioDataRegs.GPBSET.bit.GPIO34 = 1;
    //	            }
    
    				if(previousState != RUN)
    				{
    					pfcUVH = pfcUVHRun;
    					pfcUVL = pfcUVLRun;							// Set UV points now that voltage is up.
    
    					ledCurrentRampVal = 0.00002;
    
    					errorDelay++;
    
    					if(errorDelay >= 500)							// Delay going into RUN to allow time for low power LEDs to ramp up.
    					{
    						runtimeErrorsEnable = 1;
    						errorDelay = 0;
    
    					  	previousState = RUN;
    						PCB_LED_ON = 1;								// Turn pcb indicator LED.
    					}
    
    				}
    
    				if(runtimeErrorsEnable == 1)
    				{
    					runtimeErrorChecking();						// Check for various run-time errors.
    				}
    
    //				if((boostJumper == 1) && (signJumper == 1))
    /*			    if(signJumper == 1)     // SAMPLE 1
    				{
    					if((runLedCal == 1) && (EPwm5Regs.CMPA.half.CMPA >= 37450))
    					{
    						if (ledShortVoltage > _IQ15(50) && ledShortVoltage < _IQ15(55))
    						{
    						    GpioDataRegs.GPBCLEAR.bit.GPIO34 = 1;   // uncomment if --> Set Low initially
    						}
    
    						runLedCal = 0;
    					}
    */
    
                        if((ledCalValid != EEPROM_DATA_VALID) && (bluetoothMode != CONFIG) && (runLedCal == 1))// && (EPwm5Regs.CMPA.half.CMPA >= 37450))
                        {
                            ledCalTimer++;
    
                            if((ledCalTimer >= ledAutoCalTime) && (i2cTimer < I2C_AUTO_START))
                            {
                                if(boostJumper == 0)
                                {
                                    if (configJumpers == HI460MA_210_WW)                    // WW ICAO - boost
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));
                                    }
                                    else if (configJumpers == HI460MA_210_WY)               // WY ICAO - boost
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));
                                    }
                                    else if (configJumpers == HI440MA_WW_FAA)               // WW FAA - boost
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.925));
                                    }
                                    else if (configJumpers == HI440MA_WY_FAA)               // WY FAA - boost
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.955));
                                    }
                                    else if (configJumpers == HI450MA_WR_FAA)               // WR FAA - boost
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.959));
                                    }
                                    else if (configJumpers == HI470MA_WG_FAA)               // WG FAA - boost
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.961));
                                    }
                                    else if ( configJumpers == HI500mA_BST)                 // IREL WW 850CD
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));
                                    }
                                    else
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));
                                    }
                                }
                                else if (hirlJumper == 0)
                                {
                                    if (configJumpers == HI450MA_YR_FAA)                    // YR FAA - H-buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.974));
                                    }
                                    else if (configJumpers == HI475MA_YG_FAA)               // YG FAA - H-buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.972));
                                    }
                                    else if (configJumpers == I630MA)                       // GR ICAO - H-buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.667));
                                    }
                                    else
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));
                                    }
                                }
                                else if (hirlJumper == 1 && boostJumper == 1)
                                {
                                    if (configJumpers == HI700MA_G_FAA)                     // G FAA - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));
                                    }
                                    else if (configJumpers == HI630MA_R_FAA)                // R FAA - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));
                                    }
                                    else if (configJumpers == HI700MA_GR_FAA)               // GR FAA - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.941));
                                    }
                                    else if (configJumpers == HI630MA_RR_FAA)               // RR FAA - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.926));
                                    }
                                    else if (configJumpers == I700MA)                       // L850C-Y - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));
                                    }
                                    else if (configJumpers == I550MA)                       // L850D-R - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));
                                    }
                                    else if (configJumpers == HI700MA_R_FAA)                // L862 R - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.833));
                                    }
                                    else if (configJumpers == HI700MA_L862_GR)              // L862 GR - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.941));
                                    }
                                    else if (configJumpers == HI630MA_L862_RR)              // L862 RR - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.926));
                                    }
                                    else if (configJumpers == HI450MA_L850C_G)              // L850C G - buck
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));
                                    }
    //                                else if (configJumpers == HI450MA_L850D_G)              // L850D G - buck           HI450MA_L850C_G and HI450MA_L850D_G are the same
    //                                {
    //                                    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.838));
    //                                }
                                    else
                                    {
                                        ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));
                                    }
                                }
                                else
                                {
                                    ledShortVoltage = _IQ15mpy(ledVoltage, _IQ15(0.75));
                                }
    
                                runLedCal = 0;
    
                                ledCalValid		= EEPROM_DATA_VALID;
                                ledShortConfig	= ledShortVoltage;
    
                                processEEPROMwrite(CONFIGDATA0, eepromData);
                                transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);
    
                                if(i2cStatus == I2C_ERROR)
                                {
                                   i2cErrorCount++;
                                }
    
                                ledCalTimer = 0;
                                ledCalFinished = 1;
                                timerOn = 0;
                                ledOnTimer = 0;
                            }
                        }
    
                        if(ledCalFinished == 1)
                        {
                            if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)
                            {
                                if (timerOn == 0)
                                {
                                    if (blinkState == 1)
                                    {
                                        if (blinkTimer >= 26 && blinkTimer <= 32)
                                        {
                                            blinkDisable = 1;
                                            timerOn = 1;
                                        }
                                    }
                                }
                                else
                                {
                                    if (ledOnTimer < 500)
                                    {
                                        ledOnTimer++;
                                    }
                                    else
                                    {
                                        if (blinkState == 1)
                                        {
                                           if (blinkTimer >= 26 && blinkTimer <= 32)
                                           {
                                               blinkDisable = 0;
                                               timerOn = 0;
                                               ledCalFinished = 0;
                                           }
                                        }
                                    }
                                }
                            }
                            else
                            {
                                if(ledCalNotifyTimer < ledCalNotifyTime)
                                {
                                    ledCalNotifyTimer++;
                                    ledManualBlinkTimer++;
    
                                    if(ledManualBlinkTimer >= 100)
                                    {
                                        if(dimDisable == 0)
                                        {
                                            dimDisable = 1;
                                        }
                                        else
                                        {
                                            dimDisable = 0;
                                        }
    
                                        ledManualBlinkTimer = 0;
                                    }
                                }
                                else
                                {
                                    dimDisable = 0;
                                    ledManualBlinkTimer = 0;
                                    ledCalNotifyTimer = 0;
                                    ledCalFinished = 0;
                                }
                            }
                        }
    
    
    				// ERGL functions.
    				switch(dimRampVal)			// Dimming ramp timing (from off to full) determines ERGL timing.
    				   {
    				   	   case 1:
    				   		   erglLimit = 46;
    				   		   break;
    				   	   case 2:
    				   		   erglLimit = 46;
    				   		   break;
    				   	   case 3:
    				   		   erglLimit = 52;
    				   		   break;
    				   	   case 4:
    				   		   erglLimit = 54;
    				   		   break;
    				   	   case 5:
    				   		   erglLimit = 57;
    				   		   break;
    				   }
    
    		//				   erglLimit = 46;
    
    				   if((erglFlag == 1)&&(connect == 0))
    				   {
    
    						if(erglCounter == erglLimit)
    						{
    
    							ledCurrentSet = 0.0;
    
    							dimDisable = 1;
    
    						}
    
    						if (erglCounter >= 64)
    						{
    
    							erglState = erglNextState;
    
    							ledCurrentSet = ledCurrentSetVal;
    
    
    							if((erglState == ERGL_LEFT_ON) || (erglState == ERGL_RIGHT_ON))
    							{
    								dimDisable = 0;
    							}
    
    
    							erglCounter = 0;
    						}
    
    						erglCounter++;
    
    						switch(erglState)
    						{
    							case ERGL_LEFT_ON:
    
    								ERGL_GPIO_LEFT_ON = 1;
    								ERGL_GPIO_RIGHT_OFF = 1;
    
    
    								if(erglRightFault == 1)
    								{
    									erglNextState = ERGL_RIGHT_FAULT;
    								}
    								else
    								{
    									erglNextState = ERGL_RIGHT_ON;
    								}
    
    								break;
    
    							case ERGL_RIGHT_ON:
    
    								ERGL_GPIO_RIGHT_ON = 1;
    								ERGL_GPIO_LEFT_OFF = 1;
    
    								if(erglLeftFault == 1)
    								{
    									erglNextState = ERGL_LEFT_FAULT;
    								}
    								else
    								{
    									erglNextState = ERGL_LEFT_ON;
    								}
    
    								break;
    
    
    
    							case ERGL_LEFT_FAULT:
    
    								ERGL_GPIO_LEFT_OFF = 1;
    								ERGL_GPIO_RIGHT_OFF = 1;
    
    								erglNextState = ERGL_RIGHT_ON;
    
    								break;
    
    
    							case ERGL_RIGHT_FAULT:
    
    								ERGL_GPIO_RIGHT_OFF = 1;
    								ERGL_GPIO_LEFT_OFF = 1;
    
    								erglNextState = ERGL_LEFT_ON;
    
    								break;
    
    						} // End switch(erglState)
    
    						if((erglCounter >= (erglLimit-15)) && (erglCounter <= erglLimit))
    						{
    							switch(erglState)
    							{
    								case ERGL_LEFT_ON:
    
    									if(ledVoltage > ledOpenVoltage)	// Check for LED open or shorted.
    									{
    										erglLeftOVErrorCount++;
    									}
    									else if(erglLeftOVErrorCount > 0)
    									{
    										erglLeftOVErrorCount--;
    
    									}
    
    									if(runtimeErrorsEnable && (ledVoltage < ledShortVoltage))	// Check for LED open or shorted.
    									{
    										erglLeftUVErrorCount++;
    									}
    									else if(erglLeftUVErrorCount > 0)
    									{
    										erglLeftUVErrorCount--;
    
    									}
    
    									if((erglLeftOVErrorCount > ledMaxOVErrors)	|| (erglLeftUVErrorCount > LED_MAX_UV_ERRORS))				// If errors are persistent, trigger a fault.
    									{
    										erglState = ERGL_LEFT_FAULT;
    										erglLeftFault = 1;
    									}
    
    									break; // END case ERGL_LEFT_ON:
    
    							case ERGL_RIGHT_ON:
    
    								if(ledVoltage > ledOpenVoltage)	// Check for LED open or shorted.
    								{
    									erglRightOVErrorCount++;
    								}
    								else if(erglRightOVErrorCount > 0)
    								{
    									erglRightOVErrorCount--;
    
    								}
    
    								if(runtimeErrorsEnable && (ledVoltage < ledShortVoltage))	// Check for LED open or shorted.
    								{
    									erglRightUVErrorCount++;
    								}
    								else if(erglRightUVErrorCount > 0)
    								{
    									erglRightUVErrorCount--;
    
    								}
    
    								if((erglRightOVErrorCount > ledMaxOVErrors)	|| (erglRightUVErrorCount > LED_MAX_UV_ERRORS))				// If errors are persistent, trigger a fault.
    								{
    									erglState = ERGL_RIGHT_FAULT;
    									erglRightFault = 1;
    								}
    
    								break; // END case ERGL_RIGHT_ON:
    
    							} // END switch(erglState) error checking
    
    						} // END if((erglCounter >= 20) && (erglCounter <= 45))
    
    						if((erglRightFault == 1) && (erglLeftFault == 1))
    						{
    							currentState = FAULT;
    							ledVErrorLog++;
    						}
    
    				   } // END if((erglFlag == 1)&&(connect == 0))
    
    				static int currentSettlingTime = 0;
    				// Input current self-calibration.
    				long tempLong = currentFiltIQ - currentFastIQ;					// Only perform current calibration if the value has
    																			// settled by checking filtered vs unfiltered current.
    				if((tempLong > _IQ26(-0.02)) && (tempLong < _IQ26(0.02)))
    				{
    					currentSettlingTime++;
    
    					if((currentFiltIQ > _IQ26(6.2)) && (currentSettlingTime > 500))						// If input current has reached 6.2A, it is assumed to
    					{														// to be 6.6A and an offset is added (see CLA interrupt).
    						if(currentCalTimer > 0)								// Allow time for the CLA interrupt to process the offset
    						{													// by checking a timer. In a regulator circuit that is operating
    							currentCalFlag = 1;								// correctly, this will eliminate variances in isolation transformers.
    							currentCalTimer--;
    						}
    						else
    						{
    							currentCalFlag = 0;
    							currentSettlingTime = 0;// Keep current from continuously calibrating.
    						}
    					}
    
    					if(currentFiltIQ < _IQ26(5.8))	// If current value rises above 6.7A, it
    					{																		// is recalibrated to 6.6A.
    						currentCalTimer = 5;
    					}
    				}
    				else
    				{
    					if(currentSettlingTime > 0)
    					{
    						currentSettlingTime--;
    					}
    				}
    
    				if(test_timer > 0)
    				{
    					test_timer--;
    
    					if((test_timer < 2000) && (test_timer >= 1000))
    					{
    						dimOverride = 167;
    					}
    					else if(test_timer < 1000)
    					{
    						dimOverride = 37501;
    					}
    				}
    
    				if(pfcVoltage < PFC_SHUTDOWN_LEVEL)
    				{
    					i2cTimer = I2C_AUTO_START;
    					currentState = PFC_BOOT;
    					pfcUVErrorLog++;
    
    				}
    				else if(currentFastIQ < _IQ26(1.0))
    				{
    					i2cTimer = I2C_AUTO_START;			// Attempt to save EEPROM data before shutdown
    					currentState = CURRENT_LOW;
    
    //					ledPeakOCErrorLog++;  // Using ledPeakOCErrorLog to show Current is too low ((less than 1 Amp)
    				}
    
    /* The commented out code below is to test the Watchdog. If GPIO22 is first pulled low and then
    				   GPIO23 is pulled low, then the code pulls GPIO27 low and goes into an infinite loop.
    
                    if (FirstOneSet == 0)       // One GPIO has been set
                    {
                        if ((GPIO22Value == 0 && GPIO23Value == 1 ) || (GPIO22Value == 1 && GPIO23Value == 0))
                        {
                            if (GPIO22Value == 0)
                            {
                                FirstSetGpio = 22;
                            }
                            else if (GPIO23Value == 0)
                            {
                                FirstSetGpio = 23;
                            }
    
                            FirstOneSet = 1;
                        }
                    }
                    else
                    {
                        if (FirstSetGpio == 22 && GPIO23Value == 0)
                        {
                            SecondSetGpio = 23;
                        }
                    }
    
                    if (FirstSetGpio == 22 && SecondSetGpio == 23)
                    {
                        for (;;)
                        {
                            GpioDataRegs.GPASET.bit.GPIO27 = 1;
                        }
                    }
    */
    
    				break;
    
    //-------------------------------------------------------------------------
    			case FAULT:  //	STATE = "4"
    //-------------------------------------------------------------------------
    
    				if(previousState != FAULT)
    				{
    
    					ledCurrentSet = 0;
    					runtimeErrorsEnable = 0;
    
    					EALLOW;
    
    					GpioDataRegs.GPACLEAR.bit.GPIO8	 = 1;       // GPIO8 is LED1_DIM
    	 				GpioDataRegs.GPBCLEAR.bit.GPIO40  = 1;      // GPIO40 is WIG_PWM
    					GpioDataRegs.GPACLEAR.bit.GPIO10 = 1;       // GPIO10 is LED1_PWM_H
    					GpioDataRegs.GPACLEAR.bit.GPIO11 = 1;       // GPIO11 is LED1_PWM_L
    
    					GpioCtrlRegs.GPADIR.bit.GPIO8	= 1;
    		 			GpioCtrlRegs.GPBDIR.bit.GPIO40   = 1;
    					GpioCtrlRegs.GPADIR.bit.GPIO10 	= 1;
    					GpioCtrlRegs.GPADIR.bit.GPIO11 	= 1;
    
    					GpioCtrlRegs.GPAMUX1.bit.GPIO8 	= 0;
    		 			GpioCtrlRegs.GPBMUX1.bit.GPIO40  = 0;
    					GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 0;
    					GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 0;
    
    					GpioCtrlRegs.GPADIR.bit.GPIO6 = 0;	// Turn opto monitor to input (float).
    
    					EDIS;
    
    					previousState = FAULT;
    					i2cTimer = I2C_AUTO_START;			// Advance I2C timer to save data immediately.
    				}
    
    				if(failOpenFlag == 1)
    				{
    					failOpenOnTimer++;
    					failOpenOffTimer++;
    
    					if(failOpenOffTimer < FAIL_OPEN_OFF_LIMIT)
    					{
    						if(failOpenOnTimer < FAIL_OPEN_DUTY)
    						{
    							EALLOW;
    							GpioDataRegs.GPACLEAR.bit.GPIO0 = 1;
    							GpioCtrlRegs.GPADIR.bit.GPIO0	= 1;
    							GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 0;	// Set PFC PWM to output 0. This "opens" the
    							EDIS;								// front end for fail-open compatibility.
    						}
    						else
    						{
    							EALLOW;
    							GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;
    							EDIS;
    						}
    					}
    					else
    					{
    						EALLOW;
    						GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1;
    						EDIS;
    					}
    
    					if(failOpenOnTimer > FAIL_OPEN_CYCLE)
    					{
    						failOpenOnTimer = 0;
    					}
    
    					if(failOpenOffTimer > FAIL_OPEN_OFF_TIME)
    					{
    						failOpenOffTimer = 0;
    						failOpenOnTimer = 0;
    					}
    				}
    
    				ledCalTimer = 0;			// Prevent auto-cal if in FAULT.
    
    				statusLedBlink++;			// Blink pcb indicator LED to show fault.
    
    				if(statusLedBlink == 75)
    				{
    					PCB_LED_TOGGLE = 1;
    					statusLedBlink = 0;
    				}
    
    				break; //case FAULT:
    
    				}	// End switch(currentState)
    
    //--------------------------------------------------------------------------------
    // Common state code - the following code is executed regardless of machine state.
    //--------------------------------------------------------------------------------
    
            if (hirlJumper == 0 && configJumpers == NO_JUMPERS || configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == I525MA_852G_H_OFF)
            {
    //            if(sendAdName < 2)                          // Send the name of this unit for BLE advertising
    //            {
    //                if(adTimer >= 5000)
    //                {
    //                    sendSciAdData(BLE_WRITE_AD_NAME, adName, 12);
    //                    sendAdName++;
    //                    adTimer = 0;
    //                }
    //
    //                adTimer++;
    //            }
            }
            else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)
            {
                //            if(sendAdName < 2)                          // Send the name of this unit for BLE advertising
                //            {
                //                if(adTimer >= 5000)
                //                {
                //                    sendSciAdData(BLE_WRITE_AD_NAME, adName, 12);
                //                    sendAdName++;
                //                    adTimer = 0;
                //                }
                //
                //                adTimer++;
                //            }
            }
            else
            {
                if(sendAdName < 2)							// Send the name of this unit for BLE advertising
                {
                    if(adTimer >= 100)
                    {
                        sendSciAdData(BLE_WRITE_AD_NAME, adName, 12);
                        sendAdName++;
                        adTimer = 0;
                    }
    
                    adTimer++;
                }
            }
    
    		switch(syncDisableFlag)
    		{
    			case 0:
    				EALLOW;
    				EPwm6Regs.TBCTL.bit.PHSEN = TB_ENABLE;
    				EPwm6Regs.DCACTL.bit.EVT1SYNCE	= 1;
    				EDIS;
    			break;
    
    			case 1:
    				EALLOW;										// Normally, the LED PWM is synchronized to the
    				EPwm6Regs.TBCTL.bit.PHSEN = TB_DISABLE;		// LED dimmer. When the LED is at full constant
    				EPwm6Regs.DCACTL.bit.EVT1SYNCE	= 0;		// output, the synchronization continues to disturb
    				EDIS;										// the LED PWM and therefore LED current. To cancel
    			break;											// this effect, the LED PWM synchronization is only
    															// active while dimming and is disabled at full
    			case 2:											// brightness.
    				if(EPwm5Regs.CMPA.half.CMPA >= 37500)
    				{
    					EALLOW;
    					EPwm6Regs.TBCTL.bit.PHSEN = TB_DISABLE;
    					EPwm6Regs.DCACTL.bit.EVT1SYNCE	= 0;
    					EDIS;
    				}
    				else
    				{
    					EALLOW;
    					EPwm6Regs.TBCTL.bit.PHSEN = TB_ENABLE;
    					EPwm6Regs.DCACTL.bit.EVT1SYNCE	= 1;
    					EDIS;
    				}
    			break;
    		}
    
    		ledCurrentErrorChecking();								// Check for LED overcurrent errors.
    
    		// Read various operating point variables with circuit scaling.
    
    
    		long tempShift = ledVoltageCLA;
    		ledVoltage = _IQ15mpy(tempShift<<15 , _IQ15(1.0/LED_VINPUT_GAIN * 1.0/ADC_GAIN));
    
    		tempShift = ledCurrentCLA;
    		ledCurrent = _IQ15mpy(tempShift<<15 , _IQ15(1.0/LED_DC_CS_GAIN * 1.0/LED_DC_CS_RESISTOR * 1.0/ADC_GAIN));
    
    		tempShift = ADC7_hk12Voltage;
    		v12Hk = _IQ15mpy(tempShift<<15 , _IQ15(1.0/V12GAIN * 1.0/ADC_GAIN));
    
    		tempShift = linePeriod<<4;
    		lineFrequency = _IQ4div(_IQ4(100000.0),linePeriod);
    
    
    		if(bluetoothMode != CONFIG)
    		{
                if((eepromInitFlag == 0) && (i2cTimer > 0)) 	// Check the EEPROM for valid data. If no valid data exists, reset the EEPROM data variables.
                {
    
    #if DEBUG_ERASE_EEPROM
    
                    bytePointer = 0;
    
                    while(bytePointer <= EEPROM_MAX_ADDRESS)
                    {
                        initEEPROMpage(bytePointer);
                        bytePointer += 16;
                    }
    
                    bytePointer = 16;
                    eepromInitFlag = 1;
                    i2cTimer = I2C_AUTO_START;
    
    #else
    
                    i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, bytePointer, eepromData);	// Read EEPROM data.
    
                    if(i2cStatus == I2C_ERROR)						// Check for errors and timeout to prevent getting
                    {												// stuck on a bus hang-up.
                       i2cErrorCount++;
                    }
    
                    if(eepromData[0] != EEPROM_DATA_VALID)			// If primary data block is invalid (or new EEPROM) try secondary.
                    {
                        i2cStatus = receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK1, bytePointer, eepromData);	// Read secondary data.
    
                        if(i2cStatus == I2C_ERROR)
                        {
                           i2cErrorCount++;
                        }
    
                        if(eepromData[0] != EEPROM_DATA_VALID)
                        {
                            initEEPROMpage(bytePointer);				// If secondary block was invalid, erase this data set.
                        }
                        else
                        {
                            processEEPROMread(bytePointer, eepromData); // If secondary block was valid, read in this data set.
                        }
                    }
                    else
                    {
                        processEEPROMread(bytePointer, eepromData);		// If primary block was valid, read in this data set.
                    }
    
                    bytePointer += 16;									// Move to the next set of data.
    
                    if(bytePointer > EEPROM_MAX_ADDRESS)				// If at the end of EEPROM data, reset pointers.
                    {
                        bytePointer = 16;
                        eepromInitFlag = 1;
                        i2cTimer = I2C_AUTO_START;
    
                    }
    #endif
    
    			}
                else
                {
    				if(i2cTimer >= I2C_AUTO_START)								// Continuously sends EEPROM data during runtime. Data is
    				{													// stored in two data blocks in case one is corrupted.
    					processEEPROMwrite(bytePointer, eepromData);
    					i2cStatus = transmitI2cData(EEPROM_SLAVE_ADDR|blockPointer, bytePointer, eepromData);	// Send EEPROM data.
    
    					if(i2cStatus == I2C_ERROR)
    					{
    					   i2cErrorCount++;
    					}
    
    					bytePointer +=16;									// Move to the next set of data.
    
    					if(bytePointer > EEPROM_MAX_ADDRESS)				// If at the end of EEPROM data, reset pointers.
    					{
    						bytePointer = 16;
    						blockPointer++;
    
    						if(blockPointer > EEPROM_BLOCK1)
    						{
    							blockPointer = 0;
    							i2cTimer = 0;
    						}
    					}
    				}
    			}
    		}
    
    		if(i2cErrorCount > 100)		// Attempt to restart the I2C bus if there are too many communication issues.
    		{
    			i2cErrorCount = 0;
    			i2cRestart();
    		}
    
    	   if(SciaRegs.SCIFFRX.bit.RXFFST == 4)		// If serial messages were received, process them.
    	   {
    			bleMessage = sciMessageRx();		// Communication received from Bluetooth link.
    
    			if(bleMessage==0x37ED5199)			// If a valid BT link, shut off LED to indicate connection.
    			{									// Wireless module will send this message upon connection.
    				runtimeErrorsEnable = 0;
    
                    if (hirlJumper == 0 && configJumpers == NO_JUMPERS || configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == I525MA_852G_H_OFF)
                    {                               // Don't blink the LED for 852G's because it will interferes with regular
                        turnLedOn = 1;              // blinking, instead turn off the led for short while to indicate connection
                        bleTimerOn = 0;
                        bleLedOnTimer = 0;
                    }
                    else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)
                    {                               // Don't blink the LED for 852G's because it will interferes with regular
                        turnLedOn = 1;              // blinking, instead turn off the led for short while to indicate connection
                        bleTimerOn = 0;
                        bleLedOnTimer = 0;
                    }
    				else
    				{
                        EALLOW;
                        EPwm5Regs.TZFRC.bit.OST = 1;
                        EDIS;
    
                        connectTimer = 0;
                        connect = 1;
    				}
    			}
    
    			if(bleMessage==0xA6ED519A)			// Messages compared to enter various wireless modes.
    			{
    				bluetoothMode = CONFIG;
    				runtimeErrorsEnable = 0;
    
    				bleMessage = 0;
    			}
    			else if(bleMessage==0xA6ED519B)
    			{
    				bluetoothMode = DIAGNOSTIC;
    				runtimeErrorsEnable = 0;
    
    				bleMessage = 0;
    			}
    			else
    			{
    				SciaRegs.SCIFFRX.bit.RXFIFORESET = 0;	// If not a valid message, clear the serial FIFO.
    				SciaRegs.SCIFFRX.bit.RXFIFORESET = 1;
    			}
    		}
    
           if(turnLedOn == 1)
           {
               if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)
               {
                   if (bleTimerOn == 0)
                   {
                       if (blinkState == 1)
                       {
                           if (blinkTimer >= 26 && blinkTimer <= 32)
                           {
                               blinkDisable = 1;
                               bleTimerOn = 1;
                           }
                       }
                   }
                   else
                   {
                       if (bleLedOnTimer < 500)
                       {
                           bleLedOnTimer++;
                       }
                       else
                       {
                           if (blinkState == 1)
                           {
                              if (blinkTimer >= 26 && blinkTimer <= 32)
                              {
                                  blinkDisable = 0;
                                  bleTimerOn = 0;
                                  turnLedOn = 0;
                              }
                           }
                       }
                   }
               }
               else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)
               {
                   if (bleTimerOn == 0)
                   {
                       if (blinkState == 1)
                       {
                           if (blinkTimer >= 26 && blinkTimer <= 32)
                           {
                               blinkDisable = 1;
                               bleTimerOn = 1;
                           }
                       }
                   }
                   else
                   {
                       if (bleLedOnTimer < 500)
                       {
                           bleLedOnTimer++;
                       }
                       else
                       {
                           if (blinkState == 1)
                           {
                              if (blinkTimer >= 26 && blinkTimer <= 32)
                              {
                                  blinkDisable = 0;
                                  bleTimerOn = 0;
                                  turnLedOn = 0;
                              }
                           }
                       }
                   }
               }
    	   }
    
    	   if(bluetoothMode == CONFIG)						// Run the communication routines if in the correct mode.
    	   {
    	       configFunction();
    	   }
    	   else if(bluetoothMode == DIAGNOSTIC)
    	   {
    		   diagFunction();
    	   }
    
           if (toggleFlag == 1)
           {
              if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)
              {
                  if (blinkState == 1 && blinkTimer >= 26 && blinkTimer <= 32)
                  {
                      if(bleData == CONFIG_VALUE_SET)
                      {
                          blinkDisable = 1;
                          dimDisable = 0;
                          toggleFlag = 0;
                      }
                      else
                      {
                          blinkDisable = 0;
                          toggleFlag = 0;
                      }
                  }
              }
              else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)
              {
                  if (blinkState == 1 && blinkTimer >= 26 && blinkTimer <= 32)
                  {
                      if(bleData == CONFIG_VALUE_SET)
                      {
                          blinkDisable = 1;
                          dimDisable = 0;
                          toggleFlag = 0;
                      }
                      else
                      {
                          blinkDisable = 0;
                          toggleFlag = 0;
                      }
                  }
              }
           }
    
    	   if(connect == 1)									// A certain amount of time after connection, LED is re-enabled.
    	   {
    		   connectTimer++;
    
    		   if(connectTimer > 700)
    		   {
    				connect = 0;
    				connectTimer = 0;
    				runtimeErrorsEnable = 1;
    		   }
    		   else if(connectTimer > 500)
    		   {
    				EALLOW;
    				EPwm5Regs.TZCLR.bit.OST = 1;
    				EDIS;
    		   }
    	   }
    
    #if	DEBUG													// Allow coefficient tuning in debug mode.
    
    	  	CNTL_2P2Z_CoefStruct1.b2 = _IQ6(pfcDgain);
    		CNTL_2P2Z_CoefStruct1.b1 = _IQ6(pfcIgain - pfcPgain - pfcDgain - pfcDgain);
    		CNTL_2P2Z_CoefStruct1.b0 = _IQ6(pfcPgain + pfcIgain + pfcDgain);
    		CNTL_2P2Z_CoefStruct1.b1_fast = _IQ6(pfcFastIgain - pfcFastPgain - pfcDgain - pfcDgain);
    		CNTL_2P2Z_CoefStruct1.b0_fast = _IQ6(pfcFastPgain + pfcFastIgain + pfcDgain);
    
    
    		CNTL_2P2Z_CLACoefStruct_LED.b2 = (ledDgain);
    		CNTL_2P2Z_CLACoefStruct_LED.b1 = (ledIgain - ledPgain - ledDgain - ledDgain);
    		CNTL_2P2Z_CLACoefStruct_LED.b0 = (ledPgain + ledIgain + ledDgain);
    #endif
    
    		heatTempFilter += ADC5_heaterTemperature;	// Accumulate heater temp ADC value for filtering.
    		tempFiltCount++;							// Increment filter count.
    
    		if(tempFiltCount == 16)
    		{
    			heatTempFilter = heatTempFilter<<11;	// Heater temp ADC value converted to IQ15
    
    			heaterCelsius = getHeaterTemperature(heatTempFilter);	// Convert heater ADC value to Celsius temp in IQ15.
    
    			heatTempFilter = 0;						// Reset temperature filter values.
    			tempFiltCount = 0;
    		}
    
    		if(heaterCelsius > _IQ15(heatOffTemp))	// Activate or deactivate heater based on temperature limits.
    		{
    			EPwm3Regs.CMPA.half.CMPA = 200;
    		}
    
    		if(heatAmpCntl == 0)						// Only turn on heater if amp control is not active.
    		{
    			if(heaterCelsius < _IQ15(heatOnTemp))
    			{
    				EPwm3Regs.CMPA.half.CMPA = 0;
    			}
    		}
    
    		int heattemp = heaterCelsius>>15;
    
    		if(heattemp > heaterMaxTempRun)				// Compare and store maximum temperatures.
    		{
    			heaterMaxTempRun = heattemp;
    		}
    
    		long mcutemp = AdcResult.ADCRESULT13 - getTempOffset();
    		mcuCelsius = mcutemp * getTempSlope();
    
    		mcutemp = mcuCelsius>>15;
    
    		if(mcutemp > mcuMaxTempRun)
    		{
    			mcuMaxTempRun = mcutemp;
    		}
    
    		if(mcutemp > mcuMaxTemp)
    		{
    			mcuMaxTemp = mcutemp;
    		}
    
    
    		if(sfoTimer >= 100)							// The MEP (micro-edge placement) for the high-res PWM modules is
    		{											// periodically calibrated with the built-in SFO() function.
    			sfoTimer = 0;
    			if(sfoStatus == 1)
    			{
    				EALLOW;
    				EPwm1Regs.HRMSTEP=MEP_ScaleFactor;
    				EDIS;
    			}
    
    			sfoStatus = SFO();
    		}
    
    
    		// Read input current and determine regulator step.
    		if(currentFiltIQ < _IQ26(2.65))
    		{
    			currentStep = B0;
    		}
    		if((currentFiltIQ >= _IQ26(2.65))&&(currentFiltIQ <_IQ26(2.95)))
    		{
    			currentStep = B1;
    		}
    		else if((currentFiltIQ > _IQ26(3.25))&&(currentFiltIQ <_IQ26(3.55)))
    		{
    			currentStep = B2;
    		}
    		else if((currentFiltIQ > _IQ26(3.95))&&(currentFiltIQ <_IQ26(4.25)))
    		{
    			currentStep = B3;
    		}
    		else if((currentFiltIQ > _IQ26(4.65))&&(currentFiltIQ < _IQ26(4.95)))
    		{
    			currentStep = B10;
    		}
    		else if((currentFiltIQ > _IQ26(5.05))&&(currentFiltIQ < _IQ26(5.35)))
    		{
    			currentStep = B4;
    		}
    		else if((currentFiltIQ > _IQ26(5.35))&&(currentFiltIQ <_IQ26(5.65)))
    		{
    			currentStep = B30;
    		}
    		else if((currentFiltIQ > _IQ26(6.45))&&(currentFiltIQ <_IQ26(6.75)))
    		{
    			currentStep = B5;
    		}
    
    		// CCR amp control:
    		// A certain sequence of CCR transitions will activate or de-activate the heater.
    		if((ccrMsgPending == 0) && ((ccrCntlStep == B5) && ((ccrCntlStepPrev == B4) || (ccrCntlStepPrev == B30))))
    		{
    			ccrMsgPending = 1;
    			ccrCntlWord = 0;
    		}
    
    		if(ccrMsgPending)
    		{
    
    			if((ccrCntlStep == B5) && ((ccrCntlStepPrev == B4) || (ccrCntlStepPrev == B30)))
    			{
    				ccrCntlWord = ccrCntlWord << 1;
    				ccrCntlWord |= 0x0001;
    
    				ccrMsgBitCount++;
    
    			}
    			else if(((ccrCntlStep == B3) || (ccrCntlStep == B10)) && ((ccrCntlStepPrev == B4) || (ccrCntlStepPrev == B30)))
    			{
    				ccrCntlWord = ccrCntlWord << 1;
    				ccrCntlWord &= 0xFFFE;
    
    				ccrMsgBitCount++;
    			}
    
    			if(ccrMsgBitCount == 8)			// If the correct number of current transitions is received, attempt to read the message
    			{								// and reset counters.
    				ccrMsgReceived = 1;
    				ccrMsgHeader = (ccrCntlWord>>4) & 0x000F;
    				ccrMsgData	=  ccrCntlWord & 0x000F;
    				ccrCntlTimer = 0;
    				ccrMsgBitCount = 0;
    				ccrMsgPending = 0;
    			}
    
    			ccrCntlTimer++;
    
    		}
    
    		if((ccrMsgReceived == 1) && (ccrMsgHeader == 0xD))
    		{
    			switch(ccrMsgData)
    			{
    
    				case HEATER_ON:
    
    					EPwm3Regs.CMPA.half.CMPA = 0;
    					heatAmpCntl = 1;
    					break;
    
    				case HEATER_OFF:
    
    					EPwm3Regs.CMPA.half.CMPA = 200;
    					heatAmpCntl = 1;
    					break;
    
    				case HEATER_AUTO:	// Exit manual heater control (use temp sensor).
    
    					heatAmpCntl = 0;
    					break;
    
    				case LED_CALIBRATE:	// Exit manual heater control (use temp sensor).
    
    					runLedCal = 1;
    					ledCalValid = 0xFF;
    					ledShortConfig = 0;
    					ledShortVoltage = 0;
    					ledCalTimer = ledAutoCalTime;
    					break;
    			}
    
    			ccrMsgReceived = 0;
    			ccrCntlWord = 0;
    		}
    
    		ccrCntlStepPrev = ccrCntlStep;
    		ccrCntlStep = currentStep;
    
    		if(ccrCntlTimer >= 3000)			// If the correct current transitions are not received in time, reset the receiver.
    		{
    			ccrCntlTimer = 0;
    			ccrMsgBitCount = 0;
    			ccrMsgPending = 0;
    			ccrMsgReceived = 0;
    			ccrCntlWord = 0;
    		}
    
    		while (CpuTimer0Regs.TCR.bit.TIF == 0);		// Waiting here causes the background loop to cycle in set 10ms intervals.
    		CpuTimer0Regs.TCR.bit.TIF = 1;
    
    		timer10ms++;								// Increment timer variables.
    		i2cTimer++;
    		sciTimer++;
    		sfoTimer++;
    
    		if(timer10ms >= 100)						// Update total operating seconds for each step.
    		{
    			timer10ms = 0;
    			opSeconds++;
    
    			switch(currentStep)
    			{
    				case B1:
    					opSecondsB1++;
    					break;
    				case B2:
    					opSecondsB2++;
    					break;
    				case B3:
    					opSecondsB3++;
    					break;
    				case B10:
    					opSecondsB10++;
    					break;
    				case B4:
    					opSecondsB4++;
    					break;
    				case B30:
    					opSecondsB30++;
    					break;
    				case B5:
    					opSecondsB5++;
    					break;
    			}
    		}
    	} // End for(;;) (background loop)
    
    } //END MAIN CODE
    
    long sciMessageRx(void)		// Get serial data and store it in a variable.
    {
    
    long temp;
    long rxData = 0;
    
    		rxData = SciaRegs.SCIRXBUF.bit.RXDT;
    		temp = SciaRegs.SCIRXBUF.bit.RXDT;
    		temp = temp<<8;
    		rxData |=temp;
    		temp = SciaRegs.SCIRXBUF.bit.RXDT;
    		temp = temp<<16;
    		rxData |=temp;
    		temp = SciaRegs.SCIRXBUF.bit.RXDT;
    		temp = temp<<24;
    		rxData |=temp;
    
    	return rxData;
    
    }
    
    void sciMessageTx(int data[], int length)		// Send serial data from a byte array.
    {
    	int byte = 0;
    
    	CpuTimer1Regs.TCR.bit.TRB = 1;				// Reset 10ms cpu timer.
    	CpuTimer1Regs.TCR.bit.TIF = 1;
    
    	while((byte < length) && (CpuTimer1Regs.TCR.bit.TIF != 1))
    	{
    		if(SciaRegs.SCIFFTX.bit.TXFFST < 4)
    		{
    			SciaRegs.SCITXBUF = data[byte];
    			byte++;
    		}
    
    	}
    
    }
    //-------------------------------------------------------------------------------------------------------
    // Check for and log operating errors.
    //-------------------------------------------------------------------------------------------------------
    void ledCurrentErrorChecking(void)
    {
    
    	ledOCErrorReset++;
    
    	if((EPwm6Regs.TZFLG.bit.DCAEVT1 == 1)||(EPwm6Regs.TZFLG.bit.DCBEVT1 == 1))			// Check for LED peak over current.
    	{
    		ledOCWaitCounter++;
    
    //		Cla1ForceTask2();
    
    		runtimeErrorsEnable = 0;
    
    		if(ledOCErrorCount >= ledRestartMax)		// Try to restart LED
    //        if(ledOCErrorCount >= 5)
    		{
    		    Cla1ForceTask2();
    
    		    if (ledOCErrorLog <= 65000)
    		    {
    		        ledOCErrorLog++;
    		    }
    
    		    currentState = FAULT;
    /*
    			ledOCErrorCount = 0;
    			ledOCWaitCounter = 0;
    			ledCurrentSet = 0;
    
    			EALLOW;
    			EPwm6Regs.TZCLR.all = 0x0028;			// Clear DC events, but leave OST triggered.
    			EDIS;
    
    			if((erglFlag == 0)&&(currentState != FAULT))
    			{
    				currentState = LED_SOFT_START;
    				ledRestart = 1;
    			}
    */
    		}
    		else if(ledOCWaitCounter > LED_OC_WAIT)
    		{
    			EALLOW;
    			EPwm6Regs.TZCLR.all = 0x002C;
    			EDIS;
    
    			ledOCWaitCounter = 0;
    			ledOCErrorCount++;
    			ledOCErrorReset = 0;
    			runtimeErrorsEnable = 1;
    		}
    	}
    	else if(ledOCErrorReset >= LED_OC_RESET)
    	{
    		if(ledOCErrorCount > 0)
    		{
    			ledOCErrorCount--;
    		}
    
    		ledOCErrorReset = LED_OC_RESET;
    	}
    }
    
    void runtimeErrorChecking(void)
    {
    
        float f1 = 0, f2 = 0;
    
        f1 = _IQtoF(ledOpenVoltage);
        f2 = _IQtoF(ledVoltage);
    
        memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
        sprintf((char *)serialTxBytes, "ledOpenVoltage(f1): %.2f \r\n", f1);
        sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    
        memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
        sprintf((char *)serialTxBytes, "ledOVoltage(f2): %.2f \r\n", f2);
        sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    
    /*
         memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
         sprintf((char *)serialTxBytes, "ledOVErrorCount: %u \r\n", ledOVErrorCount);
         sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    */
         memset(serialTxBytes, 0x00, sizeof(serialTxBytes));
         sprintf((char *)serialTxBytes, "---------------------- \r\n");
         sciMessageTx(serialTxBytes, strlen(serialTxBytes));
    
    
    
    
    	if(erglFlag == 0)	// ERGL led voltages must be timed with switching.
    	{					// ERGL voltage checking is located in ERGL section.
    
    		if(ledVoltage > ledOpenVoltage)	// Check for LED open or shorted.
    		{
    		    ledOVErrorCount++;
    		}
    		else if(ledOVErrorCount > 0)
    		{
    			ledOVErrorCount--;
    		}
    
    		if(ledVoltage < ledShortVoltage)	// Check for LED open or shorted.
    		{
    			ledUVErrorCount++;
    		}
    		else if(ledUVErrorCount > 0)
    		{
    			ledUVErrorCount--;
    
    		}
    
    		if((ledOVErrorCount > ledMaxOVErrors) || (ledUVErrorCount > ledMaxUVErrors))	// If errors are persistent, trigger a fault.
    		{
    			currentState = FAULT;
    			ledVErrorLog++;
    		}
    
    	} // END if(erglFlag == 0)
    
    	if(pfcOVErr == 1)								// Check for PFC over voltage.
    	{
    		pfcOVErrorCount++;
    		pfcOVErr = 0;
    
    		if(pfcOVErrorCount > PFC_MAX_OV_ERRORS)		// If errors are persistent, trigger a fault.
    		{
    			currentState = FAULT;
    			pfcOVErrorLog++;
    		}
    	}
    	else if(pfcOVErrorCount > 0)
    	{
    		pfcOVErrorCount--;
    	}
    
    	if(pfcOCErr == 1)								// Check for PFC over current.
    	{
    		pfcOCErrorCount++;
    		pfcOCErr = 0;
    
    		if(pfcOCErrorCount > PFC_MAX_OC_ERRORS)		// If errors are persistent, trigger a fault.
    		{
    			currentState = FAULT;
    			pfcOCErrorLog++;
    		}
    	}
    	else if(pfcOCErrorCount > 0)
    	{
    		pfcOCErrorCount--;
    	}
    
    	if((ADC7_hk12Voltage > VHK12_OV_REF)||(ADC7_hk12Voltage < VHK12_UV_REF))	// Check 12V housekeeping limits.
    	{
    		v12ErrorCount++;
    
    		if(v12ErrorCount > V12_MAX_ERRORS)							// If errors are persistent, trigger a fault.
    		{
    			currentState = FAULT;
    			v12ErrorLog++;
    		}
    	}
    	else if(v12ErrorCount > 0)
    	{
    		v12ErrorCount--;
    	}
    
    }
    
    //-------------------------------------------------------------------------------------------------------
    // Calculate heater temperature using curve fit equations
    //-------------------------------------------------------------------------------------------------------
    long getHeaterTemperature(long adcValue)
    {
    	long heatTempIQ15;
    
    	// Heater thermistor curve fit equations:
    	// y = 18.999x + 1151.6 from -10 to 20C
    	// y = 0.0369x - 37.587 from 20 to 75C
    	// y = 0.0713x - 143.37 from 75 to 125C
    
    	if(adcValue < _IQ15(988.0))
    	{
    		heatTempIQ15 = _IQ15(-10);
    	}
    	else if(adcValue > _IQ15(3710.0))
    	{
    		heatTempIQ15 = _IQ15(125.0);
    	}
    	else if (adcValue < _IQ15(1557.0))
    	{
    		heatTempIQ15 = _IQ15mpy(_IQ15(0.0522), adcValue) - _IQ15(60.032);
    	}
    	else if (adcValue < _IQ15(3016.0))
    	{
    		heatTempIQ15 = _IQ15mpy(_IQ15(0.0369), adcValue) - _IQ15(37.587);
    	}
    	else
    	{
    		heatTempIQ15 = _IQ15mpy(_IQ15(0.0713), adcValue) - _IQ15(143.37);
    	}
    
    	return heatTempIQ15;
    }
    
    //-------------------------------------------------------------------------------------------------------
    // Initialization for CLA subprocessor.
    //-------------------------------------------------------------------------------------------------------
    void init_cla()
    {
       EALLOW;
    
       Cla1Regs.MVECT2 = (Uint16) (&Cla1Task2 - &Cla1Prog_Start)*sizeof(Uint32);	// CLA tasks.
       Cla1Regs.MVECT4 = (Uint16) (&Cla1Task4 - &Cla1Prog_Start)*sizeof(Uint32);
       Cla1Regs.MVECT8 = (Uint16) (&Cla1Task8 - &Cla1Prog_Start)*sizeof(Uint32);
    
       Cla1Regs.MPISRCSEL1.bit.PERINT2SEL = CLA_INT2_NONE;
       Cla1Regs.MPISRCSEL1.bit.PERINT4SEL = CLA_INT4_EPWM4INT;						// CLA Task 4 is triggered by PWM4.
       Cla1Regs.MPISRCSEL1.bit.PERINT8SEL = CLA_INT8_NONE;							// CLA Task 8 is software triggered only.
    
       MemCopy(&Cla1funcsLoadStart, &Cla1funcsLoadEnd, &Cla1funcsRunStart);			// Copy CLA program code to CLA RAM.
       Cla1Regs.MMEMCFG.bit.PROGE = 1;												// Transfer CLA memory access to CLA.
    
       Cla1Regs.MCTL.bit.IACKE = 1;													// Enable CLA task to be triggered by software.
       Cla1Regs.MIER.all = M_INT2|M_INT4|M_INT8;								// Enable CLA task interrupts.
    
       Cla1ForceTask8();															// Start CLA Task 8 to initialize CLA
       	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	   	// accessible variables.
       EDIS;
    
    }
    
    // When the EEPROM is New
    void initEEPROMpage(int byteAddress)
    {
    	switch(byteAddress)
    	{
    		case CONFIGDATA0:
    
    			oddEvenConfigValid	= 0xFF;
    			oddEvenConfig		= 0xFF;
    			phaseConfigValid	= 0xFF;
    			phaseConfig			= 0xFF;
    			dimRampConfigValid	= 0xFF;
    			dimRampConfig		= 0xFF;
    			ledCalValid			= 0xFF;
    			ledShortConfig		= _IQ15(0.0);
    			hardwareConfigValid = 0xFF;
    			hardwareConfig 		= 0xFF;
    
    		break;
    
    		case EEPROMDATA0:
    
    			opSeconds		= 0;
    			opSecondsB1		= 0;
    			opSecondsB2		= 0;
    
    			break;
    
    		case EEPROMDATA1:
    
    			opSecondsB3		= 0;
    			opSecondsB10	= 0;
    			opSecondsB4		= 0;
    
    			break;
    
    		case EEPROMDATA2:
    
    			opSecondsB30	= 0;
    			opSecondsB5		= 0;
    
    			mcuMaxTemp		= 0;
    
    			break;
    
    		case EEPROMDATA3:
    
    			pfcOVErrorLog		= 0;
    			pfcUVErrorLog		= 0;
    			pfcOCErrorLog		= 0;
    			v12ErrorLog			= 0;
    			ledVErrorLog		= 0;
    			ledOCErrorLog		= 0;
    			ledPeakOCErrorLog	= 0;
    
    			break;
    
    	}
    }
    
    //-------------------------------------------------------------------------------------------------------
    // EEPROM data is divided into 16 byte pages that can be written to the EEPROM using a page write.
    // The following function processes the data bytes read from the EEPROM and stores them in the proper
    // data variable.
    //-------------------------------------------------------------------------------------------------------
    void processEEPROMread(int byteAddress, int data[])
    {
    	switch(byteAddress)
    	{
    
    		case CONFIGDATA0:
    
    			oddEvenConfigValid	= data[1];
    			oddEvenConfig		= data[2];
    
    			phaseConfigValid	= data[3];
    			phaseConfig			= data[4];
    
    			dimRampConfigValid	= data[5];
    			dimRampConfig		= data[6];
    
    			ledCalValid			= data[7];
    			ledShortConfig		= parseBytesToData(data, 8, 4);
    
    			hardwareConfigValid = data[12];
    			hardwareConfig 		= data[13];
    
    			break;
    
    		case EEPROMDATA0:
    
    			opSeconds			= parseBytesToData(data, 1, 4);
    			opSecondsB1			= parseBytesToData(data, 5, 4);
    			opSecondsB2			= parseBytesToData(data, 9, 4);
    
    			break;
    
    		case EEPROMDATA1:
    
    			opSecondsB3			= parseBytesToData(data, 1, 4);
    			opSecondsB10		= parseBytesToData(data, 5, 4);
    			opSecondsB4			= parseBytesToData(data, 9, 4);
    
    			break;
    
    		case EEPROMDATA2:
    
    			opSecondsB30		= parseBytesToData(data, 1, 4);
    			opSecondsB5			= parseBytesToData(data, 5, 4);
    			mcuMaxTemp		= (int)parseBytesToData(data, 9, 2);
    
    			break;
    
    		case EEPROMDATA3:
    
    			pfcOVErrorLog		= (int)parseBytesToData(data, 1, 2);
    			pfcUVErrorLog		= (int)parseBytesToData(data, 3, 2);
    			pfcOCErrorLog		= (int)parseBytesToData(data, 5, 2);
    			v12ErrorLog			= (int)parseBytesToData(data, 7, 2);
    			ledVErrorLog		= (int)parseBytesToData(data, 9, 2);
    			ledOCErrorLog		= (int)parseBytesToData(data, 11, 2);
    			ledPeakOCErrorLog	= (int)parseBytesToData(data, 13, 2);
    
    			break;
    	}
    }
    
    //-------------------------------------------------------------------------------------------------------
    // EEPROM data is divided into 16 byte pages that can be written to the EEPROM using a page write.
    // The following function processes the data variables and stores them in a byte array to be
    // transmitted over I2C.
    //-------------------------------------------------------------------------------------------------------
    void processEEPROMwrite(int byteAddress, int data[])
    {
    	switch(byteAddress)
    	{
    
    		case CONFIGDATA0:
    			data[0] = EEPROM_DATA_VALID;
    			data[1] = oddEvenConfigValid;
    			data[2] = oddEvenConfig;
    			data[3] = phaseConfigValid;
    			data[4] = phaseConfig;
    			data[5] = dimRampConfigValid;
    			data[6] = dimRampConfig;
    
    			data[7] = ledCalValid;
    			parseDataToBytes(ledShortConfig, data, 8, 4);
    
    			data[12] = hardwareConfigValid;
    			data[13] = hardwareConfig;
    			data[14] = 0xFF;
    			data[15] = 0xFF;
    
    		break;
    
    		case EEPROMDATA0:
    			data[0] = EEPROM_DATA_VALID;
    
    			parseDataToBytes(opSeconds, data, 1, 4);
    			parseDataToBytes(opSecondsB1, data, 5, 4);
    			parseDataToBytes(opSecondsB2, data, 9, 4);
    
    			data[13] = 0xFF;
    			data[14] = 0xFF;
    			data[15] = 0xFF;
    
    			break;
    
    		case EEPROMDATA1:
    			data[0] = EEPROM_DATA_VALID;
    
    			parseDataToBytes(opSecondsB3, data, 1, 4);
    			parseDataToBytes(opSecondsB10, data, 5, 4);
    			parseDataToBytes(opSecondsB4, data, 9, 4);
    
    			data[13] = 0xFF;
    			data[14] = 0xFF;
    			data[15] = 0xFF;
    
    			break;
    
    		case EEPROMDATA2:
    			data[0] = EEPROM_DATA_VALID;
    
    			parseDataToBytes(opSecondsB30, data, 1, 4);
    			parseDataToBytes(opSecondsB5, data, 5, 4);
    			data[9] = mcuMaxTemp;
    
    			data[11] = 0xFF;	//ledMaxTemp&0x00FF;
    			data[12] = 0xFF;	//(ledMaxTemp>>8)&0x00FF;
    
    			data[13] = 0xFF;
    			data[14] = 0xFF;
    			data[15] = 0xFF;
    
    			break;
    
    		case EEPROMDATA3:
    
    			data[0] = EEPROM_DATA_VALID;
    
    			parseDataToBytes(pfcOVErrorLog, data, 1, 2);
    			parseDataToBytes(pfcUVErrorLog, data, 3, 2);
    			parseDataToBytes(pfcOCErrorLog, data, 5, 2);
    			parseDataToBytes(v12ErrorLog, data, 7, 2);
    			parseDataToBytes(ledVErrorLog, data, 9, 2);
    			parseDataToBytes(ledOCErrorLog, data, 11, 2);
    			parseDataToBytes(ledPeakOCErrorLog, data, 13, 2);
    
    			data[15] 	= 0xFF;
    
    			break;
    
    	}
    }
    
    long parseBytesToData(int data[], int offset, int length)
    {
    	long temp1 = 0;
    	long temp2 = 0;
    	int shift = 0;
    
    	while(length > 0)
    	{
    		temp1 	|= data[offset];
    		temp1	= temp1<<shift;
    		temp2	= temp2 | temp1;
    
    		temp1 = 0;
    		offset++;
    		shift += 8;
    		length--;
    	}
    
    	return temp2;
    }
    
    void parseDataToBytes(long value, int data[], int offset, int length)
    {
    	long temp = 0;
    
    	temp = value;
    
    	while(length > 0)
    	{
    		data[offset] = temp&0x000000FF;
    		temp	= temp>>8;
    		offset++;
    		length--;
    	}
    
    }
    
    void sendSciData(int dataID, long dataValue, int dataLength)
    {
    
    	serialTxBytes[0] = SERIAL_MESSAGE_HEAD; 			// Fixed message header required by wireless IC
    	serialTxBytes[1] = BLE_SEND_ATTRIBUTE;
    	serialTxBytes[2] = 8;
    
    	parseDataToBytes(dataValue, serialTxBytes, 3, 4);
    
    	serialTxBytes[7] = 0;
    	serialTxBytes[8] = 0;
    	serialTxBytes[9] = dataLength;
    	serialTxBytes[10] = dataID;
    
    	sciMessageTx(serialTxBytes, 11);
    
    }
    
    void sendSciAdData(int dataID, char data[], int length)	// Sends name of this device for Bluetooth advertising.
    {
    
    	serialTxBytes[0] = SERIAL_MESSAGE_HEAD;
    	serialTxBytes[1] = dataID;
    	serialTxBytes[2] = length;
    
        int k = 0;
        int l = 3;
    
        while( k < length )
        {
        	serialTxBytes[l] = data[k];
          k++;
          l++;
        }
    
    
    	sciMessageTx(serialTxBytes, (length + 3));
    
    }
    
    void configFunction()		// Sends and receives various configuration data over the serial/Bluetooth link.
    {
    					if(configOnEntry == 1)
    					{
    						sendSciData(CONFIG_MODE, CONFIG_MODE_ENTER, CONFIG_MODE_LENGTH);
    						configOnEntry = 0;
    					}
    
    
    					if(bleNotify == 1)
    					{
    
    						receiveI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, CONFIGDATA0, eepromData);
    						processEEPROMread(CONFIGDATA0, eepromData);
    
    						processEEPROMwrite(CONFIGDATA0, eepromData);
    						transmitI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK1, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.
    
    						switch(bleNotifyData)
    						{
    
    							case ODDEVEN_CONFIG:
    							{
    
    								sendSciData(ODDEVEN_CONFIG, oddEvenConfig, 1);
    
    								break;
    							}
    
    							case PHASE_CONFIG:
    							{
    								sendSciData(PHASE_CONFIG, phaseConfig, 1);
    
    								break;
    							}
    
    							case DIM_RAMP_CONFIG:
    							{
    								sendSciData(DIM_RAMP_CONFIG, dimRampConfig, 1);
    
    								break;
    							}
    
    							case LED_SHORT_CAL:
    							{
    								sendSciData(LED_SHORT_CAL, ledShortVoltage, 2);
    								break;
    							}
    
    							case LED_SHORT_CAL_SAVE:
    							{
    								sendSciData(LED_SHORT_CAL_SAVE, ledShortConfig, 2);
    								break;
    							}
    
    							case HARDWARE_CONFIG_VALID:
    							{
    								sendSciData(HARDWARE_CONFIG_VALID, hardwareConfigValid, 1);
    								break;
    							}
    
    							case HARDWARE_CONFIG:
    							{
    								sendSciData(HARDWARE_CONFIG, hardwareConfig, 1);
    								break;
    							}
    
    						}
    
    						bleNotify = 0;
    						bleNotifyData = 0;
    
    					}
    
    					if(bleMessage != 0)
    					{
    
    						if(bleMessage == 0xA6ED519C)		// Exit Configuration Mode (handset app has exited).
    						{
    
    							sendSciData(CONFIG_MODE, CONFIG_MODE_EXIT, CONFIG_MODE_LENGTH);
    							bluetoothMode = 0;
    							configOnEntry = 1;
    							bleMessage = 0;
    							runtimeErrorsEnable = 1;
    						}
    
    						long tempmessage = bleMessage;
    
    						bleData = tempmessage&0x000000FF;
    						tempmessage = tempmessage>>8;
    
    						bleDataID = tempmessage&0x000000FF;
    						tempmessage = tempmessage>>8;
    
    						bleCommand = tempmessage&0x000000FF;
    						tempmessage = tempmessage>>8;
    
    						bleCode = tempmessage&0x000000FF;
    						bleMessage = 0;
    
    						configExitTimer = 0;
    
    						if(bleCode == 0xA6)
    						{
    
    							if(bleCommand == SERIAL_WRITE_CONFIG)
    							{
    								switch(bleDataID)
    								{
    									case ODDEVEN_CONFIG:
    									{
    
    										if((bleData == CONFIG_VALUE_CLEARED)||(bleData ==CONFIG_VALUE_SET))
    										{
    											if(oddEvenConfig != bleData)
    											{
    												if(blinkState == 1)
    												{
    													blinkState = 0;
    													guardSyncOddEven = 0;
    												}
    												else
    												{
    													blinkState = 1;
    													guardSyncOddEven = 1;
    												}
    
    											}
    
    											oddEvenConfigValid = EEPROM_DATA_VALID;
    											oddEvenConfig = bleData;
    
    											processEEPROMwrite(CONFIGDATA0, eepromData);
    											transmitI2cData(EEPROM_SLAVE_ADDR|EEPROM_BLOCK0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.
    
    											bleNotify		= 1;				// Set to notify
    											bleNotifyData	= ODDEVEN_CONFIG;
    										}
    
    										break;
    									}
    
    									case PHASE_CONFIG:
    									{
    
    
    										if((bleData == 0x51)||(bleData == 0x50))
    										{
    
    												if(phaseConfig != bleData)
    												{
    													if(bleData == 0x51)
    													{
    														int temp = blinkTimer + 116;
    														if(temp > 232)
    														{
    															temp = temp - 232;
    														}
    															blinkTimer = temp;
    													}
    													else if (bleData == 0x50)
    													{
    														int temp = blinkTimer - 116;
    
    														if(temp < 0)
    														{
    															temp = temp + 232;
    														}
    
    														blinkTimer = temp;
    													}
    
    												}
    
    											phaseConfigValid = EEPROM_DATA_VALID;
    											phaseConfig = bleData;
    
    											processEEPROMwrite(CONFIGDATA0, eepromData);
    											transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.
    
    											bleNotify		= 1;
    											bleNotifyData	= PHASE_CONFIG;
    										}
    									break;
    									}
    
    									case FULLON_CONFIG:
    									{
    										if((bleData == CONFIG_VALUE_CLEARED)||(bleData == CONFIG_VALUE_SET))
    										{
    										    if (hirlJumper == 0 && configJumpers == I525MA_852G_H_ON || configJumpers == I525MA_852G_H_OFF || configJumpers == I525MA_852G_H_FULL_ON || configJumpers == NO_JUMPERS)
    										    {
    										        toggleFlag = 1;
    										    }
    										    else if (boostJumper == 0 && configJumpers == L852G_ODD || configJumpers == L852G_FULL_ON || configJumpers == NO_JUMPERS)
    										    {
                                                    toggleFlag = 1;
    										    }
    										    else
    										    {
    										        if(bleData == CONFIG_VALUE_SET)
    												{
    													blinkDisable = 1;
    													dimDisable = 0;
    												}
    												else
    												{
    													blinkDisable = 0;
    												}
    										    }
    										}
    									break;
    									}
    
    									case DIM_RAMP_CONFIG:
    									{
    
    										if((bleData >= 1)&&(bleData <= 5))
    										{
    											dimRampVal = bleData;
    
    											dimRampConfigValid = EEPROM_DATA_VALID;
    											dimRampConfig = bleData;
    
    											processEEPROMwrite(CONFIGDATA0, eepromData);
    											transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.
    
    											bleNotify		= 1;
    											bleNotifyData	= DIM_RAMP_CONFIG;
    										}
    
    
    									break;
    									}
    
    									case LED_SHORT_CAL:
    									{
    
    										runLedCal		= 1;
    										bleNotify		= 1;
    										bleNotifyData	= LED_SHORT_CAL;
    
    									break;
    									}
    
    									case LED_SHORT_CAL_SAVE:
    									{
    										ledCalValid		= EEPROM_DATA_VALID;
    										ledShortConfig	= ledShortVoltage;
    
    										processEEPROMwrite(CONFIGDATA0, eepromData);
    										transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData);
    
    										bleNotify		= 1;
    										bleNotifyData	= LED_SHORT_CAL_SAVE;
    
    									break;
    									}
    
    									case HARDWARE_CONFIG_VALID:
    									{
    
    										if(bleData != 0xFF)
    										{
    											hardwareConfigValid	= bleData;
    
    											if(hardwareConfigValid == hardwareConfig)
    											{
    												ledCalValid 	= 0xFF;
    												ledShortConfig 	= 0;
    												ledShortVoltage = 0;
    											}
    										}
    
    										processEEPROMwrite(CONFIGDATA0, eepromData);
    										transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.
    
    										bleNotify		= 1;
    										bleNotifyData	= HARDWARE_CONFIG_VALID;
    
    									break;
    									}
    
    									case HARDWARE_CONFIG:
    									{
    
    										if(bleData != 0xFF)
    										{
    											hardwareConfig	= bleData;
    
    											if(hardwareConfigValid == hardwareConfig)
    											{
    												ledCalValid 	= 0xFF;
    												ledShortConfig 	= 0;
    												ledShortVoltage = 0;
    											}
    										}
    
    										processEEPROMwrite(CONFIGDATA0, eepromData);
    										transmitI2cData(EEPROM_SLAVE_ADDR|0, CONFIGDATA0, eepromData); // Save redundant blocks of EEPROM data.
    
    										bleNotify		= 1;
    										bleNotifyData	= HARDWARE_CONFIG;
    
    									break;
    									}
    
    								} // End switch(bleDataID)
    							} // End if(bleCommand == SERIAL_WRITE_CONFIG)
    							else if(bleCommand == SERIAL_READ_CONFIG)
    							{
    								switch(bleDataID)
    								{
    									case ODDEVEN_CONFIG:
    									{
    										sendSciData(ODDEVEN_CONFIG, oddEvenConfig, 1);
    
    										break;
    									}
    
    									case PHASE_CONFIG:
    									{
    										sendSciData(PHASE_CONFIG, phaseConfig, 1);
    
    									break;
    									}
    
    									case DIM_RAMP_CONFIG:
    									{
    										sendSciData(DIM_RAMP_CONFIG, dimRampConfig, 1);
    
    										break;
    									}
    
    								}
    							}
    
    						} // End if(bleCode == 0xA6)
    
    					} // End if(bleCommand != 0)
    
    					configExitTimer++;
    
    					if(configExitTimer > 12000)
    					{
    						sendSciData(CONFIG_MODE, CONFIG_MODE_EXIT, CONFIG_MODE_LENGTH);
    						bluetoothMode = 0;
    						configOnEntry = 1;
    						configExitTimer = 0;
    						runtimeErrorsEnable = 1;
    					}
    
    } // End ConfigFunction()
    
    void diagFunction()			// Sends and receives diagnostic data over BLE/serial link.
    {
    
    							if(diagOnEntry == 1)
    							{
    								sendSciData(DIAG_MODE, DIAG_MODE_ENTER, DIAG_MODE_LENGTH);
    								diagOnEntry = 0;
    							}
    
    							if(bleMessage != 0)
    							{
    
    								if(bleMessage == 0xA6ED519C)		// Exit Diagnostic Mode (handset app has exited).
    								{
    									sendSciData(DIAG_MODE, DIAG_MODE_EXIT, DIAG_MODE_LENGTH);
    									bluetoothMode = 0;
    									bleMessage = 0;
    									diagOnEntry = 1;
    									runtimeErrorsEnable = 1;
    								}
    
    								long tempmessage = bleMessage;
    
    								bleData = tempmessage&0x000000FF;
    								tempmessage = tempmessage>>8;
    
    								bleDataID = tempmessage&0x000000FF;
    								tempmessage = tempmessage>>8;
    
    								bleCommand = tempmessage&0x000000FF;
    								tempmessage = tempmessage>>8;
    
    								bleCode = tempmessage&0x000000FF;
    								bleMessage = 0;
    
    								configExitTimer = 0;
    
    
    								if(bleCommand == SERIAL_READ_DIAG)
    								{
    									switch(bleDataID)
    									{
    
    										case INPUT_CURRENT_CHAR:
    										{
    											sendSciData(INPUT_CURRENT_CHAR, currentFiltIQ, 4);
    										}
    										break;
    
    										case PFC_VOLTAGE_CHAR:
    										{
    											sendSciData(PFC_VOLTAGE_CHAR, pfcVoltage, 4);
    										}
    										break;
    
    										case LED_VOLTAGE_CHAR:
    										{
    											sendSciData(LED_VOLTAGE_CHAR, ledVoltage, 4);
    										}
    										break;
    
    
    										case LED_CURRENT_CHAR:
    										{
    											sendSciData(LED_CURRENT_CHAR, ledCurrent, 4);
    										}
    										break;
    
    										case CURRENT_STEP_CHAR:
    										{
    											sendSciData(CURRENT_STEP_CHAR, currentStep, 1);
    										}
    										break;
    
    										case HEATER_TEMP_CHAR:
    										{
    											sendSciData(HEATER_TEMP_CHAR, (heaterCelsius >> 15), 1);;
    										}
    										break;
    
    										case MCU_TEMP_CHAR:
    										{
    											sendSciData(MCU_TEMP_CHAR, (mcuCelsius >> 15), 1);
    										}
    										break;
    
    										case MCU_MAX_TEMP_CHAR:
    										{
    											sendSciData(MCU_MAX_TEMP_CHAR, mcuMaxTemp, 1);;
    										}
    										break;
    
    										case V12_VOLTAGE_CHAR:
    										{
    											sendSciData(V12_VOLTAGE_CHAR, v12Hk, 4);
    										}
    										break;
    
    										case OPSECONDS_CHAR:
    										{
    											sendSciData(OPSECONDS_CHAR, opSeconds, 4);
    										}
    										break;
    
    										case OPSECONDS_B1_CHAR:
    										{
    											sendSciData(OPSECONDS_B1_CHAR, opSecondsB1, 4);
    										}
    										break;
    
    										case OPSECONDS_B2_CHAR:
    										{
    											sendSciData(OPSECONDS_B2_CHAR, opSecondsB2, 4);
    										}
    										break;
    
    										case OPSECONDS_B3_CHAR:
    										{
    											sendSciData(OPSECONDS_B3_CHAR, opSecondsB3, 4);
    										}
    										break;
    
    										case OPSECONDS_B10_CHAR:
    										{
    											sendSciData(OPSECONDS_B10_CHAR, opSecondsB10, 4);
    										}
    										break;
    
    										case OPSECONDS_B4_CHAR:
    										{
    											sendSciData(OPSECONDS_B4_CHAR, opSecondsB4, 4);
    										}
    										break;
    
    										case OPSECONDS_B30_CHAR:
    										{
    											sendSciData(OPSECONDS_B30_CHAR, opSecondsB30, 4);
    										}
    										break;
    
    										case OPSECONDS_B5_CHAR:
    										{
    											sendSciData(OPSECONDS_B5_CHAR, opSecondsB5, 4);
    										}
    										break;
    
    										case PFC_OV_ERROR_LOG_CHAR:
    										{
    											sendSciData(PFC_OV_ERROR_LOG_CHAR, pfcOVErrorLog, 2);
    										}
    										break;
    
    										case PFC_UV_ERROR_LOG_CHAR:
    										{
    											sendSciData(PFC_UV_ERROR_LOG_CHAR, pfcUVErrorLog, 2);
    										}
    										break;
    
    										case PFC_OC_ERROR_LOG_CHAR:
    										{
    											sendSciData(PFC_OC_ERROR_LOG_CHAR, pfcOCErrorLog, 2);
    										}
    										break;
    
    										case V12_ERROR_LOG_CHAR:
    										{
    											sendSciData(V12_ERROR_LOG_CHAR, v12ErrorLog, 2);
    										}
    										break;
    
    										case LED_VOLT_ERROR_LOG_CHAR:
    										{
    											sendSciData(LED_VOLT_ERROR_LOG_CHAR, ledVErrorLog, 2);
    										}
    										break;
    
    										case LED_DC_OC_ERROR_LOG_CHAR:
    										{
    											sendSciData(LED_DC_OC_ERROR_LOG_CHAR, ledOCErrorLog, 2);
    										}
    										break;
    
    										case LED_PEAK_OC_ERROR_LOG_CHAR:
    										{
    											sendSciData(LED_PEAK_OC_ERROR_LOG_CHAR, ledPeakOCErrorLog, 2);
    										}
    										break;
    
    										case MACHINE_STATE_CHAR:
    										{
    											sendSciData(MACHINE_STATE_CHAR, currentState, 1);
    										}
    										break;
    
    										case SVN_REV_CHAR:
    										{
    											sendSciData(SVN_REV_CHAR, svnRev, 2);
    										}
    										break;
    
    										case SVN_DATE_STAMP_CHAR:
    										{
    											sendSciData(SVN_DATE_STAMP_CHAR, svnRevStamp, 4);
    										}
    										break;
    
    										case CONFIG_JUMPERS_CHAR:
    										{
    											sendSciData(CONFIG_JUMPERS_CHAR, configJumpers, 1);
    										}
    										break;
    
    										case ONBOARD_JUMPERS_CHAR:
    										{
    
    											int onBoardJumpers = 0;
    											onBoardJumpers |= signJumper;
    											onBoardJumpers = onBoardJumpers<<1;
    											onBoardJumpers |= boostJumper;
    
    											sendSciData(ONBOARD_JUMPERS_CHAR, onBoardJumpers, 1);
    										}
    										break;
    
    										case HEATER_MAX_TEMP_RUN_CHAR:
    										{
    											sendSciData(HEATER_MAX_TEMP_RUN_CHAR, heaterMaxTempRun, 1);
    										}
    										break;
    
    										case MCU_MAX_TEMP_RUN_CHAR:
    										{
    											sendSciData(MCU_MAX_TEMP_RUN_CHAR, mcuMaxTempRun, 1);
    										}
    										break;
    
    										case LINE_FREQUENCY_CHAR:
    										{
    											sendSciData(LINE_FREQUENCY_CHAR, lineFrequency, 4);
    										}
    										break;
    
    										case FIRMWARE_LETTER_REV:
    										{
    											sendSciData(FIRMWARE_LETTER_REV, firmwareRev, 2);
    										}
    										break;
    									}
    
    								}
    
    							}
    
    							configExitTimer++;
    
    							if(configExitTimer > 12000)
    							{
    								sendSciData(DIAG_MODE, DIAG_MODE_EXIT, DIAG_MODE_LENGTH);
    								bluetoothMode = 0;
    								diagOnEntry = 1;
    								configExitTimer = 0;
    								runtimeErrorsEnable = 1;
    							}
    } // End DiagFunction
    
    int ledCurrentRamp(volatile float *refPoint, float setPoint)
    {
    	float diff = setPoint - *refPoint;
    
    	if (fabs(diff) <= 0.2)
    	{
    		*refPoint = setPoint;
    		return 1;
    	}
    	else
    	{
    		if(diff < 0)
    		{
    			*refPoint -= 0.2;
    		}
    		else
    		{
    			*refPoint += 0.2;
    		}
    		return 0;
    	}
    }
    
    

    Thank you for the response. Unfortunately, we cannot set break points because the processor needs to run in an infinite loop and stopping it at a break point will cause damage to the hardware.

    I know it’s a lot of code, so I will point out the places where the relevant variables are mentioned and the values calculated.

    I am inserting the Main C file and an Assembly file that deals with these variables.

    In the C code the variable is called ‘ledVoltage’ and the code snippet below shows how it’s declared and used.

     

    168: extern unsigned int ledVoltageCLA;

    3,203: long tempShift = ledVoltageCLA;

    3,204: ledVoltage = _IQ15mpy(tempShift<<15 , _IQ15(1.0/LED_VINPUT_GAIN * 1.0/ADC_GAIN));

     

    In the Assembly routine ( ULEDPS-CLA-ISR.asm), the variable names are ‘AledVoltageCLA’ and ‘ADC6_ledVoltageCLA’. The code snippets below show where it’s declared and used.

     

    86: ADC6_ledVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    89: _ledVoltageCLA   .usect "Cla1ToCpuMsgRAM",1,1

    110: .def _ledVoltageCLA

    185: MMOV16 @ADC6_ledVoltageCLA, MR0

    587: MMOVZ16 MR0, @ADC6_ledVoltageCLA

    588: MMOV16 @_ledVoltageCLA, MR0

    680: MMOV16 @_ledVoltageCLA, MR0

     

    Hope this will give you enough information to figure out the issue. Please let me know if you need anything else.

     

    Thank you
    Indika

  • Marlyn,

    I clicked on 'This resolved my issue' by mistake. Can you please reopen the ticket. Also it didn't allow me to inset the Assembly code. So I am posting it below.

    ;----------------------------------------------------------------------------------

    ; FILE: ULEDPS-CLA-ISR.C

    ;

    ; Description: CLA core interrupt routine, LED controls and protection, input current calculation.

    ;

    ; Version: 0.0

    ;

    ; Target: TMS320F28035 (Piccolo)

    ;

    ;----------------------------------------------------------------------------------

    ; Copyright Cooper Technologies Co. © 2013

    ;----------------------------------------------------------------------------------

    ; $Author: traver.gumaer $

    ; $Date: 2014-12-09 12:33:53 -0500 (Tue, 09 Dec 2014) $

    ; $Id: ULEDPS-CLA-ISR.asm 338 2014-12-09 17:33:53Z traver.gumaer $

    ; $Rev: 338 $

    ; $URL: 10.91.161.17/.../ULEDPS-CLA-ISR.asm $

    ;

    ;Gives peripheral addresses visibility in assembly

    .cdecls C,LIST,"PeripheralHeaderIncludes.h"

    .cdecls C,LIST,"ULEDPS-Settings.h"

    ;Include files for the Power Library Macro's being used by the system

    .include "ADCDRV_1ch_CLA.asm"

    .include "PWMDRV_1chHiRes_CLA.asm"

    .include "CNTL_2P2Z_CLA.asm"

    ; .include "CLAShared.h"

    .def _CLA_CTRL_INIT ; Make CLA interrupt init routine publicly visible.

    ; dummy variable for pointer initialisation

    ZeroNetCLA .usect "ZeroNetCLA_Section",2,1,1 ; output terminal 1

    .text

    ;---------------------------------------------------------

    ; ISR Initialisation

    ;---------------------------------------------------------

    _CLA_CTRL_INIT:

    ; Clear the ZeroNet

    ZAPA

    MOVL XAR0, #ZeroNetCLA

    MOVL *XAR0, ACC

    ADCDRV_1ch_CLA_INIT 1

    ADCDRV_1ch_CLA_INIT 6

    CNTL_2P2Z_CLA_INIT 1

    PWMDRV_1chHiRes_CLA_INIT 6

    LRETR

    ; CLA variable declaration

    _Kff .usect "CNTL_2P2Z_CLA_Coef",2,1,1

    _ledCurrentSet .usect "CpuToCla1MsgRAM",2,1,1

    _ledCurrentSetVal .usect "CpuToCla1MsgRAM",2,1,1

    _ledCurrentRampVal .usect "CpuToCla1MsgRAM",2,1,1

    _vFFvMin .usect "CpuToCla1MsgRAM",2,1,1

    _lineCurrentGain .usect "CpuToCla1MsgRAM",2,1,1

    _lineCurrentOffset .usect "CpuToCla1MsgRAM",2,1,1

    _syncDisableFlag .usect "CpuToCla1MsgRAM",1,1

    rmsCalcTimer .usect "CpuToCla1MsgRAM",1,1

    _ledMinDuty .usect "CpuToCla1MsgRAM",1,1

    vFFdivResult .usect "Cla1ToCpuMsgRAM",2,1,1

    _ledCurrentRef .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFastRms .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFiltRms .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFastIQ .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentFiltIQ .usect "Cla1ToCpuMsgRAM",2,1,1

    _currentDimmingRms .usect "Cla1ToCpuMsgRAM",2,1,1

    rmsTemp .usect "Cla1ToCpuMsgRAM",2,1,1

    ledDimMult .usect "Cla1ToCpuMsgRAM",2,1,1

    currentOffset .usect "Cla1ToCpuMsgRAM",2,1,1

    ctrlCurrentShift .usect "Cla1ToCpuMsgRAM",2,1,1

    currentFiltTable .usect "Cla1ToCpuMsgRAM",32,1,1

    _currentCalFlag .usect "CpuToCla1MsgRAM",1,1

    rmsCount .usect "Cla1ToCpuMsgRAM",1,1

    ledDimSet .usect "Cla1ToCpuMsgRAM",1,1

    _ledCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    rmsCalcTimerPrev .usect "Cla1ToCpuMsgRAM",1,1

    ADC1_ledCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC2_pfcVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC3_inputCurrentCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC6_ledVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    ADC9_hk12VoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    risingDelay .usect "Cla1ToCpuMsgRAM",1,1

    _ledVoltageCLA .usect "Cla1ToCpuMsgRAM",1,1

    currentFiltIndex .usect "Cla1ToCpuMsgRAM",1,1

    inHighStep .usect "Cla1ToCpuMsgRAM",1,1

    .sect "Cla1Prog"

    .align 2

    ; Make CLA variables visible to C environment

    .def _Cla1Prog_Start

    .def _Cla1Task2

    .def _Cla1Task4

    .def _Cla1Task8

    .def _Kff

    .def _currentFastIQ

    .def _currentFiltIQ

    .def _lineCurrentGain

    .def _lineCurrentOffset

    .def _syncDisableFlag

    .def ledDimSet

    .def _ledCurrentCLA

    .def _vFFvMin

    .def _ledVoltageCLA

    .def rmsCalcTimer

    .def ctrlCurrentShift

    .def _ledCurrentSet

    .def _ledCurrentSetVal

    .def _ledCurrentRampVal

    .def _ledCurrentRef

    .def _ledMinDuty

    .def _currentCalFlag

    .def _currentDimmingRms

    _Cla1Prog_Start:

    _Cla1Task1:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T1End:

    _Cla1Task2: ; Task 2 is triggered by the 28x core caused by an LED overcurrent.

    MNOP

    MNOP

    MNOP

    MSETFLG RNDF32=1

    MMOVF32 MR0, #0.0

    MMOV16 @_EPwm6Regs.CMPA.half.CMPA , MR0 ; Set LED PWM duty to 0

    MMOV16 @_EPwm6Regs.CMPA.half.CMPAHR , MR0

    MMOV16 MAR0, @_PWMDRV_1chHiRes_CLA_Duty6 ; Set address to MAR0 (takes 3 pipeline cycles to complete).

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+0, MR0 ; Zero out LED controller buffer.

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+2, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+4, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+6, MR0

    MMOV32 @_CNTL_2P2Z_CLA_DBUFF1+8, MR0

    MMOV32 *MAR0, MR0 ; Write 0 to duty macro.

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T2End:

    _Cla1Task3:

    MSTOP

    MNOP

    MNOP

    MNOP

    _Cla1T3End:

     

    _Cla1Task4: ; Task 4 is the main CLA interrupt triggered along with the 28x core interrupt.

    MNOP

    MNOP

    MNOP

    MSETFLG RNDF32=1

    ; MMOVXI MR0, #0x0002

    ; MMOV16 @_EPwm7Regs.AQCSFRC, MR0

     

     

    CLA_START

    MMOVZ16 MR0, @_AdcResult.ADCRESULT1 ; The ADC is set to auto convert all inputs at

    MMOV16 @ADC1_ledCurrentCLA, MR0 ; before during the interrupt routines. All

    MMOVZ16 MR0, @_AdcResult.ADCRESULT2 ; values are saved here so they are not being

    MMOV16 @ADC2_pfcVoltageCLA, MR0 ; read and changed at the same time later in the

    MMOVZ16 MR0, @_AdcResult.ADCRESULT3 ; cycle.

    MMOV16 @ADC3_inputCurrentCLA, MR0

    MMOVZ16 MR0, @_AdcResult.ADCRESULT6 ; cycle.

    MMOV16 @ADC6_ledVoltageCLA, MR0

    MMOVZ16 MR0, @_AdcResult.ADCRESULT9

    MMOV16 @ADC9_hk12VoltageCLA, MR0

    MMOV32 MR0, @_lineCurrentOffset

    MMOV32 MR1, @rmsTemp

    MUI16TOF32 MR2, @ADC3_inputCurrentCLA

    MSUBF32 MR2, MR2, MR0 ; Subtract the offset from the current value.

    MMPYF32 MR2, MR2, MR2 ; Square the current value.

    MADDF32 MR1, MR1, MR2 ; Add the squared value to a sum for rms calculation.

    MMOV32 @rmsTemp, MR1

    MMOVZ16 MR0, @rmsCount ; Keep count of RMS values stored and run the calculation

    MMOVIZ MR1, #0 ; when the counter is satisfied.

    MMOVXI MR1, #1

    MADD32 MR0, MR1, MR0

    MMOV16 @rmsCount, MR0

    MMOVZ16 MR0, @rmsCalcTimer