Hi,
If I enable Watchdog timer "wdt_enable(WDTO_8S);" the Arduino Leonardo start closing randomly the USB-CDC port, also to upload sketch I have to hold the reset button.
"wdt_reset();" is placed in the sketch and is called frequently. There is no way this is the issue (same sketch works fine on UNO and also on Atmega with FT232RL)
To reopen the port you have to reset the board or the watchdog has to overflow and reset.
Anyone knows what the problem is? Any timers or interrupts that watchdog timer is using that are causing the issue with USB-CDC?
Running on linux with newest Arduino IDE, newest drivers and Leonardo is connected to /dev/ttyACM0. I also tried to ln -sf to /dev/ttyS0.
I stream 80bytes every 10ms at 115200kbps and leonardo is just forwarding the data to SPI at 4MHz (I tried to change all speeds)
The errors are:
write(): write() Input/Output error ttyACM0 not such ...,
dmesg output: USB disconnect, device number ...
it is happenning randomly (can't really find any pattern)
I'm trying to figure it out for the last 5 days and just today I discovered it is the watchdog timer, that is causing the issue.
I needed the watchdog to escape the forward routine in case the serial data are not available, so it will restart and turn on failsafe until Serial.available(); I'm not looking for other solution (can be done with millis() or inceremental counter), but I'm really curious why is it happening.
wdt_enable was in setup()
and wdt_reset was in loop()
is that what you're asking?
I have the failsafe working OK now with this code using millis():
data_millis = millis(); //time when receiving started
for (i = 0; i < number_of_bytes; i++) { //receive data
while (!Serial.available()){
if ((millis() - data_millis) > timeout) { //timeout occured?
error = 1;
i = number_of_bytes;
break;
}
}
if (!error) data[i] = Serial.read(); //save incoming data to data[]
}
if (!error) new_data = 1;
the old one was that wasn't working was (typed here again):
wdt_reset();
for (i = 0; i < number_of_bytes; i++) { //receive data
while (!Serial.available());;
data[i] = Serial.read(); //save incoming data to data[]
}
Code snippet rarely help. Which is very true in this case.
I know, there is not much really in the code that could cause the problem. I thought maybe timers? Is Leonardo using the same timer for USB and watchdog? Or interrupts?
How is the reset on upload handled? - Can the issue be that the WDT is actually already used by bootloader for sketch_upload?
Does anyone know?
What is most annoying is that I can't find any pattern when Leonardo disconnect the USB port with the watchdog enabled. And after the port is disconnected, it will take the time set in watchdog timer to re-open.
I'll leave it here and see if anyone will have similar problem or is experiencing the same difficulties with the watchdog enabled. I'm gonna mention again that i used to have the same code running on UNO for the last year without any issues.
The Leonardo USB interface is running as part of the core code in the sketch. So whenever the WDT kicks in, it will stop the USB and have to re-enumerate to the host computer. You must keep in mind the USB is part of the core sketch.
So if you are not sending any serial data to the USB, the WDT will reset the Leonardo every 8 seconds and cause the USB to have to re-enumerate each time.
The Leonardo USB interface is running as part of the core code in the sketch. So whenever the WDT kicks in, it will stop the USB and have to re-enumerate to the host computer. You must keep in mind the USB is part of the core sketch.
So if you are not sending any serial data to the USB, the WDT will reset the Leonardo every 8 seconds and cause the USB to have to re-enumerate each time.
I'm aware of this. Actually that is exactly what I use it for. I have it there as a failsafe if the data stream will stop, it won't freeze but reset itself after 8 seconds, setup failsafe values and wait again for the Serial data. As soon as the serial data are available again, it will keep forwarding. I used WDT because it will reset if the Leonardo freeze in any part of the code.
The data are flowing OK and then suddenly the Leonardo close the USB port as I would pull the USB cable out. No delay in data.
hiduino:
So if you are not sending any serial data to the USB, the WDT will reset the Leonardo every 8 seconds and cause the USB to have to re-enumerate each time.
but , if I send data to serilal? how to fix loop-reboot on leonardo after wdt reset ?
PS: I have updated last caterina boot leonardo for my board and loop-reset still present