Is it possible to use mbed os instead of ArduinoCore-mbed

I need high io performance in my app. And the max value of CDC_MAX_PACKET_SIZE is 64 (If you change it bigger than 64, you can not read or write any bytes from Serial Monitor), but I want a larger value like 1024 or 2048.
And I found that in ArduinoCore-mbed, it is using UnbufferedSerial which can only read bytes one by one, and this will cause bad io performance (according to UnbufferedSerial - API references and tutorials | Mbed OS 6 Documentation).
So can I use mbed os directly instead of using ArduinoCore-mbed?

Yes you can use Mbed OS directly in Arduino IDE.

Just need to start with

#include "mbed.h"
#include "rtos.h"

Note that rtos.h is included to allow you to use the ThisThread method.

Another recommendation, to make coding easier is to add in these 2 lines of code:

using namespace mbed;
using namespace rtos;

Then it's almost cut and paste. Note that anything shown in while(1) you simply place that in the void loop() area etc.

As you'll find in the MbedOS documentation page, there is a USBCDC class: USBCDC - API references and tutorials | Mbed OS 6 Documentation

/*
 * Copyright (c) 2006-2020 Arm Limited and affiliates.
 * SPDX-License-Identifier: Apache-2.0
 */
#include "mbed.h"
#include "rtos.h"
#include "USBCDC.h"

using namespace mbed;
using namespace rtos;

USBCDC cdc;
DigitalOut myled(LED1, 0);

void setup() {
  // put your setup code here, to run once:
  
  ThisThread::sleep_for(2000);
  myled = 1;          // turn off the LED
  ThisThread::sleep_for(2000);
  myled = 0;          // turn on the LED

}

void loop() {
  // put your main code here, to run repeatedly:
        char msg[] = "Hello world\r\n";
        cdc.send((uint8_t *)msg, strlen(msg));
        ThisThread::sleep_for(1000);

}

If you upload this code to your board, notice that nothing happens to the LED until you open up the Serial Monitor. This is because USBCDC cdc; blocks until a terminal is detected.

However, you will only see the "hello world" output if using a USB host on a micro because it's sending raw data.

Note that you'll need to double tap the RST button to allow code to flash again as this code disables the serial port as you've configured it for CDC.

2 Likes

Thanks for your comment.

I found that in mbed os, USBCDC's device description is defined as USB1.1
image

and in ArduinoCore-mbed , there's a similar class arduino::USBCDC and it is defined as USB2.0
image

Giga's programing USB port is USB2.0, so arduino::USBCDC may have a better performance?

Then I found this site:
https://microchipdeveloper.com/usb:speeds
https://microchipdeveloper.com/usb:high-speed

Accrording to the links above, the max packet size in USB2.0 is 512.

But the default max packet size in arduino::USBCDC is 64, so I need to define this in my code to adjust the max packet size?

MBED_CONF_TARGET_USB_SPEED == USE_USB_OTG_HS

But it seems that this is used for the other usb port which is used for Hosting USB devices, not the programming one?

I believe that is already defined in mbed_app.json. These are then transferred to mbed_config.h (which is autogenerated).

#define MBED_CONF_TARGET_USB_SPEED USE_USB_OTG_FS // set by target:GIGA

If you dig further, you'll also discover another header file with more hardware specific settings: USBPhyHw.h

As to what this does, I am not sure, exactly. So, as you are exploring and evaluating these low level settings, maybe a better place to get answers to these technicalities is on the ArduinoCore-mbed github page.

1 Like