losing program and .txt file

15 Mar 2011

Hi all,

I used a simple program to test low long the MBED will run on a battery. Hooked it up to a 9V battery. Run the program, and every minute it will write a file to the Mbed. But after the battery was empty, and i've wanted to check the files (hooking the mbed with USB to my computer) , nothing was on the Mbed. Even the program itself was gone ??? I've checked the program first while running on USB. so i know that it will write to the MBED.

heres the program:

// a way to create incrementing filenames, sford
// every time you hit reset, will create a file with incremental filename

#include "mbed.h"

LocalFileSystem local("local");               // Create the local filesystem under the name "local"

int main() {
    char filename[64];    
    int n = 0;
    
    // set "filename" equal to the next file to write
    while(1) {
        sprintf(filename, "/local/file%03d.txt", n);    // construct the filename fileNNN.txt
        FILE *fp = fopen(filename, "r");                // try and open it
        if(fp == NULL) {                                // if not found, we're done!
            break;
        }
        fclose(fp);                                     // close the file
        n++;                                            // and try the next one
    }
    wait(60);
    FILE *fp = fopen(filename, "w");
    fprintf(fp, "I am file # %d\n", n);
    fclose(fp);
    main();
}

Is there any other way to check it. And how do i prevent that the program is getting lost ??? (most important)

15 Mar 2011

marcel van de Kamp wrote:

Is there any other way to check it

How about just reporting the elapsed time. Something like

#include "mbed.h"

LocalFileSystem local("local");               // Create the local filesystem under the name "local"
Timer t;

int main() {
    time_t starttime = time( NULL );

    // Create the file to initialize it, then close it
    FILE *fp = fopen( "/local/timefile.dat", "w");
    fprintf(fp, "Start of test data./n");
    fclose(fp);

    // Set a timer and every 60 seconds append the new elapsed time value:
    t.start();
    while(1) {
        while(t.read() < 60);
        t.reset();
        fopen("/local/timefile.dat", "a");
        fprintf(fp,"%d\n", time( NULL ) - starttime;
        fclose(fp);
    }
}

This should create a single file, "timefile.dat" on the local file system. It then writes a succession of entries, each giving the elapsed time between the start of the test and the instant at which that particular entry was written.

After the mbed power dies, you should be able to plug in the USB cable and use your PC to read the contents of timefile.dat from the USB drive. The last entry will show the total time elapsed.

I haven't tested this - or even compiled it - so some tweaking may be needed.

hth

16 Mar 2011

Hi,

thx for the input. But the main problem will be that, when the mbed doesn't have any power, all will be lost. the file, and program. That i find very strange. I've also noticed that, if you're writing a program to the Mbed, en you're pulling out de USB cable during transfer, all programs are lost. That could be a problem for my project. I dont want to lose the program if the battery is empty.

16 Mar 2011

Hi Marcel,

In your first example, it is worth noting you have a call to main() within main(). Whilst this appears to give the behaviour you want (looping), it is actually a bad idea as it is really recursion. As you call the same function again, it hasn't returned from the current function so it is actually building up an ever growing call stack, until you may eventually run out of memory!

Much better would be to put it in a while(1) loop.

Whilst the power going is not going to be good, I wouldn't necessarily expect it to corrupt the drive (although it could). Testing hexleys program would be a good idea.

Simon

16 Mar 2011

Simon,

You're quit right about the loop. Didn't think about that. I will test the program. I just want to test how long the Mbed will run. thnx for all the responses!!

18 Mar 2011

I wrote the next program. Tested it with external supply. If i bring the power down and checking afterwards program and files are still in place. Only thing i find strange? is that i can reset/bring back to life the controller, only by connecting the usb cable, not with an external powersupply. Is it possible to bring it back on with an external supply?

#include "mbed.h"
#include "PowerControl/PowerControl.h"
#include "PowerControl/EthernetPowerControl.h"

#define USR_POWERDOWN (0x104)
int semihost_powerdown() {
    uint32_t arg;
    return __semihost(USR_POWERDOWN, &arg);
    }
    
    void BrownOut() {    
    Peripheral_PowerDown(0xFFFFFFFF);
    Sleep();
    }
    
LocalFileSystem local ("local");
int main() {

    int result, count;
    result = semihost_powerdown();
    NVIC_SetVector(BOD_IRQn, (unsigned)BrownOut);
    NVIC_EnableIRQ(BOD_IRQn);
    count = 1;
    int c=0;  
    int a, b, x, minutes, hour;    
     minutes = 0;
     hour = 0;
     int n = 0; 
     char filename[64];
     
    while(1)
    {
    for (int i=0; i < 10000; i++) 
    {
    // run programm until Brownout is detected
    count = count <<1;
    if (count > 0x08) count = 0x01;
     // run main program
     
     a=100;
     b=35;
     x = a *b / (a+b) * a * (b-a) / a * b *a/(a*b) / a-b ;
     minutes = minutes + 1;
        if (minutes == 60) 
        {
        hour = hour +1;
        minutes = 0;
        }
    
     wait(60);
       sprintf(filename, "/local/file%03d.txt", n);    // construct the filename fileNNN.txt
        FILE *fp = fopen(filename, "r");                // try and open it
        if(fp == NULL) {                                // if not found, we're done!
            break;
        }
        fclose(fp);                                     // close the file
        n++;                                            // and try the next one
    }
    
    FILE *fp = fopen(filename, "w");
    fprintf(fp, "I am file # %d  minutes past: %d  hours past: %d\n", n, minutes, hour);
    fclose(fp);
   }
}