Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
w5100.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 Gerard Toonstra
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  *
21  */
22 
28 #ifndef W5100_TELEM_H
29 #define W5100_TELEM_H
30 
32 #include "generated/airframe.h"
33 
35 
36 #define W5100_RX_BUFFER_SIZE 80
37 #define W5100_TX_BUFFER_SIZE 80
38 #define W5100_BUFFER_NUM 2
39 #define STX 0x99
40 
45 };
46 
47 struct w5100_periph {
48  volatile enum W5100Status status;
49  int curbuf;
50  /* Receive buffer */
54  /* Transmit buffer */
58  volatile uint8_t work_tx[4];
59  volatile uint8_t work_rx[4];
61 };
62 
64 
65 extern struct w5100_periph chip0;
66 //extern uint8_t ck_a, ck_b;
67 
68 void w5100_init( void );
69 
70 void w5100_transmit( uint8_t data );
72 void w5100_send( void );
74 bool_t w5100_ch_available( void );
75 
76 // Defines that are done in mcu_periph on behalf of uart.
77 // We need to do these here...
78 #define W5100Init() w5100_init()
79 #define W5100CheckFreeSpace(_x) (TRUE) // w5100_check_free_space(_x)
80 #define W5100Transmit(_x) w5100_transmit(_x)
81 #define W5100SendMessage() w5100_send()
82 #define W5100ChAvailable() w5100_ch_available()
83 #define W5100Getch() w5100_getch()
84 #define W5100TxRunning chip0.tx_running
85 #define W5100SetBaudrate(_b) w5100_set_baudrate(_b)
86 
87 #define W5100TransportSizeOf(_dev, _x) (_x+4)
88 #define W5100TransportCheckFreeSpace(_dev, x) TransportLink(_dev, CheckFreeSpace(x))
89 #define W5100TransportPut1Byte(_dev, x) TransportLink(_dev, Transmit(x))
90 #define W5100TransportSendMessage(_dev) TransportLink(_dev, SendMessage())
91 
92 #define W5100TransportPutUint8(_dev, _byte) { \
93  ck_a += _byte; \
94  ck_b += ck_a; \
95  W5100TransportPut1Byte(_dev, _byte); \
96  }
97 
98 #define W5100TransportPut1ByteByAddr(_dev, _byte) { \
99  uint8_t _x = *(_byte); \
100  W5100TransportPutUint8(_dev, _x); \
101  }
102 
103 #define W5100TransportPut2Bytes(_dev, _x) { \
104  uint16_t x16 = _x; \
105  W5100TransportPut1Byte(_dev, x16>>8); \
106  W5100TransportPut1Byte(_dev, x16 & 0xff); \
107  }
108 
109 #define W5100TransportPut2ByteByAddr(_dev, _byte) { \
110  W5100TransportPut1ByteByAddr(_dev, _byte); \
111  W5100TransportPut1ByteByAddr(_dev, (const uint8_t*)_byte+1); \
112  }
113 
114 #define W5100TransportPut4ByteByAddr(_dev, _byte) { \
115  W5100TransportPut2ByteByAddr(_dev, _byte); \
116  W5100TransportPut2ByteByAddr(_dev, (const uint8_t*)_byte+2); \
117  }
118 
119 #ifdef __IEEE_BIG_ENDIAN // From machine/ieeefp.h
120 #define W5100TransportPutDoubleByAddr(_dev, _byte) { \
121  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
122  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
123  }
124 #define W5100TransportPutUint64ByAddr(_dev, _byte) { \
125  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
126  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
127  }
128 #define W5100TransportPutInt64ByAddr(_dev, _byte) { \
129  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
130  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
131  }
132 #else
133 #define W5100TransportPutDoubleByAddr(_dev, _byte) { \
134  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
135  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
136  }
137 #define W5100TransportPutUint64ByAddr(_dev, _byte) { \
138  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
139  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
140  }
141 #define W5100TransportPutInt64ByAddr(_dev, _byte) { \
142  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
143  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
144  }
145 #endif
146 
147 
148 #define W5100TransportPutInt8ByAddr(_dev, _x) W5100TransportPut1ByteByAddr(_dev, _x)
149 #define W5100TransportPutUint8ByAddr(_dev, _x) W5100TransportPut1ByteByAddr(_dev, (const uint8_t*)_x)
150 #define W5100TransportPutInt16ByAddr(_dev, _x) W5100TransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
151 #define W5100TransportPutUint16ByAddr(_dev, _x) W5100TransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
152 #define W5100TransportPutInt32ByAddr(_dev, _x) W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
153 #define W5100TransportPutUint32ByAddr(_dev, _x) W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
154 #define W5100TransportPutFloatByAddr(_dev, _x) W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
155 #define W5100TransportPutNamedUint8(_dev, _name, _byte) W5100TransportPutUint8(_dev, _byte)
156 
157 #define W5100TransportPutArray(_dev, _put, _n, _x) { \
158  uint8_t _i; \
159  W5100TransportPutUint8(_dev, _n); \
160  for(_i = 0; _i < _n; _i++) { \
161  _put(_dev, &_x[_i]); \
162  } \
163  }
164 
165 #define W5100TransportPutInt8Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutInt8ByAddr, _n, _x)
166 #define W5100TransportPutUint8Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutUint8ByAddr, _n, _x)
167 
168 #define W5100TransportPutCharArray(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutCharByAddr, _n, _x)
169 
170 #define W5100TransportPutInt16Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutInt16ByAddr, _n, _x)
171 #define W5100TransportPutUint16Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutUint16ByAddr, _n, _x)
172 
173 #define W5100TransportPutInt32Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutInt32ByAddr, _n, _x)
174 #define W5100TransportPutUint32Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutUint32ByAddr, _n, _x)
175 
176 #define W5100TransportPutFloatArray(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutFloatByAddr, _n, _x)
177 
178 #define W5100TransportPutInt64Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutInt64ByAddr, _n, _x)
179 #define W5100TransportPutUint64Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutUint64ByAddr, _n, _x)
180 
181 #define W5100TransportPutDoubleArray(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutDoubleByAddr, _n, _x)
182 
183 
184 #define W5100TransportPutFixedArray(_dev, _put, _n, _x) { \
185  uint8_t _i; \
186  for(_i = 0; _i < _n; _i++) { \
187  _put(_dev, &_x[_i]); \
188  } \
189  }
190 
191 #define W5100TransportPutInt8FixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutInt8ByAddr, _n, _x)
192 #define W5100TransportPutUint8FixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutUint8ByAddr, _n, _x)
193 
194 #define W5100TransportPutCharFixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutCharByAddr, _n, _x)
195 
196 #define W5100TransportPutInt16FixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutInt16ByAddr, _n, _x)
197 #define W5100TransportPutUint16FixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutUint16ByAddr, _n, _x)
198 
199 #define W5100TransportPutInt32FixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutInt32ByAddr, _n, _x)
200 #define W5100TransportPutUint32FixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutUint32ByAddr, _n, _x)
201 
202 #define W5100TransportPutFloatFixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutFloatByAddr, _n, _x)
203 
204 #define W5100TransportPutInt64FixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutInt64ByAddr, _n, _x)
205 #define W5100TransportPutUint64FixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutUint64ByAddr, _n, _x)
206 
207 #define W5100TransportPutDoubleFixedArray(_dev, _n, _x) W5100TransportPutFixedArray(_dev, W5100TransportPutDoubleByAddr, _n, _x)
208 
209 
210 #define W5100TransportHeader(_dev, payload_len) { \
211  W5100TransportPut1Byte(_dev, STX); \
212  uint8_t msg_len = W5100TransportSizeOf(_dev, payload_len); \
213  W5100TransportPut1Byte(_dev, msg_len); \
214  ck_a = msg_len; ck_b = msg_len; \
215  }
216 
217 #define W5100TransportTrailer(_dev) { \
218  W5100TransportPut1Byte(_dev, ck_a); \
219  W5100TransportPut1Byte(_dev, ck_b); \
220  W5100TransportSendMessage(_dev); \
221  }
222 
223 
226 // PPRZ parsing state machine
227 #define UNINIT 0
228 #define GOT_STX 1
229 #define GOT_LENGTH 2
230 #define GOT_PAYLOAD 3
231 #define GOT_CRC1 4
232 
234  // generic interface
235  struct transport trans;
236  // specific pprz transport variables
240 };
241 
242 extern struct w5100_transport w5100_tp;
243 
244 static inline void parse_w5100(struct w5100_transport * t, uint8_t c ) {
245  switch (t->status) {
246  case UNINIT:
247  if (c == STX)
248  t->status++;
249  break;
250  case GOT_STX:
251  if (t->trans.msg_received) {
252  t->trans.ovrn++;
253  goto error;
254  }
255  t->trans.payload_len = c-4; /* Counting STX, LENGTH and CRC1 and CRC2 */
256  t->ck_a = t->ck_b = c;
257  t->status++;
258  t->payload_idx = 0;
259  break;
260  case GOT_LENGTH:
261  t->trans.payload[t->payload_idx] = c;
262  t->ck_a += c; t->ck_b += t->ck_a;
263  t->payload_idx++;
264  if (t->payload_idx == t->trans.payload_len)
265  t->status++;
266  break;
267  case GOT_PAYLOAD:
268  if (c != t->ck_a)
269  goto error;
270  t->status++;
271  break;
272  case GOT_CRC1:
273  if (c != t->ck_b)
274  goto error;
275  t->trans.msg_received = TRUE;
276  goto restart;
277  default:
278  goto error;
279  }
280  return;
281  error:
282  t->trans.error++;
283  restart:
284  t->status = UNINIT;
285  return;
286 }
287 
288 static inline void w5100_parse_payload(struct w5100_transport * t) {
289  uint8_t i;
290  for(i = 0; i < t->trans.payload_len; i++)
291  dl_buffer[i] = t->trans.payload[i];
293 }
294 
295 static inline void w5100_read_buffer( struct w5100_transport *t ) {
296  while ( w5100_ch_available() ) {
298  int c = 0;
299  do {
300  parse_w5100( t, w5100_rx_buf[ c++ ] );
301  } while ( ( t->status != UNINIT ) && !(t->trans.msg_received) );
302  }
303 }
304 
305 #define W5100Buffer(_dev) TransportLink(_dev,ChAvailable())
306 
307 #define W5100CheckAndParse(_dev,_trans) { \
308  if (W5100Buffer(_dev)) { \
309  w5100_read_buffer( &(_trans) ); \
310  if (_trans.trans.msg_received) { \
311  w5100_parse_payload(&(_trans)); \
312  _trans.trans.msg_received = FALSE; \
313  } \
314  } \
315  }
316 
317 
318 #endif /* W5100_TELEM_H */
319 
W5100Status
Definition: w5100.h:41
unsigned short uint16_t
Definition: types.h:16
volatile uint16_t tx_extract_idx[W5100_BUFFER_NUM]
Definition: w5100.h:57
static void w5100_read_buffer(struct w5100_transport *t)
Definition: w5100.h:295
bool_t w5100_ch_available(void)
Definition: w5100.c:337
#define W5100_TX_BUFFER_SIZE
Definition: w5100.h:37
void w5100_send(void)
Definition: w5100.c:251
volatile uint16_t rx_insert_idx[W5100_BUFFER_NUM]
Definition: w5100.h:52
generic transport header
volatile uint8_t tx_buf[W5100_BUFFER_NUM][W5100_TX_BUFFER_SIZE]
Definition: w5100.h:55
volatile uint8_t work_rx[4]
Definition: w5100.h:59
uint8_t w5100_rx_buf[W5100_RX_BUFFER_SIZE]
Definition: w5100.c:91
Generic transport header.
Definition: transport.h:39
struct w5100_transport w5100_tp
Definition: w5100.c:109
#define W5100_RX_BUFFER_SIZE
Definition: w5100.h:36
uint8_t status
Definition: w5100.h:237
#define W5100_BUFFER_NUM
Definition: w5100.h:38
uint16_t w5100_rx_size(uint8_t _s)
Definition: w5100.c:293
static void w5100_parse_payload(struct w5100_transport *t)
Definition: w5100.h:288
int curbuf
Definition: w5100.h:49
uint8_t payload_idx
Definition: w5100.h:238
bool_t dl_msg_available
Definition: main_demo5.c:56
volatile uint8_t rx_buf[W5100_BUFFER_NUM][W5100_RX_BUFFER_SIZE]
Definition: w5100.h:51
volatile uint16_t tx_insert_idx[W5100_BUFFER_NUM]
Definition: w5100.h:56
volatile uint16_t rx_extract_idx[W5100_BUFFER_NUM]
Definition: w5100.h:53
void w5100_transmit(uint8_t data)
Definition: w5100.c:237
uint8_t ck_b
Definition: w5100.h:239
enum W5100Status status
Definition: w5100.h:48
#define GOT_PAYLOAD
Definition: w5100.h:230
uint16_t w5100_receive(uint8_t *buf, uint16_t len)
Definition: w5100.c:344
uint8_t tx_running
Definition: w5100.h:60
volatile uint8_t payload_len
Definition: transport.h:43
uint8_t payload[TRANSPORT_PAYLOAD_LEN]
Definition: transport.h:41
#define TRUE
Definition: imu_chimu.h:144
struct adc_buf * buf
Definition: adc_arch.c:586
#define GOT_CRC1
Definition: w5100.h:231
unsigned char uint8_t
Definition: types.h:14
struct w5100_periph chip0
Definition: w5100.c:89
#define GOT_LENGTH
Definition: w5100.h:229
uint8_t ck_a
Definition: w5100.h:239
volatile bool_t msg_received
Definition: transport.h:45
#define STX
Definition: w5100.h:39
void w5100_init(void)
Definition: w5100.c:169
volatile uint8_t work_tx[4]
Definition: w5100.h:58
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:59
struct transport trans
Definition: w5100.h:235
#define GOT_STX
Definition: w5100.h:228
uint8_t ovrn
Definition: transport.h:47
static void parse_w5100(struct w5100_transport *t, uint8_t c)
Definition: w5100.h:244
uint8_t error
Definition: transport.h:47
#define UNINIT
Receiving pprz messages.
Definition: w5100.h:227