I wrote a program to be run on MC9S12XEG128 as below and program/debug it using P&E BDM. I tried to take the MCU to full stop mode and fast wake up the MCU using API after 13sec, then start the oscillator clock and at last turn on the PLL. By Toggling a Pin, there should be three different types of pulses with different Period (400us, then 100us and finally 25us). When I run the program in the debugger mode, it works fine and the result is as perfect as below:
Figure 1 : when we are in debugging mode and the debugging cable is connected.
But the problem arises when I disconnect the BDM cable and run the program on its own. Then I observe the following behavior:
Figure 2: when the MCU runs on its own with the debugger cable disconnected.
It seems that the MCU wakes up on the Oscillator not the internal clock in self clock mode. I know that the BDM takes the MCU into special single chip mode using BKGD pin and the MCU normally runs in the Normal single chip mode but I don't understand these different behaviors. Shouldn't the MCU work as it is programmed and run in the debugging mode and if not so, what should I do to get the expected result (I mean those three different periods) as in figure 1? I really would appreciate your help.
void main(void)
{
MCU_init(); // does some initialization
_DISABLE_COP();
EnableInterrupts;
GPIO_STOP(); //disable other interrupt that can wakeup the MCU
/*Enable API*/
VREGAPICL = 0;
VREGAPIR = 0xFFFF; //13 seconds
VREGAPICL_APIE = 1;
VREGAPICL_APIFE = 1;
/*Full Stop Mode*/
PLLCTL = 0x09U; //enable fast wakeup
CLKSEL_PLLSEL = 0;
PLLCTL_PLLON = 0;
CLKSEL_PSTP = 0;
asm ANDCC #0x6F;
asm STOP;
/*Disable API*/
VREGAPICL = 0;
ECT_Init();
ECT_TC0 = ECT_TCNT + 400;
ECT_TSCR1 = 0x90;
PIN_Toggle = ~PIN_Toggle;
for(;;)
{
_FEED_COP();
}
}
/*the ECT-channel0 isr*/
__interrupt void isrVectch0(void)
{
ECT_TFLG1_C0F = 1;
ECT_TC0 = ECT_TCNT + 400;
PIN_Toggle = ~PIN_Toggle;
Count++;
if (Count == 5)
{
PLLCTL_FSTWKP = 0; //start oscillator
}
if (Count == 30)
{
PLLCTL_PLLON = 1; //turn on PLL
while(!CRGFLG_LOCK);
CLKSEL_PLLSEL = 1; //switch to PLL
}
}
/*It just does some initialization, then activates the API timer (using internal RC and period of 13 seconds) and enables the fast wake up mode. After that, it takes the MCU to full stop mode and wakes up in self clock mode with internal Frequency of 1MHz. Then, it initiates the ECT timer ch0 with the value of 400. In the ECT_isr, after clearing its flag, the timer is reinitiated for another 400 bus clock. After a couple of times, I enabled the oscillator (which takes a while to be stabilized) and it automatically switches to oscillator clock which is 8MHz (bus clock = 4MHz). Finally, the clock is switched to 32MHz PLL (bus clock = 16MHz).*/
Hi,
by my opinion the main issue is hidden in the function MCU_Init which is not visible.
1. The most probable root causeis that some of the bits are write once in the NORMAL modeof the MCU and write anytime in the SPECIAL mode.
Could you please check the function MCU_Init. Probably you write in this function into the PLLCTL register and you set PLLCTL_SCME to 0. In the code below you set it to 1. If you are in NORMAL mode you are not able to switch it on again. This could be a reson why it is not started in the self clock mode.
2. Note: clearovani flagu - ECT_TFLG1_C0F = 1; is incorrect. This aproach will clear all flags because bit function is read modify write instruction with entire byte. Correct approach is ECT_TFLG1 = 0x01; .// clear C0F only;
// for more info read http://www.freescale.com/files/microcontrollers/doc/app_note/AN2554.pdf, Clearing and Disabling Interrupt Flags
Best regards,
Ladislav
Hi,
Thank you for your nice reply. I employed every point you said in my code. You were right about the MCU_Init() and the write to PLLCTL_SCME. However, as I changed it in the MCU_Init (PLLCTL = 0x09 to activate SCM & fast wakeup), I've got nothing but the same result as before. Does anything else comes to your mind that could be the reason?
By the way, there is another issue even in the Debug mode. When I run the debugger and press the "start/continue" button (F5, the green arrow), I don'r get the desired result but when I press the "Reset Target" button (The red arrow inside a circle) and then press the "start/continue" button again, then I get the three types of pulses right. What's wrong here? Maybe that's the key to the solution!
I really appreciate your help.