Recent

Author Topic: GPIO...  (Read 6704 times)

krolikbest

  • Full Member
  • ***
  • Posts: 246
GPIO...
« on: November 25, 2016, 10:01:34 am »
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:
<code>

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. ');
</code>
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

  • New Member
  • *
  • Posts: 33
Re: GPIO...
« Reply #1 on: November 25, 2016, 09:22:00 pm »
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

  • Full Member
  • ***
  • Posts: 246
Re: GPIO...
« Reply #2 on: November 26, 2016, 09:56:08 pm »
I can compile and run as a sudo user program which looks like:
<code>
procedure TForm1.Button1Click(Sender: TObject);
begin                                   
  if wiringPiSetup <> -1 then
  begin
   pinMode(0,output);
   digitalWrite(0,1);
  end;
end;
</code>
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

  • Full Member
  • ***
  • Posts: 246
Re: GPIO...
« Reply #3 on: November 27, 2016, 02:05:23 pm »
I had to give up. Instead I've tested rpi_hal and it works.

Martin

feds

  • New Member
  • *
  • Posts: 33
Re: GPIO...
« Reply #4 on: November 27, 2016, 07:50:38 pm »
I 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

  • Full Member
  • ***
  • Posts: 246
Re: GPIO...
« Reply #5 on: November 28, 2016, 09:13:04 am »
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

  • Full Member
  • ***
  • Posts: 246
Re: GPIO...
« Reply #6 on: December 11, 2016, 09:06:56 pm »
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=17404.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. <SOLVED>

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.

 

TinyPortal © 2005-2018