[SOLVED] Arduino 32u4 (Caterina bootloader) freeze when external power is apply

Hello,

When I remove the USB and plug extenal 12V or even direct 5V, the Arduino 32U4 jams and screw up the bootloader.

I am using the Arduino A-star Pololu - A-Star 32U4 Mini SV (ac02c) from Pololu. It's the same as an Arduino Micro. It use the Caterian bootloader. The bootloader of the A-Star is modify to load the sketch really fast. Here is the info Pololu - 7. A-Star 32U4 Arduino library

I programmed my sketch using NilRTOS and have tried ChibiOS and LeOS. I got my code to work and I am very happy.
The problem I can not make this sketch work with external power. Even pressing reset does not restart my sketch.

When I remove the USB and plug extenal 12V or even direct 5V, the Arduino jams and screw up the bootloader. When that happen, I go throught a special procedure, I need to reload a sktech in the bootloader serial port. I have the same problem with the Arduino Micro.

Here is (private) video of the problem

I upload blink sktech and don't have that problem. This mean that the problem could be in the library of the ChibiOS/NilRTOS and LeOS. It has to do with interrupt.

I think that the problem is with ISR or the watch dog. I read that some people had issue "chip will freeze itself in a neverending loop caused by eternal resets" with the ATMEGA2560 http://www.leonardomiliani.com/en/2012/leos-un-semplice-so-per-arduino/?lang=en

Could you guys give me clues or if you hear similar problems with the ATmega32u4

When using on the ATmega 328, I have no problem at all. I can use the external power supply and the "RTOS" sketch is working fine.

I have found my mistake. Nothing to do with bootloader. The problem is at 12inch from the screen.

This is a copy paste from my post on the ChibiOs forum:


I deleted all my serial println and was able to pin point my mistake!!

I wanted to make a #define DEBUG where I could remove all my serial printf from my code instantly! To save memory.

#define DEBUG 1 

#ifdef DEBUG
void SerialDebug(const char* sMsg){
     Serial.println(sMsg);
}
#endif

Then I use this

 SerialDebug("thread1 thread initialize");

What killed the code is this: while (!Serial) {}

void setup() {

  #ifdef DEBUG
    Serial.begin(9600);
    // wait for USB Serial
    while (!Serial) {}            <======= I deleted this line!
    SerialDebug("9600 baud rate");    
  #endif  
  
   
    
  // Start Nil RTOS.
  nilSysBegin();
  
  SerialDebug("Define is set");   
}