How Do I Claim the I2C Bus with the Aardvark I2C/SPI Host Adapter?

Question from the Customer:

I am working with an I2C device that will be used for lengthy write and read transactions. There are multiple masters on this bus, any of which could send and start a command, thus taking ownership of the bus.

I am looking at using the Aardvark I2C/SPI Host Adapter for this project.  How would I “claim the bus” to prevent stops from occurring between write and read transactions? Which applications could work for me?

Response from Technical Support:

Thanks for your question! We have two software applications that could serve your purpose: the Control Center Serial Software and Aardvark Software API. Both applications can be used to run commands with the “no-stop” feature.

Run No-Stop with GUI or Batch Commands

The Control Center Serial Software is an easy-to-use application that provides access to all the features of the Aardvark adapter. You have the option to manually enter commands or run commands automatically by creating an XML batch script.

Manually Enter Commands

With the GUI dialogs, you can easily set commands. To set the no-stop feature, all you need to do is check the No Stop option as shown below.

Set no-stop for Aardvark I2C messages using Control Center Serial Software

For a complete example of executing the stop command, take a look at this article - In Batch Mode, How Do I Control Stop Bits when Sending Data to an I2C Device?  It also provides an example of using batch scripts.

Batch Scripts

You can create batch scripts in XML, which can be saved and run when needed. A built-in help system describes each command via GUI, as well as functional examples of batch scripts that can be used as-is or modified as needed for your specifications.

For more information about Batch Scripts, please refer to Control Center Serial Software Series: XML Batch Scripting for Automated Tasks.

API NO_STOP Flag

The Aardvark Software API library includes a command that combines write and read functions with the no-stop condition: aa_i2c_write_read.

This Master Write-Read command (also known as Master Register Read) is a combination of two I2C transactions. Using the AA_I2C_NO_STOP allows these two calls to operate as a single I2C transaction.

  • The first API call is aa_i2c_write(). The AA_I2C_NO_STOP flag is set, as well as the device register address from which to read.
  • The second call is aa_i2c_read(), which specifies the number of bytes you wish to read.

Using the stop flag results in a single I2C transaction:

[Start] [Device Addr][W] [Register Addr] [Start] [Device Addr][R] [data] ... [data] [Stop]

If the AA_I2C_NO_STOP was not used, there were would two separate I2C transactions:

[Start] [Device Addr][W] [Register Addr] [Stop][Start] [Device Addr][R] [data] ... [data] [Stop]

Single API Command for Non-Stop Write Read

Using the API command  aa_i2c_write_read() would be the easiest way to execute Master Register Read. This function writes a stream of bytes to the I2C slave device, which is followed by a read from the same slave device.

This is a combination of aa_i2c_write() and aa_i2c_read(), except that these functions are performed as a single operation. The AA_I2C_NO_STOP condition is taken care internally by the state machines; it does not need to be invoked.

The syntax:

int aa_i2c_write_read (Aardvark           aardvark,
aa_u16             slave_addr,
AardvarkI2cFlags   flags,
aa_u16             out_num_bytes,
const aa_u08 *     out_data,
aa_u16 *           num_written,
aa_u16             in_num_bytes,
aa_u08 *           in_data,
aa_u16 *           num_read);

Here is an example of values to apply. In this case, the command is to read 5 bytes from the register address 0x11:

  • flags - You may use AA_I2C_NO_FLAGS since you do not have special requirements listed under the "notes" section. This API is a combination of aa_i2c_write() and aa_i2c_read(), except that these functions are performed in one atomic operation. The AA_I2C_NO_STOP condition is taken care internally by the state machines, and hence need not be invoked in your case.
  • out_num_bytes – This is the address width of the register address. In this case, because it is 1 byte, the parameter must be set as 1).
  • out_data - The register address, which in this example is 0x11.
  • num_written - This is updated by the APIs about the total bytes of register address actually sent (this parameter is used to verify if the register address was sent properly on the bus)
  • in_num_bytes - Number of bytes to be read (5 in this case)
  • in_data pointer - The data read from the register location is stored in the pointer specified location.
  • num_read - The total number of bytes actually read from the register address is updated by the API. This parameter is used to verify if the function was successful in reading the desired number of bytes.

For more information, please refer the API Documentation section of the Aardvark I2C/SPI Host Adapter User Manual.

We hope this answers your questions. Additional resources that you may find helpful include the following:

If you want more information, feel free to contact us with your questions, or request a demo that applies to your application.