Jump to content

Khepera III Toolbox/The Toolbox/Modules/i2c stream

From Wikibooks, open books for an open world

The i2c_stream module implements a simple I2C data streaming protocol. The device from which data is streamed must implement that same protocol as well.

Synopsis

[edit | edit source]

Check out the source code of the i2c_stream_read program for an example.

Description

[edit | edit source]

This module uses the i2cal module to transfer data blocks from the external device (chip) to the program. It basically consists of two functions:

  • i2c_stream_read_start, to start streaming
  • i2c_stream_read_next, which has to be called frequently (and in more or less regular intervals) to read the available data blocks

The following four settings need to be provided to the module

  • device: the I2C address of the device to query
  • reg: the I2C register from which the stream is read
  • block_size: the size of one block (in bytes)
  • block_count_max: the maximum number of blocks to transfer at once (this value must be <= 128 / block_size)

Streaming Protocol

[edit | edit source]

The protocol used for data streaming is very simple: To reset the stream, the module first sends the register followed by a 1. To read a chunk, the module sends the register and then reads a multiple of (block_size + 1) bytes from the device. The first byte of a block is a status byte which can take one of the following values:

  • 255 (I2C_STREAM_BUFFER_OVERRUN): A buffer overrun occurred on the device, i.e. the blocks were not read fast enough.
  • 254 (I2C_STREAM_EMPTY): The block does not contain any data.
  • 0..253: the available number of blocks on the device