Lazarus

Programming => Embedded => Operating Systems => Embedded - AVR => Topic started by: woodengineer on June 29, 2012, 02:58:47 am

Title: Lazarus / RaspberyPi Hardware Library
Post by: woodengineer on June 29, 2012, 02:58:47 am
Hello,
please find attached a wrapper unit for Gordon Henderson's
wiringPi C library (https://projects.drogon.net/raspberry-pi/wiringpi) and a little Lazarus test program.
This library allows access to the RaspberryPi's GPIO pins as well as the PWM pin.
The RaspberryPi is an inexpensive $25 SBC that runs linux. Check it out at http://www.raspberrypi.org.
BTW Lazarus runs quite well on the Pi.
Have fun, I know I will! :D
Alex
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: marcov on June 29, 2012, 10:58:25 am
Which distro did you run Lazarus with?
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: woodengineer on June 29, 2012, 03:45:19 pm
Hello,
I am running ArchLinux, XFCE4, fpc 2.6 and Lazarus 0.9.30.4.
Got the fpc arm binaries from the freepascal website and then compiled Lazarus from source.
Works quite nice, haven't found any problems, just don't expect 2sec compile times.
But compared to e.g. Mono it fly's.
A "Build all" of this posts Hardware library sample app takes about 5sec (console).
A Gui app with a form and a button on it about  1:30sec and about 50sec on subsequent builds .
Also played around with Geany and FpGui which is faster, but not as comfortable as Lazarus.
Hope this helps.

Alex
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: DirkS on June 29, 2012, 05:35:30 pm
Marco,

FYI: I have FPC / Laz running on the debian wheezy distro for armel (http://www.raspberrypi.org/archives/1435 / http://www.raspberrypi.org/phpBB3/viewtopic.php?f=50&t=8071).
I just used apt-get to install both. Versions: FPC 2.6.0-3 (2012.05/07); Laz 09.30.4-1 Rev 35971

I tried to use BigChimp's fpcup (using fpcup-linux-armel from https://bitbucket.org/reiniero/fpcup), but that did not work (or not completely).

Gr.
Dirk.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: ludob on June 29, 2012, 06:02:35 pm
Quote
I tried to use BigChimp's fpcup (using fpcup-linux-armel from https://bitbucket.org/reiniero/fpcup), but that did not work (or not completely).
Do you stilll have any details or log files of what went wrong?
I've only tested it with --only=fpc,lazarus since the default install tries to install too much for my small arm system.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: jwdietrich on June 30, 2012, 02:17:25 pm
I am happy to read that Lazarus has arrived at the Pi.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: DirkS on July 01, 2012, 06:45:49 am
Quote
I tried to use BigChimp's fpcup (using fpcup-linux-armel from https://bitbucket.org/reiniero/fpcup), but that did not work (or not completely).
Do you stilll have any details or log files of what went wrong?
I've only tested it with --only=fpc,lazarus since the default install tries to install too much for my small arm system.
It's actually an fpc error (ran fpcup with --only=fpc,lazarus):
Quote
make[3]: Entering directory `/home/pi/fpc/utils/fpcmkcfg'
/bin/mkdir -p units/arm-linux
__missing_command_DATA2INC -b -s fppkg.cfg fppkg.inc fppkg
make[3]: __missing_command_DATA2INC: Command not found
make[3]: *** [fppkg.inc] Error 127
make[3]: Leaving directory `/home/pi/fpc/utils/fpcmkcfg'
make[2]: *** [fpcmkcfg_all] Error 2
make[2]: Leaving directory `/home/pi/fpc/utils'
make[1]: *** [utils_all] Error 2
make[1]: Leaving directory `/home/pi/fpc'
make: *** [build-stamp.arm-linux] Error 2
make: Leaving directory `/home/pi/fpc'

Info: Running make install for FPC:
Note that fpcup *does* continue with 'make install', which it probably should not do (?).
I killed the procedure at this point and the fpcup log file does not give any useful info.

Gr
Dirk
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: rmudryk on August 12, 2012, 04:17:25 am
Did a fresh download of weezy 7-15 from the raspberry pi website http://downloads.raspberrypi.org/images/raspbian/2012-07-15-wheezy-raspbian/2012-07-15-wheezy-raspbian.zip (http://downloads.raspberrypi.org/images/raspbian/2012-07-15-wheezy-raspbian/2012-07-15-wheezy-raspbian.zip) 

sudo apt-get update
sudo apt-get upgrade

sudo apt-get install fpc
sudo apt-get install lazarus
sudo apt-get install libfbclient2

this is a estimation

http://www.inprox.net/pi/Raspberry-PI-Lazarus.png (http://www.inprox.net/pi/Raspberry-PI-Lazarus.png)

Rob
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: marcov on August 13, 2012, 11:51:09 am
__missing_command_DATA2INC -b -s fppkg.cfg fppkg.inc fppkg

That means

(1) you are attempting to compile a FPC without a full install of FPC.
(2) the codebase (svn checkout, unzipped source snapshot) has messed up datetime stamps


Point (1) means that you didn't fulfill the requirements to build FPC, and (2) is that you (or fpcup) didn't
even try to implement the workarounds.

(2) typically happens if you decompress a source archive made on a different OS, or do a SVN checkout on a
non standard filesystem that has different datestamp systems (like network drives), and/or use checkouts
between multiple OSes (that might have different time synchronization characteristics)
 
A workaround on *nix when bootstrapping on a new system is to "touch" the .inc files generated by data2inc and/or
other generator programs.  On Windows this is useless since touch doesn't come with windows or FPC.  Apparently FPCUP doesn't
implement it (it would require creating a list  what exactly is generated by data2inc)
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: ludob on August 13, 2012, 01:26:09 pm
The real problem is that fpcup starts with only a bootstrap compiler and binutils. At some stage in the make all process, data2inc is build in the fpc/utils directory and a little later fpmake calls data2inc but can't find it. Later in the make install process data2inc is copied to the fpc/bin/arm-linux directory but that is too late. Fpcup has solved this by adding the fpc/utils dir to the path before launching make. Perhaps the OP is using an older version of fpcup.

So yes it is (1) but IMHO also a shortcoming in the fpc make file.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: marcov on August 13, 2012, 03:39:32 pm
The real problem is that fpcup starts with only a bootstrap compiler and binutils. At some stage in the make all process, data2inc is build in the fpc/utils directory and a little later fpmake calls data2inc but can't find it. Later in the make install process data2inc is copied to the fpc/bin/arm-linux directory but that is too late. Fpcup has solved this by adding the fpc/utils dir to the path before launching make. Perhaps the OP is using an older version of fpcup.

So yes it is (1) but IMHO also a shortcoming in the fpc make file.

Yes and no. Of course the current state is imperfect and not terribly well documented, but the requirements are/were always clear. We hope that we can concentrate many of these requirements in fppkg/fpmake system but that is long term only.

A _full_ install is needed for automated bootstrap, but tweaking with just a cmdline compiler is possible on *nix targets with some care. (this is mainly meant to facilitate bootstrapping to a new target, and never was meant to be automable). The problem is mainly (2), while in general SVN is in a consistent state, ocassionally people make mistakes (and not check in the generated version) and some operations on checkouts can corrupt timestamps by fractions enough to select a different codepath by make. The automatic regenneration guards against that, but adds a dependency.

Some other problems are the creation of initial fpc.cfgs, symlinks (and I assume fpcup has already run into those, (2) is one of the rare ones)

FPCUP tries to do it fully automated without a full install, then it will run into exception (2). I think the workaround with fpc/utils is not correct (*) and then this will fail. If you really want to work around this, you need to touch all data2inc generated .incs.

(*) IIRC data2inc might be called in compiler/utils before data2inc is generated, and during crosscompiling a native target compiler will never reach te fpc/utils directory, only a cross compiler will.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: fredycc on August 13, 2012, 04:02:15 pm
Try with CodeTyphon.

http://www.pilotlogic.com/sitejoom/index.php/forum/other-cross-build-targets/2238-my-first-raspbian-app#2430 (http://www.pilotlogic.com/sitejoom/index.php/forum/other-cross-build-targets/2238-my-first-raspbian-app#2430)

regards
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: BigChimp on August 13, 2012, 04:48:21 pm
We hope that we can concentrate many of these requirements in fppkg/fpmake system but that is long term only.
I'd certainly be willing to help work on this, if only on the testing part, but I'm not sure what exactly needs to be done/what the requirements are.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: ludob on August 13, 2012, 07:46:58 pm
Quote
A _full_ install is needed for automated bootstrap
Quote
IIRC data2inc might be called in compiler/utils before data2inc is generated
So the "shortcoming in the fpc make file" was really an understatement.  data2inc is an internal dependency for make, created by make, but not always in time.
Quote
while in general SVN is in a consistent state, ocassionally people make mistakes (and not check in the generated version) and some operations on checkouts can corrupt timestamps by fractions enough to select a different codepath by make. The automatic regenneration guards against that, but adds a dependency.
But isn't that the wrong way around? Instead of putting internally build files under svn control and put a safeguard in place, logic says that one should only put sources under version control and build always. No more problems with different codepaths. Lazarus has a similar problem with some lrs resource files under version control.  It also happens that developers forget to regenerate them before committing the new sources with sometimes build failures as a result.
Quote
If you really want to work around this, you need to touch all data2inc generated .incs.
A touch will just fool the guard. The inc doesn't not correspond with the source if the developer forgot to regenerate them. So, touch will avoid the data2inc missing error but the outcome is a fpc that does not correspond with the sources.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: marcov on August 13, 2012, 10:10:00 pm
Quote
A _full_ install is needed for automated bootstrap
Quote
IIRC data2inc might be called in compiler/utils before data2inc is generated
So the "shortcoming in the fpc make file" was really an understatement.  data2inc is an internal dependency for make, created by make, but not always in time.

The dependency on the bootstrap is a full FPC install. A full FPC install contains data2inc. There is no shortcoming.

There is a  "cmdline compiler only" shortcut ONLY on open source *nix (since other platforms require "make" "binutils" etc) for people that know what they are doing. But that is a workaround for quick and dirty work, not an official supported solution.

The cmdline compiler-only route was never official. It just works mostly, and was somewhat documented in the buildfaq to help people keep machines up to date with minimal dependencies.

That doesn't mean that you can't think about building on the standalone compiler, but big changes in the current codebase wrt fpcmake are unlikely,
since it is both a production and declared legacy. As usual with longterm infrastructural work on the successor has stalled.

Quote
Quote
while in general SVN is in a consistent state, ocassionally people make mistakes (and not check in the generated version) and some operations on checkouts can corrupt timestamps by fractions enough to select a different codepath by make. The automatic regenneration guards against that, but adds a dependency.
But isn't that the wrong way around? Instead of putting internally build files under svn control and put a safeguard in place, logic says that one should only put sources under version control and build always.

That's something for any new system to attempt. The old system already depends on way too many externals that are immutable (starting with make)

Quote
No more problems with different codepaths. Lazarus has a similar problem with some lrs resource files under version control.  It also happens that developers forget to regenerate them before committing the new sources with sometimes build failures as a result.

Actually the filesystem thing (having sources shared between multiple OSes or on network drives, extracting archives made on different OSes that cause date rounding errors during file creation) is more common.

Quote
Quote
If you really want to work around this, you need to touch all data2inc generated .incs.
A touch will just fool the guard. The inc doesn't not correspond with the source if the developer forgot to regenerate them. So, touch will avoid the data2inc missing error but the outcome is a fpc that does not correspond with the sources.

Then your only choice is to follow the official way, and always require a proper installation.

I don't think touching is so bad. It might mask a very short transient problem in rare cases, but it will save you a lot of trouble where people have moved files across filesystem bounderies. Most of such forgotten makefile regenerations are for platforms that are still under development anyway. (which is why they go unnoticed)
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: BigChimp on August 14, 2012, 11:40:11 am
Quote
I tried to use BigChimp's fpcup (using fpcup-linux-armel from https://bitbucket.org/reiniero/fpcup), but that did not work (or not completely).
Do you stilll have any details or log files of what went wrong?
I've only tested it with --only=fpc,lazarus since the default install tries to install too much for my small arm system.
It's actually an fpc error (ran fpcup with --only=fpc,lazarus):
Quote
make[3]: Entering directory `/home/pi/fpc/utils/fpcmkcfg'
/bin/mkdir -p units/arm-linux
__missing_command_DATA2INC -b -s fppkg.cfg fppkg.inc fppkg
make[3]: __missing_command_DATA2INC: Command not found
make[3]: *** [fppkg.inc] Error 127
make[3]: Leaving directory `/home/pi/fpc/utils/fpcmkcfg'
make[2]: *** [fpcmkcfg_all] Error 2
make[2]: Leaving directory `/home/pi/fpc/utils'
make[1]: *** [utils_all] Error 2
make[1]: Leaving directory `/home/pi/fpc'
make: *** [build-stamp.arm-linux] Error 2
make: Leaving directory `/home/pi/fpc'

Info: Running make install for FPC:
Note that fpcup *does* continue with 'make install', which it probably should not do (?).
I killed the procedure at this point and the fpcup log file does not give any useful info.

Thanks Dirk, we opened a bug report on it:
https://bitbucket.org/reiniero/fpcup/issue/45/make-sure-data2inc-exists-before-make-all (https://bitbucket.org/reiniero/fpcup/issue/45/make-sure-data2inc-exists-before-make-all)
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: muratech on August 19, 2012, 08:23:59 pm
Hi all

I am a noob when it comes to linux or anything similar. Have been working with Delphi on Windows for years. Here's my question -
1) Installed Lazarus
2) Downloaded the wrapper for GPIO.
3) Tried running demo program.

