Recent

Author Topic: Raspbian: running a program using PascalIO from the IDE  (Read 4353 times)

GuidoJ

  • New Member
  • *
  • Posts: 14
Raspbian: running a program using PascalIO from the IDE
« on: March 02, 2021, 03:57:38 pm »
Hello!

I'm a very experienced Delphi developer and I'm taking first steps on a Raspberry PI 4 with Raspbian and FPC+Lazarus. Of course, I have next to no knowledge about LINUX. My current task: Switch an LED on and off.

I downloaded the PascalIO package, then wrote some simple code that should turn an IO port on/off.

var
  IOvalue : boolean;

procedure TForm1.Button1Click(Sender:TObject);
var
  pin: TGpioLinuxPin;
begin
  IOvalue := not IOvalue;
  pin := TGpioLinuxPin.Create(12);
  pin.Direction := gdOut;
  pin.Value := IOvalue;
  pin.Destroy;
end.

If I start the program from the IDE, nothing happens on pushing the button.
Setting a breakpoint and stepping through the code makes the LED flash.

From the console I need to use
  sudo ./MyGPIOtest
to start the program and get a response from the LED.

The code seems to be OK.

Any suggestions, how to make the program run correctly from within the IDE?


Thank you for your help!

MarkMLl

  • Hero Member
  • *****
  • Posts: 2412
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #1 on: March 02, 2021, 04:13:51 pm »
Without having tried it, my suspicion is that when you destroy the pin you're setting it to be an input again so it stops driving the LED.

BTW, it's "Linux", and is named after its initial developer rather than being an initialism.

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

GuidoJ

  • New Member
  • *
  • Posts: 14
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #2 on: March 02, 2021, 05:13:26 pm »
Everything works fine, outside the IDE or during singlestepping. The LED even stays on after terminating the program.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2412
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #3 on: March 02, 2021, 05:47:22 pm »
Everything works fine, outside the IDE or during singlestepping. The LED even stays on after terminating the program.

If you're running it in the IDE, how are you giving it sudo? Have you made sure that your normal user ID is a member of the gpio group?

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

GuidoJ

  • New Member
  • *
  • Posts: 14
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #4 on: March 02, 2021, 06:23:17 pm »
Hi Mark!


Four cases:
1. console -> "sudo ./MyGPIOtest" - result OK
2. start in IDE [F9] - LED does not react to button
3. start in IDE [F9], singlestepping [F8] - result OK
4. start in IDE without debugger [ctrl][shift][F9] - LED does not react to button

Cases 2 and 4 - running inside the IDE but not singlestepping [F8] through the code - are the problem.


GPIO group? Ah... "sudo adduser pi gpio" -> the user "pi" is already in the group "gpio"
So the answere is "yes".


Thanks, Guido

MarkMLl

  • Hero Member
  • *****
  • Posts: 2412
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #5 on: March 02, 2021, 06:30:45 pm »
Four cases:
1. console -> "sudo ./MyGPIOtest" - result OK
2. start in IDE [F9] - LED does not react to button
3. start in IDE [F9], singlestepping [F8] - result OK
4. start in IDE without debugger [ctrl][shift][F9] - LED does not react to button

Cases 2 and 4 - running inside the IDE but not singlestepping [F8] through the code - are the problem.

GPIO group? Ah... "sudo adduser pi gpio" -> the user "pi" is already in the group "gpio"
So the answere is "yes".

