d. Bridging I2C

<< Click to Display Table of Contents >>

Navigation:  Tutorials > Tutorial 20 - CleO Bridging >

d. Bridging I2C

This tutorial shows how to use bridging commands to access I2C device interface on CleO35.

 

 

Command Prototype

 

int16_t DeviceOpen(uint8_t Slot,  uint8_t Interface,  int16_t bytestowrite,  uint8_t* Buffer)

 

Parameters

Description

Slot

Currently only one Slot is supported. Slot value should be equal to 0.

Interface

Value of this parameter should be BRIDGE_TYPE_I2CM for I2C interface access. Only master configuration is supported.

bytestowrite

Value of this parameter is the total number of bytes to be written through this function which in this case is sizeof i2cm_options structure.

Buffer

Value of this parameter should be a pointer to i2cm_options structure type in Bridge.h.

 

typedef  struct PACK {

 union PACK {

  struct PACK {

   uint16_t SlaveAddress; / Verify if SPIM actually supports 10 bit address ! /

   uint32_t ClockSpeed;

  };

  uint8_t b[6];

 };

}i2cm_options;

 

Structure member description:

 

SlaveAddress: This value should be 7 bit slave address.

 

ClockSpeed: The supported ClockSpeed values are 100 kbps, 400 kbps, 1000 kbps and 3400 kbps.

 

 

Return Value

Description

int16_t

On success, the function returns a valid handle that can be used in subsequent Device*() calls.

On failure, the function returns a negative value of error code with blue screen.

 

 

int16_t DeviceRead(uint8_t DevHandle,  int16_t bytestowrite,  int16_t bytestoread,  uint8_t* Buffer,  uint8_t* rBuffer)

 

Parameters

Description

DevHandle

A I2C handle number that is obtained using DeviceOpen() call.

bytestowrite

Value of this parameter is the total number of bytes to be written to I2C

bytestoread

Value of this parameter is the total number of bytes intend to read from I2C

Buffer

Value of this parameter should be a pointer to type uint8_t.

This buffer holds the data to be written to I2C. "bytestowrite" parameter refers to number of bytes in this Buffer. When "bytestowrite" is 0, this parameter should be NULL..

rBuffer

Value of this parameter should be read buffer pointer to which read result will be updated when the function returns.

 

Return Value

Description

int16_t

On success the function returns the number of bytes read from I2C. On failure the function returns a negative value of error code with blue screen. Return 0 if line is busy.

 

 

int16_t DeviceWrite(uint8_t DevHandle,  int16_t bytestowrite,  uint8_t* Buffer,  int16_t &byteswritten)

 

Parameters

Description

DevHandle

An I2C handle that is obtained using DeviceOpen() call.

bytestowrite

Value of this parameter is the total number of bytes to be written.

Buffer

Value of this parameter should be a pointer to type uint8_t

byteswritten

This variable will be updated with the total number of bytes written to CleO when the function returns.

 

Return Value

Description

int16_t

On success, the function returns 1 and on failure the function returns a negative value of error code with blue screen.

 

 

int16_t DeviceClose( uint8_t DevHandle)

 

Parameters

Description

DevHandle

An I2C handle that is obtained using DeviceOpen() call.

 

Return Value

Description

int16_t

On success, the function returns 1 and on failure the function returns a negative value of error code with blue screen.

 

 

Code

 

 

int16_t HandleI2C = -1;

 

void setup()

{      

 Bridge::i2cm_options i2c_open_opt;

 byte Reg = 0x07; /* Register address*/

 byte Data;

 byte Len = 1;

 CleO.begin();

 Serial.begin(115200);

 

 i2c_open_opt.SlaveAddress = 0x57;

 i2c_open_opt.ClockSpeed = 400000;

/* Open I2C device using bridging commands */

 HandleI2C = CleO.DeviceOpen(0, BRIDGE_TYPE_I2CM, sizeof(i2c_open_opt), (uint8_t*)&i2c_open_opt);  

 if(HandleI2C < 0)

 {

  /* Failed to open a handle */

   Serial.println("DeviceOpen failed");

 }

 else

 {          

 /* Read I2C device using bridging commands.

    * DeviceRead returs number of bytes read.

    */

   int16_t RetCode = CleO.DeviceRead(HandleI2C, 1, Len, &Reg, &Data);

   Serial.print("Read I2C : ");Serial.print(RetCode,DEC);Serial.println(" Bytes");

 

  /* Close I2C device using bridging commands.    

    */

   HandleI2C = CleO.DeviceClose(HandleI2C);

   if(HandleI2C < 0)

       Serial.println("DeviceClose failed");

 }

}

 

void loop()

{      

}

 

 

 

 

Description

 

The tutorial opens a handle to I2C interface using DeviceOpen(). The DeviceRead() is used to read the value of I2C slave register 0x07. DeviceClose() is used to close the handle.

 

 

Output

 

Upon executing the tutorial, depending on the success or failure, an appropriate message is displayed on the serial monitor window. For illustration purpose, a failure message window is shown here in the picture.