po_hi_driver_rasta_serial.c 7.94 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_returns.h>
22
#include <po_hi_utils.h>
23
#include <drivers/po_hi_rtems_utils.h>
24
#include <drivers/po_hi_driver_rasta_serial.h>
25
#include <drivers/po_hi_driver_rasta_common.h>
26

27
28
#include <drivers/po_hi_driver_serial_common.h>

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

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

41
#include <drivers/configuration/serial.h>
42

43
44
int po_hi_c_driver_rasta_serial_fd_read;
int po_hi_c_driver_rasta_serial_fd_write;
45

46
47
__po_hi_msg_t        __po_hi_c_driver_rasta_serial_msg;
__po_hi_request_t    __po_hi_c_driver_rasta_serial_request;
48

49
50
void __po_hi_c_driver_serial_rasta_poller (void)
{
51

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

julien.delange's avatar
julien.delange committed
56
   ts = __PO_HI_MESSAGES_MAX_SIZE;
57
58
   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
59
   while (ts > 0)
60
   {
61
62
      __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Poller waits for incoming message (%d bytes are required)!\n", ts);
      n = read (po_hi_c_driver_rasta_serial_fd_read, ptr, ts); 
julien.delange's avatar
julien.delange committed
63

64
      __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] read() %d returns %d!\n", po_hi_c_driver_rasta_serial_fd_read, n);
julien.delange's avatar
julien.delange committed
65
66
      if (n == -1)
      {
67
         __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Cannot read on socket !\n");
julien.delange's avatar
julien.delange committed
68
69
         return;
      }
70
71
72
73
74
      else
      {
         ptr += n;
         ts -= n;
      }
75
76
   }

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

84
   __po_hi_c_driver_rasta_serial_msg.length = __PO_HI_MESSAGES_MAX_SIZE;
85

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

89
   __po_hi_main_deliver (&__po_hi_c_driver_rasta_serial_request);
90
91
}

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

95
void __po_hi_c_driver_serial_rasta_init (__po_hi_device_id id)
96
{
97
98
   __po_hi_c_serial_conf_t* serialconf;

99
   __po_hi_c_driver_rasta_common_init ();
100

101
   serialconf = (__po_hi_c_serial_conf_t*)__po_hi_get_device_configuration (id);
102

103
   if (serialconf == NULL)
104
105
106
107
108
   {
      __PO_HI_DEBUG_INFO ("[RASTA SERIAL] Cannot get the name of the device !\n");
      return;
   }

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

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

115
116
117
118
119
   po_hi_c_driver_rasta_serial_fd_write = 
   po_hi_c_driver_rasta_serial_fd_read = open (serialconf->devname, O_RDWR);
   /*
   po_hi_c_driver_rasta_serial_fd_write = open (serialconf->devname, O_WRONLY);
   */
120

121
   if (po_hi_c_driver_rasta_serial_fd_read < 0)
122
   {
123
      __PO_HI_DEBUG_CRITICAL ("[RASTA SERIAL] Error while opening device %s for reading\n", serialconf->devname);
124
   }
125
126
127
128
129
130
131
   if (po_hi_c_driver_rasta_serial_fd_write < 0)
   {
      __PO_HI_DEBUG_CRITICAL ("[RASTA SERIAL] Error while opening device %s for writing\n", serialconf->devname);
   }

   __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Device opened for read (%s), fd=%d\n", serialconf->devname , po_hi_c_driver_rasta_serial_fd_read);
   __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Device opened for write (%s), fd=%d\n", serialconf->devname , po_hi_c_driver_rasta_serial_fd_write);
132

133
134
135
   switch (__po_hi_c_driver_serial_common_get_speed (id))
   {
      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_19200:
136
137
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_SET_BAUDRATE, 19200);
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_write, APBUART_SET_BAUDRATE, 19200);
138
139
140
         break;

      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_38400:
141
142
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_SET_BAUDRATE, 38400);
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_write, APBUART_SET_BAUDRATE, 38400);
143
144
145
         break;

      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_57600:
146
147
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_SET_BAUDRATE, 57600);
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_write, APBUART_SET_BAUDRATE, 57600);
148
149
150
         break;

      case __PO_HI_DRIVER_SERIAL_COMMON_SPEED_115200:
151
152
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_SET_BAUDRATE, 115200);
         __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_write, APBUART_SET_BAUDRATE, 115200);
153
154
155
156
157
158
159
         break;

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

160
   /*
161
  __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);
162
163
164
165
166
167
168
169
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_BLOCKING, APBUART_BLK_FLUSH | APBUART_BLK_RX);
  */
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_SET_BLOCKING, APBUART_BLK_FLUSH | APBUART_BLK_TX );
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_SET_TXFIFO_LEN, 256);  /* Transmitt buffer 64 chars */
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_SET_RXFIFO_LEN, 256); /* Receive buffer 256 chars */
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_SET_ASCII_MODE, 0); /* Make \n go \n\r or \r\n */
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_CLR_STATS, 0);
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd_read, APBUART_START, 0);
julien.delange's avatar
julien.delange committed
170

171
172
173
174
175
176
177

  if (tcflush (po_hi_c_driver_rasta_serial_fd_read, TCIOFLUSH) != 0)
  {
     __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Error when trying to flush read part\n");
  }

  if (tcflush (po_hi_c_driver_rasta_serial_fd_write, TCIOFLUSH) != 0)
julien.delange's avatar
julien.delange committed
178
  {
179
     __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Error when trying to flush write part\n");
julien.delange's avatar
julien.delange committed
180
  }
181
182

  __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Initialization complete !\n");
183
184
}

185
186
int __po_hi_c_driver_serial_rasta_sender (const __po_hi_task_id task_id, const __po_hi_port_t port)
{
187
   int n;
188
   int ts;
189
   __po_hi_local_port_t local_port;
190
191
192
193
194
195
196
197
   __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);

198
199
200
201
202
203
   if (request->port == -1)
   {
      __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Send output task %d, port %d (local_port=%d): no value to send\n", task_id, port, local_port);
      return __PO_HI_SUCCESS;
   }

204
205
206
207
208
   destination_port     = __po_hi_gqueue_get_destination (task_id, local_port, 0);

   __po_hi_msg_reallocate (&msg);

   request->port = destination_port;
209
   __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Destination port= %d\n", destination_port);
210
211
212

   __po_hi_marshall_request (request, &msg);

213
   __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] Message sent: 0x");
214
215
   for (ts = 0 ; ts < __PO_HI_MESSAGES_MAX_SIZE ; ts++)
   {
216
      __PO_HI_DEBUG_DEBUG ("%x", msg.content[ts]);
217
   }
218

219
   __PO_HI_DEBUG_DEBUG ("\n");
220

221
   n = write (po_hi_c_driver_rasta_serial_fd_write, &msg, __PO_HI_MESSAGES_MAX_SIZE);
222

223
   __PO_HI_DEBUG_DEBUG ("[RASTA SERIAL] write() returns %d\n", n);
224
225
226

   request->port = __PO_HI_GQUEUE_INVALID_PORT;

227
228
229
   return 1;
}

230
231
#endif