Bosse_B
Posts: 879
Joined: Thu Jan 30, 2014 9:53 am

Trying to find FPC solution for GPIO pin setting

Fri Oct 09, 2015 8:31 pm

I need to pull two relays which will be connected to two GPIO pins.
I need to use FreePascal because I have a pretty big set of ObjectPascal files from Delphi, which contain all needed functionality, except of course the relay pulling stuff is written for Windows...
The hardware is RPi2B with Raspbian Wheezy.

I have read about gpio being a "file" in Raspbian, but I don't understand how to use that.
Could someone here please advice how to set and clear two hardware pins in the Pi2B connector on the Pi2B using FreePascal as the programming language?
Bo Berglund
Sweden

User avatar
hansotten
Posts: 326
Joined: Fri Jul 13, 2012 6:01 am
Contact: Website

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 7:38 am

http://duinorasp.hansotten.com for Raspberry Pi and Arduino and ESP8266
http://retro.hansotten.nl for retrocomputing with the 6502 cpu

Bosse_B
Posts: 879
Joined: Thu Jan 30, 2014 9:53 am

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 8:42 am

Thanks,
but I see a few problems here:
1) The chip specified is the one for the orioginal Pi and not the Pi2 (BCM2835 vs BCM2836)
2) It is stated after each example that the program must be executed as root. I want to be able to run as pi so it does not get elevated permissions.

For the first problem I already got a solution from another thread:
The address of the GPIO is different on the chips so in PiGpio.pas this needs to be changed:

Code: Select all

REG_GPIO = $20000; //For original Pi chip BCM2835
//change to:
REG_GPIO = $3F000; //For new Pi2 chip BCM2836
But still I don't like to have to run the program as root just to get access to the GPIO pins...

I have investigated further on this and found a large library of functions, which also includes GPIO for the Raspberry Pi (it actually has a define one can use to set it to compile for RPi2).
I have not yet tested it though, so I don't know about the root versus pi user requirement.
You can have a look at the Pascal eXTended library on the Afterwarp website.
Bo Berglund
Sweden

User avatar
joan
Posts: 14668
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 8:48 am

If you change the source to open /dev/gpiomem rather than /dev/mem you will be able to use the program without root privileges. This assumes the program only use the GPIO area from /dev/mem. Try it and see.

Bosse_B
Posts: 879
Joined: Thu Jan 30, 2014 9:53 am

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 9:08 am

When I look closer into the pigpio.pas file I find that the actions seem to be done towards another dev:

Code: Select all

function TIoDriver.MapIo: boolean;
begin
 Result := True;
 fd := fpopen('/dev/mem', O_RdWr or O_Sync); // Open the master /dev/memory device
  if fd < 0 then
  begin
    Result := False; // unsuccessful memory mapping
  end;
 //
end;
And when I check the groups for pi it does not list a group named mem...

Code: Select all

[email protected] ~ $ groups pi
pi : pi adm dialout cdrom sudo audio video plugdev games users input netdev spi i2c gpio
Does this matter?
Bo Berglund
Sweden

User avatar
joan
Posts: 14668
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 9:31 am

Change fd := fpopen('/dev/mem' to fd := fpopen('/dev/gpiomem'

Bosse_B
Posts: 879
Joined: Thu Jan 30, 2014 9:53 am

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 10:08 am

I am just compiling a new trunk version of fpc and lazarus on my pi so I cannot test right away. Takes about 2 hours on a Pi2..

But I would like to ask this:
Over on the fpc mailing list I asked about GPIO access from fpc and got directions to use "echo" on the command line to test the /dev/gpio but they gave no details at all on what to type in etc...
And when I tried to get it they threw me out because it was considered off-topic for the fpc mail list. :(
Do you have any idea how one can use echo to test the dev "files"?
Bo Berglund
Sweden

User avatar
joan
Posts: 14668
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 11:19 am

Bosse_B wrote:I am just compiling a new trunk version of fpc and lazarus on my pi so I cannot test right away. Takes about 2 hours on a Pi2..

But I would like to ask this:
Over on the fpc mailing list I asked about GPIO access from fpc and got directions to use "echo" on the command line to test the /dev/gpio but they gave no details at all on what to type in etc...
And when I tried to get it they threw me out because it was considered off-topic for the fpc mail list. :(
Do you have any idea how one can use echo to test the dev "files"?
I could hazard a guess at what they mean, probably to check the write permissions, but it would be a pointless exercise.

/dev/gpiomem should work with an up to date RPi Foundation version of Raspbian.

Bosse_B
Posts: 879
Joined: Thu Jan 30, 2014 9:53 am

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 3:08 pm

OK thanks,
I will try that by editing the pigpio.pas file to adjust the address for the registers on Pi2 and to use /dev/gpiomem rather than /dev/mem.
I have just finished installing fpc+lazarus on the Raspbian-Jessie image available for download.
Had to re-start half way through when I ran out of disk space during lazarus compile.
Imaged the state of the 8GB disk and wrote it to a 16GB disk and then expanded the file system. Then I could install it all.
However, in the gui the Lazarus item does not appear under Menu/Programming as it did in Wheezy. :(
I have to start it now using the Menu/Run... item and then type in lazarus-ide, what a drag!

I just wish there was a way to self-customize the Menu content so I could manually add lazarus to the Programming sub-menu...

Will now head off and get myself some prototype sockets to attach to the 40 pin header on Pi2 to check LED blinking as a test for the IO handling in FPC.
Bo Berglund
Sweden

User avatar
joan
Posts: 14668
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 4:15 pm

Bosse_B wrote: ...
However, in the gui the Lazarus item does not appear under Menu/Programming as it did in Wheezy. :(
I have to start it now using the Menu/Run... item and then type in lazarus-ide, what a drag!

I just wish there was a way to self-customize the Menu content so I could manually add lazarus to the Programming sub-menu...
...
There will be a way. I don't use the GUI so can not help. If you post that as a separate question someone will answer.

Bosse_B
Posts: 879
Joined: Thu Jan 30, 2014 9:53 am

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 4:28 pm

joan wrote: There will be a way. I don't use the GUI so can not help. If you post that as a separate question someone will answer.
Done so. It is located here..
Bo Berglund
Sweden

Bosse_B
Posts: 879
Joined: Thu Jan 30, 2014 9:53 am

Re: Trying to find FPC solution for GPIO pin setting

Sat Oct 10, 2015 9:55 pm

joan wrote: /dev/gpiomem should work with an up to date RPi Foundation version of Raspbian.
VERY GOOD ADVICE!
Thank you so much!
I have now modified the PiGpio.pas file shown in the lazarus wiki in the following way:
1) Added a conditional define for RPI2, which controls the setting of the REG_GPIO value
2) Changed the control file from /dev/mem to /dev/gpiomem
3) Added connector pin defines into the PiGpio.pas file where they rightly belong.

I created a test program based on the one in the lazarus wiki but only dealing with the I/O and it worked just fine even though I am running Lazarus as user pi!

So again, thank you for your help!
Bo Berglund
Sweden

Return to “General programming discussion”