EdgeTX / edgetx

EdgeTX is the cutting edge open source firmware for your R/C radio
https://edgetx.org
GNU General Public License v2.0
1.6k stars 341 forks source link

crashed RM TX16S with touchscreen in test lua script #914

Closed MNS26 closed 2 years ago

MNS26 commented 3 years ago

Describe the bug RM TX16S touchscreen breaks lua script and reopening it crashes the radio (found out about this with ELRSv2 script)

To Reproduce the most reliable way to do it is to (open ELRSv2 -> swipe around -> scroll and hear beeps -> plug in usb -> sd mode -> disconnect -> reopen ELRSv2 -> crash)

examples https://drive.google.com/file/d/1tUkWwpORuFSGrMxJNBBed3axxf3IuZDo/view?usp=sharing https://youtu.be/cJGlfYjm8DI

Expected behavior for it to ignore touch event if not added to the script

Screenshots (see video)

General information

pfeerick commented 3 years ago

I have a much simpler way, as touch is a red herring ;) It's also not related to ELRSv2, as it's present for ELRSv1 also.

Open ELRS v1, plug in usb, unplug usb, Re-open ELRSv1, EM

The underlying issue appears to be the ELRS script being open when USB is connected. If you exit it before plugging in the USB, it is fine.

Plug in USB with Lua script open: https://youtu.be/HCpBQjakh6Y

Plug in USB with Lua script closed: https://youtu.be/LQUX7Jjjvy0

raphaelcoeffic commented 3 years ago

I guess this holds only for the mass storage device mode, right?

MNS26 commented 3 years ago

@raphaelcoeffic i used mass storage mode because that was the only way to get out of the script but ive had plenty of crashes by just reopening the script

oh and yes its not ELRSv2 related but i found out about this issue with that specific script, and because its quite complex it had a quite consistent outcome (about 75% or more)

raphaelcoeffic commented 3 years ago

@MNS26 so what you say if that it is not only related to plugging USB? Or that it would crash as well with other USB modes?

MNS26 commented 3 years ago

was about to say something about that

MNS26 commented 3 years ago

here is it crashing without using USB (it took a few more attempts compared to with USB storage mode) https://youtu.be/i3GJMiUjAZI

@pfeerick USB storage mode and the script being used could definitely be a factor. But it still crashes without touching USB storage mode, and note how the home screen changed after it breaks the script and i exit the script

MNS26 commented 3 years ago

oh and the reason im not using the enter button on the scroll wheel is because im waiting for a replacement due to a broken button

MNS26 commented 3 years ago

@raphaelcoeffic i have a feeling we're looking at multiple different crashes as if they are ene.

I'm sure the touchscreen issue in standalone lua scripts is definitely one of them. But i feel like the USB storage mode might be an issue with files/scripts being loaded and then not being closed properly when entering USB storage mode.

raphaelcoeffic commented 3 years ago

@MNS26 i was thinking the same right now.

MNS26 commented 3 years ago

@raphaelcoeffic my main issue is that its next to impossible to reproduce in the sim due to it crashing IF it does works.... Is there a way to temporarily sacrifice 1 of the uarts for a debug data stream of what the state and active processes on the device are?

jfrickmann commented 3 years ago

Please test this on the current main and see if #899 fixed the issue

MNS26 commented 3 years ago

@jfrickmann nope it still crashed (USB and touch)

MNS26 commented 3 years ago

i also found something else on the latest master branch (not related to this problem) im assuming its supposed to be XXhXXm

20211018_194033 1

pfeerick commented 3 years ago

i also found something else on the latest master branch (not related to this problem) im assuming its supposed to be XXhXXm

Yes, it's in the process be being re-written, so expected behaviour.

pfeerick commented 3 years ago

@raphaelcoeffic i have a feeling we're looking at multiple different crashes as if they are ene.

I'm sure the touchscreen issue in standalone lua scripts is definitely one of them. But i feel like the USB storage mode might be an issue with files/scripts being loaded and then not being closed properly when entering USB storage mode.

Yeah, that pretty much has to be the case, and I wonder if it could also be related to it being a script that talks to the module, rather than a user interactive script only... it is strange that a Lua script failure is knocking out a internal widget like outputs - wonder if it means it's more than the lua script playig up and the ELRS module isn't talking properly...

MNS26 commented 3 years ago

@pfeerick well ANY lua script running not as a widget has this issue. i just use the ELRSv2 one because it crashes the radio the most reliable (likely due to its complexity)

