const __FLASH correct syntax/usage

Discussion and support of the compiler from ImageCraft for the M8C.

Moderator: richard@imagecraft.com

const __FLASH correct syntax/usage

Postby moxbox » Wed Jan 11, 2012 9:58 am

Hi Folks,

I'm having a devil of time getting my "Hayes Command" parser to work in ImageCraft,
and this is code that worked quite well in Hi_Tech.
I've tried many variation of const/__flash statement placement,
but can't seem to find the right combination.
Any help would be most welcome...

I get these errors :
main.c(103): invalid initialization type; found `pointer to int function(pointer to char)' expected `pointer to const int function(pointer to char)'

Here is the (original working in Hi-Tech) example snippet :

main.c
------------------
typedef struct
{
char *command;
int (*function)( char*);
} vector;


vector commandTable[] =
{
{"ATZ", ATZ_func},
{"ATD", ATD_func},
{"ATH", ATH_func}
};

void executeCommand( char* inputLine, vector commandTable)
{
char* command = getCommand( inputLine );

for ( int k = 0, k < TABLESIZE; k++ )
if ( strcmp( command, commandTable[k].command ) == 0 )
commandTable[k].function( inputLine );
}

---------------------------
I will also attach the whole main.c file for reference...

thanks,
Tom

-------------------------------
main.c
Hayes AT Parser
(4.64 KiB) Downloaded 173 times
If we knew what we were doing, it wouldn't be called research, would it?
-- Albert Einstein
moxbox
Cheese Cube
Cheese Cube
 
Posts: 41
Joined: Thu Oct 15, 2009 9:59 am
Website: http://www.moxon.com
Location: Portland, OR

Re: const __FLASH correct syntax/usage

Postby bobmarlowe » Wed Jan 11, 2012 10:16 am

OK, the most obvious: main() is a function!!! (that's C-language) All variables defined here are allocated on the stack, thus cannot be in const program-memory (as you would like to have as I understood).
There is an example in the c-programmers manual of defining const data.
But to be honest, it took me several hours to define an array of pointers to functions in program memory and call them correctly.

Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1490
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany

Re: const __FLASH correct syntax/usage

Postby moxbox » Wed Jan 11, 2012 11:05 am

Hi,

Okay, well after fiddling a bit more I did figure this out.
The key is putting 'const" on each member of the struct,
and using the "cstrcmp" function to indicate that one of the strings
is stored in flash...

here's what works :
Code: Select all
void main(void)
{
    char  * strPtr;                       // Parameter pointer 
    int    cmdcnt;                        // count of commands
    int    numcmd;                        // number of total commands
    int i,j;
   
typedef struct
    {
       const char *command;
       const char (*function)( char* );
    } vector;
   
vector commandTable[] =
    {
       {"ATZ",  ATZcmd},
       {"ATH",  ATHcmd},
       {"ATD",  ATDcmd},
       {"ATDT", ATDTcmd}
    };
    numcmd = 4;
   
    M8C_EnableGInt ;   // Turn on interrupts
   UART_Initialize();
   LCD_Initialize();
   
    while(1)
   { 
       if( UART_bCmdCheck() )
      {                                         // Wait for command   
          if( strPtr = UART_szGetParam() )
        {                                      // More than delimiter? 
             for ( cmdcnt = 0; cmdcnt < numcmd; cmdcnt++ )
          {
                 if ( cstrcmp( commandTable[cmdcnt].command, strPtr ) == 0 )
             {
                      commandTable[cmdcnt].function(strPtr);
                 break;
              }
             }
          if ( cmdcnt >= numcmd ) Undefinedcmd(strPtr);
        }   
          UART_CmdReset();                       // Reset command buffer
       }
    }   
} // main.c


and the observent will note that :
numcmd = sizeof(commandTable);
is not what I inteded, it returns the size (in bytes) of the struct,
and not the number of entries...

So, this is all working now, thanks...

;-) Tom
If we knew what we were doing, it wouldn't be called research, would it?
-- Albert Einstein
moxbox
Cheese Cube
Cheese Cube
 
