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.

Exiting Standby with WDT

Other Parts Discussed in Thread: TMS320F28027

I am trying to use the wdog timer to periodically exit from standby mode, but there is some issue because it gets stuck! I cannot figure out what I am missing.

My code is below. I bolded all the code think is relevant. Thanks in advance for help!

Chip: TMS320F28027; IDE: CCS v5.4

void enableGPIO(void);
char Exponent(unsigned int value);

// Constants
const int samples = 128;

// Flags/counters
volatile unsigned int adcIsDone = 0;
volatile unsigned int adcCount = 0;
volatile unsigned int onOffFlag1 = 0;
volatile unsigned int conditionMet = 0;
volatile unsigned char fadeInOut = 0; // if 1, fadeIn is true. if 2, fadeOut is true. False otherwise.
volatile unsigned long i = 0;
volatile unsigned char j = 0;
volatile unsigned long timer0IntCount = 0x4C42; // initialize at full period
volatile unsigned long timer1IntCount = 0;
volatile unsigned long timer2IntCount = 0;

// Variables

double vReal[128];
double vImag[128];

ADC_Handle myAdc;
CLK_Handle myClk;
FLASH_Handle myFlash;
GPIO_Handle myGpio;
PIE_Handle myPie;
PWM_Handle myPwm1, myPwm2, myPwm3, myPwm4;
TIMER_Handle myTimer0, myTimer1, myTimer2;

void main(void)
{

CPU_Handle myCpu;
PLL_Handle myPll;
WDOG_Handle myWDog;
PWR_Handle myPwr;
GPIO_Handle myGpio;

// Initialize all the handles needed for this application
myAdc = ADC_init((void *)ADC_BASE_ADDR, sizeof(ADC_Obj));
myClk = CLK_init((void *)CLK_BASE_ADDR, sizeof(CLK_Obj));
myCpu = CPU_init((void *)NULL, sizeof(CPU_Obj));
myFlash = FLASH_init((void *)FLASH_BASE_ADDR, sizeof(FLASH_Obj));
myGpio = GPIO_init((void *)GPIO_BASE_ADDR, sizeof(GPIO_Obj));
myPie = PIE_init((void *)PIE_BASE_ADDR, sizeof(PIE_Obj));
myPll = PLL_init((void *)PLL_BASE_ADDR, sizeof(PLL_Obj));
myPwm1 = PWM_init((void *)PWM_ePWM1_BASE_ADDR, sizeof(PWM_Obj));
myPwm2 = PWM_init((void *)PWM_ePWM2_BASE_ADDR, sizeof(PWM_Obj));
myPwm3 = PWM_init((void *)PWM_ePWM3_BASE_ADDR, sizeof(PWM_Obj));
myPwm4 = PWM_init((void *)PWM_ePWM4_BASE_ADDR, sizeof(PWM_Obj));
myPwr = PWR_init((void *)PWR_BASE_ADDR, sizeof(PWR_Obj));
myTimer0 = TIMER_init((void *)TIMER0_BASE_ADDR, sizeof(TIMER_Obj));
myTimer1 = TIMER_init((void *)TIMER1_BASE_ADDR, sizeof(TIMER_Obj));
myTimer2 = TIMER_init((void *)TIMER2_BASE_ADDR, sizeof(TIMER_Obj));
myWDog = WDOG_init((void *)WDOG_BASE_ADDR, sizeof(WDOG_Obj));

WDOG_disable(myWDog);
CLK_enableAdcClock(myClk);
CLK_enableTbClockSync(myClk);
CLK_enablePwmClock(myClk, PWM_Number_1);
CLK_enablePwmClock(myClk, PWM_Number_2);
CLK_enablePwmClock(myClk, PWM_Number_3);
CLK_enablePwmClock(myClk, PWM_Number_4);
(*Device_cal)();

CLK_setOscSrc(myClk, CLK_OscSrc_Internal);

PLL_setup(myPll, PLL_Multiplier_5, PLL_DivideSelect_ClkIn_by_1);

// Disable the PIE and all interrupts
PIE_disable(myPie);
PIE_disableAllInts(myPie);
CPU_disableGlobalInts(myCpu);
CPU_clearIntFlags(myCpu);

// If running from flash copy RAM only functions to RAM
#ifdef _FLASH
memcpy(&RamfuncsRunStart, &RamfuncsLoadStart, (size_t)&RamfuncsLoadSize);
#endif

// Setup a debug vector table and enable the PIE
PIE_setDebugIntVectorTable(myPie);
PIE_enable(myPie);
EALLOW;
((PIE_Obj *)myPie)->TINT0 = &cpu_timer0_isr;
((PIE_Obj *)myPie)->TINT1 = &cpu_timer1_isr;
((PIE_Obj *)myPie)->TINT2 = &cpu_timer2_isr;
SysCtrlRegs.LPMCR0.bit.WDINTE = 1;
EDIS;
// Register interrupt handlers in the PIE vector table
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_10, PIE_SubGroupNumber_1, (intVec_t)&adc_isr);
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_1, PIE_SubGroupNumber_7, (intVec_t)&cpu_timer0_isr);
PIE_registerSystemIntHandler(myPie, PIE_SystemInterrupts_TINT1, (intVec_t)&cpu_timer1_isr);
PIE_registerSystemIntHandler(myPie, PIE_SystemInterrupts_TINT2, (intVec_t)&cpu_timer2_isr);
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_1, PIE_SubGroupNumber_8, (intVec_t)&wakeint_isr);

