unit i2cbasic;
{$mode objfpc}{$H+}
interface
uses
baseUnix, Classes, SysUtils;
type
Regarray = array [0..63] of byte;
pRegarray = ^Regarray;
function i2cfpIoctl(i2c_deviceaddress: ptruint): cint;
function i2cfpgeterrno() : longint;
function i2cOpen() : cint;
function i2cClose() : cint;
function i2cwrite(var buf ; nbytes : TSize) : Tsize;
function i2cread( var buf ; nbytes : TSize) : Tsize;
var
i2c_device : cint = -1; //file handle preset
i2c_address_sht35 : ptruint = $44; //address of SHT75 sensor as found per i2cdetect -y 1
i2c_address_sht35a : ptruint = $45; //alternative address of SHT75 sensor as found per i2cdetect -y 1
i2c_address_sht85 : ptruint = $44; //address of SHT85 sensor as found per i2cdetect -y 1
i2c_address_sgp30 : ptruint = $58; //address of SGP30 SVM30 board as found per i2cdetect -y 1
i2c_address_sgp40 : ptruint = $59; //address of SGP40 sensor from data sheet, i2cdetect -y 1 does not show it!
i2c_address_dps310 : ptruint = $77; //address of DPS310 sensor as found per i2cdetect -y 1
i2c_address_svm40 : ptruint = $6A; //address of SVM40 board as found per i2cdetect -y 1
i2c_address_mpc23017 : ptruint = $27; //address of MPC23017 board as found per i2cdetect -y 1
i2c_address_pca9685 : ptruint = $70; //address of PCA9685 board as found per i2cdetect -y 1
i2c_address_shtc1 : ptruint = $70; //address of SHTC1 on SVM30 board as found per i2cdetect -y 1
i2c_address_all : ptruint = $00; //Broaadcast
data : Regarray;
count : uint16;
pdata : pRegarray;
(*
pi@RasPiHW:~ $ i2cdetect -y 1
0 1 2 3 4 5 6 7 8 9 a b c d e f
00: -- 04 -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- 44 45 -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- 58 59 -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: 70 -- -- -- -- -- -- 77
pi@RasPiHW:~ $
*)
const
I2C_DEVICE_PATH : string ='/dev/i2c-1'; //i²c bus on header pins 3 & 5
// The following defines were taken from i2c-dev.h.
I2C_SLAVE = 1795; // 0x0703 - 1795 dec
I2C_WRITE_FAILED = -1;
I2C_READ_FAILED = -1;
implementation
function i2cfpIoctl(i2c_deviceaddress: ptruint): cint;
// i2c_device as file handle, Ndx (=I2C_SLAVE) is the IOCTL function request type, data is the target i2c device address.
begin
Result := BaseUnix.FpIOCtl(i2c_device, I2C_SLAVE, pointer(i2c_deviceaddress));
end { i2cfpIoctl } ;
function i2cfpgeterrno() : longint ;
begin
result:=fpgeterrno;
end { i2cfpgeterrno } ;
function i2cOpen() : cint;
begin
i2c_device := fpopen(I2C_DEVICE_PATH, O_RdWr);
Result:=i2c_device;
end { i2cOpen } ;
function i2cClose() : cint;
begin
if i2c_device >= 0
then result:=fpclose(i2c_device)
else result:=-1;
end { i2cClose } ;
function i2cwrite(var buf ; nbytes : TSize) : Tsize;
begin
result:= fpwrite(i2c_device, buf, nbytes);
end{ i2cwrite};
function i2cread(var buf; nbytes : TSize) : Tsize;
begin
result:= fpread(i2c_device, buf, nbytes);
end{ i2cread};
end { i2cbasic}.