Tool/software: Code Composer Studio
Hello,
I am trying to generate Interrupt service routine (ISR) from ePWM-1A and ePWM-2A at two different events. The PWM signal from both is 250 kHz with updown counter mode and 50% duty cycle.
The instant for ePWM-1A for ISR is when the counter value while incrementing becomes equal to CMPA value and for ePWM-2A is when the counter value while decrementing becomes equal to CMPA.
Furthermore I am also using SOCA and SOCB of ePWM-1A for ADC sampling trigger. ADC is working fine but the ISR routine is not working. I have written two files for the project one has the initialization of the peripherals and the other one has the main code. on debugging there is no error. But the values that i want to calculate when the trigger occur are not updating and calculating.
I always start the project from one example and then keep on adding that therefore may be some commands and comments will be extra
Here is the main code:
#include "PeripheralHeaderIncludes.h"
#include "DSP2802x_EPwm_defines.h" // useful defines for initialization
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// FUNCTION PROTOTYPES
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void DeviceInit(void);
void InitFlash();
void MemCopy(Uint16 *SourceAddr, Uint16* SourceEndAddr, Uint16* DestAddr);
void Init_EPWM1(void);
void Init_EPWM2(void);
void Init_ADC(void);
void Init_ECapture(void);
void PieCntlInit(void);
void PieVectTableInit(void);
__interrupt void epwm1_isr(void);
__interrupt void epwm2_isr(void);
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// VARIABLE DECLARATIONS - GENERAL
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// Used for running BackGround in flash and the ISR in RAM
extern Uint16 RamfuncsLoadStart, RamfuncsLoadEnd, RamfuncsRunStart;
Uint16 duty_cycle_A=60; // Set duty 50% initially
Uint16 duty_cycle_B=60; // Set duty 50% initially
// ADC Result Register
#define ADCRESULT_uLpos (AdcResult.ADCRESULT0)
#define ADCRESULT_uLneg (AdcResult.ADCRESULT1)
#define ADCRESULT_uM (AdcResult.ADCRESULT7)
// Counter Register
#define COUNTER_LOW (ECap1Regs.CAP1)
#define COUNTER_HIGH (ECap1Regs.CAP2)
// -----------------------------Variables-------------------------
float uLpos, uLneg, uLposRead, uLnegRead; // Positive and negative sampled voltage of auxiliary winding of the inductor
float iLbarRead, iLbar; // Natural Sample of current transformer output voltage
int C1, C2; // Counter variables
//Uint16 C3 = 0, C4 = 0;
// -----------------------------Constants-------------------------
// float L = 0.000551; // Inductor of the converter
// long int fs = 250000; // Switching frequency 250 kHz
// -----------------------------Controller Parameters-------------------------
float K_Lf = 0.007259; // L * fs = 137.75 and K_Lf = 1/(L*fs) = 1/137.75 = 0.007259
float threshold = 0.001; // Need to define threshold value 1 mA from simulations
// -----------------------------Current Observer Signals-------------------------
float iLmax = 0, iLmin = 0, iLmed = 0;
float delta_i = 0;
// #define period 500 // 60kHz when PLL is set to 0xC (60MHz)
// period 250 // 120kHz when PLL is set to 0xC (60MHz)
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
// MAIN CODE - starts here
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
void main(void)
{
//=================================
// INITIALISATION - General
//=================================
DeviceInit(); // Device Life support & GPIO mux settings
// Only used if running from FLASH
// Note that the variable FLASH is defined by the compiler (-d FLASH)
#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)
//-------------------------------------------------------------
Init_EPWM1();
Init_EPWM2();
Init_ADC();
Init_ECapture();
PieCntlInit();
PieVectTableInit();
EALLOW;
PieVectTable.EPWM1_INT = &epwm1_isr;
PieVectTable.EPWM2_INT = &epwm2_isr;
IER |= M_INT3;
PieCtrlRegs.PIEIER3.bit.INTx1 = 1;
// PieCtrlRegs.PIEIER3.bit.INTx2 = 1;
// Set Compare values
EPwm1Regs.CMPA.half.CMPA = duty_cycle_A; // Set duty 50% initially
EPwm2Regs.CMPA.half.CMPA = duty_cycle_A; // Set duty 50% initially
EPwm2Regs.CMPB = duty_cycle_B; // Set duty 50% initially
//=================================
// Forever LOOP
//=================================
// Just sit and loop forever:
// No interrups needed in this example.
// PWM pins can be observed with a scope.
for(;;)
{
EPwm1Regs.CMPA.half.CMPA = duty_cycle_A; // Set duty 50% initially
EPwm2Regs.CMPA.half.CMPA = duty_cycle_A; // Add duty_cycle_A to watch window
EPwm2Regs.CMPB = duty_cycle_B; // Add duty_cycle_B to watch window
// and change its value to change duty cycle
uLposRead = ADCRESULT_uLpos;
uLnegRead = ADCRESULT_uLneg;
iLbarRead = ADCRESULT_uM;
uLpos = 1.065;//(uLposRead*3.3)/4096;
uLneg = 1.065;//(uLnegRead*3.3)/4096;
iLbar = (iLbarRead*3.3)/4096;
C1 = COUNTER_HIGH;
C2 = COUNTER_LOW;
// C3 = ECap1Regs.CAP3;
// C4 = ECap1Regs.CAP4;
delta_i = abs (iLbar - iLmed);
iLmed = (iLmax + iLmin) / 2;
}
}
__interrupt void epwm1_isr(void){
iLmin = iLmax - (C2 * uLneg * K_Lf); // Minimum value of inductor current by current observer
EPwm1Regs.ETCLR.bit.INT = 1;
}
__interrupt void epwm2_isr(void){
if (delta_i < threshold){
iLmax = (C1 * uLpos * K_Lf) + iLmin; // Maximum value of inductor current by current observer
}
else {
iLmax = iLbar + (0.5 * C2 * uLneg * K_Lf);
}
EPwm2Regs.ETCLR.bit.INT = 1;
}
and the defined functions for peripherals is here:
#include "PeripheralHeaderIncludes.h"
#include "DSP2802x_EPwm_defines.h" // useful defines for initialization
#include "DSP2802x_ECap.h"
// Functions that will be run from RAM need to be assigned to
// a different section. This section will then be mapped to a load and
// run address using the linker cmd file.
#pragma CODE_SECTION(InitFlash, "ramfuncs");
#define Device_cal (void (*)(void))0x3D7C80
#define ADC_usDELAY 5000L
#define period 120 //To generate PWM of 250 kHz symmetric PWM
void DeviceInit(void);
void PieCntlInit(void);
void PieVectTableInit(void);
void WDogDisable(void);
void PLLset(Uint16);
void ISR_ILLEGAL(void);
void Init_EPWM1(void);
void Init_EPWM2(void);
void Init_ADC(void);
void Init_ECapture(void);
//--------------------------------------------------------------------
// Configure Device for target Application Here
//--------------------------------------------------------------------
void DeviceInit(void)
{
WDogDisable(); // Disable the watchdog initially
DINT; // Global Disable all Interrupts
IER = 0x0000; // Disable CPU interrupts
IFR = 0x0000; // Clear all CPU interrupt flags
// The Device_cal function, which copies the ADC & oscillator calibration values
// from TI reserved OTP into the appropriate trim registers, occurs automatically
// in the Boot ROM. If the boot ROM code is bypassed during the debug process, the
// following function MUST be called for the ADC and oscillators to function according
// to specification.
EALLOW;
SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1; // Enable ADC peripheral clock
(*Device_cal)(); // Auto-calibrate from TI OTP
SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 0; // Return ADC clock to original state
EDIS;
// Switch to Internal Oscillator 1 and turn off all other clock
// sources to minimize power consumption
EALLOW;
SysCtrlRegs.CLKCTL.bit.INTOSC1OFF = 0;
SysCtrlRegs.CLKCTL.bit.OSCCLKSRCSEL=0; // Clk Src = INTOSC1
SysCtrlRegs.CLKCTL.bit.XCLKINOFF=1; // Turn off XCLKIN
SysCtrlRegs.CLKCTL.bit.XTALOSCOFF=1; // Turn off XTALOSC
SysCtrlRegs.CLKCTL.bit.INTOSC2OFF=1; // Turn off INTOSC2
EDIS;
// SYSTEM CLOCK speed based on internal oscillator = 10 MHz
// 0xC = 60 MHz (12)
// 0xB = 55 MHz (11)
// 0xA = 50 MHz (10)
// 0x9 = 45 MHz (9)
// 0x8 = 40 MHz (8)
// 0x7 = 35 MHz (7)
// 0x6 = 30 MHz (6)
// 0x5 = 25 MHz (5)
// 0x4 = 20 MHz (4)
// 0x3 = 15 MHz (3)
// 0x2 = 10 MHz (2)
PLLset(0xC); // choose from options above
// Initialise interrupt controller and Vector Table
// to defaults for now. Application ISR mapping done later.
PieCntlInit();
PieVectTableInit();
EALLOW; // below registers are "protected", allow access.
// LOW SPEED CLOCKS prescale register settings
SysCtrlRegs.LOSPCP.all = 0x0002; // Sysclk / 4 (15 MHz)
SysCtrlRegs.XCLK.bit.XCLKOUTDIV=2;
// PERIPHERAL CLOCK ENABLES
//---------------------------------------------------
// If you are not using a peripheral you may want to switch
// the clock off to save power, i.e. set to =0
//
// Note: not all peripherals are available on all 280x derivates.
// Refer to the datasheet for your particular device.
SysCtrlRegs.PCLKCR0.bit.ADCENCLK = 1; // ADC
//------------------------------------------------
SysCtrlRegs.PCLKCR3.bit.COMP1ENCLK = 0; // COMP1
SysCtrlRegs.PCLKCR3.bit.COMP2ENCLK = 0; // COMP2
//------------------------------------------------
SysCtrlRegs.PCLKCR0.bit.I2CAENCLK = 0; // I2C
//------------------------------------------------
SysCtrlRegs.PCLKCR0.bit.SPIAENCLK = 0; // SPI-A
//------------------------------------------------
SysCtrlRegs.PCLKCR0.bit.SCIAENCLK = 0; // SCI-A
//------------------------------------------------
SysCtrlRegs.PCLKCR1.bit.ECAP1ENCLK = 1; //eCAP1
//------------------------------------------------
SysCtrlRegs.PCLKCR1.bit.EPWM1ENCLK = 1; // ePWM1
SysCtrlRegs.PCLKCR1.bit.EPWM2ENCLK = 1; // ePWM2
SysCtrlRegs.PCLKCR1.bit.EPWM3ENCLK = 0; // ePWM3
SysCtrlRegs.PCLKCR1.bit.EPWM4ENCLK = 0; // ePWM4
//------------------------------------------------
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; // Enable TBCLK
//------------------------------------------------
//--------------------------------------------------------------------------------------
// GPIO (GENERAL PURPOSE I/O) CONFIG
//--------------------------------------------------------------------------------------
//-----------------------
// QUICK NOTES on USAGE:
//-----------------------
// If GpioCtrlRegs.GP?MUX?bit.GPIO?= 1, 2 or 3 (i.e. Non GPIO func), then leave
// rest of lines commented
// If GpioCtrlRegs.GP?MUX?bit.GPIO?= 0 (i.e. GPIO func), then:
// 1) uncomment GpioCtrlRegs.GP?DIR.bit.GPIO? = ? and choose pin to be IN or OUT
// 2) If IN, can leave next to lines commented
// 3) If OUT, uncomment line with ..GPACLEAR.. to force pin LOW or
// uncomment line with ..GPASET.. to force pin HIGH or
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// GPIO-00 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // 0=GPIO, 1=EPWM1A, 2=Resv, 3=Resv
GpioCtrlRegs.GPADIR.bit.GPIO0 = 1; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO0 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO0 = 1; // uncomment if --> Set High initially
// GpioCtrlRegs.GPAPUD.bit.GPIO0 = 1; // Disable pull-up on GPIO0 (EPWM1A)
//--------------------------------------------------------------------------------------
// GPIO-01 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 0; // 0=GPIO, 1=EPWM1B, 2=EMU0, 3=COMP1OUT
GpioCtrlRegs.GPADIR.bit.GPIO1 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO1 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO1 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-02 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1; // 0=GPIO, 1=EPWM2A, 2=Resv, 3=Resv
GpioCtrlRegs.GPADIR.bit.GPIO2 = 1; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO2 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO2 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-03 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1; // 0=GPIO, 1=EPWM2B, 2=Resv, 3=COMP2OUT
GpioCtrlRegs.GPADIR.bit.GPIO3 = 1; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO3 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO3 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-04 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0; // 0=GPIO, 1=EPWM3A, 2=Resv, 3=Resv
GpioCtrlRegs.GPADIR.bit.GPIO4 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO4 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO4 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-05 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0; // 0=GPIO, 1=EPWM3B, 2=Resv, 3=ECAP1
GpioCtrlRegs.GPADIR.bit.GPIO5 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO5 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO5 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-06 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0; // 0=GPIO, 1=EPWM4A, 2=SYNCI, 3=SYNCO
GpioCtrlRegs.GPADIR.bit.GPIO6 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO6 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO6 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-07 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0; // 0=GPIO, 1=EPWM4B, 2=SCIRX-A, 3=Resv
GpioCtrlRegs.GPADIR.bit.GPIO7 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO7 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO7 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-08 - GPIO-11 Do Not Exist
//--------------------------------------------------------------------------------------
// GPIO-12 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 0; // 0=GPIO, 1=TZ1, 2=SCITX-A, 3=Resv
GpioCtrlRegs.GPADIR.bit.GPIO12 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO12 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO12 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-13 - GPIO-15 Do Not Exist
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// GPIO-16 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 0; // 0=GPIO, 1=SPISIMO-A, 2=Resv, 3=TZ2
GpioCtrlRegs.GPADIR.bit.GPIO16 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO16 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO16 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-17 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX2.bit.GPIO17 = 0; // 0=GPIO, 1=SPISOMI-A, 2=Resv, 3=TZ3
GpioCtrlRegs.GPADIR.bit.GPIO17 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO17 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO17 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-18 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX2.bit.GPIO18 = 0; // 0=GPIO, 1=SPICLK-A, 2=SCITX-A, 3=XCLKOUT
GpioCtrlRegs.GPADIR.bit.GPIO18 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO18 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO18 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-19 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX2.bit.GPIO19 = 3; // 0=GPIO, 1=SPISTE-A, 2=SCIRX-A, 3=ECAP1
GpioCtrlRegs.GPADIR.bit.GPIO19 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO19 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO19 = 1; // uncomment if --> Set High initially
GpioCtrlRegs.GPAPUD.bit.GPIO19 = 0; // 0 = Enable pullup
//--------------------------------------------------------------------------------------
// GPIO-20 - GPIO-27 Do Not Exist
//--------------------------------------------------------------------------------------
// GPIO-28 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX2.bit.GPIO28 = 0; // 0=GPIO, 1=SCIRX-A, 2=I2C-SDA, 3=TZ2
GpioCtrlRegs.GPADIR.bit.GPIO28 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO28 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO28 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-29 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 0; // 0=GPIO, 1=SCITXD-A, 2=I2C-SCL, 3=TZ3
GpioCtrlRegs.GPADIR.bit.GPIO29 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPACLEAR.bit.GPIO29 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPASET.bit.GPIO29 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-30 - GPIO-31 Do Not Exist
//--------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------
// GPIO-32 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPBMUX1.bit.GPIO32 = 0; // 0=GPIO, 1=I2C-SDA, 2=SYNCI, 3=ADCSOCA
GpioCtrlRegs.GPBDIR.bit.GPIO32 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPBCLEAR.bit.GPIO32 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPBSET.bit.GPIO32 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-33 - PIN FUNCTION = --Spare--
GpioCtrlRegs.GPBMUX1.bit.GPIO33 = 0; // 0=GPIO, 1=I2C-SCL, 2=SYNCO, 3=ADCSOCB
GpioCtrlRegs.GPBDIR.bit.GPIO33 = 0; // 1=OUTput, 0=INput
// GpioDataRegs.GPBCLEAR.bit.GPIO33 = 1; // uncomment if --> Set Low initially
// GpioDataRegs.GPBSET.bit.GPIO33 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
// GPIO-34 - PIN FUNCTION = LED for F28027 USB dongle
GpioCtrlRegs.GPBMUX1.bit.GPIO34 = 0; // 0=GPIO, 1=COMP2OUT, 2=EMU1, 3=Resv
GpioCtrlRegs.GPBDIR.bit.GPIO34 = 1; // 1=OUTput, 0=INput
// GpioDataRegs.GPBCLEAR.bit.GPIO34 = 1; // uncomment if --> Set Low initially
GpioDataRegs.GPBSET.bit.GPIO34 = 1; // uncomment if --> Set High initially
//--------------------------------------------------------------------------------------
GpioCtrlRegs.GPAQSEL1.all = 0x0000; // GPIO0-GPIO15 Synch to SYSCLKOUT
GpioCtrlRegs.GPAQSEL2.all = 0x0000; // GPIO16-GPIO31 Synch to SYSCLKOUT
GpioCtrlRegs.GPBQSEL1.all = 0x0000; // GPIO32-GPIO34 Synch to SYSCLKOUT
EDIS; // Disable register access
}
//============================================================================
// NOTE:
// EPWM2 is used for sampling the voltage of auxiliary winding of the inductor
// EPWM-2A generates SOC signal to sample u_Lpos
// EPWM-2B generates SOC signal to sample u_Lneg
//============================================================================
void Init_EPWM1(void){
// Time-base registers
EPwm1Regs.TBPRD = period; // Set timer period, PWM frequency = 1 / period
EPwm1Regs.TBPHS.all = 0; // Time-Base Phase Register
EPwm1Regs.TBCTR = 0; // Time-Base Counter Register
EPwm1Regs.TBCTL.bit.PRDLD = TB_IMMEDIATE; // Set Immediate load
EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count-up mode: used for asymmetric PWM
EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading
EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;
EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1;
// Setup shadow register load on ZERO
EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // Load on CTR=Zero
// Set actions
EPwm1Regs.AQCTLA.bit.CAU = AQ_SET; // Set PWM1A on Zero event
EPwm1Regs.AQCTLA.bit.CAD = AQ_CLEAR; // Clear PWM1A on Up-Count CompareA event
//Interrupt from EPWM-1A
EPwm1Regs.ETSEL.bit.INTSEL = 100; // Select INT on TBCTR = CMPA while incrementing
EPwm1Regs.ETSEL.bit.INTEN = 1; // Enable INT
EPwm1Regs.ETPS.bit.INTPRD = 01; // Generate INT on 1st event
}
void Init_EPWM2(void){
// Time-base registers
EPwm2Regs.TBPRD = period; // Set timer period, PWM frequency = 1 / period
EPwm2Regs.TBPHS.all = 0; // Time-Base Phase Register
EPwm2Regs.TBCTR = 0; // Time-Base Counter Register
EPwm2Regs.TBCTL.bit.PRDLD = TB_IMMEDIATE; // Set Immediate load
EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Count-updown mode: used for symmetric PWM
EPwm2Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Disable phase loading
EPwm2Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE;
EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1;
EPwm2Regs.TBCTL.bit.CLKDIV = TB_DIV1;
// Setup shadow register load on ZERO
EPwm2Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW;
EPwm2Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW;
EPwm2Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; // load on CTR=Zero
EPwm2Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; // load on CTR=Zero
// Set actions
EPwm2Regs.AQCTLA.bit.CAU = AQ_SET; // Set PWM2A on event A, up count
EPwm2Regs.AQCTLA.bit.CAD = AQ_CLEAR; // Clear PWM2A on event A, down count
EPwm2Regs.AQCTLB.bit.CBU = AQ_SET; // Set PWM2B on event B, up count
EPwm2Regs.AQCTLB.bit.CBD = AQ_CLEAR; // Clear PWM2B on event B, down count
// Event trigger select register
// For EPWM-2A when time counter = period generate SOC pulse
// For EPWM-2B when time counter = zero generate SOC pulse
EPwm2Regs.ETSEL.bit.INTEN = 1; // 0 = disable INT, 1 = enable INT
EPwm2Regs.ETSEL.bit.INTSEL = 101; // Select INT on TBCTR = CMPA while decrementing
EPwm2Regs.ETPS.bit.INTPRD = 01; // Generate INT on 1st event
EPwm2Regs.ETSEL.bit.SOCAEN = 1; // Enable SOCA pulse
EPwm2Regs.ETSEL.bit.SOCASEL = ET_CTR_PRD; // trigger ADC on PRD (CTR=PRD)
EPwm2Regs.ETSEL.bit.SOCBEN = 1; // Enable SOCA pulse
EPwm2Regs.ETSEL.bit.SOCBSEL = ET_CTR_ZERO; // trigger ADC on PRD (CTR=ZERO)
EPwm2Regs.ETPS.bit.SOCAPRD = ET_1ST; // Trigger on first event
EPwm2Regs.ETPS.bit.SOCAPRD = ET_1ST; // Trigger on first event
EPwm2Regs.ETPS.bit.SOCBPRD = ET_1ST; // Trigger on first event
}
//============================================================================
// ADC-A0, ADC-A1 and ADC-A7 are used to sample u_Lpos, u_Lneg and u_M signal.
// ADC-A0 and ADC-A7 are triggered by SOCA of EPWM-2A @ TBCTR = PRD and
// ADC-A1 is triggered by SOCB of EPWM-2B @ TBCTR = ZERO
//============================================================================
void Init_ADC(void){
Uint16 i = 0;
EALLOW;
// ADC power-up sequence
AdcRegs.ADCCTL1.bit.ADCREFSEL = 0; // Internal ;External(VREFHI/VREFLO) Bandgap used for reference generation
AdcRegs.ADCCTL1.bit.ADCPWDN = 1; // The analog circuitry inside core is powered up
AdcRegs.ADCCTL1.bit.ADCBGPWD = 1; // Bandgap Buffer's circuitry inside core is powered up
AdcRegs.ADCCTL1.bit.ADCREFPWD = 1; // Reference Buffer's Circuitry inside core is powered up
AdcRegs.ADCCTL1.bit.ADCENABLE = 1; // Enable ADC
for(i=0; i<5000; i++){} // wait 60000 cycles = 1ms (each iteration is 12 cycles)
// ------------TRIGSEL-----------------
AdcRegs.ADCSOC0CTL.bit.TRIGSEL = 0x7; // Trigger Source: EPWM2 ADCSOCA
AdcRegs.ADCSOC7CTL.bit.TRIGSEL = 0x7; // Trigger Source: EPWM2 ADCSOCA
AdcRegs.ADCSOC1CTL.bit.TRIGSEL = 0x8; // Trigger Source: EPWM2 ADCSOCB
// -------------CHSEL-----------------
AdcRegs.ADCSOC0CTL.bit.CHSEL = 0x0; // ADCINA0 (A0: pin 3 controlSTICK )
AdcRegs.ADCSOC1CTL.bit.CHSEL = 0x1; // ADCINA1 (A1: pin 12 controlSTICK)
AdcRegs.ADCSOC7CTL.bit.CHSEL = 0x7; // ADCINA7 (A7: pin 1 controlSTICK)
// -------------ACQPS ----------------
AdcRegs.ADCSOC0CTL.bit.ACQPS = 0x6; // Sample Window 7 clock cycles
AdcRegs.ADCSOC1CTL.bit.ACQPS = 0x6; // Sample Window 7 clock cycles
AdcRegs.ADCSOC7CTL.bit.ACQPS = 0x6; // Sample Window 7 clock cycles
// -------------INTSELxNy-------------
AdcRegs.INTSEL1N2.bit.INT1CONT = 1; // continuous mode (clear intFlag is not needed)
AdcRegs.INTSEL1N2.bit.INT2CONT = 1; // continuous mode (clear intFlag is not needed)
// AdcRegs.INTSEL3N4.bit.INT3CONT = 1; // continuous mode (clear intFlag is not needed)
AdcRegs.INTSEL1N2.bit.INT1E = 1; // 1= EOC interrupt enabled !
AdcRegs.INTSEL1N2.bit.INT2E = 1; // 1= EOC interrupt enabled !
// AdcRegs.INTSEL3N4.bit.INT3E = 1; // 1= EOC interrupt enabled !
AdcRegs.INTSEL1N2.bit.INT1SEL = 0; // EOC0 is trigger for ADCINT1 //***
AdcRegs.INTSEL1N2.bit.INT2SEL = 1; // EOC1 is trigger for ADCINT2 //***
// AdcRegs.INTSEL3N4.bit.INT3SEL = 7; // EOC7 is trigger for ADCINT3 //***
// -----------------------------------
EDIS;
}
//============================================================================
// ECapture peripheral module initialization. GPIO-19 pin 25 on controlSTICK
// is used as input.
//============================================================================
void Init_ECapture(void){
// Disable global interrupts, Stop eCAP counter, Disable eCAP interrupts
ECap1Regs.ECEINT.all = 0x0000; // Disable all capture interrupts
ECap1Regs.ECCLR.all = 0xFFFF; // Clear all CAP interrupt flags
ECap1Regs.ECCTL1.bit.CAPLDEN = 0; // Disable CAP1-CAP4 register loads
ECap1Regs.ECCTL2.bit.TSCTRSTOP = 0; // Make sure the counter is stopped
//Configure peripheral registers
ECap1Regs.ECCTL1.bit.CAP1POL = 0; //Capture Event 1 triggered on a rising edge (RE)
ECap1Regs.ECCTL1.bit.CAP2POL = 1; //Capture Event 2 triggered on a falling edge (FE)
ECap1Regs.ECCTL1.bit.CAP3POL = 0; //Capture Event 3 triggered on a rising edge (RE)
ECap1Regs.ECCTL1.bit.CAP4POL = 1; //Capture Event 4 triggered on a falling edge (FE)
ECap1Regs.ECCTL1.bit.CTRRST1 = 1; //Reset counter after Event 1 time-stamp has been captured
ECap1Regs.ECCTL1.bit.CTRRST2 = 1; //Reset counter after Event 2 time-stamp has been captured
// ECap1Regs.ECCTL1.bit.CTRRST3 = 1; //Reset counter after Event 3 time-stamp has been captured
// ECap1Regs.ECCTL1.bit.CTRRST4 = 1; //Reset counter after Event 4 time-stamp has been captured
ECap1Regs.ECCTL2.bit.CONT_ONESHT = 0; // Operate in continuous mode
ECap1Regs.ECCTL2.bit.STOP_WRAP = 1; // Wraps up after Event 2
ECap1Regs.ECCTL2.bit.SYNCO_SEL = 2; // Disable
ECap1Regs.ECCTL2.bit.SYNCI_EN = 0; // Disable
ECap1Regs.ECCTL1.bit.CAPLDEN = 1; // Enable CAP 1-4 register loads at capture event time
ECap1Regs.ECCTL1.bit.PRESCALE = 0; // Divide by 1
//Start eCAP counter
ECap1Regs.ECCTL2.bit.CAP_APWM = 0;
ECap1Regs.ECCTL2.bit.TSCTRSTOP = 1;
// Interrupt after capturing event 1 and event 2
// ECap1Regs.ECEINT.all = 0x0004;
}
void PieCntlInit(void)
{
// Disable Interrupts at the CPU level:
DINT;
// Disable the PIE
PieCtrlRegs.PIECTRL.bit.ENPIE = 0;
// Clear all PIEIER registers:
PieCtrlRegs.PIEIER1.all = 0;
PieCtrlRegs.PIEIER2.all = 0;
PieCtrlRegs.PIEIER3.all = 0;
PieCtrlRegs.PIEIER4.all = 0;
PieCtrlRegs.PIEIER5.all = 0;
PieCtrlRegs.PIEIER6.all = 0;
PieCtrlRegs.PIEIER7.all = 0;
PieCtrlRegs.PIEIER8.all = 0;
PieCtrlRegs.PIEIER9.all = 0;
PieCtrlRegs.PIEIER10.all = 0;
PieCtrlRegs.PIEIER11.all = 0;
PieCtrlRegs.PIEIER12.all = 0;
// Clear all PIEIFR registers:
PieCtrlRegs.PIEIFR1.all = 0;
PieCtrlRegs.PIEIFR2.all = 0;
PieCtrlRegs.PIEIFR3.all = 0;
PieCtrlRegs.PIEIFR4.all = 0;
PieCtrlRegs.PIEIFR5.all = 0;
PieCtrlRegs.PIEIFR6.all = 0;
PieCtrlRegs.PIEIFR7.all = 0;
PieCtrlRegs.PIEIFR8.all = 0;
PieCtrlRegs.PIEIFR9.all = 0;
PieCtrlRegs.PIEIFR10.all = 0;
PieCtrlRegs.PIEIFR11.all = 0;
PieCtrlRegs.PIEIFR12.all = 0;
}
void PieVectTableInit(void)
{
int16 i;
PINT *Dest = &PieVectTable.TINT1;
EALLOW;
for(i=0; i < 115; i++)
*Dest++ = &ISR_ILLEGAL;
EDIS;
// Enable the PIE Vector Table
PieCtrlRegs.PIECTRL.bit.ENPIE = 1;
}