Example #1
0
//*****************************************************************************
//
//! Performs a read-modify-write of a SHT21 register.
//!
//! \param psInst is a pointer to the SHT21 instance data.
//! \param ui8Reg is the register to modify.
//! \param ui8Mask is the bit mask that is ANDed with the current register
//! value.
//! \param ui8Value is the bit mask that is ORed with the result of the AND
//! operation.
//! \param pfnCallback is the function to be called when the data has been
//! changed (can be \b NULL if a callback is not required).
//! \param pvCallbackData is a pointer that is passed to the callback function.
//!
//! This function changes the value of a register in the SHT21 via a
//! read-modify-write operation, allowing one of the fields to be changed
//! without disturbing the other fields.  The \e ui8Reg register is read, ANDed
//! with \e ui8Mask, ORed with \e ui8Value, and then written back to the SHT21.
//!
//! \return Returns 1 if the read-modify-write was successfully started and 0
//! if it was not.
//
//*****************************************************************************
uint_fast8_t
SHT21ReadModifyWrite(tSHT21 *psInst, uint_fast8_t ui8Reg, uint_fast8_t ui8Mask,
                     uint_fast8_t ui8Value, tSensorCallback *pfnCallback,
                     void *pvCallbackData)
{
    //
    // Return a failure if the SHT21 driver is not idle (in other words, there
    // is already an outstanding request to the SHT21).
    //
    if(psInst->ui8State != SHT21_STATE_IDLE)
    {
        return(0);
    }

    //
    // Save the callback information.
    //
    psInst->pfnCallback = pfnCallback;
    psInst->pvCallbackData = pvCallbackData;

    //
    // Move the state machine to the wait for read-modify-write state.
    //
    psInst->ui8State = SHT21_STATE_RMW;

    //
    // Submit the read-modify-write request to the TMP006.
    //
    if(I2CMReadModifyWrite8(&(psInst->uCommand.sReadModifyWriteState),
                            psInst->psI2CInst, psInst->ui8Addr, ui8Reg,
                            ui8Mask, ui8Value, SHT21Callback, psInst) == 0)
    {
        //
        // The I2C read-modify-write failed, so move to the idle state and
        // return a failure.
        //
        psInst->ui8State = SHT21_STATE_IDLE;
        return(0);
    }

    //
    // Success.
    //
    return(1);
}
Example #2
0
//*****************************************************************************
//
//! Performs a read-modify-write of a TMP100 register.
//!
//! \param psInst is a pointer to the TMP100 instance data.
//! \param ui8Reg is the register offset to read modify and write
//! \param ui16Mask is the bit mask that is ANDed with the current register
//! value.
//! \param ui16Value is the bit mask that is ORed with the result of the AND
//! operation.
//! \param pfnCallback is the function to be called when the data has been
//! changed (can be \b NULL if a callback is not required).
//! \param pvCallbackData is a pointer that is passed to the callback function.
//!
//! This function changes the value of a register in the TMP100 via a
//! read-modify-write operation, allowing one of the fields to be changed
//! without disturbing the other fields.  The \e ui8Reg register is read, ANDed
//! with \e ui16Mask, ORed with \e ui16Value, and then written back to the
//! TMP100.
//!
//! \return Returns 1 if the read-modify-write was successfully started and 0
//! if it was not.
//
//*****************************************************************************
uint_fast8_t
TMP100ReadModifyWrite(tTMP100 *psInst, uint_fast8_t ui8Reg,
                      uint_fast16_t ui16Mask, uint_fast16_t ui16Value,
                      tSensorCallback *pfnCallback, void *pvCallbackData)
{
    //
    // Return a failure if the TMP100 driver is not idle (in other words, there
    // is already an outstanding request to the TMP100).
    //
    if(psInst->ui8State != TMP100_STATE_IDLE)
    {
        return(0);
    }

    //
    // Save the callback information.
    //
    psInst->pfnCallback = pfnCallback;
    psInst->pvCallbackData = pvCallbackData;

    //
    // Move the state machine to the wait for read-modify-write state.
    //
    psInst->ui8State = TMP100_STATE_RMW;

    //
    // Submit the read-modify-write request to the TMP100.
    //
    if(ui8Reg == TMP100_O_CONFIG)
    {
        //
        // The configuration register is only one byte, so only a single byte
        // read-modify-write is necessary and no endian swapping is required.
        //
        if(I2CMReadModifyWrite8(&(psInst->uCommand.sReadModifyWriteState8),
                                psInst->psI2CInst, psInst->ui8Addr, ui8Reg,
                                ui16Mask & 0xff, ui16Value & 0xff,
                                TMP100Callback, psInst) == 0)
        {
            //
            // The I2C read-modify-write failed, so move to the idle state and
            // return a failure.
            //
            psInst->ui8State = TMP100_STATE_IDLE;
            return(0);
        }
    }
    else
    {
        //
        // This is one of the temperature registers, which are 16-bit
        // big-endian registers.
        //
        if(I2CMReadModifyWrite16BE(&(psInst->uCommand.sReadModifyWriteState16),
                                   psInst->psI2CInst, psInst->ui8Addr, ui8Reg,
                                   ui16Mask, ui16Value, TMP100Callback,
                                   psInst) == 0)
        {
            //
            // The I2C read-modify-write failed, so move to the idle state and
            // return a failure.
            //
            psInst->ui8State = TMP100_STATE_IDLE;
            return(0);
        }
    }

    //
    // Success.
    //
    return(1);
}