Recent

Author Topic: Problem running example on Raspberry PI  (Read 8678 times)

MartynA

  • New Member
  • *
  • Posts: 19
Problem running example on Raspberry PI
« on: December 04, 2017, 08:07:51 pm »
Hi,

I've been using Lazarus on Win10 for a while, and yesterday set up Lazarus on a Raspberry PI 3.  Compiling and running simpe apps works fine but I'm having a problem running the example in the section Switching a device via the GPIO port of this article:

http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#PiGpio_Low-level_native_pascal_unit_.28GPIO_control_instead_of_wiringPi_c_library.29

The problem I get is that all the fpwrite() calls return -1, and the app fails to toggle an LED I've wired between pin 11 and one of the ground pins via a series resistor.  However, if I run it via sudo in a terminal window, the app works fine and the LED turn on and off as it should.

I want to get the app to run without the need to use sudo.  I've seen the note in the article section about the need to execute the app as root OR to "A better option is to add the user to the gpio group, the i2c group and the spi group. ".

I tried the adduser calls

sudo adduser pi gpio
sudo adduser pi i2c
sudo adduser pi spi

as the article suggests before I began setting up the app, but they all returned a msg like "The user  pi is already a member of the 'gpio' group'" (and the other two groups).

So, any suggestions how to get the app to work without running it via sudo in the terminal window?

Btw,  this is on a newly installed Raspbian - all I did after its self-set-up was to use apt-get to install fpc, lazarus, sqlite3 and libsqlite3-dev.

TIA, Martyn
« Last Edit: December 04, 2017, 10:24:19 pm by MartynA »

mas steindorff

  • Hero Member
  • *****
  • Posts: 532
Re: Problem running example on Raspberry PI
« Reply #1 on: December 04, 2017, 08:29:46 pm »
I can not help you on the software side but on the hardware... one should not directly connect a LED to a CPU pin.  you need to have a resistor in the path as well to prevent the CPU from resetting (short story). 
just in case you forgot... 
windows 10 &11, Ubuntu 21+ - fpc 3.0.4, IDE 2.0 general releases

MartynA

  • New Member
  • *
  • Posts: 19
Re: Problem running example on Raspberry PI
« Reply #2 on: December 04, 2017, 08:38:24 pm »
Thanks, but I didn't forget - I soldered a 680 ohm resistor in series with the LED's anode.  It's as well to remind readers of the need for one.

Cheers, Martyn

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Problem running example on Raspberry PI
« Reply #3 on: December 04, 2017, 09:25:15 pm »
Check that /dev/gpiomem has the correct permissions with
ls -l /dev/gpiomem

If it doesn't have group access for read and write for group gpio, fix it.
sudo chown root.gpio /dev/gpiomem
sudo chmod g+rw /dev/gpiomem

MartynA

  • New Member
  • *
  • Posts: 19
Re: Problem running example on Raspberry PI
« Reply #4 on: December 04, 2017, 09:52:47 pm »
Thanks.  I'm not sure what the correct permissions are supposed to be, but

  ls -l /dev/gpiomem

returned

  crw-rw----l root gpio 244, 0 Dec  4 18:09 /dev/gpiomem

I ran the two following commands you listed, rebooted the system, restarted Lazarus,  and ran the app from within the IDE but I'm afraid the app still  behaves as I initially described:  the fpwrites() still return -1 and the LED doesn't toggle.

Cheers, Martyn

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Problem running example on Raspberry PI
« Reply #5 on: December 04, 2017, 10:03:43 pm »
  crw-rw----l root gpio 244, 0 Dec  4 18:09 /dev/gpiomem
That should be correct.

What does this give you?
ls /sys/class/gpio -l

MartynA

  • New Member
  • *
  • Posts: 19
Re: Problem running example on Raspberry PI
« Reply #6 on: December 04, 2017, 10:19:58 pm »
I get this

pi@raspberrypi:~ $ ls /sys/class/gpio -l
total 0
-rwxrwx--- 1 root gpio 4096 Dec  4 20:47 export
lrwxrwxrwx 1 root gpio    0 Dec  4 20:47 gpiochip0 -> ../../devices/platform/soc/3f200000.gpio/gpio/gpiochip0
lrwxrwxrwx 1 root gpio    0 Dec  4 20:47 gpiochip100 -> ../../devices/gpiochip2/gpio/gpiochip100
lrwxrwxrwx 1 root gpio    0 Dec  4 20:47 gpiochip128 -> ../../devices/gpiochip1/gpio/gpiochip128
-rwxrwx--- 1 root gpio 4096 Dec  4 20:47 unexport

Btw, I should probably have mentioned in my q that this is on a newly installed Raspbian - all I did after its self-set-up was to use apt-get to install fpc, lazarus, sqlite3 and libsqlite3-dev.

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Problem running example on Raspberry PI
« Reply #7 on: December 04, 2017, 10:25:56 pm »
Can you toggle the led manually as user?

Like the way on this page
http://raspberrypi.tomasgreno.cz/setup-gpio.html

But then with GPIO17 (for pin 11):
echo 17 > /sys/class/gpio/export
echo out > /sys/class/gpio/gpio17/direction
echo 1 > /sys/class/gpio/gpio17/value

and release the pin
echo 17 > /sys/class/gpio/unexport
« Last Edit: December 04, 2017, 10:28:16 pm by rvk »

MartynA

  • New Member
  • *
  • Posts: 19
Re: Problem running example on Raspberry PI
« Reply #8 on: December 04, 2017, 10:38:29 pm »
Yes, those turn the LED on. (and off using 'echo 0 > ...')

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Problem running example on Raspberry PI
« Reply #9 on: December 04, 2017, 10:47:32 pm »
Yes, those turn the LED on. (and off using 'echo 0 > ...')
As user pi?
Then it's not a permission problem.

