po_hi_driver_rasta_serial.c 6.16 KB
Newer Older
1
2
3
4
5
6
7
8
/*
 * This is a part of PolyORB-HI-C distribution, a minimal
 * middleware written for generated code from AADL models.
 * You should use it with the Ocarina toolsuite.
 *
 * Copyright (C) 2010, European Space Agency
 */

9
10
#include <deployment.h>
/* Generated code header */
11

12
#ifdef __PO_HI_NEED_DRIVER_SERIAL_RASTA
13

14
#include <activity.h>
15
16
#include <marshallers.h>

17
#include <po_hi_debug.h>
18
19
20
#include <po_hi_transport.h>
#include <po_hi_gqueue.h>
#include <po_hi_messages.h>
21
#include <po_hi_utils.h>
22
#include <drivers/po_hi_rtems_utils.h>
23
#include <drivers/po_hi_driver_rasta_serial.h>
24
#include <drivers/po_hi_driver_rasta_common.h>
25

26
27
#include <drivers/po_hi_driver_serial_common.h>

28
29
30
31
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
julien.delange's avatar
julien.delange committed
32
#include <termios.h>
33
34
/* POSIX-style files */

35
36
37
38
39
#include <pci.h>
#include <rasta.h>
#include <apbuart_rasta.h>
/* Rasta includes from GAISLER drivers */

40
#define __PO_HI_DRIVER_SERIAL_RASTA_BAUDRATE 19200
41
42
43

int po_hi_c_driver_rasta_serial_fd;

44
45
__po_hi_msg_t        __po_hi_c_driver_rasta_serial_msg;
__po_hi_request_t    __po_hi_c_driver_rasta_serial_request;
46

47
48
void __po_hi_c_driver_serial_rasta_poller (void)
{
49

50
   int n;
51
   int ts;
julien.delange's avatar
julien.delange committed
52
53
   int tr;
   uint8_t* ptr;
54

55
   __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Poller waits for incoming message !\n");
julien.delange's avatar
julien.delange committed
56
57
   ts = __PO_HI_MESSAGES_MAX_SIZE;
   tr = 0;
58
59
   ptr = &(__po_hi_c_driver_rasta_serial_msg.content[0]);
   __po_hi_msg_reallocate (&__po_hi_c_driver_rasta_serial_msg);
julien.delange's avatar
julien.delange committed
60
   while (ts > 0)
61
   {
julien.delange's avatar
julien.delange committed
62
63
64
65
66
      n = read (po_hi_c_driver_rasta_serial_fd, ptr, ts); 
      ptr += n;

      if (n == -1)
      {
67
         __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Cannot read on socket !\n");
julien.delange's avatar
julien.delange committed
68
69
70
71
         return;
      }
      ts -= n;
   tr += n;
72
73
   }

74
#ifdef __PO_HI_DEBUG_INFO
75
   __PO_HI_DEBUG_INFO ("[RASTA SERIAL] read() returns total %d, max message size=%d\n", tr, __PO_HI_MESSAGES_MAX_SIZE);
76

77
   __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Message received by poller: 0x");
78
   for (ts = 0 ; ts < __PO_HI_MESSAGES_MAX_SIZE ; ts++)
79
   {
80
      __PO_HI_DEBUG_INFO ("%x", __po_hi_c_driver_rasta_serial_msg.content[ts]);
81
   }
82
   __PO_HI_DEBUG_INFO ("\n");
83
#endif
84

85
   __po_hi_c_driver_rasta_serial_msg.length = tr;
86

87
88
   __po_hi_unmarshall_request (&__po_hi_c_driver_rasta_serial_request,
                               &__po_hi_c_driver_rasta_serial_msg);
89

90
   __po_hi_main_deliver (&__po_hi_c_driver_rasta_serial_request);
91
92
}

93
94
95
extern amba_confarea_type* __po_hi_driver_rasta_common_get_bus ();
void __po_hi_rasta_interrrupt_register(void *handler, int irqno, void *arg);

