Esempio n. 1
0
uint8_t I2cMcuWriteBuffer( I2c_t *obj, uint8_t deviceAddr, uint16_t addr, uint8_t *buffer, uint16_t size )
{
    uint32_t timeOut;

    /* Test on BUSY Flag */
    timeOut = TIMEOUT_MAX;
    while( I2C_GetFlagStatus( obj->I2c, I2C_FLAG_BUSY) ) 
    {
        if( ( timeOut-- ) == 0 )
        {
            I2cResetBus( obj );
            return( FAIL );
        }
    }

    /* Send START condition */
    I2C_GenerateSTART( obj->I2c, ENABLE );

    /* Test on EV5 and clear it */
    timeOut = TIMEOUT_MAX;
    while( !I2C_CheckEvent( obj->I2c, I2C_EVENT_MASTER_MODE_SELECT ) )
    {
        if( ( timeOut-- ) == 0 )
        {
            I2cResetBus( obj );
            return( FAIL );
        }
    }

    /* Send device's address for write */
    I2C_Send7bitAddress( obj->I2c, deviceAddr, I2C_Direction_Transmitter );

    /* Test on EV6 and clear it */
    timeOut = TIMEOUT_MAX;
    while( !I2C_CheckEvent( obj->I2c, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED ) )
    {
        if( ( timeOut-- ) == 0 )
        {
            I2cResetBus( obj );
            return( FAIL );
        }
    }

    if( ( addr & 0xFF00 ) != 0x0000 ) 
    {
        /* Send the device's internal address MSB to write to */
        I2C_SendData( obj->I2c, ( uint8_t )( ( addr & 0xFF00 ) >> 8 )  );

        /* Test on EV8 and clear it */
        timeOut = TIMEOUT_MAX;
        while( !I2C_CheckEvent( obj->I2c, I2C_EVENT_MASTER_BYTE_TRANSMITTING ) )
        {
            if( ( timeOut-- ) == 0 )
            {
                I2cResetBus( obj );
                return( FAIL );
            }
        }
    }
uint8_t MPL3115Init( void )
{
    uint8_t regVal = 0;

    MPL3115SetDeviceAddr( MPL3115A_I2C_ADDRESS );

    if( MPL3115Initialized == false )
    {
        MPL3115Write( CTRL_REG1, RST );
        DelayMs( 50 );
        I2cResetBus( &I2c );

        // Check MPL3115 ID
        MPL3115Read( MPL3115_ID, &regVal );
        if( regVal != 0xC4 )
        {
            return FAIL;
        }

        MPL3115Write( PT_DATA_CFG_REG, DREM | PDEFE | TDEFE );      // Enable data ready flags for pressure and temperature )
        MPL3115Write( CTRL_REG1, ALT | OS_32 | SBYB );              // Set sensor to active state with oversampling ratio 128 (512 ms between samples)
        MPL3115Initialized = true;
    }
    return SUCCESS;
}