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

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

Re: [avr-gcc-list] Array of pointers to functions revisited...


From: E. Weddington
Subject: Re: [avr-gcc-list] Array of pointers to functions revisited...
Date: Mon, 19 Jul 2004 07:43:47 -0600

On 19 Jul 2004 at 6:34, Ned Konz wrote:

> On Sunday 18 July 2004 2:52 pm, Royce & Sharal Pereira wrote:
> > Now when I looked at the .lss file it wasn't any fun() :)
> >
> > The array call_fun had anything but the addresses of fun1, fun2 etc. !!
> > In the .lst file I see a   '.word pm(fun1)' instead of a '.word fun1'
> > etc.(BTW What's this pm ?)
> >
> > The second problem is- The line-
> >
> > fun= pgm_read_word(call_fun[f_cnt]);
> >
> > does not get the desired address correctly. There is a un-necessary 'ld'
> > before 'lpm' which destroys the Z reg, so that the fetched address is
> > wrong(I assume you will compile my code & see the result).
>
> Why are you all going through all of the pgm_read_word() stuff? Just good old
> C...
>
> --------
> /*
>  avr-gcc -O3 -Wall -Wl,-Map,test.map -g -mtiny-stack -mint8 -mmcu=atmega16 -o
> test.elf test.c;
>  avr-objdump -h -S  test.elf > test.lst
>
>  R16/17 is call_fun
>  R26 is X reg
>  R28 is Y reg
>  R30 is Z register
> */
>
> #include <inttypes.h>
> #include <avr/pgmspace.h>
>
> static void fun1(void) { }
> static void fun2(void) { }
> static void fun3(void) { }
>
> typedef void (PROGMEM * pFun)(void);
>
> static pFun PROGMEM call_fun [3] = { &fun1, &fun2, &fun3 };
>
> int main(void)
> {
>       uint8_t i;
>       for (i = 0; i< 3; i++)
>               (call_fun[i])();
>       return 0;
> }
>
> --------
>
> Which produces (on avr-gcc 3.4.0) the extremely straightforward:
>

Which is wrong.

The call_fun array above has been put into the Program Memory Space by using 
the keyword
PROGMEM. Therefore, to access those values, one needs to pull them from Program 
Memory
Space by using the <avr/progmem.h> API, pgm_read_word(), which reads a word (16 
bits)
from the Program Memory Space. This reads the specified array slot which 
contains the
address of the function, which is then dereferenced and called.

If you leave off the PROGMEM from call_fun, then you can use your code as above.

Eric



reply via email to

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