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.

DRV8301: Do not found the supporting files

Part Number: DRV8301
Other Parts Discussed in Thread: LAUNCHXL-F28069M, DRV8312, MOTORWARE, CONTROLSUITE

Respected sir,

I am interfacing BOOSTXL-DRV8301 with LAUNCHXL-F28069M. I have got the code for BLDC motor drive which I have attached herewith. There are some header files included in that project out of them I am unable to find the header files "BLDC_Sensored-Settings.h" and "BLDC_Sensored.h". Can you please tell me where I will found those files. Please share the directory link if possible.

Regards,

Nisha

// Include header files used in the main function

#include "PeripheralHeaderIncludes.h"
#include "BLDC_Sensored-Settings.h"
#include "IQmathLib.h"
#include "BLDC_Sensored.h"
#include <math.h>

#ifdef FLASH
#pragma CODE_SECTION(MainISR,"ramfuncs");
void MemCopy();
void InitFlash();
#endif

// Prototype statements for functions found within this file.
interrupt void MainISR(void);
void DeviceInit();

// State Machine function prototypes
//------------------------------------
// Alpha states
void A0(void); //state A0
void B0(void); //state B0
void C0(void); //state C0

// A branch states
void A1(void); //state A1
void A2(void); //state A2
void A3(void); //state A3

// B branch states
void B1(void); //state B1
void B2(void); //state B2
void B3(void); //state B3

// C branch states
void C1(void); //state C1
void C2(void); //state C2
void C3(void); //state C3

// Variable declarations
void (*Alpha_State_Ptr)(void); // Base States pointer
void (*A_Task_Ptr)(void); // State pointer A branch
void (*B_Task_Ptr)(void); // State pointer B branch
void (*C_Task_Ptr)(void); // State pointer C branch

// Used for running BackGround in flash, and ISR in RAM
extern Uint16 *RamfuncsLoadStart, *RamfuncsLoadEnd, *RamfuncsRunStart;

int16 VTimer0[4]; // Virtual Timers slaved off CPU Timer 0 (A events)
int16 VTimer1[4]; // Virtual Timers slaved off CPU Timer 1 (B events)
int16 VTimer2[4]; // Virtual Timers slaved off CPU Timer 2 (C events)
int16 SerialCommsTimer;

// Global variables used in this system


float32 T = 0.001/ISR_FREQUENCY; // Samping period (sec), see parameter.h

Uint32 IsrTicker = 0;
Uint16 BackTicker = 0;


Uint16 PreviousState;
Uint16 ClosedFlag = FALSE;

Uint32 VirtualTimer = 0;
Uint16 ILoopFlag = FALSE;
Uint16 SpeedLoopFlag = FALSE;
int16 DFuncDesired = (int16)_IQtoQ15(_IQ(0.1));
int16 DfuncTesting = (int16)_IQtoQ15(_IQ(0.12));

#if (BUILDLEVEL<= LEVEL2)
Uint32 CmtnPeriodTarget = 168;
Uint32 CmtnPeriodSetpt = 1024;
Uint32 RampDelay = 10;
#else
Uint32 CmtnPeriodTarget = 0x00000350;
Uint32 CmtnPeriodSetpt = 0x00000500;
Uint32 RampDelay = 10;
#endif

_iq CurrentSet = _IQ(0.1);
_iq DCbus_current = 0;
_iq SpeedRef=_IQ(0.3);
_iq tempIdc = 0;

_iq BemfA = 0;
_iq BemfB = 0;
_iq BemfC = 0;

int16 PwmDacCh1 = 0;
int16 PwmDacCh2 = 0;
int16 PwmDacCh3 = 0;

int16 DlogCh1 = 0;
int16 DlogCh2 = 0;
int16 DlogCh3 = 0;
int16 DlogCh4 = 0;


volatile Uint16 EnableFlag = FALSE;
Uint16 RunMotor = FALSE;

_iq BemfA_offset = _IQ15(0.0);
_iq BemfB_offset = _IQ15(0.0);
_iq BemfC_offset = _IQ15(0.0);
_iq IDC_offset = _IQ15(0.5);
_iq cal_filt_gain;

// Instance PID regulator to regulate the DC-bus current and speed
PID_GRANDO_CONTROLLER pid1_idc = {PID_TERM_DEFAULTS,PID_PARAM_DEFAULTS,PID_DATA_DEFAULTS};
PID_GRANDO_CONTROLLER pid1_spd = {PID_TERM_DEFAULTS,PID_PARAM_DEFAULTS,PID_DATA_DEFAULTS};

// Instance a PWM driver instance
PWMGEN pwm1 = PWMGEN_DEFAULTS;

// Instance a PWM DAC driver instance
PWMDAC pwmdac1 = PWMDAC_DEFAULTS;

// Instance a Hall effect driver
HALL3 hall1 = HALL3_DEFAULTS;

// Instance a ramp controller to smoothly ramp the frequency
RMPCNTL rc1 = RMPCNTL_DEFAULTS;

// Instance a RAMP2 Module
RMP2 rmp2 = RMP2_DEFAULTS;

// Instance a RAMP3 Module
RMP3 rmp3 = RMP3_DEFAULTS;

// Instance a MOD6 Module
MOD6CNT mod1 = MOD6CNT_DEFAULTS;

// Instance a IMPULSE Module
IMPULSE impl1 = IMPULSE_DEFAULTS;

// Instance a SPEED_PR Module
SPEED_MEAS_CAP speed1 = SPEED_MEAS_CAP_DEFAULTS;

// Create an instance of DATALOG Module
DLOG_4CH dlog = DLOG_4CH_DEFAULTS;

void main(void)
{

DeviceInit(); // Device Life support & GPIO

// Only used if running from FLASH
// Note that the variable FLASH is defined by the compiler

#ifdef FLASH
// Copy time critical code and Flash setup code to RAM
// The RamfuncsLoadStart, RamfuncsLoadEnd, and RamfuncsRunStart
// symbols are created by the linker. Refer to the linker files.
MemCopy(&RamfuncsLoadStart, &RamfuncsLoadEnd, &RamfuncsRunStart);

// Call Flash Initialization to setup flash waitstates
// This function must reside in RAM
InitFlash(); // Call the flash wrapper init function
#endif //(FLASH)


// Timing sync for background loops
// Timer period definitions found in device specific PeripheralHeaderIncludes.h
CpuTimer1Regs.PRD.all = mSec1; // A tasks
CpuTimer2Regs.PRD.all = mSec5; // B tasks

// Tasks State-machine init
Alpha_State_Ptr = &A0;
A_Task_Ptr = &A1;
B_Task_Ptr = &B1;


// Waiting for enable flag set
while (EnableFlag==FALSE)
{
BackTicker++;
}

// Initialize all the Device Peripherals:
// This function is found in DSP280x_CpuTimers.c
InitCpuTimers();
// Configure CPU-Timer 0 to interrupt every ISR Period:
// 60MHz CPU Freq, ISR Period (in uSeconds)
// This function is found in DSP280x_CpuTimers.c
ConfigCpuTimer(&CpuTimer0, SYSTEM_FREQUENCY, 1000/ISR_FREQUENCY);
StartCpuTimer0();

// Reassign ISRs.
// Reassign the PIE vector for TINT0 to point to a different
// ISR then the shell routine found in DSP280x_DefaultIsr.c.
// This is done if the user does not want to use the shell ISR routine
// but instead wants to use their own ISR.

EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.TINT0 = &MainISR;
EDIS; // This is needed to disable write to EALLOW protected registers

// Enable PIE group 1 interrupt 7 for TINT0
PieCtrlRegs.PIEIER1.all = M_INT7;

// Enable CPU INT1 for TINT0:
IER |= M_INT1;
// Enable Global realtime interrupt DBGM

// Initialize PWM module
pwm1.PeriodMax = (SYSTEM_FREQUENCY/PWM_FREQUENCY)*1000; // Asymmetric PWM
pwm1.DutyFunc = ALIGN_DUTY; // DutyFunc = Q15
BLDCPWM_INIT_MACRO(pwm1)

// Initialize PWMDAC module
pwmdac1.PeriodMax = 500; // 3000->10kHz, 1500->20kHz, 1000-> 30kHz, 500->60kHz
pwmdac1.PwmDacInPointer0 = &PwmDacCh1;
pwmdac1.PwmDacInPointer1 = &PwmDacCh2;
pwmdac1.PwmDacInPointer2 = &PwmDacCh3;
PWMDAC_INIT_MACRO(pwmdac1)


// Initialize Hall module
hall1.DebounceAmount = 0;
hall1.Revolutions = -3;
HALL3_INIT_MACRO(hall1)

// Initialize DATALOG module
dlog.iptr1 = &DlogCh1;
dlog.iptr2 = &DlogCh2;
dlog.iptr3 = &DlogCh3;
dlog.iptr4 = &DlogCh4;
dlog.trig_value = 0x1;
dlog.size = 0x0C8;
dlog.prescalar = 25;
dlog.init(&dlog);

// Initialize ADC module (F2803XILEG_VDC.H)
ADC_MACRO_INIT()

// Initialize the SPEED_PR module
speed1.InputSelect = 0;
speed1.BaseRpm = 120*(BASE_FREQ/POLES);
speed1.SpeedScaler = (Uint32)(ISR_FREQUENCY/(1*BASE_FREQ*0.001));

// Initialize RMPCNTL module
rc1.RampDelayMax = 25;
rc1.RampLowLimit = _IQ(0.0);
rc1.RampHighLimit = _IQ(1.0);

// Initialize RMP2 module
rmp2.Out = ALIGN_DUTY;
rmp2.Ramp2Delay = 0x0050;
rmp2.Ramp2Max = 0x7FFF;
rmp2.Ramp2Min = 0x000F;

// Initialize RMP3 module
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
rmp3.Out = CmtnPeriodSetpt;
rmp3.Ramp3Min = 0x00000010;

// Initialize the PID_GRANDO_CONTROLLER module for dc-bus current
pid1_idc.param.Kp = _IQ(3.176*BASE_CURRENT/BASE_VOLTAGE);
pid1_idc.param.Kr = _IQ(1.0);
pid1_idc.param.Ki = _IQ(T/0.0005);
pid1_idc.param.Kd = _IQ(0/T);
pid1_idc.param.Km = _IQ(1.0);
pid1_idc.param.Umax = _IQ(0.95);
pid1_idc.param.Umin = _IQ(0.0);

// Initialize the PID_GRANDO_CONTROLLER module for Speed
pid1_spd.param.Kp = _IQ(0.75);
pid1_spd.param.Kr = _IQ(1.0);
pid1_spd.param.Ki = _IQ(T/0.3);
pid1_spd.param.Kd = _IQ(0/T);
pid1_spd.param.Km = _IQ(1.0);
pid1_spd.param.Umax = _IQ(0.95);
pid1_spd.param.Umin = _IQ(0.0);

// Initialize the current offset calibration filter
cal_filt_gain = _IQ15(T/(T+TC_CAL));

// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM


// IDLE loop. Just sit and loop forever:
for(;;) //infinite loop
{
BackTicker++;

// State machine entry & exit point
//===========================================================
(*Alpha_State_Ptr)(); // jump to an Alpha state (A0,B0,...)
//===========================================================

}
} //END MAIN CODE

