OSDN Git Service

Minor comment updates.
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-sercom-linux.adb
1 ------------------------------------------------------------------------------
2 --                                                                          --
3 --                         GNAT COMPILER COMPONENTS                         --
4 --                                                                          --
5 --           G N A T . S E R I A L _ C O M M U N I C A T I O N S            --
6 --                                                                          --
7 --                                 B o d y                                  --
8 --                                                                          --
9 --                    Copyright (C) 2007-2009, AdaCore                      --
10 --                                                                          --
11 -- GNAT is free software;  you can  redistribute it  and/or modify it under --
12 -- terms of the  GNU General Public License as published  by the Free Soft- --
13 -- ware  Foundation;  either version 2,  or (at your option) any later ver- --
14 -- sion.  GNAT is distributed in the hope that it will be useful, but WITH- --
15 -- OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY --
16 -- or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License --
17 -- for  more details.  You should have  received  a copy of the GNU General --
18 -- Public License  distributed with GNAT;  see file COPYING.  If not, write --
19 -- to  the  Free Software Foundation,  51  Franklin  Street,  Fifth  Floor, --
20 -- Boston, MA 02110-1301, USA.                                              --
21 --                                                                          --
22 -- As a special exception,  if other files  instantiate  generics from this --
23 -- unit, or you link  this unit with other files  to produce an executable, --
24 -- this  unit  does not  by itself cause  the resulting  executable  to  be --
25 -- covered  by the  GNU  General  Public  License.  This exception does not --
26 -- however invalidate  any other reasons why  the executable file  might be --
27 -- covered by the  GNU Public License.                                      --
28 --                                                                          --
29 -- GNAT was originally developed  by the GNAT team at  New York University. --
30 -- Extensive contributions were provided by Ada Core Technologies Inc.      --
31 --                                                                          --
32 ------------------------------------------------------------------------------
33
34 --  This is the GNU/Linux implementation of this package
35
36 with Ada.Streams;                use Ada.Streams;
37 with Ada;                        use Ada;
38 with Ada.Unchecked_Deallocation;
39
40 with System;               use System;
41 with System.Communication; use System.Communication;
42 with System.CRTL;          use System.CRTL;
43
44 with GNAT.OS_Lib; use GNAT.OS_Lib;
45
46 package body GNAT.Serial_Communications is
47
48    use type Interfaces.C.unsigned;
49
50    type Port_Data is new int;
51
52    subtype unsigned is Interfaces.C.unsigned;
53    subtype char is Interfaces.C.char;
54    subtype unsigned_char is Interfaces.C.unsigned_char;
55
56    function fcntl (fd : int; cmd : int; value : int) return int;
57    pragma Import (C, fcntl, "fcntl");
58
59    O_RDWR   : constant := 8#02#;
60    O_NOCTTY : constant := 8#0400#;
61    O_NDELAY : constant := 8#04000#;
62    FNDELAY  : constant := O_NDELAY;
63    F_SETFL  : constant := 4;
64    TCSANOW  : constant := 0;
65    TCIFLUSH : constant := 0;
66    CLOCAL   : constant := 8#04000#;
67    CREAD    : constant := 8#0200#;
68    CSTOPB   : constant := 8#0100#;
69    CRTSCTS  : constant := 8#020000000000#;
70    PARENB   : constant := 8#00400#;
71    PARODD   : constant := 8#01000#;
72
73    --  c_cc indexes
74
75    VTIME : constant := 5;
76    VMIN  : constant := 6;
77
78    C_Data_Rate : constant array (Data_Rate) of unsigned :=
79                    (B1200   => 8#000011#,
80                     B2400   => 8#000013#,
81                     B4800   => 8#000014#,
82                     B9600   => 8#000015#,
83                     B19200  => 8#000016#,
84                     B38400  => 8#000017#,
85                     B57600  => 8#010001#,
86                     B115200 => 8#010002#);
87
88    C_Bits      : constant array (Data_Bits) of unsigned :=
89                    (CS7 => 8#040#, CS8 => 8#060#);
90
91    C_Stop_Bits : constant array (Stop_Bits_Number) of unsigned :=
92                    (One => 0, Two => CSTOPB);
93
94    C_Parity    : constant array (Parity_Check) of unsigned :=
95                    (None => 0, Odd => PARENB or PARODD, Even => PARENB);
96
97    procedure Raise_Error (Message : String; Error : Integer := Errno);
98    pragma No_Return (Raise_Error);
99
100    ----------
101    -- Name --
102    ----------
103
104    function Name (Number : Positive) return Port_Name is
105       N     : constant Natural := Number - 1;
106       N_Img : constant String  := Natural'Image (N);
107    begin
108       return Port_Name ("/dev/ttyS" & N_Img (N_Img'First + 1 .. N_Img'Last));
109    end Name;
110
111    ----------
112    -- Open --
113    ----------
114
115    procedure Open
116      (Port : out Serial_Port;
117       Name : Port_Name)
118    is
119       C_Name : constant String := String (Name) & ASCII.NUL;
120       Res    : int;
121
122    begin
123       if Port.H = null then
124          Port.H := new Port_Data;
125       end if;
126
127       Port.H.all := Port_Data (open
128          (C_Name (C_Name'First)'Address, int (O_RDWR + O_NOCTTY + O_NDELAY)));
129
130       if Port.H.all = -1 then
131          Raise_Error ("open: open failed");
132       end if;
133
134       --  By default we are in blocking mode
135
136       Res := fcntl (int (Port.H.all), F_SETFL, 0);
137
138       if Res = -1 then
139          Raise_Error ("open: fcntl failed");
140       end if;
141    end Open;
142
143    -----------------
144    -- Raise_Error --
145    -----------------
146
147    procedure Raise_Error (Message : String; Error : Integer := Errno) is
148    begin
149       raise Serial_Error with Message & " (" & Integer'Image (Error) & ')';
150    end Raise_Error;
151
152    ----------
153    -- Read --
154    ----------
155
156    overriding procedure Read
157      (Port   : in out Serial_Port;
158       Buffer : out Stream_Element_Array;
159       Last   : out Stream_Element_Offset)
160    is
161       Len : constant int := Buffer'Length;
162       Res : int;
163
164    begin
165       if Port.H = null then
166          Raise_Error ("read: port not opened", 0);
167       end if;
168
169       Res := read (Integer (Port.H.all), Buffer'Address, Len);
170
171       if Res = -1 then
172          Raise_Error ("read failed");
173       end if;
174
175       Last := Last_Index (Buffer'First, C.int (Res));
176    end Read;
177
178    ---------
179    -- Set --
180    ---------
181
182    procedure Set
183      (Port      : Serial_Port;
184       Rate      : Data_Rate        := B9600;
185       Bits      : Data_Bits        := CS8;
186       Stop_Bits : Stop_Bits_Number := One;
187       Parity    : Parity_Check     := None;
188       Block     : Boolean          := True;
189       Timeout   : Duration         := 10.0)
190    is
191       type termios is record
192          c_iflag  : unsigned;
193          c_oflag  : unsigned;
194          c_cflag  : unsigned;
195          c_lflag  : unsigned;
196          c_line   : unsigned_char;
197          c_cc     : Interfaces.C.char_array (0 .. 31);
198          c_ispeed : unsigned;
199          c_ospeed : unsigned;
200       end record;
201       pragma Convention (C, termios);
202
203       function tcgetattr (fd : int; termios_p : Address) return int;
204       pragma Import (C, tcgetattr, "tcgetattr");
205
206       function tcsetattr
207         (fd : int; action : int; termios_p : Address) return int;
208       pragma Import (C, tcsetattr, "tcsetattr");
209
210       function tcflush (fd : int; queue_selector : int) return int;
211       pragma Import (C, tcflush, "tcflush");
212
213       Current : termios;
214       Res     : int;
215
216    begin
217       if Port.H = null then
218          Raise_Error ("set: port not opened", 0);
219       end if;
220
221       --  Get current port settings
222
223       Res := tcgetattr (int (Port.H.all), Current'Address);
224
225       --  Change settings now
226
227       Current.c_cflag      := C_Data_Rate (Rate)
228                                 or C_Bits (Bits)
229                                 or C_Stop_Bits (Stop_Bits)
230                                 or C_Parity (Parity)
231                                 or CLOCAL
232                                 or CREAD
233                                 or CRTSCTS;
234       Current.c_lflag      := 0;
235       Current.c_iflag      := 0;
236       Current.c_oflag      := 0;
237       Current.c_ispeed     := Data_Rate_Value (Rate);
238       Current.c_ospeed     := Data_Rate_Value (Rate);
239       Current.c_cc (VMIN)  := char'Val (0);
240       Current.c_cc (VTIME) := char'Val (Natural (Timeout * 10));
241
242       --  Set port settings
243
244       Res := tcflush (int (Port.H.all), TCIFLUSH);
245       Res := tcsetattr (int (Port.H.all), TCSANOW, Current'Address);
246
247       --  Block
248
249       if Block then
250          Res := fcntl (int (Port.H.all), F_SETFL, 0);
251       else
252          Res := fcntl (int (Port.H.all), F_SETFL, FNDELAY);
253       end if;
254
255       if Res = -1 then
256          Raise_Error ("set: fcntl failed");
257       end if;
258    end Set;
259
260    -----------
261    -- Write --
262    -----------
263
264    overriding procedure Write
265      (Port   : in out Serial_Port;
266       Buffer : Stream_Element_Array)
267    is
268       Len : constant int := Buffer'Length;
269       Res : int;
270
271    begin
272       if Port.H = null then
273          Raise_Error ("write: port not opened", 0);
274       end if;
275
276       Res := write (int (Port.H.all), Buffer'Address, Len);
277       pragma Assert (Res = Len);
278
279       if Res = -1 then
280          Raise_Error ("write failed");
281       end if;
282    end Write;
283
284    -----------
285    -- Close --
286    -----------
287
288    procedure Close (Port : in out Serial_Port) is
289       procedure Unchecked_Free is
290         new Unchecked_Deallocation (Port_Data, Port_Data_Access);
291
292       Res : int;
293       pragma Unreferenced (Res);
294
295    begin
296       if Port.H /= null then
297          Res := close (int (Port.H.all));
298          Unchecked_Free (Port.H);
299       end if;
300    end Close;
301
302 end GNAT.Serial_Communications;