// Initialize the ADC
ADC_enableBandGap(myAdc);
ADC_enableRefBuffers(myAdc);
ADC_powerUp(myAdc);
ADC_enable(myAdc);
ADC_setVoltRefSrc(myAdc, ADC_VoltageRefSrc_Int);

WDOG_setCount(myWDog, 255);
WDOG_enable(myWDog);
WDOG_enableInt(myWDog);
WDOG_setPreScaler(myWDog, WDOG_PreScaler_OscClk_by_512_by_16);
PWR_enableWatchDogInt(myPwr);

PIE_enableTimer0Int(myPie);
PIE_enableInt(myPie, PIE_GroupNumber_1, PIE_InterruptSource_WAKE);
PIE_clearInt(myPie, PIE_GroupNumber_1);
PIE_enableAdcInt(myPie, ADC_IntNumber_1);
CPU_enableInt(myCpu, CPU_IntNumber_1);
CPU_enableInt(myCpu, CPU_IntNumber_13);
CPU_enableInt(myCpu, CPU_IntNumber_14);
CPU_enableInt(myCpu, CPU_IntNumber_10);
CPU_enableGlobalInts(myCpu);
CPU_enableDebugInt(myCpu);

//initialize peripherals
//gpioSetup
GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_0, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_1, GPIO_1_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_1, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_2, GPIO_2_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_2, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_3, GPIO_3_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_3, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_4, GPIO_4_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_4, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_5, GPIO_5_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_5, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_6, GPIO_6_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_6, GPIO_Direction_Output);
GPIO_setMode(myGpio, GPIO_Number_7, GPIO_7_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_7, GPIO_Direction_Output);

//output
GPIO_setLow(myGpio, GPIO_Number_0);
GPIO_setLow(myGpio, GPIO_Number_1);
GPIO_setLow(myGpio, GPIO_Number_2);
GPIO_setLow(myGpio, GPIO_Number_3);
GPIO_setLow(myGpio, GPIO_Number_4);
GPIO_setLow(myGpio, GPIO_Number_5);
GPIO_setLow(myGpio, GPIO_Number_6);
GPIO_setLow(myGpio, GPIO_Number_7);
//PWM Output
GPIO_setPullUp(myGpio, GPIO_Number_0, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_0, GPIO_0_Mode_EPWM1A); //output pwm on gpio0
//GPIO_setPullUp(myGpio, GPIO_Number_1, GPIO_PullUp_Disable);
//GPIO_setMode(myGpio, GPIO_Number_1, GPIO_1_Mode_EPWM1B); //output pwm on gpio1

pwmSetup();
setupTimers();
adcSetup();

// Device Function
while(1){
if(adcIsDone == 1){

PLL_setMultiplier(myPll, PLL_Multiplier_5); // shift to 50 MHz

if(vReal[20] >= 1000 && vReal[30] >= 1000){ // retain 50 MHz condition
conditionMet = 1;
shiftFrequencies();
GPIO_setPullUp(myGpio, GPIO_Number_2, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_2, GPIO_2_Mode_EPWM2A); //output pwm on gpio2
GPIO_setPullUp(myGpio, GPIO_Number_3, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_3, GPIO_3_Mode_EPWM2B); //output pwm on gpio3
GPIO_setPullUp(myGpio, GPIO_Number_4, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_4, GPIO_4_Mode_EPWM3A); //output pwm on gpio4
GPIO_setPullUp(myGpio, GPIO_Number_5, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_5, GPIO_5_Mode_EPWM3B); //output pwm on gpio5
GPIO_setPullUp(myGpio, GPIO_Number_6, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_6, GPIO_6_Mode_EPWM4A); //output pwm on gpio6
GPIO_setPullUp(myGpio, GPIO_Number_7, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_7, GPIO_7_Mode_EPWM4B); //output pwm on gpio7

}
else{
conditionMet = 0;
shiftFrequencies();
PLL_setMultiplier(myPll, PLL_Multiplier_1); // shift back to 10 MHz
GPIO_setMode(myGpio, GPIO_Number_2, GPIO_4_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_2, GPIO_Direction_Output);
GPIO_setPullUp(myGpio, GPIO_Number_2, GPIO_PullUp_Enable);
GPIO_setLow(myGpio, GPIO_Number_2);
GPIO_setMode(myGpio, GPIO_Number_3, GPIO_3_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_3, GPIO_Direction_Output);
GPIO_setPullUp(myGpio, GPIO_Number_3, GPIO_PullUp_Enable);
GPIO_setLow(myGpio, GPIO_Number_3);
GPIO_setMode(myGpio, GPIO_Number_4, GPIO_4_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_4, GPIO_Direction_Output);
GPIO_setPullUp(myGpio, GPIO_Number_4, GPIO_PullUp_Enable);
GPIO_setLow(myGpio, GPIO_Number_4);
GPIO_setMode(myGpio, GPIO_Number_5, GPIO_5_Mode_GeneralPurpose);
GPIO_setDirection(myGpio, GPIO_Number_5, GPIO_Direction_Output);
GPIO_setPullUp(myGpio, GPIO_Number_5, GPIO_PullUp_Enable);
GPIO_setLow(myGpio, GPIO_Number_5);

WDOG_clearCounter(myWDog);
PWR_setNumStandByClocks(myPwr, PWR_NumStandByClocks_2);
if ( PLL_getClkStatus(myPll) != PLL_PLLSTS_MCLKSTS_BITS) {
PWR_enableWatchDogInt(myPwr);
PWR_setLowPowerMode(myPwr, PWR_LowPowerMode_Standby);
}
IDLE;
}
adcIsDone = 0;
}
else{
//wait
}
} //end while

} // end main

void pwmSetup(void){
// Enable PWM clock
CLK_enablePwmClock(myClk, PWM_Number_1);
CLK_enablePwmClock(myClk, PWM_Number_2);
CLK_enablePwmClock(myClk, PWM_Number_3);
CLK_enablePwmClock(myClk, PWM_Number_4);

// Setup PWM1
PWM_enableSocAPulse(myPwm1); // Enable SOC on A group
PWM_setSocAPulseSrc(myPwm1, PWM_SocPulseSrc_CounterEqualCmpAIncr); // Select SOC from from CPMA on upcount
PWM_setSocAPeriod(myPwm1, PWM_SocPeriod_FirstEvent); // Generate pulse on 1st event
PWM_setCmpA(myPwm1, 0x2621);//2621 // Set compare A value (50% duty)
PWM_setCmpB(myPwm1, 0x2621);
PWM_setPeriod(myPwm1, 0x4C42); // Set period for ePWM1 (~1.28 kHz)
PWM_setCounterMode(myPwm1, PWM_CounterMode_Up); // count up and start
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm1, PWM_ActionQual_Clear); // High til compare value
PWM_setActionQual_Period_PwmA(myPwm1, PWM_ActionQual_Set); // Clear after CmpA
PWM_setActionQual_CntUp_CmpB_PwmB(myPwm1, PWM_ActionQual_Clear);
PWM_setActionQual_Period_PwmB(myPwm1, PWM_ActionQual_Set);
//Setup PWM2
PWM_setCmpA(myPwm2, 0x0); // Set compare A value (50% duty)
PWM_setCmpB(myPwm2, 0x0);
PWM_setPeriod(myPwm2, 0x400); // Set period for ePWM1 (~1.28 kHz)
PWM_setCounterMode(myPwm2, PWM_CounterMode_Up); // count up and start
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm2, PWM_ActionQual_Clear); // High til compare value
PWM_setActionQual_Period_PwmA(myPwm2, PWM_ActionQual_Set); // Clear after CmpA
PWM_setActionQual_CntUp_CmpB_PwmB(myPwm2, PWM_ActionQual_Clear);
PWM_setActionQual_Period_PwmB(myPwm2, PWM_ActionQual_Set);
//Setup PWM3
PWM_setCmpA(myPwm3, 0x0); // Set compare A value (50% duty)
PWM_setCmpB(myPwm3, 0x0);
PWM_setPeriod(myPwm3, 0x400); // Set period for ePWM1 (~1.28 kHz)
PWM_setCounterMode(myPwm3, PWM_CounterMode_Up); // count up and start
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm3, PWM_ActionQual_Clear); // High til compare value
PWM_setActionQual_Period_PwmA(myPwm3, PWM_ActionQual_Set); // Clear after CmpA
PWM_setActionQual_CntUp_CmpB_PwmB(myPwm3, PWM_ActionQual_Clear);
PWM_setActionQual_Period_PwmB(myPwm3, PWM_ActionQual_Set);
//Setup PWM2
PWM_setCmpA(myPwm4, 0x60); // Set compare A value (50% duty)
PWM_setCmpB(myPwm4, 0x60);
PWM_setPeriod(myPwm4, 0x400); // Set period for ePWM1 (~1.28 kHz)
PWM_setCounterMode(myPwm4, PWM_CounterMode_Up); // count up and start
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm4, PWM_ActionQual_Clear); // High til compare value
PWM_setActionQual_Period_PwmA(myPwm4, PWM_ActionQual_Set); // Clear after CmpA
PWM_setActionQual_CntUp_CmpB_PwmB(myPwm4, PWM_ActionQual_Clear);
PWM_setActionQual_Period_PwmB(myPwm4, PWM_ActionQual_Set);
}

