[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[avr-gcc-list] Solution to function pointers in program memory-FYI

From: Guy Robinson
Subject: [avr-gcc-list] Solution to function pointers in program memory-FYI
Date: Sun, 6 May 2001 22:08:26 +1200

For those who have been following my attempt to get an array of function
pointers into program memory please have a look at the following solution.
Most of this is due to Larry Barello's code examples and help (thank you
Larry)and as written works correctly. The program reads a number from a SRAM
tasklist(FuncToRun) and runs the appropriate function from the function
array in program memory.

I don't know if the pgm_rw macro will work in every circumstance but it does
here. Any GCC assembler experts please feel free to poke holes in the macro
as a reliable read word from flash macro would to be very useful command to
have around.



#include <io.h>
#include <pgmspace.h>

typedef unsigned char u08;
typedef unsigned int u16;

/* the actual functions all must match the type TFunctionPtr */
typedef void(*TFunctionPtr)();

#define pgm_rw(addr) ({     \
    u16 __word;             \
    __asm__ (               \
        "mov r30,%A1\n\t" \
        "mov r31,%B1\n\t" \
        "lpm\n\t"           \
        "mov %A1, R0\n\t" \
        "adiw r30, 1\n\t"  \
        "lpm\n\t"           \
        "mov %A0,%A1\n\t" \
        "mov %B0,R0\n\t" \
        : "=z" (__word)     \
        : "r" ((u16)(addr)) \
        : "r0" );           \
    __word;                 \

void function0(); // function prototypes
void function1();
void RunFunc();

static u08 FuncToRun[] ={1,0,1,1,0,0,1,1,0,99};
static u08 *pFTR;  // pointer to FunctionToRun array
// function pointer array in program memory
TFunctionPtr CommandArray[] __attribute__ ((progmem))={function0,function1};

void main(void)
    pFTR = &FuncToRun[0];   // initialise pointer
    while(*pFTR != 99);

// run required function
void RunFunc()

// functions
void function0(){
void function1(){

reply via email to

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