SparkFun Forums 

Where electronics enthusiasts find answers.

Everything ARM and LPC
By fll-freak
#151695
This is the definition of a pointer to a function. Often used as a callback or table based call.

getTicks is the name of the function. (*getTicks)
It takes no arguments (*getTicks)(void)
It returns an unsigned 32 bit integer unit32_t (*getTicks)(void)
It is static to this function (no global scope) static uint32_t (*getTicks)(void)
And as a pointer it is initialized to NULL such that a caller can tell if the function is valid. Most likely called like:
if (getTicks != NULL) {
val = getTicks();
}
By lpc1769
#151726
hi fll-freak,

thanks a lot for the reply. I have below temperature sensor header file for my LPC EAboard.

Lets say if i want to initialse "void temp_init (uint32_t (*getMsTicks)(void))" function, what parameter in need to pass to the function. Thank you.
Code: Select all
/*****************************************************************************
 *   temp.c:  Driver for the Temp Sensor
 *
 *   Copyright(C) 2009, Embedded Artists AB
 *   All rights reserved.
 *
 ******************************************************************************/

/*
 * NOTE: GPIOInit must have been called before using any functions in this
 * file.
 */

/******************************************************************************
 * Includes
 *****************************************************************************/

#include "lpc17xx_gpio.h"
#include "temp.h"

/******************************************************************************
 * Defines and typedefs
 *****************************************************************************/

/*
 * Time-Select Pin Configuration. Selected by Jumper J26 on the base board
 */
#define TEMP_TS1 0
#define TEMP_TS0 0

/*
 * Pin 0.2 or pin 1.5 can be used as input source for the temp sensor
 * Selected by jumper J25.
 */
//#define TEMP_USE_P0_2

#if TEMP_TS1 == 0 && TEMP_TS0 == 0
#define TEMP_SCALAR_DIV10 1
#define NUM_HALF_PERIODS 340
#elif TEMP_TS1 == 0 && TEMP_TS0 == 1
#define TEMP_SCALAR_DIV10 4
#define NUM_HALF_PERIODS 100
#elif TEMP_TS1 == 1 && TEMP_TS0 == 0
#define TEMP_SCALAR_DIV10 16
#define NUM_HALF_PERIODS 32
#elif TEMP_TS1 == 1 && TEMP_TS0 == 1
#define TEMP_SCALAR_DIV10 64
#define NUM_HALF_PERIODS 10
#endif


#define P0_6_STATE ((GPIO_ReadValue(0) & (1 << 6)) != 0)
#define P0_2_STATE ((GPIO_ReadValue(0) & (1 << 2)) != 0)


#ifdef TEMP_USE_P0_6
#define    GET_TEMP_STATE P0_6_STATE
#else
#define    GET_TEMP_STATE P0_2_STATE
#endif


/******************************************************************************
 * External global variables
 *****************************************************************************/

/******************************************************************************
 * Local variables
 *****************************************************************************/

static uint32_t (*getTicks)(void) = NULL;

/******************************************************************************
 * Local Functions
 *****************************************************************************/

/******************************************************************************
 * Public Functions
 *****************************************************************************/

/******************************************************************************
 *
 * Description:
 *    Initialize Temp Sensor driver
 *
 * Params:
 *   [in] getMsTicks - callback function for retrieving number of elapsed ticks
 *                     in milliseconds
 *
 *****************************************************************************/
void temp_init (uint32_t (*getMsTicks)(void))
{
#ifdef TEMP_USE_P0_6
    GPIO_SetDir( 0, (1<<6), 0 );
#else
    GPIO_SetDir( 0, (1<<2), 0 );
#endif
    getTicks = getMsTicks;
}

/******************************************************************************
 *
 * Description:
 *    Read temperature
 *
 * Returns:
 *    10 x T(c), i.e. 10 times the temperature in Celcius. Example:
 *    if the temperature is 22.4 degrees the returned value is 224.
 *
 *****************************************************************************/
int32_t temp_read (void)
{
    uint8_t state = 0;
    uint32_t t1 = 0;
    uint32_t t2 = 0;
    int i = 0;

    /*
     * T(C) = ( period (us) / scalar ) - 273.15 K
     *
     * 10T(C) = (period (us) / scalar_div10) - 2731 K
     */

    state = GET_TEMP_STATE;

    /* get next state change before measuring time */
    while(GET_TEMP_STATE == state);
    state = !state;

    t1 = getTicks();

    for (i = 0; i < NUM_HALF_PERIODS; i++) {
        while(GET_TEMP_STATE == state);
        state = !state;
    }

    t2 = getTicks();
    if (t2 > t1) {
        t2 = t2-t1;
    }
    else {
        t2 = (0xFFFFFFFF - t1 + 1) + t2;
    }


    return ( (2*1000*t2) / (NUM_HALF_PERIODS*TEMP_SCALAR_DIV10) - 2731 );
}
By fll-freak
#151747
Although the code you posted was for an assignment of null to a function pointer, later in the file the function is fully defined. I am a bit surprised the compiler tolerates this, but given that it does, you need no do anything special to use the getMsTicks function.

Edit: Above is a bad answer. See later post.
Last edited by fll-freak on Wed Nov 14, 2012 6:28 am, edited 1 time in total.
By fll-freak
#151751
My previous comment was in error. Too sleepy in the morning to properly read.

This will be a two step operation. First you must call the temp_init routine to initialize various things. When you call that function, you must pass in the address of a function that returns the number of ms since some arbitrary time. You will have to either write that function yourself, or find something appropriate in your OS or board support package.

After this step it should be simply a matter of calling temp_read.

Be advise that although I have had a cup of coffee, I have no direct experience with this setup.