OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [ada/] [g-sercom-linux.adb] - Blame information for rev 281

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 281 jeremybenn
------------------------------------------------------------------------------
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, size_t (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
 
215
      Res : int;
216
      pragma Warnings (Off, Res);
217
      --  Warnings off, since we don't always test the result
218
 
219
   begin
220
      if Port.H = null then
221
         Raise_Error ("set: port not opened", 0);
222
      end if;
223
 
224
      --  Get current port settings
225
 
226
      Res := tcgetattr (int (Port.H.all), Current'Address);
227
 
228
      --  Change settings now
229
 
230
      Current.c_cflag      := C_Data_Rate (Rate)
231
                                or C_Bits (Bits)
232
                                or C_Stop_Bits (Stop_Bits)
233
                                or C_Parity (Parity)
234
                                or CLOCAL
235
                                or CREAD
236
                                or CRTSCTS;
237
      Current.c_lflag      := 0;
238
      Current.c_iflag      := 0;
239
      Current.c_oflag      := 0;
240
      Current.c_ispeed     := Data_Rate_Value (Rate);
241
      Current.c_ospeed     := Data_Rate_Value (Rate);
242
      Current.c_cc (VMIN)  := char'Val (0);
243
      Current.c_cc (VTIME) := char'Val (Natural (Timeout * 10));
244
 
245
      --  Set port settings
246
 
247
      Res := tcflush (int (Port.H.all), TCIFLUSH);
248
      Res := tcsetattr (int (Port.H.all), TCSANOW, Current'Address);
249
 
250
      --  Block
251
 
252
      Res := fcntl (int (Port.H.all), F_SETFL, (if Block then 0 else FNDELAY));
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;

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.