Skip to content

BufferedSerial skips characters in some circumstances #13422

Closed
@lefebvresam

Description

@lefebvresam

test.zip
commandinterface.zip

Description of defect

When using BufferedSerial without NDEBUG=1 mode and with speeds higher than 350000Bd, from time to time serial characters are skipped. When recompiling with NDEBUG=1 in mbed_app.json, there is no problem with speeds up to 460800Bd. I tried using priority NVIC_SetPriority(USART3_8_IRQn, 255); but without result.

The parts of the code that invoke this behavior:

The constructor implements a thread and an BufferedSerial object:

CommandInterface::CommandInterface(EventQueue* queue, PinName tx, PinName rx, int baud) 
    : _queue(queue), _bufferedserial(tx, rx, baud), _running(false), _response_sent(false),
    _thread(osPriorityNormal, 1024, nullptr, "commandinterface"), _interprete(nullptr) {
        FINE("serial port Tx/Rx pins [%i|%i], Bd [%i], blocking [%i]", tx, rx, baud, _bufferedserial.is_blocking());
        NVIC_SetPriority(USART3_8_IRQn, 255);
    }

The thread checks for characters in the software buffer and processes them:

// clears a string until n, shrink memory and reset counter
void CommandInterface::clear(std::string& string, size_t n) {
    string.erase(0, n);
    string.reserve(0); // release unnecessary memory to avoid overflows
}

void CommandInterface::serialProcessor() {
    std::string command_buf = "";
    char buf[100] = {0};
    while(_running) {
        uint32_t num = _bufferedserial.read(buf, sizeof(buf));
        printf("\n%i:", num);
        command_buf.append(buf, num);
        printf("%s\n", command_buf.c_str());
        clear(command_buf, string::npos);
    }
    _thread.terminate();
}

mbed_app.json:

{
    "macros": [
        "NDEBUG=1", --> delete or not delete this line
        "MBED_HEAP_STATS_ENABLED=1",
        "MBED_THREAD_STATS_ENABLED=1",
        "OS_TIMERS=0"
    ],
    "target_overrides": {
        "*": {
            "platform.memory-tracing-enabled": true,
            "platform.callback-nontrivial": true,
            "target.printf_lib": "std"
        },
        "NUCLEO_F091RC": {
            "events.shared-dispatch-from-application": true,
            "rtos.main-thread-stack-size": 5632,
            "drivers.uart-serial-rxbuf-size": 512,
            "drivers.uart-serial-txbuf-size": 256
        }
    }
}

A Python script sends a sequence of commands of equal length for testing:

def pattern_loop(running):
  ...
  while running.is_set():
    if not(q%5): ser.write(b'p1.sil=1\np1.pb0.val=1\np1.sil=0' + closing_seq)
    ...
    elif state == State.IDLE:
    ...  
    time.sleep(fspeed)

Serial console output 400000Bd and not NDEBUG (skipping chars):

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

32:p1sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

31:p1sil=1
p1.pb0.val=1p1.sil=0ÿÿÿ

32:p1sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

32:p1sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

31:p1sil=1
p1.pb0.val=1
p1.si=0ÿÿÿ

Serial console output 350000Bd and not NDEBUG (not skipping chars):

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

Serial console output 460800Bd and NDEBUG=1 (not skipping chars):

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

33:p1.sil=1
p1.pb0.val=1
p1.sil=0ÿÿÿ

Target(s) affected by this defect ?

NUCLEO_F091RC

Toolchain(s) (name and version) displaying this defect ?

GCC_ARM

What version of Mbed-os are you using (tag or sha) ?

mbed-os-6.2.0
#a2ada74770f043aff3e61e29d164a8e78274fcd4

What version(s) of tools are you using. List all that apply (E.g. mbed-cli)

mbed-cli 1.10.4

How is this defect reproduced ?

Create a small project with a similar implementation and check the reason of the issue. You can start with the files attached.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions