i2c communication with Asphyre/PXL

Discussion and questions about programming with Ultibo.
ChP
Posts: 8
Joined: Fri Oct 11, 2019 6:13 pm

i2c communication with Asphyre/PXL

Postby ChP » Sat Oct 12, 2019 6:54 pm

Hello everybody,

I am new on this forum.

I am playing with a Raspberry Pi 3B+ and I would like to use PLX units to manage I2c, SPI, PWM and GPIO.

I downloaded these units and there are some examples with. But, about I2c communication the example is dealing with display and it is not easy to see how PLX communicates with the I2c component.

I have a BMP180 and I would like to communicate with it.

For this, I have to write and read some bytes into registers.

So, do you have a general example (or a link to) that shows how to do this.

In other words, with rpi-hal :

    how initialize it for i2c communication,
    what is the equivalent of the linux command : i2cset -y 1 Addr Register value b
    what is the equivalent of the linux command : i2cget –y 1 Addr Register b

Sincerely.

Pierre
Last edited by ChP on Sun Oct 13, 2019 1:32 pm, edited 1 time in total.
User avatar
Ultibo
Site Admin
Posts: 2303
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: i2c communication with rpi-hal

Postby Ultibo » Sat Oct 12, 2019 10:32 pm

ChP wrote:I am playing with a Raspberry Pi 3B+ and I would like to use PLX units to manage I2c, SPI, PWM and GPIO.

Hello Pierre, welcome to Ultibo.

We might have to ask you to provide a little more info, the PLX units are not something we have heard of and Google doesn't seem to find anything.

Can you provide a link?

EDIT: Do you mean the Asphyre/PXL library?

ChP wrote:In other words, with rpi-hal :

how initialize it for i2c communication,
...

The rpi-hal library is specifically for Linux and doesn't work in Ultibo (although it could be ported), we can provide examples that show how to do these things using the Ultibo functions from the I2C unit if that gives you the information you need.

Please let us know.
Ultibo.org | Make something amazing
https://ultibo.org
ChP
Posts: 8
Joined: Fri Oct 11, 2019 6:13 pm

Re: i2c communication with rpi-hal

Postby ChP » Sun Oct 13, 2019 8:52 am

I'm sorry, I confused the library "rpi-hal" that I'm currently testing with the library "PXL" that I want to try. Therefore, replace "rpi-hal" with "PXL".
Ultibo wrote: ... We might have to ask you to provide a little more info, the PXL units are not something we have heard of and Google doesn't seem to find anything.

Can you provide a link?

EDIT: Do you mean the Asphyre/PXL library?

Yes, it is this library.

Ultibo wrote: ... In other words, with rpi-hal PXL units :

how initialize it for i2c communication,
...


and use functions : ReadByte, WriteByte, ReadByteData, WriteByteData,

Sincerely.

Pierre.
User avatar
Ultibo
Site Admin
Posts: 2303
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: i2c communication with rpi-hal

Postby Ultibo » Sun Oct 13, 2019 11:10 pm

ChP wrote:
Ultibo wrote:EDIT: Do you mean the Asphyre/PXL library?

Yes, it is this library.

Hello Pierre,

Thanks for that, it was just that we didn't recognize PLX.

ChP wrote: ... In other words, with rpi-hal PXL units :

how initialize it for i2c communication,
...

Initializing I2C communication using the PXL units is simply a matter of creating an object of the appropriate class. So for the Ultibo port of Asphyre/PXL you need to add the PXL.Boards.Ultibo unit to the uses clause of your program like this:

Code: Select all

uses
  ...
  PXL.Boards.Types,
  PXL.Boards.Ultibo;

This gives you access to the TUltiboI2C class which you can create an instance of to control your I2C device, like this:

Code: Select all

var
   I2C: TUltiboI2C;
   
begin
   // Create an I2C using the default device and frequency
   I2C := TUltiboI2C.Create;
 
   // You can also pass a specific device and frequency if needed
   I2C := TUltiboI2C.Create(I2CDeviceGetDefault, 100000);
   
   ....


ChP wrote:and use functions : ReadByte, WriteByte, ReadByteData, WriteByteData,


Which of these functions you need to use will depend on your I2C device and the behavior it needs.

For ReadByte and WriteByte you simply pass a byte variable to the function which will be either received from or sent to the device like this:

Code: Select all

