From 1056d13dadc679ee78ec46277ea32851b49d64fb Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Fri, 4 Dec 2026 17:20:35 +0100 Subject: [PATCH] S.Port: no broken packets when the radio pushes too fast, no stalls after a full buffer (#1224) * S.Port: drop a radio packet when the downlink buffer is full SportQueue() wrote the stuffed packet without checking for room: a radio pushing faster than the RF link drains (8 bytes per frame) wrapped the 64-byte SportData ring over the bytes not sent yet, and the receivers got broken S.Port frames. Now the whole new packet is dropped when it doesn't fit (stuffed length 9 + 1 per 7D/7E byte). The buffer low flag is set as before, and also on a drop. * S.Port: report the downlink buffer state once, also when it has room The buffer low flag was cleared silently, so the radio only learnt at the next periodic status (up to 500 ms) and held its packets with the buffer empty: 442 B/s instead of ~1150 B/s with the flag honoured (4-in-1 and MPM v2 benches). Send the status as soon as the flag clears. While the flag is set, the status is no longer forced again on every packet: only when the flag gets set, the periodic status repeats it. --------- Co-authored-by: Raphael Coeffic <1050031+raphaelcoeffic@users.noreply.github.com> --- Multiprotocol/FrSkyDVX_common.ino | 5 +-- Multiprotocol/Multiprotocol.ino | 60 +++++++++++++++---------------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/Multiprotocol/FrSkyDVX_common.ino b/Multiprotocol/FrSkyDVX_common.ino index c5e192e1..e3798286 100644 --- a/Multiprotocol/FrSkyDVX_common.ino +++ b/Multiprotocol/FrSkyDVX_common.ino @@ -449,9 +449,10 @@ void Frsky_init_clone(void) used += MAX_SPORT_BUFFER - SportHead ; else used -= SportHead ; - if ( used < (MAX_SPORT_BUFFER>>1) ) - { + if ( used < (MAX_SPORT_BUFFER>>1) && IS_DATA_BUFFER_LOW_on ) + {// The status at once: the radio sends again DATA_BUFFER_LOW_off; + SEND_MULTI_STATUS_on; debugln("Ok buf:%d",used); } } diff --git a/Multiprotocol/Multiprotocol.ino b/Multiprotocol/Multiprotocol.ino index 6da36ac3..e1bcf2a7 100644 --- a/Multiprotocol/Multiprotocol.ino +++ b/Multiprotocol/Multiprotocol.ino @@ -701,43 +701,39 @@ void SportQueue(const uint8_t *b) #define BYTE_STUFF 0x7D #define STUFF_MASK 0x20 //debug("SPort_in: "); - boolean sport_valid=false; + //Stuffed packet: 7E, physical ID, 7 bytes, 1 more per 7D/7E + uint8_t pkt[16], len=2, nz=0; + pkt[0]=0x7E; + pkt[1]=b[0]&0x1F; for(uint8_t i=1;i<8;i++) - if(b[i]!=0) sport_valid=true; //Check that the payload is not full of 0 - if((b[0]&0x1F) > 0x1B) //Check 1st byte validity - sport_valid=false; - if(sport_valid) { - SportData[SportTail]=0x7E; - SportTail = (SportTail+1) & (MAX_SPORT_BUFFER-1); - SportData[SportTail]=b[0]&0x1F; - SportTail = (SportTail+1) & (MAX_SPORT_BUFFER-1); - for(uint8_t i=1;i<8;i++) + uint8_t c=b[i]; + nz|=c; + if( (c==BYTE_STUFF) || (c==0x7E) ) + {//stuff + pkt[len++]=BYTE_STUFF; + c^=STUFF_MASK; + } + pkt[len++]=c; + } + if(nz==0 || pkt[1] > 0x1B) + return; //Payload full of 0 or bad 1st byte + uint8_t used = (SportTail - SportHead) & (MAX_SPORT_BUFFER-1); + if(used + len < MAX_SPORT_BUFFER) + {//Room for it, else the whole packet is dropped: never overwrite the bytes not sent yet + for(uint8_t i=0;i SportTail ) - used += MAX_SPORT_BUFFER - SportHead ; - else - used -= SportHead ; - if ( used >= MAX_SPORT_BUFFER-(MAX_SPORT_BUFFER>>2) ) - { - DATA_BUFFER_LOW_on; - //Send Multi Status ASAP to inform the TX - SEND_MULTI_STATUS_on; - Update_Telem(); - debugln("Low buf=%d,h=%d,t=%d",used,SportHead,SportTail); - } + used += len; + } + if ( used >= MAX_SPORT_BUFFER-(MAX_SPORT_BUFFER>>2) && IS_DATA_BUFFER_LOW_off ) + { + DATA_BUFFER_LOW_on; + //Send Multi Status ASAP to inform the TX (Update_All() calls Update_Telem() next) + SEND_MULTI_STATUS_on; + debugln("Low buf=%d,h=%d,t=%d",used,SportHead,SportTail); } } #endif