Unit I2CGPIO

From Ultibo.org
Revision as of 03:22, 27 May 2022 by Ultibo (Talk | contribs)

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


To be documented


Return to Unit Reference