[solved] I2C sensor handling problems lazarus / 21-General ---------------------------------------------------------------------- ThomasK · 2021-08-06 11:05 Hi, after reviewing https://forum.lazarus.freepascal.org/index.php/topic,47384.0.html I am quite sure that I have followed the advices. But I still can't get my code to work I am trying to interface to an i2c sensor and already fail using fpioctl..... I follow the c example of the manufacturer.https://github.com/Sensirion/raspberry- pi-i2c-sht4x/blob/master/sensirion_i2c_hal.c. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i unit shc35; {$mode objfpc}{$H+} interface uses  baseUnix, Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls; type   { TForm1 }   TForm1 = class(TForm)    Buttoncmd0x24: TButton;    ButtonDevOpen: TButton;    ButtonDevClose: TButton;    ButtonIOctl: TButton;    Memo1: TMemo;    procedure Buttoncmd0x24Click(Sender: TObject);    procedure ButtonIOctlClick(Sender: TObject);    procedure ButtonDevCloseClick(Sender: TObject);    procedure ButtonDevOpenClick(Sender: TObject);  private   public   end; var  Form1: TForm1;  data : array[0..63] of byte;  count : uint16;const  I2C_DEVICE_PATH   : string ='/dev/i2c-1';// The following defines were taken from i2c-dev.h.  I2C_SLAVE         : integer = $0703;  //1795 dec  I2C_WRITE_FAILED  = -1;  I2C_READ_FAILED   = -1;//  i2c_device : integer = -1;            //file handle preset  i2c_address: uint8   = $45;           //adress of SHT35 sensor as found per i2cdetect -y 1 implementation {$R *.lfm} { TForm1 } procedure TForm1.ButtonDevOpenClick(Sender: TObject);begin  i2c_device := fpopen(I2C_DEVICE_PATH, O_RdWr);  if i2c_device = -1  then memo1.lines.add('device open failed')  else memo1.lines.add('device opened:'+inttostr(i2c_device));end; procedure TForm1.ButtonDevCloseClick(Sender: TObject);begin  if i2c_device >= 0 then  begin    fpclose(i2c_device);    memo1.lines.add('device closed');  end else memo1.lines.add('device handle < 0');end; procedure TForm1.ButtonIOctlClick(Sender: TObject);var  rslt : integer;begin  rslt := fpIOCtl(i2c_device, I2C_SLAVE, @i2c_address);  memo1.lines.add('ioctl rslt: '+inttostr(rslt));end; procedure TForm1.Buttoncmd0x24Click(Sender: TObject);var  rslt : integer;begin  fpIOCtl(i2c_device, I2C_SLAVE, @i2c_address);  data[0]:=$24;  count :=1;  rslt:=fpwrite(i2c_device,data[0],count);  if rslt=count then memo1.lines.add('write with '+inttostr(rslt)+' byte(s) ok')  else memo1.lines.add('write: '+inttostr(rslt));end; end.   Does anybody have a clue what's wrong? Thanks Thomas ---------------------------------------------------------------------- rsz · 2021-08-06 20:24 Hi, on first glance everything seems OK. The result of fpIOCtl only indicates that there was an error with -1, you can use fpGetErrno after an error to retrieve extended error information which may help in finding the issue. You can then take the number that you get and see what it means in /usr/include/asm-generic/errno-base.h and /usr/include/asm-generic/errno.h. EDIT: I just noticed that you pass the address of your i2c_address variable to the fpIOCtl function, I believe it should be Pointer(i2c_address) instead, where you cast the value of i2c_address to a pointer instead. ---------------------------------------------------------------------- ThomasK · 2021-08-07 16:56 Hi RSZ, in other code I used the operator @ together with the variable to provide the fpioctl with the pointer. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   strcopy(ifr.ifr_name, CANDevice);  if fpioctl(s, SIOCGIFINDEX, @ifr) < 0 then  begin    perror:='error IOctl';//    mLog.Lines.Add(perror);  Addmessages(perror);    Exit; Why the fpioctl is so picky when I provide a pointer with the same operator to an integer, I don't know. Is this a bug or a feature....because the data size is the same afterwards  Your hint to check the error number was helpfull anyway. Together with the code from MarkMLl of the other topic (link already given) I got it to work. Before using the pointer() it is necessary to cast the integer value to a ptruint..... Code: C  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i int8_t sensirion_i2c_hal_read(uint8_t address, uint8_t* data, uint16_t count) {    if (i2c_address != address) {        ioctl(i2c_device, I2C_SLAVE, address);        i2c_address = address;    }In the C-code of the device manufacturer I don't see that, maybe the compiler is more clever..... So my code looks now clumpsy like that but it works. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i   rslt := fpioctl(i2c_device, I2C_SLAVE, pointer(ptruint(i2c_address))); I am communicating with the sensor and the data is ok. Thanks again. Thomas ---------------------------------------------------------------------- MarkMLl · 2021-08-07 18:00 Why not simply define your variable as a ptruint? The point is that it's an unsigned integer which is the same size as a pointer, that that relationship will hold irrespective of platform, and that- unlike C- the compiler can and will check for overflow etc. For a whole lot of low-level stuff you have to look carefully to see whether a declaration expects a pointer or uses a var parameter. On occasion the distinction looks pretty arbitrary, and I suspect that the rationale has been lost to the mists of time. MarkMLl ---------------------------------------------------------------------- ThomasK · 2021-08-08 11:02 Hi Mark, thank you very much for the encouraging words. The difference between poetry and reality is always easy when understood. Have a nice day. Thomas