Recent

Author Topic: FPC on Rasp Pi, non Lazarus, use of GPIO.  (Read 6246 times)

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #90 on: February 25, 2021, 01:58:05 pm »
MarkMLI wrote:
Quote
Wrapping this up from my POV, after comparing GPIO handling on a PC and a Raspberry Pi, with particular emphasis on the (new) gpiod API.

By the weekend I may even understand enough of that (just rolled out of bed - haven't had enough coffee yet ...

Thanks.  I look forward to trying that out with the package you posted the other day.  Big day at work today, finalizing the 2020 books for tax time.. almost done - then I should have more time for this.  My components are still jammed up in NJ/FedEx. 

What I get for choosing the cheapest shipping option "FedEx mail".

I did purchase a 4K monitor which allegedly arrives today.  That should make working with Lazarus on the Pi a lot easier than the 1080p screen presently attached.

Cheers!
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2423
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #91 on: February 25, 2021, 02:19:43 pm »
Thanks.  I look forward to trying that out with the package you posted the other day.

I believe that the stuff I posted the other day is functionally OK. Cosmetically it's a bit of a mess since it's had the minimum TLC necessary to convert the .h to a .inc which my (unpublished) preprocessor can then use to generate static and dynamic .pas wrappers.

You might find that there's one or two functions which aren't in the library version that distreaux are currently shipping. This will obviously cause a link error if you're using static linkage, but a runtime error if you're using dynamic... hence e.g.

Code: Pascal  [Select][+][-]
  1. // Older versions of libgpiod e.g. as shipped with Debian "Buster" might not
  2. // support this entry point.
  3.  
  4.           try
  5.             x := Gpiod.gpiod_line_is_active_low(lineHandle)
  6.           except
  7.             x := false
  8.           end
  9.  

After coming across a number of places where this happened I changed the dynamic loading unit to have this in the two LoadRoutine() functions:

Code: Pascal  [Select][+][-]
  1.     raise DynamicModuleException.Create(FLastError)
  2.  

MarkMLl



MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #92 on: February 26, 2021, 02:30:37 pm »
Quote
I believe that the stuff I posted the other day is functionally OK.

I guess I missed something along the road.  I'll look for the latest version.
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2423
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #93 on: February 26, 2021, 02:38:30 pm »
Quote
I believe that the stuff I posted the other day is functionally OK.

I guess I missed something along the road.  I'll look for the latest version.

Only been posted once. See #73 of this thread.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #94 on: February 27, 2021, 01:04:33 am »

Quote
Only been posted once. See #73 of this thread.
Great - already DL'd - not unpacked yet.  Probably tomorrow.  I have a test target - an old GPS with a serial port.

Cheers,
Have a great weekend!
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #95 on: February 27, 2021, 04:55:11 pm »
Quote
  I have a test target - an old GPS with a serial port.

... which I can't use directly with the Pi because it's 5V, and Pi needs 3V.  So I need to find some level converters first... delays, delays...

I'll delve into MarkMLI's GPIO package in the meantime.  Can't get it to link - even though  lgpiod is in the same folder as the project ( or in its subfolder)
Code: Text  [Select][+][-]
  1. Compile Project, Target: pharosGPS: Exit code 1, Errors: 1, Warnings: 2
  2. pharosGPS.lpr(12,0) Warning: "crtbegin.o" not found, this will probably cause a linking failure
  3. pharosGPS.lpr(12,0) Warning: "crtend.o" not found, this will probably cause a linking failure
  4. /usr/bin/ld.bfd: cannot find -lgpiod
  5. pharosGPS.lpr(12,0) Error: Error while linking
  6.  
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2423
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #96 on: February 27, 2021, 06:06:02 pm »
Have you installed the gpiod development package? My Lazarus "Other unit files" setting included gpiod (i.e. as a subdirectory of the project directory and that contained


./gui_test.lpi

./gpiod
...
./gpiod/gpiod.pas
./gpiod/gpiodDefs.inc
./gpiod/gpiod_dynamic.pas
./gpiod/headers
./gpiod/headers/gpiod.h
./gpiod/headers/gpiod.pp-
./gpiod/headers/gpiod-original.h
./gpiod/gpiod-macros.inc
./gpiod/Makefile
./gpiod/WHENCE
./gpiod/dynamicmodule.pas
./gpiod/dynamicmodule.pas~
...
./pollgpiochardev.pas
./gpiochardevhardware.pas


Assume that some of that is cruft that you won't have, I'm showing it "warts and all".

Otherwise you might do better using the dynamic unit to start with, but note the two changed statements I mentioned a couple of days ago.

MarkMLl


Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #97 on: February 27, 2021, 06:33:26 pm »

Sounds right.  Wasn't clear I had to "install" anything -  I thought the package (so to speak) would just be a complete thing - soup to nuts.

Yes - folder is there.  I had moved some of those files into the same folder as where my main program (project) file is located.
  • I know absolutely nothing about programming in this environment. Lazarus,

    I know absolutely nothing about the LCL.

    I'm not proficient in object programming


I'll plow on,,,

(I also have to re-install Lazarus ... alas I re-installed the OS because of something I munged up in Linux land..., at least I had backed up all the other stuff done to date).
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2423
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #98 on: February 27, 2021, 07:08:38 pm »
Sounds right.  Wasn't clear I had to "install" anything -  I thought the package (so to speak) would just be a complete thing - soup to nuts.

You've got a Raspberry Pi. Running Raspbian or whatever they call it these days. I suggest you familiarise yourself with its SOP, and the significance of lines like this which describe Raspbian (nee Debian) packages:

gpiod - Tools for interacting with Linux GPIO character device - binary
libgpiod-dev - C library for interacting with Linux GPIO device - static libraries and headers

The basic gpiod package will include a dynamically-linkable library, to which gpiod_dynamic.pas should interface.

The libgpiod-dev package will include a statically-linkable library, to which gpiod.pas should interface.

Absolutely nothing to do with Lazarus. this is RPi stuff.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #99 on: February 27, 2021, 07:56:32 pm »
Quote
gpiod - Tools for interacting with Linux GPIO character device - binary
libgpiod-dev - C library for interacting with Linux GPIO device - static libraries and headers

The basic gpiod package will include a dynamically-linkable library, to which gpiod_dynamic.pas should interface.

The libgpiod-dev package will include a statically-linkable library, to which gpiod.pas should interface.

Absolutely nothing to do with Lazarus. this is RPi stuff.

Thanks - I confused that error with the names of the folder you had in your package (gpiod) containing gpiod.pas ...;;

OTOH:
Code: Text  [Select][+][-]
  1. pi@raspberrypi:~/Documents/PharosGPS $ sudo apt-get install gpiod
  2. Reading package lists... Done
  3. Building dependency tree      
  4. Reading state information... Done
  5. gpiod is already the newest version (1.2-3+rpi1).
  6. 0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
  7.  
  8. pi@raspberrypi:~/Documents/PharosGPS $ sudo apt-get install lgpiod-dev
  9. Reading package lists... Done
  10. Building dependency tree      
  11. Reading state information... Done
  12. E: Unable to locate package lgpiod-dev
  13.  
  14.  

yet: ( line 5 of below):

Code: Text  [Select][+][-]
  1. Compile Project, Target: pharosGPS: Exit code 1, Errors: 1, Warnings: 2
  2. pharosGPS.pas(13,60) Warning: "crtbegin.o" not found, this will probably cause a linking failure
  3. pharosGPS.pas(13,60) Warning: "crtend.o" not found, this will probably cause a linking failure
  4. /usr/bin/ld.bfd: warning: /home/pi/Documents/PharosGPS/link.res contains output sections; did you forget -T?
  5. /usr/bin/ld.bfd: cannot find -lgpiod
  6. pharosGPS.pas(13,60) Error: Error while linking
  7.  
  8.  



« Last Edit: February 27, 2021, 08:23:10 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2423
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #100 on: February 27, 2021, 08:16:29 pm »
have you installed the libgpiod-dev package? Are you using gpiod.pas or gpiod_dynamic.pas?

I had this stuff running painlessly on a newly-configured RPi a couple of days ago, so I know it works.

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #101 on: February 27, 2021, 08:34:13 pm »
What a difference a few chars make ... libgpiod-dev  , not lgpiod-dev  :-[

So, it compiles (with the crt begin/end errors), runs (I have it write a random number to a file).

So, on to some blinking lights.

Thanks!!!
« Last Edit: February 27, 2021, 08:40:34 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #102 on: February 28, 2021, 03:59:49 pm »
@MarkMLI:

In a post further up (#76), you posted what I assume is a demo function to list all of the ports.  I tried to use this but it won't compile using your units:

Example:
Code: Pascal  [Select][+][-]
  1. FUNCTION ListGpioChardevPorts(currentPort: THandle= InvalidGpioHandle): TStringList;

Code: Text  [Select][+][-]
  1. pharosGPS.pas(14,53) Error: Identifier not found "InvalidGpioHandle"

"InvalidGpioHandle"is not defined anywhere in your package that I can find. The compiler threw other errors with that function, as well

Searched for it and it's only in my testcode:
Code: Text  [Select][+][-]
  1. pi@raspberrypi:~/Documents/PharosGPS $ grep -rnw  -e 'InvalidGpioHandle'
  2. backup/pharosGPS.pas:14:FUNCTION ListGpioChardevPorts(currentPort: THandle= InvalidGpioHandle): TStringList;
  3. backup/pharosGPS.pas:22:  if currentPort <> InvalidGpioHandle then
  4. pharosGPS.pas:14:FUNCTION ListGpioChardevPorts(currentPort: THandle= InvalidGpioHandle): TStringList;
  5. pharosGPS.pas:22:  if currentPort <> InvalidGpioHandle then
  6. pi@raspberrypi:~/Documents/PharosGPS $
  7.  

The problem with me is, as stated, everything LCL, object programming is in between me (simple programmer) and the hardware.

Your package is so abstract to me that I can't make heads or tails of where the code meets the hardware,   Your objective is good, but the implementation might as well be Chinese to me,

Could you post as _few lines_ as possible of CLI code that, using your units, will do, and only do:

Code: Pascal  [Select][+][-]
  1. program Alanisasimpledumbass;
  2. USES
  3.     crt,  //if need be
  4.     gpiod,
  5.     gpiod_dynamic,
  6.     Dynamicmodule,
  7.     SysUtils ,
  8.     Classes //,
  9.     ;  
  10.  
  11. type
  12.            //
  13. var
  14.           i: integer;
  15.  
  16. // set GPIO pin 17 to output mode.
  17.          <  >
  18.  
  19.         for i := 1 to 10 do
  20.              begin
  21. // turn the pin on
  22.                    < >
  23.                   sleep(100);
  24. //turn the pin off.
  25.                    < >
  26.                   sleep(100);
  27.              end;
  28. //- return the pin to the system (equivalent to Unexport).
  29.           <  >
  30. //- other'
  31.            < >
  32.  
  33.  
  34. end.
  35.  

With a working version of that, I'll be able to trace the code down to understand how it works.

Thanks,
« Last Edit: February 28, 2021, 04:26:28 pm by AlanTheBeast »
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2423
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #103 on: February 28, 2021, 06:42:08 pm »
Assume InvalidGpioHandle is -1. That wasn't so much working demo code as an illustration of how to call the entry points, noting IN PARTICULAR that the leading "Gpiod." is REQUIRED for both the static and dynamic variants.

Apart from that, the function of the various entry points is as described in the documentation for the gpiod library. I suggest familiarising yourself with that and with their examples: my units are a /very/ thin wrapper around that with no attempt to convert their API into something object-based.

Looking briefly at your code, use EITHER the gpiod unit, OR the gpiod_dynamic unit, not both since that has your program trying to use two different library interfaces for the same functionality. I would suggest using gpiod_dynamic, since (a) that doesn't need the -dev package and (b) I've already pointed out that some of the documented gpiod functionality (i.e. from the POV of the kernel developers) isn't actually in the library version shipped by Debian so might or might not be in the one shipped by Raspbian, and if you use dynamic linkage you'll get a run-time exception which you can work around.

I've not got code that you can easily drop into a project and run, but below are two snippets that you can use to see the sequence I've used... apart from that you're going to have to read the documentation.

Assume that e indicates whether a line is enabled, x whether it needs to be inverted (xor operation) and v its value. You don't want to see the GUI stuff.

Code: Pascal  [Select][+][-]
  1.         e := true;
  2.         v := 0;
  3.         lineHandle := Gpiod.gpiod_chip_get_line(chipHandle, i);
  4.         if lineHandle = nil then
  5.           e := false;
  6.         if e and Gpiod.gpiod_line_is_used(lineHandle) then
  7.           e := false;
  8.         if e and (Gpiod.gpiod_line_direction(linehandle) <> GPIOD_LINE_DIRECTION_INPUT) then
  9.           e := false;
  10.         if e and (Gpiod.gpiod_line_request_input(lineHandle, Pchar('GuiTest')) <> 0) then
  11.           e := false;
  12.         if e then
  13.  
  14. // Older versions of libgpiod e.g. as shipped with Debian "Buster" might not
  15. // support this entry point.
  16.  
  17.           try
  18.             x := Gpiod.gpiod_line_is_active_low(lineHandle)
  19.           except
  20.             x := false
  21.           end
  22.         else
  23.           x := false;
  24.         if e then begin                 (* Accessible and input                 *)
  25.           v := Gpiod.gpiod_line_get_value(lineHandle); // Bit from GPIO
  26.           if not (v in [0, 1]) then
  27.             continue;
  28.           v := v xor Ord(x);
  29.           linesForDisplay.Append(Format('d%d:%d', [i - lowest, v])); // Bit to GUI
  30.           Gpiod.gpiod_line_release(lineHandle)
  31.         end
  32.       end;
  33.  
  34. ...
  35.  
  36.         e := true;
  37.         v := Ord(guiTestForm.DigitalOutValue[i - lowest]); // Bit from GUI
  38.         lineHandle := Gpiod.gpiod_chip_get_line(chipHandle, i);
  39.         if lineHandle = nil then
  40.           e := false;
  41.         if e and Gpiod.gpiod_line_is_used(lineHandle) then
  42.           e := false;
  43.         if e and (Gpiod.gpiod_line_direction(linehandle) <> GPIOD_LINE_DIRECTION_OUTPUT) then
  44.           e := false;
  45.         if e and (Gpiod.gpiod_line_request_output(lineHandle, Pchar('GuiTest'), 0) <> 0) then
  46.           e := false;
  47.         if e then
  48.  
  49. // Older versions of libgpiod e.g. as shipped with Debian "Buster" might not
  50. // support this entry point.
  51.  
  52.           try
  53.             x := Gpiod.gpiod_line_is_active_low(lineHandle)
  54.           except
  55.             x := false
  56.           end
  57.         else
  58.           x := false;
  59.         if e then begin                 (* Accessible and output                *)
  60.           if Gpiod.gpiod_line_set_value(lineHandle, Ord((v <> 0) xor x)) = 0 then // Bit to GPIO
  61.             linesForDisplay.Append(Format('d%d:%d', [i - lowest, Ord((v <> 0) xor x)])); // Bit to GUI
  62.           Gpiod.gpiod_line_release(lineHandle)
  63.         end
  64.  

I've not investigated using that API to set directions. What I've done so far is set them using the sysfs API, then unexport the line to make it unused hence available to the dev interface. As I've already written up, there's a gpiod program to show you chip/line status.

I've been working hard outdoors all day, and now have to do paperwork.

MarkMLl

« Last Edit: February 28, 2021, 07:11:34 pm by MarkMLl »
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

AlanTheBeast

  • Full Member
  • ***
  • Posts: 185
  • My software never cras....
Re: FPC on Rasp Pi, non Lazarus, use of GPIO.
« Reply #104 on: March 01, 2021, 12:01:12 am »

Minor progress... (between errands etc.).

Can't seem to get a valid line handle. (probably because the chip handle is not valid?)

Many variations on path used:    /dev/gpiochip0 (and 1, and via /sys/class, etc and so on...

Compiles, runs, crashes on attempting to

a) testing the path (commented out below) and

b)   lineHandle := gpiod.gpiod_chip_get_line(chipHandle, i); 

because presumably the returned chip handle is not valid because the path is wrong?

Code: Pascal  [Select][+][-]
  1. program pharosGPS;
  2. //{$mode objfpc}{$H+}
  3. USES
  4.     crt,
  5. //    gpiod,
  6.     gpiod_dynamic,
  7.     Dynamicmodule,SysUtils,
  8.     Classes
  9.  
  10.     ;
  11. Const GPIOPath : Pchar = '/dev/gpiochip0/';       // tried via  /sys/class/gpio/... also
  12.  
  13. var
  14.   e: boolean;
  15.   v: integer;
  16.   x: boolean;
  17.   i: word = 22;
  18.   lineHandle: Pgpiod_line;
  19.   chiphandle: Pgpiod_chip;
  20.  
  21. begin
  22.      writeln ('START: ',GPIOPath);
  23.   e := true;
  24.   v := 0;
  25.   //writeln(gpiod.gpiod_is_gpiochip_device(GPIOPath));   //    <- this crashes too
  26.  
  27.   chiphandle := gpiod.gpiod_chip_open (GPIOPath);
  28.  
  29.   writeln ('LH');
  30.   lineHandle := gpiod.gpiod_chip_get_line(chipHandle, i);     //crashes here
Everyone talks about the weather but nobody does anything about it.
..Samuel Clemens.

 

TinyPortal © 2005-2018