OSDN Git Service

e5034115995a65e8493a6f6185b17e7e4556e437
[pf3gnuchains/gcc-fork.git] / gcc / ada / g-sercom-mingw.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-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 Windows implementation of this package
35
36 with Ada.Unchecked_Deallocation; use Ada;
37 with Ada.Streams;                use Ada.Streams;
38
39 with System;               use System;
40 with System.Communication; use System.Communication;
41 with System.Win32;         use System.Win32;
42 with System.Win32.Ext;     use System.Win32.Ext;
43
44 package body GNAT.Serial_Communications is
45
46    --  Common types
47
48    type Port_Data is new HANDLE;
49
50    C_Bits      : constant array (Data_Bits) of Interfaces.C.unsigned := (8, 7);
51    C_Parity    : constant array (Parity_Check) of Interfaces.C.unsigned :=
52                    (None => NOPARITY, Odd => ODDPARITY, Even => EVENPARITY);
53    C_Stop_Bits : constant array (Stop_Bits_Number) of Interfaces.C.unsigned :=
54                    (One => ONESTOPBIT, Two => TWOSTOPBITS);
55
56    -----------
57    -- Files --
58    -----------
59
60    procedure Raise_Error (Message : String; Error : DWORD := GetLastError);
61    pragma No_Return (Raise_Error);
62
63    -----------
64    -- Close --
65    -----------
66
67    procedure Close (Port : in out Serial_Port) is
68       procedure Unchecked_Free is
69         new Unchecked_Deallocation (Port_Data, Port_Data_Access);
70
71       Success : BOOL;
72
73    begin
74       if Port.H /= null then
75          Success := CloseHandle (HANDLE (Port.H.all));
76          Unchecked_Free (Port.H);
77
78          if Success = Win32.FALSE then
79             Raise_Error ("error closing the port");
80          end if;
81       end if;
82    end Close;
83
84    ----------
85    -- Name --
86    ----------
87
88    function Name (Number : Positive) return Port_Name is
89       N_Img : constant String := Positive'Image (Number);
90    begin
91       return Port_Name ("COM" & N_Img (N_Img'First + 1 .. N_Img'Last) & ':');
92    end Name;
93
94    ----------
95    -- Open --
96    ----------
97
98    procedure Open
99      (Port : out Serial_Port;
100       Name : Port_Name)
101    is
102       C_Name  : constant String := String (Name) & ASCII.NUL;
103       Success : BOOL;
104       pragma Unreferenced (Success);
105
106    begin
107       if Port.H = null then
108          Port.H := new Port_Data;
109       else
110          Success := CloseHandle (HANDLE (Port.H.all));
111       end if;
112
113       Port.H.all := CreateFileA
114         (lpFileName            => C_Name (C_Name'First)'Address,
115          dwDesiredAccess       => GENERIC_READ or GENERIC_WRITE,
116          dwShareMode           => 0,
117          lpSecurityAttributes  => null,
118          dwCreationDisposition => OPEN_EXISTING,
119          dwFlagsAndAttributes  => 0,
120          hTemplateFile         => 0);
121
122       if Port.H.all = 0 then
123          Raise_Error ("cannot open com port");
124       end if;
125    end Open;
126
127    -----------------
128    -- Raise_Error --
129    -----------------
130
131    procedure Raise_Error (Message : String; Error : DWORD := GetLastError) is
132    begin
133       raise Serial_Error with Message & " (" & DWORD'Image (Error) & ')';
134    end Raise_Error;
135
136    ----------
137    -- Read --
138    ----------
139
140    overriding procedure Read
141      (Port   : in out Serial_Port;
142       Buffer : out Stream_Element_Array;
143       Last   : out Stream_Element_Offset)
144    is
145       Success   : BOOL;
146       Read_Last : aliased DWORD;
147
148    begin
149       if Port.H = null then
150          Raise_Error ("read: port not opened", 0);
151       end if;
152
153       Success :=
154         ReadFile
155           (hFile                => HANDLE (Port.H.all),
156            lpBuffer             => Buffer (Buffer'First)'Address,
157            nNumberOfBytesToRead => DWORD (Buffer'Length),
158            lpNumberOfBytesRead  => Read_Last'Access,
159            lpOverlapped         => null);
160
161       if Success = Win32.FALSE then
162          Raise_Error ("read error");
163       end if;
164
165       Last := Last_Index (Buffer'First, C.int (Read_Last));
166    end Read;
167
168    ---------
169    -- Set --
170    ---------
171
172    procedure Set
173      (Port      : Serial_Port;
174       Rate      : Data_Rate        := B9600;
175       Bits      : Data_Bits        := CS8;
176       Stop_Bits : Stop_Bits_Number := One;
177       Parity    : Parity_Check     := None;
178       Block     : Boolean          := True;
179       Timeout   : Duration         := 10.0)
180    is
181       Success      : BOOL;
182       Com_Time_Out : aliased COMMTIMEOUTS;
183       Com_Settings : aliased DCB;
184
185    begin
186       if Port.H = null then
187          Raise_Error ("set: port not opened", 0);
188       end if;
189
190       Success := GetCommState (HANDLE (Port.H.all), Com_Settings'Access);
191
192       if Success = Win32.FALSE then
193          Success := CloseHandle (HANDLE (Port.H.all));
194          Port.H.all := 0;
195          Raise_Error ("set: cannot get comm state");
196       end if;
197
198       Com_Settings.BaudRate        := DWORD (Data_Rate_Value (Rate));
199       Com_Settings.fParity         := 1;
200       Com_Settings.fBinary         := Bits1 (System.Win32.TRUE);
201       Com_Settings.fOutxCtsFlow    := 0;
202       Com_Settings.fOutxDsrFlow    := 0;
203       Com_Settings.fDsrSensitivity := 0;
204       Com_Settings.fDtrControl     := DTR_CONTROL_DISABLE;
205       Com_Settings.fOutX           := 0;
206       Com_Settings.fInX            := 0;
207       Com_Settings.fRtsControl     := RTS_CONTROL_DISABLE;
208       Com_Settings.fAbortOnError   := 0;
209       Com_Settings.ByteSize        := BYTE (C_Bits (Bits));
210       Com_Settings.Parity          := BYTE (C_Parity (Parity));
211       Com_Settings.StopBits        := BYTE (C_Stop_Bits (Stop_Bits));
212
213       Success := SetCommState (HANDLE (Port.H.all), Com_Settings'Access);
214
215       if Success = Win32.FALSE then
216          Success := CloseHandle (HANDLE (Port.H.all));
217          Port.H.all := 0;
218          Raise_Error ("cannot set comm state");
219       end if;
220
221       --  Set the timeout status
222
223       if Block then
224          Com_Time_Out := (others => 0);
225       else
226          Com_Time_Out :=
227            (ReadTotalTimeoutConstant => DWORD (1000 * Timeout),
228             others                   => 0);
229       end if;
230
231       Success :=
232         SetCommTimeouts
233           (hFile          => HANDLE (Port.H.all),
234            lpCommTimeouts => Com_Time_Out'Access);
235
236       if Success = Win32.FALSE then
237          Raise_Error ("cannot set the timeout");
238       end if;
239    end Set;
240
241    -----------
242    -- Write --
243    -----------
244
245    overriding procedure Write
246      (Port   : in out Serial_Port;
247       Buffer : Stream_Element_Array)
248    is
249       Success   : BOOL;
250       Temp_Last : aliased DWORD;
251
252    begin
253       if Port.H = null then
254          Raise_Error ("write: port not opened", 0);
255       end if;
256
257       Success :=
258         WriteFile
259           (hFile                  => HANDLE (Port.H.all),
260            lpBuffer               => Buffer'Address,
261            nNumberOfBytesToWrite  => DWORD (Buffer'Length),
262            lpNumberOfBytesWritten => Temp_Last'Access,
263            lpOverlapped           => null);
264
265       if Success = Win32.FALSE
266         or else Stream_Element_Offset (Temp_Last) /= Buffer'Length
267       then
268          Raise_Error ("failed to write data");
269       end if;
270    end Write;
271
272 end GNAT.Serial_Communications;