# Custom firmware is being killed after 1-2 seconds \[Solved\]

**URL:** <https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635>\
**Category:** Firmware\
**Created:** [December 6, 2014, 9:40pm UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635 "2014-12-06T21:40:45Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![0avid](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/0avid/32/3782_2.png) [@0avid](https://community.particle.io/u/0avid)\
**Post date:** [December 6, 2014, 9:40pm UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/1 "2014-12-06T21:40:45Z")

</div>

Hi all,

I’m working on some custom firmware for the Spark Core. For non-technical reasons, I’m not interested in using the Spark firmware or wifi, just the bootloader and my own code.

I can download to the Spark using DFU just fine, and the application runs as expected for 1-2 seconds. After that, the spark stops running the firmware I just downloaded and either:

1. flashes red rapidly, then blinks yellow like it’s in DFU mode
2. turns white, flashes white rapidly, then deep blue like it’s looking for a wifi connection

I have videos of these two scenareos, if they would be helpful to see. The applicaton code just reads a digital pin and turns on some LEDs in a loop. Could this have to do with a watchdog timer somewhere I’m missing? or something with the bootloader?

Thanks!

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [December 6, 2014, 10:00pm UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/2 "2014-12-06T22:00:40Z")

</div>

If possible, would you mind sharing the code?

---

<div class="post-metadata">

**Author:** ![0avid](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/0avid/32/3782_2.png) [@0avid](https://community.particle.io/u/0avid)\
**Post date:** [December 6, 2014, 10:03pm UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/3 "2014-12-06T22:03:49Z")

</div>

Here it is:

```auto
#include "stm32f10x.h"

int main() {
  // initialization
  int count = 0;
  int button_down = 0;

  GPIOB->CRL = 0x11111111; // set all port B pins to output push-pull
  GPIOB->CRH = 0x11111111;
  
  GPIOA->CRL = 0x44444444; // set all port A pins to floating input
  GPIOA->CRH = 0x44444444;
  
  for (;;) {
    
    // set D0-2 to match count
    GPIOB->ODR = (GPIOB->ODR & 0xFFFF0000) | (count << 5);
    
    // if A0 is pressed, increment count
    if (GPIOA->IDR & 1 & ~button_down) {
      button_down = 1; 
      if (count < 7) {
        count++;
      } else {
        count = 0;
      }
    } else if (~button_down) {
      button_down = 0;
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![Moors7](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/moors7/32/588_2.png) [@Moors7](https://community.particle.io/u/Moors7)\
**Post date:** [December 6, 2014, 10:16pm UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/4 "2014-12-06T22:16:32Z")

</div>

Thanks, that should make it easier to debug. Since this goes a bit above my head, I’m going to ping the reinforcements: @Dave, @kennethlimcp, @peekay123, @bko. I’d be highly surprised if they won’t be able to help you out. Good luck!

---

<div class="post-metadata">

**Author:** ![kennethlimcp](https://avatars.discourse-cdn.com/v4/letter/k/c0e974/32.png) [@kennethlimcp](https://community.particle.io/u/kennethlimcp)\
**Post date:** [December 7, 2014, 12:20am UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/5 "2014-12-07T00:20:48Z")

</div>

I’m guessing it’s the watchdog that is causing the issue 🙂

---

<div class="post-metadata">

**Author:** ![0avid](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/0avid/32/3782_2.png) [@0avid](https://community.particle.io/u/0avid)\
**Post date:** [December 7, 2014, 3:04am UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/6 "2014-12-07T03:04:02Z")

</div>

Any advice on how to remedy this? The relevent TRM says the watchdog is disabled by default, so I’m assuming the bootloader does something with it before my code runs. Actually, I’m not 100% sure on how the bootloader works anyway. How does it run with my firmware in there?

Thanks for the attention and suggestions 🙂

EDIT: Some discoveries.

This behavior is indeed being caused by the independent watchdog timer. Feeding the dog at the end of the loop with

```auto
IWDG->KR = 0xAAAA;

```

stops the resets from happening. The TRM doesn’t mention how to disable the watchdog from software, so I’m guessing it’s impossible (unless someone here knows otherwise!). I guess I’d like to know if there’s ANY way to disable the watchdog without breaking out a jtag programmer and messing with the bootloader. Otherwise, I’d be willing to do that as a last resort.

---

<div class="post-metadata">

**Author:** ![bko](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bko/32/20704_2.png) [@bko](https://community.particle.io/u/bko)\
**Post date:** [December 7, 2014, 4:17am UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/7 "2014-12-07T04:17:56Z")

</div>

This thread has a lot of info:

> **[Can the Spark Core trigger the Reset Pin?](https://community.particle.io/t/can-the-spark-core-trigger-the-reset-pin/2693)**
>
> I have a spark core and I’m using it now to push sensor data to Xivley which works fine as long as there are no CC3000 issues that pop up that kills the connection or freezes the Spark Core. I have read that sometimes the code in the main loop still...

Since @BDub is now at Spark, I am he knows what the code is doing.

Maybe the best approach is the change the prescaler and “kick the dog” in your code.

---

<div class="post-metadata">

**Author:** ![0avid](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/0avid/32/3782_2.png) [@0avid](https://community.particle.io/u/0avid)\
**Post date:** [December 7, 2014, 4:55am UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/8 "2014-12-07T04:55:43Z")

</div>

Yup, that’s what I’m doing right now. AFAIK, there’s no way to avoid having to feed the dog unless I mess with the bootloader, but for the time being that’s out of the question. I’ll follow up on this if I ever decide to go down that road.

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [December 15, 2014, 5:10am UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/9 "2014-12-15T05:10:04Z")

</div>

Looks like you figured this out already, but my advice would be to keep the watchdog active. It’s always a good idea to prevent your application from completely locking up due to a hard loop. I’m sure there is some edge case you could come up with that would be better off without a watchdog, so it pays to just think about it thoroughly.

---

<div class="post-metadata">

**Author:** ![0avid](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/0avid/32/3782_2.png) [@0avid](https://community.particle.io/u/0avid)\
**Post date:** [December 15, 2014, 5:31am UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/10 "2014-12-15T05:31:24Z")

</div>

My reasoning for wanting to disable the watchdog was non-technical. I think I’ve found out how to disable it by writing to a backup register that the bootloader reads. Haven’t tried this yet because it’s pretty low on the priority list right now. For now I’m leaving it on while I work on other things.

---

<div class="post-metadata">

**Author:** ![BDub](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/bdub/32/5137_2.png) [@BDub](https://community.particle.io/u/BDub)\
**Post date:** [December 15, 2014, 6:08am UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/11 "2014-12-15T06:08:54Z")

</div>

Right, certainly while you are debugging and creating your software it’s a good idea to disable it to ensure you have one less thing getting in your way when you write code that will trip over the watchdog.

Also once it’s enabled with writing 0xCCCC to the IWDG-\>KR register… AFAIK there’s no way to disable it.

> <https://github.com/particle-iot/core-common-lib/blob/master/STM32F10x_StdPeriph_Driver/src/stm32f10x_iwdg.c>

Writing 0xA5A5 to backup register BKP\_DR9 looks like it will prevent IWDG from being enabled, but it’s used to signal Standby Mode…

> <https://github.com/particle-iot/bootloader/blob/master/src/main.c>

So what you should probably do as a work around to make sure BKP\_DR9 stays set is just write 0xAAAA to BKP\_DR9 like this:

```cpp
BKP_WriteBackupRegister(BKP_DR9, 0xAAAA);

/* Optionally SW Reset System */
NVIC_SystemReset();

```

---

<div class="post-metadata">

**Author:** ![0avid](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/0avid/32/3782_2.png) [@0avid](https://community.particle.io/u/0avid)\
**Post date:** [December 15, 2014, 6:29am UTC](https://community.particle.io/t/custom-firmware-is-being-killed-after-1-2-seconds-solved/8635/12 "2014-12-15T06:29:24Z")

</div>

Interesting, I’ll give it a shot next time I work on this.

My reason for wanting to disable the watchdog timer is that I’m working on course material for a university microcontroller programming class. We don’t want to introduce the watchdog until later in the semester.
