I2C SPI USB CAN eSPI Cable Testing View All Videos Quick Start Guides Software Downloads App Notes White Papers User Manuals Knowledge Base Sales Support About Us
Products Blog Sales Support Contact Search
How Can I Update My API Script to Work with Multi-Byte Address Registers and Data?
Rena Ayeras

Question from the Customer:

I am using the Aardvark I2C/SPI Host Adapter to read/write to an I2C slave device that has registers with multi-byte addresses. Most of the register addresses are 2 bytes, and some are 3 bytes. The data is also multi-byte. I am trying to create a script in C with the Aardvark Software API.

That should be possible, as the Control Center Serial Software allows me to specify how many bytes to read or write, as well as the size of the register address in bytes. I just need to know how to use the Aardvark Software API to do this.

I see the aa_i2c_read() and aa_i2c_write() functions for specifying an I2C device ID, a register to read from, and the data to return. So far, I figured out how to use them for 1-byte read and write addresses and data, but I have yet to get them to work for multi-byte address and data.

Is there an example of C code that supports multi-byte address and data?

Response from Technical Support:

Thank you for your question! We do not have an example available, but we can create a basic script for you to expand for your use. We will provide an overview of what the Aardvark Software API provides, and then show you some guidelines for handling multi-byte transactions.

How the Aardvark API Addresses Data

The Aardvark Software API functions control the Aardvark I2C/SPI Host Adapter, and can be used to write a custom program for your specific requirements. The API supports multiple OS (Windows, Linux, and Mac) and multiple languages (C, Python, Visual Basic, and C#), and includes examples that can be used as is or modified as need.

The example scripts are available in C, Visual Basic, Python, and Aardvark XML batch script code. README.txt files are included in the package to explain each of the examples. Note – the Aardvark API examples work specifically with the Atmel SPI EEPROM AT25080A, which is included in the I2C/SPI Activity Board.

Here is a summary of the Aardvark API examples:

  • aadetect: Detect Aardvark devices attached to the system.
  • aalights: Flash LEDs attached to a Philips PCA9554D I/O port expander as found on the Activity Board.
  • aai2c_eeprom: Read from or write to an I2C serial EEPROM, such as the Atmel AT24C02 on the Activity Board.
  • aaspi_eeprom: Read from or write to an SPI serial EEPROM, such as the Atmel AT25080A found on the I2C/SPI Activity Board.
  • aai2c_file, aai2c_slave: Demonstrate the I2C slave functionality of the Aardvark device. This example requires two Aardvark devices. First run aai2c_slave with the first Aardvark device to wait for a new slave transmission. Then, in another shell, run aai2c_file to transmit a binary file with the second Aardvark device.
  • aaspi_file, aaspi_slave: Demonstrate the SPI slave functionality of the Aardvark device. This example requires two Aardvark devices. First run aaspi_slave with the first Aardvark device to wait for a new slave transmission. Then, in another shell, run aaspi_file to transmit a binary file with the second Aardvark device.
  • aagpio: Perform some simple GPIO tests with a single Aardvark adapter. The results can be verified using an oscilloscope or multimeter.

Recommended Starting Point for Multi-Byte Address and Data Functions

Currently, we do not have multi-byte access examples. However, the example script aai2c_eeprom could be a good starting point for your use. You should be able to use the API to manually pack the register address into the data packet, using C bit shift operators as needed. The datasheet for your I2C component probably specifies the exact bit layout.

Here is a simple script in which the I2C device has a 16-bit address. The aai2c_eeprom example, which was coded for an 8-bit address, was modified for a 16-bit address. To send a 16-bit address, here is how you would use the _writeMemory function:

  • Implement an 8-bit shift
  • Assign the 16-bit address as two 8-bit addresses

:::::::

Data_out[0]=(addr >> 8) & 0xff;

Data_out[1]=(addr >> 0) & 0xff;

Data_out[2]=data1;  //Actual

:::::::

Here is a snippet of that part of the program:

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.