Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C programming problem

Status
Not open for further replies.

SimonB

New Member
I am looking through some Atmel ARM code and cannot decipher the following:

I am trying to configure an interrupt and the example code to configure it is as follows:

Code:
void IRQ_ConfigureIT(unsigned int source,
                     unsigned int mode,
                     void( *handler )( void ))
{
    // Disable the interrupt first
    AT91C_BASE_AIC->AIC_IDCR = 1 << source;

    // Configure mode and handler
    AT91C_BASE_AIC->AIC_SMR[source] = mode;
    AT91C_BASE_AIC->AIC_SVR[source] = (unsigned int) handler;

    // Clear interrupt
    AT91C_BASE_AIC->AIC_ICCR = 1 << source;
}

Here you are calling a function with variables 'source', 'mode' and a general pointer 'handler'

All good so far..

Elsewhere in the text, the interrupt service routine is specified:

Code:
void TC0_IrqHandler(void)
{
    volatile unsigned int dummy;
    // Clear status bit to acknowledge interrupt
    dummy = AT91C_BASE_TC0->TC_SR;

    // Toggle LED state
    LED_Toggle(1);
    printf("2 ");
}

The bit I am having trouble with is when IRQ_ConfigureIT is called. The line used is:

Code:
IRQ_ConfigureIT(AT91C_ID_TC0, 0, TC0_IrqHandler);

Here it looks like they are using the function name (TC0_IrqHandler) as a variable in the function call.

Does C allow you to do this, or am I misinterpreting this? I thought function calls can only use variables, not function names.

Any thoughts would be most welcome.

Thanks

Simon
 
It's passing the address of the function. The called function expects a void pointer which is the address of the function to call.

Mike.
 
Hi Pommie

Thanks for this.

So just to get this perfectly straight, the compiler sees the 'variable':

Code:
TC0_IrqHandler

and substitutes the address of the ISR (TC0_IrqHandler) into the function call.

Is this the standard way of calling an interrupt (using a void pointer)? In the PIC world, you specify 'ISR' in the name of the interrupt service routine and the compiler jumps to that routine.

Please let me know

Thanks

Simon
 
The thing that is passed to the function is a pointer, the compiler knows this because the parameters name is preceded by a *. A pointer can hold the address of a variable or, as in this case, the address of a function. The parameter in this case is written void (*FunctionName)(void) which tells the compiler it's a function.

Pointers are a little tricky to get your head around at first. It may be a good idea to read up on them.

On Pics I assume you could handle interrupts in the same way but I can't see a need for it. There is also an added complication with Pics as there are 4 different pointer types.

Edit, I think what the above code is doing is the equivalent of putting goto handler at location 4 on a Pic but, as I don't know the ARM chip, I'm probably wrong.

Mike.
 
Last edited:
Hi Mike

Thanks for your quick reply. If I consider the code from the viewpoint that a pointer can hold a memory address, it seems a lot clearer. This is the first time I have come across a void pointer, but it is really no different from a normal pointer (if you can ever call pointers normal).

Yes I need to read up on pointers. I have Kernighan and Ritchie on order....

Thanks

Simon
(I used to live in Frankston, just outside Melbourne, back in 1972)
 
Status
Not open for further replies.

Latest threads

Back
Top