FPRINTF ON LOCALFILESYSTEM FREEZE MY MBED

25 Nov 2010 . Edited: 25 Nov 2010

Hi everybody,

I'm new in programming mbed, for feww weeks. I also have a problem with localfilesystem that nobody seems to have (after a look in the forum).

My program do chrono things like this :

* Create an ini file if not exists, to store the default hardcoded IP conf to use with (default : DHCP),

 

------------------------------------------------------------------

int Save() {

LocalFileSystem local("local");

FILE *fp = fopen("/local/config.ini", "w");

if (fp == NULL) {

printf("\n\rERROR IN SAVING CONTROLER CONFIG FILE\n\r");

return -1;

}

fprintf(fp,"[IPADDRESS=%s]\n", _ipaddress);

fprintf(fp,"[SUBNETMASK=%s]\n", _subnetmask);

fprintf(fp,"[GATEWAY=%s]n", _gateway);

fprintf(fp,"[DNSSERVER=%s]\n", _dnsserver);

fprintf(fp,"[UDPPORT=%d]\n", _udpport);

fprintf(fp,"[NTPSERVER=%s]\n", _ntpserver);

fprintf(fp,"[UTCOFFSET=%d]\n", _utcoffset);

fprintf(fp,"[NBACCESSUNITS=%d]\n", _nbaccessunits);

fprintf(fp,"[USEDHCP=%d]\n", _usedhcp);

fclose(fp);

return 0;

}

------------------------------------------------------------------

*  Initiate the IP conf, which result is success

 

* Get local time from my ntp server, with set_time just after, which result is also ok

* Than wait for a tag from an rfid2 reader

=> When a tag is passed, strore the timcode and tag id in a log file

 

------------------------------------------------------------------

int Writetageventlog(int id, int status) {
LocalFileSystem local("local");
FILE *fp = fopen("/local/ctrler.log", "a");
if (fp == NULL) {
printf("\n\rERROR IN SAVING TAG EVENT LOG FILE\n\r");
return -1;
}
fprintf(fp,"[%s//%d//%d]\n", GetUTCTIMESTR(), id, status);
printf("[%s//%d//%d]\n", GetUTCTIMESTR(), id, status);
fclose(fp);
return 0;
}

 

 

------------------------------------------------------------------

 

INI and LOG file are 8.3 files on localfilsystem /local/...

The problem is that INI file is correctly created (recreated after reboot), but the log one not ! fopen seems to do the job because I have no ERROR message back, but the fprintf freeze the mbed, printf is also not output, and after a while, USB disk on my PC is removed !!!....

Did you encountered such freezing attitude using localfilesystem ?

One thing next : when I test my program, I'm connected both with the USB interface (to see debug messages in Teraterm), and with the Ethernet plug.

Does it mean the problem of both masters could have this symptomatic result ?

When I omit the printf, an empty file is created. Even I limit my fprintf to a simple string, it freeze, so that I think the problem is not with my variables types....

Can someone Help please ?

 

28 Nov 2010

Hi,..

I think it may be that you keep declaring your local file system?

you can declare it once and then use it wherever. sorry I don't have time to publish the below properly, but its the WHOLE main, so cut and paste it into a new main to have a look.

You don't need all the rest, but have a look at the structure,

I declare the filesystem at the beginning, then use "loggit()"

this open and closes file's and write's into them. (triggered by time in the main(), where there's a loop).

anyway..

have a look at this, see if it helps.

your logging method, just needs to open/write/close. Not declare a file system etc.

cheers

Dave.

 

#include "mbed.h"

DigitalOut myled(LED1);

I2C i2c(p28, p27);        // sda, scl
Serial pc(USBTX, USBRX); // tx, rx
LocalFileSystem local("local");
time_t seconds;

const int addr = 0x90; // define the I2C Address


void setTime() {
    // setup time structure for Wed, 28 Oct 2009 11:35:37
    struct tm t;
    t.tm_sec = 37;    // 0-59
    t.tm_min = 58;    // 0-59
    t.tm_hour = 13;   // 0-23
    t.tm_mday = 28;   // 1-31
    t.tm_mon = 11;     // 0-11
    t.tm_year = 110;  // year since 1900

    // convert to timestamp and display (1256729737)
    time_t seconds = mktime(&t);
     set_time(seconds);
    printf("Time as seconds since January 1, 1970 = %d\n", seconds);
}

float read() {

    char data[2] = {0,0};

    i2c.read(addr, data, 2); // read the two-byte register
    pc.printf("Byte1: %X ", data[0]);
    pc.printf("Byte2: %X \n\r", data[1]);

 

    //float nn = total*0.125;
    //     pc.printf("11 bit?: %f \n\r", nn);

    short res = (data[0] << 4) | (data[1] >> 4);

    float temp =  (float) ((float)res * 0.0625);
    pc.printf("12 bit ?: %f \n\r", temp);

    int b8 = data[0];
    pc.printf("8 bit ?: %i \n\r", b8);


// return r;
    return temp;
}

void loggit(float temp) {

 myled=!myled;
wait(1);
 myled=!myled;


    pc.printf("loggit");

    seconds = time(NULL);
    struct tm *t = localtime(&seconds);
 
    pc.printf("%i ",seconds);

    int hours = seconds/3600;
    int mins = (seconds - (hours * 3600))/60;

    int secs = seconds - (mins*60) - (hours * 3600);
    pc.printf("FORMATTED ");
    pc.printf("%f",temp);

    FILE *lp = fopen("/local/temp.txt", "a");

    pc.printf("Open");
    int res=0;
    res = fprintf(lp,"%04d-%02d-%02d %02d:%02d:%02d ",(t->tm_year+1900),(t->tm_mon+1),t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);

    if (res==0) {
        pc.printf("Could not write to lp file \n");
    }

    res = fprintf(lp,"%04.2f \r",temp);
    if (res==0) {
        pc.printf("Could not write to lp file \n");
    }

    fclose(lp);
    pc.printf("Closed");
}

 

int main() {

 //setTime();
    int i = 0;
    wait(1);
    while (1) {

      
    pc.printf("read");

        float t = read();
        pc.printf("TEMP: %f \n\r", t);
        if (t>210) {
           t=t-256;
           }
       

    seconds = time(NULL);
    struct tm *n = localtime(&seconds);
    pc.printf("%04d-%02d-%02d %02d:%02d:%02d UTC\r\n",(n->tm_year+1900),(n->tm_mon+1),n->tm_mday,n->tm_hour,n->tm_min,n->tm_sec);

        if (i==59) {
            loggit(t);
            i = 0;
        }

        wait(1);
        i++;
    }
}

28 Nov 2010

potentialy the function call in the fprintf is causing a problem. call the argument outside the fprintf into a tempory variable and then pass the tempory variable into the fprintf. do a pc.printf before and after to identify where the system crashes.

01 Dec 2010

Ok,

 

I'll try both your solutions and get back to you ASAP. Thanks for your replies !

 

 

01 Dec 2010

Well !

Results are not so good....I've tried with LocalFileSystem in global declaration, and also with just a printf without any param...Nothing to do with !

But the symptom is very interesting : When I invoke my printf, the mbed usb drive connected to my pc is ejected at the same time !!!

That's why I think the problem deal with the "both master mutual exception".... Try to do the same : printf somthing while connecting with both USB and RJ, and tell me if you have the same problem (see last firmware update fo more info on how the master exception has been managed in the last version....).

Nos, I'm going to try with just an USB power supply, not the PC....Get back soon...

 

01 Dec 2010 . Edited: 01 Dec 2010

I've now also tried without PC USB link, but also crash at the same line : my fprintf !!!

Help !!!!!!!! :-((

01 Dec 2010

Publish your project so we can look and maybe try it out.

01 Dec 2010

having a good old think, I belive the firmware doesn't support mulitiple things talking on the usb, so for instance running teraterm, pc.printf and trying to download a program doesn't work, or at least not for me.

 

oh, and asking for help without any details isn't going to get answers. no one knows what you need help with.

01 Dec 2010

 

My "Help" was more a joke than an ask...I apologize !

However, I have put several parts of the code in comment to get more directly tru the fprintf, and seems to be ok !

So I'm going to uncomment my code piece by piece and target where it goes wrong...Maybe I will taarget the same point that you deal with...many USB talking is not supported...

Back soon....

 

01 Dec 2010

Hi Louis,

You should be able to do everything you describe. You can certainly print to the USB serial at the same time as writing a file.

One problem in the section of code you published has already been highlighted, that you were re-creating instances of the LocalFileSystem, when you really just want one global instance that makes access tot he local file system possible.

There are a good number of reasons your program could hang. From the parts you've published, it could be things like:

  • your string variables you are fprintf-ing are not proper null-terminated C strings; depends how you are constructing them.
  • You use things like GetUTCTIMESTR() as a parameter to fprintf; unless the pointer returned really is a pointer to a C string that is properly allocated (i.e. it must be a global or dynamically allocated variable, not local to the function), you may get bad results!

The best thing is probably to see what part of the program is causing the hang, and if tracking that down doesn't cause an a-ha moment, try and publish a simplified but complete program that demonstrates the problem, and people will be able to reproduce it and likely fix it for you.

Simon

01 Dec 2010

Hi Simon,

 

Effectively I had a check this evening on all my code, and found that the problem occur when I htis : call ntp.setTime(server);

 from the library NTPClient... Now, how to find a relationshp between that and a problem of fprintf is anothe story, because NTPClient is compiled !

I will publish something tomorrow that reproduce the hang, to let you see !

Regards,

 

03 Dec 2010

Hi,

Finally, thing to retain is that things are not always where you're looking for !...Or Thruth is somewhere else as you like...

You're write when you say that it is more accurate to put declarations in global, than localy to a function...In fact, my NTPClient declaration was in a function of my own class. I put it in main.cpp, just after my other global variables, and everything is fine now...

AVOID LOCAL DECLARATION !!!!

Thanks to you for your help and attention....I can progress now !