* opts.c (finish_options): Remove duplicate sorry.
[official-gcc.git] / gcc / ada / g-sercom-mingw.adb
blobdf3754b685a3b9b53baca823c93584a08c8d7f68
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-2010, 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 3, 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. --
17 -- --
18 -- As a special exception under Section 7 of GPL version 3, you are granted --
19 -- additional permissions described in the GCC Runtime Library Exception, --
20 -- version 3.1, as published by the Free Software Foundation. --
21 -- --
22 -- You should have received a copy of the GNU General Public License and --
23 -- a copy of the GCC Runtime Library Exception along with this program; --
24 -- see the files COPYING3 and COPYING.RUNTIME respectively. If not, see --
25 -- <http://www.gnu.org/licenses/>. --
26 -- --
27 -- GNAT was originally developed by the GNAT team at New York University. --
28 -- Extensive contributions were provided by Ada Core Technologies Inc. --
29 -- --
30 ------------------------------------------------------------------------------
32 -- This is the Windows implementation of this package
34 with Ada.Unchecked_Deallocation; use Ada;
35 with Ada.Streams; use Ada.Streams;
37 with System; use System;
38 with System.Communication; use System.Communication;
39 with System.CRTL; use System.CRTL;
40 with System.Win32; use System.Win32;
41 with System.Win32.Ext; use System.Win32.Ext;
43 package body GNAT.Serial_Communications is
45 -- Common types
47 type Port_Data is new HANDLE;
49 C_Bits : constant array (Data_Bits) of Interfaces.C.unsigned := (8, 7);
50 C_Parity : constant array (Parity_Check) of Interfaces.C.unsigned :=
51 (None => NOPARITY, Odd => ODDPARITY, Even => EVENPARITY);
52 C_Stop_Bits : constant array (Stop_Bits_Number) of Interfaces.C.unsigned :=
53 (One => ONESTOPBIT, Two => TWOSTOPBITS);
55 -----------
56 -- Files --
57 -----------
59 procedure Raise_Error (Message : String; Error : DWORD := GetLastError);
60 pragma No_Return (Raise_Error);
62 -----------
63 -- Close --
64 -----------
66 procedure Close (Port : in out Serial_Port) is
67 procedure Unchecked_Free is
68 new Unchecked_Deallocation (Port_Data, Port_Data_Access);
70 Success : BOOL;
72 begin
73 if Port.H /= null then
74 Success := CloseHandle (HANDLE (Port.H.all));
75 Unchecked_Free (Port.H);
77 if Success = Win32.FALSE then
78 Raise_Error ("error closing the port");
79 end if;
80 end if;
81 end Close;
83 ----------
84 -- Name --
85 ----------
87 function Name (Number : Positive) return Port_Name is
88 N_Img : constant String := Positive'Image (Number);
89 begin
90 return Port_Name ("COM" & N_Img (N_Img'First + 1 .. N_Img'Last) & ':');
91 end Name;
93 ----------
94 -- Open --
95 ----------
97 procedure Open
98 (Port : out Serial_Port;
99 Name : Port_Name)
101 C_Name : constant String := String (Name) & ASCII.NUL;
102 Success : BOOL;
103 pragma Unreferenced (Success);
105 begin
106 if Port.H = null then
107 Port.H := new Port_Data;
108 else
109 Success := CloseHandle (HANDLE (Port.H.all));
110 end if;
112 Port.H.all := CreateFileA
113 (lpFileName => C_Name (C_Name'First)'Address,
114 dwDesiredAccess => GENERIC_READ or GENERIC_WRITE,
115 dwShareMode => 0,
116 lpSecurityAttributes => null,
117 dwCreationDisposition => OPEN_EXISTING,
118 dwFlagsAndAttributes => 0,
119 hTemplateFile => 0);
121 if Port.H.all = 0 then
122 Raise_Error ("cannot open com port");
123 end if;
124 end Open;
126 -----------------
127 -- Raise_Error --
128 -----------------
130 procedure Raise_Error (Message : String; Error : DWORD := GetLastError) is
131 begin
132 raise Serial_Error with Message & " (" & DWORD'Image (Error) & ')';
133 end Raise_Error;
135 ----------
136 -- Read --
137 ----------
139 overriding procedure Read
140 (Port : in out Serial_Port;
141 Buffer : out Stream_Element_Array;
142 Last : out Stream_Element_Offset)
144 Success : BOOL;
145 Read_Last : aliased DWORD;
147 begin
148 if Port.H = null then
149 Raise_Error ("read: port not opened", 0);
150 end if;
152 Success :=
153 ReadFile
154 (hFile => HANDLE (Port.H.all),
155 lpBuffer => Buffer (Buffer'First)'Address,
156 nNumberOfBytesToRead => DWORD (Buffer'Length),
157 lpNumberOfBytesRead => Read_Last'Access,
158 lpOverlapped => null);
160 if Success = Win32.FALSE then
161 Raise_Error ("read error");
162 end if;
164 Last := Last_Index (Buffer'First, size_t (Read_Last));
165 end Read;
167 ---------
168 -- Set --
169 ---------
171 procedure Set
172 (Port : Serial_Port;
173 Rate : Data_Rate := B9600;
174 Bits : Data_Bits := CS8;
175 Stop_Bits : Stop_Bits_Number := One;
176 Parity : Parity_Check := None;
177 Block : Boolean := True;
178 Timeout : Duration := 10.0)
180 Success : BOOL;
181 Com_Time_Out : aliased COMMTIMEOUTS;
182 Com_Settings : aliased DCB;
184 begin
185 if Port.H = null then
186 Raise_Error ("set: port not opened", 0);
187 end if;
189 Success := GetCommState (HANDLE (Port.H.all), Com_Settings'Access);
191 if Success = Win32.FALSE then
192 Success := CloseHandle (HANDLE (Port.H.all));
193 Port.H.all := 0;
194 Raise_Error ("set: cannot get comm state");
195 end if;
197 Com_Settings.BaudRate := DWORD (Data_Rate_Value (Rate));
198 Com_Settings.fParity := 1;
199 Com_Settings.fBinary := Bits1 (System.Win32.TRUE);
200 Com_Settings.fOutxCtsFlow := 0;
201 Com_Settings.fOutxDsrFlow := 0;
202 Com_Settings.fDsrSensitivity := 0;
203 Com_Settings.fDtrControl := DTR_CONTROL_DISABLE;
204 Com_Settings.fOutX := 0;
205 Com_Settings.fInX := 0;
206 Com_Settings.fRtsControl := RTS_CONTROL_DISABLE;
207 Com_Settings.fAbortOnError := 0;
208 Com_Settings.ByteSize := BYTE (C_Bits (Bits));
209 Com_Settings.Parity := BYTE (C_Parity (Parity));
210 Com_Settings.StopBits := BYTE (C_Stop_Bits (Stop_Bits));
212 Success := SetCommState (HANDLE (Port.H.all), Com_Settings'Access);
214 if Success = Win32.FALSE then
215 Success := CloseHandle (HANDLE (Port.H.all));
216 Port.H.all := 0;
217 Raise_Error ("cannot set comm state");
218 end if;
220 -- Set the timeout status
222 if Block then
223 Com_Time_Out := (others => 0);
224 else
225 Com_Time_Out :=
226 (ReadTotalTimeoutConstant => DWORD (1000 * Timeout),
227 others => 0);
228 end if;
230 Success :=
231 SetCommTimeouts
232 (hFile => HANDLE (Port.H.all),
233 lpCommTimeouts => Com_Time_Out'Access);
235 if Success = Win32.FALSE then
236 Raise_Error ("cannot set the timeout");
237 end if;
238 end Set;
240 -----------
241 -- Write --
242 -----------
244 overriding procedure Write
245 (Port : in out Serial_Port;
246 Buffer : Stream_Element_Array)
248 Success : BOOL;
249 Temp_Last : aliased DWORD;
251 begin
252 if Port.H = null then
253 Raise_Error ("write: port not opened", 0);
254 end if;
256 Success :=
257 WriteFile
258 (hFile => HANDLE (Port.H.all),
259 lpBuffer => Buffer'Address,
260 nNumberOfBytesToWrite => DWORD (Buffer'Length),
261 lpNumberOfBytesWritten => Temp_Last'Access,
262 lpOverlapped => null);
264 if Success = Win32.FALSE
265 or else Stream_Element_Offset (Temp_Last) /= Buffer'Length
266 then
267 Raise_Error ("failed to write data");
268 end if;
269 end Write;
271 end GNAT.Serial_Communications;