AnsweredAssumed Answered

Reset keeps occuring after program started.

Question asked by piquois.romain on May 20, 2012
Latest reply on May 29, 2012 by lopez.pedro.001

I have just received my discovery board and started to port my code over it but encountered the following problem : if I put a breakpoint at the entry point (first line of code of "main" function), I noticed that the breakpoint is hit more than once.
Step by step works fine, but breakpoint + fail show this behavior.

=> So my only conclusion is that a RESET occurs when running.

So I wrote the following little code to try to find out when and how it is reset :

#include "stm8s.h"

void main(void) {
    u16 cnt = 0;    // Counter

    // Disable WWDG to provoke reset.
    WWDG->CR = 0x7F;     // Disable WWDG reset mecanism. (Bit 6 is set to avoid software reset right away.) and counter set to 0x3F, Bit 7=0 Disabled
    WWDG->WR = 0;        // Comparison to occur when reach 0, in case.
    // IWDG Setup
    IWDG_SetPrescaler(IWDG_Prescaler_256);    // Reduce to minimum speed.
    IWDG_Enable(); // No disable found in the library...
    IWDG_SetReload(0xFF);                    // Reload counter to max to avoid reset.
    for (;;) {
        GPIO_WriteReverse(GPIOD, GPIO_PIN_0);
        IWDG_SetReload(0xFF); // Reload counter to avoid reset.
        if (cnt > 7000) {
            GPIO_WriteReverse(GPIOD, GPIO_PIN_0); // Put breakpoint HERE


As you can see, it is a very basic program that :
- Setup the clock
- Initialize the port for the LED
- Disable the Window WatchDog (WWDG)
- Set the IWDG regularly to avoid reset.

Now the interesting thing is that if I set up a breakpoing inside the IF condition, if I setup a value like 6000, I can reach the breakpoint.
If the value is set to 6500, the breakpoint is never reachable = the cpu has reset before reaching that value.

As I tried to control the only TWO sources that could generate a RESET, I am stuck and have no idea why the reset still keep occuring.

Any idea or hint would be a really really big help.