Unit I2C

From Ultibo.org
Jump to: navigation, search

Return to Unit Reference


Description


Ultibo I2C Interface unit

I2C (Inter-Integrated Circuit) is a serial bus for communication between peripheral components.

Originally invented by Phillips the I2C protocol is used by thousands of common chips that perform a wide range of tasks such as real time clocks, temperature and other sensors, small LCD displays and many more.

Each device is assigned a 7bit address which is used by the host (or master) to signal the device that a message written to the bus is intended for that device or that the host wants to read data from that device.

A small number of devices and hosts support 10bit addressing which expands the number of available addresses. The 10bit address format is defined here https://www.i2c-bus.org/addressing/10-bit-addressing/ and the reserved addresses are defined on this page https://www.i2c-bus.org/addressing/

Speeds range from 10Kbps to 3.4Mbps although the typical speed is either 100Kbps or 400Kbps.

This unit implements the standardized interface for I2C devices and allows reading or writing to a specific address, setting a clock rate for the communication and determining device properties.

For the purpose of this interface a device is the I2C controller attached to the local system and may be either a master or a slave. Since the protocol does not include any form of enumeration or identification the interface does not attempt to represent the devices connected to the bus,any driver written to communicate with a connected I2C device should know (or allow configuration of) the address to read and write to and the specific message format required for that device. The Intel System Management Bus (SMBus) is a variation of the I2C bus and in certain cases the two are compatible with each other.

Constants



[Expand]
I2C specific constants I2C_*


[Expand]
I2C device type I2C_TYPE_*


[Expand]
I2C device state I2C_STATE_*


[Expand]
I2C device flag I2C_FLAG_*


[Expand]
I2C logging I2C_LOG_*


Type definitions



I2C properties

[Expand]

PI2CProperties = ^TI2CProperties;

TI2CProperties = record

I2C enumeration callback

TI2CEnumerate = function(I2C:PI2CDevice; Data:Pointer):LongWord;

I2C notification callback

TI2CNotification = function(Device:PDevice; Data:Pointer; Notification:LongWord):LongWord;

I2C device start

TI2CDeviceStart = function(I2C:PI2CDevice;Rate:LongWord):LongWord;

I2C device stop

TI2CDeviceStop = function(I2C:PI2CDevice):LongWord;

I2C device read

