storing fixed data table in flash

30 Mar 2012

With the help of several on the forum (tnx Wim and the rest) last project done. On to the next... Would like to design and implement an antenna rotor controller (amateur radio). Conceptually, this seems a fairly simple nut to crack. Rotor has pot that outputs variable voltage to indicate direction. Right and left turn inputs are digital; easy. I'd like to be able to command direction by dialling up a country name or prefix and need a way to stash this (fixed) data on chip. Data then would be ascii character strings and lat/lon data. I've prowled thru the forum and seen similar questions asked. Not sure I've seen that anyone has been successful in bringing it off. Can anyone give me a nudge here?? Tnx...

garyb

30 Mar 2012

How big is the data and what's the range of values?

31 Mar 2012

hey Igor

each 'entry' in the table would consist of a character string of approx 15 (country, prefix) and 2 signed numbers for corresponding lat and lon (+/- 90, 180 respectively). suppose it would be smart to fix the # of bytes per entry to make indexing easy. total # of entries would be perhaps 300 or so. ideas?

31 Mar 2012

Sounds like it should be small enough to fit in a static table in the flash. For example:

// countries.h
struct country_data
{
  const char *name;
  float lat, lon;
};
extern const country_data country_table[];

// countries.cpp
#include "countries.h"
const country_data country_table[] = {
  { "country1", 24.33, 234.33 },
  { "country2", -44.33, +123.33 },
  //...
};

Then you can just include countries.h where you need to use the data.

03 Apr 2012

tnx Igor! looks like this might a one 'chip' solution. 1 question: do i have to define the number of table entries in the header (or the code)?? tnx again...

garyb

03 Apr 2012

The compiler will figure out the number of entries automatically. You can use the empty brackets [] as shown in the example. However, you probably want to know the number of entries in your code to loop through all of them. In general the compiler can figure that out for you by using the sizeof function, but it has some issues when you use separate header and cpp files like in this example. I would suggest using a constant number of entries to keep it simple.

03 Apr 2012

Or you can mark the end of the struct. In this case I'd use NIL (0L) as the string pointer in an empty record This is basically like how 'C' strings work. In this case there is the disadvantage that it uses up several bytes. But it avoids the problem of working across multiple files.

03 Apr 2012

You should be able to do this:

// countries.h
extern const country_data country_table[];
extern const int country_count;

// countries.cpp
#include "countries.h"
const country_data country_table[] = {
  //...
};
const int country_count = sizeof(country_table) / sizeof(country_table[0]);
04 Apr 2012

tnx guys. will commence with the experimenting...

garyb

07 Apr 2012

well, i'm beginning with something simple here. just trying to access data in country_table and print one entry to the console. compiler doesnt like at all and kicks out "argument is incompatible with corresponding format string conversion" and "the format string requires additional arguments". my starter code is below. i'm sure that i'm overlooking the obvious... ideas gents?

#ifndef mbed_countries_H
#define mbed_countries_H
#include "mbed.h"

namespace mbed {

struct country_data {
    const char *name;
    float lat, lon;
};
extern const country_data country_table[];
extern const int country_count;
}
#endif
______________________________
#include "mbed.h"
#include "countries.h"

Serial pc(USBTX, USBRX);

int main() {
    pc.baud(38400);
    const country_data country_table[] = {
        { "country1", 24.33, 234.33 },
        { "country2", -44.33, 123.33 },
        { "country3", -15.67, 74.11 },
    };
printf ("%s %3.2f %3.2f \n", country_table[0]);
}
07 Apr 2012

printf cannot use a structure like that, you'll need to list all fields explicitly:

printf ("%s %3.2f %3.2f \n", country_table[0].name, country_table[0].lat, country_table[0].lon);
07 Apr 2012

tnx Igor. that was easy enough. learning how to simply manipulate data elements now. really looks like c++ is WELL suited to this. more experimenting then...

garyb

07 Apr 2012

my next problem may be a bit tougher... i need to store a modifiable variable or two in flash (rotor full scale and zero ADC values). obviously can't set aside flash with const. ideas??

10 Apr 2012

Gary: Would the local file system allow you to do what you want?

http://mbed.org/handbook/LocalFileSystem

Chuck, W5UXH

gary belcaster wrote:

my next problem may be a bit tougher... i need to store a modifiable variable or two in flash (rotor full scale and zero ADC values). obviously can't set aside flash with const. ideas??