avr-gcc-list
[Top][All Lists]
Advanced

[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: Theodore A. Roth
Subject: Re: [avr-gcc-list] Pointer to a function in a structure in flash?
Date: Mon, 27 Oct 2003 11:56:56 -0800 (PST)


On Mon, 27 Oct 2003, Bob Paddock wrote:

>
> > Ok, how are you defining cmd_func_table? Does it reside in
> > RAM or Flash? How are you initializing it?
>
> I want it all in Flash, strings, tables, pointers struct ...
>
> This shows one complete entity:
>
> 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;
>
> static CONST char FLASH baud_str[] PROGMEM = "BAUD";
>
> static uint16_t set_baud( CONST char * rate_ptr, CONST char * tok_ptr )
> {
> ...
> }
>
> static FLASH str_func_entry cmd_func_table[] PROGMEM =
> {
>      {baud_str,               set_baud}
> }

I'm doing something very similar in one of my projects. It looks like
this:

    int
    example_menu_1 (FILE *fp_in, FILE *fp_out)
    {
        // Code to handle selection 1.

        return MENU_CONT;
    }

    int
    example_menu_2 (FILE *fp_in, FILE *fp_out)
    {
        // Code to handle selection 2.

        return MENU_CONT;
    }

    void
    example_menu_display (FILE *fp_in, FILE *fp_out)
    {
        static char P_desc_ex_1[] PROGMEM = "Example Option 1.";
        static char P_desc_ex_2[] PROGMEM = "Example Option 2.";
        static char P_desc_exit[] PROGMEM = "Exit Data Logger.";

        static struct menuitem P_top_menu[] PROGMEM = {
            { example_menu_1, P_desc_ex_1 },
            { example_menu_2, P_desc_ex_2 },
            { menu_exit,      P_desc_exit },
            MENU_TERMINATOR
        };

        static char P_title[] PROGMEM = "An Example Menu";

        menu_display (fp_in, fp_out, P_title, P_top_menu);
    }

The menuitem is defined like this:

    typedef int (*FP_MenuItemHandler)(FILE *fp_in, FILE *fp_out);

    struct menuitem
    {
        FP_MenuItemHandler handler;
        const char * PROGMEM desc;
    };

The menu_display function then calls the menu item handler like this:

    /* Call the handler. */

    memcpy_P (mi, menu+(choice-1), sizeof (struct menuitem));
    if (mi->handler (fp_in, fp_out) == MENU_EXIT)
        break;

mi is a menuitem in sram and menu is the array in program space.

This works fine with GCC. I have no idea if IAR would need to use the
obsolete (*mi->handler) (fp_in, fp_out) construct when calling the
handler.




reply via email to

[Prev in Thread] Current Thread [Next in Thread]