comp.lang.ada
 help / color / mirror / Atom feed
From: "Björn Lundin" <b.f.lundin@gmail.com>
Subject: Re: GNAT/Ada on Raspberry Pi 3
Date: Sun, 8 Jul 2018 15:20:49 +0200
Date: 2018-07-08T15:20:49+02:00	[thread overview]
Message-ID: <pht334$t55$1@dont-email.me> (raw)
In-Reply-To: <39e66beb-921e-492c-b24f-0d9b281edc65@googlegroups.com>

On 2018-07-06 23:15, dontspam@dontspam.no wrote:
> Hi!
> I have a Raspberry Pi 3 with
> Linux 4.4.34-v7 armv7l / Raspbian GNU/Linux 8.0 (jessie) 
> 
> I have successfully installed GNAT 4.9.2 and I have created and executed a "Hello World" program on the device.
> 
> From Python it is very convenient to operate the GPIOs but I haven't found something for Ada. Does it exists some package/library that can do this from Ada? It is not very important if it requires a different distro.
> 
> Frank
> 

It's easy to bind to the most comon c-api - http://wiringpi.com/


below is not the full api - just enough to write to a pin
call setup once first, then set pin-mode out, then write to it.
To read, set pin-mode in, and call read (which is not in the package yet)

http://wiringpi.com/ has good documentation



---spec

with Interfaces.C; use Interfaces.C;

package Gpio is

  -- wiringPi modes

  Wpi_Mode_Pins           : constant Interfaces.C.Int :=  0;
  Wpi_Mode_Gpio           : constant Interfaces.C.Int :=  1;
  Wpi_Mode_Gpio_Sys       : constant Interfaces.C.Int :=  2;
  Wpi_Mode_Phys           : constant Interfaces.C.Int :=  3;
  Wpi_Mode_Piface         : constant Interfaces.C.Int :=  4;
  Wpi_Mode_Uninitialised  : constant Interfaces.C.Int := -1;

  -- Pin modes

  Input                   : constant Interfaces.C.Int := 0;
  Output                  : constant Interfaces.C.Int := 1;
  Pwm_Output              : constant Interfaces.C.Int := 2;
  Gpio_Clock              : constant Interfaces.C.Int := 3;
  Soft_Pwm_Output         : constant Interfaces.C.Int := 4;
  Soft_Tone_Output        : constant Interfaces.C.Int := 5;
  Pwm_Tone_Output         : constant Interfaces.C.Int := 6;

  Low                     : constant Interfaces.C.Int := 0;
  High                    : constant Interfaces.C.Int := 1;

  -- Pull up/down/none

  Pud_Off                 : constant Interfaces.C.Int := 0;
  Pud_Down                : constant Interfaces.C.Int := 1;
  Pud_Up                  : constant Interfaces.C.Int := 2;

  -- PWM

  Pwm_Mode_Ms             : constant Interfaces.C.Int := 0;
  Pwm_Mode_Bal            : constant Interfaces.C.Int := 1;

  -- Interrupt levels

  Int_Edge_Setup          : constant Interfaces.C.Int := 0;
  Int_Edge_Falling        : constant Interfaces.C.Int := 1;
  Int_Edge_Rising         : constant Interfaces.C.Int := 2;
  Int_Edge_Both           : constant Interfaces.C.Int := 3;

  Bad_Gpio_Call : exception;

  procedure Setup ;

  procedure Pin_Mode(Pin : Interfaces.C.Int ; Mode : Interfaces.C.Int) ;

  procedure Digital_Write(Pin : Interfaces.C.Int; Value : Boolean);


private
  pragma Import(C, Pin_Mode, "pinMode");


end Gpio;


----------
--body

with Ada.Environment_Variables;

package body Gpio is

  ---------------------------------------------------------
  procedure Setup is
    R : Int := 0;

    function Wiring_Pi_Setup_Gpio return Interfaces.C.Int ;
    pragma Import(C, Wiring_Pi_Setup_Gpio, "wiringPiSetupGpio");

  begin --http://wiringpi.com/reference/setup/
    -- If you want to restore the v1 behaviour, then you need to set the
environment variable: WIRINGPI_CODES
    -- to any value
    Ada.Environment_Variables.Set("WIRINGPI_CODES","1");
    R := Wiring_Pi_Setup_Gpio;
    if R /= 0 then
      raise Bad_Gpio_Call with "Wiring_Pi_Setup_Gpio" & R'Img;
    end if;
  end Setup;
  ---------------------------------------------------------
  procedure Digital_Write(Pin : Interfaces.C.Int; Value : Boolean) is
    procedure Digital_Write(Pin : Interfaces.C.Int; Value
:Interfaces.C.Int);
    pragma Import(C, Digital_Write, "digitalWrite");

  begin
    if Value then
      Digital_Write(Pin, High);
    else
      Digital_Write(Pin, Low);
    end if;
  end Digital_Write;
  ------------------------------------------------------
end Gpio;
 ----------




-- 
--
Björn


  parent reply	other threads:[~2018-07-08 13:20 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-06 21:15 GNAT/Ada on Raspberry Pi 3 dontspam@dontspam.no
2018-07-06 23:32 ` Philip Munts
2018-07-07  2:30 ` Dennis Lee Bieber
2018-07-07  8:30 ` Dmitry A. Kazakov
2018-07-08  0:00   ` Philip Munts
2018-07-08 13:20 ` Björn Lundin [this message]
2018-07-08 16:53 ` dontspam@dontspam.no
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox