polyorb_hi_drivers_gruart.adb 12 KB
Newer Older
jhugues's avatar
jhugues committed
1
2
3
4
5
6
7
8
------------------------------------------------------------------------------
--                                                                          --
--                          PolyORB HI COMPONENTS                           --
--                                                                          --
--            P O L Y O R B _ H I _ D R I V E R S _ G R U A R T             --
--                                                                          --
--                                 B o d y                                  --
--                                                                          --
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
--                   Copyright (C) 2012-2015 ESA & ISAE.                    --
--                                                                          --
-- PolyORB-HI is free software; you can redistribute it and/or modify under --
-- terms of the  GNU General Public License as published  by the Free Soft- --
-- ware  Foundation;  either version 3,  or (at your option) any later ver- --
-- sion. PolyORB-HI is distributed in the hope that it will be useful, but  --
-- WITHOUT ANY WARRANTY; without even the implied warranty of               --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
jhugues's avatar
jhugues committed
26
27
28
29
30
31
--                                                                          --
--              PolyORB-HI/Ada is maintained by the TASTE project           --
--                      (taste-users@lists.tuxfamily.org)                   --
--                                                                          --
------------------------------------------------------------------------------

32
With Interfaces;
33
with Ada.Unchecked_Conversion;
34

35
with Uart.Core; use type UART.Core.UART_Device;
36
37
with Uart.HLInterface;
with Uart.Streams;
38
39
40
41
42
43
44
45
46

with PolyORB_HI.Output;
with PolyORB_HI.Messages;

with PolyORB_HI_Generated.Transport;

--  This package provides support for the GRUART device driver as
--  defined in the GRUART AADLv2 model.

47
48
49
50
with System; use System;

with POHICDRIVER_UART; use POHICDRIVER_UART;

51
package body PolyORB_HI_Drivers_GRUART is
52

jhugues's avatar
jhugues committed
53
54
55
56
57
58
59
60
61
62
63
   task body Idle_Task is
      --  This Idle task is present to ensure the leon processor is
      --  never put idle, which would cause the processor to never be
      --  awakened by external events.
      --  XXX Check whether this is still necessary
   begin
      loop
         null;
      end loop;
   end Idle_Task;

64
65
66
67
   type Serial_Conf_T_Acc is access all POHICDRIVER_UART.Serial_Conf_T;
   function To_Serial_Conf_T_Acc is new Ada.Unchecked_Conversion
     (System.Address, Serial_Conf_T_Acc);

68
   To_GNAT_Baud_Rate : constant array (POHICDRIVER_UART.Baudrate_T) of
69
70
71
72
73
74
75
     UART.HLInterface.Data_Rate :=
     (B9600 => UART.HLInterface.B9600,
      B19200 => UART.HLInterface.B19200,
      B38400 => UART.HLInterface.B38400,
      B57600 => UART.HLInterface.B57600,
      B115200 => UART.HLInterface.B115200,
      B230400 => UART.HLInterface.B115200);
jhugues's avatar
jhugues committed
76
   --  XXX does not exist in ORK+
77
78
79
80
81
82
83
84

   To_GNAT_Parity_Check : constant array (POHICDRIVER_UART.Parity_T) of
     UART.HLInterface.Parity_Check :=
     (Even => UART.HLInterface.Even,
      Odd => UART.HLInterface.Odd);

   To_GNAT_Bits : constant array (7 .. 8) of
     UART.HLInterface.Data_Bits :=
85
     (7 => UART.HLInterface.B8,
86
87
      8 => UART.HLInterface.B8);

88
89
90
91
92
93
94
95
96
97
   pragma Suppress (Elaboration_Check, PolyORB_HI_Generated.Transport);
   --  We do not want a pragma Elaborate_All to be implicitely
   --  generated for Transport.

   use Interfaces;
   use PolyORB_HI.Messages;
   use PolyORB_HI.Utils;
   use PolyORB_HI.Output;

   type Node_Record is record
98
99
      --  UART is a simple protocol, we use one port to send, assuming
      --  it can be used in full duplex mode.
100

101
102
103
      UART_Port   : Uart.HLInterface.Serial_Port;
      UART_Device : Uart.Core.UART_Device;
      UART_Config : Serial_Conf_T;
104
105
106
107
   end record;

   Nodes : array (Node_Type) of Node_Record;

108
   subtype AS_Message_Length_Stream is Uart.STreams.Stream_Element_Array
109
110
111
112
     (1 .. Message_Length_Size);
   subtype Message_Length_Stream is Stream_Element_Array
     (1 .. Message_Length_Size);

113
   subtype AS_Full_Stream is Uart.Streams.Stream_Element_Array (1 .. PDU_Size);
