jbliesener
Well-known member
I'm stuck with a problem that I believe to be in the USB flightsim stack. The attached minimal sketch demonstrates the issue. It doesn't require any additional hardware and only needs X-Plane with the plugin running when the Teensy starts up. No inputs or external wirings are required. The sketch should blink the Teensy's LED in regular intervals after an initial startup phase, instead, it freezes on the first call to FlightSim.update().
After observing the problem on Arduino 1.6.5/Teensyduino 1.27(?), I updated to Arduino 1.6.9/Teensyduino 1.29 beta. No change.
To me, it looks like a very subtle timing issue when sending and reallocating USB buffers from the low-level Flightsim communications (FlightSimClass::xmit and related calls to the USB stack). Maybe, that could affect other USB devices as well if they implement a similar buffer handling.
The freeze occurs as soon as you have more than 16 outstanding packets on the first call of FlightSim.update(), as soon as the identify-functions are called for each variable.
All of those packets are below the threshold size that would trigger my "xmit_big_packet" function, that I contributed a while ago. Also, those packets ARE actually sent to X-Plane (I can see it on the Plugin's comm-log), but the buffer doesn't seem to become available to usb_malloc to be reallocated.
I suspect a timing issue, as uncommenting the serial_print-functions in usb_malloc makes the program behave differently: only a single packet is transmitted to X-Plane. If I uncomment the serial_print-functions in usb_free, the sketch seems to run. There are other ways to make it run:
None of them work for our practical case though, as we with about 42 datarefs in our sketch and NUM_USB_BUFFERS is limited to 32, due to the 32 bit usb_buffers_available bitmap.
I also tried playing with the CPU frequency and optimization settings, and at one point the program was actually running when I set it to 24 MHz. I sent the sketch to raflyer, but he couldn't reproduce it. After upgrading to 1.6.9/1.29beta, I wasn't able to reproduce it either.
As those USB stack functions are very deeply integrated in your USB stack, I don't feel confident enough to make any changes. Maybe, it's only about moving a __disable_irq() a line up or an __enable_irq() a line down, but I'm unsure about the implications.
Could you take a look at usb_flightsim.cpp, maybe especially at FlightSimClass::xmit and tell me if you see something wrong?
After observing the problem on Arduino 1.6.5/Teensyduino 1.27(?), I updated to Arduino 1.6.9/Teensyduino 1.29 beta. No change.
To me, it looks like a very subtle timing issue when sending and reallocating USB buffers from the low-level Flightsim communications (FlightSimClass::xmit and related calls to the USB stack). Maybe, that could affect other USB devices as well if they implement a similar buffer handling.
The freeze occurs as soon as you have more than 16 outstanding packets on the first call of FlightSim.update(), as soon as the identify-functions are called for each variable.
All of those packets are below the threshold size that would trigger my "xmit_big_packet" function, that I contributed a while ago. Also, those packets ARE actually sent to X-Plane (I can see it on the Plugin's comm-log), but the buffer doesn't seem to become available to usb_malloc to be reallocated.
I suspect a timing issue, as uncommenting the serial_print-functions in usb_malloc makes the program behave differently: only a single packet is transmitted to X-Plane. If I uncomment the serial_print-functions in usb_free, the sketch seems to run. There are other ways to make it run:
- Reduce the number of datarefs to 16 (see comment in sketch)
- Increase NUM_USB_BUFFERS in usb_desc.h to 32
None of them work for our practical case though, as we with about 42 datarefs in our sketch and NUM_USB_BUFFERS is limited to 32, due to the 32 bit usb_buffers_available bitmap.
I also tried playing with the CPU frequency and optimization settings, and at one point the program was actually running when I set it to 24 MHz. I sent the sketch to raflyer, but he couldn't reproduce it. After upgrading to 1.6.9/1.29beta, I wasn't able to reproduce it either.
As those USB stack functions are very deeply integrated in your USB stack, I don't feel confident enough to make any changes. Maybe, it's only about moving a __disable_irq() a line up or an __enable_irq() a line down, but I'm unsure about the implications.
Could you take a look at usb_flightsim.cpp, maybe especially at FlightSimClass::xmit and tell me if you see something wrong?