Skip to content

Commit

Permalink
AP_HAL_ChibiOS: only store a single UART frame
Browse files Browse the repository at this point in the history
  • Loading branch information
andyp1per committed Sep 19, 2023
1 parent ca90eb7 commit 1a8a5f2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
27 changes: 11 additions & 16 deletions libraries/AP_HAL_ChibiOS/UARTDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,22 +489,22 @@ void UARTDriver::_begin(uint32_t b, uint16_t rxS, uint16_t txS)
begin framing. Data from _readbuf is read into _frame_readbuf when an idle ISR is detected.
_frame_readbuf is sized to be able to accommodate at least frames_in_buffer of size _frame_size
*/
bool UARTDriver::_begin_framing(uint16_t _frame_size, uint16_t frames_in_buffer)
bool UARTDriver::_begin_framing(uint16_t _frame_size)
{
// disallow framing from a different thread
if (_uart_owner_thd != chThdGetSelfX()) {
return false;
}

_frame_readbuf.clear(); // in case framing was already started
_frame_readbuf.set_size((_frame_size + sizeof(uint32_t)) * frames_in_buffer);
_frame_readbuf.set_size(_frame_size);

_framing_initialised = true;

return true;
}

bool UARTDriver::_frames_available()
bool UARTDriver::_frame_available()
{
if (!_framing_initialised) {
return false;
Expand All @@ -520,7 +520,7 @@ bool UARTDriver::_frames_available()
read bytes (usually the size of the frame) or 0 if no frames are available
or -1 if an error occurs
*/
ssize_t UARTDriver::_read_frame(uint8_t *buffer, uint16_t count)
ssize_t UARTDriver::_read_frame(uint8_t *buffer, uint16_t buf_size)
{
if (_uart_owner_thd != chThdGetSelfX()){
return -1;
Expand All @@ -534,18 +534,18 @@ ssize_t UARTDriver::_read_frame(uint8_t *buffer, uint16_t count)
return -1;
}

uint32_t flen;
if (_frame_readbuf.read((uint8_t*)&flen, sizeof(uint32_t)) == 0 || flen == 0) {
uint32_t flen = _frame_readbuf.available();
if (flen == 0) {
return 0;
}

const uint32_t ret = _frame_readbuf.read(buffer, MIN(count, flen));
const uint32_t ret = _frame_readbuf.read(buffer, MIN(buf_size, flen));
if (ret == 0) {
return 0;
}

if (count < flen) {
_frame_readbuf.advance(flen - count);
if (buf_size < flen) {
_frame_readbuf.advance(flen - buf_size);
}

if (!_rts_is_active) {
Expand Down Expand Up @@ -620,15 +620,10 @@ void UARTDriver::transfer_frame()
return;
}

// framing error, restart at the next frame
if (fsize > frame_size) {
_frame_readbuf.clear();
_readbuf.clear();
return;
}
// discard the old frame if necessary
_frame_readbuf.clear();

// read the remaining available bytes as a frame
_frame_readbuf.write((const uint8_t*)&fsize, sizeof(uint32_t));
while (fsize > 0) {
uint32_t avail;
const uint8_t* buf = _readbuf.readptr(avail);
Expand Down
6 changes: 3 additions & 3 deletions libraries/AP_HAL_ChibiOS/UARTDriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -281,9 +281,9 @@ class ChibiOS::UARTDriver : public AP_HAL::UARTDriver {
bool _discard_input() override;

// frame based operations
bool _begin_framing(uint16_t frame_size, uint16_t frames_in_buffer) override;
ssize_t _read_frame(uint8_t *buffer, uint16_t count) override;
bool _frames_available() override;
bool _begin_framing(uint16_t frame_size) override;
ssize_t _read_frame(uint8_t *buffer, uint16_t buf_size) override;
bool _frame_available() override;

private:
void transfer_frame();
Expand Down

0 comments on commit 1a8a5f2

Please sign in to comment.