pfeerick commented 3 years ago

Keeping in mind touch support hasn't been added to standalone scripts (only widgets), I'm not sure how that can even be a factor here! :-O Regardless, something is not working, we're just not 100% clear on the what yet.

MNS26 commented 3 years ago

@pfeerick its midnight here so i will fiddle with it tomorrow. I also just found out that if im scrolling with the wheel and touch the display it starts to beep but only when running the script by selecting it through the sd card

pfeerick commented 3 years ago

Ok, I think I just managed to pin down real "launch through SD browser" problem - for some reason the script is not either not launching properly or touch is somehow changing focus. I was thinking since you must have the sounds set to all (to be getting beeps), rather than NoKey, I turned that one, then went into sd browser, scripts/tools, and ran the script there. Wjem I scroll back and forwards with the roller, the hi-lighted menu entry correct updates. but if I swipe the screen a few times, it doesn't. But the scroll wheel is still beeping when it's turned (so input is being detected), so when I press enter after a couple beeps, it pops up the context menu from the file browser... so it seems some touch events are somehow changing the focus back to the underlying sd browser (not crashing the script/lua, as the ELRS counter is still flickering back an forward every now and then). And once this crazy state has been triggered, it seems like that also crashes into EM if you then go back into the script via tools. This also effects 2.5.0 since nothing has been changed in this area of code recently.

If you've built the firmware yourself with the -DDEBUG=Y flag set, the left uart on the RM TX16S will be a debug port (or you can use the USB HCI / USB Serial mode option that will also now be an option when you connect the top usb serial port to the PC. However, I don't think it will show anything useful, as there is no message in the debug log on simulator when that happens, just that the file-browser behaviour is happening when you scroll again and it needs to figure out the paths to files.

MNS26 commented 3 years ago

@pfeerick i just realized that in the sim it shows [ViewMain] Focus lost when you scroll and [ViewMain] Focus gained then clicking the screen. and thanks for the tip on how to get the debug data, i will try and make a version with it enabled

EDIT: i just did some tests and i managed to get the sim to also break the UI (selected line is the first one after it broke) image

and opening basically any file caused a Data buffer overflow. the text file is just a lot of random characters to fill up memory when opened image

pfeerick commented 3 years ago

Excellent. [ViewMain] Focus lost was what I was expecting to see. In the next day or so it will be a matter of running the code up again with the debugger attached and seeing why it is jumping out / losing focus...

MNS26 commented 3 years ago

i havent been able to track down the issue with USB yet, but im very suspicious of how it handles files that are open when going into storage mode

pfeerick commented 3 years ago

It will probably need the debugger as well, as it is probably something nasty to do with the state the script was in since it is talking to the module... and then Lua basically getting re-initialised when you pull the USB so that changes to scripts are detected, etc...

MNS26 commented 3 years ago

the USB craash is not related to just the ELRS script, ive crashed it with just a test one i made

local function run(event)
  lcd.clear()
  lcd.drawText(0, 0, "Hello World")
  return 0
end

return { run = run }

although its a VERY hit or miss situation with it because its so small and have a hunch its roughly the same as the other bug and not clearing it from memory

EDIT: it took 3x before it brashed this time

pfeerick commented 3 years ago

Oh excellent, that rules out a few things all by itself... nice! :) It's pointing more and more to something the last script state not being being cleared, and it thinking it can just resume... badly... (at a guess)

MNS26 commented 3 years ago

sadly its impossible to test that theory in the sim... no usb, im currently in the process setting up the build environment with WSL

MNS26 commented 3 years ago

@pfeerick pfff... finally got it to work using the left uart (to keep USB SD) but all i see is garbage on the serial port image

rotorman commented 3 years ago

This looks like wrong baudrate. Is it TX16S? Try 400.000 baud 8N1 on PC side.

MNS26 commented 3 years ago

if i try to go that high it throws an error saying its out of range image

pfeerick commented 3 years ago

Whatever terminal or adapter you're using probably doesn't support that board rate, as 400000 baud is correct. Try putty (I use kitty, a derivative of putty).

MNS26 commented 3 years ago

Ive tried multiple adapters (silab, arduino) just to name a few and all weren't able go go that high. Ibe trird it will putty but that ends up ad a COMx not available message

rotorman commented 3 years ago

I am using hTerm: https://www.der-hammer.info/pages/terminal.html and following FTDI adapter integrated into cable: https://www.sparkfun.com/products/9717 Can recommend both. On the FTDI adapter, do NOT connect the power rail (5V), just GND and TX from radio (RX on FTDI) is enough for getting debug out. If you opt for another product, make sure it has 3.3V I/O levels.