void adcSetup(void){
// Configure ADC
//Note: Channel ADCINA4 will be double sampled to workaround the ADC 1st sample issue for rev0 silicon errata
ADC_setIntPulseGenMode(myAdc, ADC_IntPulseGenMode_Prior); //ADCINT1 trips after AdcResults latch
ADC_enableInt(myAdc, ADC_IntNumber_1); //Enabled ADCINT1
ADC_setIntMode(myAdc, ADC_IntNumber_1, ADC_IntMode_ClearFlag); //Disable ADCINT1 Continuous mode
ADC_setIntSrc(myAdc, ADC_IntNumber_1, ADC_IntSrc_EOC2); //setup EOC2 to trigger ADCINT1 to fire
ADC_setSocChanNumber (myAdc, ADC_SocNumber_2, ADC_SocChanNumber_A2); //set SOC2 channel select to ADCINA2
ADC_setSocTrigSrc(myAdc, ADC_SocNumber_2, ADC_SocTrigSrc_EPWM1_ADCSOCA); //set SOC2 start trigger on EPWM1A, due to round-robin SOC0 converts first then SOC1, then SOC2
ADC_setSocSampleWindow(myAdc, ADC_SocNumber_2, ADC_SocSampleWindow_7_cycles); //set SOC2 S/H Window to 7 ADC Clock Cycles, (6 ACQPS plus 1)
}

void setupTimers(void){
TIMER_stop(myTimer0);
TIMER_stop(myTimer1);
TIMER_stop(myTimer2);

//Timer0
TIMER_setPeriod(myTimer0, 5 * 1000000);
TIMER_setPreScaler(myTimer0, 0);
TIMER_reload(myTimer0);
TIMER_setEmulationMode(myTimer0, TIMER_EmulationMode_StopAfterNextDecrement);
TIMER_enableInt(myTimer0);
TIMER_start(myTimer0);
//Timer1
TIMER_setPeriod(myTimer1, 50 * 1000000); // once per sec
TIMER_setPreScaler(myTimer1, 0);
TIMER_reload(myTimer1);
TIMER_setEmulationMode(myTimer1, TIMER_EmulationMode_StopAfterNextDecrement);
TIMER_enableInt(myTimer1);
TIMER_start(myTimer1);
//Timer2
TIMER_setPeriod(myTimer2, 50 * 1000000); // once per sec
TIMER_setPreScaler(myTimer2, 0);
TIMER_reload(myTimer2);
TIMER_setEmulationMode(myTimer2, TIMER_EmulationMode_StopAfterNextDecrement);
TIMER_enableInt(myTimer2);
TIMER_start(myTimer2);
}