Getting the following errors -

Quote
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
/usr/bin/ld: error: raspberry uses VFP register arguments, ./wiringPi/wiringPi.o does not
/usr/bin/ld: failed to merge target specific data of file ./wiringPi/wiringPi.o
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
raspberry.lpr(46,47) Error: Error while linking
raspberry.lpr(46,47) Fatal: There were 1 errors compiling module, stopping
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: woodengineer on August 21, 2012, 05:15:30 pm
Hi all

I am a noob when it comes to linux or anything similar. Have been working with Delphi on Windows for years. Here's my question -
1) Installed Lazarus
2) Downloaded the wrapper for GPIO.
3) Tried running demo program.

Getting the following errors -

Quote
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
/usr/bin/ld: error: raspberry uses VFP register arguments, ./wiringPi/wiringPi.o does not
/usr/bin/ld: failed to merge target specific data of file ./wiringPi/wiringPi.o
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
/usr/bin/ld: BFD (GNU Binutils for Debian) 2.22 assertion fail ../../bfd/elf32-arm.c:12049
raspberry.lpr(46,47) Error: Error while linking
raspberry.lpr(46,47) Fatal: There were 1 errors compiling module, stopping

Hello muratech,
I am running Archlinux not Debian,
Try to recompile the the wiringPi.c file to get a new wiringPi.o file as described in the readme.txt file of the lazwiringpi folder.