pfeerick commented 3 years ago

Those serial console cables are good... I can recommend them also ;) I've also found CP2102 based usb-to-serial uart boards have wide compatibility, both for low speed and high speed 1.5M speeds...

MNS26 commented 3 years ago

ive tried a CP2102 as well and got up to 115000 but not to 4000000 and h-term also wasnt able to go that high i will get one of those serial cables but it will take forever to get here due to global shipping issues

rotorman commented 3 years ago

You should use four hundred thousand baud (400.000) not 4 million (not 4.000.000)!

MNS26 commented 3 years ago

even at 400000 baud its still garbage image

rotorman commented 3 years ago

Do you have a oscilloscope or a logic analyzer, like Saleae or similar, that you could hook up to TX16S AUX1 TX pin to be able to see how the signal and timing looks that it outputs for you?

dlktdr commented 3 years ago

Had a bit of grift getting mine to work right away too. I also have a CP2102. Is it a recent driver?https://www.silabs.com/documents/public/software/CP210x_Universal_Windows_Driver.zip

I use terra term and it shows more available serial bauds. You can set targets/horus/board.h LN# 689 to match one of the available bauds if 400,000 isn't listed. EDIT yes line 689, Was late and I was on a different branch :) Permalink next time :)

Can also check this link too http://www.olliw.eu/storm32bgc-wiki/How_to_configure_CP2102_USB_adapters_for_high_baud_rates

The utility link isn't working but here is a copy https://www.silabs.com/documents/public/example-code/AN721SW.zip

MNS26 commented 3 years ago

@dlktdr thanks for the driver link, but ive already got the latest ones

splitting the serial cable i have and checking the signals this is what it gives and as you can see there is definitely the data image

but what i receive is still garbage (even tried an old FTDI that i found that was salvaged from a Arduino about 4y ago) image

MNS26 commented 3 years ago

i think found the issue?.... h-term is reading the bits backwards..

this is the output of the last one image

and this is what h-term sees image

MNS26 commented 3 years ago

so... even though it looks absolutely F^^^ed it is the exact same values 🤔

oh well im going to take a break from it for now

pfeerick commented 3 years ago

lol... use putty/kitty... it's as simple as select serial, enter com port number, speed 400000, and open! 😆 You don't need anything more than that since this is just to see the debug output ;)

image

MNS26 commented 3 years ago

@pfeerick the issue is that somehow the data becomes garbage so "just use putty" isnt that easy because it will only output valid ascii characters but.... if none of the data is valid..... nothing will show

(furiously scrolled with the roller for 5 sec and.... nothing... at... all) image

pfeerick commented 3 years ago

Hm... that makes no sense... you logic analyser says the bits are the right way around, and that hterm is seeing them the wrong way around... but if putty is doing it also, it looks like your uart is somehow reading in wrong LSB/MSB order?

MNS26 commented 3 years ago

is there a way to... oh idk.. force a low baud rate (something like 115200)? if i use serial over usb-c it works but i cant test the usb SD crash then

MNS26 commented 3 years ago

and i know the serial adapter is fine because i just tested it with my arduino (prints E until it receives A) image

pfeerick commented 3 years ago

Cliff mentioned it earlier ;)

I use terra term and it shows more available serial bauds. You can set targets/horus/board.h LN# 670 to match one of the available bauds if 400,000 isn't listed.

I think he was just out a couple of lines?

https://github.com/EdgeTX/edgetx/blob/a36aac0c901c7f5655558b8682e03d8fbb22f6b0/radio/src/targets/horus/board.h#L689

MNS26 commented 3 years ago

isnt that for the Horus and not for the TX16S?

rotorman commented 3 years ago

One line above it, it says: https://github.com/EdgeTX/edgetx/blob/a36aac0c901c7f5655558b8682e03d8fbb22f6b0/radio/src/targets/horus/board.h#L688-L689

I assume the issue lies that your CP2102 cannot do 400000 with the current settings or driver. Best, have another look at OlliW website that Cliff provided above to you already: http://www.olliw.eu/storm32bgc-wiki/How_to_configure_CP2102_USB_adapters_for_high_baud_rates

MNS26 commented 3 years ago

ive already used it to change the max baud rate but still no luck will make a version of the firmware that has the debug baud rate at a lower speed (400k baud seems somewhat excessive also)