/** Performs the data transaction described in @a p_bus_request.
 */
void I2CBusRequestProcessorEndpoint::ExecuteBusRequest( BusRequest *p_bus_request )
{
  int request_type;
  uint8_t addr;
  uint8_t register_addr;

  if ( p_bus_request != NULL )
  {
    if ( p_bus_request->IsLockable() )
    {
      p_bus_request->Lock();
    }

    // Sanity check
    if ( 2 == p_bus_request->GetAddressBufferSize() ) 
    {
      request_type = p_bus_request->GetRequestType();
      addr = p_bus_request->GetAddressBuffer()[0];
      register_addr = p_bus_request->GetAddressBuffer()[1];

      if ( REQUEST_READ == request_type )
      {
        I2cSetSlaveAddress( _i2c_fd, addr, 0 );

        I2cReadBytes( _i2c_fd, register_addr,
                      p_bus_request->GetDataBuffer(),
                      p_bus_request->GetDataBufferSize() );
      }
      else if ( REQUEST_WRITE == request_type )
      {
        I2cSetSlaveAddress( _i2c_fd, addr, 0 );

        I2cWriteBytes(  _i2c_fd, register_addr,
                        p_bus_request->GetDataBuffer(),
                        p_bus_request->GetDataBufferSize() );
      }

      p_bus_request->SetRequestComplete( true );
    }
  
    // Done altering the request
    if ( p_bus_request->IsBlocked() )
    {
      p_bus_request->Unblock();
    }

    if ( p_bus_request->IsLocked() )
    {
      p_bus_request->Unlock();
    }
  }
}
Ejemplo n.º 2
0
Archivo: i2c.c Proyecto: ivanovedgar/MP
void ProcessWriteByteCommand( int i2cDev, uint8_t addr, const char *dataStr )
{
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    uint8_t bytesParsed;
    int     rc;

    if (( rc = I2cWriteBytes( i2cDev, addr, dataByte, bytesParsed )) != 0 )
    {
        fprintf(stderr, "I2cWriteBytes failed: %d\n", rc );
        return;
    }

} // ProcessWriteByteCommand
Ejemplo n.º 3
0
/**
  * @brief  This function send multiple bytes to I2C slave with address
  * @param  Buf: Buffer pointer to the data to be send
  * @param  Len: Data Length to be send in byte uint
  * @return
  *     @arg TRUE: success
  *     @arg FALSE:fail
  */
bool I2cWriteNByte(void* I2cMasterHandle, uint8_t SlaveAddr, uint8_t RegAddr, uint8_t* Buf, uint8_t Len)
{
	if(I2cSendAddr(I2cMasterHandle, SlaveAddr, RegAddr, IIC_WRITE))
	{
		if(I2cWriteBytes(I2cMasterHandle, Buf, Len))
		{
			I2cStop(I2cMasterHandle);
			return TRUE;
		}
	}
	I2cStop(I2cMasterHandle);
	return FALSE;
}