Posts: 41
Joined: Thu Oct 15, 2009 9:59 am
Website: http://www.moxon.com
Location: Portland, OR

Re: const __FLASH correct syntax/usage

Postby slandrum » Wed Jan 11, 2012 11:16 am

One good way to get the number of elements in an array is:
elementsInArray = sizeof(array)/sizeof(*array);
slandrum
The Big Cheese
The Big Cheese
 
Posts: 453
Joined: Fri Feb 29, 2008 11:38 am
Location: California

Re: const __FLASH correct syntax/usage

Postby moxbox » Wed Jan 11, 2012 11:22 am

bobmarlowe wrote:OK, the most obvious: main() is a function!!! (that's C-language) All variables defined here are allocated on the stack, thus cannot be in const program-memory (as you would like to have as I understood).
There is an example in the c-programmers manual of defining const data.
But to be honest, it took me several hours to define an array of pointers to functions in program memory and call them correctly.

Bob


Bob,

Thanks for your help, as with most things -
it helps to read (then reread) the manual.
(And then read it again - more slowly...)
Here's the relevent passages :

6.12 Stack Architecture and Frame Layout
The stack must reside on the last page of data memory and grows towards high memory. Most local
variables (non-static) and function parameters are allocated on the stack. A typical function stack
frame would be:
[high address]
[returned values]
X: [local variables and other compiler generated temporaries]
[return address]
[incoming arguments]
[old X]
[low address]
Register X is used as the frame pointer and for accessing all stacked items. Because the M8C limits
the stack access to one page, no more than 256 bytes can be allocated on the stack, even if the
device supports more than 256 bytes of RAM. Less RAM is available to the stack if the total RAM
space is 256 bytes for the target device.

6.13 Strings
The compiler allocates all literal strings in program memory. Effectively, the type for declaring a literal
string is const char and the type for referencing it is const char*. You must ensure that function
parameters take the appropriate argument type.

So that main takeway here is that if you get the dreaded :

error (xxx) : pointer to __flash char , expected pointer to char

error message, then check BOTH your variable declaration and library/function call.
Make sure to use the "c" functions for const variables (i.e. variables defined to be located in flash).
there are "cstrcmp", "cstrcpy", etc. versions for things defined as "const"...

regards,
Tom
If we knew what we were doing, it wouldn't be called research, would it?
-- Albert Einstein
moxbox
Cheese Cube
Cheese Cube
 
Posts: 41
Joined: Thu Oct 15, 2009 9:59 am
Website: http://www.moxon.com
Location: Portland, OR

Re: const __FLASH correct syntax/usage

Postby moxbox » Wed Jan 11, 2012 11:43 am

slandrum wrote:One good way to get the number of elements in an array is:
elementsInArray = sizeof(array)/sizeof(*array);


That works nicely!

Thanks,
Tom
If we knew what we were doing, it wouldn't be called research, would it?
-- Albert Einstein
moxbox
Cheese Cube
Cheese Cube
 
Posts: 41
Joined: Thu Oct 15, 2009 9:59 am
Website: http://www.moxon.com
Location: Portland, OR

Re: const __FLASH correct syntax/usage

Postby bobmarlowe » Wed Jan 11, 2012 2:37 pm

And I thought you would like to save unneccesary used ram and put your vector-table into flash which is done with
Code: Select all
   typedef struct
    {
       const char *command;
       int(*function)( char* );
    } vector;
   
   const vector commandTable[] =
    {
       {"ATZ",  &ATZcmd},
       {"ATH",  &ATHcmd},
       {"ATD",  &ATDcmd},
       {"ATDT", &ATDTcmd}
    };

void main(void)
{
    // Insert your main routine code here.
   
   char  * strPtr;                       // Parameter pointer 
    int    cmdcnt;                        // count of commands
    int    numcmd;                        // number of total commands

your above given example does not compile at my site.
Bob
User avatar
bobmarlowe
The Big Cheese
The Big Cheese
 
Posts: 1490
Joined: Thu Oct 06, 2011 2:03 am
Location: Germany


Return to “%s” ImageCraft ICCv7 for M8C Compiler

Who is online

Users browsing this forum: No registered users and 1 guest