Difference between revisions of "Unit I2CGPIO"

From Ultibo.org
Jump to: navigation, search
(Created page with "Return to Unit Reference === Description === ---- ''To be documented'' === Constants === ---- ''To be documented'' === Type definitions === ---- ''To...")
 
Line 5: Line 5:
 
----
 
----
  
''To be documented''
+
'''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 with 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 ===
 
=== Constants ===

Revision as of 02:53, 27 May 2022

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 with 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


To be documented

Type definitions


To be documented

Public variables


To be documented

Function declarations


To be documented


Return to Unit Reference