TI2CDeviceRead = function(I2C:PI2CDevice; Address:Word; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;

I2C device write

TI2CDeviceWrite = function(I2C:PI2CDevice; Address:Word; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;

I2C device write read

TI2CDeviceWriteRead = function(I2C:PI2CDevice; Address:Word; Initial:Pointer; Len:LongWord; Data:Pointer; Size:LongWord; var Count:LongWord):LongWord;

I2C device write write

TI2CDeviceWriteWrite = function(I2C:PI2CDevice; Address:Word; Initial:Pointer; Len:LongWord; Data:Pointer; Size:LongWord; var Count:LongWord):LongWord;

I2C device get rate

TI2CDeviceGetRate = function(I2C:PI2CDevice):LongWord;

I2C device set rate

TI2CDeviceSetRate = function(I2C:PI2CDevice; Rate:LongWord):LongWord;

I2C device get address

TI2CDeviceGetAddress = function(I2C:PI2CDevice):Word;

I2C device set address

TI2CDeviceSetAddress = function(I2C:PI2CDevice; Address:Word):LongWord;

I2C device get properties

TI2CDeviceGetProperties = function(I2C:PI2CDevice; Properties:PI2CProperties):LongWord;

I2C device

[Expand]

PI2CDevice = ^TI2CDevice;

TI2CDevice = record


Public variables



I2C logging

I2C_DEFAULT_LOG_LEVEL:LongWord = I2C_LOG_LEVEL_DEBUG; Minimum level for I2C messages. Only messages with level greater than or equal to this will be printed.
I2C_LOG_ENABLED:Boolean;


Function declarations



Initialization functions

[Expand]
procedure I2CInit;
Description: Initialize the I2C unit and I2C device table


I2C functions

[Expand]
function I2CDeviceStart(I2C:PI2CDevice; Rate:LongWord):LongWord;
Description: Start the specified I2C device ready for reading and writing


[Expand]
function I2CDeviceStop(I2C:PI2CDevice):LongWord;
Description: Stop the specified I2C device and terminate reading and writing


[Expand]
function I2CDeviceRead(I2C:PI2CDevice; Address:Word; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Read data from the specified I2C device


[Expand]
function I2CDeviceWrite(I2C:PI2CDevice; Address:Word; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Write data to the specified I2C device


[Expand]
function I2CDeviceWriteRead(I2C:PI2CDevice; Address:Word; Initial:Pointer; Len:LongWord; Data:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Write data to and Read data from the specified I2C device in one operation


[Expand]
function I2CDeviceWriteWrite(I2C:PI2CDevice; Address:Word; Initial:Pointer; Len:LongWord; Data:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Write 2 data blocks to the specified I2C device in one operation


[Expand]
function I2CDeviceGetRate(I2C:PI2CDevice):LongWord;
Description: Get the clock rate of the specified I2C device


[Expand]
function I2CDeviceSetRate(I2C:PI2CDevice; Rate:LongWord):LongWord;
Description: Set the clock rate for the specified I2C device


[Expand]
function I2CDeviceGetAddress(I2C:PI2CDevice):Word;
Description: Get the slave address for the specified I2C device


[Expand]
function I2CDeviceSetAddress(I2C:PI2CDevice; Address:Word):LongWord;
Description: Set the slave address for the specified I2C device


[Expand]
function I2CDeviceProperties(I2C:PI2CDevice; Properties:PI2CProperties):LongWord; inline;
Description: Get the properties for the specified I2C device


[Expand]
function I2CDeviceGetProperties(I2C:PI2CDevice; Properties:PI2CProperties):LongWord;
Description: Get the properties for the specified I2C device


[Expand]
function I2CDeviceCreate:PI2CDevice;
Description: Create a new I2C entry


[Expand]
function I2CDeviceCreateEx(Size:LongWord):PI2CDevice;
Description: Create a new I2C entry


[Expand]
function I2CDeviceDestroy(I2C:PI2CDevice):LongWord;
Description: Destroy an existing I2C entry


[Expand]
function I2CDeviceRegister(I2C:PI2CDevice):LongWord;
Description: Register a new I2C in the I2C table


[Expand]
function I2CDeviceDeregister(I2C:PI2CDevice):LongWord;
Description: Deregister an I2C from the I2C table


[Expand]
function I2CDeviceFind(I2CId:LongWord):PI2CDevice;
Description: Find an I2C device by ID in the I2C table


[Expand]
function I2CDeviceFindByName(const Name:String):PI2CDevice; inline;
Description: Find an I2C device by name in the device table


[Expand]
function I2CDeviceFindByDescription(const Description:String):PI2CDevice; inline;
Description: Find an I2C device by description in the device table


[Expand]
function I2CDeviceEnumerate(Callback:TI2CEnumerate; Data:Pointer):LongWord;
Description: Enumerate all I2C devices in the I2C table


[Expand]
function I2CDeviceNotification(I2C:PI2CDevice; Callback:TI2CNotification; Data:Pointer; Notification,Flags:LongWord):LongWord;
Description: Register a notification for I2C device changes


I2C slave functions

[Expand]
function I2CSlaveStart(I2C:PI2CDevice):LongWord; inline;
Description: Start the specified I2C slave ready for reading and writing


[Expand]
function I2CSlaveStop(I2C:PI2CDevice):LongWord; inline;
Description: Stop the specified I2C slave and terminate reading and writing


[Expand]
function I2CSlaveRead(I2C:PI2CDevice; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord; inline;
Description: Read data from the specified I2C slave


[Expand]
function I2CSlaveWrite(I2C:PI2CDevice; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord; inline;
Description: Write data to the specified I2C slave


[Expand]
function I2CSlaveGetAddress(I2C:PI2CDevice):Word; inline;
Description: Get the address for the specified I2C slave


[Expand]
function I2CSlaveSetAddress(I2C:PI2CDevice; Address:Word):LongWord; inline;
Description: Set the address for the specified I2C slave


[Expand]
function I2CSlaveGetProperties(I2C:PI2CDevice; Properties:PI2CProperties):LongWord; inline;
Description: Get the properties for the specified I2C slave


[Expand]
function I2CSlaveCreate:PI2CDevice;
Description: Create a new I2C slave entry


[Expand]
function I2CSlaveCreateEx(Size:LongWord):PI2CDevice;
Description: Create a new I2C slave entry


[Expand]
function I2CSlaveDestroy(I2C:PI2CDevice):LongWord; inline;
Description: Destroy an existing I2C slave entry


[Expand]
function I2CSlaveRegister(I2C:PI2CDevice):LongWord; inline;
Description: Register a new I2C slave in the I2C table


[Expand]
function I2CSlaveDeregister(I2C:PI2CDevice):LongWord; inline;
Description: Deregister an I2C slave from the I2C table


[Expand]
function I2CSlaveFind(I2CId:LongWord):PI2CDevice;
Description: Find an I2C slave by ID in the I2C table


[Expand]
function I2CSlaveFindByName(const Name:String):PI2CDevice; inline;
Description: Find an I2C slave by name in the device table


[Expand]
function I2CSlaveFindByDescription(const Description:String):PI2CDevice; inline;
Description: Find an I2C slave by description in the device table


RTL I2C functions

[Expand]
function SysI2CAvailable:Boolean;
Description: Check if an I2C device is available


[Expand]
function SysI2CStart(Rate:LongWord):LongWord;
Description: Start the default I2C device ready for reading and writing


[Expand]
function SysI2CStop:LongWord;
Description: Stop the default I2C device and terminate reading and writing


[Expand]
function SysI2CRead(Address:Word; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Read data from the default I2C device


[Expand]
function SysI2CWrite(Address:Word; Buffer:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Write data to the default I2C device


[Expand]
function SysI2CWriteRead(Address:Word; Initial:Pointer; Len:LongWord; Data:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Write data to and Read data from the default I2C device in one operation


[Expand]
function SysI2CWriteWrite(Address:Word; Initial:Pointer; Len:LongWord; Data:Pointer; Size:LongWord; var Count:LongWord):LongWord;
Description: Write 2 data blocks to the default I2C device in one operation


[Expand]
function SysI2CGetRate:LongWord;
Description: Get the clock rate of the default I2C device


[Expand]
function SysI2CSetRate(Rate:LongWord):LongWord;
Description: Set the clock rate for the default I2C device


[Expand]
function SysI2CGetAddress:Word; 
Description: Get the slave address for the default I2C device


[Expand]
function SysI2CSetAddress(Address:Word):LongWord;
Description: Set the slave address for the default I2C device


I2C helper functions

[Expand]
function I2CGetCount:LongWord;
Description: Get the current I2C count


[Expand]
function I2CDeviceGetDefault:PI2CDevice;
Description: Get the current default I2C device


[Expand]
function I2CDeviceSetDefault(I2C:PI2CDevice):LongWord;
Description: Set the current default I2C device


[Expand]
function I2CDeviceCheck(I2C:PI2CDevice):PI2CDevice;
Description: Check if the supplied I2C is in the I2C table


[Expand]
function I2CDeviceIsSlave(I2C:PI2CDevice):Boolean;
Description: Check if the supplied I2C is a slave device


[Expand]
function I2CTypeToString(I2CType:LongWord):String;
Description: Convert an I2C type value to a string


[Expand]
function I2CStateToString(I2CState:LongWord):String;
Description: Convert an I2C state value to a string


[Expand]
function I2CIs7BitAddress(Address:Word):Boolean;
Description: Determine if the supplied address is a 7bit address


[Expand]
function I2CIs10BitAddress(Address:Word):Boolean;
Description: Determine if the supplied address is a 10bit address


[Expand]
procedure I2CLog(Level:LongWord; I2C:PI2CDevice; const AText:String);
Description: To be documented


[Expand]
procedure I2CLogInfo(I2C:PI2CDevice; const AText:String); inline;
Description: To be documented


[Expand]
procedure I2CLogWarn(I2C:PI2CDevice; const AText:String); inline;
Description: To be documented


[Expand]
procedure I2CLogError(I2C:PI2CDevice; const AText:String); inline;
Description: To be documented


[Expand]
procedure I2CLogDebug(I2C:PI2CDevice; const AText:String); inline;
Description: To be documented


Return to Unit Reference