" The source file for Gordon's library is inside the wiringPi subdirectory. You can recompile
the library by executing a simple gcc -c wiringPi.c.
The resulting wiringPi.o file is whats used by pascal."

Also, your resulting program must be run as root.
Hope this helps!

Alex
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: muratech on August 21, 2012, 07:51:12 pm
 :) Thanks ALex
Busy installing ARCH now and will try this out.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: muratech on August 27, 2012, 07:42:44 pm
Thanks Woodengineer.
I did a recompile of Wiringpi.

I need to run a program with a GUi in StartX. Is there a way I can do this as root.
Program is running but can't read anything from Pins, tested in Python and my wirring is correct.

Thanks
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: avra on November 19, 2012, 03:12:13 pm
I need to run a program with a GUi in StartX. Is there a way I can do this as root.
Have you tried to start terminal from the desktop, navigate to your GUI executable, and start executable with sudo from command line? Does that work? Can you check if your binary is flagged as executable?
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: marun on December 01, 2012, 09:39:47 am
Hello,
please find attached a wrapper unit for Gordon Henderson's
wiringPi C library (https://projects.drogon.net/raspberry-pi/wiringpi) and a little Lazarus test program.
This library allows access to the RaspberryPi's GPIO pins as well as the PWM pin.
The RaspberryPi is an inexpensive $25 SBC that runs linux. Check it out at http://www.raspberrypi.org.
BTW Lazarus runs quite well on the Pi.
Have fun, I know I will! :D
Alex
i tried to use your unit but it doesnt work it crashes on pinMode "esternal error SIGSEGV"
where i have to copy files from your unit?
here is my project http://leteckaposta.cz/556983847 (http://leteckaposta.cz/556983847) (it is fan control)
edit: i added "if wiring pisetup..." and it ends with error (wiringpisetup=-1) do you know where is problem?
edit2: finally it works but only as a root
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: Max12 on December 04, 2012, 09:37:00 pm
why it works only when starting by root????

I've exact the same problem!
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: DirkS on December 05, 2012, 12:41:32 am
why it works only when starting by root????
IIRC accessing GPIO functions on the PI requires root priviliges.
You can use 'sudo <yourprogramname>' to start the compiled program

Gr.
Dirk
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: marun on December 08, 2012, 07:11:50 pm
i made this test program because i want to verify pinout but if i want change log. level of any pin it write access violation (started as a root) does anybody know where is problem? poject is here http://leteckaposta.cz/356175095 (http://leteckaposta.cz/356175095)
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: lynnr on January 13, 2013, 11:02:56 pm
Hello All,

I'm trying to get my Raspberry Pi working using the wiringPi library, but haven't had any success.  I'm new to this, I've only had my Pi for about 1 month. 

Using Raspbian wheezy image, I was able to install Lazarus and compile and run a hello world program.  Next, installed wiringpi using directions on: //projects.drogon.net/raspberry-pi/wiringpi/download-and-install/  . All went smooth, so next I compiled the example(lazwiringpi.pas) from the beginning of this topic using the IDE. The compile completed normally.
But when I execute I get the following error:


pi@raspberrypi ~/lazarus/lazwiringpi $ sudo ./raspberryStarting RaspberryPi
An unhandled exception occurred at $000449F4 :
EAccessViolation : Access violation
  $000449F4


Help would be much appreciated.
Lynn
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: jwdietrich on January 27, 2013, 08:10:12 pm
An alternative, and perhaps simpler, way to access the Pi's GPIO ports is via encapsulated Unix shell calls. See http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Hardware_access_via_encapsulated_shell_calls for an example.

This method is directly supported by Lazarus / Free Pascal and doesn't require installation of any additional libraries or packages. Of course, your application has to be run with root privileges.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: vcp_ai on March 04, 2013, 07:05:33 pm
An alternative, and perhaps simpler, way to access the Pi's GPIO ports is via encapsulated Unix shell calls. See http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Hardware_access_via_encapsulated_shell_calls for an example.

This method is directly supported by Lazarus / Free Pascal and doesn't require installation of any additional libraries or packages. Of course, your application has to be run with root privileges.
Hi,  can you give an example of how to READ a GPIO port into fpc, using fpsystem ???

Regards
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: jwdietrich on April 28, 2013, 08:39:33 pm
Hi,  can you give an example of how to READ a GPIO port into fpc, using fpsystem ???

I can give an example to read the status of a port with native FPC methods (fpopen, fpread, fpclose etc.). This is a more elegant solution than the detour over fpsystem.

See http://wiki.lazarus.freepascal.org/Lazarus_on_Raspberry_Pi#Reading_the_status_of_a_port for a working example.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: picstart on April 28, 2013, 11:10:52 pm
Just keep in mind that GPIO ( working with pins) on the rasp pi is a restricted operation and often needs to be run with root privileges that usually means the lazarus IDE or the o file needs to run from root. Other things like I2C are less restricted
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: jwdietrich on April 28, 2013, 11:22:37 pm
Just keep in mind that GPIO ( working with pins) on the rasp pi is a restricted operation and often needs to be run with root privileges that usually means the lazarus IDE or the o file needs to run from root.

That is correct. For automated operation it may be advisable to write a shell script that calls the executable via sudo.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: rmudryk on May 18, 2013, 06:15:59 pm
This works but you do not have to expose the entire OS to Root access, as well as run Lazarus as root.  But because these are dynamic devices you have to run a script in rc.local or something as root to give yourself permissions to write on every reboot or runlevel change.

Example command line:
sudo echo 4 > /sys/class/gpio/export
sudo echo out > /sys/class/gpio4/direction
sudo chmod 666 > /sys/class/gpio4/value

now because the permissions are rw rw rw  world can read/write to gpio4

you can add this kinda stuff to the rc.local in the /etc  without the sudo, as that runs as root on boot up, so your System is GPIO ready on boot for normal users to use.


i2c is a bit different, I found these instructions in a word document for settting up i2c on the raspberry pi, but I use this all the time instead of running lazarus as "Root".  I am sorry but I can't remember the link to these instructions, but thanks!  it was the key to cleaning up this debacle of having to be root for me.  I also hear there is a more formal program to do all this in unix as well. 

Once the reboot has finished you need to make the ports active by running this command:

   $sudo modprobe i2c-dev

 Now you should be able to see the i2c ports listed in your /dev/ directory

   $ls /dev/i2c*

You should see /dev/i2c-0 and /dev/i2c-1 listed. To be able to use these ports you need to give yourself permission to access them.

   $sudo chmod o+rw /dev/i2c*

Now your i2c ports will be ready for you to use. You will have to run the modprobe and chmod commands every time you login to activate the ports and apply the permissions. If you wish to automate this process so the commands are run every time you log in then you can edit /etc/rc.local and add the commands in.



Just keep in mind that GPIO ( working with pins) on the rasp pi is a restricted operation and often needs to be run with root privileges that usually means the lazarus IDE or the o file needs to run from root. Other things like I2C are less restricted
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: bartdereu on August 10, 2013, 07:05:57 pm
When i try the WiringPi example in Lazarus i get :

Code: [Select]
..
/usr/bin/ld:error : raspberry uses VFP register arguments, .wiringPi/wiringPi.o does not
/usr/bin/ld: failed to merge target specific data of file ./wiringPi/wiringPi.o
..

Is there someone who can help me trough getting the PiFace board working with Lazarus ?
I'm getting pretty fast grey hair the last few days!  :P


I think i tried every single piece of pascal code to make the PiFace respond to me, but nothing
has been working so far. The Piface-Emulator does work fine.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: rmudryk on August 10, 2013, 09:05:10 pm
Are you running as root or a user?   Always try as root first, and make sure the devices are already created.  I have not played with lazarus lately, but I have felt your pain before.

When i try the WiringPi example in Lazarus i get :

Code: [Select]
..
/usr/bin/ld:error : raspberry uses VFP register arguments, .wiringPi/wiringPi.o does not
/usr/bin/ld: failed to merge target specific data of file ./wiringPi/wiringPi.o
..

Is there someone who can help me trough getting the PiFace board working with Lazarus ?
I'm getting pretty fast grey hair the last few days!  :P


I think i tried every single piece of pascal code to make the PiFace respond to me, but nothing
has been working so far. The Piface-Emulator does work fine.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: bartdereu on August 10, 2013, 10:27:10 pm
Yes i am running it as a root. Man, i thought the raspberry pi was a neat thing but the piface
is going to make me crazy !
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: avra on August 11, 2013, 12:53:34 am
And have you tried to google for "uses VFP register arguments"? The first link explains it all:
http://stackoverflow.com/questions/9753749/arm-compilation-error-vpf-registered-used-by-executable-not-object-file
Your compiled code and that lib do not match. You are trying to link code compiled with soft-float ABI with another hard-float ABI code. Search the net if you don't know what is that and why is that important with ARM. That can be fixed, but it would be easier for you to try another way to access hardware as others have already suggested. The link is still the same: http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware. It seams that rpi_hal directly supports piface as mentioned here: http://forum.lazarus.freepascal.org/index.php/topic,20991.msg126708.html#msg126708.

I would really suggest you to learn more about ARM Linux, architecture and compiling for ARM, and also about RPi from it's site and wiki and about similar RPi projects, and search this forum and wikis for anything about RPi. You can also find valuable info on PilotLogic forum. Otherwise you will run into many walls like this one.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: picstart on August 11, 2013, 03:36:06 am
Let's hope most won't want to use the Raspbery pi as a slave on I2C.... just as well because the pi folks didn't bring a trace out to the GPIO to support the slave clock. A pi must always be the I2C master which can be limiting if the I2C bus already has a working master.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: rmudryk on August 11, 2013, 04:20:24 pm
They make I2C slave to SPI master chips that would solve that problem with hardware.  Or a write a software I2C slave.

Let's hope most won't want to use the Raspbery pi as a slave on I2C.... just as well because the pi folks didn't bring a trace out to the GPIO to support the slave clock. A pi must always be the I2C master which can be limiting if the I2C bus already has a working master.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: avra on August 11, 2013, 05:12:56 pm
Let's hope most won't want to use the Raspbery pi as a slave on I2C.... just as well because the pi folks didn't bring a trace out to the GPIO to support the slave clock. A pi must always be the I2C master which can be limiting if the I2C bus already has a working master.
A need to use RPi as a I2C slave will be very rare. People will mostly want to interface I2C slaves as a master and that's where RPi fits very well. Even if you have such a specific need when you design your own project you are aware of this problem and there are many ways to design around it.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: rmudryk on August 11, 2013, 06:51:00 pm
Being such a complete IDE, I would really hope Lazarus would be added into the main Distribution of Raspberry PI at some point.  It is probably one of the most, if not the most complete and intuitive IDEs available, and far superior than what comes with the default Debian Squeeze distro now.  There are newer and better ways to allow users access to a virtual device in /dev that does not require having to do permissions every boot, I just forgot where. 
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: avra on August 12, 2013, 08:19:09 am
Being such a complete IDE, I would really hope Lazarus would be added into the main Distribution of Raspberry PI at some point.
It will hardly happen, because foundation owners and Pi creators like Python. Even Pi's name came as an association to Python. For something simpler they chose Scratch, and for something low level they chose C.

What I would really like to see is that Debian finally moves from that old 0.9.30 to something newer. That would help a lot.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: bartdereu on August 12, 2013, 03:49:34 pm
Ofcourse i've tried different approaches to get the PiFace running, and i almost found a very good one.

You must have (ofcourse) GPIO installed correctly, then you can just execute gpio commands with the fpsystem(' ')
function. Don't forget to add 'Unix' in the uses clause of the unit.
 
Code: [Select]
Procedure TForm1.Checkbox1Change(Sender:TObject)
begin
 if Checkbox1.Checked then
  gReturncode:=fpsystem('gpio -p write 200 1')
else
  gReturncode:=fpsystem('gpio -p write 200 0');
end;

A little explanation : With the gpio installed the piface input output ports are situated on ports 200-207.
the fact that things like this are NOT explained in the PiFace manual , makes it very difficult to work with
boards like this.
This works ok, but i'm now struggling to read the state of a button (button 1 = 200)

Code: [Select]
Procedure TForm1.Timer1Timer(Sender:TObject)
begin
  gReturncode:=fpsystem('gpio -p read 200')
  if gReturncode=0 then checkbox2.checked:=true;
  else if gReturncode=1 then checkbox2.checked:=false;
end;

The command 'gpio -p read 200' in the terminal gives me a '1' when the button is not
pressed and a '0' when it does (probably bc it goes low)
So far i have been unable to get the result with this Lazarus code. The gReturncode
remains 0 all the time. Maybe pfsystem doesn't give me back a result ? is there another
way to execute the gpio line and get the result back ?
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: bartdereu on August 12, 2013, 05:32:23 pm
And the final solution :  ;D

Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  ExtCtrls, Unix,BaseUnix;

type

  { TForm1 }

  TForm1 = class(TForm)
    ApplicationProperties1: TApplicationProperties;
    Button1: TButton;
    CheckBox1: TCheckBox;
    CheckBox2: TCheckBox;
    Edit1: TEdit;
    Memo1: TMemo;
    Timer1: TTimer;
    procedure ApplicationProperties1Idle(Sender: TObject; var Done: Boolean);
    procedure CheckBox1Change(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  gReturncode,a : longint;
  return: integer;

implementation

{$R *.lfm}

{ TForm1 }

// Set the output high for output 1:

procedure TForm1.CheckBox1Change(Sender: TObject);
begin
  if Checkbox1.Checked then
        gReturncode:=fpsystem('gpio -p write 200 1')
  else
    gReturncode:=fpsystem('gpio -p write 200 0');

  end;

// Read the state of button 1 on the PiFace :

procedure TForm1.ApplicationProperties1Idle(Sender: TObject; var Done: Boolean);
var FileDesc:Integer;
  buttonStatus:String[1] ='1';
begin
 try
 return:=fpsystem('gpio -p read 200 > inputs.dat');
 fileDesc:=fpopen('inputs.dat',O_RdOnly);
 if FileDesc > 0 Then
       begin
         gReturncode:=fpread(fileDesc,buttonStatus[1],1);
         if buttonStatus ='0' then
               checkbox2.Checked:=true
               else
                 checkbox2.Checked:=false;
       end;
  finally
  gReturncode :=fpclose(fileDesc);
end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
 fpsystem('gpio -p mode 200 up');
end;

end.

Hope this helps someone some day :-)
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: bartdereu on August 14, 2013, 10:34:07 pm
Apparently this approach has a nasty side -effect . It read/writes to the SD card several times a second,
and an SD card is not well suited for this kind of things. After on million writes the best is just gone. That
would be within a few days.

Now i've been trying to use a TProcess to get the output of the 'gpio -p read 200' directly into my progam,
but i'm running in more troubles then i'd like. Acces violations at the NumBytesAvailable :

Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var bufstr:string;
  buflen:integer;
begin
AProcess:= TProcess.Create(nil);
AStringlist := Tstringlist.create;
AProcess.commandline := 'echo 17';
Aprocess.Execute;

try
Aprocess.Execute;
BufStr := '';
repeat
if aProcess.Output.NumBytesAvailable > 0 then
begin
BufLen := Length(BufStr);
SetLength(BufStr, BufLen + aProcess.Output.NumBytesAvailable);
aProcess.Output.Read(BufStr[BufLen+1],aProcess.Output.NumBytesAvailable);
end;
until not aProcess.Running;
finally
aProcess.Free;
edit1.text:=BufStr;

end;
end;

Did anyone succeed to get a TProcess get an output in Lazarus ?

Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: lazaursrotton on March 14, 2015, 02:13:28 am

it will not  require a recompile
 linklib to installed shared lib

translated more of the lib from

 2.23 wiringpi
 
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: Pelican on May 26, 2015, 01:40:31 am
I've downloaded the unit a year ago and it worked on the older Pis but now I have a Pi 2 which needs the new version of the wiringPi but when I try to build any program using that new object files the linker gives me the following error messages:

Code: [Select]
/usr/bin/ld.bfd: wiringPi.o undefined reference to symbol '__aeabi_idiv@@GCC3.5'
//lib/arm-linux-gnueabihf/libgcc_s.so.1: error adding symbols: DSO missing from command line

If I link the lazwiringpi's object file then no error but the gpio function doesn't work.
Could you help me how can I solve this linker issue?
I tried to make the object files by myself - no change.

wiringPi 2.25
Lazarus 0.9.30.4-6

I added to the wrapper more functions in wiringPi (and the spi, i2c and serial functions too) send me an email if you want the sources.

Edit:
I think this is a similar problem, but I have no idea how could I fix this.
http://glandium.org/blog/?p=2510
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: Pelican on June 14, 2015, 09:34:21 am
I've updated my Lazarus to v1.5 and fpc to the latest but the problem is still there, cannot compile any code which use the wiringPi.o.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: DonAlfredo on June 14, 2015, 09:50:07 am
Try to add this to the compiler options when compiling for RPi2:
Code: [Select]
-dFPC_ARMHF -CpARMV7A -CaEABIHF -CfVFPv3 -OoFASTMATH
if you use hardfloat for RPi2.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: Pelican on June 15, 2015, 06:11:05 pm
Hi,

Thank you for your reply.
I tried to put those to the Project options/Compilation and Linking/Linking/pass to linker...
I've got unrecognized option warning for these options: -CpARMV7A -CaEABIHF -CfVFPv3
I removed them and got a new error message: Error while linking

Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: DonAlfredo on June 15, 2015, 07:01:00 pm
Could it be that you have a compiler that does not support hardfloat ?
I presume you need hardfloat due to the info in your post:
Code: [Select]
//lib/arm-linux-gnueabihf/libgcc_s.so.1: error adding symbols: DSO missing from command line
If your compiler does hardfloat, it would support the instructions I gave you.
So, check your compiler would be my advice !
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: Pelican on June 15, 2015, 10:54:36 pm
This is the compiler: Free Pascal Compiler version 3.1.1 [2015/05/28] for arm
Based on it's help it supports all of the options you gave but I put all those to the linker's option...  :o
How can I give these option to the compiler?
I tried to put Project options/Compiler options/Additions and overrides/Custom and got this error message>

Code: [Select]
Compile Project, Target: homeserver: Exit code 256, Errors: 1, Warnings: 2, Hints: 1
main.pas(8,79) Hint: Unit "wiringPi" not used in Main
homeserver.lpr(21,0) Warning: "crtbegin.o" not found, this will probably cause a linking failure
homeserver.lpr(21,0) Warning: "crtend.o" not found, this will probably cause a linking failure
/usr/bin/ld: warning: link.res contains output sections; did you forget -T?
/usr/bin/ld: /home/pi/projects/homeserver/wiringPi.o: undefined reference to symbol '__aeabi_idiv@@GCC_3.5'
//lib/arm-linux-gnueabihf/libgcc_s.so.1: error adding symbols: DSO missing from command line
[8.977] Executing "/usr/bin/ld" with command line "  --dynamic-linker=/lib/ld-linux-armhf.so.3    -L. -o homeserver link.res"
homeserver.lpr(21,0) Error: Error while linking

Edit:

Anyway, I tried the h2wiringpi from the previous page and it seems it's working...

The
{$linklib libwiringPi}
just did the trick.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: lazaursrotton on June 16, 2015, 12:25:09 am
https://github.com/laz2wiringpi/lazI2cdev
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: lazaursrotton on June 16, 2015, 12:26:21 am
https://github.com/laz2wiringpi/laz2wiringpi
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: Pelican on June 18, 2015, 11:07:02 pm
I've put SPI, I2C, serial and other functions to it.
http://pel.hu/down/libwiringpi.pas
Title: Re: Lazarus / RaspberyPi 2 model B Hardware Library
Post by: kaptindan on February 15, 2016, 03:53:43 pm
Hello, newbe here! I've been working with the Raspberry Pi 2 Model B and trying to access the GPIO....no luck so far using h2wiringPi...any ideas?????
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: avra on May 10, 2021, 09:42:24 am
I've put SPI, I2C, serial and other functions to it.
http://pel.hu/down/libwiringpi.pas

In case someone else was hunting it down (original site has gone to oblivion):
Code: Pascal  [Select][+][-]
  1. unit libwiringpi;
  2.  
  3. (* Pascal wrapper unit for Gordon Henderson wiringPi library. The source can
  4.  * be found at https://http://wiringpi.com
  5.  *
  6.  * hwiringpi: origanal Wrapper and
  7.  * pascal sample by Alex Schaller.
  8.  *
  9.  * Fork
  10.  * h2wiringpi: Version 0.1 By Allen Roton
  11.  * wiringPi Version 2.26
  12.  *
  13.  * libwiringpi: Version 0.2 by Pelican (pel.hu)
  14.  * I2C, SPI, serial and other functions
  15.  *
  16.  * wiringPi:
  17.  *  Arduino compatable (ish) Wiring library for the Raspberry Pi
  18.  *  Copyright (c) 2012 Gordon Henderson
  19.  ***********************************************************************
  20.  * This file is part of wiringPi:
  21.  *  https://projects.drogon.net/raspberry-pi/wiringpi/
  22.  *
  23.  *    wiringPi is free software: you can redistribute it and/or modify
  24.  *    it under the terms of the GNU General Public License as published by
  25.  *    the Free Software Foundation, either version 3 of the License, or
  26.  *    (at your option) any later version.
  27.  *
  28.  *    wiringPi is distributed in the hope that it will be useful,
  29.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  30.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  31.  *    GNU General Public License for more details.
  32.  *
  33.  *    You should have received a copy of the GNU General Public License
  34.  *    along with wiringPi.  If not, see <http://www.gnu.org/licenses/>.
  35.  ***********************************************************************
  36.  *)
  37.  
  38. {$linklib c}
  39. {$linklib libwiringPi}
  40.  
  41. interface
  42.  
  43. // Handy defines
  44. const
  45. // Pin modes
  46.   INPUT =           0;
  47.   OUTPUT  =         1;
  48.   PWM_OUTPUT=       2;
  49.   GPIO_CLOCK=       3;
  50.   SOFT_PWM_OUTPUT=  4;
  51.   SOFT_TONE_OUTPUT= 5;
  52.   PWM_TONE_OUTPUT=  6;
  53.  
  54.   LOW=  0;
  55.   HIGH= 1;
  56.  
  57. // Pull up/down/none
  58.   PUD_OFF=     0;
  59.   PUD_DOWN=    1;
  60.   PUD_UP=      2;
  61.  
  62. // PWM
  63.   PWM_MODE_MS=    0;
  64.   PWM_MODE_BAL=   1;
  65.  
  66. // Interrupt levels
  67.   INT_EDGE_SETUP=   0;
  68.   INT_EDGE_FALLING= 1;
  69.   INT_EDGE_RISING=  2;
  70.   INT_EDGE_BOTH=    3;
  71.  
  72.   PI_MODELS : array [0..7] of string = (
  73.     'Unknown',
  74.     'model A',
  75.     'model B',
  76.     'model B+',
  77.     'model CM',
  78.     'model A+',
  79.     'model 2',
  80.     'New unknown'
  81.    );
  82.  
  83.   PI_VERSIONS : array [0..5] of string = (
  84.     'version unknown',
  85.     'v1',
  86.     'v1.1',
  87.     'v1.2',
  88.     'v2',
  89.     'new unknown'
  90.    );
  91.  
  92.   PI_MAKERS : array [0..5] of string = (
  93.     'Unknown',
  94.     'EGOMAN',
  95.     'SONY',
  96.     'QISDA',
  97.     'MBEST',
  98.     'New unknown');
  99.  
  100.   // Pin mappings from P1 connector to WiringPi library
  101.   // Px represents to physical pin on the RaspberryPi P1 connector
  102.  
  103.   // P1 = 3.3V
  104.   // P2 = 5V
  105.   P3  = 8;
  106.   // P4 = 5V
  107.   P5  = 9;
  108.   //P6 = GND
  109.   P7  = 7;
  110.   P8  = 15;
  111.   //P9 = GND
  112.   P10 = 16;
  113.   P11 = 0;
  114.   P12 = 1;
  115.   P13 = 2;
  116.   //P14 = GND
  117.   P15 = 3;
  118.   P16 = 4;
  119.   // P17 = 3.3V
  120.   P18 = 5;
  121.   P19 = 12;
  122.   // P20 = GND
  123.   P21 = 13;
  124.   P22 = 6;
  125.   P23 = 14;
  126.   P24 = 10;
  127.   // P25 = GND
  128.   P26 = 11;
  129.   // pi2
  130.   P27 = 30;
  131.   P28 = 31;
  132.   P29 = 21;
  133.   // P30 = GND
  134.   P31  = 22;
  135.   P32 = 26;
  136.   P33 = 23;
  137.   // P34 = GND
  138.   P35 = 24;
  139.   P36 = 27;
  140.   P37 = 25;   // ops was 24
  141.   P38 = 28;
  142.   // P39 = GND
  143.   P40 = 29;
  144.  
  145.   PI_2_WiretoPhy : array[0..29] of integer = (
  146.     11, 12,     //0,1
  147.     13, 15,     //2,3
  148.     16, 18,     //4,5
  149.     22, 7,      //6,7
  150.     3,  5,      //8,9
  151.     24, 26,     //10,11
  152.     19, 21,     //12,13
  153.     23, 8,      //14,15
  154.     10,-17,     //16,17
  155.    -18,-19,     //18,19
  156.    -20, 29,     //20,21
  157.     31, 33,     //22,23
  158.     35, 37,     //24,25
  159.     32, 36,     //26,27
  160.     38, 40      //28,29
  161.   ) ;
  162.  // -1 GND
  163.  // -5 5V
  164.  // -3 3.3V
  165.  // -6
  166.  // -7
  167.   PI_2_Phy_toWire : array [1..40] of integer =
  168.     ( -3,  -5,
  169.        8,  -5,
  170.        9,  -1,
  171.        7,   15,
  172.       -1,   16,
  173.        0,   1,
  174.        2,  -1,
  175.        3,   4,
  176.       -3,   5,
  177.        12, -1,
  178.        13,  6,
  179.        14,  10,
  180.       -1,   11,
  181.       -6,  -7,
  182.        21, -1,
  183.        22,  26,
  184.        23, -1,
  185.        24,  27,
  186.        25,  28,
  187.       -1,   29
  188.     );
  189.  
  190.   altmodes : array[0..7] of string = ('IN', 'OUT', 'ALT5', 'ALT4', 'ALT0', 'ALT1', 'ALT2', 'ALT3');
  191.  
  192. type
  193.   TPinMode = (pm_INPUT,pm_OUTPUT,pm_PWM_OUTPUT,pm_GPIO_CLOCK,pm_SOFT_PWM_OUTPUT,pm_SOFT_TONE_OUTPUT,pm_PWM_TONE_OUTPUT);
  194.   TpullRisistor = (prPUD_OFF  ,prPUD_DOWN , prPUD_UP );
  195.   Tintlevles = (il_EDGE_SETUP,il_EDIGE_FALLING,il_EDGE_RISING,il_EDGE_BOTH);
  196.  
  197. // Core wiringPi functions
  198.  
  199. //extern struct wiringPiNodeStruct *wiringPiFindNode (int pin) ;
  200. //extern struct wiringPiNodeStruct *wiringPiNewNode  (int pinBase, int numPins) ;
  201.  
  202. //This initialises wiringPi and assumes that the calling program is going to be using the wiringPi pin numbering scheme. This is a simplified numbering scheme which provides a mapping from virtual pin numbers 0 through 16 to the real underlying Broadcom GPIO pin numbers. See the pins page for a table which maps the wiringPi pin number to the Broadcom GPIO pin number to the physical location on the edge connector.
  203. //This function needs to be called with root privileges.
  204. function wiringPiSetup:longint;cdecl;external;
  205. //This initialises wiringPi but uses the /sys/class/gpio interface rather than accessing the hardware directly. This can be called as a non-root user provided the GPIO pins have been exported before-hand using the gpio program. Pin numbering in this mode is the native Broadcom GPIO numbers – the same as wiringPiSetupGpio() above, so be aware of the differences between Rev 1 and Rev 2 boards.
  206. //Note: In this mode you can only use the pins which have been exported via the /sys/class/gpio interface before you run your program. You can do this in a separate shell-script, or by using the system() function from inside your program to call the gpio program.
  207. //Also note that some functions have no effect when using this mode as theyÂ’re not currently possible to action unless called with root privileges. (although you can use system() to call gpio to set/change modes if needed)
  208. function wiringPiSetupSys:longint;cdecl;external;
  209. // This is identical to wiringPiSetup, however it allows the calling programs to use the Broadcom GPIO pin numbers directly with no re-mapping.
  210. // this function needs to be called with root privileges, and note that some pins are different from revision 1 to revision 2 boards.
  211. function wiringPiSetupGpio:longint;cdecl;external;
  212. //Identical to wiringPiSetupGpio, however it allows the calling programs to use the physical pin numbers on the P1 connector only.
  213. //this function needs to be called with root priviliges.
  214. function wiringPiSetupPhys:longint;cdecl;external;
  215. procedure pinModeAlt(pin:longint; mode:longint);cdecl;external;
  216.  
  217. procedure pinMode(pin:longint; mode:longint);cdecl;external;
  218. procedure pinMode_pas(pin:longint; mode:TPinMode);
  219.  
  220. procedure pullUpDnControl(pin:longint; pud:longint);cdecl;external;
  221. //This sets the pull-up or pull-down resistor mode on the given pin, which should be set as an input.
  222. //Unlike the Arduino, the BCM2835 has both pull-up an down internal resistors.
  223. //The parameter pud should be; PUD_OFF, (no pull up/down), PUD_DOWN (pull to ground) or PUD_UP (pull to 3.3v)
  224. //The internal pull up/down resistors have a value of approximately 50K? on the Raspberry Pi.
  225. //This function has no effect on the Raspberry PiÂ’s GPIO pins when in Sys mode.
  226. //If you need to activate a pull-up/pull-down, then you can do it with the gpio program in a script before you start your program.
  227. procedure pullUpDnControl_pas(pin : longint ; pud : tpullRisistor);
  228.  
  229. function digitalRead(pin:longint):longint;cdecl;external;
  230. procedure digitalWrite(pin:longint; value:longint);cdecl;external;
  231. function digitalWriteByte(value:longint):longint;cdecl;external;
  232. procedure pwmWrite(pin:longint; value:longint);cdecl;external;
  233. function analogRead(pin:longint):longint;cdecl;external;
  234. procedure analogWrite(pin:longint; value:longint);cdecl;external;
  235.  
  236. // PiFace specifics
  237. //  (Deprecated)
  238. //extern int  wiringPiSetupPiFace (void) ;
  239. //extern int  wiringPiSetupPiFaceForGpioProg (void) ; // Don't use this - for gpio program only
  240.  
  241. // On-Board Raspberry Pi hardware specific stuff
  242. function piBoardRev:longint;cdecl;external;
  243. // USE AT YOU OWN RISK
  244. procedure piBoardId(
  245.   var model : longint ;
  246.   var rev : longint;
  247.   var mem : longint;
  248.   var maker : longint ;
  249.   var overvolted :longint );cdecl;external;
  250. // USE AT YOUR OWN RISK
  251. function piboardtostr() : string ;
  252.  
  253. function wpiPinToGpio(wpiPin:longint):longint;cdecl;external;
  254. function physPinToGpio(physPin:longint):longint;cdecl;external;
  255. procedure setPadDrive(group:longint; value:longint);cdecl;external;
  256. function getAlt(pin:longint):longint;cdecl;external;
  257. // from getalt to array of sting
  258. function getaltpintostr(pin : longint):string;
  259.  
  260. procedure pwmToneWrite(group:longint; freq:longint);cdecl;external;
  261. procedure pwmSetMode(range:longint);cdecl;external;    
  262. procedure pwmSetRange(range:dword);cdecl;external;     //unsigned
  263. procedure pwmSetClock(divisor:longint);cdecl;external;
  264. procedure gpioClockSet(pin:longint; freq:longint);cdecl;external;
  265.  
  266. // Interrupts
  267. //  (Also Pi hardware specific)
  268. // DEPICATED use wiringPiISR
  269. function waitForInterrupt(pin:longint;mS:longint):longint;cdecl;external;
  270. function wiringPiISR(pin:longint;mode:longint ;  pcallbback : pointer):longint;cdecl;external;
  271. //  wiringPiISR
  272. function wiringPiISR_pas(pin:longint; mode:Tintlevles ;  pcallbback : pointer):longint;
  273.  
  274. // Threads
  275. //  function piThreadCreate(ptr:pointer):longint;cdecl;external;
  276. procedure piLock(key:longint);cdecl;external;
  277. procedure piUnlock(key:longint);cdecl;external;
  278.  
  279. // Schedulling priority
  280. function piHiPri(const pri:longint):longint;cdecl;external;
  281.  
  282. // Extras from arduino land
  283. procedure delay(howLong:dword);cdecl;external;
  284. procedure delayMicroseconds(howLong:dword);cdecl;external;
  285. //This returns a number representing the number of milliseconds since your
  286. //program called one of the wiringPiSetup functions. It returns an
  287. //unsigned 32-bit number which wraps after 49 days.
  288. function millis:dword;cdecl;external;
  289. function micros:dword;cdecl;external;
  290.  
  291. procedure wiringPiGpioMode(mode:longint);cdecl;external;
  292.  
  293. function wiringPiI2CSetupInterface(const device:PChar; devId: longint):longint;cdecl;external;
  294. function wiringPiI2CSetup(const devId: longint):longint;cdecl;external;
  295. function wiringPiI2CRead(fd:longint):longint;cdecl;external;
  296. function wiringPiI2CReadReg8(fd,reg:longint):longint;cdecl;external;
  297. function wiringPiI2CReadReg16(fd,reg:longint):longint;cdecl;external;
  298. function wiringPiI2CWrite(fd,data:longint):longint;cdecl;external;
  299. function wiringPiI2CWriteReg8(fd,reg,data:longint):longint;cdecl;external;
  300. function wiringPiI2CWriteReg16(fd,reg,data:longint):longint;cdecl;external;
  301.  
  302. function wiringPiSPIGetFd(channel:longint):longint;cdecl;external;
  303. function wiringPiSPIDataRW(channel:longint; data: Pointer; len:longint):longint;cdecl;external;
  304. function wiringPiSPISetup(channel,speed:longint):longint;cdecl;external;
  305.  
  306. function serialOpen(const device:PChar; const baud: longint):longint;cdecl;external;
  307. procedure serialClose(const fd:longint);cdecl;external;
  308. procedure serialFlush(const fd:longint);cdecl;external;
  309. procedure serialPutchar(const fd:longint; const c: char);cdecl;external;
  310. procedure serialPuts(const fd:longint; const s:PChar);cdecl;external;
  311. //  procedure serialPrintf(const fd:longint; const message);cdecl;external;
  312.  
  313. function serialDataAvail(const fd:longint):longint;cdecl;external;
  314. function serialGetchar(const fd:longint):longint;cdecl;external;
  315. function serialGetString(const fd:longint): String;
  316. procedure serialPutString(const fd:longint; s:string);
  317.  
  318. //extern void gpioClockSet        (int pin, int freq) ;
  319.  
  320. function max31855Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
  321. function max5322Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
  322. function mcp23008Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
  323. function mcp23016Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
  324. function mcp23017Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
  325. function mcp23s08Setup (const pinBase:longint; const spiPort:longint; const devId: longint):longint;cdecl;external;
  326. function mcp23s17Setup (const pinBase:longint; const spiPort:longint; const devId: longint):longint;cdecl;external;
  327. function mcp3002Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
  328. function mcp3004Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
  329. function mcp4802Setup (const pinBase:longint; const spiChannel:longint):longint;cdecl;external;
  330. function sn3218Setup(const pinBase:longint):longint;cdecl;external;
  331.  
  332. function softPwmCreate(pin,value,range:longint):longint;cdecl;external;
  333. procedure softPwmWrite(pin,value:longint);cdecl;external;
  334.  
  335. function pcf8574Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
  336. function pcf8591Setup(const pinBase:longint; const i2cAddress:longint):longint;cdecl;external;
  337.  
  338. procedure setBit(pin:longint);
  339. procedure clearBit(pin:longint);
  340. function getBit(pin:longint):longint;
  341.  
  342.  
  343. implementation
  344.  
  345. uses sysutils;
  346.  
  347. procedure pullUpDnControl_pas(pin : longint ; pud : tpullRisistor);
  348. begin
  349.   pullUpDnControl(pin,ord(pud));
  350. end;
  351.  
  352. procedure pinMode_pas(pin:longint; mode:TPinMode);
  353. begin
  354.      pinMode(pin, ord(mode));
  355. end;
  356.  
  357. function wiringPiISR_pas(pin:longint; mode:Tintlevles ;  pcallbback : pointer):longint;
  358. begin
  359.    result := wiringPiISR(pin,ord(mode) , pcallbback) ;
  360. end;
  361.  
  362. function piboardtostr() : string ;
  363.  var model : longint ;
  364.  var rev : longint;
  365.  var mem : longint;
  366.  var maker : longint ;
  367.  var overvolted :longint;
  368. begin
  369.   result:='Unkown';
  370.   piBoardId(model,rev,mem,maker,overvolted );
  371.   result:='MODEL: '+PI_MODELS[model]+#13#10+'REV: '+IntToStr(rev)+#13#10+'MAKER: '+PI_MAKERS[maker]+
  372.       #13#10+'MEM: '+IntToStr(mem);
  373.  // if overvolted <> 0 then
  374.  //  result := result + ' OVER VOLT' + inttostr(overvolted );
  375. end;
  376.  
  377. function getaltpintostr(pin : longint):string;
  378. begin
  379.   result := altmodes[ getAlt(pin) ];
  380. end;
  381.  
  382. function serialGetString(const fd:longint): String;
  383. begin
  384.   Result:='';
  385.   while serialDataAvail(fd)>0 do Result:=Result+chr(serialGetChar(fd));
  386. end;
  387.  
  388. procedure serialPutString(const fd:longint; s:string);
  389. begin
  390.   serialPuts(fd,PChar(s));
  391. end;
  392.  
  393. procedure setBit(pin:longint);
  394. begin
  395.   digitalWrite(pin,1);
  396. end;
  397.  
  398. procedure clearBit(pin:longint);
  399. begin
  400.   digitalWrite(pin,0);
  401. end;
  402.  
  403. function getBit(pin:longint):longint;
  404. begin
  405.   Result:=digitalRead(pin);
  406. end;
  407.  
  408. end.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: ccrause on May 10, 2021, 12:05:01 pm
Gordon has deprecated wiringPi (http://wiringpi.com/wiringpi-deprecated/).  Obviously the library didn't become useless overnight, but I guess as time passes users will miss out on support for newer RPi models (beyond 4B).  In future perhaps consider one of the alternative hardware access methods (https://wiki.lazarus.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware) mentioned in the wiki.
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: MarkMLl on May 10, 2021, 01:45:39 pm
Gordon has deprecated wiringPi (http://wiringpi.com/wiringpi-deprecated/).  Obviously the library didn't become useless overnight, but I guess as time passes users will miss out on support for newer RPi models (beyond 4B).  In future perhaps consider one of the alternative hardware access methods (https://wiki.lazarus.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware) mentioned in the wiki.

Noting also that hardware access via /sys/class/gpio has itself been deprecated in favour of /dev/gpiochip, and that /dev/mem is specific to RPi-like boards while /sys/class/gpio and /dev/gpiochip are supported by the standard Linux kernel.

MarkMLl
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: avra on May 11, 2021, 08:58:29 am
Gordon has deprecated wiringPi (http://wiringpi.com/wiringpi-deprecated/).  Obviously the library didn't become useless overnight, but I guess as time passes users will miss out on support for newer RPi models (beyond 4B).
WiringPi has been forked and maintained, having also a Debian package. We'll see what happens. According to number of commits and issues solved (33 closed) - things do not look that bad.

https://github.com/WiringPi/WiringPi
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: ccrause on May 11, 2021, 10:35:01 am
Gordon has deprecated wiringPi (http://wiringpi.com/wiringpi-deprecated/).  Obviously the library didn't become useless overnight, but I guess as time passes users will miss out on support for newer RPi models (beyond 4B).
WiringPi has been forked and maintained, having also a Debian package. We'll see what happens. According to number of commits and issues solved (33 closed) - things do not look that bad.

https://github.com/WiringPi/WiringPi
Thanks, good to know it is alive somewhere else!
Title: Re: Lazarus / RaspberyPi Hardware Library
Post by: avra on May 11, 2021, 02:53:44 pm
Gordon has deprecated wiringPi (http://wiringpi.com/wiringpi-deprecated/).  Obviously the library didn't become useless overnight, but I guess as time passes users will miss out on support for newer RPi models (beyond 4B).
WiringPi has been forked and maintained, having also a Debian package. We'll see what happens. According to number of commits and issues solved (33 closed) - things do not look that bad.

https://github.com/WiringPi/WiringPi
Thanks, good to know it is alive somewhere else!

I have updated the wiki for future reference:
https://wiki.lazarus.freepascal.org/Lazarus_on_Raspberry_Pi#External_Links
TinyPortal © 2005-2018