interrupt void adc_isr(void)
{

if(adcIsDone == 0){ // if in state 1 and ready for adc, load buffer
if(adcCount < 128){// fill buffer

vReal[adcCount] = (double)ADC_readResult(myAdc, ADC_ResultNumber_2);
vImag[adcCount] = 0;
}

else if(adcCount >= 128){
adcIsDone = 1;
}

else{
}
adcCount++;
}
else if(adcIsDone == 1){
adcCount = 0;
//do nothing
}


ADC_clearIntFlag(myAdc, ADC_IntNumber_1);
PIE_clearInt(myPie, PIE_GroupNumber_10);

return;
}

interrupt void wakeint_isr(void){

PIE_clearInt(myPie, PIE_GroupNumber_1);
}

interrupt void cpu_timer0_isr(void)
{

if(conditionMet == 1){
onOffFlag1++;

if(onOffFlag1 == 2){ //3A
PWM_setCmpA(myPwm3, 0x60);
PWM_setCmpB(myPwm3, 0);
PWM_setCmpA(myPwm2, 0);
PWM_setCmpB(myPwm2, 0);
}
else if(onOffFlag1 == 3){ // 3B
PWM_setCmpA(myPwm3, 0);
PWM_setCmpB(myPwm3, 0x60);
PWM_setCmpA(myPwm2, 0);
PWM_setCmpB(myPwm2, 0);
}
else if(onOffFlag1 == 4){ // 2A
PWM_setCmpA(myPwm3, 0);
PWM_setCmpB(myPwm3, 0);
PWM_setCmpA(myPwm2, 0x60);
PWM_setCmpB(myPwm2, 0);
}

else if(onOffFlag1 == 5){ // 2B
PWM_setCmpA(myPwm3, 0);
PWM_setCmpB(myPwm3, 0);
PWM_setCmpA(myPwm2, 0);
PWM_setCmpB(myPwm2, 0x60);
onOffFlag1 = 1;
}
else{
PWM_setCmpA(myPwm3, 0);
PWM_setCmpB(myPwm3, 0);
PWM_setCmpA(myPwm2, 0);
PWM_setCmpB(myPwm2, 0);
}
}
else{
PWM_setCmpA(myPwm3, 0);
PWM_setCmpB(myPwm3, 0);
PWM_setCmpA(myPwm2, 0);
PWM_setCmpB(myPwm2, 0);
}


PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
PIE_clearInt(myPie, PIE_GroupNumber_1);
}

interrupt void cpu_timer1_isr(void)
{
timer1IntCount++;

if(timer1IntCount > 6000){
timer1IntCount = 0;
}
else{

}
}

interrupt void cpu_timer2_isr(void)
{
timer2IntCount++;

if(timer1IntCount > 6000){
timer2IntCount = 0;
}
else{

}
}

void Swap(double *x, double *y)
{
double temp = *x;
*x = *y;
*y = temp;
}

char Exponent(unsigned int value)
{
/* Computes the Exponent of a powered 2 value */
char result = 0;
while (((value >> result) & 1) != 1) result++;
return(result);
}

void shiftFrequencies(void){
if(conditionMet > 0){ // 50 MHz condition
PWM_setCmpA(myPwm1, 0x2621);
PWM_setCmpB(myPwm1, 0x2621);
PWM_setPeriod(myPwm1, 0x4C42);
PWM_setPeriod(myPwm2, 0xCC);
PWM_setPeriod(myPwm3, 0xCC);
PWM_setPeriod(myPwm4, 0xCC);
TIMER_setPeriod(myTimer0, 5 * 1000000);
TIMER_setPeriod(myTimer1, 5 * 1000000);
TIMER_setPeriod(myTimer2, 5 * 1000000);


}
else{ // 10 MHz condition
PWM_setCmpA(myPwm1, 0x7A4);
PWM_setCmpB(myPwm1, 0x7A4);
PWM_setPeriod(myPwm1, 0xF40);
PWM_setPeriod(myPwm2, 0xCC);
PWM_setPeriod(myPwm3, 0xCC);
PWM_setPeriod(myPwm4, 0xCC);
TIMER_setPeriod(myTimer0, 1 * 1000000);
TIMER_setPeriod(myTimer1, 1 * 1000000);
TIMER_setPeriod(myTimer2, 1 * 1000000);
}
}