Skip to content

Commit b6eaf4f

Browse files
committed
Added send and receive buffer clearing timeouts for CDC command
interface
1 parent 58cf907 commit b6eaf4f

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

Firmware/FFBoard/Inc/CDCcomm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CDCcomm {
1818
static uint16_t cdcSend(std::string* reply,uint8_t itf);
1919
static void cdcFinished(uint8_t itf = 0);
2020
static uint32_t remainingData(uint8_t itf = 0);
21+
static void clearRemainingBuffer(uint8_t itf = 0);
2122

2223
private:
2324
static bool usb_busy_retry;

Firmware/FFBoard/Inc/CommandInterface.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ class CDC_CommandInterface : public StringCommandInterface,public cpp_freertos::
7474

7575
private:
7676
bool enableBroadcastFromOtherInterfaces = true;
77+
uint32_t lastSendTime = 0;
7778
const uint32_t parserTimeout = 2000;
7879
//cpp_freertos::BinarySemaphore sendSem;
7980
std::vector<CommandResult> resultsBuffer;

Firmware/FFBoard/Src/CDCcomm.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,20 @@ void CDCcomm::cdcFinished(uint8_t itf){
3838
}
3939
}
4040

41+
/**
42+
* Checks if data is remaining in a buffer to be sent
43+
*/
4144
uint32_t CDCcomm::remainingData(uint8_t itf){
4245
return CDCcomm::remainingStrs[itf].size();
4346
}
4447

48+
/**
49+
* Clears a buffer
50+
*/
51+
void CDCcomm::clearRemainingBuffer(uint8_t itf){
52+
CDCcomm::remainingStrs[itf].clear();
53+
}
54+
4555
/**
4656
* Sends a string via CDC
4757
* If not everything can be sent it will be buffered for later in a new string

Firmware/FFBoard/Src/CmdParser.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ bool CmdParser::add(char* Buf, uint32_t *Len){
3535

3636
}
3737
}
38-
38+
if(clearBufferTimeout && HAL_GetTick() - lastAddTime > clearBufferTimeout ){
39+
this->buffer.clear();
40+
}
3941
lastAddTime = HAL_GetTick();
4042
this->buffer.append((char*)Buf,*Len);
4143
return flag;
@@ -51,6 +53,9 @@ void CmdParser::setClearBufferTimeout(uint32_t timeout){
5153
}
5254

5355
int32_t CmdParser::bufferCapacity(){
56+
if(lastAddTime > clearBufferTimeout){
57+
return bufferMaxCapacity;
58+
}
5459
return std::max<int32_t>(bufferMaxCapacity - buffer.size(),0);
5560
}
5661

Firmware/FFBoard/Src/CommandInterface.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,19 +250,16 @@ void CDC_CommandInterface::Run(){
250250

251251

252252
void CDC_CommandInterface::sendReplies(const std::vector<CommandResult>& results,CommandInterface* originalInterface){
253-
// if( (!enableBroadcastFromOtherInterfaces && originalInterface != this) ){
254-
// return;
255-
// }
256-
//
257-
// replystr.reserve(100);
258-
// StringCommandInterface::formatReply(replystr,results, originalInterface != this && originalInterface != nullptr);
259-
// if(!replystr.empty())
260-
// CDCcomm::cdcSend(&replystr, 0);
253+
254+
if(HAL_GetTick() - lastSendTime > parserTimeout){
255+
resultsBuffer.clear(); // Empty buffer because we were not able to reply in time to prevent the full buffer from blocking future commands
256+
//CDCcomm::clearRemainingBuffer(0);
257+
}
261258

262259
if( (!enableBroadcastFromOtherInterfaces && originalInterface != this) ){
263260
return;
264261
}
265-
262+
lastSendTime = HAL_GetTick();
266263
resultsBuffer.assign(results.begin(), results.end());
267264
resultsBuffer.shrink_to_fit();
268265
nextFormat = originalInterface != this && originalInterface != nullptr;
@@ -273,6 +270,9 @@ void CDC_CommandInterface::sendReplies(const std::vector<CommandResult>& results
273270
* Ready to send if there is no data in the backup buffer of the cdc port
274271
*/
275272
bool CDC_CommandInterface::readyToSend(){
273+
if(HAL_GetTick() - lastSendTime > parserTimeout && CDCcomm::remainingData(0) == 0){
274+
return true;
275+
}
276276
return CDCcomm::remainingData(0) == 0 && resultsBuffer.empty();
277277
}
278278

0 commit comments

Comments
 (0)