SDFileSystem + Multiple MODSERIAL +mbed-rtos doesnt work (conflict?)

28 Sep 2012

Hi,

Please help me, I can't find anything about that what happen to me. Maybe somebody has already faced to this problem.

I've written a software which operates on multiple Serials (3x), SD card uses Fat. I would like to extend my SW to additional feature using 3G modem Vodafone k3770. Because available library is still 'close' in beta testing phase i was forced :) to use RTOS to work with. I must admit that program worked fine until it uses mbed-rtos port.

Problem starts when I was initializing some objects that keep control on several peripherals. I have one big 'Object Holder' class that keeps pointers to other smaller low-level class for controlling GPS on serial, Bluetooth module (RN42) and additionaly there is a common trace system which outputs on USB serial. This is production board and I have no JTAG on it (I know my mistake).

Heres what I have found. All object looks similar. 3 of them initializes MODSERIAL, one SD card. Thing is I need to choose. I cannot use 3 MODSERIAL at once and SDcard Object because at first try of using fopen() functions (read a particular file on my sd card) thread that is related to SD card operation just stucks. There is no message , no stack overfloww output. nothing it hangs but other Threads are working.

What is happening? Is there any way to find a reason what kills a thread and in which moment?

LIke I said If I will not init all MODSERIAL object just one will be not inited SDcard FatSystem working well. All my functions that openning something are prepared for NULL pointer returning after try to open a file. But in this case I get nothing.

Thanks in advance for any kind of help.

P.S Im using typical current releases of library MOSDERIAL, SDHC, FatFileSytem, mbed, mbed-rtos

28 Sep 2012

OK, I can see no response. Maybe I have get myself not enough cleared.

Does code below do something special when uses RTOS enviroment?

RN42::RN42(PinName tx, PinName rx,PinName _nreset,PinName _status,ObjectHolder *_r):r(*_r),_connected(_status),nreset(_nreset),serial(tx,rx)
{
    _connected.mode(PullNone);
    
   serial.attach(this,&RN42::RxCallback, MODSERIAL::RxIrq);
   serial.rxBufferFlush();
    isResponseReceived = false;
    MODE_ACTIVE = false;
    RECEIVED_COMMAND = false;
    RECEIVED_CHAR = false;
}

I have 3 kind of very similar object using Serial(MODSERIAL) in my program. SD card is initialized like below.

SDFileSystem sd(P0_18,P0_17,P0_15,P0_16,"sd");

main(){
...
RN42* bluetooth =  new RN42(P4_28,P4_29,P0_4,P0_5,&r);
...
}

Let say Im using 2 of 3 object like RN42 and class SD CARD working fine. Opening files, closing... everything working well.

Wneh I put 3 of 3 object like RN42 (lest name it GPS,Console - uses different UART of course, both using MODSERIAL). SDCARD suddely stops working. There is no way I can open files or other else. And thread that is used for file operation in loop just stops.

So question is what is happening in that code in constr. of class RN42. For example if I will use RN42 class like above and not use GPS - its works fine. Seems like no more than two instance of object that using MODSERIAL objecst can exist? Strange

27 Nov 2012

I'm having a similar problem. When using 3 Serial or MODSERIAL objects, the mbed hangs. I am not using SD card but I am using LocalFileSystem and some other libraries. All was working ok until I introduced the third serial port.

Any ideas?

regards, Adrian.

27 Nov 2012

Might be a red herring, but how big did you set the buffers in MODSERIAL?

#ifndef MODSERIAL_DEFAULT_RX_BUFFER_SIZE
#define MODSERIAL_DEFAULT_RX_BUFFER_SIZE    1024
#endif

#ifndef MODSERIAL_DEFAULT_TX_BUFFER_SIZE
#define MODSERIAL_DEFAULT_TX_BUFFER_SIZE    1024
#endif
27 Nov 2012

That was my first thought too. I tried some really small (16 bytes each) buffer sizes but no luck. It seems fine with two Serial objects with large buffers, but three Serial objects with small buffers didn't work - mbed hangs at bootup.

