Interrupt Driven Serial Required in OS6

Hello Jon,

The following worked for me:

  • Create a new Mbed OS 6 project and copy & paste the following test code to the main.cpp:
#include "mbed.h"
#include "BufSerial.h"

BufSerial   pc(USBTX, USBRX);

int main()
{
    pc.baud(115200);

    while (1) {
        Timer   s;

        s.start();
        pc.printf("Hello World - buff\n");

        int buffered_time = s.read_us();
        ThisThread::sleep_for(100ms);  // give time for the buffer to empty
        s.reset();
        printf("Hello World - poll\n");

        int polled_time = s.read_us();
        s.stop();
        ThisThread::sleep_for(100ms);  // give time for the buffer to empty
        pc.printf("printf buffered took %d us\n", buffered_time);
        pc.printf("printf polled took %d us\n", polled_time);
        ThisThread::sleep_for(500ms);
    }
}
  • Import Sam Grove’s BufferedSerial library into the project.

To avoid name clash with the Mbed OS 6 BufferedSerial:

  • Right-click on the library’s name and select Convert to Folder.
  • Rename the BufferedSerial folder to (for example) BufSerial.
  • In the BufSerial folder rename the files BufferedSerial.h and BufferedSerial.cpp to BufSerial.h and BufSerial.cpp, respectively.
  • In the BufSerial.h and BufSerial.cpp rename the BufferedSerial class to BufSerial.
  • In the BufSerial.h file comment out the lines as below:
//#if (MBED_MAJOR_VERSION == 5) && (MBED_MINOR_VERSION >= 2)
//#elif (MBED_MAJOR_VERSION == 2) && (MBED_PATCH_VERSION > 130)
//#else
//#error "BufSerial version 13 and newer requires use of Mbed OS 5.2.0 and newer or Mbed 2 version 130 and newer. Use BufSerial version 12 and older or upgrade the Mbed version.
//#endif
  • In the BufSerial.h file replace:
#ifndef BUFFEREDSERIAL_H
#define BUFFEREDSERIAL_H
 
#include "mbed.h"
#include "MyBuffer.h"

with

#ifndef BUFSERIAL_H
#define BUFSERIAL_H
 
#include "mbed.h"
#include "RawSerial.h"
#include "MyBuffer.h"
  • In the BufSerial.cpp replace
#include "BufferedSerial.h"

with

#include "BufSerial.h"

and in the BufSerial constructor replace

    RawSerial::attach(callback(this, &BufferedSerial::rxIrq), Serial::RxIrq);

with

    RawSerial::attach(callback(this, &BufSerial::rxIrq), SerialBase::RxIrq);
  • Copy & paste the RawSerial.h and RawSerial.cpp files from the Mbed OS 2 repository to the BufSerial folder.
  • In the RawSerial.cpp file replace
#include "drivers/RawSerial.h"

with

#include "RawSerial.h"
  • Build the project and program your board.

  • Make sure you set the the serial monitor speed to 115200 when testing.

It could happen that I missed to indicate some steps I did so please let me know how it worked for you.

If it works you can use the BufSerial folder (copy & paste) as a library in your Mbed OS 6 projects :slight_smile:

Warning: Mbed OS 6 serial communication classes are POSIX 6 compliant (re-entrant and thread safe). However, the BufSerial is not. So use it with caution!

Best regards, Zoltan

1 Like