//=================================================================================
// STATE-MACHINE SEQUENCING AND SYNCRONIZATION FOR SLOW BACKGROUND TASKS
//=================================================================================

//--------------------------------- FRAMEWORK -------------------------------------
void A0(void)
{
// loop rate synchronizer for A-tasks
if(CpuTimer0Regs.TCR.bit.TIF == 1)
{
CpuTimer0Regs.TCR.bit.TIF = 1; // clear flag

//-----------------------------------------------------------
(*A_Task_Ptr)(); // jump to an A Task (A1,A2,A3,...)
//-----------------------------------------------------------

VTimer0[0]++; // virtual timer 0, instance 0 (spare)
SerialCommsTimer++;
}

Alpha_State_Ptr = &B0; // Comment out to allow only A tasks
}

void B0(void)
{
// loop rate synchronizer for B-tasks
if(CpuTimer1Regs.TCR.bit.TIF == 1)
{
CpuTimer1Regs.TCR.bit.TIF = 1; // clear flag

//-----------------------------------------------------------
(*B_Task_Ptr)(); // jump to a B Task (B1,B2,B3,...)
//-----------------------------------------------------------
VTimer1[0]++; // virtual timer 1, instance 0 (spare)
}

Alpha_State_Ptr = &A0; // Allow C state tasks
}

//=================================================================================
// A - TASKS (executed in every 1 msec)
//=================================================================================
//--------------------------------------------------------
void A1(void) // SPARE (not used)
//--------------------------------------------------------
{
if (EnableFlag == FALSE)
{
RunMotor = FALSE;

EALLOW;
EPwm1Regs.TZFRC.bit.OST=1;
EPwm2Regs.TZFRC.bit.OST=1;
EPwm3Regs.TZFRC.bit.OST=1;
EDIS;
}
else if((EnableFlag == TRUE) && (RunMotor == FALSE))
{
hall1.CmtnTrigHall = 0;
hall1.CapCounter = 0;
hall1.DebounceCount = 0;
hall1.DebounceAmount = 0;
hall1.HallGpio = 0;
hall1.HallGpioBuffer = 0;
hall1.HallGpioAccepted = 0;
hall1.EdgeDebounced = 0;
hall1.HallMap[0] = 0;
hall1.HallMap[1] = 0;
hall1.HallMap[2] = 0;
hall1.HallMap[3] = 0;
hall1.HallMap[4] = 0;
hall1.HallMap[5] = 0;
hall1.CapFlag = 0;
hall1.StallCount = 0xFFFF;
hall1.HallMapPointer = 0;
hall1.Revolutions = -3;

speed1.InputSelect = 0;
speed1.NewTimeStamp = 0;
speed1.OldTimeStamp = 0;
speed1.EventPeriod = 0;

rc1.EqualFlag = 0;
rc1.RampDelayCount = 0;
rc1.SetpointValue = 0;
rc1.TargetValue = 0;

rmp2.DesiredInput = 0;
rmp2.Ramp2DelayCount = 0;
rmp2.Out = ALIGN_DUTY;

rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Out = CmtnPeriodSetpt;
rmp3.Ramp3DelayCount = 0;
rmp3.Ramp3DoneFlag = 0;

impl1.Counter = 0;
impl1.Out = 0;

mod1.Counter = 0;

ClosedFlag = FALSE;
ILoopFlag = FALSE;
SpeedLoopFlag = FALSE;

pid1_idc.data.d1 = 0;
pid1_idc.data.d2 = 0;
pid1_idc.data.i1 = 0;
pid1_idc.data.ud = 0;
pid1_idc.data.ui = 0;
pid1_idc.data.up = 0;
pid1_idc.data.v1 = 0;
pid1_idc.data.w1 = 0;
pid1_idc.term.Out = 0;

pid1_spd.data.d1 = 0;
pid1_spd.data.d2 = 0;
pid1_spd.data.i1 = 0;
pid1_spd.data.ud = 0;
pid1_spd.data.ui = 0;
pid1_spd.data.up = 0;
pid1_spd.data.v1 = 0;
pid1_spd.data.w1 = 0;
pid1_spd.term.Out = 0;

RunMotor = TRUE;

EALLOW;
EPwm1Regs.TZCLR.bit.OST=1;
EPwm2Regs.TZCLR.bit.OST=1;
EPwm3Regs.TZCLR.bit.OST=1;
EDIS;
}

//-------------------
//the next time CpuTimer0 'counter' reaches Period value go to A2
A_Task_Ptr = &A2;
//-------------------
}

//-----------------------------------------------------------------
void A2(void) // SPARE (not used)
//-----------------------------------------------------------------
{

//-------------------
//the next time CpuTimer0 'counter' reaches Period value go to A3
A_Task_Ptr = &A3;
//-------------------
}

//-----------------------------------------
void A3(void) // SPARE (not used)
//-----------------------------------------
{

//-----------------
//the next time CpuTimer0 'counter' reaches Period value go to A1
A_Task_Ptr = &A1;
//-----------------
}

//=================================================================================
// B - TASKS (executed in every 5 msec)
//=================================================================================

//----------------------------------- USER ----------------------------------------

//----------------------------------------
void B1(void) // Toggle GPIO-00
//----------------------------------------
{
//GpioDataRegs.GPBTOGGLE.bit.GPIO34 = 1;
//-----------------
//the next time CpuTimer1 'counter' reaches Period value go to B2
B_Task_Ptr = &B2;
//-----------------
}

//----------------------------------------
void B2(void) // SPARE
//----------------------------------------
{

//-----------------
//the next time CpuTimer1 'counter' reaches Period value go to B3
B_Task_Ptr = &B3;
//-----------------
}

//----------------------------------------
void B3(void) // SPARE
//----------------------------------------
{

//-----------------
//the next time CpuTimer1 'counter' reaches Period value go to B1
B_Task_Ptr = &B1;
//-----------------
}

// ==============================================================================
// =============================== MAIN ISR =====================================
// ==============================================================================


interrupt void MainISR(void)

{
// Verifying the ISR
IsrTicker++;

if(RunMotor)
{
// =============================== LEVEL 1 ======================================
// This Level describes the steps for a minimum system check-out which confirms
// operation of system interrupts, some peripheral & target independent modules
// and one peripheral dependent module.
// ==============================================================================

#if (BUILDLEVEL==LEVEL1)

// ------------------------------------------------------------------------------
// Connect inputs of the RMP3 module and call the RAMP Control 3 macro.
// ------------------------------------------------------------------------------
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
RC3_MACRO(rmp3)

// ------------------------------------------------------------------------------
// Connect inputs of the IMPULSE module and call the Impulse macro.
// ------------------------------------------------------------------------------
impl1.Period = rmp3.Out;
IMPULSE_MACRO(impl1)

// ------------------------------------------------------------------------------
// Connect inputs of the MOD6 module and call the Mod 6 counter macro.
// ------------------------------------------------------------------------------
mod1.TrigInput = impl1.Out;
MOD6CNT_MACRO(mod1)

// ------------------------------------------------------------------------------
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update macro.
// ------------------------------------------------------------------------------
pwm1.CmtnPointer = (int16)mod1.Counter;
pwm1.DutyFunc = DfuncTesting;
BLDCPWM_MACRO(pwm1)

#endif // (BUILDLEVEL==LEVEL1)

// =============================== LEVEL 2 ======================================
// Level 2 verifies the analog-to-digital conversion, offset compensation,
// open loop motor operation.
// ==============================================================================

#if (BUILDLEVEL==LEVEL2)

// ------------------------------------------------------------------------------
// ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
// ------------------------------------------------------------------------------
#ifdef F2806x_DEVICE_H
BemfA = ((AdcResult.ADCRESULT3)*0.00024414)-BemfA_offset;
BemfB = ((AdcResult.ADCRESULT4)*0.00024414)-BemfB_offset;
BemfC = ((AdcResult.ADCRESULT5)*0.00024414)-BemfC_offset;
DCbus_current = ((AdcResult.ADCRESULT6)*0.00024414-IDC_offset)*2; //1.65V offset added on DRV8312 board.
#endif

#ifdef DSP2803x_DEVICE_H
BemfA = _IQ15toIQ((AdcResult.ADCRESULT4<<3)-BemfA_offset);
BemfB = _IQ15toIQ((AdcResult.ADCRESULT5<<3)-BemfB_offset);
BemfC = _IQ15toIQ((AdcResult.ADCRESULT6<<3)-BemfC_offset);
DCbus_current = _IQ15toIQ((AdcResult.ADCRESULT7<<3)-IDC_offset)<<1; //1.65V offset added on DRV8312 board.
#endif

// ------------------------------------------------------------------------------
// Connect inputs of the RMP3 module and call the Ramp control 3 macro.
// ------------------------------------------------------------------------------
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
RC3_MACRO(rmp3)

// ------------------------------------------------------------------------------
// Connect inputs of the IMPULSE module and call the Impulse macro.
// ------------------------------------------------------------------------------
impl1.Period = rmp3.Out;
IMPULSE_MACRO(impl1)

// ------------------------------------------------------------------------------
// Connect inputs of the MOD6 module and call the Modulo 6 counter macro.
// ------------------------------------------------------------------------------
mod1.TrigInput = impl1.Out;
MOD6CNT_MACRO(mod1)

// ------------------------------------------------------------------------------
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update macro.
// ------------------------------------------------------------------------------
pwm1.CmtnPointer = (int16)mod1.Counter;
pwm1.DutyFunc = DfuncTesting;
BLDCPWM_MACRO(pwm1)

// ------------------------------------------------------------------------------
// Connect inputs of the PWMDAC module
// ------------------------------------------------------------------------------
PwmDacCh1 = _IQtoQ15(BemfA);
PwmDacCh2 = _IQtoQ15(BemfB);
PwmDacCh3 = _IQtoQ15(BemfC);
// PwmDacCh3 = (int16)(mod1.Counter * 4096.0L);

// ------------------------------------------------------------------------------
// Connect inputs of the DATALOG module
// ------------------------------------------------------------------------------
DlogCh1 = (int16)mod1.Counter;
DlogCh2 = _IQtoQ15(BemfA);
DlogCh3 = _IQtoQ15(BemfB);
DlogCh4 = _IQtoQ15(BemfC);

#endif // (BUILDLEVEL==LEVEL2)

// =============================== LEVEL 3 ======================================
// Level 3 auto-calculates the current sensor offset calibration
// ==============================================================================

#if (BUILDLEVEL==LEVEL3)

_iq IDCfdbk;
// ------------------------------------------------------------------------------
// Measure DC Bus current, subtract the offset and normalize from (-0.5,+0.5) to (-1,+1).
// ------------------------------------------------------------------------------
#ifdef F2806x_DEVICE_H
IDCfdbk=((AdcResult.ADCRESULT6)*0.00024414-IDC_offset)*2; // DC Bus curr.
#endif // ((ADCmeas(q12)/2^12)-0.5)*2

#ifdef DSP2803x_DEVICE_H
IDCfdbk=_IQ15toIQ((AdcResult.ADCRESULT7<<3)-IDC_offset)<<1;
#endif

// ------------------------------------------------------------------------------
// LPF to average the calibration offsets
// Use the offsets calculated here to initialize IDC_offset
// so that they are used for the remaining build levels
// ------------------------------------------------------------------------------
IDC_offset = _IQ15mpy(cal_filt_gain,_IQtoIQ15(IDCfdbk)) + IDC_offset;

// ------------------------------------------------------------------------------
// force all PWMs to 0% duty cycle
// ------------------------------------------------------------------------------
EPwm1Regs.CMPA.half.CMPA=0; // PWM 1A - PhaseA
EPwm2Regs.CMPA.half.CMPA=0; // PWM 2A - PhaseB
EPwm3Regs.CMPA.half.CMPA=0; // PWM 3A - PhaseC

#endif // (BUILDLEVEL==LEVEL3)

// =============================== LEVEL 4 ======================================
// Level 4 describes the closed-loop operation of sensored trapezoidal
// drive of BLDC motor using Hall sensor.
// ==============================================================================

#if (BUILDLEVEL==LEVEL4)

// ------------------------------------------------------------------------------
// ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
// ------------------------------------------------------------------------------
#ifdef F2806x_DEVICE_H
BemfA = ((AdcResult.ADCRESULT3)*0.00024414)-BemfA_offset;
BemfB = ((AdcResult.ADCRESULT4)*0.00024414)-BemfB_offset;
BemfC = ((AdcResult.ADCRESULT5)*0.00024414)-BemfC_offset;
DCbus_current = ((AdcResult.ADCRESULT6)*0.00024414-IDC_offset)*2; //1.65V offset added on DRV8312 board.
#endif

#ifdef DSP2803x_DEVICE_H
BemfA = _IQ15toIQ((AdcResult.ADCRESULT4<<3)-BemfA_offset);
BemfB = _IQ15toIQ((AdcResult.ADCRESULT5<<3)-BemfB_offset);
BemfC = _IQ15toIQ((AdcResult.ADCRESULT6<<3)-BemfC_offset);
DCbus_current = _IQ15toIQ((AdcResult.ADCRESULT7<<3)-IDC_offset)<<1; //1.65V offset added on DRV8312 board.
#endif

// ------------------------------------------------------------------------------
// Connect inputs of the RMP3 module and call the Ramp control 3 macro.
// ------------------------------------------------------------------------------
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
RC3_MACRO(rmp3)

// ------------------------------------------------------------------------------
// Connect inputs of the IMPULSE module and call the Impulse macro.
// ------------------------------------------------------------------------------
impl1.Period = rmp3.Out;
IMPULSE_MACRO(impl1)

// ------------------------------------------------------------------------------
// Connect inputs of the HALL module and call the Hall sensor read macro.
// ------------------------------------------------------------------------------
hall1.HallMapPointer = (int16)mod1.Counter;
HALL3_READ_MACRO(hall1)
if (hall1.Revolutions>=0)
ClosedFlag=TRUE;

// ------------------------------------------------------------------------------
// Connect inputs of the MOD6 module and call the Modulo 6 counter macro.
// ------------------------------------------------------------------------------
if (ClosedFlag==FALSE)
{
mod1.TrigInput = impl1.Out;
mod1.Counter = (int32)hall1.HallMapPointer;
}
else
mod1.TrigInput = (int32)hall1.CmtnTrigHall;

MOD6CNT_MACRO(mod1)

// ------------------------------------------------------------------------------
// Connect inputs of the RMP2 module and call the Ramp control 2 macro.
// ------------------------------------------------------------------------------
rmp2.DesiredInput = DFuncDesired;
RC2_MACRO(rmp2)

// ------------------------------------------------------------------------------
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update macro.
// ------------------------------------------------------------------------------
if (ClosedFlag==TRUE) {
if (hall1.CmtnTrigHall==0x7FFF) {

PreviousState = pwm1.CmtnPointer;

// Comment the following if-else-if statements in case of
// inverted Hall logics for commutation states.
if (hall1.HallGpioAccepted==5)
pwm1.CmtnPointer = 0;

else if (hall1.HallGpioAccepted==1)
pwm1.CmtnPointer = 1;

else if (hall1.HallGpioAccepted==3)
pwm1.CmtnPointer = 2;

else if (hall1.HallGpioAccepted==2)
pwm1.CmtnPointer = 3;

else if (hall1.HallGpioAccepted==6)
pwm1.CmtnPointer = 4;

else if (hall1.HallGpioAccepted==4)
pwm1.CmtnPointer = 5;

/*
// Comment the following if-else-if statements in case of
// non-inverted Hall logics for commutation states.
if (hall1.HallGpioAccepted==2)
pwm1.CmtnPointer = 0;

else if (hall1.HallGpioAccepted==6)
pwm1.CmtnPointer = 1;

else if (hall1.HallGpioAccepted==4)
pwm1.CmtnPointer = 2;

else if (hall1.HallGpioAccepted==5)
pwm1.CmtnPointer = 3;

else if (hall1.HallGpioAccepted==1)
pwm1.CmtnPointer = 4;

else if (hall1.HallGpioAccepted==3)
pwm1.CmtnPointer = 5;
*/

} //hall1.CmtnTrigHall == 0x7FFF
} // ClosedFlag==TRUE
else
pwm1.CmtnPointer = (int16)mod1.Counter;

pwm1.DutyFunc = rmp2.Out;
BLDCPWM_MACRO(pwm1)

// ------------------------------------------------------------------------------
// Connect inputs of the SPEED_PR module and call the speed calculation macro.
// ------------------------------------------------------------------------------
if ((pwm1.CmtnPointer==5)&&(PreviousState==4)&&(hall1.CmtnTrigHall==0x7FFF))
{
speed1.TimeStamp = VirtualTimer;
SPEED_PR_MACRO(speed1);
}

// ------------------------------------------------------------------------------
// Connect inputs of the PWMDAC module
// ------------------------------------------------------------------------------
PwmDacCh1 = (int16)(mod1.Counter * 4096.0L);
PwmDacCh2 = (int16)(hall1.HallGpioAccepted * 4096.0L);
PwmDacCh3 = _IQtoQ15(BemfA);

// ------------------------------------------------------------------------------
// Connect inputs of the DATALOG module
// ------------------------------------------------------------------------------
DlogCh1 = (int16)mod1.Counter;
DlogCh2 = hall1.HallGpioAccepted;
DlogCh3 = _IQtoQ15(BemfA);
DlogCh4 = _IQtoQ15(BemfB);

#endif // (BUILDLEVEL==LEVEL4)

// =============================== LEVEL 5 ======================================
// Level 5 verifies the closed current loop and current PI controller.
// ==============================================================================

#if (BUILDLEVEL==LEVEL5)

// ------------------------------------------------------------------------------
// Connect inputs of the RMP3 module and call the Ramp control 3 macro.
// ------------------------------------------------------------------------------
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
RC3_MACRO(rmp3)

// ------------------------------------------------------------------------------
// Connect inputs of the IMPULSE module and call the Impulse macro.
// ------------------------------------------------------------------------------
impl1.Period = rmp3.Out;
IMPULSE_MACRO(impl1)

// ------------------------------------------------------------------------------
// Connect inputs of the HALL module and call the Hall sensor read macro.
// ------------------------------------------------------------------------------
hall1.HallMapPointer = (int16)mod1.Counter;
HALL3_READ_MACRO(hall1)

if (hall1.Revolutions>=0)
ClosedFlag=TRUE;

// ------------------------------------------------------------------------------
// Connect inputs of the MOD6 module and call the Modulo 6 counter macro.
// ------------------------------------------------------------------------------
if (ClosedFlag==FALSE) {
mod1.TrigInput = impl1.Out;
mod1.Counter = (int32)hall1.HallMapPointer;
}
else
mod1.TrigInput = (int32)hall1.CmtnTrigHall;

MOD6CNT_MACRO(mod1)

// ------------------------------------------------------------------------------
// Connect inputs of the RMP2 module and call the Ramp control 2 macro.
// ------------------------------------------------------------------------------
rmp2.DesiredInput = DFuncDesired;
RC2_MACRO(rmp2)

// ------------------------------------------------------------------------------
// Connect inputs of the PID_GRANDO_CONTROLLER module and call the PID controller macro.
// ------------------------------------------------------------------------------
tempIdc=pid1_idc.term.Fbk;
pid1_idc.term.Ref = CurrentSet;
#ifdef F2806x_DEVICE_H
pid1_idc.term.Fbk = ((AdcResult.ADCRESULT6)*0.00024414-IDC_offset)*2; //1.65V offset added on DRV8312 board.
// pid1_idc.term.Fbk = -(((AdcResult.ADCRESULT6)*0.00024414-IDC_offset)*2); //1.65V offset added on DRV8312 board.
#endif

#ifdef DSP2803x_DEVICE_H
pid1_idc.term.Fbk = _IQ15toIQ((AdcResult.ADCRESULT7<<3)-IDC_offset)<<1; //1.65V offset added on DRV8312 board.
// pid1_idc.term.Fbk = -(_IQ15toIQ((AdcResult.ADCRESULT7<<3)-IDC_offset)<<1); //1.65V offset added on DRV8312 board.
#endif

if(pid1_idc.term.Fbk<0) pid1_idc.term.Fbk=tempIdc; // Eliminate negative values
PID_GR_MACRO(pid1_idc)

// ------------------------------------------------------------------------------
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update macro.
// ------------------------------------------------------------------------------
// Switch from fixed duty-cycle or controlled Idc duty-cycle by ILoopFlag variable

if (ILoopFlag == FALSE)
{ pwm1.DutyFunc = rmp2.Out; // fixed duty-cycle
pid1_idc.data.ui=0;
pid1_idc.data.i1=0;
}
else
{
pwm1.DutyFunc = _IQtoQ15(pid1_idc.term.Out); // controlled Idc duty-cycle
}

if (ClosedFlag==TRUE)
{
if (hall1.CmtnTrigHall==0x7FFF)
{

PreviousState = pwm1.CmtnPointer;


// Comment the following if-else-if statements in case of
// inverted Hall logics for commutation states. r
if (hall1.HallGpioAccepted==5)
pwm1.CmtnPointer = 0;

else if (hall1.HallGpioAccepted==1)
pwm1.CmtnPointer = 1;

else if (hall1.HallGpioAccepted==3)
pwm1.CmtnPointer = 2;

else if (hall1.HallGpioAccepted==2)
pwm1.CmtnPointer = 3;

else if (hall1.HallGpioAccepted==6)
pwm1.CmtnPointer = 4;

else if (hall1.HallGpioAccepted==4)
pwm1.CmtnPointer = 5;
/*

// Comment the following if-else-if statements in case of
// non-inverted Hall logics for commutation states.

if (hall1.HallGpioAccepted==2)
pwm1.CmtnPointer = 0;

else if (hall1.HallGpioAccepted==6)
pwm1.CmtnPointer = 1;

else if (hall1.HallGpioAccepted==4)
pwm1.CmtnPointer = 2;

else if (hall1.HallGpioAccepted==5)
pwm1.CmtnPointer = 3;

else if (hall1.HallGpioAccepted==1)
pwm1.CmtnPointer = 4;

else if (hall1.HallGpioAccepted==3)
pwm1.CmtnPointer = 5;
*/
} // Hall1.CmtnTrigHall == 0x7FFF
} // ClosedFlag==TRUE

else
pwm1.CmtnPointer = (int16)mod1.Counter;

BLDCPWM_MACRO(pwm1)
// ------------------------------------------------------------------------------
// Connect inputs of the SPEED_PR module and call the speed calculation macro.
// ------------------------------------------------------------------------------
if ((pwm1.CmtnPointer==5)&&(PreviousState==4)&&(hall1.CmtnTrigHall==0x7FFF))
{
speed1.TimeStamp = VirtualTimer;
SPEED_PR_MACRO(speed1)
}

// ------------------------------------------------------------------------------
// Connect inputs of the PWMDAC module
// ------------------------------------------------------------------------------
PwmDacCh1 = (int16)(mod1.Counter * 4096.0L);
PwmDacCh2 = (int16)(hall1.HallGpioAccepted * 4096.0L);
PwmDacCh3 = _IQtoQ15(speed1.Speed);
// PwmDacCh3 = _IQtoQ15(pid1_idc.term.Fbk);

// ------------------------------------------------------------------------------
// Connect inputs of the DATALOG module
// ------------------------------------------------------------------------------
DlogCh1 = pwm1.CmtnPointer;
DlogCh2 = hall1.HallGpioAccepted;
DlogCh3 = _IQtoQ15(speed1.Speed);
DlogCh4 = _IQtoQ15(pid1_idc.term.Fbk);

#endif // (BUILDLEVEL==LEVEL5)


// =============================== LEVEL 6 ======================================
// Level 6 verifies the closed speed loop and speed PI controller.
// ==============================================================================

#if (BUILDLEVEL==LEVEL6)

// ------------------------------------------------------------------------------
// ADC conversion and offset adjustment (observing back-emfs is optinal for this prj.)
// ------------------------------------------------------------------------------
#ifdef F2806x_DEVICE_H
BemfA = ((AdcResult.ADCRESULT3)*0.00024414)-BemfA_offset;
BemfB = ((AdcResult.ADCRESULT4)*0.00024414)-BemfB_offset;
BemfC = ((AdcResult.ADCRESULT5)*0.00024414)-BemfC_offset;
// DCbus_current = ((AdcResult.ADCRESULT6)*0.00024414-IDC_offset)*2; //1.65V offset added on DRV8312 board.
#endif

#ifdef DSP2803x_DEVICE_H
BemfA = _IQ15toIQ((AdcResult.ADCRESULT4<<3)-BemfA_offset);
BemfB = _IQ15toIQ((AdcResult.ADCRESULT5<<3)-BemfB_offset);
BemfC = _IQ15toIQ((AdcResult.ADCRESULT6<<3)-BemfC_offset);
// DCbus_current = _IQ15toIQ((AdcResult.ADCRESULT7<<3)-IDC_offset)<<1; //1.65V offset added on DRV8312 board.
#endif

// ------------------------------------------------------------------------------
// Connect inputs of the RMP module and call the Ramp control macro.
// ------------------------------------------------------------------------------
rc1.TargetValue = SpeedRef;
RC_MACRO(rc1)

// ------------------------------------------------------------------------------
// Connect inputs of the RMP3 module and call the Ramp control 3 macro.
// ------------------------------------------------------------------------------
rmp3.DesiredInput = CmtnPeriodTarget;
rmp3.Ramp3Delay = RampDelay;
RC3_MACRO(rmp3)

// ------------------------------------------------------------------------------
// Connect inputs of the IMPULSE module and call the Impulse macro.
// ------------------------------------------------------------------------------
impl1.Period = rmp3.Out;
IMPULSE_MACRO(impl1)

// ------------------------------------------------------------------------------
// Connect inputs of the HALL module and call the Hall sensor read macro.
// ------------------------------------------------------------------------------
hall1.HallMapPointer = (int16)mod1.Counter;
HALL3_READ_MACRO(hall1)

if (hall1.Revolutions>=0)
ClosedFlag=TRUE;

// ------------------------------------------------------------------------------
// Connect inputs of the MOD6 module and call the Modulo 6 counter macro.
// ------------------------------------------------------------------------------
if (ClosedFlag==FALSE)
{
mod1.TrigInput = impl1.Out;
mod1.Counter = (int32)hall1.HallMapPointer;
}
else
mod1.TrigInput = (int32)hall1.CmtnTrigHall;

MOD6CNT_MACRO(mod1)

// ------------------------------------------------------------------------------
// Connect inputs of the RMP2 module and call the Ramp control 2 macro.
// ------------------------------------------------------------------------------
rmp2.DesiredInput = DFuncDesired;
RC2_MACRO(rmp2)

// ------------------------------------------------------------------------------
// Connect inputs of the PID_REG3 module and call the PID speed controller
// macro.
// ------------------------------------------------------------------------------
pid1_spd.term.Ref = rc1.SetpointValue;
pid1_spd.term.Fbk = speed1.Speed;
PID_GR_MACRO(pid1_spd)

// ------------------------------------------------------------------------------
// Set the speed closed loop flag once the speed is built up to a desired value.
// ------------------------------------------------------------------------------
if ( rc1.EqualFlag == 0x7FFFFFFF )
{ SpeedLoopFlag = TRUE;}

// ------------------------------------------------------------------------------
// Connect inputs of the PWM_DRV module and call the PWM signal generation
// update macro.
// ------------------------------------------------------------------------------
// Switch from fixed duty-cycle or controlled Speed duty-cycle by SpeedLoopFlag variable
if (SpeedLoopFlag == FALSE)
{
pwm1.DutyFunc = rmp2.Out; // fixed duty-cycle
pid1_spd.data.ui=0;
pid1_spd.data.i1=0;
}
else
{
pwm1.DutyFunc = (int16)_IQtoQ15(pid1_spd.term.Out); // controlled Speed duty-cycle
}

if (ClosedFlag==TRUE)
{
if (hall1.CmtnTrigHall==0x7FFF)
{

PreviousState = pwm1.CmtnPointer;


// Comment the following if-else-if statements in case of
// inverted Hall logics for commutation states.
if (hall1.HallGpioAccepted==5)
pwm1.CmtnPointer = 0;

else if (hall1.HallGpioAccepted==1)
pwm1.CmtnPointer = 1;

else if (hall1.HallGpioAccepted==3)
pwm1.CmtnPointer = 2;

else if (hall1.HallGpioAccepted==2)
pwm1.CmtnPointer = 3;

else if (hall1.HallGpioAccepted==6)
pwm1.CmtnPointer = 4;

else if (hall1.HallGpioAccepted==4)
pwm1.CmtnPointer = 5;

/*

// Comment the following if-else-if statements in case of
// non-inverted Hall logics for commutation states.
if (hall1.HallGpioAccepted==2)
pwm1.CmtnPointer = 0;

else if (hall1.HallGpioAccepted==6)
pwm1.CmtnPointer = 1;

else if (hall1.HallGpioAccepted==4)
pwm1.CmtnPointer = 2;

else if (hall1.HallGpioAccepted==5)
pwm1.CmtnPointer = 3;

else if (hall1.HallGpioAccepted==1)
pwm1.CmtnPointer = 4;

else if (hall1.HallGpioAccepted==3)
pwm1.CmtnPointer = 5;
*/

} // Hall1.CmtnTrigHall == 0x7FFF
} // ClosedFlag==TRUE

else
pwm1.CmtnPointer = (int16)mod1.Counter;

BLDCPWM_MACRO(pwm1)

// ------------------------------------------------------------------------------
// Connect inputs of the SPEED_PR module and call the speed calculation macro.
// ------------------------------------------------------------------------------
if ((pwm1.CmtnPointer==5)&&(PreviousState==4)&&(hall1.CmtnTrigHall==0x7FFF))
{
speed1.TimeStamp = VirtualTimer;
SPEED_PR_MACRO(speed1)
}
// ------------------------------------------------------------------------------
// Connect inputs of the PWMDAC module
// ------------------------------------------------------------------------------
PwmDacCh1 = (int16)(mod1.Counter * 4096.0L);
PwmDacCh2 = _IQtoQ15(BemfA);
PwmDacCh3 = _IQtoQ15(BemfB);
// PwmDacCh3 = _IQtoQ15(BemfC);

// ------------------------------------------------------------------------------
// Connect inputs of the DATALOG module
// ------------------------------------------------------------------------------

DlogCh1 = pwm1.CmtnPointer;
DlogCh2 = _IQtoQ15(BemfA);
DlogCh3 = _IQtoQ15(BemfB);
DlogCh4 = _IQtoQ15(BemfC);


#endif // (BUILDLEVEL==LEVEL6)
}//end if(RunMotor)


// ------------------------------------------------------------------------------
// Call the PWMDAC update macro.
// ------------------------------------------------------------------------------
PWMDAC_MACRO(pwmdac1)

// ------------------------------------------------------------------------------
// Call the DATALOG update function.
// ------------------------------------------------------------------------------
dlog.update(&dlog);

// ------------------------------------------------------------------------------
// Increase virtual timer and force 15 bit wrap around
// ------------------------------------------------------------------------------
VirtualTimer++;
VirtualTimer &= 0x00007FFF;

#if (DSP2803x_DEVICE_H==1)||(F2806x_DEVICE_H==1)
/* Enable more interrupts from this timer
EPwm1Regs.ETCLR.bit.INT = 1;

// Acknowledge interrupt to recieve more interrupts from PIE group 3
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
*/

// Acknowledge interrupt to recieve more interrupts from PIE group 1
PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;

#endif

#if (DSP280x_DEVICE_H==1)
// Enable more interrupts from this timer
EPwm1Regs.ETCLR.bit.INT = 1;

// Acknowledge interrupt to recieve more interrupts from PIE group 3
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
#endif


}// ISR Ends Here


//===========================================================================
// No more.
//===========================================================================