I wonder if it has something to do with this http://mbed.org/forum/mbed/topic/3499/?page=1#comment-18817

cheers, Adrian.

04 Dec 2012

Am I the only person using 3 serial ports? Someone must be having this same issue!

I have dropped Modserial in favour of the mbed official Serial object, but it hasn't fixed anything. Anytime I use 3 serial ports the mbed hangs!

This is a show stopper for me since I actually intend to use 4 serial ports, (The LPC1768 has 3 uarts + mbed usb interface).

04 Dec 2012

No you are the second one. I have the same issue earlier :) Unfortunately with no solution for that as far. I was thought that I make a fundamental mistake or something but when you came out with same problem I think someone should be interested in fixing that.

I have make a several check as far as I remember. I can confirm that no matter how much memory you designate for rx & tx buffer and from where you initiate object MODSERIAL, 3rd initialization of object pass but first call of function that has something to do with FatFileSystem (Im using SD card, but you may use any kind of lib that uses that: Local, USB) hangs task that 'uses' a call. If you determine using only 2 UARTS everything works perfect.

BR

04 Dec 2012

Which combination do you need to get it to hang? Just 3+ serial objects? Or do you also need SD and/or RTOS?

04 Dec 2012

Correct, Im using such combinations: 3x SERIAL, SD, USB (last 2 uses FATFileSystem)

Cases: 1.3xSERIALS, SD - FAIL 2.3xSERIALS, USB - FAIL 3.3xSERIALS only - FAIL 4.2xSERIALS, SD,USB - PASS

Like described in one of first post in thread, no matter which object take init of SERIAL. I've also noticed that this has some kind of influence to SDcard interface(if you want to read a file - fopen() causes hang of task).

04 Dec 2012

It fails for you with 3x Serials and nothing else in the code? That is weird since I had one project which used 4 (MOD)Serial objects, and that didnt give problems. And it fails for you with 3x Serial (that is the easiest one where it fails for you), no RTOS, and nothing else?

04 Dec 2012

Hi,

Like I wrote 3xSERIAL used in code is not a problem. Problem is when you want do something else in this case im heavily using FatFileSystem with SD card. Reading files where you got multiple Serials with RTOS is not possible, because task is stuck in fopen().

I'm must admit that when I'm not using RTOS implementation - this configuration works perfectly.

04 Dec 2012

I am not using SD but I am using RTOS and LocalFileSystem. After some trial and error, removing the LocalFileSystem seems to fix it.

I also think it's not hanging at startup but when it accesses the local file system or possibly when it transmits from serial, not 100% sure. More experimentation is need there, but it does look like some kind of combination of things that is causing the headaches.

regards, Adrian.

12 Dec 2012

Hello,

I am running into this same problem. RTOS + Serial(USB) + FatFileSystem works ok. However, when I add a 2nd slow-speed Serial, the program crashes on the first access to the local file system. It even fails if the serial interrupts haven't been initialized yet.

I wouldn't be using the RTOS if EthernetInterface didn't require its use.

Any solutions out there?

...kevin

12 Dec 2012

Kevin,

I have a solution that might help... I wrote a buffered serial driver that uses the rtos and modified the chan fat library to work with the rtos... Here are my notes on the buffered serial driver http://mbed.org/users/tylerjw/notebook/buffered-serial-with-rtos/ I wrote the code to work with UART1(p13,p14) but it can easily modified to work with any of the Serial devices. It will speed up what you are doing by allowing the slow serial device to write and read using interrupts and it'll allow your other threads to run in the meantime.

Import librarychan_fatfs_sd

updated chan_fatfs

Import librarybuffered-serial1

UART1 buffered serial driver, requires RTOS

12 Dec 2012

Thanks Tyler - I'll set it up now.

Q: How does one set up both a local and SD file system?

...kevin

13 Dec 2012

