How use serial::attach(&callback) in mbedOS

when I use like follow code ,it can’t operate correct. thank you!

#include “mbed-drivers/mbed.h”
#include “minar/minar.h”
#include “core-util/FunctionPointer.h”

using namespace mbed::util;

static DigitalOut led(LED1);

static void led_toggle_user(void) {
led = !led;
// printf(“LED = %d \r\n”, led.read());
}

static void led_toggle_irq(void) {
minar::Scheduler::postCallback(FunctionPointer0(&led_toggle_user).bind());
}

void app_start(int, char**) {

static Serial pc(USBTX, USBRX);
pc.attach(&led_toggle_irq);
led = 1;
while(true)
{
wait(5);
led = !led;

}

}

Hi,
app_start in mbed is a minar event only, not like a main application. As the minar is a preemptive scheduler, we have to terminate an event to process another. so we should not use an infinte loop in app_start or any event. I hope that is the issue in your code.

refer the following link : GitHub - ARMmbed/minar: mbed OS scheduler

Thanks

when i remove the infinte loop the problem still exists. My purpose is receive uart message in mbedos ,what should i do ? thank you !!!

Hello,

there are not sufficient information provided.

what target are you using? yotta list - share the output, are all modules up to date?

You should not use static Serial pc(USBTX, USBRX);. If you want to use stdio Serial object, use :

/** Get the stdio Serial object, which is lazy instantiated.
 *
 *  @returns stdio object 
 */
Serial& get_stdio_serial();

in the app:

Serial& pc = get_stdio_serial();

the follow code can run correct on frdm-k64f-gcc 2.0.0 ,but got frozen on st-nucleo-f401re-gcc 0.2.4 board ,if I want make 401 can also run the code which source should modified ? thanks !!!

#include "mbed-drivers/mbed.h"
#include "minar/minar.h"
#include "core-util/FunctionPointer.h"
using namespace mbed::util;
static char buf [10];
static DigitalOut led(LED1);
//static DigitalOut led(YOTTA_CFG_HARDWARE_PINS_D4);
static Serial pc(USBTX, USBRX);

static void led_toggle_user(void) {
led =!led;
//pc.printf("receive");
buf[0]=pc.getc();
}
static void test(){

pc.printf(buf);
}


void app_start(int, char**) {

pc.attach(&led_toggle_user);


buf[0]='*';
buf[1]='\0';

led=1;
minar::Scheduler::postCallback(test).period(minar::milliseconds(500));
}

when I use target st-nucleo-f401re-gcc 0.2.4 board the code got frozen, but frdm-k64f-gcc 2.0.0 works well,my purpose is make 401 can run the code correct ,can you give me some hints please? thank you !!! (the code gives in above)

Please don’t use static Serial pc(USBTX, USBRX) with mbed-drivers >= 0.12.x .Use the API shared above.

cc @bcostm

I want use serial communicate with other device ,not just USBTX/RX ,the API you shared above is just about USBTX,UBSRX pins ,right? -_-

I want use serial communicate with other device ,not just USBTX/RX ,the API you shared above is just about USBTX,UBSRX pins ,right? --@Kojto
yotta list output
_ mbed-drivers 0.12.1
|
mbed-hal 1.2.2 yotta_modules\mbed-hal
| _ mbed-hal-st 1.0.0 yotta_modules\mbed-hal-st
| _ mbed-hal-st-stm32f4 1.1.2 yotta_modules\mbed-hal-st-stm32f4
| |_ uvisor-lib 1.0.12 yotta_modules\uvisor-lib
| |_ mbed-hal-st-stm32cubef4 1.0.2 yotta_modules\mbed-hal-st-stm32cubef4
| _ mbed-hal-st-stm32f401re 0.1.2 yotta_modules\mbed-hal-st-stm32f401re
|_ cmsis-core 1.1.2 yotta_modules\cmsis-core
| _ cmsis-core-st 1.0.0 yotta_modules\cmsis-core-st
| _ cmsis-core-stm32f4 1.0.5 yotta_modules\cmsis-core-stm32f4
| _ cmsis-core-stm32f401xe 0.1.0 yotta_modules\cmsis-core-stm32f401xe
|_ ualloc 1.0.3 yotta_modules\ualloc
| _ dlmalloc 1.0.0 yotta_modules\dlmalloc
|_ minar 1.0.4 yotta_modules\minar
| _ minar-platform 1.0.0 yotta_modules\minar-platform
| _ minar-platform-mbed 1.1.0 yotta_modules\minar-platform-mbed
|_ core-util 1.2.0 yotta_modules\core-util
_ compiler-polyfill 1.2.1 yotta_modules\compiler-polyfill

I want use serial communicate with other device ,not just USBTX/RX ,the API you shared above is just about USBTX,UBSRX pins ,right? -_-

Yes only for stdio. The above example defines Serial object for USBTX, RX

the mbed classic sdk can not run in mbed os 3.0? I just want migrate my project from mbed sdk to mbed os …

in my project just need serial ,can I remove minar module and just use main fun()? because the mbedos new feature for my project is not used.