114
115
116
117
118
119
120
121
122
123
124
125
126
   subtype Full_Stream is Stream_Element_Array (1 .. PDU_Size);

   function To_PO_HI_Message_Length_Stream is new Ada.Unchecked_Conversion
     (AS_Message_Length_Stream, Message_Length_Stream);
   function To_PO_HI_Full_Stream is new Ada.Unchecked_Conversion
     (AS_Full_Stream, Full_Stream);

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize (Name_Table : PolyORB_HI.Utils.Naming_Table_Type) is
      Success : Boolean;
127
      Use_Asn1 : Boolean := False;
128
      Parity : UART.HLInterface.Parity_Check;
129
130
131
132

   begin
      Uart.HLInterface.Initialize (Success);
      if not Success then
jhugues's avatar
jhugues committed
133
134
135
	 Put_Line (Normal,
		   "Initialization failure: cannot find UART cores");
	 raise Program_Error;
136
137
138
      end if;

      for J in Name_Table'Range loop
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
	 if Name_Table (J).Variable = System.Null_Address then
	    Nodes (J).UART_Device
	      := Uart.Core.UART_Device'Value
	      (To_String (Name_Table (J).Location) (1 .. 1));

	    --  Note: we only consider the first half of the
	    --  configuration string.

	 else
	    Nodes (J).UART_Config := To_Serial_Conf_T_Acc
	      (Name_Table (J).Variable).all;
	    Use_Asn1 := True;
            Put_Line (Normal, "Device: " & Nodes (J).UART_Config.devname);

	    --  Translate the device name into an UART_Device

	    if Nodes (J).UART_Config.Devname (1 .. 14) /= "/dev/apburasta" then
jhugues's avatar
jhugues committed
156
	       Put_Line (Error, "invalid device name");
