kyle.tk wrote:
I am having some (compiler) problems with the following code. The
compiler complains of "warning: assignment makes pointer from integer
without a cast" at the lines I indicated. But it still compiles and
works as expected. So what is the proper way to write this.
void test(void){
printk("test\n");
}
/* In main */
register_isr(29,test);
/* In isr.c */
int register_isr(unsigned int vector, void (*isr)(void)){
volatile unsigned long *vaddr; /* Vector Address */
Here you declare vaddr as a pointer.
vaddr = (volatile unsigned long) EVT_BASE + (vector * 4); /*
warings here */
Here you assign an integer to it. Integers and pointers are *not* the
same thing, so the compiler is quite correct to complain.
Not knowing the definition of EVT_BASE is it impossible to say what the
correct solution to it is. However, I would guess that what you want to
do is loose the cast and ensure that EVT_BASE is of the correct type.
*vaddr = isr; /* and here */
Here you try to assign a pointer to a function to the location that
vaddr points to but vaddr is defined as pointing to an integer
(specifically an unsigned long) not a function pointer.
return 0;
}
So whats wrong with this?
The problem seems to be you thinking pointers and integers are the same
thing. They most definitely are *not* the same.
If you post a compilable minimal example (which would at least include
the definition of EVT_BASE and preferable some sample code calling this
function) together with what you think you are trying to do we can help
better.
I'm guessing that EVT_BASE is the start of an interrupt vector table
(which is OT here) and this function is trying to assign functions to
specific location.
I think what you actually want is something like the following untested
code:
typedef void (*isr_t)(void);
#define EVT ((volatile isr_t*)0x1234)
void register_isr(unsigned int vector, isr_t isr)
{
EVT[vector] = isr;
}
Another possible definition for EVT is:
extern volatile isr_t EVT[NO_OF_ISRS];
Then you can possibly use some linker magic (which is OT here) to place
EVT at the correct location.
Note the use of the typedef for the function pointer to make writing the
rest of it simpler. Also note that apart from casting the address of the
interrupt vector table to a pointer I've got rid of the need to cast
anything. In general, casts are bad. However, if I'm correct in my
assumptions of what you are trying to do, this is an instance where you
need to convert an integer to a pointer, something which gives an
implementation defined result at best and undefined behaviour at worst.
Please note that you are skirting the edges of topicality. How your
interrupt vector table works in not topical, but given that it is an
array of pointers to functions at an address that you know we can
discuss ways you can write to it.
--
Flash Gordon
Living in interesting times.
Web site -
http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro -
http://clc-wiki.net/wiki/Intro_to_clc