OSDN Git Service

Daily bump.
[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-2008, 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.CRTL; use System, System.CRTL;
41
42 with GNAT.OS_Lib; use GNAT.OS_Lib;
43
44 package body GNAT.Serial_Communications is
45
46    use type Interfaces.C.unsigned;
47
48    type Port_Data is new int;
49
50    subtype unsigned is Interfaces.C.unsigned;
51    subtype char is Interfaces.C.char;
52    subtype unsigned_char is Interfaces.C.unsigned_char;
53
54    function fcntl (fd : int; cmd : int; value : int) return int;
55    pragma Import (C, fcntl, "fcntl");
56
57    O_RDWR   : constant := 8#02#;
58    O_NOCTTY : constant := 8#0400#;
59    O_NDELAY : constant := 8#04000#;
60    FNDELAY  : constant := O_NDELAY;
61    F_SETFL  : constant := 4;
62    TCSANOW  : constant := 0;
63    TCIFLUSH : constant := 0;
64    CLOCAL   : constant := 8#04000#;
65    CREAD    : constant := 8#0200#;
66    CSTOPB   : constant := 8#0100#;
67    CRTSCTS  : constant := 8#020000000000#;
68    PARENB   : constant := 8#00400#;
69    PARODD   : constant := 8#01000#;
70
71    --  c_cc indexes
72
73    VTIME : constant := 5;
74    VMIN  : constant := 6;
75
76    C_Data_Rate : constant array (Data_Rate) of unsigned :=
77                    (B1200   => 8#000011#,
78                     B2400   => 8#000013#,
79                     B4800   => 8#000014#,
80                     B9600   => 8#000015#,
81                     B19200  => 8#000016#,
82                     B38400  => 8#000017#,
83                     B57600  => 8#010001#,
84                     B115200 => 8#010002#);
85
86    C_Bits      : constant array (Data_Bits) of unsigned :=
87                    (CS7 => 8#040#, CS8 => 8#060#);
88
89    C_Stop_Bits : constant array (Stop_Bits_Number) of unsigned :=
90                    (One => 0, Two => CSTOPB);
91
92    C_Parity    : constant array (Parity_Check) of unsigned :=
93                    (None => 0, Odd => PARENB or PARODD, Even => PARENB);
94
95    procedure Raise_Error (Message : String; Error : Integer := Errno);
96    pragma No_Return (Raise_Error);
97
98    ----------
99    -- Name --
100    ----------
101
102    function Name (Number : Positive) return Port_Name is
103       N     : constant Natural := Number - 1;
104       N_Img : constant String  := Natural'Image (N);
105    begin
106       return Port_Name ("/dev/ttyS" & N_Img (N_Img'First + 1 .. N_Img'Last));
107    end Name;
108
109    ----------
110    -- Open --
111    ----------
112
113    procedure Open
114      (Port : out Serial_Port;
115       Name : Port_Name)
116    is
117       C_Name : constant String := String (Name) & ASCII.NUL;
118       Res    : int;
119
120    begin
121       if Port.H = null then
122          Port.H := new Port_Data;
123       end if;
124
125       Port.H.all := Port_Data (open
126          (C_Name (C_Name'First)'Address, int (O_RDWR + O_NOCTTY + O_NDELAY)));
127
128       if Port.H.all = -1 then
129          Raise_Error ("open: open failed");
130       end if;
131
132       --  By default we are in blocking mode
133
134       Res := fcntl (int (Port.H.all), F_SETFL, 0);
135
136       if Res = -1 then
137          Raise_Error ("open: fcntl failed");
138       end if;
139    end Open;
140
141    -----------------
142    -- Raise_Error --
143    -----------------
144
145    procedure Raise_Error (Message : String; Error : Integer := Errno) is
146    begin
147       raise Serial_Error with Message & " (" & Integer'Image (Error) & ')';
148    end Raise_Error;
149
150    ----------
151    -- Read --
152    ----------
153
154    overriding procedure Read
155      (Port   : in out Serial_Port;
156       Buffer : out Stream_Element_Array;
157       Last   : out Stream_Element_Offset)
158    is
159       Len : constant int := Buffer'Length;
160       Res : int;
161
162    begin
163       if Port.H = null then
164          Raise_Error ("read: port not opened", 0);
165       end if;
166
167       Res := read (Integer (Port.H.all), Buffer'Address, Len);
168
169       if Res = -1 then
170          Last := 0;
171          Raise_Error ("read failed");
172       else
173          Last := Buffer'First + Stream_Element_Offset (Res) - 1;
174       end if;
175    end Read;
176
177    ---------
178    -- Set --
179    ---------
180
181    procedure Set
182      (Port      : Serial_Port;
183       Rate      : Data_Rate        := B9600;
184       Bits      : Data_Bits        := CS8;
185       Stop_Bits : Stop_Bits_Number := One;
186       Parity    : Parity_Check     := None;
187       Block     : Boolean          := True;
188       Timeout   : Duration         := 10.0)
189    is
190       type termios is record
191          c_iflag  : unsigned;
192          c_oflag  : unsigned;
193          c_cflag  : unsigned;
194          c_lflag  : unsigned;
195          c_line   : unsigned_char;
196          c_cc     : Interfaces.C.char_array (0 .. 31);
197          c_ispeed : unsigned;
198          c_ospeed : unsigned;
199       end record;
200       pragma Convention (C, termios);
201
202       function tcgetattr (fd : int; termios_p : Address) return int;
203       pragma Import (C, tcgetattr, "tcgetattr");
204
205       function tcsetattr
206         (fd : int; action : int; termios_p : Address) return int;
207       pragma Import (C, tcsetattr, "tcsetattr");
208
209       function tcflush (fd : int; queue_selector : int) return int;
210       pragma Import (C, tcflush, "tcflush");
211
212       Current : termios;
213       Res     : int;
214
215    begin
216       if Port.H = null then
217          Raise_Error ("set: port not opened", 0);
218       end if;
219
220       --  Get current port settings
221
222       Res := tcgetattr (int (Port.H.all), Current'Address);
223
224       --  Change settings now
225
226       Current.c_cflag      := C_Data_Rate (Rate)
227                                 or C_Bits (Bits)
228                                 or C_Stop_Bits (Stop_Bits)
229                                 or C_Parity (Parity)
230                                 or CLOCAL
231                                 or CREAD
232                                 or CRTSCTS;
233       Current.c_lflag      := 0;
234       Current.c_iflag      := 0;
235       Current.c_oflag      := 0;
236       Current.c_ispeed     := Data_Rate_Value (Rate);
237       Current.c_ospeed     := Data_Rate_Value (Rate);
238       Current.c_cc (VMIN)  := char'Val (0);
239       Current.c_cc (VTIME) := char'Val (Natural (Timeout * 10));
240
241       --  Set port settings
242
243       Res := tcflush (int (Port.H.all), TCIFLUSH);
244       Res := tcsetattr (int (Port.H.all), TCSANOW, Current'Address);
245
246       --  Block
247
248       if Block then
249          Res := fcntl (int (Port.H.all), F_SETFL, 0);
250       else
251          Res := fcntl (int (Port.H.all), F_SETFL, FNDELAY);
252       end if;
253
254       if Res = -1 then
255          Raise_Error ("set: fcntl failed");
256       end if;
257    end Set;
258
259    -----------
260    -- Write --
261    -----------
262
263    overriding procedure Write
264      (Port   : in out Serial_Port;
265       Buffer : Stream_Element_Array)
266    is
267       Len : constant int := Buffer'Length;
268       Res : int;
269
270    begin
271       if Port.H = null then
272          Raise_Error ("write: port not opened", 0);
273       end if;
274
275       Res := write (int (Port.H.all), Buffer'Address, Len);
276       pragma Assert (Res = Len);
277
278       if Res = -1 then
279          Raise_Error ("write failed");
280       end if;
281    end Write;
282
283    -----------
284    -- Close --
285    -----------
286
287    procedure Close (Port : in out Serial_Port) is
288       procedure Unchecked_Free is
289         new Unchecked_Deallocation (Port_Data, Port_Data_Access);
290
291       Res : int;
292       pragma Unreferenced (Res);
293
294    begin
295       if Port.H /= null then
296          Res := close (int (Port.H.all));
297          Unchecked_Free (Port.H);
298       end if;
299    end Close;
300
301 end GNAT.Serial_Communications;