What does fpgeterrno give you after getting -1 for fpwrite?

What example exactly did you use? (the first one?)

You could try the shell method.
Under "Hardware access via encapsulated shell calls"

MartynA

  • New Member
  • *
  • Posts: 19
Re: Problem running example on Raspberry PI
« Reply #10 on: December 05, 2017, 05:52:25 pm »
Sorry for the delay replying ...

>What does fpgeterrno give you after getting -1 for fpwrite?

The results I get from all the calls are s follows:

fpwrite(fileDesc, PIN_17[0], 2) returned 2  fpgeterrno:0
fpclose(fileDesc) returned 0  fpgeterrno:0
fpwrite(fileDesc,OUT_DIRECTION[0], 3) returned o -1  fpgeterrno:9
fpclose(fileDesc) returned -1  fpgeterrno:9

>What example exactly did you use? (the first one?)

Yes, the first one.

>You could try the shell method.
>Under "Hardware access via encapsulated shell calls"

I tried that, hoping my success with the Terminal example you pointed me at would repeat itself.  Unfortunately the app under "Hardware access via encapsulated shell calls"  behaves erratically.  One time out of about 10, the first call, i.e to "export", returns 0 and the the rest of the app behaves as expected.  The other ~9 times, the first call returns 512 and the subsequent calls to set the direction and set port 17 to on, return 256, and the LED stays off.  There are two odd things about this:

- The misbehaviour persists across cold-starting the Pi (i.e. disconnecting the power during a shutdown/reboot cycle).  Do the GPIO settings persist across a cold-restart?

- Sometimes executing the commands from the terminal article clear the problem, but other times not.

Update:  As with the first app I tried, the EncapsulatedShell one works fine if run from a terminal window using sudo.  I know virtually nothing about Linux but if it's not a permissions issue, maybe it's to do with the app seeing a different execution environment when run in the Terminal as compared with the desktop or Lazarus?

Does any of that give you any clues?  It seems to me as if some other process is interfering with the port settings, but I only have Lazarus and the app under test running, in the gui at any rate.
« Last Edit: December 05, 2017, 06:44:42 pm by MartynA »

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Problem running example on Raspberry PI
« Reply #11 on: December 05, 2017, 07:47:30 pm »
I tried that, hoping my success with the Terminal example you pointed me at would repeat itself.  Unfortunately the app under "Hardware access via encapsulated shell calls"  behaves erratically.  One time out of about 10, the first call, i.e to "export", returns 0 and the the rest of the app behaves as expected.  The other ~9 times, the first call returns 512 and the subsequent calls to set the direction and set port 17 to on, return 256, and the LED stays off.  There are two odd things about this:
When the first call to export fails, you shouldn't execute the others.
echo 17 > export creates the gpio17 directory with correct permissions
echo 17 > unexport removes it.
So if > export fails nothing else matters (and will fail).

Not sure what 512 as error is yet.

But for the most part the shell commands work for you.

The reason it could be unstable is if you open the port and the port stays in use. In that case using the port again might give problems.

I can't test right now. I dusted off an old RPI3 and installed Rasbian Stretch but installing trunk gave me trouble. Something in the compile of Lazarus with invalid opcode. Not sure if it's a user-error or trunk error yet.

Now checking out 1.6.4 (but haven't much time tonight).
Mmm, 1.6.4 doesn't work with new fpc trunk.
Checking out 1.8.0 RC5.
« Last Edit: December 05, 2017, 07:49:44 pm by rvk »

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Problem running example on Raspberry PI
« Reply #12 on: December 06, 2017, 12:01:15 am »
fpwrite(fileDesc, PIN_17[0], 2) returned 2  fpgeterrno:0
fpclose(fileDesc) returned 0  fpgeterrno:0
fpwrite(fileDesc,OUT_DIRECTION[0], 3) returned o -1  fpgeterrno:9
fpclose(fileDesc) returned -1  fpgeterrno:9
Ok, I did some testing.

I had the same result as you. The fpopen on export works correctly and the next fpWrite returns 2. That's also correct because the error is 0 and you have written 2 bytes (characters 1 and 7). fpClose also wasn't a problem.

Next you get an error (permission denied) on fpOpen on gpio17/direction. After that you can go on but you'll get Bad file number because fpOpen wasn't successful.

I have noticed that the gpio17 directory was created with the correct permissions so I was puzzled why it didn't work.

Then I put in some logging and it worked  %) %)

I traced the problem to accessing the direction file too fast after putting 17 in export.
The OS hasn't fully created the gpio17 directory yet when you try to open the direction file inside it !  :P

So, even if you put a Application.ProcessMessages or a Sleep(1000) just before opening the direction-file it should all work correctly.

Could you test that in your version?

MartynA

  • New Member
  • *
  • Posts: 19
Re: Problem running example on Raspberry PI
« Reply #13 on: December 06, 2017, 12:24:10 am »
Thanks,  I really appreciate the effort you've put into this.

I confess I'm a bit Application.ProcessMessages-averse but, yes, that avoids the problem and so does Sleep(100) (I didn't need 1000 mS).

It's a bit late here (UK), so I'll carry on testing tomorrow, and then go back to the first example and see whether your fix does anything for that, too.

Later, Martyn

rvk

  • Hero Member
  • *****
  • Posts: 6112
Re: Problem running example on Raspberry PI
« Reply #14 on: December 06, 2017, 12:30:15 am »
At least we're close to a solution. I wonder why the delay wasn't build in the example. The example could use some extra error logging too. I will change that when we know for sure that's also the problem on your end.

It's late here too (NL) so sleep well.

 

TinyPortal © 2005-2018