From: Bob Goddard <1963bib@googlemail.com>
Subject: Re: GNATCOLL-Mmap example
Date: Tue, 28 Jan 2020 10:53:53 -0800 (PST)
Date: 2020-01-28T10:53:53-08:00 [thread overview]
Message-ID: <d62dca95-7349-47eb-8858-46980fbb0088@googlegroups.com> (raw)
In-Reply-To: <r0or5j$7t3$1@gioia.aioe.org>
On Tuesday, 28 January 2020 08:25:25 UTC, Dmitry A. Kazakov wrote:
> On 2020-01-25 22:52, Bob Goddard wrote:
>
> > I need to map the RPi's GPIO memory addresses so that I can do what I need to do.
>
> See files under:
>
> /sys/class/gpio
>
> You can use standard Ada I/O to read and set GPIO pins. E.g.
>
> /sys/class/gpio/gpio/XXX/value
This was the way I started, but could not get it to read anything. It just blocks trying to read a single character. My c code works fine using the wiringPi libs.
Stephen Leake:
Mmap is used to read more than just disk files. Opening /dev/mem, or even /dev/gpiomem, is just another file.
Regardless, I got it to work. The moral of the story, is never assume example code in the library source works, or even can compile. I got it running a couple of days ago, and have since spent time banging my head against a wall.
This works, both with System.Mmap & GNATCOLL.Mmap...
with Ada.Unchecked_Conversion;
with Ada.Text_IO;
with GNATCOLL.Mmap; use GNATCOLL.Mmap;
with Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO;
procedure Mmaptest is
package Mmap renames GNATCOLL.Mmap;
File : Mmap.Mapped_File;
Reg : Mmap.Mapped_Region;
Str : Mmap.Str_Access;
Offs : Mmap.File_Size := 0;
Page : constant Mmap.File_Size := Mmap.File_Size (Mmap.Get_Page_Size);
begin
File := Mmap.Open_Write ("/tmp/file_on_disk");
while Offs < Mmap.Length (File) loop
Mmap.Read (File, Reg, Offs, Length => Page, Mutable => False);
Str := Mmap.Data (Reg);
Ada.Strings.Unbounded.Text_IO.Put (Ada.Strings.Unbounded.To_Unbounded_String (
Str (Integer (Offs - Mmap.Offset (Reg)) + 1 .. Mmap.Last (Reg)))
);
Offs := Offs + Page; -- Mmap.File_Size (Mmap.Last (Reg));
end loop;
Mmap.Free (Reg);
if Reg /= Mmap.Invalid_Mapped_Region then
Ada.Text_IO.Put_Line ("Mapped region still valid");
end if;
Mmap.Close (File);
if File /= Mmap.Invalid_Mapped_File then
Ada.Text_IO.Put_Line ("Mapped file still valid");
end if;
end Mmaptest;
next prev parent reply other threads:[~2020-01-28 18:53 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-01-25 21:52 GNATCOLL-Mmap example Bob Goddard
2020-01-28 2:10 ` Stephen Leake
2020-01-28 5:59 ` J-P. Rosen
2020-01-28 8:25 ` Dmitry A. Kazakov
2020-01-28 18:53 ` Bob Goddard [this message]
2020-01-28 20:07 ` Dmitry A. Kazakov
2020-01-28 20:17 ` Bob Goddard
2020-01-29 8:19 ` Dmitry A. Kazakov
2020-01-29 21:56 ` Bob Goddard
2020-01-30 2:45 ` Dennis Lee Bieber
2020-01-31 16:59 ` Björn Lundin
2020-01-28 19:11 ` Philip Munts
2020-01-28 20:15 ` Dmitry A. Kazakov
2020-01-28 20:18 ` Philip Munts
2020-01-28 22:06 ` Bob Goddard
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox