po_hi_driver_rasta_serial.c 4.23 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
28
29

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

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

38
39
#define __PO_HI_DRIVER_SERIAL_RASTA_DEVICE "/dev/apburasta0"
#define __PO_HI_DRIVER_SERIAL_RASTA_BAUDRATE 19200
40
41
42

int po_hi_c_driver_rasta_serial_fd;

43
44
void __po_hi_c_driver_serial_rasta_poller (void)
{
45
46
47
   __po_hi_msg_t msg;
   __po_hi_request_t request;

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

53
   __DEBUGMSG ("[RASTA SERIAL] Hello, i'm the serial poller !\n");
julien.delange's avatar
julien.delange committed
54
55
56
57
   ts = __PO_HI_MESSAGES_MAX_SIZE;
   tr = 0;
   ptr = &msg.content;
   while (ts > 0)
58
   {
julien.delange's avatar
julien.delange committed
59
60
61
62
63
64
65
66
67
68
      n = read (po_hi_c_driver_rasta_serial_fd, ptr, ts); 
      ptr += n;

      if (n == -1)
      {
         __DEBUGMSG("[RASTA SERIAL] Cannot read on socket !\n");
         return;
      }
      ts -= n;
   tr += n;
69
70
   }

julien.delange's avatar
julien.delange committed
71
   __DEBUGMSG ("[RASTA SERIAL] read() returns total %d, max message size=%d\n", tr, __PO_HI_MESSAGES_MAX_SIZE);
72
73
74

   __DEBUGMSG ("[RASTA SERIAL] Message received by poller: 0x");
   for (ts = 0 ; ts < __PO_HI_MESSAGES_MAX_SIZE ; ts++)
75
   {
76
      __DEBUGMSG ("%x", msg.content[ts]);
77
   }
78
79
   __DEBUGMSG ("\n");

julien.delange's avatar
julien.delange committed
80
   msg.length = tr;
81
82
83

   __po_hi_unmarshall_request (&request, &msg);

84
   __DEBUGMSG ("[RASTA SERIAL] Destination port: %d\n", request.port);
85
   __po_hi_main_deliver (&request);
86
87
}

88
void __po_hi_c_driver_serial_rasta_init (__po_hi_device_id id)
89
{
90
91
   __po_hi_c_driver_rasta_common_init ();
   po_hi_c_driver_rasta_serial_fd = open (__PO_HI_DRIVER_SERIAL_RASTA_DEVICE, O_RDWR);
92
93
94

   if (po_hi_c_driver_rasta_serial_fd < 0)
   {
95
      __DEBUGMSG ("[RASTA SERIAL] Error while opening device %s\n", __PO_HI_DRIVER_SERIAL_RASTA_DEVICE);
96
97
   }

98
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_BAUDRATE, __PO_HI_DRIVER_SERIAL_RASTA_BAUDRATE); /* stream mode */
99
100
101
  __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 */
julien.delange's avatar
julien.delange committed
102
  __PO_HI_DRIVERS_RTEMS_UTILS_IOCTL(po_hi_c_driver_rasta_serial_fd, APBUART_SET_ASCII_MODE, 1); /* Make \n go \n\r or \r\n */
103
104
  __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
105
106
107
108
109

  if (tcflush (po_hi_c_driver_rasta_serial_fd, TCIOFLUSH) != 0)
  {
     __DEBUGMSG("[RASTA SERIAL] Error when trying to flush\n");
  }
110
111
}

112
113
int __po_hi_c_driver_serial_rasta_sender (const __po_hi_task_id task_id, const __po_hi_port_t port)
{
114
   int n;
115
   int ts;
116
   __po_hi_local_port_t local_port;
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
   __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;

   __po_hi_marshall_request (request, &msg);

133
134
135
136
137
138
139
   __DEBUGMSG  ("[RASTA SERIAL] Message sent: 0x");
   for (ts = 0 ; ts < __PO_HI_MESSAGES_MAX_SIZE ; ts++)
   {
      __DEBUGMSG ("%x", msg.content[ts]);
   }
   __DEBUGMSG ("\n");

140
   n = write (po_hi_c_driver_rasta_serial_fd, &msg, __PO_HI_MESSAGES_MAX_SIZE);
141

142
   __DEBUGMSG ("[RASTA SERIAL] write() returns %d\n", n);
143
144
145
   return 1;
}

146
147
#endif