Recent

Author Topic: How to access GPIO on RPi  (Read 7178 times)

Koot33

  • Newbie
  • Posts: 4
How to access GPIO on RPi
« on: January 25, 2021, 04:18:08 am »
Hi,

After 35 years avoiding Linux I decided to try using a RPi for my current project. Not least because I have been using Pascal for a long time and Lazarus has come through for me in the past. I need to access the gpio pins and I installed the Asphyre library because it seems well recommended. I opened the Blinky project and it compiled with no trouble. But, when I run it, it exits immediately. I have seen that I must do something in the OS to  tell the rest of the system that I want the GPIO's. Unfortunately, either I'm dense (I am getting old) or they are not explaining it well because despite reading a lot of posts I still don't know what to do.

Can anyone point me to the right source or even just explain it to me?

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: How to access GPIO on RPi
« Reply #1 on: January 25, 2021, 09:03:51 am »
You did read about the various alternatives mentioned here?
Nevertheless it would be useful for you to explain what you tried and what step didn't work then.

And just to be sure: you're using Linux or are you working bare metal?

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: How to access GPIO on RPi
« Reply #2 on: January 25, 2021, 09:48:12 am »
The wiki contains many solutions, some simple, some less so and some need an update for different Raspberry Pi models. All work, though, The most complex and best only on older models, but just requires to map the GPIO start adress correctly.  (It works with 4096 size pages)

Start here: https://wiki.freepascal.org/Lazarus_on_Raspberry_Pi
« Last Edit: January 25, 2021, 09:50:40 am by Thaddy »
Specialize a type, not a var.

Koot33

  • Newbie
  • Posts: 4
Re: How to access GPIO on RPi
« Reply #3 on: January 25, 2021, 10:31:04 am »
As I said, I have compiled the "Blinky" example that comes with the "PXL" library so I now have a file named "Blinky" in the "...Documents/Lazarus/PXL/Samples/Embedded/Blinky" directory. If I try to run it in the Lazarus IDE there is a black flash and Lazarus reports that execution is done.

I then opened a Terminal window and tried using "sudo Blinky" after navigating to the right directory. It says "Blinky is not a command"

As usual it helps to discuss your problems. I looked again at the "Lazarus on Raspberry PI" page and I see something I didn't see before:
 
  sudo adduser pi gpio
  sudo adduser pi i2c
  sudo adduser pi spi

I'll try this in the morning, I mean, when I get up, it's 2:30AM right now so I don't feel like going to my office to try it (it's also -13C out there). Maybe I will since I can't sleep, I'll let you know if I need more help. Thanks.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: How to access GPIO on RPi
« Reply #4 on: January 25, 2021, 11:10:38 am »
Go to the directory where you compiled blinky to and do ./blinky or sudo ./blinky ( I run GPIO code under group rights, more or less as per the above )
Also note the convention is all lowercase, make sure that you keep to that. (Linux will not even execute Blinky if the binary is blinky, contrary to windows and dos)

PS. It is only -5 C, here..
« Last Edit: January 25, 2021, 11:16:56 am by Thaddy »
Specialize a type, not a var.

avra

  • Hero Member
  • *****
  • Posts: 2514
    • Additional info
Re: How to access GPIO on RPi
« Reply #5 on: January 25, 2021, 01:35:40 pm »
Go to the directory where you compiled blinky to and do ./blinky or sudo ./blinky
According to https://sourceforge.net/p/asphyre/code/HEAD/tree/trunk/Samples/FreePascal/SingleBoard/RaspberryPI/Blinky/ it seams that Blinkey example should be typed with capital B. Not ideal but that's the way it is.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Koot33

  • Newbie
  • Posts: 4
Re: How to access GPIO on RPi
« Reply #6 on: January 25, 2021, 08:23:30 pm »
Go to the directory where you compiled blinky to and do ./blinky or sudo ./blinky ( I run GPIO code under group rights, more or less as per the above )
Also note the convention is all lowercase, make sure that you keep to that. (Linux will not even execute Blinky if the binary is blinky, contrary to windows and dos)

PS. It is only -5 C, here..

OMG!
I knew it was something stupid like that. Thanks!!!

Koot33

  • Newbie
  • Posts: 4
Re: How to access GPIO on RPi
« Reply #7 on: January 25, 2021, 08:28:48 pm »
Running
  sudo adduser pi gpio
  sudo adduser pi i2c
  sudo adduser pi spi

Gave me "pi is already a user of xx"

However, I did find this information at some point in the night and emailed it to myself:
Quote
I don't know if this is too late, but in Free Pascal I changed
fd := fpopen('/dev/mem', O_RdWr or O_Sync);
to
fd := fpopen('/dev/gpiomem', O_RdWr or O_Sync);
and that solved the problem

Did the trick for me too! The only problem is I don't remember who offered this or where but a big thank you to whoever it was.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: How to access GPIO on RPi
« Reply #8 on: January 26, 2021, 09:13:02 am »
However, I did find this information at some point in the night and emailed it to myself:
Quote
I don't know if this is too late, but in Free Pascal I changed
fd := fpopen('/dev/mem', O_RdWr or O_Sync);
to
fd := fpopen('/dev/gpiomem', O_RdWr or O_Sync);
and that solved the problem

Did the trick for me too! The only problem is I don't remember who offered this or where but a big thank you to whoever it was.

Ah, yes, that is an important point as well. The access to /dev/mem is limited to root no matter what user/group the device has while /dev/gpiomem was explicitly created to only cover the GPIO area and be useable by the specified user/group of the device file. Thus the former works only with sudo.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: How to access GPIO on RPi
« Reply #9 on: January 26, 2021, 12:01:08 pm »
Ah, yes, that is an important point as well. The access to /dev/mem is limited to root no matter what user/group the device has while /dev/gpiomem was explicitly created to only cover the GPIO area and be useable by the specified user/group of the device file. Thus the former works only with sudo.

Or by assigning appropriate capabilities to the binary. In any event, don't consider that to be portable: the Linux APIs for GPIO access go either through /sys/class/gpio/* or /dev/gpiochip* (the latter is newer and supposedly higher-performance, but I've not played with it yet).

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & 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