polyorb_hi_drivers_gruart.adb 6.19 KB
Newer Older
1
2
with Interfaces;
with Ada.Unchecked_Conversion;
3

4
with Uart.Core;
5
6
with Uart.HLInterface;
with Uart.Streams;
7
8
9
10
11
12
13
14
15

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.

16
package body PolyORB_HI_Drivers_GRUART is
17
18
19
20
21
22
23
24
25
26
27

   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
28
29
      --  UART is a simple protocol, we use one port to send, another
      --  to receive.
30
31
32
33
34
35
36
37
38
39
40

      UART_Port_Send   : Uart.HLInterface.Serial_Port;
      UART_Device_Send : Uart.Core.UART_Device;

      UART_Port_Receive   : Uart.HLInterface.Serial_Port;
      UART_Device_Receive : Uart.Core.UART_Device;

   end record;

   Nodes : array (Node_Type) of Node_Record;

41
   subtype AS_Message_Length_Stream is Uart.STreams.Stream_Element_Array
42
43
44
45
     (1 .. Message_Length_Size);
   subtype Message_Length_Stream is Stream_Element_Array
     (1 .. Message_Length_Size);

46
   subtype AS_Full_Stream is Uart.Streams.Stream_Element_Array (1 .. PDU_Size);
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
   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;

   begin
      Uart.HLInterface.Initialize (Success);
      if not Success then
         Put_Line (Normal,
                   "Initialization failure: cannot find UART cores");
66
         raise Program_Error;
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
      end if;

      for J in Name_Table'Range loop
         Nodes (J).UART_Device_Send
           := Uart.Core.UART_Device'Value
           (To_String (Name_Table (J).Location) (1 .. 1));

         Nodes (J).UART_Device_Receive
           := Uart.Core.UART_Device'Value
           (To_String (Name_Table (J).Location) (3 .. 3));
      end loop;

      Uart.HLInterface.Open (Port   => Nodes (My_Node).UART_Port_Send,
                             Number => Nodes (My_Node).UART_Device_Send);

      Uart.HLInterface.Open (Port   => Nodes (My_Node).UART_Port_Receive,
                             Number => Nodes (My_Node).UART_Device_Receive);

      Uart.HLInterface.Set (Port   => Nodes (My_Node).UART_Port_Send,
                            Rate => Uart.HLInterface.B19200);

      Uart.HLInterface.Set (Port   => Nodes (My_Node).UART_Port_Receive,
                            Rate => Uart.HLInterface.B19200,
                            Block => True);

      pragma Debug (Put_Line (Normal, "Initialization of UART subsystem"
                                & " is complete"));
   end Initialize;

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

   procedure Receive is
101
      use type Uart.Streams.Stream_Element_Offset;
102
103
104

      SEL : AS_Message_Length_Stream;
      SEA : AS_Full_Stream;
105
106
107
      SEO : Uart.Streams.Stream_Element_Offset;
      Packet_Size : Uart.Streams.Stream_Element_Offset;
      Data_Received_Index : Uart.Streams.Stream_Element_Offset;
108
109
110
111
112
113
114
115
116
117
118
119
120
   begin

      Main_Loop : loop
         Put_Line ("Using user-provided GRUART stack to receive");
         Put_Line ("Waiting on UART #"
                     & Nodes (My_Node).UART_Device_Receive'Img);

         --  UART is a character-oriented protocol

         --  1/ Receive message length

         Uart.HLInterface.Read (Nodes (My_Node).UART_Port_Receive, SEL, SEO);

121
         Packet_Size := Uart.Streams.Stream_Element_Offset
122
           (To_Length (To_PO_HI_Message_Length_Stream (SEL)));
123
124
         SEO := Packet_Size;

125
126
127
128
         SEA (1 .. Message_Length_Size) := SEL;

         Data_Received_Index := Message_Length_Size + 1;

129
         while Data_Received_Index <= Packet_Size + Message_Length_Size loop
130
131
132
133
134
            --  We must loop to make sure we receive all data

            Uart.HLInterface.Read (Nodes (My_Node).UART_Port_Receive,
                                   SEA (Data_Received_Index .. SEO + 1),
                                   SEO);
135
            Data_Received_Index := 1 + SEO + 1;
136
137
138
139
140
141
142
143
144
145
         end loop;

         --  2/ Receive full message

         if SEO /= SEA'First - 1 then
            Put_Line
              (Normal,
               "UART #"
                 & Nodes (My_Node).UART_Device_Receive'Img
                 & " received"
146
                 & Uart.Streams.Stream_Element_Offset'Image (SEO)
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
                 & " 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.

178
179
      Msg : Uart.Streams.Stream_Element_Array
        (1 .. Uart.Streams.Stream_Element_Offset (Size));
180
181
182
183
184
185
186
187
188
189
190
191
192
      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 #"
                  & Nodes (Node).UART_Device_Send'Img
                  & Size'Img & " bytes");

      Uart.HLInterface.Write (Port   => Nodes (My_Node).UART_Port_Send,
                              Buffer => Msg);

      return Error_Kind'(Error_None);
193
      --  Note: we have no way to know there was an error here
194
195
   end Send;

196
end PolyORB_HI_Drivers_GRUART;