96
void __po_hi_c_driver_serial_rasta_init (__po_hi_device_id id)
97
{
98
   char  devname[16];
99
   __po_hi_c_driver_rasta_common_init ();
100

101
102
103
104
105
106
107
108
109
   memset (devname, '\0', 16);

   if (! __po_hi_c_driver_serial_common_get_dev (id, devname))
   {
      __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Cannot get the name of the device !\n");
      return;
   }


110
111
112
113
    /* provide the spacewire driver with AMBA Plug&Play
     * info so that it can find the GRSPW cores.
     */

114
   __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Initialization starts !\n");
115

116
   po_hi_c_driver_rasta_serial_fd = open (devname, O_RDWR);
117
118
119

   if (po_hi_c_driver_rasta_serial_fd < 0)
   {
120
      __PO_HI_DEBUG_CRITICAL ("[RASTA SERIAL] Error while opening device %s\n", devname);
121
   }
122
   __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Device open, fd=%d\n", po_hi_c_driver_rasta_serial_fd);
123

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
   switch (__po_hi_c_driver_serial_common_get_speed (id))
   {
      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_19200:
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_BAUDRATE, 19200);
         break;

      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_38400:
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_BAUDRATE, 38400);
         break;

      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_57600:
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_BAUDRATE, 57600);
         break;

      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_115200:
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_BAUDRATE, 115200);
         break;

      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_UNKNWON:
         __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Unknwon speed for the serial line\n");
         break;
   }

147
148
149
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_BLOCKING, APBUART_BLK_RX | APBUART_BLK_TX | APBUART_BLK_FLUSH);
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_TXFIFO_LEN, 64);  /* Transmitt buffer 64 chars */
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_RXFIFO_LEN, 256); /* Receive buffer 256 chars */
150
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_ASCII_MODE, 0); /* Make \n go \n\r or \r\n */
151
152
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_CLR_STATS, 0);
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_START, 0);
julien.delange's avatar
julien.delange committed
153
154
155

  if (tcflush (po_hi_c_driver_rasta_serial_fd, TCIOFLUSH) != 0)
  {
156
     __PO_HI_DEBUG_CRITICAL ("[RASTA SERIAL] Error when trying to flush\n");
julien.delange's avatar
julien.delange committed
157
  }
158
  __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Initialization complete !\n");
159
160
}

161
162
int __po_hi_c_driver_serial_rasta_sender (const __po_hi_task_id task_id, const __po_hi_port_t port)
{
163
   int n;
164
   int ts;
165
   __po_hi_local_port_t local_port;
166
167
168
169
170
171
172
173
174
175
176
177
178
   __po_hi_request_t* request;
   __po_hi_msg_t msg;
   __po_hi_port_t destination_port;

   local_port = __po_hi_get_local_port_from_global_port (port);

   request = __po_hi_gqueue_get_most_recent_value (task_id, local_port);

   destination_port     = __po_hi_gqueue_get_destination (task_id, local_port, 0);

   __po_hi_msg_reallocate (&msg);

   request->port = destination_port;
179
   __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Destination port= %d\n", destination_port);
180
181
182

   __po_hi_marshall_request (request, &msg);

183
   __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Message sent: 0x");
184
#ifdef __PO_HI_DEBUG_LEVEL_INFO
185
186
   for (ts = 0 ; ts < __PO_HI_MESSAGES_MAX_SIZE ; ts++)
   {
187
      __PO_HI_DEBUG_INFO ("%x", msg.content[ts]);
188
   }
189
#endif
190
191

   __PO_HI_DEBUG_INFO ("\n");
192

193
   n = write (po_hi_c_driver_rasta_serial_fd, &msg, __PO_HI_MESSAGES_MAX_SIZE);
194

195
   __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] write() returns %d\n", n);
196
197
198
   return 1;
}

199
200
#endif