5 years, 8 months ago.

Hang up; Add to empty string in Serial RxIRQ func with using RTOS.

mbed official RTOS lib and mbed lib are recent lib. I use LPC1768.

I made serial communication device lib; the lib has private string buffer, and copies rx buffer of Serial to private string with background rxIRQ attach for received over 16 Bytes.

But the my lib hangs up with official RTOS. (Of course, normal operation without RTOS.)

Therefore, I make check code (bottom).

As a result, when add to empty string (LINE34) if comment out LINE19, hanged up. If within LINE19, global string is not empty at LINE34, so this code is normal operation.

What is my code wrong?

Please teach me.

#include "mbed.h"
#include "rtos.h"
#include <string>
DigitalOut led[]= {LED1, LED2, LED3, LED4};
RawSerial serial(p9, p10);
//Mutex mutex;
//Semaphore semaphore(1);
//volatile string str= "_";
string str= "_";
void threadFunc(void const *argument)
{
    while(true) {
        led[1]= !led[1];
        if(str.size() > 1) {
            serial.printf(str.c_str());
            serial.printf("BE thread's pointer: %p\r\n", str);  // Before Erase
            str.erase();    // or str= "";
            serial.printf("AE thread's pointer: %p\r\n", str);  // Before Erase
//            str += "_";   // This line is required for normal operation.
            serial.printf("AE_ thread's pointer: %p\r\n", str);  // Before Erase
        }
        wait_ms(50);
    }
}
void serialAttach()
{
    led[2]= !led[2];
    if(serial.readable()) {
        led[3]= !led[3];
        serial.printf("attach's pointer: %p\r\n", str);
        // if without LINE19, mbed is hanged THERE.
//        mutex.lock();
//        semaphore.wait();
        str += serial.getc();;
//        mutex.unlock();
//        semaphore.release();
    }
    led[2]= !led[2];
    return;
}

int main()
{
    serial.baud(115200);
    serial.attach(&serialAttach);
    Thread thread(threadFunc);
    while(true) {
        led[0]= !led[0];
        wait_ms(25);
    }
}
// eof
Be the first to answer this question.