This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Sensorlib i2c_drv C API's ?

Hi,

There are several C API's at sensor library i2cm_drv.h and i2cm_drv.c that are declared as below.

I need a little help clarifying why the I2CMRead C API at i2cm_drv.c is an empty function?

Also, why the inline function I2CMRead is at the header file? From what I know, the inline function should be at C file.

i2cm_drv.c

// The extern here provides a non-inline definition for this function to handle
// the case where the compiler chooses not to inline the function (which is a
// valid choice for the compiler to make).
//
//*****************************************************************************
extern uint_fast8_t I2CMRead(tI2CMInstance *psInst, uint_fast8_t ui8Addr,
                             const uint8_t *pui8WriteData,
                             uint_fast16_t ui16WriteCount,
                             uint8_t *pui8ReadData,
                             uint_fast16_t ui16ReadCount,
                             tSensorCallback pfnCallback,
                             void *pvCallbackData);

i2cm_drv.h

//*****************************************************************************
//
// A convenience wrapper around I2CMCommand to perform a read.
//
//*****************************************************************************
inline uint_fast8_t
I2CMRead(tI2CMInstance *psInst, uint_fast8_t ui8Addr,
         const uint8_t *pui8WriteData, uint_fast16_t ui16WriteCount,
         uint8_t *pui8ReadData, uint_fast16_t ui16ReadCount,
         tSensorCallback *pfnCallback, void *pvCallbackData)
{
    return(I2CMCommand(psInst, ui8Addr, pui8WriteData, ui16WriteCount,
                       ui16WriteCount, pui8ReadData, ui16ReadCount,
                       ui16ReadCount, pfnCallback, pvCallbackData));
}

- kel

  • Hello Kel

    Not necessary. Inline function can be a part of the header file.

    Regards

    Amit

  • Amit,

    Okay, so the inline function can be at header file.

    From my current knowledge and searching at google, this seems not conventional. The usual is that C Function prototype should be at header file and it's C Function is at C file. But, as long as it works and compiles without errors, that is okay with me.

    // The extern here provides a non-inline definition for this function to handle
    // the case where the compiler chooses not to inline the function (which is a
    // valid choice for the compiler to make).
    //
    //*****************************************************************************

    This comment is new to me. I thought that the user sets whether the compiler inline a function, by putting the "inline" c keyword at the function name. I did not know that the compiler has a mind of its own and decide whether to make a function inline or not.

    - kel

  • The C standard says that inline is a suggestion from the user to the compiler.  It is valid for the compiler to choose to not inline a function.  I believe the compiler can also inline sometimes and not others and still be standard compliant.

     

    I won't say I 100% understand this either but I know the guy who did this bit of code knew what he was doing and I trust his judgment and knowledge.