I don't have a confident answer then. I've been looking at lower-level access to the GPIO bits over the last week or so, i.e. interacting directly with them at the /sys/class/gpio/* or /dev/gpiochip* level (there's two different APIs) but not using PascalIO... my major interest was trying to disentangle different behaviour on PCs and RPis.

I think one final thing I'd suggest is to add an APM like this

Code: Pascal  [Select][+][-]
  1. var
  2.   IOvalue : boolean;
  3.  
  4. procedure TForm1.Button1Click(Sender:TObject);
  5. var
  6.   pin: TGpioLinuxPin;
  7. begin
  8.   IOvalue := not IOvalue;
  9.   pin := TGpioLinuxPin.Create(12);
  10.   pin.Direction := gdOut;
  11.   pin.Value := IOvalue;
  12.   Application.ProcessMessages;
  13.   pin.Destroy
  14. end;
  15.  

...just in case there's something tricky about the way the component has been written.

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

GuidoJ

  • New Member
  • *
  • Posts: 14
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #6 on: March 02, 2021, 08:23:59 pm »
Nope, the APM did not work! I guess, I just have to learn more about rights and user levels in Linux.

My current project: Make my heating react to the weather forecast.

  • Get a forecast website via HTTP/HTTPS
  • Parse the website and extract the sunshine hours and temperature data (already running on windows)
  • Switch off the gas heater "a few hours" before the sun starts to shine and let the thermal solar system take over
  • ...using GPIO and maybe an optocoupler to signal the status to the heating
  • (later) readout the energy content of the solar tanks via 1-wire-bus.

Thanks, Mark, for your ideas.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2412
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #7 on: March 02, 2021, 08:52:54 pm »
I'm a bit bemused as to what's going on. At this point the normal thing would be for you to zip or tar your entire project and tack it onto a message so that somebody else could try it... I for one though aren't promising immediate time since tomorrow is going to be hard enough work as it is.

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

avra

  • Hero Member
  • *****
  • Posts: 2144
    • Additional info
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #8 on: March 03, 2021, 10:12:14 am »
1. console -> "sudo ./MyGPIOtest" - result OK
2. start in IDE [F9] - LED does not react to button
3. start in IDE [F9], singlestepping [F8] - result OK
4. start in IDE without debugger [ctrl][shift][F9] - LED does not react to button
Does "./MyGPIOtest" without sudo works? If not, then sudo gives some permissions that debugger already has, but IDE and simple command line do not have? Normally, gpio group should be enough, so you should investigate why sudo makes the difference in your case.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

GuidoJ

  • New Member
  • *
  • Posts: 14
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #9 on: March 03, 2021, 12:39:24 pm »
Thanks, avra!

Without sudo the LED does not react, so sudo (root rights) are needed. That is also stated in the documentation of the PascalIO Package.

I did some tracing an added some more code. The error occurs at FpOpen('/sys/class/gpio//gpio12/direction', O_WRONLY); Error 9: Bad file number.

I checked UserID, GroupID and ProcessID with and without debugging, but they where the same.

Is there a way to run the program from the IDE with root privileges? Maybe via "run"->"run parameters"->"launching application"?


MarkMLl

  • Hero Member
  • *****
  • Posts: 2412
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #10 on: March 03, 2021, 01:13:42 pm »
Without sudo the LED does not react, so sudo (root rights) are needed. That is also stated in the documentation of the PascalIO Package.

There's something wrong there, since you've already said that you're in the gpio group.

Quote
I did some tracing an added some more code. The error occurs at FpOpen('/sys/class/gpio//gpio12/direction', O_WRONLY); Error 9: Bad file number.

I checked UserID, GroupID and ProcessID with and without debugging, but they where the same.

Does that actually exist? I was looking at those devices in a slightly different context a few days ago https://forum.lazarus.freepascal.org/index.php/topic,53365.msg395471.html#msg395471 and there definitely wasn't a gpio12 on my Rpi.


# ls -l /sys/class/gpio


...the leading # is a convention that indicates that your shell login is root, you'll usually get there using sudo su or similar.

Quote
Is there a way to run the program from the IDE with root privileges? Maybe via "run"->"run parameters"->"launching application"?

Ouch. Short answer: no, longer answer (again, slightly different context) https://forum.lazarus.freepascal.org/index.php/topic,53541.msg396133.html#msg396133

What you can do however is run your program using gdbserver as root i.e. something like (in my case)


# gdbserver :2345 ./Watchxxx-x86_64-linux-gtk2


In the IDE you'll need Tools -> Options -> Debugger -> Debugger backend -> Debugger type and path set to gdbserver, and Debugger_Remote_Port set to 2345 to match the command. You might also need to install gdbserver if it's not on your system already... that's fairly foolproof but I've had the occasional problem with it at the GPIO level.

I've just tried using /usr/bin/sudo -A -k -E as the "Launching application" without success, even though that works as part of the build sequence.

I'm afraid I'm out shortly for a few hours so if you choose to try that I won't be able to advise until later.

MarkMLl
« Last Edit: March 03, 2021, 01:54:57 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

GuidoJ

  • New Member
  • *
  • Posts: 14
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #11 on: March 03, 2021, 04:13:24 pm »
Hi Mark!

That's quite a handful... but looking into remote debugging is on my list anyway  8)

Quote
Does that actually exist?
root@raspberrypi:/home/pi# ls -l /sys/class/gpio
insgesamt 0
-rwxrwx--- 1 root gpio 4096 Mär  3 13:09 export
lrwxrwxrwx 1 root gpio    0 Mär  3 13:09 gpiochip0 -> ../../devices/platform/soc/fe200000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root gpio    0 Mär  3 13:09 gpiochip504 -> ../../devices/platform/soc/soc:firmware/soc:firmware:gpio/gpio/gpiochip504
-rwxrwx--- 1 root gpio 4096 Mär  3 13:09 unexport

But, as the code works with root rights, the filename should be OK.

I will look into remote debugging tomorrow morning (that's CET).

Greetings from germany, Guido

MarkMLl

  • Hero Member
  • *****
  • Posts: 2412
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #12 on: March 03, 2021, 05:39:59 pm »
That's quite a handful... but looking into remote debugging is on my list anyway  8)

'Fraid so :-)

OK, I think I see one of the problems. With the important caveat that I've not looked at the specific library/package that you're using, I think you're missing a step. Watch:


markMLl@raspberrypi:/sys/class/gpio$ ls -l
total 0
-rwxrwx--- 1 root gpio 4096 Mar  3 16:21 export
lrwxrwxrwx 1 root gpio    0 Feb 26 15:11 gpiochip0 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root gpio    0 Feb 26 15:11 gpiochip100 -> ../../devices/gpiochip1/gpio/gpiochip100
lrwxrwxrwx 1 root gpio    0 Feb 26 15:11 gpiochip504 -> ../../devices/platform/soc/soc:firmware/soc:firmware:expgpio/gpio/gpiochip504
-rwxrwx--- 1 root gpio 4096 Mar  3 16:23 unexport

markMLl@raspberrypi:/sys/class/gpio$ echo 12 > export

markMLl@raspberrypi:/sys/class/gpio$ ls -l
total 0
-rwxrwx--- 1 root gpio 4096 Mar  3 16:24 export
lrwxrwxrwx 1 root gpio    0 Mar  3 16:24 gpio12 -> ../../devices/platform/soc/3f200000.gpio/gpiochip0/gpio/gpio12
lrwxrwxrwx 1 root gpio    0 Feb 26 15:11 gpiochip0 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root gpio    0 Feb 26 15:11 gpiochip100 -> ../../devices/gpiochip1/gpio/gpiochip100
lrwxrwxrwx 1 root gpio    0 Feb 26 15:11 gpiochip504 -> ../../devices/platform/soc/soc:firmware/soc:firmware:expgpio/gpio/gpiochip504
-rwxrwx--- 1 root gpio 4096 Mar  3 16:23 unexport

markMLl@raspberrypi:/sys/class/gpio$ ls -l gpio12/direction
-rwxrwx--- 1 root gpio 4096 Mar  3 16:24 gpio12/direction

markMLl@raspberrypi:/sys/class/gpio$ cat gpio12/direction
in


It's that echo 12 > export that's enabling gpio12 (an i/o line associated with the physical gpiochip0 device) in the context of the API that uses the pseudo-files in /sys/class/gpio.

As you'll see if you browse the "thread from Hell" that I linked to, the overall naming etc. is a problem.

You might find these useful if you start prodding the pseudo-files directly:

Code: Pascal  [Select][+][-]
  1. (* Read a single line from the designated file, trimming leading and trailing
  2.   whitespace from the result. Return a blank (zero-length) line on error.
  3. *)
  4. function Cat(const fn: string): string;
  5.  
  6. var
  7.   fd: text;
  8.  
  9. begin
  10.   result := '';
  11.   try
  12.     Assign(fd, fn);
  13.     Reset(fd);
  14.     try
  15.       try
  16.         Read(fd, result)
  17.       except
  18.         result := ''
  19.       end;
  20.       result := Trim(result)
  21.     finally
  22.       Close(fd)
  23.     end
  24.   except
  25.   end
  26. end { Cat } ;
  27.  
  28.  
  29. (* Send the string passed as the parameter, trimming trailing control characters
  30.   but no other whitespace, to the designated file; normally appending a single
  31.   newline as terminator. Return false on error.
  32. *)
  33. function Echo(str: string; const fn: string; noNl: boolean= false): boolean;
  34.  
  35. var
  36.   fd: text;
  37.  
  38. begin
  39.   result := true;
  40.   while (str <> '') and (str[Length(str)] < #$20) do
  41.     SetLength(str, Length(str) - 1);
  42.   try
  43.     Assign(fd, fn);
  44.     Rewrite(fd);
  45.     try
  46.       try
  47.         Write(fd, str);
  48.         if not noNl then
  49.           WriteLn(fd);
  50.       except
  51.         result := false
  52.       end
  53.     finally
  54.       Close(fd)
  55.     end
  56.   except
  57.     result := false
  58.   end
  59. end { Echo } ;
  60.  

I've not explored debugging this stuff on an RPi, and I suspect that you don't really need to use sudo because your normal user is in the gpio group. My rather rushed experiments with Lazarus+sudo+debugging on a PC earlier weren't particularly successful.

I can say however that using gdbserver is successful on a PC debugging stuff that needs elevated privilege to create low-numbered sockets (Linux is more retrictive that Windows in this area.

Hadjab 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

GuidoJ

  • New Member
  • *
  • Posts: 14
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #13 on: March 04, 2021, 08:35:47 am »
Hi Mark!

PascalIO does exactly what you suggested, only encapsulated in a lot of functions. They create and destroy the files in every call, therefore the file ../gpio12 is visible only if you stop the program at the correct moment. I did this and...

root@raspberrypi:/home/pi# ls -l /sys/class/gpio
insgesamt 0
-rwxrwx--- 1 root gpio 4096 Mär  3 16:34 export
lrwxrwxrwx 1 root gpio    0 Mär  4 08:12 gpio12 -> ../../devices/platform/soc/fe200000.gpio/gpiochip0/gpio/gpio12
lrwxrwxrwx 1 root gpio    0 Mär  3 16:34 gpiochip0 -> ../../devices/platform/soc/fe200000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root gpio    0 Mär  3 16:34 gpiochip504 -> ../../devices/platform/soc/soc:firmware/soc:firmware:gpio/gpio/gpiochip504
-rwxrwx--- 1 root gpio 4096 Mär  3 16:34 unexport


I found a workaround:
If I start the lazarus IDE as root my program works from within the IDE! Not a nice thing to do, but is does the trick.

Next stop "remote debugging"  ;)
...and maybe looking into some other GPIO packages.

MarkMLl

  • Hero Member
  • *****
  • Posts: 2412
Re: Raspbian: running a program using PascalIO from the IDE
« Reply #14 on: March 04, 2021, 08:51:04 am »
PascalIO does exactly what you suggested, only encapsulated in a lot of functions. They create and destroy the files in every call, therefore the file ../gpio12 is visible only if you stop the program at the correct moment. I did this and...

That's lousy design IMO. Apart from anything else it wastes a lot of processor time parsing names in the /sys/class/gpio tree... but what do I know :-)

If you look for example at https://elinux.org/RPi_GPIO_Code_Samples which is a well-respected site, you will see that while programs that start and then terminate (e.g. C run from the command line) do a single-shot state change and then unexport on termination, the Lazarus/FPC example exports the pin at startup, changes state as required, and keeps hold of the pin as long as the GUI program is running.

There are other implications to device/pin ownership and direction, and it's entirely possible to write a program that changes its UI based on which pins are instantaneously input and which are output (and which are neither, because they've not been exported).

Quote
If I start the lazarus IDE as root my program works from within the IDE! Not a nice thing to do, but is does the trick.

It should definitely not be necessary to do that. I'll try to take a look at it a bit later in the day.

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

 

TinyPortal © 2005-2018