Bookstore

Recent

Author Topic: Location for WiringPi on the Raspberry Pi  (Read 16786 times)

Bob1960evens

  • New member
  • *
  • Posts: 8
Location for WiringPi on the Raspberry Pi
« on: December 28, 2013, 07:55:16 pm »
I am new to the Raspberry Pi, although experienced in Delphi / Windows. I am running Lazarus IDE v0.9.30.4-6, and downloaded wiringPi and the hwiringPi wrapper for it. I have a simple program.

Code: [Select]
Uses ..., hwiringPi;
...
procedure TForm1.vbSetDigitalClick (Sender : TObject);
begin
DigitalWrite (P12, 1);
end;

hwiringPi.pas includes the line
  {$link ./wiringPi/wiringPi.o}

Initially on compile it could not find wiringPi.o, so I changed it to
  {$link ../wiringPi/wiringPi/wiringPi.o}

If I comment out the DigitalWrite request, the project compiles. When I uncomment the DigitalWrite, it fails with

  wiringPi.c:(.text+0x1d04): undefined reference to 'piHiPri'

I have tried rebuilding the library, and moving it so that the
  {$link ./wiringPi/wiringPi.o}
was correct, but to no avail. I suspect some paths are wrong somewhere, but have run out of options to try. Has anyone got any suggestions?

Bob1960evens

  • New member
  • *
  • Posts: 8
Re: Location for WiringPi on the Raspberry Pi
« Reply #1 on: January 01, 2014, 11:38:05 am »
After a lot more searching, I found that there is no piHiPri.h file. I tried writing one and adding it in to various files, but still no success. I don't think I know enough about C to really understand what I was doing. Finally, I copied the code for the single function in piHiPri.c into wiringPi.c, renamed it slightly to avoid any naming clash, and now I can compile a program which uses wiringPi.o. Maybe not the best solution, but at least it works.
« Last Edit: January 01, 2014, 01:24:00 pm by Bob1960evens »

jwdietrich

  • Hero Member
  • *****
  • Posts: 1078
    • formatio reticularis
Re: Location for WiringPi on the Raspberry Pi
« Reply #2 on: January 01, 2014, 12:31:27 pm »
The advantage of WiringPi is that it provides an Arduino-compatible numbering scheme, but it isn't necessary to access external hardware from Lazarus and Free Pascal. If you don't want to port software from the Arduino you will not need WiringPi.

I use native hardware access via the BaseUnix unit, and it works flawlessly. See http://wiki.lazarus.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware for details and an example.
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

avra

  • Hero Member
  • *****
  • Posts: 1828
    • Additional info
Re: Location for WiringPi on the Raspberry Pi
« Reply #3 on: January 01, 2014, 12:48:31 pm »
Can you compile raspberry.lpr example in lazwiringpi wrapper lib?

If all you want is simple digital pins access then there are also other alternative methods:
http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#Accessing_external_hardware
« Last Edit: January 01, 2014, 12:50:14 pm by avra »
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Bob1960evens

  • New member
  • *
  • Posts: 8
Re: Location for WiringPi on the Raspberry Pi
« Reply #4 on: January 01, 2014, 02:06:53 pm »
Thanks for the pointers. I had already found the article, and am handling the pin writes and reads as you suggest. However, I also need to drive the SPI bus, to read some analog inputs. I have not found anything on native code to do this, and wiringPi includes an SPI interface which works well. I looked at the wiringPi source code to see if I could do it in pascal, but couldn't work out how to get at the low level libraries that it imports.

Regarding the lazwiringpi example, that uses an older version of wiringPi, which does not include the SPI interface.
« Last Edit: January 01, 2014, 02:09:58 pm by Bob1960evens »

avra

  • Hero Member
  • *****
  • Posts: 1828
    • Additional info
Re: Location for WiringPi on the Raspberry Pi
« Reply #5 on: January 01, 2014, 03:49:05 pm »
Then you have two choices:
1) Extend WiringPi wrapper with missing SPI functions and link with latest wiringPi.o instead of the old one. From https://projects.drogon.net/raspberry-pi/wiringpi/spi-library it seams that you need to add wiringPiSPISetup and wiringPiSPIDataRW functions to the wrapper, converting their declarations from C to Pascal. There are many tutorials on this topic and in such simple case even convertor tools should be able to do the job. For a help, take a look at original declarations of existing functions and their pascal variants, and you'll get the idea.
2) Use rpihal lib which also supports SPI:
http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#4._rpi_hal-Hardware_Abstraction_Library_.28GPIO.2C_I2C_and_SPI_functions_and_procedures.29
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Bob1960evens

  • New member
  • *
  • Posts: 8
Re: Location for WiringPi on the Raspberry Pi
« Reply #6 on: January 01, 2014, 11:51:25 pm »
Thanks again. However, I decided it must be possible to code it in pascal from scratch, and once I found out the structure of the spi_ioc_transfer record, decided that fpioctl implemented the ioctl procedures from wiringPi's code, and found a way to generate all the constants used by the fpioctl function, the job was nearly done. I now have some native pascal spi helper functions just like those in wiringPi. When it's tidied up and tested to destruction, I'll post it somewhere.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1078
    • formatio reticularis
Re: Location for WiringPi on the Raspberry Pi
« Reply #7 on: January 02, 2014, 12:22:19 am »
Would you mind to expand the wiki page to reflect your experiences?
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

Bob1960evens

  • New member
  • *
  • Posts: 8
Re: Location for WiringPi on the Raspberry Pi
« Reply #8 on: January 02, 2014, 11:07:48 am »
Which wiki page is that?

avra

  • Hero Member
  • *****
  • Posts: 1828
    • Additional info
Re: Location for WiringPi on the Raspberry Pi
« Reply #9 on: January 02, 2014, 03:02:29 pm »
... it must be possible to code it in pascal from scratch...
rpihal lib I mentioned does exactly that, does not depend on any C lib, and implements SPI. However, one more RPi IO FPC lib won't hurt anyone ;-)
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

jwdietrich

  • Hero Member
  • *****
  • Posts: 1078
    • formatio reticularis
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

jwdietrich

  • Hero Member
  • *****
  • Posts: 1078
    • formatio reticularis
Re: Location for WiringPi on the Raspberry Pi
« Reply #11 on: January 02, 2014, 09:42:00 pm »
I guess that basic SPI read and write functions could possibly be like:

Code: [Select]
var
  gReturnCode: longint; {stores the result of the IO operation}
 
procedure TForm1.GPIO17ToggleBoxChange(Sender: TObject, theSignal: byte);
var
  fileDesc: integer;
begin
  try
    fileDesc := fpopen('/dev/spidev0.0', O_WrOnly);
    { replace "/dev/spidev0.0" by "/dev/spidev0.1" for device at CS1 }
    gReturnCode := fpwrite(fileDesc, theSignal, 1);
  finally
    gReturnCode := fpclose(fileDesc);
  end;
end;

procedure TForm1.ApplicationProperties1Idle(Sender: TObject; var theSignal: byte);
var
  fileDesc: integer;
  buttonStatus: string[1] = '1';
begin
  try
    fileDesc := fpopen('/dev/spidev0.0', O_RdOnly);
    { replace "/dev/spidev0.0" by "/dev/spidev0.1" for device at CS1 }
    if fileDesc > 0 then
    begin
      gReturnCode := fpread(fileDesc, theSignal, 1);
    end;
  finally
    gReturnCode := fpclose(fileDesc);
  end;
end;

The code is, however, completely untested, since I don't have SPI devices for testing. You should therefore expect to change it in some respect.

Of course, the SPI port needs first to be enabled in Raspbian before it can be used. See http://www.raspberry-projects.com/pi/pi-operating-systems/raspbian/io-pins-raspbian/spi-pins for a description.
« Last Edit: January 02, 2014, 10:54:43 pm by jwdietrich »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

Bob1960evens

  • New member
  • *
  • Posts: 8
Re: Location for WiringPi on the Raspberry Pi
« Reply #12 on: January 03, 2014, 01:13:20 am »
While there might be some SPI devices that would respond to read and write commands, many of the DtoA and AtoD chips don't. They use a full-duplex mechanism, where in order to read data back, you have to send data to the device. To read the 3202 AtoD chip, for instance, I have to send 5 bits to request the data, and then another 12 to enable it to send the result back.

jwdietrich

  • Hero Member
  • *****
  • Posts: 1078
    • formatio reticularis
Re: Location for WiringPi on the Raspberry Pi
« Reply #13 on: January 03, 2014, 08:50:44 am »
function GetRandomNumber: integer; // xkcd.com
begin
  GetRandomNumber := 4; // chosen by fair dice roll. Guaranteed to be random.
end;

http://www.formatio-reticularis.de

Lazarus 2.0.6 | FPC 3.0.4 | PPC, Intel, ARM | macOS, Windows, Linux

Bob1960evens

  • New member
  • *
  • Posts: 8
Re: Location for WiringPi on the Raspberry Pi
« Reply #14 on: January 03, 2014, 10:22:16 am »
Both of the above examples are very similar to what I have already done. They use the spi_ioc_transfer structure, ioctl, and the messages that ioctl requires.