aboutsummaryrefslogtreecommitdiff
path: root/gcc/ada/g-sercom-linux.adb
diff options
context:
space:
mode:
authorPascal Obry <obry@adacore.com>2008-03-26 08:40:18 +0100
committerArnaud Charlet <charlet@gcc.gnu.org>2008-03-26 08:40:18 +0100
commitf88ecba0bc8dca1af95ba6ff29de6c2c263c5b76 (patch)
tree28343910375ab6a065b5ea0288fb08858441c855 /gcc/ada/g-sercom-linux.adb
parent4e9f48a126a0812b87a7dba7dfcd11d441c48e80 (diff)
downloadgcc-f88ecba0bc8dca1af95ba6ff29de6c2c263c5b76.zip
gcc-f88ecba0bc8dca1af95ba6ff29de6c2c263c5b76.tar.gz
gcc-f88ecba0bc8dca1af95ba6ff29de6c2c263c5b76.tar.bz2
Makefile.in: Add proper GNAT.Serial_Communications implementation on supported platforms.
2008-03-26 Pascal Obry <obry@adacore.com> * Makefile.in: Add proper GNAT.Serial_Communications implementation on supported platforms. * Makefile.rtl: Add g-sercom.o. * impunit.adb: Add g-sercom.adb. * s-crtl.ads (open): New routine. (close): Likewise. (write): Likewise. * s-osinte-mingw.ads (BYTE): New type. (CHAR): Likewise. (OVERLAPPED): Likewise. (GENERIC_READ): New constant. (GENERIC_WRITE): Likewise. (OPEN_EXISTING): Likewise. (PSECURITY_ATTRIBUTES): Removed this type, use anonymous access type instead. (CreateFile): New routine. (WriteFile): Likewise. (ReadFile): Likewise. (CloseHandle): Move next to the other file oriented routines. * g-sercom.ads: New unit. * g-sercom.adb: Default implementation, calls to this unit will raise a program error exception. * g-sercom-mingw.adb, g-sercom-linux.adb: Windows and GNU/Linux implementations. From-SVN: r133569
Diffstat (limited to 'gcc/ada/g-sercom-linux.adb')
-rw-r--r--gcc/ada/g-sercom-linux.adb289
1 files changed, 289 insertions, 0 deletions
diff --git a/gcc/ada/g-sercom-linux.adb b/gcc/ada/g-sercom-linux.adb
new file mode 100644
index 0000000..bcb5952
--- /dev/null
+++ b/gcc/ada/g-sercom-linux.adb
@@ -0,0 +1,289 @@
+------------------------------------------------------------------------------
+-- --
+-- GNAT COMPILER COMPONENTS --
+-- --
+-- G N A T . S E R I A L _ C O M M U N I C A T I O N S --
+-- --
+-- B o d y --
+-- --
+-- Copyright (C) 2007-2008, AdaCore --
+-- --
+-- GNAT is free software; you can redistribute it and/or modify it under --
+-- terms of the GNU General Public License as published by the Free Soft- --
+-- ware Foundation; either version 2, or (at your option) any later ver- --
+-- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
+-- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
+-- or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License --
+-- for more details. You should have received a copy of the GNU General --
+-- Public License distributed with GNAT; see file COPYING. If not, write --
+-- to the Free Software Foundation, 51 Franklin Street, Fifth Floor, --
+-- Boston, MA 02110-1301, USA. --
+-- --
+-- As a special exception, if other files instantiate generics from this --
+-- unit, or you link this unit with other files to produce an executable, --
+-- this unit does not by itself cause the resulting executable to be --
+-- covered by the GNU General Public License. This exception does not --
+-- however invalidate any other reasons why the executable file might be --
+-- covered by the GNU Public License. --
+-- --
+-- GNAT was originally developed by the GNAT team at New York University. --
+-- Extensive contributions were provided by Ada Core Technologies Inc. --
+-- --
+------------------------------------------------------------------------------
+
+-- This is the GNU/Linux implementation of this package
+
+with Ada.Streams; use Ada.Streams;
+with Ada; use Ada;
+with Ada.Unchecked_Deallocation;
+
+with System.CRTL; use System, System.CRTL;
+
+with GNAT.OS_Lib; use GNAT.OS_Lib;
+
+package body GNAT.Serial_Communications is
+
+ type Port_Data is new int;
+
+ subtype unsigned is Interfaces.C.unsigned;
+ subtype char is Interfaces.C.char;
+ subtype unsigned_char is Interfaces.C.unsigned_char;
+
+ function fcntl (fd : int; cmd : int; value : int) return int;
+ pragma Import (C, fcntl, "fcntl");
+
+ O_RDWR : constant := 8#02#;
+ O_NOCTTY : constant := 8#0400#;
+ O_NDELAY : constant := 8#04000#;
+ FNDELAY : constant := O_NDELAY;
+ F_SETFL : constant := 4;
+ TCSANOW : constant := 0;
+ TCIFLUSH : constant := 0;
+ CLOCAL : constant := 8#04000#;
+ CREAD : constant := 8#0200#;
+ CSTOPB : constant := 8#0100#;
+ CRTSCTS : constant := 8#020000000000#;
+
+ -- c_cc indexes
+
+ VTIME : constant := 5;
+ VMIN : constant := 6;
+
+ C_Data_Rate : constant array (Data_Rate) of unsigned :=
+ (B1200 => 8#000011#,
+ B2400 => 8#000013#,
+ B4800 => 8#000014#,
+ B9600 => 8#000015#,
+ B19200 => 8#000016#,
+ B38400 => 8#000017#,
+ B57600 => 8#010001#);
+
+ C_Bits : constant array (Data_Bits) of unsigned :=
+ (B7 => 8#040#, B8 => 8#060#);
+
+ procedure Raise_Error (Message : String; Error : Integer := Errno);
+ pragma No_Return (Raise_Error);
+
+ ----------
+ -- Name --
+ ----------
+
+ function Name (Number : Positive) return Port_Name is
+ N : constant Natural := Number - 1;
+ N_Img : constant String := Natural'Image (N);
+ begin
+ return Port_Name ("/dev/ttyS" & N_Img (N_Img'First + 1 .. N_Img'Last));
+ end Name;
+
+ ----------
+ -- Open --
+ ----------
+
+ procedure Open
+ (Port : out Serial_Port;
+ Name : Port_Name)
+ is
+ C_Name : constant String := String (Name) & ASCII.NUL;
+ Res : int;
+
+ begin
+ if Port.H = null then
+ Port.H := new Port_Data;
+ end if;
+
+ Port.H.all := Port_Data (open
+ (C_Name (C_Name'First)'Address, int (O_RDWR + O_NOCTTY + O_NDELAY)));
+
+ if Port.H.all = -1 then
+ Raise_Error ("open: open failed");
+ end if;
+
+ -- By default we are in blocking mode
+
+ Res := fcntl (int (Port.H.all), F_SETFL, 0);
+
+ if Res = -1 then
+ Raise_Error ("open: fcntl failed");
+ end if;
+ end Open;
+
+ -----------------
+ -- Raise_Error --
+ -----------------
+
+ procedure Raise_Error (Message : String; Error : Integer := Errno) is
+ begin
+ raise Serial_Error with Message & " (" & Integer'Image (Error) & ')';
+ end Raise_Error;
+
+ ----------
+ -- Read --
+ ----------
+
+ overriding procedure Read
+ (Port : in out Serial_Port;
+ Buffer : out Stream_Element_Array;
+ Last : out Stream_Element_Offset)
+ is
+ Len : constant int := Buffer'Length;
+ Res : int;
+
+ begin
+ if Port.H = null then
+ Raise_Error ("read: port not opened", 0);
+ end if;
+
+ Res := read (Integer (Port.H.all), Buffer'Address, Len);
+
+ if Res = -1 then
+ Last := 0;
+ Raise_Error ("read failed");
+ else
+ Last := Buffer'First + Stream_Element_Offset (Res) - 1;
+ end if;
+ end Read;
+
+ ---------
+ -- Set --
+ ---------
+
+ procedure Set
+ (Port : Serial_Port;
+ Rate : Data_Rate := B9600;
+ Bits : Data_Bits := B8;
+ Block : Boolean := True;
+ Timeout : Integer := 10)
+ is
+ use type unsigned;
+
+ type termios is record
+ c_iflag : unsigned;
+ c_oflag : unsigned;
+ c_cflag : unsigned;
+ c_lflag : unsigned;
+ c_line : unsigned_char;
+ c_cc : Interfaces.C.char_array (0 .. 31);
+ c_ispeed : unsigned;
+ c_ospeed : unsigned;
+ end record;
+ pragma Convention (C, termios);
+
+ function tcgetattr (fd : int; termios_p : Address) return int;
+ pragma Import (C, tcgetattr, "tcgetattr");
+
+ function tcsetattr
+ (fd : int; action : int; termios_p : Address) return int;
+ pragma Import (C, tcsetattr, "tcsetattr");
+
+ function tcflush (fd : int; queue_selector : int) return int;
+ pragma Import (C, tcflush, "tcflush");
+
+ Current : termios;
+ Res : int;
+
+ begin
+ if Port.H = null then
+ Raise_Error ("set: port not opened", 0);
+ end if;
+
+ -- Get current port settings
+
+ Res := tcgetattr (int (Port.H.all), Current'Address);
+
+ -- Change settings now
+
+ Current.c_cflag := C_Data_Rate (Rate)
+ or C_Bits (Bits)
+ or CLOCAL
+ or CREAD
+ or CSTOPB
+ or CRTSCTS;
+ Current.c_lflag := 0;
+ Current.c_iflag := 0;
+ Current.c_oflag := 0;
+ Current.c_ispeed := Data_Rate_Value (Rate);
+ Current.c_ospeed := Data_Rate_Value (Rate);
+ Current.c_cc (VMIN) := char'Val (0);
+ Current.c_cc (VTIME) := char'Val (Timeout);
+
+ -- Set port settings
+
+ Res := tcflush (int (Port.H.all), TCIFLUSH);
+ Res := tcsetattr (int (Port.H.all), TCSANOW, Current'Address);
+
+ -- Block
+
+ if Block then
+ Res := fcntl (int (Port.H.all), F_SETFL, 0);
+ else
+ Res := fcntl (int (Port.H.all), F_SETFL, FNDELAY);
+ end if;
+
+ if Res = -1 then
+ Raise_Error ("set: fcntl failed");
+ end if;
+ end Set;
+
+ -----------
+ -- Write --
+ -----------
+
+ overriding procedure Write
+ (Port : in out Serial_Port;
+ Buffer : Stream_Element_Array)
+ is
+ Len : constant int := Buffer'Length;
+ Res : int;
+
+ begin
+ if Port.H = null then
+ Raise_Error ("write: port not opened", 0);
+ end if;
+
+ Res := write (int (Port.H.all), Buffer'Address, Len);
+ pragma Assert (Res = Len);
+
+ if Res = -1 then
+ Raise_Error ("write failed");
+ end if;
+ end Write;
+
+ -----------
+ -- Close --
+ -----------
+
+ procedure Close (Port : in out Serial_Port) is
+ procedure Unchecked_Free is
+ new Unchecked_Deallocation (Port_Data, Port_Data_Access);
+
+ Res : int;
+ pragma Unreferenced (Res);
+
+ begin
+ if Port.H /= null then
+ Res := close (int (Port.H.all));
+ Unchecked_Free (Port.H);
+ end if;
+ end Close;
+
+end GNAT.Serial_Communications;