earlephilhower / arduino-pico

Raspberry Pi Pico Arduino core, for all RP2040 and RP2350 boards
GNU Lesser General Public License v2.1
2.03k stars 422 forks source link

USB stops during Arduino IDE upload when using 2nd core and watchdog #1653

Closed greymfm closed 1 year ago

greymfm commented 1 year ago

Hello, I have stripped down our complex code (using 2nd core and watchdog) to some minimum example code. In the Pico library version 2.0.3 our code did work, in version 3.3.2 the USB disconnects during Linux Arduino IDE (1.8.19) uploads.

[1140119.996026] usb 1-3: New USB device strings: Mfr=1, Product=2, SerialNumber=3 [1140119.996031] usb 1-3: Product: Pico [1140119.996035] usb 1-3: Manufacturer: Raspberry Pi [1140119.996038] usb 1-3: SerialNumber: E4622C87CB54282D [1140120.000989] cdc_acm 1-3:1.0: ttyACM0: USB ACM device [1140168.497316] usb 1-3: USB disconnect, device number 76

Example code to reproduce:

test_usb_issue.zip

  1. If I do not use the watchdog, USB does not disconnect.
  2. If I use watchdog and add some small delay, USB does not disconnect.
  3. If I use watchdog without a delay, USB disconnects.

In our complex code, the 2nd core computes and controls time-critical signals (5000 Hz) for a brushless motor (SimpleFOC), and if possible I don't want to add any delays to the code.

How could I get rid of the USB disconnects in latest Pico library? :-) Or another idea: how could I detect that the Arduino IDE is going to upload (to go into some delay loop so that the USB disconnect does not happen)...

Thanks, Alexander

maxgerhardt commented 1 year ago

multicore_launch_core1(core1_entry);

This seems very weird, Arduino-Pico already looks for setup1() and loop1() functions as the code for the second core to run. Maybe you're bypassing some internal initialization that way.

And just to understand you correct, the crashing "number 3" behavior is exhibited when the code in line 92 is made active and line 71 is commented out?

greymfm commented 1 year ago

Thanks, I changed the code to use 'setup1()' and 'loop1()' instead (updated above link). Same results (USB still disconnects right before Arduino IDE uploads). Yes, exactly, the IDE upload no longer works when commenting out this line:

  //delayMicroseconds(1);  // delay makes the USB stack not to stop during Arduino IDE uploads!
maxgerhardt commented 1 year ago

Can you narrow down the faulty versions further? 2.0.3 (June 2022) to 3.3.2 (August 2023) is still a wide range. Usually this is done with a git bisect but I think you can also easily install e.g. 3.0.0, 2.7.3, 2.4.0 and retest, which would help alot.

greymfm commented 1 year ago

For some reason I get the issue with all tested Pico library versions (2.0.3, 2.4.0, 2.7.3, 3.0.0, 3.3.2). However, I don't get the issue in our complex code with the old Pico library. Anyways, I think I have found a workaround (will go into a delay loop when we start an IDE upload and then the Pico will properly reflash via the IDE). I think we can close this for now as I cannot reproduce any difference between the different Pico library versions using the minimum example code...