Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 
23 /* W5100 ethernet chip I/O */
24 
25 #ifndef W5100_TELEM_H
26 #define W5100_TELEM_H
27 
29 #include "generated/airframe.h"
30 
32 
33 #define W5100_RX_BUFFER_SIZE 80
34 #define W5100_TX_BUFFER_SIZE 80
35 #define W5100_BUFFER_NUM 2
36 #define STX 0x99
37 
42 };
43 
44 struct w5100_periph {
45  volatile enum W5100Status status;
46  int curbuf;
47  /* Receive buffer */
51  /* Transmit buffer */
55  volatile uint8_t work_tx[4];
56  volatile uint8_t work_rx[4];
58 };
59 
61 
62 extern struct w5100_periph chip0;
63 //extern uint8_t ck_a, ck_b;
64 
65 void w5100_init( void );
66 
67 void w5100_transmit( uint8_t data );
69 void w5100_send( void );
71 bool_t w5100_ch_available( void );
72 
73 // Defines that are done in mcu_periph on behalf of uart.
74 // We need to do these here...
75 #define W5100Init() w5100_init()
76 #define W5100CheckFreeSpace(_x) (TRUE) // w5100_check_free_space(_x)
77 #define W5100Transmit(_x) w5100_transmit(_x)
78 #define W5100SendMessage() w5100_send()
79 #define W5100ChAvailable() w5100_ch_available()
80 #define W5100Getch() w5100_getch()
81 #define W5100TxRunning chip0.tx_running
82 #define W5100SetBaudrate(_b) w5100_set_baudrate(_b)
83 
84 #define W5100TransportSizeOf(_dev, _x) (_x+4)
85 #define W5100TransportCheckFreeSpace(_dev, x) TransportLink(_dev, CheckFreeSpace(x))
86 #define W5100TransportPut1Byte(_dev, x) TransportLink(_dev, Transmit(x))
87 #define W5100TransportSendMessage(_dev) TransportLink(_dev, SendMessage())
88 
89 #define W5100TransportPutUint8(_dev, _byte) { \
90  ck_a += _byte; \
91  ck_b += ck_a; \
92  W5100TransportPut1Byte(_dev, _byte); \
93  }
94 
95 #define W5100TransportPut1ByteByAddr(_dev, _byte) { \
96  uint8_t _x = *(_byte); \
97  W5100TransportPutUint8(_dev, _x); \
98  }
99 
100 #define W5100TransportPut2Bytes(_dev, _x) { \
101  uint16_t x16 = _x; \
102  W5100TransportPut1Byte(_dev, x16>>8); \
103  W5100TransportPut1Byte(_dev, x16 & 0xff); \
104  }
105 
106 #define W5100TransportPut2ByteByAddr(_dev, _byte) { \
107  W5100TransportPut1ByteByAddr(_dev, _byte); \
108  W5100TransportPut1ByteByAddr(_dev, (const uint8_t*)_byte+1); \
109  }
110 
111 #define W5100TransportPut4ByteByAddr(_dev, _byte) { \
112  W5100TransportPut2ByteByAddr(_dev, _byte); \
113  W5100TransportPut2ByteByAddr(_dev, (const uint8_t*)_byte+2); \
114  }
115 
116 #ifdef __IEEE_BIG_ENDIAN // From machine/ieeefp.h
117 #define W5100TransportPutDoubleByAddr(_dev, _byte) { \
118  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
119  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
120  }
121 #else
122 #define W5100TransportPutDoubleByAddr(_dev, _byte) { \
123  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte); \
124  W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_byte+4); \
125  }
126 #endif
127 
128 
129 #define W5100TransportPutInt8ByAddr(_dev, _x) W5100TransportPut1ByteByAddr(_dev, _x)
130 #define W5100TransportPutUint8ByAddr(_dev, _x) W5100TransportPut1ByteByAddr(_dev, (const uint8_t*)_x)
131 #define W5100TransportPutInt16ByAddr(_dev, _x) W5100TransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
132 #define W5100TransportPutUint16ByAddr(_dev, _x) W5100TransportPut2ByteByAddr(_dev, (const uint8_t*)_x)
133 #define W5100TransportPutInt32ByAddr(_dev, _x) W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
134 #define W5100TransportPutUint32ByAddr(_dev, _x) W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
135 #define W5100TransportPutFloatByAddr(_dev, _x) W5100TransportPut4ByteByAddr(_dev, (const uint8_t*)_x)
136 #define W5100TransportPutNamedUint8(_dev, _name, _byte) W5100TransportPutUint8(_dev, _byte)
137 
138 #define W5100TransportPutArray(_dev, _put, _n, _x) { \
139 uint8_t _i; \
140 W5100TransportPutUint8(_dev, _n); \
141 for(_i = 0; _i < _n; _i++) { \
142 _put(_dev, &_x[_i]); \
143 } \
144 }
145 
146 #define W5100TransportPutInt16Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutInt16ByAddr, _n, _x)
147 
148 #define W5100TransportPutUint16Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutUint16ByAddr, _n, _x)
149 #define W5100TransportPutUint8Array(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutUint8ByAddr, _n, _x)
150 #define W5100TransportPutFloatArray(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutFloatByAddr, _n, _x)
151 #define W5100TransportPutDoubleArray(_dev, _n, _x) W5100TransportPutArray(_dev, W5100TransportPutDoubleByAddr, _n, _x)
152 
153 
154 #define W5100TransportHeader(_dev, payload_len) { \
155 W5100TransportPut1Byte(_dev, STX); \
156 uint8_t msg_len = W5100TransportSizeOf(_dev, payload_len); \
157 W5100TransportPut1Byte(_dev, msg_len); \
158 ck_a = msg_len; ck_b = msg_len; \
159 }
160 
161 #define W5100TransportTrailer(_dev) { \
162 W5100TransportPut1Byte(_dev, ck_a); \
163 W5100TransportPut1Byte(_dev, ck_b); \
164 W5100TransportSendMessage(_dev); \
165 }
166 
167 
170 // PPRZ parsing state machine
171 #define UNINIT 0
172 #define GOT_STX 1
173 #define GOT_LENGTH 2
174 #define GOT_PAYLOAD 3
175 #define GOT_CRC1 4
176 
178  // generic interface
179  struct transport trans;
180  // specific pprz transport variables
184 };
185 
186 extern struct w5100_transport w5100_tp;
187 
188 static inline void parse_w5100(struct w5100_transport * t, uint8_t c ) {
189  switch (t->status) {
190  case UNINIT:
191  if (c == STX)
192  t->status++;
193  break;
194  case GOT_STX:
195  if (t->trans.msg_received) {
196  t->trans.ovrn++;
197  goto error;
198  }
199  t->trans.payload_len = c-4; /* Counting STX, LENGTH and CRC1 and CRC2 */
200  t->ck_a = t->ck_b = c;
201  t->status++;
202  t->payload_idx = 0;
203  break;
204  case GOT_LENGTH:
205  t->trans.payload[t->payload_idx] = c;
206  t->ck_a += c; t->ck_b += t->ck_a;
207  t->payload_idx++;
208  if (t->payload_idx == t->trans.payload_len)
209  t->status++;
210  break;
211  case GOT_PAYLOAD:
212  if (c != t->ck_a)
213  goto error;
214  t->status++;
215  break;
216  case GOT_CRC1:
217  if (c != t->ck_b)
218  goto error;
219  t->trans.msg_received = TRUE;
220  goto restart;
221  default:
222  goto error;
223  }
224  return;
225  error:
226  t->trans.error++;
227  restart:
228  t->status = UNINIT;
229  return;
230 }
231 
232 static inline void w5100_parse_payload(struct w5100_transport * t) {
233  uint8_t i;
234  for(i = 0; i < t->trans.payload_len; i++)
235  dl_buffer[i] = t->trans.payload[i];
237 }
238 
239 static inline void w5100_read_buffer( struct w5100_transport *t ) {
240  while ( w5100_ch_available() ) {
242  int c = 0;
243  do {
244  parse_w5100( t, rx_buf[ c++ ] );
245  } while ( ( t->status != UNINIT ) && !(t->trans.msg_received) );
246  }
247 }
248 
249 #define W5100Buffer(_dev) TransportLink(_dev,ChAvailable())
250 
251 #define W5100CheckAndParse(_dev,_trans) { \
252  if (W5100Buffer(_dev)) { \
253  w5100_read_buffer( &(_trans) ); \
254  if (_trans.trans.msg_received) { \
255  w5100_parse_payload(&(_trans)); \
256  _trans.trans.msg_received = FALSE; \
257  } \
258  } \
259  }
260 
261 
262 #endif /* W5100_TELEM_H */
263 
W5100Status
Definition: w5100.h:38
unsigned short uint16_t
Definition: types.h:16
volatile uint16_t tx_extract_idx[W5100_BUFFER_NUM]
Definition: w5100.h:54
static void w5100_read_buffer(struct w5100_transport *t)
Definition: w5100.h:239
bool_t w5100_ch_available(void)
Definition: w5100.c:324
#define W5100_TX_BUFFER_SIZE
Definition: w5100.h:34
void w5100_send(void)
Definition: w5100.c:238
volatile uint16_t rx_insert_idx[W5100_BUFFER_NUM]
Definition: w5100.h:49
generic transport header
volatile uint8_t tx_buf[W5100_BUFFER_NUM][W5100_TX_BUFFER_SIZE]
Definition: w5100.h:52
volatile uint8_t work_rx[4]
Definition: w5100.h:56
Generic transport header.
Definition: transport.h:39
struct w5100_transport w5100_tp
Definition: w5100.c:95
#define W5100_RX_BUFFER_SIZE
Definition: w5100.h:33
uint8_t status
Definition: w5100.h:181
#define W5100_BUFFER_NUM
Definition: w5100.h:35
uint16_t w5100_rx_size(uint8_t _s)
Definition: w5100.c:280
static void w5100_parse_payload(struct w5100_transport *t)
Definition: w5100.h:232
int curbuf
Definition: w5100.h:46
uint8_t payload_idx
Definition: w5100.h:182
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:48
volatile uint16_t tx_insert_idx[W5100_BUFFER_NUM]
Definition: w5100.h:53
volatile uint16_t rx_extract_idx[W5100_BUFFER_NUM]
Definition: w5100.h:50
void w5100_transmit(uint8_t data)
Definition: w5100.c:224
uint8_t ck_b
Definition: w5100.h:183
enum W5100Status status
Definition: w5100.h:45
#define GOT_PAYLOAD
Definition: w5100.h:174
uint16_t w5100_receive(uint8_t *buf, uint16_t len)
Definition: w5100.c:331
uint8_t tx_running
Definition: w5100.h:57
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:578
#define GOT_CRC1
Definition: w5100.h:175
unsigned char uint8_t
Definition: types.h:14
struct w5100_periph chip0
Definition: w5100.c:75
#define GOT_LENGTH
Definition: w5100.h:173
uint8_t ck_a
Definition: w5100.h:183
volatile bool_t msg_received
Definition: transport.h:45
#define STX
Definition: w5100.h:36
void w5100_init(void)
Definition: w5100.c:155
volatile uint8_t work_tx[4]
Definition: w5100.h:55
uint8_t dl_buffer[MSG_SIZE]
Definition: main_demo5.c:59
struct transport trans
Definition: w5100.h:179
#define GOT_STX
Definition: w5100.h:172
uint8_t ovrn
Definition: transport.h:47
static void parse_w5100(struct w5100_transport *t, uint8_t c)
Definition: w5100.h:188
uint8_t rx_buf[W5100_RX_BUFFER_SIZE]
Definition: w5100.c:77
uint8_t error
Definition: transport.h:47
static struct point c
Definition: discsurvey.c:39
#define UNINIT
Receiving pprz messages.
Definition: w5100.h:171