Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCS_MAVLink: Avoid serial passthrough buffer exhausted/lost data #26883

Merged
merged 1 commit into from
Apr 29, 2024

Conversation

brad112358
Copy link
Contributor

Rework the serial port write logic in passthru_timer() to use the return value to detect partial writes due to the serial buffer filling up. Unwritten data is saved in buf1 or buf2 and the write is retried on the next iteration. This avoids lost data when the speed of the two serial ports differs and large blocks of data are sent.

For example, flash programming of ESP MCUs via USB to serial passthrough results in writes of more than 6000 bytes at a time which quickly fills the serial port buffer.

The existing code fails when flashing ELRS receivers via serial passthrough of ELRS which has become a more common need with the mavlink-rc branch of ELRS and the recent support of ESP based receivers in mLRS. This is especially troublesome with AIO flight controllers which may not provide easy access to the serial port connecting the FC to an integrated ELRS receiver.

@olliw42
Copy link
Contributor

olliw42 commented Apr 25, 2024

excellent !

@IamPete1
Copy link
Member

Why not use port->txspace() to only read what there is room to write? I think that would remove the need for the buffers.

@olliw42
Copy link
Contributor

olliw42 commented Apr 25, 2024

you mean doing something like read_locked(buf, MIN(sizeof(buf),port->txspace()), lock_key); , right?

@brad112358
Copy link
Contributor Author

Why not use port->txspace() to only read what there is room to write? I think that would remove the need for the buffers.

Another question is why didn't I think of that. The extra memory used was bothering me.

Yes, that is a much better idea and also simplifies the code! I'll start over. :)

@IamPete1
Copy link
Member

you mean doing something like read_locked(buf, MIN(sizeof(buf),port->txspace()), lock_key); , right?

Yes, exactly.

@brad112358 brad112358 force-pushed the passthrough_overflow branch from bc1286e to 37c86fd Compare April 25, 2024 16:32
@brad112358
Copy link
Contributor Author

brad112358 commented Apr 25, 2024

Thanks @IamPete1! I tested and force pushed your much better approach. So simple. It has been pointed out to me that people have been discussing problems flashing ELRS via Ardupilot serial passthroug for more than 2 years. Since it is now such a minimal change, perhaps we can get this into a release soon?

Copy link
Contributor

@peterbarker peterbarker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do realise the term "buffer overflow" requires a trigger warning, right?

AFAICS there's no buffer overflow here, the data is simply lost, correct?

@brad112358
Copy link
Contributor Author

Yes, of course. I should have said buffer exhausted. I will fix the commit comment.

@brad112358 brad112358 changed the title GCS_MAVLink: Avoid serial passthrough buffer overflow/lost data GCS_MAVLink: Avoid serial passthrough buffer exhausted/lost data Apr 26, 2024
@brad112358 brad112358 force-pushed the passthrough_overflow branch from 37c86fd to 707bbfc Compare April 26, 2024 01:39
@andyp1per
Copy link
Collaborator

I could not update the firmware on my F9P without this - afterwards it was flawless!

@tridge tridge merged commit e8d2097 into ArduPilot:master Apr 29, 2024
91 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 4.5.2-beta1
Development

Successfully merging this pull request may close these issues.

6 participants