On Tue, 11 May 2021 09:43:49 -0700, in gmane.comp.hardware.beagleboard.user
"John Dammeyer" <[email protected]> wrote:


>To try this program do sudo apt install lazarus. 
>
        I think not... Not for an experiment at least. No offence intended, but
it wants to install over 1GB of stuff -- which is a 33% increase in the
content of my uSD card!, and would require me to attach a keyboard and
display to work with the GUI.

        Now, a simple command line only program would be a different matter
(does FPC support command line only? -- FPC may be small enough to justify
installing for testing via SSH, but not Lazarus... *** nope, just FPC is
most of the load, 900+MB)

        An example using GNAT (Ada). There is no (or I don't know of one)
library for GPIO access in GNAT Ada, so everything is SysFS I/O. I had to
set the pin number to string as the simple integer'image(pin) was
generating leading spaces, which resulted in invalid file name. Everything
is in one (117 line) file -- no nasty forms, etc.

debian@beaglebone:~/BBB_IO$ ls
main.adb
debian@beaglebone:~/BBB_IO$ cat main.adb
with Ada.Text_IO;         use Ada.Text_IO;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is

   Sysfs_Path : constant String := "/sys/class/gpio";
   GPIO_Pin   : constant String := "48";
   Pin_Value  : Integer;

   procedure Export_Pin (Pin : in String) is

      Export_File : File_Type;
      Export_Path : constant String := Sysfs_Path & "/export";

   begin
      Put_Line ("Opening " & Export_Path);
      Open (Export_File, Mode => Out_File, Name => Export_Path);

      Put_Line ("Writing pin number: " & Pin);
      Put (Export_File, Pin);

      Put_Line ("Closing " & Export_Path);
      Close (Export_File);

      New_Line;

   end Export_Pin;

   procedure Set_Direction (Pin : in String; Direction : in String) is
      Pin_File : File_Type;
      Pin_Path : constant String := Sysfs_Path & "/gpio" & Pin &
"/direction";

   begin
      Put_Line ("Opening " & Pin_Path);
      Open (Pin_File, Mode => Out_File, Name => Pin_Path);

      Put_Line ("Writing direction: " & Direction);
      Put (Pin_File, Direction);

      Put_Line ("Closing " & Pin_Path);
      Close (Pin_File);

      New_Line;

   end Set_Direction;

   procedure Set_Value (Pin : in String; Value : in Integer) is

      Pin_File : File_Type;
      Pin_Path : constant String := Sysfs_Path & "/gpio" & Pin & "/value";

   begin
      Put_Line ("Opening " & Pin_Path);
      Open (Pin_File, Mode => Out_File, Name => Pin_Path);

      Put_Line ("Writing value: " & Integer'Image (Value));
      Put (Pin_File, Value, Width => 1);

      Put_Line ("Closing " & Pin_Path);
      Close (Pin_File);

      New_Line;

   end Set_Value;

   procedure Get_Value (Pin : in String; Value : out Integer) is

      Pin_File : File_Type;
      Pin_Path : constant String := Sysfs_Path & "/gpio" & Pin & "/value";
      In_Value : String          := " ";

   begin
      Put_Line ("Opening " & Pin_Path);
      Open (Pin_File, Mode => In_File, Name => Pin_Path);

      Put_Line ("Reading value");
      Get (Pin_File, Value, Width => 0);

      Put_Line ("Closing " & Pin_Path);
      Close (Pin_File);

      New_Line;

   end Get_Value;

begin
   -- Believe the pins are already exported in Buster
   --Export_Pin (Gpio_Pin);

   Get_Value (GPIO_Pin, Pin_Value);
   Put_Line
     ("Current value of " & GPIO_Pin & " is " & Integer'Image (Pin_Value));
   New_Line;

   Set_Direction (GPIO_Pin, "out");

   New_Line;

   Get_Value (GPIO_Pin, Pin_Value);
   Put_Line
     ("Current value of " & GPIO_Pin & " is " & Integer'Image (Pin_Value));

   New_Line;

   Set_Value (GPIO_Pin, 0);
   Get_Value (GPIO_Pin, Pin_Value);
   Put_Line
     ("Current value of " & GPIO_Pin & " is " & Integer'Image (Pin_Value));

   New_Line;

   Set_Value (GPIO_Pin, 1);
   Get_Value (GPIO_Pin, Pin_Value);
   Put_Line
     ("Current value of " & GPIO_Pin & " is " & Integer'Image (Pin_Value));

end Main;
debian@beaglebone:~/BBB_IO$

debian@beaglebone:~/BBB_IO$ gnatmake main
arm-linux-gnueabihf-gcc-8 -c main.adb
arm-linux-gnueabihf-gnatbind-8 -x main.ali
arm-linux-gnueabihf-gnatlink-8 main.ali
debian@beaglebone:~/BBB_IO$
debian@beaglebone:~/BBB_IO$ ls
main  main.adb  main.ali  main.o
debian@beaglebone:~/BBB_IO$
debian@beaglebone:~/BBB_IO$ ./main
Opening /sys/class/gpio/gpio48/value
Reading value
Closing /sys/class/gpio/gpio48/value

Current value of 48 is  1

Opening /sys/class/gpio/gpio48/direction
Writing direction: out
Closing /sys/class/gpio/gpio48/direction


Opening /sys/class/gpio/gpio48/value
Reading value
Closing /sys/class/gpio/gpio48/value

Current value of 48 is  0

Opening /sys/class/gpio/gpio48/value
Writing value:  0
Closing /sys/class/gpio/gpio48/value

Opening /sys/class/gpio/gpio48/value
Reading value
Closing /sys/class/gpio/gpio48/value

Current value of 48 is  0

Opening /sys/class/gpio/gpio48/value
Writing value:  1
Closing /sys/class/gpio/gpio48/value

Opening /sys/class/gpio/gpio48/value
Reading value
Closing /sys/class/gpio/gpio48/value

Current value of 48 is  1
debian@beaglebone:~/BBB_IO$

         Again, under a Buster IoT image (no X-Window overhead), NO SUDO NEEDED
FOR GPIO48 -- and it is already exported so I could even skip the export
call.

        I'm not going to try to configure a SPI device for testing.


-- 
Dennis L Bieber

-- 
For more options, visit http://beagleboard.org/discuss
--- 
You received this message because you are subscribed to the Google Groups 
"BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/beagleboard/cr0m9g5jld35qrvviacghq4ut5o2aivj9q%404ax.com.

Reply via email to