annotate gcc/ada/libgnat/g-sercom__mingw.adb @ 145:1830386684a0

gcc-9.2.0
author anatofuz
date Thu, 13 Feb 2020 11:34:05 +0900
parents 84e7813d76e9
children
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 -- --
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
9 -- Copyright (C) 2007-2019, AdaCore --
111
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
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
34 with Ada.Streams; use Ada.Streams, Ada;
111
kono
parents:
diff changeset
35
kono
parents:
diff changeset
36 with System; use System;
kono
parents:
diff changeset
37 with System.Communication; use System.Communication;
kono
parents:
diff changeset
38 with System.CRTL; use System.CRTL;
kono
parents:
diff changeset
39 with System.Win32; use System.Win32;
kono
parents:
diff changeset
40 with System.Win32.Ext; use System.Win32.Ext;
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 with GNAT.OS_Lib;
kono
parents:
diff changeset
43
kono
parents:
diff changeset
44 package body GNAT.Serial_Communications is
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 package OSC renames System.OS_Constants;
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 -- Common types
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 C_Bits : constant array (Data_Bits) of Interfaces.C.unsigned := (8, 7);
kono
parents:
diff changeset
51 C_Parity : constant array (Parity_Check) of Interfaces.C.unsigned :=
kono
parents:
diff changeset
52 (None => NOPARITY, Odd => ODDPARITY, Even => EVENPARITY);
kono
parents:
diff changeset
53 C_Stop_Bits : constant array (Stop_Bits_Number) of Interfaces.C.unsigned :=
kono
parents:
diff changeset
54 (One => ONESTOPBIT, Two => TWOSTOPBITS);
kono
parents:
diff changeset
55
kono
parents:
diff changeset
56 -----------
kono
parents:
diff changeset
57 -- Files --
kono
parents:
diff changeset
58 -----------
kono
parents:
diff changeset
59
kono
parents:
diff changeset
60 procedure Raise_Error (Message : String; Error : DWORD := GetLastError);
kono
parents:
diff changeset
61 pragma No_Return (Raise_Error);
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 -----------
kono
parents:
diff changeset
64 -- Close --
kono
parents:
diff changeset
65 -----------
kono
parents:
diff changeset
66
kono
parents:
diff changeset
67 procedure Close (Port : in out Serial_Port) is
kono
parents:
diff changeset
68 Success : BOOL;
kono
parents:
diff changeset
69
kono
parents:
diff changeset
70 begin
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
71 if Port.H /= -1 then
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
72 Success := CloseHandle (HANDLE (Port.H));
111
kono
parents:
diff changeset
73
kono
parents:
diff changeset
74 if Success = Win32.FALSE then
kono
parents:
diff changeset
75 Raise_Error ("error closing the port");
kono
parents:
diff changeset
76 end if;
kono
parents:
diff changeset
77 end if;
kono
parents:
diff changeset
78 end Close;
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 ----------
kono
parents:
diff changeset
81 -- Name --
kono
parents:
diff changeset
82 ----------
kono
parents:
diff changeset
83
kono
parents:
diff changeset
84 function Name (Number : Positive) return Port_Name is
kono
parents:
diff changeset
85 N_Img : constant String := Positive'Image (Number);
kono
parents:
diff changeset
86 begin
kono
parents:
diff changeset
87 if Number > 9 then
kono
parents:
diff changeset
88 return
kono
parents:
diff changeset
89 Port_Name ("\\.\COM" & N_Img (N_Img'First + 1 .. N_Img'Last));
kono
parents:
diff changeset
90 else
kono
parents:
diff changeset
91 return
kono
parents:
diff changeset
92 Port_Name ("COM" & N_Img (N_Img'First + 1 .. N_Img'Last) & ':');
kono
parents:
diff changeset
93 end if;
kono
parents:
diff changeset
94 end Name;
kono
parents:
diff changeset
95
kono
parents:
diff changeset
96 ----------
kono
parents:
diff changeset
97 -- Open --
kono
parents:
diff changeset
98 ----------
kono
parents:
diff changeset
99
kono
parents:
diff changeset
100 procedure Open
kono
parents:
diff changeset
101 (Port : out Serial_Port;
kono
parents:
diff changeset
102 Name : Port_Name)
kono
parents:
diff changeset
103 is
kono
parents:
diff changeset
104 C_Name : constant String := String (Name) & ASCII.NUL;
kono
parents:
diff changeset
105 Success : BOOL;
kono
parents:
diff changeset
106 pragma Unreferenced (Success);
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 begin
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
109 if Port.H /= -1 then
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
110 Success := CloseHandle (HANDLE (Port.H));
111
kono
parents:
diff changeset
111 end if;
kono
parents:
diff changeset
112
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
113 Port.H := CreateFileA
111
kono
parents:
diff changeset
114 (lpFileName => C_Name (C_Name'First)'Address,
kono
parents:
diff changeset
115 dwDesiredAccess => GENERIC_READ or GENERIC_WRITE,
kono
parents:
diff changeset
116 dwShareMode => 0,
kono
parents:
diff changeset
117 lpSecurityAttributes => null,
kono
parents:
diff changeset
118 dwCreationDisposition => OPEN_EXISTING,
kono
parents:
diff changeset
119 dwFlagsAndAttributes => 0,
kono
parents:
diff changeset
120 hTemplateFile => 0);
kono
parents:
diff changeset
121
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
122 pragma Assert (INVALID_HANDLE_VALUE = -1);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
123
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
124 if Port.H = Serial_Port_Descriptor (INVALID_HANDLE_VALUE) then
111
kono
parents:
diff changeset
125 Raise_Error ("cannot open com port");
kono
parents:
diff changeset
126 end if;
kono
parents:
diff changeset
127 end Open;
kono
parents:
diff changeset
128
kono
parents:
diff changeset
129 -----------------
kono
parents:
diff changeset
130 -- Raise_Error --
kono
parents:
diff changeset
131 -----------------
kono
parents:
diff changeset
132
kono
parents:
diff changeset
133 procedure Raise_Error (Message : String; Error : DWORD := GetLastError) is
kono
parents:
diff changeset
134 begin
kono
parents:
diff changeset
135 raise Serial_Error with Message
kono
parents:
diff changeset
136 & (if Error /= 0
kono
parents:
diff changeset
137 then " (" & GNAT.OS_Lib.Errno_Message (Err => Integer (Error)) & ')'
kono
parents:
diff changeset
138 else "");
kono
parents:
diff changeset
139 end Raise_Error;
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 ----------
kono
parents:
diff changeset
142 -- Read --
kono
parents:
diff changeset
143 ----------
kono
parents:
diff changeset
144
kono
parents:
diff changeset
145 overriding procedure Read
kono
parents:
diff changeset
146 (Port : in out Serial_Port;
kono
parents:
diff changeset
147 Buffer : out Stream_Element_Array;
kono
parents:
diff changeset
148 Last : out Stream_Element_Offset)
kono
parents:
diff changeset
149 is
kono
parents:
diff changeset
150 Success : BOOL;
kono
parents:
diff changeset
151 Read_Last : aliased DWORD;
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 begin
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
154 if Port.H = -1 then
111
kono
parents:
diff changeset
155 Raise_Error ("read: port not opened", 0);
kono
parents:
diff changeset
156 end if;
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 Success :=
kono
parents:
diff changeset
159 ReadFile
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
160 (hFile => HANDLE (Port.H),
111
kono
parents:
diff changeset
161 lpBuffer => Buffer (Buffer'First)'Address,
kono
parents:
diff changeset
162 nNumberOfBytesToRead => DWORD (Buffer'Length),
kono
parents:
diff changeset
163 lpNumberOfBytesRead => Read_Last'Access,
kono
parents:
diff changeset
164 lpOverlapped => null);
kono
parents:
diff changeset
165
kono
parents:
diff changeset
166 if Success = Win32.FALSE then
kono
parents:
diff changeset
167 Raise_Error ("read error");
kono
parents:
diff changeset
168 end if;
kono
parents:
diff changeset
169
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
170 Last := Last_Index (Buffer'First, CRTL.size_t (Read_Last));
111
kono
parents:
diff changeset
171 end Read;
kono
parents:
diff changeset
172
kono
parents:
diff changeset
173 ---------
kono
parents:
diff changeset
174 -- Set --
kono
parents:
diff changeset
175 ---------
kono
parents:
diff changeset
176
kono
parents:
diff changeset
177 procedure Set
kono
parents:
diff changeset
178 (Port : Serial_Port;
kono
parents:
diff changeset
179 Rate : Data_Rate := B9600;
kono
parents:
diff changeset
180 Bits : Data_Bits := CS8;
kono
parents:
diff changeset
181 Stop_Bits : Stop_Bits_Number := One;
kono
parents:
diff changeset
182 Parity : Parity_Check := None;
kono
parents:
diff changeset
183 Block : Boolean := True;
kono
parents:
diff changeset
184 Local : Boolean := True;
kono
parents:
diff changeset
185 Flow : Flow_Control := None;
kono
parents:
diff changeset
186 Timeout : Duration := 10.0)
kono
parents:
diff changeset
187 is
kono
parents:
diff changeset
188 pragma Unreferenced (Local);
kono
parents:
diff changeset
189
kono
parents:
diff changeset
190 Success : BOOL;
kono
parents:
diff changeset
191 Com_Time_Out : aliased COMMTIMEOUTS;
kono
parents:
diff changeset
192 Com_Settings : aliased DCB;
kono
parents:
diff changeset
193
kono
parents:
diff changeset
194 begin
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
195 if Port.H = -1 then
111
kono
parents:
diff changeset
196 Raise_Error ("set: port not opened", 0);
kono
parents:
diff changeset
197 end if;
kono
parents:
diff changeset
198
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
199 Success := GetCommState (HANDLE (Port.H), Com_Settings'Access);
111
kono
parents:
diff changeset
200
kono
parents:
diff changeset
201 if Success = Win32.FALSE then
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
202 Success := CloseHandle (HANDLE (Port.H));
111
kono
parents:
diff changeset
203 Raise_Error ("set: cannot get comm state");
kono
parents:
diff changeset
204 end if;
kono
parents:
diff changeset
205
kono
parents:
diff changeset
206 Com_Settings.BaudRate := DWORD (Data_Rate_Value (Rate));
kono
parents:
diff changeset
207 Com_Settings.fParity := 1;
kono
parents:
diff changeset
208 Com_Settings.fBinary := Bits1 (System.Win32.TRUE);
kono
parents:
diff changeset
209 Com_Settings.fOutxDsrFlow := 0;
kono
parents:
diff changeset
210 Com_Settings.fDsrSensitivity := 0;
kono
parents:
diff changeset
211 Com_Settings.fDtrControl := OSC.DTR_CONTROL_ENABLE;
kono
parents:
diff changeset
212 Com_Settings.fInX := 0;
kono
parents:
diff changeset
213 Com_Settings.fRtsControl := OSC.RTS_CONTROL_ENABLE;
kono
parents:
diff changeset
214
kono
parents:
diff changeset
215 case Flow is
kono
parents:
diff changeset
216 when None =>
kono
parents:
diff changeset
217 Com_Settings.fOutX := 0;
kono
parents:
diff changeset
218 Com_Settings.fOutxCtsFlow := 0;
kono
parents:
diff changeset
219
kono
parents:
diff changeset
220 when RTS_CTS =>
kono
parents:
diff changeset
221 Com_Settings.fOutX := 0;
kono
parents:
diff changeset
222 Com_Settings.fOutxCtsFlow := 1;
kono
parents:
diff changeset
223
kono
parents:
diff changeset
224 when Xon_Xoff =>
kono
parents:
diff changeset
225 Com_Settings.fOutX := 1;
kono
parents:
diff changeset
226 Com_Settings.fOutxCtsFlow := 0;
kono
parents:
diff changeset
227 end case;
kono
parents:
diff changeset
228
kono
parents:
diff changeset
229 Com_Settings.fAbortOnError := 0;
kono
parents:
diff changeset
230 Com_Settings.ByteSize := BYTE (C_Bits (Bits));
kono
parents:
diff changeset
231 Com_Settings.Parity := BYTE (C_Parity (Parity));
kono
parents:
diff changeset
232 Com_Settings.StopBits := BYTE (C_Stop_Bits (Stop_Bits));
kono
parents:
diff changeset
233
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
234 Success := SetCommState (HANDLE (Port.H), Com_Settings'Access);
111
kono
parents:
diff changeset
235
kono
parents:
diff changeset
236 if Success = Win32.FALSE then
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
237 Success := CloseHandle (HANDLE (Port.H));
111
kono
parents:
diff changeset
238 Raise_Error ("cannot set comm state");
kono
parents:
diff changeset
239 end if;
kono
parents:
diff changeset
240
kono
parents:
diff changeset
241 -- Set the timeout status, to honor our spec with respect to read
kono
parents:
diff changeset
242 -- timeouts. Always disconnect write timeouts.
kono
parents:
diff changeset
243
kono
parents:
diff changeset
244 -- Blocking reads - no timeout at all
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 if Block then
kono
parents:
diff changeset
247 Com_Time_Out := (others => 0);
kono
parents:
diff changeset
248
kono
parents:
diff changeset
249 -- Non-blocking reads and null timeout - immediate return with what we
kono
parents:
diff changeset
250 -- have - set ReadIntervalTimeout to MAXDWORD.
kono
parents:
diff changeset
251
kono
parents:
diff changeset
252 elsif Timeout = 0.0 then
kono
parents:
diff changeset
253 Com_Time_Out :=
kono
parents:
diff changeset
254 (ReadIntervalTimeout => DWORD'Last,
kono
parents:
diff changeset
255 others => 0);
kono
parents:
diff changeset
256
kono
parents:
diff changeset
257 -- Non-blocking reads with timeout - set total read timeout accordingly
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 else
kono
parents:
diff changeset
260 Com_Time_Out :=
kono
parents:
diff changeset
261 (ReadTotalTimeoutConstant => DWORD (1000 * Timeout),
kono
parents:
diff changeset
262 others => 0);
kono
parents:
diff changeset
263 end if;
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 Success :=
kono
parents:
diff changeset
266 SetCommTimeouts
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
267 (hFile => HANDLE (Port.H),
111
kono
parents:
diff changeset
268 lpCommTimeouts => Com_Time_Out'Access);
kono
parents:
diff changeset
269
kono
parents:
diff changeset
270 if Success = Win32.FALSE then
kono
parents:
diff changeset
271 Raise_Error ("cannot set the timeout");
kono
parents:
diff changeset
272 end if;
kono
parents:
diff changeset
273 end Set;
kono
parents:
diff changeset
274
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
275 ------------
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
276 -- To_Ada --
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
277 ------------
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
278
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
279 procedure To_Ada (Port : out Serial_Port; Fd : Serial_Port_Descriptor) is
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
280 begin
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
281 Port.H := Fd;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
282 end To_Ada;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
283
111
kono
parents:
diff changeset
284 -----------
kono
parents:
diff changeset
285 -- Write --
kono
parents:
diff changeset
286 -----------
kono
parents:
diff changeset
287
kono
parents:
diff changeset
288 overriding procedure Write
kono
parents:
diff changeset
289 (Port : in out Serial_Port;
kono
parents:
diff changeset
290 Buffer : Stream_Element_Array)
kono
parents:
diff changeset
291 is
kono
parents:
diff changeset
292 Success : BOOL;
kono
parents:
diff changeset
293 Temp_Last : aliased DWORD;
kono
parents:
diff changeset
294
kono
parents:
diff changeset
295 begin
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
296 if Port.H = -1 then
111
kono
parents:
diff changeset
297 Raise_Error ("write: port not opened", 0);
kono
parents:
diff changeset
298 end if;
kono
parents:
diff changeset
299
kono
parents:
diff changeset
300 Success :=
kono
parents:
diff changeset
301 WriteFile
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
302 (hFile => HANDLE (Port.H),
111
kono
parents:
diff changeset
303 lpBuffer => Buffer'Address,
kono
parents:
diff changeset
304 nNumberOfBytesToWrite => DWORD (Buffer'Length),
kono
parents:
diff changeset
305 lpNumberOfBytesWritten => Temp_Last'Access,
kono
parents:
diff changeset
306 lpOverlapped => null);
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 if Success = Win32.FALSE
kono
parents:
diff changeset
309 or else Stream_Element_Offset (Temp_Last) /= Buffer'Length
kono
parents:
diff changeset
310 then
kono
parents:
diff changeset
311 Raise_Error ("failed to write data");
kono
parents:
diff changeset
312 end if;
kono
parents:
diff changeset
313 end Write;
kono
parents:
diff changeset
314
kono
parents:
diff changeset
315 end GNAT.Serial_Communications;