var
   I2C: TUltiboI2C;
   
   Value: Byte;
begin
   // Create an I2C using the default device and frequency
   I2C := TUltiboI2C.Create;
 
   // Read a byte
   I2C.ReadByte(Value);
   
   // Write a byte
   Value := 123;
   I2C.WriteByte(Value);
   
   ...
   
   I2C.Free;


Many I2C devices require you to pass the register number that you want to read from or write to as part of the request, this is where you would need ReadByteData or WriteByteData. For these two functions the Command value is the register number within the device that you want to access.

Code: Select all

var
   I2C: TUltiboI2C;
   
   Command: Byte;
   Value: Byte;
begin
   // Create an I2C using the default device and frequency
   I2C := TUltiboI2C.Create;
 
   // The datasheet for the device will tell you the register numbers to use for each function
   Command := $6A;
   
   // Read a byte from the specified register
   I2C.ReadByteData(Command, Value);
   
   // Write a byte to the specified register
   Value := 123;
   I2C.WriteByteData(Command, Value);
   ...
   
   I2C.Free;


To use the BMP180 sensor, the Asphyre/PXL has a unit that is ready to use with this device, you can read the temperature by doing something like this:

Code: Select all

function ReadBMP180Temperature: Single;
var
  SystemCore: TUltiboSystemCore;
  I2C: TUltiboI2C;
  BMP180: TSensorBMP180;
begin
  SystemCore := TUltiboSystemCore.Create;
  try
    I2C := TUltiboI2C.Create;
    try
      BMP180 := TSensorBMP180.Create(SystemCore, I2C);
      try
        Result := BMP180.Temperature;
      finally
        BMP180.Free;
      end;
    finally
      I2C.Free;
    end;
  finally
    SystemCore.Free;
  end;
end;


Of course that is only an example, you probably wouldn't create and destroy all of the objects every time you read the temperature, instead you might create those objects when your application starts and reuse them each time.

Hope that makes sense, please feel free to ask if you have more questions.

EDIT: Forgot to mention that you should also set I2C.Address to the address of your device
Ultibo.org | Make something amazing
https://ultibo.org
ChP
Posts: 8
Joined: Fri Oct 11, 2019 6:13 pm

Re: i2c communication with Asphyre/PXL

Postby ChP » Mon Oct 14, 2019 8:11 am

Thank you very much "ultibo" for this answer.

In the "pxl v110" I have downloaded, there is not the unit "PXL.Boards.Ultibo". I found it in the version v101. But this unit needs other units : "GlobalConst, GlobalConfig, Platform, GPIO, UART, I2C, SPI" that I have not found. May be you tell me where to find them.

So, I can't try your examples.

In the meantime, searching to solve by mysefl the problem, I have found this solution that allows me to read and write in the registers :

Code: Select all

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure EcrireClick(Sender: TObject);
    procedure LireClick(Sender: TObject);
    procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
  private
    { private declarations }
    SystemCore: TFastSystemCore;
    GPIO: TFastGPIO;
    PortI2C: TFastI2C;
    AddrI2c: Byte; // Address of the component
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  SystemCore:= TFastSystemCore.Create;
  GPIO:= TFastGPIO.Create(SystemCore);
  PortI2C:= TFastI2C.Create(GPIO);
  AddrI2c:= $1E; // Address of my test component
  PortI2C.SetAddress(AddrI2C);
end;

procedure TForm1.EcrireClick(Sender: TObject);
var
  Res: Boolean;
  RegE, Val: Byte;
begin
  RegE:= Hex2Dec(edRegE.Text); // I use a TEdit to load the register
  Val:= Hex2Dec(edValE.Text); // I use a TEdit to load the value
  Res:= PortI2C.WriteByteData(RegE, val);
end;

procedure TForm1.LireClick(Sender: TObject);
var
  Res: Boolean;
  RegL, Val: Byte;
begin
  Val:= $00;
  RegL:= Hex2Dec(edRegL.Text); // I use a TEdit to load the register
  Res:= PortI2C.ReadByteData(RegL, Val); // I use a TEdit to read the returned value
  edValL.Text:= IntToHex(Val, 2);
end;


This works fine.

I also want to drive some component with PWM signal, but using the dedicated units of pxl v110, I have not achieved to do it. So, could you give an simple example for generating a PWM signal.

I Thank you in advance.

Sincerely.