157
158
159
160
161
162
163
164
165
166
167

	    else
	       --  We assume the device name to be "/dev/apburastaX"
	       --  with X in 0 .. 2. We need to move X to the 1 .. 3
	       --  range.

	       Nodes (J).UART_Device
		 := UART.Core.UART_Device
		 (Integer'Value (Nodes (J).UART_Config.Devname (15 .. 15)) + 1);
	    end if;
	 end if;
168
169
      end loop;

jhugues's avatar
jhugues committed
170
171
172
      Put_Line (Normal, "Opening UART #"
                     & Nodes (My_Node).UART_Device'Img);

173
174
175
176
      Uart.HLInterface.Open (Port   => Nodes (My_Node).UART_Port,
			     Number => Nodes (My_Node).UART_Device);

      if not Use_Asn1 then
jhugues's avatar
jhugues committed
177
178
	 Put_Line (Normal, " -> Using default configuration");

179
180
181
182
	 Uart.HLInterface.Set (Port   => Nodes (My_Node).UART_Port,
			       Rate => Uart.HLInterface.B19200,
			       Block => True);
      else
jhugues's avatar
jhugues committed
183
184
	 Put_Line (Normal, " -> Using ASN.1 configuration");

185
186
187
	 if Nodes (My_Node).UART_Config.Use_Paritybit then
	    Parity := To_GNAT_Parity_Check (Nodes (My_Node).UART_Config.Parity);
	 else
jhugues's avatar
jhugues committed
188
	    Put_Line (Normal, "  * Use Parity bits: FALSE");
189
190
191
	    Parity := UART.HLInterface.None;
	 end if;

jhugues's avatar
jhugues committed
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
	 declare
	    use Uart.HLInterface;

	    Rate : constant Uart.HLInterface.Data_Rate
	      := To_GNAT_Baud_Rate (Nodes (My_Node).UART_Config.Speed);
	    Bits : constant Data_Bits
	      := To_GNAT_Bits (Integer (Nodes (My_Node).UART_Config.Bits));
	 begin
	    case Rate is
	       when B1200 =>
		  Put_Line (Normal, "  * Rate : B1200");
	       when B2400 =>
		  Put_Line (Normal, "  * Rate : B2400");
	       when B4800 =>
		  Put_Line (Normal, "  * Rate : B4800");
	       when B9600 =>
		  Put_Line (Normal, "  * Rate : B9600");
	       when B19200 =>
		  Put_Line (Normal, "  * Rate : B19200");
	       when B38400 =>
		  Put_Line (Normal, "  * Rate : B38400");
	       when B57600 =>
		  Put_Line (Normal, "  * Rate : B57600");
	       when B115200 =>
		  Put_Line (Normal, "  * Rate : B115200");
	    end case;

	    case Parity is
	       when None =>
		  Put_Line (Normal, "  * Parity: None");
	       when Odd =>
		  Put_Line (Normal, "  * Parity: Odd");
	       when Even =>
		  Put_Line (Normal, "  * Parity: Even");
	    end case;

	    case Bits is
	       when B8 =>
		  Put_Line (Normal, "  * Bits: B8");
	       when B7 =>
		  Put_Line (Normal, "  * Bits: B7");
	    end case;

	 end;

237
	 UART.HLInterface.Set
jhugues's avatar
jhugues committed
238
           (Port   => Nodes (My_Node).UART_Port,
239
	    Rate   => To_GNAT_Baud_Rate (Nodes (My_Node).UART_Config.Speed),
240
	    Parity => Parity,
241
242
243
	    Bits   => To_GNAT_Bits (Integer (Nodes (My_Node).UART_Config.Bits)),
	    Block  => True);
      end if;
244
245
246
247
248
249
250
251
252
      pragma Debug (Put_Line (Normal, "Initialization of UART subsystem"
                                & " is complete"));
   end Initialize;

   -------------
   -- Receive --
   -------------

   procedure Receive is
253
      use type Uart.Streams.Stream_Element_Offset;
254
255
256

      SEL : AS_Message_Length_Stream;
      SEA : AS_Full_Stream;
257
258
259
      SEO : Uart.Streams.Stream_Element_Offset;
      Packet_Size : Uart.Streams.Stream_Element_Offset;
      Data_Received_Index : Uart.Streams.Stream_Element_Offset;
260
261
262
263
264
   begin

      Main_Loop : loop
         Put_Line ("Using user-provided GRUART stack to receive");
         Put_Line ("Waiting on UART #"
265
                     & Nodes (My_Node).UART_Device'Img);
266
267
268
269
270

         --  UART is a character-oriented protocol

         --  1/ Receive message length

271
         Uart.HLInterface.Read (Nodes (My_Node).UART_Port, SEL, SEO);
272

273
         Packet_Size := Uart.Streams.Stream_Element_Offset
274
           (To_Length (To_PO_HI_Message_Length_Stream (SEL)));
275
276
         SEO := Packet_Size;

277
278
279
280
         SEA (1 .. Message_Length_Size) := SEL;

         Data_Received_Index := Message_Length_Size + 1;

281
         while Data_Received_Index <= Packet_Size + Message_Length_Size loop
282
283
            --  We must loop to make sure we receive all data

284
            Uart.HLInterface.Read (Nodes (My_Node).UART_Port,
285
286
                                   SEA (Data_Received_Index .. SEO + 1),
                                   SEO);
287
            Data_Received_Index := 1 + SEO + 1;
288
289
290
291
292
293
294
295
         end loop;

         --  2/ Receive full message

         if SEO /= SEA'First - 1 then
            Put_Line
              (Normal,
               "UART #"
296
                 & Nodes (My_Node).UART_Device'Img
297
                 & " received"
298
                 & Uart.Streams.Stream_Element_Offset'Image (SEO)
299
300
301
302
303
304
                 & " bytes");

            --  Deliver to the peer handler

            PolyORB_HI_Generated.Transport.Deliver
              (Corresponding_Entity
305
                 (Unsigned_8 (SEA (Message_Length_Size + 1))),
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
               To_PO_HI_Full_Stream (SEA)
                 (1 .. Stream_Element_Offset (SEO)));
         else
            Put_Line ("Got error");
         end if;
      end loop Main_Loop;
   end Receive;

   ----------
   -- Send --
   ----------

   function Send
     (Node    : Node_Type;
      Message : Stream_Element_Array;
      Size    : Stream_Element_Offset)
     return Error_Kind
   is
      --  We cannot cast both array types using
      --  Ada.Unchecked_Conversion because they are unconstrained
      --  types. We cannot either use direct casting because component
      --  types are incompatible. The only time efficient manner to do
      --  the casting is to use representation clauses.

330
331
      Msg : Uart.Streams.Stream_Element_Array
        (1 .. Uart.Streams.Stream_Element_Offset (Size));
332
333
334
335
336
337
      pragma Import (Ada, Msg);
      for Msg'Address use Message'Address;

   begin
      Put_Line ("Using user-provided UART stack to send");
      Put_Line ("Sending through UART #"
338
                  & Nodes (Node).UART_Device'Img
339
340
                  & Size'Img & " bytes");

341
      Uart.HLInterface.Write (Port   => Nodes (My_Node).UART_Port,
342
343
344
                              Buffer => Msg);

      return Error_Kind'(Error_None);
345
      --  Note: we have no way to know there was an error here
346
347
   end Send;

348
end PolyORB_HI_Drivers_GRUART;