This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Branch to function pointer array

Hopefully someone knows...

I'm writing a jump table for a number of functions. The jump table located '__at' a location in memory contains __asm instructions to branch to all required functions defined previously, using "__asm{B func1}", for example.

Some code I am using contains a function pointer array (*funcptr[16])(arg..), and I'm not sure how to branch to it. if I try __asm{B funcptr}, the compiler (uvision3) complains that the "Target is out of range", which is understandable. Unfortunately my asm abilities are nearly non-existent and I'm unsure how to rectify it.


extern void func1(char stuff);
extern void(* funcptr[16]) (unsigned int arg);

void usb_table(void)    __at 0x00002000
}
   __asm{B func1}                // located at 0x2000
   __asm{B funcptr}        // target out of range
{

  • Could you explain why you (think you) need assembler for this?

    Why can't you just use the function pointers in 'C' in the normal 'C' manner?
    What am I missing?

  • Thanks for replying,

    I want to put all my interrupt handling code into SRAM so that an interrupt-driven communications channel doesn't need to be stopped whilst runnig IAP commands. Using the jump table, all functions relating to this interrupt can be easily called via jump table offsets without having to know where the compiler has located them. I had defined a section of memory in the CPU just for this code, and set the jump table to the beginning of it using the __at directive.

    If there was a better way to do this in C, I'd be quite happy to do it. I just remembered that I might be able to branch to the array of function pointers using an indirect branch, such as BX, but this switches the cpu to thumb mode; I don't yet know if this can be avoided, or if it's a necessary step.

    Not sure if that really answers the question, but using the __asm branch has worked very well in previous code with standard functions.

  • Just to note, the jump table at 0x2000 is still in flash, but it will be moved to SRAM later along with the rest of the code.

  • If they're function pointers, won't you need to call them, rather than just branch to them?

  • yeah...I can see your point and admit I wasn't thinking properly. As they are function pointers, they already contain an indirect link to the actual functions, so no need for a jump table reference. I'll give it a go as it is. Sorry to waste your time, and thanks for pointing that out.