Discussion:
[Lazarus] Raspberry Pi Debug Digital I/O
Donald Ziesig
2016-01-23 01:55:59 UTC
Permalink
Hi All!

This applies to the Raspberry Pi 2 B.

After a bit of cut and try, I got Anthony Walter's version of Lazarus to
run as root (from the Raspbian menu) so I could debug programs which do
DigitalRead, DigitalWrite, etc. using h2wiringPi.

Note that this could be dangerous if you are permanently connected to
the web while using Lazarus, but in my case, the only time that my Pi
will be connected to the web is when I am getting Lazarus set up (and it
is not yet running as root). Thereafter I will be connected directly
from Pi to Laptop on a private subnet with no router. Once the app is
developed the Pi will not be networked at all.

Incidentally, I got the Pi setup so that if it fails DHCP (is not
connected to a router) it reverts to the private subnet. If anyone
wants to do this, let me know and I'll publish the "magic incantations".
;-) It is much more convenient than editing /etc/network/interfaces
every time you need to change from accessing the web to a local private
subnet.

The application itself must run as root to do digital I/O. If you don't
want to debug using the IDE, you don't have to make the IDE run as root,
though, just the app.

Using Anthony's default paths, I created a file in
/home/<mydir>/Development/FreePascal/lazarus called lazroot.sh This
file contains two lines:

xhost +localhost
sudo /home/<mydir>/Development/FreePascal/lazarus/lazarus.sh

Then chmod +x lazroot.sh

I used the Menu > Preferences > Main Menu Editor to change the command
which starts Lazarus to:

/home/<mydir>/Development/FreePascal/lazarus/lazroot.sh

Obviously <mydir> is the directory where lazarus is located.

At first I tried to change the Menu command to the two lines in the
lazroot.sh file, but the internals of the menu system did not support
multi-command arguments.

For some reason, Lazarus re-built itself (as if I were installing
components) the very first time I ran it as root. After that it starts
normally.

Hope this helps someone in return for all the help I have been given here.

Don Ziesig



--
Bo Berglund
2016-01-24 10:47:42 UTC
Permalink
Post by Donald Ziesig
The application itself must run as root to do digital I/O. If you don't
want to debug using the IDE, you don't have to make the IDE run as root,
though, just the app.
I do not think this is necessary!

I am NOT running my RPi2B as any other user than pi and it works just
fine to access the GPIO pins. Both FPC and Lazarus run just fine as pi
user.

The trick is to make sure that pi belongs to the proper groups dealing
with the hardware. You can check this using the command:
cat /etc/group | grep pi

It will list the groups pi belongs to and you will find out if there
are missing groups here. This is what my Pi says:

***@rpi2-jessie2:~/dev $ cat /etc/group | grep pi
adm:x:4:pi
dialout:x:20:pi
cdrom:x:24:pi
sudo:x:27:pi
audio:x:29:pi
video:x:44:pi
plugdev:x:46:pi
games:x:60:pi
users:x:100:pi
input:x:101:pi
netdev:x:108:pi
spi:x:999:pi
i2c:x:998:pi
gpio:x:997:pi <== This for GPIO

I never use the NOOBS disk, I always create the start image from the
offiocial Raspbian Jessie image, currently I am on the Nov 2015
release. And the groups above were set from the start on that distro.
--
Bo Berglund
Developer in Sweden


--
Bo Berglund
2016-01-24 20:38:16 UTC
Permalink
begin 644 PiGpio.zip
M4$L#!!0``@`(`..:*DBT1R3E"@L``%\E```*````4&E'<&EO+G!A<\U:;4_;
MRA+^W$K]#U,)Z22G*;&=0(&<'IU`$AH52)2$(MT/C9QX#;YU;,MVH+3BO]]G
M=NVU"8'"%>7>B`9[9W;>=W9FZ#+P4AIZAY$7MMZ\_OGF->T?'%L[C2TZ'/8'
M-!+G7I+&U]2)O4L1U\CVDY#F=D#+1%`:TL(.O&CIVZF@>;2D,+T0,<7Y+CL6
M=O+F-:A.+KR$YJ$C"+]3D:3"H3#PKVD_#FUG'BYH-E](OB!3(\=S71&+(`6)
M!2\EX'1-@<`V#0/9<\A-CI2-O$7DBP76[=0+`\G5,LP&'=JS,*;QC]#WP\1[
M\_H&>FXL6)1P]F\WFM_\W/CT[H;QO2`5L6O/!;_4ZQ!:D,O;***@G,*(Z4*)
M((U#/Z&K"R&UQ9?6C'$6W@](Z8+GR$ZBF8AAB&&?K$U)L^TX@$*PK?>***@1^:
MP08A[8OXW%\&3HG?E9B1[5QZ<VGH<Y$27'45QM^D,('D._0LNK!CYPJ69K4Z
MW5[_I$LCK$N-X"78G^C`MQ,\UFA\G9RFGI^T&`I5$K8BC;J'4^GOC_1SH]\#
M%46"-AH]PS"PV#T:=_%J&=GK2:??NVG5ZYG7LM_;RB'*_S"-\9TW$&_:A/.N
MR(V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ZE8I!+^O57\_=KVY/3]>U7+S*S:3N
MTK2?[ZO<=U"Y`YUWY3>'_$-H4;PW4+SKVK[`,U?PY+$0W[VT=+$\ZC*#5Z-L
M+E6ZPY3B?"Q5%;_?Z\E>6=8=<]U7_487@;#\+QK_`5!+`0(4`!0``@`(`..:
M*DBT1R3E"@L``%\E```*`"0```````$`(`````````!0:4=P:6\N<&%S"@`@
M```````!`!@`@(J-\M-+T0&`BHWRTTO1`1#[DX;42]$!4$L%!@`````!``$`
*7````#(+`````(J-
`
end


--
Bo Berglund
2016-01-24 20:38:15 UTC
Permalink
On Sun, 24 Jan 2016 20:59:40 +0100, Koenraad Lelong
Post by Bo Berglund
Post by Donald Ziesig
The application itself must run as root to do digital I/O. If you don't
want to debug using the IDE, you don't have to make the IDE run as root,
though, just the app.
I do not think this is necessary!
Strange,
I tried this with my test-application and it does not work as user 'pi'.
On your Pi, what groups are user pi member of?
...
B.T.W. what 'library' are you using for the GPIO's ?
I have used PiGpio.pas, I will try to attach it - don't really know
how one does thios in a newsreader...
--
Bo Berglund
Developer in Sweden


--
Marc Santhoff
2016-01-24 20:42:06 UTC
Permalink
/sys/class/gpio
totaal 0
-rwxrwx--- 1 root gpio 4096 jan 22 22:03 export
lrwxrwxrwx 1 root gpio 0 jan 24 19:43 gpio17 ->
../../devices/platform/soc/3f200000.gpio/gpio/gpio17
lrwxrwxrwx 1 root gpio 0 jan 24 19:43 gpio18 ->
../../devices/platform/soc/3f200000.gpio/gpio/gpio18
lrwxrwxrwx 1 root gpio 0 jan 22 22:03 gpiochip0 ->
../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0
-rwxrwx--- 1 root gpio 4096 jan 22 22:03 unexport
But the IO does not work. When I do
echo 1 > /sys/class/gpio/gpio17/value
from the command-line, the IO does not work, even when I do this as root.
When I run the application as root, everything works as expected.
What do

$ cat /etc/group | grep gpio

and

$ ls -lL /sys/class/gpio

show?
--
Marc Santhoff <***@web.de>


--
Bo Berglund
2016-01-24 23:11:06 UTC
Permalink
Post by Marc Santhoff
What do
$ cat /etc/group | grep gpio
and
$ ls -lL /sys/class/gpio
show?
For me it is like this:
***@rpi2-jessie2:~/dev $ cat /etc/group | grep gpio
gpio:x:997:pi
***@rpi2-jessie2:~/dev $ ls -lL /sys/class/gpio
total 0
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 export
drwxrwx--- 2 root gpio 0 Jan 17 16:47 gpiochip0
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 unexport
--
Bo Berglund
Developer in Sweden


--
Marc Santhoff
2016-01-24 23:18:50 UTC
Permalink
Post by Bo Berglund
Post by Marc Santhoff
What do
$ cat /etc/group | grep gpio
and
$ ls -lL /sys/class/gpio
show?
gpio:x:997:pi
Thje last one wrong, should have been

$ ls -lL /sys/class/gpio/gpiochip0

and

$ ls -lL ../../devices/platform/soc/3f200000.gpio/gpio/

to see the the targets of the links (correct permissions there, too?)
and content of the directory, sorry.
Post by Bo Berglund
total 0
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 export
drwxrwx--- 2 root gpio 0 Jan 17 16:47 gpiochip0
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 unexport
--
Marc Santhoff <***@web.de>


--
Bo Berglund
2016-01-25 07:33:25 UTC
Permalink
Post by Marc Santhoff
Post by Bo Berglund
gpio:x:997:pi
Thje last one wrong, should have been
$ ls -lL /sys/class/gpio/gpiochip0
New test:

***@rpi2-jessie2:~/dev $ ls -lL /sys/class/gpio/gpiochip0
total 0
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 base
drwxr-xr-x 3 root root 0 Jan 17 16:47 device
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 label
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 ngpio
drwxrwx--- 2 root gpio 0 Jan 17 16:47 subsystem
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 uevent
Post by Marc Santhoff
and
$ ls -lL ../../devices/platform/soc/3f200000.gpio/gpio/
This is a relative path and you did not specify where one should be
located in order to execute it. Cannot perform this command.
Please supply an absolute path instead.
--
Bo Berglund
Developer in Sweden


--
Marc Santhoff
2016-01-25 16:11:37 UTC
Permalink
Post by Bo Berglund
Post by Marc Santhoff
Post by Bo Berglund
gpio:x:997:pi
Thje last one wrong, should have been
$ ls -lL /sys/class/gpio/gpiochip0
total 0
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 base
drwxr-xr-x 3 root root 0 Jan 17 16:47 device
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 label
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 ngpio
drwxrwx--- 2 root gpio 0 Jan 17 16:47 subsystem
-rwxrwx--- 1 root gpio 4096 Jan 17 16:47 uevent
Post by Marc Santhoff
and
$ ls -lL ../../devices/platform/soc/3f200000.gpio/gpio/
This is a relative path and you did not specify where one should be
located in order to execute it. Cannot perform this command.
Please supply an absolute path instead.
I know. The user can see from where links are not resolved by reading
the failing command. My point was to help diagnosing why a program
running as user pi did not work. I explained with the changed command,
for checking permissions on the targets of the links, not the links
themselves.

But since you found the cause by your patch differentiating between RPi1
and RPi2 it's obsolete now.
--
Marc Santhoff <***@web.de>


--
Donald Ziesig
2016-01-25 03:02:49 UTC
Permalink
Post by Bo Berglund
Post by Donald Ziesig
The application itself must run as root to do digital I/O. If you don't
want to debug using the IDE, you don't have to make the IDE run as root,
though, just the app.
I do not think this is necessary!
I am NOT running my RPi2B as any other user than pi and it works just
fine to access the GPIO pins. Both FPC and Lazarus run just fine as pi
user.
The trick is to make sure that pi belongs to the proper groups dealing
cat /etc/group | grep pi
It will list the groups pi belongs to and you will find out if there
adm:x:4:pi
dialout:x:20:pi
cdrom:x:24:pi
sudo:x:27:pi
audio:x:29:pi
video:x:44:pi
plugdev:x:46:pi
games:x:60:pi
users:x:100:pi
input:x:101:pi
netdev:x:108:pi
spi:x:999:pi
i2c:x:998:pi
gpio:x:997:pi <== This for GPIO
Hi Bo,

I get exactly this on my pi except there is a line:

pi:x:1000:

immediately following the netdev line..
Post by Bo Berglund
I never use the NOOBS disk, I always create the start image from the
offiocial Raspbian Jessie image, currently I am on the Nov 2015
release. And the groups above were set from the start on that distro.
I updated the copy of NOOBS to use jessie as soon as jessie was available.
I updated FPC and Lazarus from Anthony's download (as he corrected it
for the problem with installing custom components) as soon as it was
available.

When I run my program unprivileged from the command line I get:

wiringPiSetup: Must be root. (did you forget sudo?).

Using the same executable but prefacing the command with sudo, it runs
and does digital I/O.

When I try to run my program using unprivileged Lazarus it compiles but
the Debugger crashes.

When I run my program using root-mode Lazarus it compiles, runs and does
digital I/O.

The comments in h2WiringPi state that the programs that use it must run
as root, which is how I got to this position in the first place.

Don




--
Bo Berglund
2016-01-25 07:43:38 UTC
Permalink
Post by Donald Ziesig
immediately following the netdev line..
I edited out that because the grep found pi in the wrong place...
Post by Donald Ziesig
wiringPiSetup: Must be root. (did you forget sudo?).
Using the same executable but prefacing the command with sudo, it runs
and does digital I/O.
When I try to run my program using unprivileged Lazarus it compiles but
the Debugger crashes.
When I run my program using root-mode Lazarus it compiles, runs and does
digital I/O.
The comments in h2WiringPi state that the programs that use it must run
as root, which is how I got to this position in the first place.
I solved my root/pi problem back in Oct 2015.
You can read about it here:
http://lists.lazarus.freepascal.org/pipermail/lazarus/2015-October/094438.html

Specifically see how the change from using /dev/mem to /dev/gpiomem
made it possible to run as user pi.

I personally totally detest having to be root in a development system
and having my programs require root to run.
I don't have to anymore.
--
Bo Berglund
Developer in Sweden


--
Loading...