Pierre
hansotten
Posts: 65
Joined: Thu Feb 04, 2016 7:07 am

Re: i2c communication with Asphyre/PXL

Postby hansotten » Mon Oct 14, 2019 10:56 am

Pierre,

I see you search for Ultibo things in the Asphyre PXL library. So what OS and what PXL library are you using?

Your examples show Lazarus/GUI objects, so that means Raspbian/Linux and the standard Asphyre library. WHoch both know noting about Ultibo, but do know FPC and Lazarus.

The Ultibo version lives here: https://github.com/ultibohub/Asphyre

Note Ultibo does not support Lazarus GUI objects.
ChP
Posts: 8
Joined: Fri Oct 11, 2019 6:13 pm

Re: i2c communication with Asphyre/PXL

Postby ChP » Mon Oct 14, 2019 2:30 pm

hansotten wrote: The Ultibo version lives here: https://github.com/ultibohub/Asphyre ...

This version is the v101.

I downloaded the PXL v110 from here.

These two versions differs by :

  • v101 contains : "PXL.Devices.GLES.Ultibo.pas" and "PXL.Boards.Utilbo.pas" that aren't in the v110 version,
  • v110 is an upgrade and contains : "PXL.Boards.LCP11xx.pas", "PXL.Boards.STM32F4xx.pas" and "PXL.Sysfs.Core.pas" that aren't in the v101 version.

Both version does'nt contain "GlobalConst, GlobalConfig, Platform, GPIO, UART, I2C, SPI" called in "PXL.Boards.Utilbo.pas".

So, my priority request, for now, is a simple example for generating PWM signal.

Sincerely.

Pierre
User avatar
Ultibo
Site Admin
Posts: 2303
Joined: Sat Dec 19, 2015 3:49 am
Location: Australia

Re: i2c communication with Asphyre/PXL

Postby Ultibo » Mon Oct 14, 2019 10:59 pm

ChP wrote:In the "pxl v110" I have downloaded, there is not the unit "PXL.Boards.Ultibo". I found it in the version v101. But this unit needs other units : "GlobalConst, GlobalConfig, Platform, GPIO, UART, I2C, SPI" that I have not found. May be you tell me where to find them.

Hello Pierre,

From your example which uses TForm it looks like you are developing an application to run on a Raspberry Pi using Raspbian (Linux).

Ultibo is a platform that replaces Raspbian completely so you cannot mix the two together, in order to develop for Ultibo you need to download our modified version of Lazarus and Free Pascal from our download page but only if you want to create bare metal (without Linux) applications.

If you want to create applications for Raspberry Pi and Raspbian then you should use the standard Lazarus and the PXL v110 from the official Asphyre/PXL site, if you have questions about developing for Raspberry Pi and Raspbian then you should find good information on the Lazarus forum instead.

Hope that helps, please let us know if we have misunderstood what you are wanting to do.
Ultibo.org | Make something amazing
https://ultibo.org
Gavinmc42
Posts: 1665
Joined: Sun Jun 05, 2016 12:38 pm
Location: Brisbane, Australia

Re: i2c communication with Asphyre/PXL

Postby Gavinmc42 » Tue Oct 15, 2019 2:24 am

If you want to use Ultibo there is some equivalent code for i2cdetect/i2cset/i2cget.
viewtopic.php?f=13&t=301&p=1234&hilit=i2cdetect#p1234

Not sure how to do it with Laz/FPC/Raspbian.
I found i2c comms was more reliable in Ultibo anyway.
ChP
Posts: 8
Joined: Fri Oct 11, 2019 6:13 pm

Re: i2c communication with Asphyre/PXL

Postby ChP » Tue Oct 15, 2019 7:37 am

Ultibo wrote: ...If you want to create applications for Raspberry Pi and Raspbian then you should use the standard Lazarus and the PXL v110 from the official Asphyre/PXL site ...

It's what I want to do.

Ultibo wrote: ... if you have questions about developing for Raspberry Pi and Raspbian then you should find good information on the Lazarus forum instead. ...

I know this forum and I go to it to get some information. But I directed me to your forum because it's the place where to find information about i2c bus, PWM waveform via your PXL units.

So, my request is how to generate PWM signal via a simple example using PXL (v110) units.

Sincerely.

Pierre

Return to “General”

Who is online

Users browsing this forum: No registered users and 40 guests