Unit I2CGPIO

From Ultibo.org
Jump to: navigation, search

Return to Unit Reference


Description


GPIO based software I2C Driver unit

This is a software based (bit bang) I2C driver which can implement an I2C interface using any available pair of GPIO pins without the need for a hardware controller.

This driver supports clock stretching and 10bit addresses and can operate with either the default GPIO device or any other suitable GPIO device.

Creating multiple instances on different sets of GPIO pins is fully supported, all instances will appear in the standard Ultibo device tables.

Being a software based driver the clock speed is mostly meaningless, instead a delay parameter can be passed during device creation which determines the interval between high and low states and the length of start and stop conditions. The default delay value equates to approximately a 100KHz clock rate.

To use this driver in place of the default hardware I2C controller on the Raspberry Pi (I2C0 or BSC1) which is on GPIO pins 2 and 3 you simply initialize an instance of the I2CGPIO device by calling the I2CGPIOCreate function like this:

uses
 I2C,
 GPIO,
 I2CGPIO;

var
 I2CDevice:PI2CDevice;
 GPIODevice:PGPIODevice;
 
begin
 // Get the default GPIO device
 GPIODevice:=GPIODeviceGetDefault;
 
 // Create an instance of the I2CGPIO device
 I2CDevice:=I2CGPIOCreate(GPIODevice,GPIO_PIN_2,GPIO_PIN_3,4,0,False,False);

 // Pass the returned I2CDevice to the standard I2C API functions
 ...
end;

The returned I2CDevice instance can be passed to any of the standard I2C API functions to perform I2C reads and writes. As noted above, any available pair of GPIO pins can be used to create a new I2CGPIO device.

If an instance of the I2CGPIO device is no longer required it can be released by calling the I2CGPIODestroy function and passing the I2CDevice instance returned from I2CGPIOCreate.

Handling of SDA/SCL

If OpenDrain is set to True in I2CGPIOCreate then the SDA/SCL pins will be set to Output and toggled High or Low as required, the driver assumes that the hardware provides the high impedance state required for the I2C protocol.

If OpenDrain is set to False in I2CGPIOCreate then the SDA/SCL pins will be set to Input when the state is High and Output with a value or Low when the state is Low. This will provide the high impedance (floating) state required for the I2C protocol.

If OutputOnly is set to True in I2CGPIOCreate then reading of the SCL value is not used and clock stretching is not supported.

Constants



I2CGPIO specific constants I2CGPIO_*
I2CGPIO_I2C_DESCRIPTION = 'GPIO Software I2C'; Description of I2CGPIO I2C device
I2CGPIO_I2C_MAX_SIZE = $FFFF;  
I2CGPIO_I2C_MIN_CLOCK = 10000; Arbitrary minimum of 10KHz, actual rate is determined by Delay parameter.
I2CGPIO_I2C_MAX_CLOCK = 100000; Arbitrary maximum of 100KHz, actual rate is determined by Delay parameter.
I2CGPIO_RETRY_COUNT = 3;  
I2CGPIO_DEFAULT_TIMEOUT = 100;  


Type definitions



I2CGPIO device

PI2CGPIODevice = ^TI2CGPIODevice;

TI2CGPIODevice = record

I2C Properties
I2C:TI2CDevice;  
I2CGPIO Properties
GPIO:PGPIODevice; The GPIO device this device is connected to
SDA:LongWord; GPIO pin for the SDA line
SCL:LongWord; GPIO pin for the SCL line
Delay:LongWord; Clock and Data delay in microseconds
Timeout:LongWord; Clock timeout in milliseconds
OutputOnly:LongBool; Clock line is output only, no test for SCL high.
OpenDrain:LongBool; Clock and Data are open drain, no need to simulate by switching direction.


Public variables


None defined

Function declarations



I2CGPIO functions

function I2CGPIOCreate(GPIO:PGPIODevice; SDA,SCL,Delay,Timeout:LongWord; OutputOnly,OpenDrain:Boolean):PI2CDevice;
Description: Create and register a new I2CGPIO I2C device connected to the specified GPIO device
Note None documented


function I2CGPIODestroy(I2C:PI2CDevice):LongWord;
Description: To be documented
Note None documented


I2CGPIO I2C functions

function I2CGPIOStart(I2C:PI2CDevice; Rate:LongWord):LongWord;
Description: Implementation of I2CDeviceStart API for I2CGPIO device
Note Not intended to be called directly by applications, use I2CDeviceStart instead.


function I2CGPIOStop(I2C:PI2CDevice):LongWord;
Description: Implementation of I2CDeviceStop API for I2CGPIO device
Note Not intended to be called directly by applications, use I2CDeviceStop instead.


function I2CGPIORead(I2C:PI2CDevice; Address:Word; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Implementation of I2CDeviceRead API for I2CGPIO device
Note Not intended to be called directly by applications, use I2CDeviceRead instead.


function I2CGPIOWrite(I2C:PI2CDevice; Address:Word; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Implementation of I2CDeviceWrite API for I2CGPIO device
Note Not intended to be called directly by applications, use I2CDeviceWrite instead.


function I2CGPIOWriteRead(I2C:PI2CDevice; Address:Word; Initial:Pointer; Len:LongWord; Data:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Implementation of I2CDeviceWriteRead API for I2CGPIO device
Note Not intended to be called directly by applications, use I2CDeviceWriteRead instead.


function I2CGPIOWriteWrite(I2C:PI2CDevice; Address:Word; Initial:Pointer; Len:LongWord; Data:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Implementation of I2CDeviceWriteWrite API for I2CGPIO device
Note Not intended to be called directly by applications, use I2CDeviceWriteWrite instead.


Return to Unit Reference