Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 27th October 2009, 09:06 AM   #1
Default C programming problem

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
SimonB is offline  
Old 27th October 2009, 09:11 AM   #2
Default

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.
Pommie is online now  
Old 27th October 2009, 09:37 AM   #3
Default

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
SimonB is offline  
Old 27th October 2009, 10:20 AM   #4
Default

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 by Pommie; 27th October 2009 at 10:25 AM.
Pommie is online now  
Old 27th October 2009, 10:38 AM   #5
Default

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)
SimonB is offline  
Reply

Tags
problem, programming

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Programming problem ahmedragia21 Micro Controllers 14 18th May 2009 12:21 AM
c programming problem kapcai Micro Controllers 5 17th March 2009 04:12 PM
programming problem surangauop@gawab.com Micro Controllers 4 10th February 2009 08:11 PM
Programming Problem DMW Micro Controllers 40 7th December 2007 03:18 AM
programming problem UnD3aD Micro Controllers 6 6th May 2005 01:07 PM



All times are GMT. The time now is 05:57 AM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker