GPIO...
lazarus / 13-General
----------------------------------------------------------------------
krolikbest · 2016-11-25 09:01
Hello,
i've installed wiringPi and RTB (Return To Basic) in order to test
GPIO. That works even as a pi user (I can on/off gpio pins).
Then i decided to try it with Lazarus. So downloaded lazwirinpi from
there http://forum.lazarus.freepascal.org/index.php/topic,17404.0.html
. Afetr unpacking i compiled: gcc -c wiringPi.c. In Lazarus->Project
Options->Paths->Library set new path to this folder (lazwiringpi) and
the same with path to units.
Then i wrote simple code in command line program:
program test1;
uses hwiringPi;
begin
if wiringPiSetup<>-1 then
begin
pinMode(P11, 1); // instead of P11 I tried 0 too
digitalWrite(P11,1); // instead of P11 I tried 0 too
end
else
Writeln('Some error. ');
Try to run in shell as a pi user and get the error "/dev/mem acces
denied". Ok. Try as a root.
Now didn't get error with "acces denied", but nothing has happened. I
expected at least diode (P11 or 0) "on".
Why I can do it with RTB? Maybe lazwiringPi is not right library in
this case, maybe should I look for wiringPi library installed with
RTB?
I have no idea what to do..
Some help?
Martin
----------------------------------------------------------------------
feds · 2016-11-25 20:22
Hi
Never tried lazwirinpi (or anything else to control ports on the PI).
But mybe you should have a look at:
http://wiki.freepascal.org/Lazarus_on_Raspberry_Pi#1._Native_hardware_
access
There is a exhaustive discussion (including code samples) about
handling GPIOs on the PI.
If you can't get it to work then please let me know. Maybe i can try
to reproduce it here.
Regards
feds
----------------------------------------------------------------------
krolikbest · 2016-11-26 20:56
I can compile and run as a sudo user program which looks like:
procedure TForm1.Button1Click(Sender: TObject);
begin
if wiringPiSetup <> -1 then
begin
pinMode(0,output);
digitalWrite(0,1);
end;
end;
but it doesn't affect gpio.
I can also read state of pins, but regardless of real state (e.g. pin0
is ON (1)) always get 0(OFF)
Hmm..
It looks like I operate on some virtual gpio
----------------------------------------------------------------------
krolikbest · 2016-11-27 13:05
I had to give up. Instead I've tested rpi_hal and it works.
Martin
----------------------------------------------------------------------
feds · 2016-11-27 18:50
Quote from: krolikbest on November 27, 2016, 02:05:23 pmI had to give
up. Instead I've tested rpi_hal and it works.
Martin
Hmm, have you tried the link that i posted?
I just make a short hack to test it:
Code: [Select]program io_test;
{$mode objfpc}{$H+}
uses
sysutils, unix, baseunix;
type
tDirection = (input, output);
tValue = (OFF, ON);
Function PinActivate(port : Cardinal):Cint;
var
fd : integer;
s : string;
begin
try
fd:= fpopen('/sys/class/gpio/export', O_WrOnly);
s:=IntToStr(port);
fpwrite(fd, pChar(s), length(s) );
finally
result := fpclose(fd);
end;
end;
Function PinDeactivate(port : Cardinal):Cint;
var
fd : integer;
s : string;
begin
try
fd:= fpopen('/sys/class/gpio/unexport', O_WrOnly);
s:=IntToStr(port);
fpwrite(fd, pChar(s), length(s) );
finally
result := fpclose(fd);
end;
end;
Function PinDirection(port : Cardinal; direction : tDirection):Cint;
var
fd : integer;
s : string;
begin
s:='/sys/class/gpio/gpio'+IntToStr(port)+'/direction';
try
fd:= fpopen(s, O_WrOnly);
if direction = output then
fpwrite(fd, pChar('out'), 3 )
else
fpwrite(fd, pChar('in'), 2 );
finally
result := fpclose(fd);
end;
end;
Function PinValue(port : Cardinal; value : tValue):Cint;
var
fd : integer;
s : string;
begin
s:='/sys/class/gpio/gpio'+IntToStr(port)+'/value';
try
fd:= fpopen(s, O_WrOnly);
if value=ON then s:='1'
else s:='0';
fpwrite(fd, pChar(s), 1 );
finally
result := fpclose(fd);
end;
end;
begin
try
PinActivate(4);
PinDirection(4, OUTPUT);
while true do
begin
PinValue(4, ON);
PinValue(4, OFF);
end;
finally
PinDeactivate(4);
end;
end.
Works like a charm and doesn't need any external stuff.
Obviously it should have some error handling added and be packaged
into a unit. But take it as simple "proof of concept".
/regards
P.S:
Sorry, the png is low quality (but only ~3.5K in size
----------------------------------------------------------------------
krolikbest · 2016-11-28 08:13
Hi,
i'll play with direct access as a next step. at the moment i try to
understand different ways to use gpio. Thank you for help. I'll report
my 'progress'
----------------------------------------------------------------------
krolikbest · 2016-12-11 20:06
Finalli i've got wiringpi to work with RPi3. It was really simply, one
have to download this
http://forum.lazarus.freepascal.org/index.php?action=dlattach;topic=17
404.0;attach=11201 then unpack, e.g. in /home/pi/laz2wiring and then
in your project option set proper path to file called
"h2wiringpi.pas", so path looks like "/home/pi/laz2wiring". This file
you should use in uses clause. That all.
In fact I didn't want to dive into direct controll of GPIO based on
unix unit. I looked for something simply and despite of my first
apporach finally succeeded.