* * *

Author Topic: AV in the PiGpio Low-level native pascal unit example in the Lazarus on Raspberr  (Read 441 times)

MartynA

  • New member
  • *
  • Posts: 17
I'm running Lazarus 1.6.2 on a recently set-up RPi 3.

I have cut'n pasted the code from the PiGpio Low-level native pascal unit (GPIO control instead of wiringPi c library) section on this page:http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#PiGpio_Low-level_native_pascal_unit_.28GPIO_control_instead_of_wiringPi_c_library.29

into a new Lazarus project without making any changes.  Every time I run it, I get a SIGSEGV exception on the line

  gpiof := Pointer(LongWord(Self.FGpio) + fSel);

which is the 3rd line of TIOPort.SetPinMode, which is being called from Form1.Activate.

Can anyone reproduce this and suggest a fix/workaraound?

TIA, Martyn


rvk

  • Hero Member
  • *****
  • Posts: 2974
Are you running this as root?

If not, did you set the correct permissions for /dev/mem ?
/dev/mem doesn't have the same permissions as the /dev/gpiomem.

MartynA

  • New member
  • *
  • Posts: 17
Not knowingly, just running it from the Lazarus IDE. 

>If not, did you set the correct permissions for /dev/mem ?

Err, no.  What are they supposed to be and how would I set them?

Btw, I noticed that this section, unlike the ones above it in the article, didn't say anything about needing to run as root.  So if that is what's needed, I think the article could do with a minor addition to say so and how to do it.

Cheers, Martyn

rvk

  • Hero Member
  • *****
  • Posts: 2974
Not knowingly, just running it from the Lazarus IDE.
Then you're probably not running it as root.

You can compile the program and run it as root.
Or run lazarus itself as root.

MartynA

  • New member
  • *
  • Posts: 17
Well, the app will run from the Terminal window using SUDO but I would really rather not have to run the app with root privileges - there are warnings all over the place about not running a gui app that way from the pov of security.

In any case, it seems scarcely credible to me that the RPi - intended for experimenters - seemingly doesn't support using the IO pins without requiring root privs.  I've tried the various suggestions here: https://raspberrypi.stackexchange.com/questions/40105/access-gpio-pins-without-root-no-access-to-dev-mem-try-running-as-root (inccluding installing the updates Joan suggests) but they do not avoid the AV I mentioned in my first post. 

Btw there seems to be a serious omission from the example in the article that I cut'n pasted the code from in that the code omits to create the GPIO_Driver object before attempting to use it.  Even with that corrected, the example still fails the call to GPIO_Driver.MapIo because the call to fpopen('/dev/mem ...) returns -1.

Any ideas?

Cheers, Martyn

rvk

  • Hero Member
  • *****
  • Posts: 2974
I'm not 100% sure the GPIO_Driver needs to be "created".
There are no class functions and procedures but there are also no properties.
So you might get away with not creating the object and just using the functions as is.

Then the problem with /dev/mem.
You say you don't want to give pi SUDO rights or run your program as root.
But... you are trying to access /dev/mem. THAT's the entire memory space. You would need to have full access to ALL that memory. Running without it you have no way to write to /dev/mem. So either you give permission or you don't use /dev/mem. Simple as that.

In the link Joan suggest using /dev/gpiomem. This is a separate entryway to the gpio pins without giving access to the full /dev/mem memory. But that doesn't work in the examples where you do want to write to /dev/mem. The answer of Karl in that link explains it in more detail.

On my pi the user pi was automatically added to the group gpio. So I didn't have to add it.

The first example (where the files to /sys/class/gpio are created) is for access to the pins for non-root users. For direct pin access via /dev/mem you need to have the correct permissions (which user pi default doesn't have). Karl explains that in his answer but it is not advisable as you already have found on the net.

I'm not sure if you can do something directly with /dev/gpiomem itself but I haven't found much about that.
« Last Edit: December 08, 2017, 11:10:03 pm by rvk »

molly

  • Hero Member
  • *****
  • Posts: 2037
@rvk / MartynA:
perhaps i'm daft, but i ran into this thread, which assumes user is member of group gpio, and action is performed on gpiomem. So adjust the example code from wiki accordingly ?

rvk

  • Hero Member
  • *****
  • Posts: 2974
I'm not sure if you can do something directly with /dev/gpiomem itself but I haven't found much about that.
perhaps i'm daft, but i ran into this thread, which assumes user is member of group gpio, and action is performed on gpiomem. So adjust the example code from wiki accordingly ?

So, you did find it  :D

Yes. I can't try it right now but it looks promising that you can just change /dev/mem to /dev/gpiomem.

molly

  • Hero Member
  • *****
  • Posts: 2037
So, you did find it  :D
Lucky shot in the dark  :)

If that is not what TS seeks, then i'm overly confused because this library seems originally a c library. That needs to run (or at least can run) as root daemon and adds f.i. a pipe to /dev/pigpio. As far as i am able to tell there is no such sign in the pascal version. So if you want to have it all then act like all (=root)  :)

Other then that the Pascal implementation does not seem to have any resemblance with the mentioned c library (no matter pascal version being encapsulated in classes).

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus