Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
w5100.c
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 #include "mcu_periph/sys_time.h"
30 #include "mcu_periph/spi.h"
31 #include "mcu_periph/gpio.h"
32 
33 #define TXBUF_BASE 0x4000
34 #define RXBUF_BASE 0x6000
35 #define SOCKETS 4
36 
37 #define CMD_SOCKET 1
38 #define TELEM_SOCKET 0
39 
40 #define SOCK_OPEN 0x01
41 #define SOCK_CLOSE 0x10
42 #define SOCK_SEND 0x20
43 #define SOCK_RECV 0x40
44 #define SNMR_UDP 0x02
45 #define SNMR_MULTI 0x80
46 #define SNIR_SEND_OK 0x10
47 #define SNIR_TIMEOUT 0x08
48 #define CH_BASE 0x0400
49 #define CH_SIZE 0x0100
50 #define SMASK 0x07FF // Tx buffer MASK
51 #define RMASK 0x07FF // Tx buffer MASK
52 
53 #define REG_MR 0x0000
54 #define REG_RX_MEM 0x001A
55 #define REG_TX_MEM 0x001B
56 
57 #define REG_GAR 0x0001
58 #define REG_SUBR 0x0005
59 #define REG_SHAR 0x0009
60 #define REG_SIPR 0x000F
61 
62 #define SOCK_MR 0x0000
63 #define SOCK_CR 0x0001
64 #define SOCK_IR 0x0002
65 #define SOCK_PORT 0x0004
66 #define SOCK_DHAR 0x0006
67 #define SOCK_DIPR 0x000C
68 #define SOCK_DPORT 0x0010
69 #define SOCK_TX_WR 0x0024
70 #define SOCK_RSR 0x0026
71 #define SOCK_RXRD 0x0028
72 
73 #ifndef W5100_SPI_DEV
74 #define W5100_SPI_DEV spi1
75 #endif
76 
77 #ifndef W5100_SLAVE_IDX
78 #define W5100_SLAVE_IDX SPI_SLAVE1
79 #endif
80 
81 #ifndef W5100_DRDY_GPIO
82 #define W5100_DRDY_GPIO GPIOB
83 #endif
84 
85 #ifndef W5100_DRDY_GPIO_PIN
86 #define W5100_DRDY_GPIO_PIN GPIO1
87 #endif
88 
92 
93 // the media access control (ethernet hardware) address for the shield.
94 static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
95 
96 //the IP address for the shield:
97 static uint8_t ip[] = { W5100_IP };
98 static uint8_t dest[] = { W5100_MULTICAST_IP };
99 static uint8_t subnet[] = { W5100_SUBNET };
100 static uint16_t dport = W5100_MULTICAST_PORT;
101 
102 static const uint8_t RST = 7; // Reset BIT
103 
104 uint16_t SBASE[SOCKETS]; // Tx buffer base address
105 uint16_t RBASE[SOCKETS]; // Rx buffer base address
106 static const uint16_t SSIZE = 2048; // Max Tx buffer size
107 static const uint16_t RSIZE = 2048; // Max Rx buffer size
108 
110 
111 static void w5100_close_socket(uint8_t _s);
112 static void configure_socket(uint8_t _s, uint8_t _flag, uint16_t _lport, uint16_t _dport, uint8_t *_dest);
113 static void w5100_read_data(uint8_t s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len);
114 static uint16_t w5100_read(uint16_t _addr, uint8_t *_buf, uint16_t _len);
115 
116 static inline void w5100_set(uint16_t _reg, uint8_t _val)
117 {
118  w5100_spi.output_buf[0] = 0xF0;
119  w5100_spi.output_buf[1] = _reg >> 8;
120  w5100_spi.output_buf[2] = _reg & 0xFF;
121  w5100_spi.output_buf[3] = _val;
122 
124 
125  // FIXME: no busy waiting! if really needed add a timeout!!!!
126  while (w5100_spi.status != SPITransSuccess);
127 }
128 
129 static inline uint8_t w5100_get(uint16_t _reg)
130 {
131  w5100_spi.output_buf[0] = 0x0F;
132  w5100_spi.output_buf[1] = _reg >> 8;
133  w5100_spi.output_buf[2] = _reg & 0xFF;
134 
136 
137  // FIXME: no busy waiting! if really needed add a timeout!!!!
138  while (w5100_spi.status != SPITransSuccess);
139 
140  return w5100_spi.input_buf[3];
141 }
142 
143 static inline void w5100_set_buffer(uint16_t _reg, volatile uint8_t *_buf, uint16_t _len)
144 {
145  for (int i = 0; i < _len; i++) {
146  w5100_set(_reg, _buf[ i ]);
147  _reg++;
148  }
149 }
150 
151 static inline void w5100_sock_set(uint8_t _sock, uint16_t _reg, uint8_t _val)
152 {
153  w5100_set(CH_BASE + _sock * CH_SIZE + _reg, _val);
154 }
155 
156 static inline uint8_t w5100_sock_get(uint8_t _sock, uint16_t _reg)
157 {
158  return w5100_get(CH_BASE + _sock * CH_SIZE + _reg);
159 }
160 
161 static inline uint16_t w5100_sock_get16(uint8_t _sock, uint16_t _reg)
162 {
163  uint16_t res = w5100_sock_get(_sock, _reg);
164  uint16_t res2 = w5100_sock_get(_sock, _reg + 1);
165  res = res << 8;
166  res2 = res2 & 0xFF;
167  res = res | res2;
168  return res;
169 }
170 
171 // Functions for the generic device API
172 static int true_function(struct w5100_periph *p __attribute__((unused)), long *fd __attribute__((unused)), uint16_t len __attribute__((unused))) { return true; }
173 static void dev_transmit(struct w5100_periph *p __attribute__((unused)), long fd __attribute__((unused)), uint8_t byte) { w5100_transmit(byte); }
174 static void dev_transmit_buffer(struct w5100_periph *p __attribute__((unused)), long fd __attribute__((unused)), uint8_t *data, uint16_t len) { w5100_transmit_buffer(data, len); }
175 static void dev_send(struct w5100_periph *p __attribute__((unused)), long fd __attribute__((unused))) { w5100_send(); }
176 static int dev_char_available(struct w5100_periph *p __attribute__((unused))) { return w5100_ch_available; }
177 static uint8_t dev_getch(struct w5100_periph *p __attribute__((unused)))
178 {
179  uint8_t c = 0;
180  w5100_receive(&c, 1);
181  return c;
182 }
183 
184 void w5100_init(void)
185 {
186 
187  // configure the SPI bus.
197 
199  chip0.curbuf = 0;
202 
203  // wait one second for proper initialization (chip getting powered up).
204  sys_time_usleep(1000000);
205 
206  // set DRDY pin
209  sys_time_usleep(200);
211 
212  // allow some time for the chip to wake up.
213  sys_time_usleep(20000);
214 
215  // write reset bit into mode register
216  w5100_set(REG_MR, 1 << RST);
217 
218  // allow some time to wake up...
219  sys_time_usleep(20000);
220 
221  // receive memory size
222  w5100_set(REG_RX_MEM, 0x55);
223 
224  // transmit memory size
225  w5100_set(REG_TX_MEM, 0x55);
226 
227  // Setting the required socket base addresses for reads and writes to/from sockets
228  for (int i = 0; i < SOCKETS; i++) {
229  SBASE[i] = TXBUF_BASE + SSIZE * i;
230  RBASE[i] = RXBUF_BASE + RSIZE * i;
231  }
232 
233  uint8_t gateway[4];
234  gateway[0] = ip[0];
235  gateway[1] = ip[1];
236  gateway[2] = ip[2];
237  gateway[3] = 1;
238 
239  // configure gateway, subnet, mac and ip on "NIC".
240  w5100_set_buffer(REG_GAR, gateway, 4);
244 
245  // create a socket to send telemetry through.
247 
248  // make dest zero and configure socket to receive data
249  dest[ 0 ] = 0x00;
251 
252  // Configure generic device
253  chip0.device.periph = (void *)(&chip0);
254  chip0.device.check_free_space = (check_free_space_t) true_function;
255  chip0.device.put_byte = (put_byte_t) dev_transmit;
256  chip0.device.put_buffer = (put_buffer_t) dev_transmit_buffer;
257  chip0.device.send_message = (send_message_t) dev_send;
258  chip0.device.char_available = (char_available_t) dev_char_available;
259  chip0.device.get_byte = (get_byte_t) dev_getch;
260 }
261 
263 {
264 
266 
267  if (temp == chip0.tx_extract_idx[ chip0.curbuf ]) {
268  // no more room in this transaction.
269  return;
270  }
271 
272  // check if in process of sending data
274  chip0.tx_insert_idx[ chip0.curbuf ] = temp;
275 }
276 
278 {
279  int i;
280  for (i = 0; i < len; i++) {
281  w5100_transmit(data[i]);
282  }
283 }
284 
286 {
287  // Now send off spi transaction.
289  uint8_t curbuf = chip0.curbuf;
290 
291  // switch to other buffer to accept more chars.
292  chip0.curbuf++;
293  if (chip0.curbuf >= W5100_BUFFER_NUM) {
294  chip0.curbuf = 0;
295  }
296 
298  uint16_t offset = ptr & SMASK;
299  uint16_t dstAddr = offset + SBASE[ TELEM_SOCKET ];
300 
303 
304  if (offset + len > SSIZE) {
305  // Wrap around circular buffer
306  uint16_t size = SSIZE - offset;
307  w5100_set_buffer(dstAddr, &chip0.tx_buf[curbuf][0], size);
308  w5100_set_buffer(SBASE[ TELEM_SOCKET ], &chip0.tx_buf[curbuf][0] + size, len - size);
309  } else {
310  w5100_set_buffer(dstAddr, &chip0.tx_buf[curbuf][0], len);
311  }
312 
313  // Reset write pointer
314  ptr += len;
316  w5100_sock_set(TELEM_SOCKET, SOCK_TX_WR + 1, ptr & 0xFF);
317 
318  // send
320 
322  while (complete != 0x00) {
323  complete = w5100_sock_get(TELEM_SOCKET, SOCK_CR);
324  }
325 }
326 
328 {
329  uint16_t val = 0, val1 = 0;
330 
331  do {
332  val1 = w5100_sock_get16(_s, SOCK_RSR);
333  if (val1 != 0) {
334  val = w5100_sock_get16(_s, SOCK_RSR);
335  }
336  } while (val != val1);
337  return val;
338 }
339 
341 {
342  // send command to close socket
344  // clear interrupt registers
345  w5100_sock_set(_s, SOCK_IR, 0xFF);
346 }
347 
348 static void configure_socket(uint8_t _s, uint8_t _flag, uint16_t _lport, uint16_t _dport, uint8_t *_dest)
349 {
350  // Configure socket that receives data on 1234
351  w5100_close_socket(_s);
352  // configure type of socket
353  w5100_sock_set(_s, SOCK_MR, SNMR_UDP | _flag);
354  // configure MSB+LSB of local port number
355  w5100_sock_set(_s, SOCK_PORT, _lport >> 8);
356  w5100_sock_set(_s, SOCK_PORT + 1, _lport & 0xFF);
357  if (_dest[0] != 0x00) {
358  // configure destination to send stuff to.
359  w5100_set_buffer(CH_BASE + _s * CH_SIZE + SOCK_DIPR, _dest, 4);
360  }
361 
362  // this mac corresponds to a mac for multicasting....
363  uint8_t mac_multi[] = { 0x01, 0x00, 0x5E, 0x01, 0x01, 0x0B };
364  w5100_set_buffer(CH_BASE + _s * CH_SIZE + SOCK_DHAR, mac_multi, 6);
365 
366  // configure destination port
367  w5100_sock_set(_s, SOCK_DPORT, _dport >> 8);
368  w5100_sock_set(_s, SOCK_DPORT + 1, _dport & 0xFF);
369 
370  // send command to open the socket
372 }
373 
375 {
376  if (w5100_rx_size(CMD_SOCKET) > 0) {
377  return true;
378  }
379  return false;
380 }
381 
382 uint16_t w5100_receive(uint8_t *buf, uint16_t len __attribute__((unused)))
383 {
384  uint8_t head[8];
385  uint16_t data_len = 0;
386  uint16_t ptr = 0;
387 
388  // Get socket read pointer
390  w5100_read_data(CMD_SOCKET, (uint8_t *)ptr, head, 0x08);
391  ptr += 8;
392  data_len = head[6];
393  data_len = (data_len << 8) + head[7];
394 
395  // read data from buffer.
396  w5100_read_data(CMD_SOCKET, (uint8_t *)ptr, buf, data_len); // data copy.
397  ptr += data_len;
398 
399  // update read pointer
400  w5100_sock_set(CMD_SOCKET, SOCK_RXRD, ptr >> 8);
401  w5100_sock_set(CMD_SOCKET, SOCK_RXRD + 1, ptr & 0xFF);
402 
403  // finalize read.
405  return data_len;
406 }
407 
408 static void w5100_read_data(uint8_t s __attribute__((unused)), volatile uint8_t *src, volatile uint8_t *dst,
409  uint16_t len)
410 {
411  uint16_t size;
412  uint16_t src_mask;
413  uint16_t src_ptr;
414 
415  src_mask = (uint16_t)src & RMASK;
416  src_ptr = RBASE[CMD_SOCKET] + src_mask;
417 
418  if ((src_mask + len) > RSIZE) {
419  size = RSIZE - src_mask;
420  w5100_read(src_ptr, (uint8_t *)dst, size);
421  dst += size;
422  w5100_read(RBASE[CMD_SOCKET], (uint8_t *) dst, len - size);
423  } else {
424  w5100_read(src_ptr, (uint8_t *) dst, len);
425  }
426 }
427 
428 static uint16_t w5100_read(uint16_t _addr, uint8_t *_buf, uint16_t _len)
429 {
430  for (int i = 0; i < _len; i++) {
431  _buf[i] = w5100_get(_addr);
432  _addr++;
433  }
434  return _len;
435 }
436 
unsigned short uint16_t
Definition: types.h:16
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
uint16_t RBASE[SOCKETS]
Definition: w5100.c:105
static void w5100_set(uint16_t _reg, uint8_t _val)
Definition: w5100.c:116
volatile uint8_t work_tx[4]
Definition: w5100.h:56
#define W5100_TX_BUFFER_SIZE
Definition: w5100.h:36
static uint8_t dest[]
Definition: w5100.c:98
static void w5100_sock_set(uint8_t _sock, uint16_t _reg, uint8_t _val)
Definition: w5100.c:151
static struct EcefCoor_d offset
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
#define W5100_SLAVE_IDX
Definition: w5100.c:78
uint16_t output_length
number of data words to write
Definition: spi.h:146
static void gpio_clear(ioportid_t port, uint16_t pin)
Clear a gpio output to low level.
Definition: gpio_arch.h:103
static int true_function(struct w5100_periph *p, long *fd, uint16_t len)
Definition: w5100.c:172
static const uint16_t SSIZE
Definition: w5100.c:106
#define RXBUF_BASE
Definition: w5100.c:34
#define SNMR_MULTI
Definition: w5100.c:45
#define SOCK_MR
Definition: w5100.c:62
uint16_t w5100_rx_size(uint8_t _s)
Definition: w5100.c:327
Some architecture independent helper functions for GPIOs.
void w5100_init(void)
Definition: w5100.c:184
#define SOCK_CLOSE
Definition: w5100.c:41
#define SMASK
Definition: w5100.c:50
#define W5100_DRDY_GPIO
Definition: w5100.c:82
#define REG_MR
Definition: w5100.c:53
#define W5100_SPI_DEV
Definition: w5100.c:74
#define CH_BASE
Definition: w5100.c:48
#define W5100_RX_BUFFER_SIZE
Definition: w5100.h:35
#define CH_SIZE
Definition: w5100.c:49
static uint8_t dev_getch(struct w5100_periph *p)
Definition: w5100.c:177
SPI transaction structure.
Definition: spi.h:142
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:364
uint8_t ck_a
Definition: w5100.c:90
#define SOCK_RECV
Definition: w5100.c:43
static void dev_send(struct w5100_periph *p, long fd)
Definition: w5100.c:175
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
#define SOCK_RSR
Definition: w5100.c:70
#define W5100_BUFFER_NUM
Definition: w5100.h:37
void gpio_setup_output(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as outputs.
Definition: gpio_arch.c:33
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
volatile uint8_t tx_buf[W5100_BUFFER_NUM][W5100_TX_BUFFER_SIZE]
Definition: w5100.h:53
#define SOCK_CR
Definition: w5100.c:63
#define REG_SIPR
Definition: w5100.c:60
#define SOCK_PORT
Definition: w5100.c:65
uint16_t w5100_receive(uint8_t *buf, uint16_t len)
Definition: w5100.c:382
static uint8_t w5100_sock_get(uint8_t _sock, uint16_t _reg)
Definition: w5100.c:156
Architecture independent SPI (Serial Peripheral Interface) API.
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
Definition: sys_time_arch.c:95
static uint8_t ip[]
Definition: w5100.c:97
#define SOCK_RXRD
Definition: w5100.c:71
#define CMD_SOCKET
Definition: w5100.c:37
#define SOCK_TX_WR
Definition: w5100.c:69
#define SOCK_OPEN
Definition: w5100.c:40
Definition: spi.h:84
#define TXBUF_BASE
Definition: w5100.c:33
#define SNMR_UDP
Definition: w5100.c:44
static void dev_transmit_buffer(struct w5100_periph *p, long fd, uint8_t *data, uint16_t len)
Definition: w5100.c:174
#define REG_RX_MEM
Definition: w5100.c:54
uint8_t ck_b
Definition: w5100.c:90
Architecture independent timing functions.
static int dev_char_available(struct w5100_periph *p)
Definition: w5100.c:176
volatile uint16_t tx_insert_idx[W5100_BUFFER_NUM]
Definition: w5100.h:54
#define RMASK
Definition: w5100.c:51
#define SOCK_IR
Definition: w5100.c:64
static const uint16_t RSIZE
Definition: w5100.c:107
volatile uint16_t tx_extract_idx[W5100_BUFFER_NUM]
Definition: w5100.h:55
uint16_t val[TCOUPLE_NB]
#define REG_SUBR
Definition: w5100.c:58
volatile uint8_t work_rx[4]
Definition: w5100.h:57
static uint16_t w5100_sock_get16(uint8_t _sock, uint16_t _reg)
Definition: w5100.c:161
enum W5100Status status
Definition: w5100.h:46
#define TELEM_SOCKET
Definition: w5100.c:38
static uint16_t w5100_read(uint16_t _addr, uint8_t *_buf, uint16_t _len)
Definition: w5100.c:428
static void w5100_read_data(uint8_t s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len)
Definition: w5100.c:408
#define SOCK_DPORT
Definition: w5100.c:68
static void w5100_close_socket(uint8_t _s)
Definition: w5100.c:340
struct w5100_periph chip0
Definition: w5100.c:89
CPOL = 0.
Definition: spi.h:77
static void dev_transmit(struct w5100_periph *p, long fd, uint8_t byte)
Definition: w5100.c:173
bool w5100_ch_available()
Definition: w5100.c:374
uint16_t input_length
number of data words to read
Definition: spi.h:145
static uint8_t subnet[]
Definition: w5100.c:99
uint16_t SBASE[SOCKETS]
Definition: w5100.c:104
static void w5100_set_buffer(uint16_t _reg, volatile uint8_t *_buf, uint16_t _len)
Definition: w5100.c:143
#define SOCKETS
Definition: w5100.c:35
#define W5100_DRDY_GPIO_PIN
Definition: w5100.c:86
W5100 ethernet chip I/O.
void w5100_transmit_buffer(uint8_t *data, uint16_t len)
Definition: w5100.c:277
unsigned char uint8_t
Definition: types.h:14
#define SOCK_SEND
Definition: w5100.c:42
#define byte
#define SOCK_DIPR
Definition: w5100.c:67
CPHA = 0.
Definition: spi.h:68
int fd
Definition: serial.c:26
slave is selected before transaction and unselected after
Definition: spi.h:57
#define REG_SHAR
Definition: w5100.c:59
struct spi_transaction w5100_spi
Definition: w5100.c:109
void w5100_send()
Definition: w5100.c:285
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t w5100_rx_buf[W5100_RX_BUFFER_SIZE]
Definition: w5100.c:91
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
Definition: spi.h:119
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
static float p[2][2]
static uint8_t w5100_get(uint16_t _reg)
Definition: w5100.c:129
static uint16_t dport
Definition: w5100.c:100
void w5100_transmit(uint8_t data)
Definition: w5100.c:262
int curbuf
Definition: w5100.h:47
static uint8_t mac[]
Definition: w5100.c:94
#define REG_TX_MEM
Definition: w5100.c:55
struct link_device device
Generic device interface.
Definition: w5100.h:60
#define SOCK_DHAR
Definition: w5100.c:66
#define REG_GAR
Definition: w5100.c:57
static void gpio_set(ioportid_t port, uint16_t pin)
Set a gpio output to high level.
Definition: gpio_arch.h:93
static const uint8_t RST
Definition: w5100.c:102
static void configure_socket(uint8_t _s, uint8_t _flag, uint16_t _lport, uint16_t _dport, uint8_t *_dest)
Definition: w5100.c:348
enum SPITransactionStatus status
Definition: spi.h:156