Reputation: 761
Let's say I have a micro controller and I know that when it receives an interrupt it will jump to address 0x8000 (just as an example). How do I define that in C, it seems like it might be similar to casting an address to a variable so I can read or write memory.
Usually the support package comes with some pragma statement or other way to indicate where your interrupt code is. But I want to understand how you would do just do it in standard C. Some kind of function pointers or something?
Upvotes: 1
Views: 790
Reputation: 227
For interrupt routine you want to place your code at the desired address (0x8000). Its not just a casting address as you have asked. The #pragma macro creates a function table of all the code that is defined by that macro. Along with this function table you will need to use a linker descriptor file. This descriptor file will specify the physical addresses regions of memory. Then you can place your #pragma code by sections in a particular physical address.
In order to do the same in C you will have to follow similar process in program. You have to copy the function code to the address desired. You can easily get the function address start by the function name. But this is not enough as you also need to save current CPU state and variables on stack. Setup the stack for local variables and also setup the external references correctly. This part is done by compiler for you when a function call is done. One of the trick in such case is to use relocatable functions. but these would still need some compiler options to get you the function start and end addresses. You can check your compiler guide for relocatable option
Upvotes: 0
Reputation: 12515
Not a straight C answer here, but here's how I've seen it done on systems I've worked with:
The best way to place a function at an address is to use a linker script of some sort. This lets you control how the code will align and run. Not sure which flavor of C you are using, but almost all I've used had the ability to set locations via the linker.
Next up, you could define a pointer at that location which is a "jump" instruction to wherever you want your interrupt code to live. This would probably be best done with an assembler and an .org statement though. This is how we did it on very old hardware as it allowed us to easily change what the vector did by just changing the actual jump op-code to point elsewhere.
Upvotes: 1