[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [avr-gcc-list] Pointer to a function in a structure in flash?
From: |
E . Weddington |
Subject: |
Re: [avr-gcc-list] Pointer to a function in a structure in flash? |
Date: |
Mon, 27 Oct 2003 17:32:21 GMT |
>
> I'm having more problems converting code from IAR to GCC,
in getting
info>
> from
> flash:
>
> I have this structure:
>
> typedef struct /* define the struct 'cmd_entry' */
> {
> /* Pointer to string in flash: */
> PGM_P command;
>
> /* Pointer to function in flash returning an int,
passed a char
> pointer: */
> uint16_t (*function)(CONST char * tok_buf, CONST
char * tok_ptr);
> }str_func_entry;
>
> This IAR code does what I want:
> flash_str_out( cmd_func_table[i].command );
>
> which to get to work in GCC I had to change to this:
>
> flash_str_out( (PGM_P) pgm_read_word( &cmd_func_table
[i].command )
)> ;
>
> However I can not figure out the right magic words to
convert this
> section, that has a
> pointer to a function in a structure in flash:
>
> Working IAR code snippet (STRCMP = strcmp_P):
> /* If find a table match then call that function: */
> if( !STRCMP( (const char *) token , cmd_func_table
[i].command ) )
> {
> /* Call the function from the function table: */
> retval = ( *(cmd_func_table[i].function) )( (char
const *) token,
(c> har
> const *) tokptr );
> }
>
> I tried this, and other variations, with various casts:
> retval = ( *(pgm_read_word( &cmd_func_table
[i].function )) )( (char
> const *) token, (char const *) tokptr );
>
> but then I get errors like "object is not function" or
> "invalid type argument of 'unary *'".
>
> Suggestions?
>
You're close. What you need to do is to typecast the
pointer that is returned from pgm_read_word() to the type
that it is pointing to (the particular function) before
dereferencing the pointer and calling the function.
Unfortunately this could look a bit messy without doing
some additional typedefs beforehand. I would suggest this
(sorry for the gratuitous wrapping):
typedef uint16_t (*functype_t)(CONST char * tok_buf, CONST
char * tok_ptr);
typedef struct
{
PGM_P command;
functype_t function;
}str_func_entry;
retval = (*((functype_t)(pgm_read_word(&cmd_func_table
[i].function))))((char const *)token, (char const *)tokptr);
this is untested of course.
HTH
Eric
Re: [avr-gcc-list] Pointer to a function in a structure in flash?, E . Weddington, 2003/10/27
Re: [avr-gcc-list] Pointer to a function in a structure in flash?, E . Weddington, 2003/10/27
Re: [avr-gcc-list] Pointer to a function in a structure in flash?, E . Weddington, 2003/10/27