KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
uartbridge.c
1 /*
2  * KM3NeT CLB v2 Firmware
3  * ----------------------
4  *
5  * Copyright 2013 National Institute for Subatomic Physics Nikhef
6  *
7  * All Rights Reserved.
8  *
9  *
10  * File : udp4serial.c
11  * Created : 19 jun. 2013
12  * Author : Vincent van Beveren
13  */
14 
15 #include "net/uartbridge.h"
16 
17 #include "drv/wb/suart.h"
18 #include "kernel/timer.h"
19 
20 #define UDP_ESCAPE 0xF5
21 #define EMBED_UDP 0x01
22 #define BUF_SIZE 1024
23 
24 static int _inChar = -1;
25 
26 static bool _rxMode = false;
27 static bool _txMode = false;
28 
29 static uint8_t _ubRxBuf[BUF_SIZE];
30 static int _ubRxPos = 0;
31 static int _ubRxLen = 0;
32 static int _ubRxTyp = 0;
33 
34 static uint8_t _ubTxBuf[BUF_SIZE];
35 static int _ubTxPos = 0;
36 static int _ubTxLen = 0;
37 
38 static uint8_t _check;
39 
40 
41 #ifdef UARTBRIDGE
42 int _stdIn()
43 {
44  if (_inChar == -1) return -1;
45  int tmp = _inChar;
46  _inChar = -1;
47  return tmp;
48 }
49 #endif
50 
51 
52 static void add2Check(uint8_t v)
53 {
54 
55  _check ^= v;
56  _check = 0xFF & ( ( _check << 1 ) | ( _check >> 7 ) );
57 
58 }
59 
60 static int readChar(bool wait, bool a2c)
61 {
62  uint8_t c;
63  if (wait) while (!suartRxReady(CONSOLE_UART)) { };
64  if (!suartRx(CONSOLE_UART,(char*)&c)) return -1;
65  if (a2c) {
66  add2Check(c);
67  }
68 
69  return c;
70 }
71 
72 static int writeChar(bool wait, uint8_t c)
73 {
74  if (wait) while (!csuartTxReady(CONSOLE_UART)) { };
75  if (!suartTx(CONSOLE_UART,c)) return -1;
76  return c;
77 }
78 
79 
80 static void ubInterpretRx()
81 {
82  int i;
83  SockAddr addr;
84 
85  for (i = 0; i < _ubRxLen - 1; ++i)
86  {
87  add2Check(_ubRxBuf[i]);
88  }
89  if (_check != _ubRxBuf[_ubRxLen - 1]) {
90  _ubRxLen = 0;
91  printf("[CE!]");
92  return;
93  }
94 
95  switch (_ubRxTyp) {
96  case EMBED_UDP:
97 
98  addr.ip = ((0xFF & _ubRxBuf[0]) << 24) | ((0xFF & _ubRxBuf[1]) << 16) |
99  ((0xFF & _ubRxBuf[2]) << 8) | (0xFF & _ubRxBuf[3]);
100  addr.port = ((0xFF & _ubRxBuf[4]) << 8) | (0xFF & _ubRxBuf[5]);
101  _ubRx(&addr, &_ubRxBuf[6], _ubRxLen - 6);
102  break;
103  default:
104  _ubRxLen = 0;
105  break;
106  }
107 }
108 
109 
110 
111 void ubProcess()
112 {
113  char c;
114 
115  while (_txMode && suartTxReady(CONSOLE_UART))
116  {
117  suartTx(_ubTxBuf[_ubTxPos++]);
118  if (_ubTxPos >= _ubTxLen) _txMode = false;
119  }
120 
121  while (suartRxReady(CONSOLE_UART))
122  {
123  if (suartRx(CONSOLE_UART,&c))
124  {
125 
126  if (_rxMode)
127  {
128  _ubRxBuf[_ubRxPos++] = c;
129  if (_ubRxPos >= _ubRxLen ) {
130  ubInterpretRx();
131  _rxMode = false;
132  }
133  } else
134  {
135  if (c == UDP_ESCAPE) {
136  _check = 0;
137 
138  _ubRxTyp = readChar(true, true);
139  _ubRxLen = ( ( readChar(true, true) << 8 ) | readChar(true, true) ) + 1;
140  if (_ubRxLen > BUF_SIZE) {
141  puts("INVALID LENGTH");
142  } else {
143  _ubRxPos = 0;
144  _rxMode = true;
145  }
146  }
147  else {
148  _inChar = c;
149  }
150  }
151  }
152 
153  }
154 }
155 
156 bool ubTx(SockAddr * addr, uint8_t * data, int len)
157 {
158  if (_txMode) return false;
159 
160  _ubTxBuf[0] = 0xFF & ( addr->ip >> 24 );
161  _ubTxBuf[1] = 0xFF & ( addr->ip >> 16 );
162  _ubTxBuf[2] = 0xFF & ( addr->ip >> 8 );
163  _ubTxBuf[3] = 0xFF & ( addr->ip >> 0 );
164  _ubTxBuf[4] = 0xFF & ( addr->port >> 8 );
165  _ubTxBuf[5] = 0xFF & ( addr->port >> 0 );
166  memcpy(&_ubTxBuf[6], data, len);
167  _ubTxLen = len + 6;
168  _ubTxPos = 0;
169  _txMode = true;
170  writeChar(true, UDP_ESCAPE);
171  writeChar(true, EMBED_UDP);
172  writeChar(true, _ubTxLen >> 8);
173  writeChar(true, _ubTxLen);
174  return true;
175 }
176 
177 
uint16_t port
Port number.
Definition: net.h:34
static bool suartTxReady(SUART_Descriptor *desc)
Returns whether or not the TX buffer is empty.
Definition: suart.h:80
White Rabbit Simple UART Driver.
uint32_t ip
IP address.
Definition: net.h:33
Combination of IP address and port.
Definition: net.h:31
bool suartRx(SUART_Descriptor *desc, char *c)
Receives a character.
Definition: suart.c:73
static bool suartRxReady(SUART_Descriptor *desc)
Returns whether or not the RX buffer has data.
Definition: suart.h:104
bool suartTx(SUART_Descriptor *desc, char c)
Transmits a character.
Definition: suart.c:58