override built-in code?

hi all -

sorry if this has been asked before: is there a simple way to override arduino built-in code from inside a sketch?

my specific example is that i’m adding some code to the timer0 overflow interrupt routine. this is for a servo motor library that i want to release publicly. i would really rather not require people to go in and edit their wiring.c library files. is there some self-contained way in the sketch that i can tell the compiler to ignore the previous declaration of the ISR (or some other library routine), and use mine instead?

thanks… jm

Unfortunately, I don't think there is at the moment (although I might be wrong). It would be nice to have, but it also gets tricky - how do people know that the code has been overridden (and, for example, some of the PWM pins won't work); what happens if two libraries both try to override the same function?

One thing, though, that we should be adding soon, is a hook for your own timer overflow functions, which will get called by the one in the core. That way, both the core and your library should be able to use the timers. Actually, this is probably not too hard to implement - if you wanted to take a crack at it, I'd probably incorporate it into the core. Creating those hooks in combination with a use-case for them would probably make sure that they're actually useful, which would probably be better than whatever I would have come up with without knowing how they'd be used.

So, I'm sorry I don't have a better answer for you at the moment, but I think we should be able to come up with something good for the future.

I too have been thinking about this. I have added some #ifdef statements to the arduino code and recompiled the core code. So, for example, if I don't need any of the delay features, I add a #define nodelay statement to my sketch and this will disable any of the timer 0 overflow code in arduino.

ok, thanks. i had already put something like #ifndef USE_CUSTOM_T0_ISR / #endif, around the ISR in wiring.c, for my own use, so that it wouldn’t affect any other of my sketches. then i just copy the one line of the ISR into my own custom one, to keep the millis counter going. works fine for what i need, can’t think of anything fancier offhand. actually i prefer that to having a function called by the core ISR, to keep the overhead low.

if you wanted to include those #if statements in the distribution, it would solve my problem.

as for a more general code-overriding mechanism, i’m not sure it’s totally necessary, since you can always just write your own functions and use them instead of the built-in ones. i haven’t thought about it much. but maybe just for things like the ISRs, you could include some conditional compilation statements in the core code.


hi - just coming back to this after a couple of months. now i'm confused, i'm not able to get this method to work anymore (did i ever have it working in the first place?) - if i make my wiring.c look like this:


SIGNAL(SIG_OVERFLOW0) { timer0_overflow_count++; }


and then i put #define USE_CUSTOM_T0_ISR into my sketch, plus my own custom ISR, it doesn't work, i get an error about "multiple definition of __vector_16". but if i put the same #define USE_CUSTOM_T0_ISR directly into the wiring.c file, it works. should a #define in the sketch affect directives in wiring.c?

i tried arduino0007, 0008, 0009, on mac and windows, just can't get it to work. i thought i had it working before, and it sounds like the same thing worked for zephyr, so now i have that "losing my mind" feeling... any ideas?

I can believe it never worked - the #define would need to be above the #ifdef (i.e. in a file included from wiring.c), and your sketch isn't. I still don't really know of a good way to handle this nicely in general, although for the timer interrupts we should probably add a hook to a user-defined function (similar to the external interrupts).

makes sense - well, i don't know what was going on before, or why zephyr seemed to think it worked...

anyway, it would be really nice to be able to add code to the timer 0 interrupt routine. i had a look at the attachInterrupt stuff for an example of how to add in a hook, but unfortunately it seemed a little more complicated than i have the time to deal with at the moment.

basically i've written an eight-channel r/c servo controller system, and i've also integrated it into Firmata/Pduino. it seems to work reasonably well and i'd like to release it. but i don't want to release something that won't even compile, in the standard environment.

since it works good enough for me, i have to move on to some other priorities at the moment. if you get a chance to think about it, that would be great, otherwise maybe i'll have more time in a month or so.