polyorb_hi_drivers_gruart.adb 7.94 KB
Newer Older
1
2
3
pragma Style_Checks (Off);

With Interfaces;
4
with Ada.Unchecked_Conversion;
5

6
with Uart.Core; use type UART.Core.UART_Device;
7
8
with Uart.HLInterface;
with Uart.Streams;
9
10
11
12
13
14
15
16
17

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.

18
19
20
21
with System; use System;

with POHICDRIVER_UART; use POHICDRIVER_UART;

22
package body PolyORB_HI_Drivers_GRUART is
23

24
25
26
27
   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);

28
   To_GNAT_Baud_Rate : constant array (POHICDRIVER_UART.Baudrate_T) of
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
     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);
   --  XXX does not exist in GCC.4.4.4

   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 :=
     (7 => UART.HLInterface.B7,
      8 => UART.HLInterface.B8);

48
49
50
51
52
53
54
55
56
57
   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
58
59
      --  UART is a simple protocol, we use one port to send, assuming
      --  it can be used in full duplex mode.
60

61
62
63
      UART_Port   : Uart.HLInterface.Serial_Port;
      UART_Device : Uart.Core.UART_Device;
      UART_Config : Serial_Conf_T;
64
65
66
67
   end record;

   Nodes : array (Node_Type) of Node_Record;

68
   subtype AS_Message_Length_Stream is Uart.STreams.Stream_Element_Array
69
70
71
72
     (1 .. Message_Length_Size);
   subtype Message_Length_Stream is Stream_Element_Array
     (1 .. Message_Length_Size);

73
   subtype AS_Full_Stream is Uart.Streams.Stream_Element_Array (1 .. PDU_Size);
74
75
76
77
78
79
80
81
82
83
84
85
86
   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;
87
      Use_Asn1 : Boolean := False;
88
      Parity : UART.HLInterface.Parity_Check;
89
90
91
92
93
94

   begin
      Uart.HLInterface.Initialize (Success);
      if not Success then
         Put_Line (Normal,
                   "Initialization failure: cannot find UART cores");
95
         raise Program_Error;
96
97
98
      end if;

      for J in Name_Table'Range loop
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
	 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
	       Put_Line ("invalid device name");

	    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;
128
129
      end loop;

130
131
132
133
134
135
136
137
      Uart.HLInterface.Open (Port   => Nodes (My_Node).UART_Port,
			     Number => Nodes (My_Node).UART_Device);

      if not Use_Asn1 then
	 Uart.HLInterface.Set (Port   => Nodes (My_Node).UART_Port,
			       Rate => Uart.HLInterface.B19200,
			       Block => True);
      else
138
139
140
141
142
143
	 if Nodes (My_Node).UART_Config.Use_Paritybit then
	    Parity := To_GNAT_Parity_Check (Nodes (My_Node).UART_Config.Parity);
	 else
	    Parity := UART.HLInterface.None;
	 end if;

144
145
146
	 UART.HLInterface.Set
	   (Port   => Nodes (My_Node).UART_Port,
	    Rate   => To_GNAT_Baud_Rate (Nodes (My_Node).UART_Config.Speed),
147
	    Parity => Parity,
148
149
150
	    Bits   => To_GNAT_Bits (Integer (Nodes (My_Node).UART_Config.Bits)),
	    Block  => True);
      end if;
151
152
153
154
155
156
157
158
159
      pragma Debug (Put_Line (Normal, "Initialization of UART subsystem"
                                & " is complete"));
   end Initialize;

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

   procedure Receive is
160
      use type Uart.Streams.Stream_Element_Offset;
161
162
163

      SEL : AS_Message_Length_Stream;
      SEA : AS_Full_Stream;
164
165
166
      SEO : Uart.Streams.Stream_Element_Offset;
      Packet_Size : Uart.Streams.Stream_Element_Offset;
      Data_Received_Index : Uart.Streams.Stream_Element_Offset;
167
168
169
170
171
   begin

      Main_Loop : loop
         Put_Line ("Using user-provided GRUART stack to receive");
         Put_Line ("Waiting on UART #"
172
                     & Nodes (My_Node).UART_Device'Img);
173
174
175
176
177

         --  UART is a character-oriented protocol

         --  1/ Receive message length

178
         Uart.HLInterface.Read (Nodes (My_Node).UART_Port, SEL, SEO);
179

180
         Packet_Size := Uart.Streams.Stream_Element_Offset
181
           (To_Length (To_PO_HI_Message_Length_Stream (SEL)));
182
183
         SEO := Packet_Size;

184
185
186
187
         SEA (1 .. Message_Length_Size) := SEL;

         Data_Received_Index := Message_Length_Size + 1;

188
         while Data_Received_Index <= Packet_Size + Message_Length_Size loop
189
190
            --  We must loop to make sure we receive all data

191
            Uart.HLInterface.Read (Nodes (My_Node).UART_Port,
192
193
                                   SEA (Data_Received_Index .. SEO + 1),
                                   SEO);
194
            Data_Received_Index := 1 + SEO + 1;
195
196
197
198
199
200
201
202
         end loop;

         --  2/ Receive full message

         if SEO /= SEA'First - 1 then
            Put_Line
              (Normal,
               "UART #"
203
                 & Nodes (My_Node).UART_Device'Img
204
                 & " received"
205
                 & Uart.Streams.Stream_Element_Offset'Image (SEO)
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
                 & " bytes");

            --  Deliver to the peer handler

            PolyORB_HI_Generated.Transport.Deliver
              (Corresponding_Entity
                 (Integer_8 (SEA (Message_Length_Size + 1))),
               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.

237
238
      Msg : Uart.Streams.Stream_Element_Array
        (1 .. Uart.Streams.Stream_Element_Offset (Size));
239
240
241
242
243
244
      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 #"
245
                  & Nodes (Node).UART_Device'Img
246
247
                  & Size'Img & " bytes");

248
      Uart.HLInterface.Write (Port   => Nodes (My_Node).UART_Port,
249
250
251
                              Buffer => Msg);

      return Error_Kind'(Error_None);
252
      --  Note: we have no way to know there was an error here
253
254
   end Send;

255
end PolyORB_HI_Drivers_GRUART;