annotate gcc/ada/libgnat/g-sercom__mingw.adb @ 111:04ced10e8804

gcc 7
author kono
date Fri, 27 Oct 2017 22:46:09 +0900
parents
children 84e7813d76e9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 ------------------------------------------------------------------------------
kono
parents:
diff changeset
2 -- --
kono
parents:
diff changeset
3 -- GNAT COMPILER COMPONENTS --
kono
parents:
diff changeset
4 -- --
kono
parents:
diff changeset
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 --
kono
parents:
diff changeset
6 -- --
kono
parents:
diff changeset
7 -- B o d y --
kono
parents:
diff changeset
8 -- --
kono
parents:
diff changeset
9 -- Copyright (C) 2007-2017, AdaCore --
kono
parents:
diff changeset
10 -- --
kono
parents:
diff changeset
11 -- GNAT is free software; you can redistribute it and/or modify it under --
kono
parents:
diff changeset
12 -- terms of the GNU General Public License as published by the Free Soft- --
kono
parents:
diff changeset
13 -- ware Foundation; either version 3, or (at your option) any later ver- --
kono
parents:
diff changeset
14 -- sion. GNAT is distributed in the hope that it will be useful, but WITH- --
kono
parents:
diff changeset
15 -- OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY --
kono
parents:
diff changeset
16 -- or FITNESS FOR A PARTICULAR PURPOSE. --
kono
parents:
diff changeset
17 -- --
kono
parents:
diff changeset
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
kono
parents:
diff changeset
19 -- additional permissions described in the GCC Runtime Library Exception, --
kono
parents:
diff changeset
20 -- version 3.1, as published by the Free Software Foundation. --
kono
parents:
diff changeset
21 -- --
kono
parents:
diff changeset
22 -- You should have received a copy of the GNU General Public License and --
kono
parents:
diff changeset
23 -- a copy of the GCC Runtime Library Exception along with this program; --
kono
parents:
diff changeset
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
kono
parents:
diff changeset
25 -- <http://www.gnu.org/licenses/>. --
kono
parents:
diff changeset
26 -- --
kono
parents:
diff changeset
27 -- GNAT was originally developed by the GNAT team at New York University. --
kono
parents:
diff changeset
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
kono
parents:
diff changeset
29 -- --
kono
parents:
diff changeset
30 ------------------------------------------------------------------------------
kono
parents:
diff changeset
31
kono
parents:
diff changeset
32 -- This is the Windows implementation of this package
kono
parents:
diff changeset
33
kono
parents:
diff changeset
34 with Ada.Streams; use Ada.Streams;
kono
parents:
diff changeset
35 with Ada.Unchecked_Deallocation; use Ada;
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 with System; use System;
kono
parents:
diff changeset
38 with System.Communication; use System.Communication;
kono
parents:
diff changeset
39 with System.CRTL; use System.CRTL;
kono
parents:
diff changeset
40 with System.OS_Constants;
kono
parents:
diff changeset
41 with System.Win32; use System.Win32;
kono
parents:
diff changeset
42 with System.Win32.Ext; use System.Win32.Ext;
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 with GNAT.OS_Lib;
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 package body GNAT.Serial_Communications is
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 package OSC renames System.OS_Constants;
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 -- Common types
kono
parents:
diff changeset
51
kono
parents:
diff changeset
52 type Port_Data is new HANDLE;
kono
parents:
diff changeset
53
kono
parents:
diff changeset
54 C_Bits : constant array (Data_Bits) of Interfaces.C.unsigned := (8, 7);
kono
parents:
diff changeset
55 C_Parity : constant array (Parity_Check) of Interfaces.C.unsigned :=
kono
parents:
diff changeset
56 (None => NOPARITY, Odd => ODDPARITY, Even => EVENPARITY);
kono
parents:
diff changeset
57 C_Stop_Bits : constant array (Stop_Bits_Number) of Interfaces.C.unsigned :=
kono
parents:
diff changeset
58 (One => ONESTOPBIT, Two => TWOSTOPBITS);
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 -----------
kono
parents:
diff changeset
61 -- Files --
kono
parents:
diff changeset
62 -----------
kono
parents:
diff changeset
63
kono
parents:
diff changeset
64 procedure Raise_Error (Message : String; Error : DWORD := GetLastError);
kono
parents:
diff changeset
65 pragma No_Return (Raise_Error);
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 -----------
kono
parents:
diff changeset
68 -- Close --
kono
parents:
diff changeset
69 -----------
kono
parents:
diff changeset
70
kono
parents:
diff changeset
71 procedure Close (Port : in out Serial_Port) is
kono
parents:
diff changeset
72 procedure Unchecked_Free is
kono
parents:
diff changeset
73 new Unchecked_Deallocation (Port_Data, Port_Data_Access);
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75 Success : BOOL;
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 begin
kono
parents:
diff changeset
78 if Port.H /= null then
kono
parents:
diff changeset
79 Success := CloseHandle (HANDLE (Port.H.all));
kono
parents:
diff changeset
80 Unchecked_Free (Port.H);
kono
parents:
diff changeset
81
kono
parents:
diff changeset
82 if Success = Win32.FALSE then
kono
parents:
diff changeset
83 Raise_Error ("error closing the port");
kono
parents:
diff changeset
84 end if;
kono
parents:
diff changeset
85 end if;
kono
parents:
diff changeset
86 end Close;
kono
parents:
diff changeset
87
kono
parents:
diff changeset
88 ----------
kono
parents:
diff changeset
89 -- Name --
kono
parents:
diff changeset
90 ----------
kono
parents:
diff changeset
91
kono
parents:
diff changeset
92 function Name (Number : Positive) return Port_Name is
kono
parents:
diff changeset
93 N_Img : constant String := Positive'Image (Number);
kono
parents:
diff changeset
94 begin
kono
parents:
diff changeset
95 if Number > 9 then
kono
parents:
diff changeset
96 return
kono
parents:
diff changeset
97 Port_Name ("\\.\COM" & N_Img (N_Img'First + 1 .. N_Img'Last));
kono
parents:
diff changeset
98 else
kono
parents:
diff changeset
99 return
kono
parents:
diff changeset
100 Port_Name ("COM" & N_Img (N_Img'First + 1 .. N_Img'Last) & ':');
kono
parents:
diff changeset
101 end if;
kono
parents:
diff changeset
102 end Name;
kono
parents:
diff changeset
103
kono
parents:
diff changeset
104 ----------
kono
parents:
diff changeset
105 -- Open --
kono
parents:
diff changeset
106 ----------
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 procedure Open
kono
parents:
diff changeset
109 (Port : out Serial_Port;
kono
parents:
diff changeset
110 Name : Port_Name)
kono
parents:
diff changeset
111 is
kono
parents:
diff changeset
112 C_Name : constant String := String (Name) & ASCII.NUL;
kono
parents:
diff changeset
113 Success : BOOL;
kono
parents:
diff changeset
114 pragma Unreferenced (Success);
kono
parents:
diff changeset
115
kono
parents:
diff changeset
116 begin
kono
parents:
diff changeset
117 if Port.H = null then
kono
parents:
diff changeset
118 Port.H := new Port_Data;
kono
parents:
diff changeset
119 else
kono
parents:
diff changeset
120 Success := CloseHandle (HANDLE (Port.H.all));
kono
parents:
diff changeset
121 end if;
kono
parents:
diff changeset
122
kono
parents:
diff changeset
123 Port.H.all := CreateFileA
kono
parents:
diff changeset
124 (lpFileName => C_Name (C_Name'First)'Address,
kono
parents:
diff changeset
125 dwDesiredAccess => GENERIC_READ or GENERIC_WRITE,
kono
parents:
diff changeset
126 dwShareMode => 0,
kono
parents:
diff changeset
127 lpSecurityAttributes => null,
kono
parents:
diff changeset
128 dwCreationDisposition => OPEN_EXISTING,
kono
parents:
diff changeset
129 dwFlagsAndAttributes => 0,
kono
parents:
diff changeset
130 hTemplateFile => 0);
kono
parents:
diff changeset
131
kono
parents:
diff changeset
132 if Port.H.all = Port_Data (INVALID_HANDLE_VALUE) then
kono
parents:
diff changeset
133 Raise_Error ("cannot open com port");
kono
parents:
diff changeset
134 end if;
kono
parents:
diff changeset
135 end Open;
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 -----------------
kono
parents:
diff changeset
138 -- Raise_Error --
kono
parents:
diff changeset
139 -----------------
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 procedure Raise_Error (Message : String; Error : DWORD := GetLastError) is
kono
parents:
diff changeset
142 begin
kono
parents:
diff changeset
143 raise Serial_Error with Message
kono
parents:
diff changeset
144 & (if Error /= 0
kono
parents:
diff changeset
145 then " (" & GNAT.OS_Lib.Errno_Message (Err => Integer (Error)) & ')'
kono
parents:
diff changeset
146 else "");
kono
parents:
diff changeset
147 end Raise_Error;
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 ----------
kono
parents:
diff changeset
150 -- Read --
kono
parents:
diff changeset
151 ----------
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 overriding procedure Read
kono
parents:
diff changeset
154 (Port : in out Serial_Port;
kono
parents:
diff changeset
155 Buffer : out Stream_Element_Array;
kono
parents:
diff changeset
156 Last : out Stream_Element_Offset)
kono
parents:
diff changeset
157 is
kono
parents:
diff changeset
158 Success : BOOL;
kono
parents:
diff changeset
159 Read_Last : aliased DWORD;
kono
parents:
diff changeset
160
kono
parents:
diff changeset
161 begin
kono
parents:
diff changeset
162 if Port.H = null then
kono
parents:
diff changeset
163 Raise_Error ("read: port not opened", 0);
kono
parents:
diff changeset
164 end if;
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 Success :=
kono
parents:
diff changeset
167 ReadFile
kono
parents:
diff changeset
168 (hFile => HANDLE (Port.H.all),
kono
parents:
diff changeset
169 lpBuffer => Buffer (Buffer'First)'Address,
kono
parents:
diff changeset
170 nNumberOfBytesToRead => DWORD (Buffer'Length),
kono
parents:
diff changeset
171 lpNumberOfBytesRead => Read_Last'Access,
kono
parents:
diff changeset
172 lpOverlapped => null);
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 if Success = Win32.FALSE then
kono
parents:
diff changeset
175 Raise_Error ("read error");
kono
parents:
diff changeset
176 end if;
kono
parents:
diff changeset
177
kono
parents:
diff changeset
178 Last := Last_Index (Buffer'First, size_t (Read_Last));
kono
parents:
diff changeset
179 end Read;
kono
parents:
diff changeset
180
kono
parents:
diff changeset
181 ---------
kono
parents:
diff changeset
182 -- Set --
kono
parents:
diff changeset
183 ---------
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 procedure Set
kono
parents:
diff changeset
186 (Port : Serial_Port;
kono
parents:
diff changeset
187 Rate : Data_Rate := B9600;
kono
parents:
diff changeset
188 Bits : Data_Bits := CS8;
kono
parents:
diff changeset
189 Stop_Bits : Stop_Bits_Number := One;
kono
parents:
diff changeset
190 Parity : Parity_Check := None;
kono
parents:
diff changeset
191 Block : Boolean := True;
kono
parents:
diff changeset
192 Local : Boolean := True;
kono
parents:
diff changeset
193 Flow : Flow_Control := None;
kono
parents:
diff changeset
194 Timeout : Duration := 10.0)
kono
parents:
diff changeset
195 is
kono
parents:
diff changeset
196 pragma Unreferenced (Local);
kono
parents:
diff changeset
197
kono
parents:
diff changeset
198 Success : BOOL;
kono
parents:
diff changeset
199 Com_Time_Out : aliased COMMTIMEOUTS;
kono
parents:
diff changeset
200 Com_Settings : aliased DCB;
kono
parents:
diff changeset
201
kono
parents:
diff changeset
202 begin
kono
parents:
diff changeset
203 if Port.H = null then
kono
parents:
diff changeset
204 Raise_Error ("set: port not opened", 0);
kono
parents:
diff changeset
205 end if;
kono
parents:
diff changeset
206
kono
parents:
diff changeset
207 Success := GetCommState (HANDLE (Port.H.all), Com_Settings'Access);
kono
parents:
diff changeset
208
kono
parents:
diff changeset
209 if Success = Win32.FALSE then
kono
parents:
diff changeset
210 Success := CloseHandle (HANDLE (Port.H.all));
kono
parents:
diff changeset
211 Port.H.all := 0;
kono
parents:
diff changeset
212 Raise_Error ("set: cannot get comm state");
kono
parents:
diff changeset
213 end if;
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 Com_Settings.BaudRate := DWORD (Data_Rate_Value (Rate));
kono
parents:
diff changeset
216 Com_Settings.fParity := 1;
kono
parents:
diff changeset
217 Com_Settings.fBinary := Bits1 (System.Win32.TRUE);
kono
parents:
diff changeset
218 Com_Settings.fOutxDsrFlow := 0;
kono
parents:
diff changeset
219 Com_Settings.fDsrSensitivity := 0;
kono
parents:
diff changeset
220 Com_Settings.fDtrControl := OSC.DTR_CONTROL_ENABLE;
kono
parents:
diff changeset
221 Com_Settings.fInX := 0;
kono
parents:
diff changeset
222 Com_Settings.fRtsControl := OSC.RTS_CONTROL_ENABLE;
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 case Flow is
kono
parents:
diff changeset
225 when None =>
kono
parents:
diff changeset
226 Com_Settings.fOutX := 0;
kono
parents:
diff changeset
227 Com_Settings.fOutxCtsFlow := 0;
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 when RTS_CTS =>
kono
parents:
diff changeset
230 Com_Settings.fOutX := 0;
kono
parents:
diff changeset
231 Com_Settings.fOutxCtsFlow := 1;
kono
parents:
diff changeset
232
kono
parents:
diff changeset
233 when Xon_Xoff =>
kono
parents:
diff changeset
234 Com_Settings.fOutX := 1;
kono
parents:
diff changeset
235 Com_Settings.fOutxCtsFlow := 0;
kono
parents:
diff changeset
236 end case;
kono
parents:
diff changeset
237
kono
parents:
diff changeset
238 Com_Settings.fAbortOnError := 0;
kono
parents:
diff changeset
239 Com_Settings.ByteSize := BYTE (C_Bits (Bits));
kono
parents:
diff changeset
240 Com_Settings.Parity := BYTE (C_Parity (Parity));
kono
parents:
diff changeset
241 Com_Settings.StopBits := BYTE (C_Stop_Bits (Stop_Bits));
kono
parents:
diff changeset
242
kono
parents:
diff changeset
243 Success := SetCommState (HANDLE (Port.H.all), Com_Settings'Access);
kono
parents:
diff changeset
244
kono
parents:
diff changeset
245 if Success = Win32.FALSE then
kono
parents:
diff changeset
246 Success := CloseHandle (HANDLE (Port.H.all));
kono
parents:
diff changeset
247 Port.H.all := 0;
kono
parents:
diff changeset
248 Raise_Error ("cannot set comm state");
kono
parents:
diff changeset
249 end if;
kono
parents:
diff changeset
250
kono
parents:
diff changeset
251 -- Set the timeout status, to honor our spec with respect to read
kono
parents:
diff changeset
252 -- timeouts. Always disconnect write timeouts.
kono
parents:
diff changeset
253
kono
parents:
diff changeset
254 -- Blocking reads - no timeout at all
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 if Block then
kono
parents:
diff changeset
257 Com_Time_Out := (others => 0);
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 -- Non-blocking reads and null timeout - immediate return with what we
kono
parents:
diff changeset
260 -- have - set ReadIntervalTimeout to MAXDWORD.
kono
parents:
diff changeset
261
kono
parents:
diff changeset
262 elsif Timeout = 0.0 then
kono
parents:
diff changeset
263 Com_Time_Out :=
kono
parents:
diff changeset
264 (ReadIntervalTimeout => DWORD'Last,
kono
parents:
diff changeset
265 others => 0);
kono
parents:
diff changeset
266
kono
parents:
diff changeset
267 -- Non-blocking reads with timeout - set total read timeout accordingly
kono
parents:
diff changeset
268
kono
parents:
diff changeset
269 else
kono
parents:
diff changeset
270 Com_Time_Out :=
kono
parents:
diff changeset
271 (ReadTotalTimeoutConstant => DWORD (1000 * Timeout),
kono
parents:
diff changeset
272 others => 0);
kono
parents:
diff changeset
273 end if;
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 Success :=
kono
parents:
diff changeset
276 SetCommTimeouts
kono
parents:
diff changeset
277 (hFile => HANDLE (Port.H.all),
kono
parents:
diff changeset
278 lpCommTimeouts => Com_Time_Out'Access);
kono
parents:
diff changeset
279
kono
parents:
diff changeset
280 if Success = Win32.FALSE then
kono
parents:
diff changeset
281 Raise_Error ("cannot set the timeout");
kono
parents:
diff changeset
282 end if;
kono
parents:
diff changeset
283 end Set;
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 -----------
kono
parents:
diff changeset
286 -- Write --
kono
parents:
diff changeset
287 -----------
kono
parents:
diff changeset
288
kono
parents:
diff changeset
289 overriding procedure Write
kono
parents:
diff changeset
290 (Port : in out Serial_Port;
kono
parents:
diff changeset
291 Buffer : Stream_Element_Array)
kono
parents:
diff changeset
292 is
kono
parents:
diff changeset
293 Success : BOOL;
kono
parents:
diff changeset
294 Temp_Last : aliased DWORD;
kono
parents:
diff changeset
295
kono
parents:
diff changeset
296 begin
kono
parents:
diff changeset
297 if Port.H = null then
kono
parents:
diff changeset
298 Raise_Error ("write: port not opened", 0);
kono
parents:
diff changeset
299 end if;
kono
parents:
diff changeset
300
kono
parents:
diff changeset
301 Success :=
kono
parents:
diff changeset
302 WriteFile
kono
parents:
diff changeset
303 (hFile => HANDLE (Port.H.all),
kono
parents:
diff changeset
304 lpBuffer => Buffer'Address,
kono
parents:
diff changeset
305 nNumberOfBytesToWrite => DWORD (Buffer'Length),
kono
parents:
diff changeset
306 lpNumberOfBytesWritten => Temp_Last'Access,
kono
parents:
diff changeset
307 lpOverlapped => null);
kono
parents:
diff changeset
308
kono
parents:
diff changeset
309 if Success = Win32.FALSE
kono
parents:
diff changeset
310 or else Stream_Element_Offset (Temp_Last) /= Buffer'Length
kono
parents:
diff changeset
311 then
kono
parents:
diff changeset
312 Raise_Error ("failed to write data");
kono
parents:
diff changeset
313 end if;
kono
parents:
diff changeset
314 end Write;
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 end GNAT.Serial_Communications;