Bin files on the fly

30 Mar 2012

Is it possible to switch bin files on the fly, say I have 2 bin files on mbed and I want to switch between them is this possible somehow.

30 Mar 2012

as they stay on the flash drive, could you get the code to rename the one you don't want to use with a

.non instead of a .bin extension ?

and then do a software reboot ?

I don't know if "rename" is part of the standard C file handling libraries though.

(I'm quite new to C)

30 Mar 2012

Well I want to do it by the hardware reset route, so when I press the reset button it will switch bins.

I know for software it could be something like this perharps

int main (void)
{
for (;;)
If

extern "C" void mbed_reset();

flash.write = binfile1

else

flash.write = binfile2

endif


}

but not sure what would need to be done for the hardware route still looking and reading the forum

30 Mar 2012

how about

2 ".bin" files and a ".cfg" file on the flash drive. Each bin file would read the config file, and rename the .bin files depending on the config file, and then reboot to load the .bin file that is now the only one called .bin

(I hope that makes sense)

It's the only way I can think of doing it using the reset button, the reset button would cause a toggle between the two files.

PSEUDO CODE WARNING.

open "lastStatus.cfg" file;

read laststatus from lastStatus.cfg;

delete laststatus.cfg;

if (lastStatus == 1) {
write 0 into a new lastStatus.cfg
} else {
write 1 into a new lastStatus.cfg
}

if (laststatus == 1) {
rename binfile1 to .old;
rename binfile2 to .bin;
}

if (laststatus == 0) {
rename binfile1 to .bin;
rename binfile2 to .old;
}

perform software reboot;

01 Apr 2012

Does not seem to be a way to rename the bin file on mbed no library or method seems to exists or has been brought to the light of the mbed community yet.

#include "mbed.h"
#include "ConfigFile.h"
 
LocalFileSystem local("local");
ConfigFile cfg;

int main(void) {
    
    /*
     * Read a configuration file from a mbed.
     */
    cfg.read("/local/lastStatus.cfg");

if (lastStatus == 1) {  cfg.write("/local/lastStatus.cfg");{
 0 
} else {
 1
}

//For the software reset, you can put extern "C" void mbed_reset(); in your code and then call mbed_reset()
extern "C" void mbed_reset();


mbed_reset();

was gonna try the lib http://mbed.org/cookbook/ConfigFile

is comment here about porting coreutils

at the moment the only other way I can think of is if I can overwrite the time date stamp of the binfile using the cfg file method you suggested:)

like discussed in this thread

http://mbed.org/forum/mbed/topic/654/

//probably does not work but I had a a go at it
if (laststatus == 1) {
strcat(fname,datestamp, buf,40, "%a,%d %m 1990Y.%H:%M:%S\r",);}

if (laststatus == 0) {strcat(fname,datestamp, buf,40, "%a,%d %m 1997Y.%H:%M:%S\r",);}
01 Apr 2012

Just remove the existing .bin file and then create a new bin file with fopen("/local/run.bin", "w") and copy the data from the desired .old file or whatever.

There is no copy API either so you will need to open the source file for read and the destination file for write and read from the source and write to the destination in chunks of 512 or so bytes at a time.

01 Apr 2012

Adam Green wrote:

Just remove the existing .bin file and then create a new bin file with fopen("/local/run.bin", "w") and copy the data from the desired .old file or whatever.

There is no copy API either so you will need to open the source file for read and the destination file for write and read from the source and write to the destination in chunks of 512 or so bytes at a time.

yes but if the bin is removed, how can a copy it, does it get copied out of what is stored in flash, I see this is time consuming method, I want fast switches, it would hurt flash as well with so many read/write cycles, because I plan to do a lot of switches, hence why I think it is better faster to just change the date stamp. Or if somebody could port something so we could rename files, but I like time stamp change the best tho:)

02 Apr 2012

I found rename but can not get it to work properly yet anybody know why

 #include "mbed.h"

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

 int main() {
 FILE *fp = fopen("/local/123out.txt", "w");  // Open "out.txt" on the local file system for writing
    fprintf(fp, "Hello World!");             
    fclose(fp);                               
    //remove("/local/out.txt");                 // Removes the file "out.txt" from the local file system
     rename("/local/123out.txt", "/local/123.txt");


 }
02 Apr 2012

rename doesn't work on the mbed because this is its implementation:

int rename(const char *old, const char *new)
{
    return -1;
}

You also can't change the time stamp of files in the LocalFileSystem since there is no semihost operation provided by the interface chip to do so with the file system that it maintains on the flash part.

02 Apr 2012

Adam Green wrote:

rename doesn't work on the mbed because this is its implementation:

int rename(const char *old, const char *new)
{
    return -1;
}

You also can't change the time stamp of files in the LocalFileSystem since there is no semihost operation provided by the interface chip to do so with the file system that it maintains on the flash part.

well surly you must know how to fix so the rename part can work, and I am still not convinced that you can not change the time stamp somehow.

02 Apr 2012

Philips Philips wrote:

int rename(const char *old, const char *new) { return -1; }

Done it, it works thanks to ARM page http://www.keil.com/support/man/docs/rlarm/rlarm_frename.htm

 #include "mbed.h"

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

 int main() {
 FILE *fp = fopen("/local/Test.txt", "w");  // Open "out.txt" on the local file system for writing
    fprintf(fp, "Hello World!");             
    fclose(fp);                               
    //remove("/local/out.txt");                 // Removes the file "out.txt" from the local file system
     //rename("/local/123out.txt", "/local/123.txt");
      if (rename ("F:Test.txt", "New name.txt") == 0)  {
    printf ("Rename Successful.\n");


 }}

oh well I thought I did still does not work

03 Apr 2012

I think I Found a way to change the timestamp, with http://mbed.org/users/emh203/libraries/chan_fatfs/lktgi7

But time stamp or boot loader seem to be only way as coping or renaming the file gives the files the default time stamp way back to 2008 so that method is a not going to work

FRESULT set_timestamp (
    char *obj,     <span>/* Pointer to the file name */</span>
    int year,
    int month,
    int mday,
    int hour,
    int min,
    int sec
)
{
    FILINFO fno;

    fno.fdate = (WORD)(((year - 1980) * 512U) | month * 32U | mday);
    fno.ftime = (WORD)(hour * 2048U | min * 32U | sec / 2U);

    return f_utime(obj, &amp;fno);

I can not figure out how to use the code tho because I have no examples

03 Apr 2012

I can't get it to read the 0 or the 1 yet from the configure file, does anybody see and errors or have anything helpful to suggest I should do thank you.

#include "mbed.h"
 #include "rename.h"
 #include "ConfigFile.h"


 LocalFileSystem local("local");             // Create the local filesystem under the name "local"
 ConfigFile cfg;
 int lastStat;
  int main(void) {
    
    /*
     * Read a configuration file from a mbed.
     */
    cfg.read("/local/lastStat.cfg");
    
    * Read a configuration value.// just added this not sure how to use it yet 
     */
    if (cfg.getValue(key1, &value[0], sizeof(value))) {
    
    
    

if (lastStat.cfg == 1) {  cfg.write("/local/lastStat.cfg", "0");
  
} else {
 cfg.write("/local/lastStat.cfg", "1");
}

if (lastStat.cfg == 1) {  file_rename("/local/Test.bin", "/local/new_name.old");}

if (lastStat.cfg == 0) {  file_rename("/local/new_name.old", "/local/Test.bin");}




// int main() {
 
  //FILE *fp = fopen("/local/Test.txt", "w");  // Open "out.txt" on the local file system for writing
   // fprintf(fp, "Hello World!");             
    //fclose(fp);                               
     //file_rename("/local/Test.bin", "/local/new_name.old");
     
    
      
    }

03 Apr 2012

would any of you know how to detect when the mbed's hardware reset button has been pushed, so I can then use that to trigger a copy rename and delete of my bin file.