Forum > Debugger

Raspbian: running a program using PascalIO from the IDE

<< < (3/6) > >>

MarkMLl:

--- Quote from: GuidoJ on March 03, 2021, 12:39:24 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.

--- End quote ---

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.

--- End quote ---

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"?

--- End quote ---

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

GuidoJ:
Hi Mark!

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


--- Quote ---Does that actually exist?
--- End quote ---
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:

--- Quote from: GuidoJ on March 03, 2021, 04:13:24 pm ---That's quite a handful... but looking into remote debugging is on my list anyway  8)

--- End quote ---

'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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---(* Read a single line from the designated file, trimming leading and trailing  whitespace from the result. Return a blank (zero-length) line on error.*)function Cat(const fn: string): string; var  fd: text; begin  result := '';  try    Assign(fd, fn);    Reset(fd);    try      try        Read(fd, result)      except        result := ''      end;      result := Trim(result)    finally      Close(fd)    end  except  endend { Cat } ;  (* Send the string passed as the parameter, trimming trailing control characters  but no other whitespace, to the designated file; normally appending a single  newline as terminator. Return false on error.*)function Echo(str: string; const fn: string; noNl: boolean= false): boolean; var  fd: text; begin  result := true;  while (str <> '') and (str[Length(str)] < #$20) do    SetLength(str, Length(str) - 1);  try    Assign(fd, fn);    Rewrite(fd);    try      try        Write(fd, str);        if not noNl then          WriteLn(fd);      except        result := false      end    finally      Close(fd)    end  except    result := false  endend { Echo } ; 
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

GuidoJ:
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:

--- Quote from: GuidoJ on March 04, 2021, 08:35:47 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...

--- End quote ---

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.

--- End quote ---

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

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version