1

I am developing a simple OS, which needs a small interrupt vectors. I have been reading about the interrupt vectors in order to understand the concepts.

Many of the resources refer to like this: enter image description here

[1 ]http://www.embedded.com/electronics-blogs/programming-pointers/4025714/Modeling-interrupt-vectors#references

What confuses me is that how do I know the address of the interrupt handler? I know that I need to create different functions for the different types of interrupts but how can I know the address of the function?

I am developing my code in C.

Garf365
  • 3,619
  • 5
  • 29
  • 41
Salma
  • 119
  • 3
  • 15
  • Everything is done by linker. You write interrupt handlers, you fill a vector table with your handlers and mark it as the vector table, and linker do the rest. Have you yet developped on embedded device ? – Garf365 Nov 18 '16 at 14:13
  • 1
    The _address_ of a function is expressed by the name of the function. E.g: the address of this function: `void Foo() {bar();};` is simply `foo`. And to print that address: `printf("%p\n", (void*)foo);`. – Jabberwocky Nov 18 '16 at 14:13

1 Answers1

2

Interrupt handler is, at the end, a function.

A function starts at "an address" and you can simply use its name to retrieve it.

The following code will warn about %p parameter type passed, but is explicative:

#include <stdio.h>

void dummy_ISR ( void )
{

}

int main ( void )
{
    printf("Address of function: %p\n", dummy_ISR);

}

You can use function pointer to create a table

#include<stdio.h>

int dummy_isr1 ( void )
{
    return 0;
}

int dummy_isr2 ( void )
{
    return 0;
}

typedef int (*functionPtr)(void);

functionPtr table[] =
{
        dummy_isr1,
        dummy_isr2
};


int main(void)
{
    for (size_t i=0; i<sizeof(table)/sizeof(table[0]); i++)
    {
        printf("Address %zu: %p\n", i, table[i]);
    }

   return 0;
}
LPs
  • 16,045
  • 8
  • 30
  • 61
  • Thanks, but if I am pointing to a function, what would the type of the vector? if I have 32-bit machine – Salma Nov 18 '16 at 14:42
  • You can use function pointers to do so. Another way is to simply use `void *`, but Standard Lawyers can kill me if I officially support that solution ;) – LPs Nov 18 '16 at 14:57
  • @Salma: Note that on ARM, GCC (and some other compilers) support the [`interrupt` function attribute](https://gcc.gnu.org/onlinedocs/gcc/ARM-Function-Attributes.html) (or `isr`), which tells the compiler to generate the necessary code around the function that makes the function work safely as an interrupt service routine. For the table, you can use the [`section("name")`variable attribute](https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html#Common-Variable-Attributes), and use a linker script to tell the linker where to place that section. – Nominal Animal Nov 18 '16 at 17:23