If you use the library I sent you it goes around the C standard library and doesn't use any of the standard functions. Some of the functions are a bit different and you need to look up the syntax for using them, but they are much more efficient. You can use the standard way for accessing the local file system using the C standard library functions and it shouldn't interfere with your SD code. By the way, this only works with standard SD cards (up to 4gb I think). I learned this the hard way... SDHC is another animal.

ChaN FAT Library Reference

20 Dec 2012

Hi,

I think I have the same problem. In my application I read some measurements, store them to SD-card and once in a while send the measurements to a server using Vodafone's k3770. I wanted to add GPS values to the measurements but once I added the GPS(a serial device) to the application, the SD-card thread hangs. I ran several tests trying to find the reason. I was able to narrow it down to a point where the program hangs if I use more than 2 Serial-class instances. I made a simple program that states the problem. The code is as follows:

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

#define FILE_LOC "/sd/test.txt"

Serial pc(USBTX, USBRX);
Serial gps(p28, p27);
Serial test(p9,p10);
SDFileSystem sd(p5, p6, p7, p8, "sd");
DigitalOut myled(LED1);

int main() { 

    while(1) {
        myled = !myled;   
        FILE *fp = NULL;
        fp = fopen(FILE_LOC, "w");
        if( fp != NULL ) fclose(fp);
        Thread::wait(1000); 
    }
}

With this code the led doesn't blink. If I remove one of the Serial lines from the code, the led starts blinking. Am I doing something wrong here? Naturally the program also works if I remove the mbed-rtos from the project.

I'm using the newest versions of mbed and mbed-rtos libraries. Filesystem and SD libraries are:

Can someone get the same results?

Teemu

25 Jan 2013

Anyone try ScmRTOS for the 3 serial problem? http://scmrtos.sourceforge.net/ScmRTOS

27 Jan 2013

HM Yoong wrote:

Anyone try ScmRTOS for the 3 serial problem? http://scmrtos.sourceforge.net/ScmRTOS

While ScmRTOS looks promising, I think the mbed RTOS will be needed by many because as Kevin pointed out, it's required by EthernetInterface.

This problem is really starting to bite hard! Not being able to use 3 serial objects is very limiting and prevented me from progressing further with development.

I would really like the mbed library authors to give some consideration to this show-stopping problem!!!

27 Jan 2013

Sorry, this thread did pass under my radar.

Did you try increasing the stack size of your threads?

You can see an example here: /forum/bugs-suggestions/topic/3288/

If increasing the stack size of your threads does not solve your problem, could you point me to a published program that is presenting the issue?

Cheers, Emilio

28 Jan 2013

Hi,

I got things working when I removed the SD-library and started using Tyler Weaver's version of Chan's FATFS(Thank you Tyler for publishing the modified library!). After changing the SD-library I still had some problems with the gps. For some reason if MODSERIAL-object was placed and used inside a class, it sometimes didn't work properly(constanly receiving new, incomprehensible lines from the gps). Once I removed the class and created the MODSERIAL-object globally or inside the task scope, everything started working.

The problem with Serial objects, rtos and official SD-library can be tested with the following program: http://mbed.org/users/tetuomin/code/Test_Program/

The program should start working if you remove one of the serial objects.

Teemu

28 Jan 2013

Hi, firstly, thank you for reporting this issue and for providing a program to reproduce it.

It was quite a puzzling problem: we were running out of mutexes for the C standard library.

I have increased the default number of mutexes for the C standard library from 8 to 12. In particular, I have added a proper error message when we run out of mutexes. You can see the details here: http://mbed.org/users/mbed_official/code/mbed-rtos/rev/53e6cccd8782

To pull the changes simply Update the "mbed-rtos" library in your project.

Cheers, Emilio

29 Jan 2013

I have tested and confirmed Emilio's fix. I now have all 4 serial ports, LocalFileSystem and mbed-rtos and they all seem to get along happily :)

Many thanks to Emilio.

Thank you Thank you Thank you

Let the development continue...

Adrian.