Mutex: ..., Not allowed in ISR context when type casting float

Hi,

I receive
Error Status: 0x80010133 Code: 307 Module: 1
Error Message: Mutex: 0x200019CC, Not allowed in ISR context

when I change
std::string s1 = to_string(5);
to
std::string s1 = to_string(5.1);
inside an ISR context for my serial output.

How can I circumvent this?

Thank you!

Code:

#include “ThisThread.h”
#include “mbed.h”
#include “cstddef”
#include “iostream”
#include “string”
#include “iomanip”

// Maximum number of element the application buffer can contain
#define MAXIMUM_BUFFER_SIZE 32

InterruptIn button(SW2);
DigitalOut led(LED1);

// Create a BufferedSerial object with a default baud rate.
static UnbufferedSerial SerialPort(USBTX, USBRX);

// Application buffer to receive the data
char buf[MAXIMUM_BUFFER_SIZE] = {0};
uint8_t bufPos;

void sendMotorStatusMessage(double speed)
{
bufPos = 0;
std::string s1 = to_string(5.1);
strncpy(buf + bufPos, s1.c_str(), s1.length());
bufPos += s1.length();

buf[bufPos] = '\0';

SerialPort.write(buf, sizeof(buf));
memset(buf, 0, sizeof(buf));

}

void flip()
{
led = !led;
sendMotorStatusMessage(0);
}

int main(void)
{
// Set desired properties (9600-8-N-1).
SerialPort.baud(115200);
SerialPort.format(
/* bits / 8,
/
parity / BufferedSerial::None,
/
stop bit */ 1
);
button.rise(&flip);

while (1)
{
    ThisThread::sleep_for(250ms);
}

}

Hi Marcel,

it would help people who might be able to answer if you provided which version of Mbed OS you are using and perhaps a large code snippet .

Thanks
Anna

It’s most likely because you’re calling ‘sendMotorStatusMessage’ (which contains a printf call) from within the interrupt.

The way I got around this is to use EventQueues (in mBedOS 5.13 anyway). Post an message to the queue and let the queue handler print the status message.

HTH

Done.

I’m aware of that but I use UnbufferedSerial class and it works with writing 5 to the serial interface. Only floats like 5.1 do not work.