aboutsummaryrefslogtreecommitdiff
path: root/libraries/Ethernet/utility
diff options
context:
space:
mode:
Diffstat (limited to 'libraries/Ethernet/utility')
-rw-r--r--libraries/Ethernet/utility/socket.cpp400
-rwxr-xr-xlibraries/Ethernet/utility/socket.h41
-rw-r--r--libraries/Ethernet/utility/w5100.cpp188
-rwxr-xr-xlibraries/Ethernet/utility/w5100.h400
4 files changed, 1029 insertions, 0 deletions
diff --git a/libraries/Ethernet/utility/socket.cpp b/libraries/Ethernet/utility/socket.cpp
new file mode 100644
index 0000000..fd3e442
--- /dev/null
+++ b/libraries/Ethernet/utility/socket.cpp
@@ -0,0 +1,400 @@
+#include "w5100.h"
+#include "socket.h"
+
+static uint16_t local_port;
+
+/**
+ * @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it.
+ * @return 1 for success else 0.
+ */
+uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag)
+{
+ if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE))
+ {
+ close(s);
+ W5100.writeSnMR(s, protocol | flag);
+ if (port != 0) {
+ W5100.writeSnPORT(s, port);
+ }
+ else {
+ local_port++; // if don't set the source port, set local_port number.
+ W5100.writeSnPORT(s, local_port);
+ }
+
+ W5100.execCmdSn(s, Sock_OPEN);
+
+ return 1;
+ }
+
+ return 0;
+}
+
+
+/**
+ * @brief This function close the socket and parameter is "s" which represent the socket number
+ */
+void close(SOCKET s)
+{
+ W5100.execCmdSn(s, Sock_CLOSE);
+ W5100.writeSnIR(s, 0xFF);
+}
+
+
+/**
+ * @brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer.
+ * @return 1 for success else 0.
+ */
+uint8_t listen(SOCKET s)
+{
+ if (W5100.readSnSR(s) != SnSR::INIT)
+ return 0;
+ W5100.execCmdSn(s, Sock_LISTEN);
+ return 1;
+}
+
+
+/**
+ * @brief This function established the connection for the channel in Active (client) mode.
+ * This function waits for the untill the connection is established.
+ *
+ * @return 1 for success else 0.
+ */
+uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port)
+{
+ if
+ (
+ ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) ||
+ ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
+ (port == 0x00)
+ )
+ return 0;
+
+ // set destination IP
+ W5100.writeSnDIPR(s, addr);
+ W5100.writeSnDPORT(s, port);
+ W5100.execCmdSn(s, Sock_CONNECT);
+
+ return 1;
+}
+
+
+
+/**
+ * @brief This function used for disconnect the socket and parameter is "s" which represent the socket number
+ * @return 1 for success else 0.
+ */
+void disconnect(SOCKET s)
+{
+ W5100.execCmdSn(s, Sock_DISCON);
+}
+
+
+/**
+ * @brief This function used to send the data in TCP mode
+ * @return 1 for success else 0.
+ */
+uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
+{
+ uint8_t status=0;
+ uint16_t ret=0;
+ uint16_t freesize=0;
+
+ if (len > W5100.SSIZE)
+ ret = W5100.SSIZE; // check size not to exceed MAX size.
+ else
+ ret = len;
+
+ // if freebuf is available, start.
+ do
+ {
+ freesize = W5100.getTXFreeSize(s);
+ status = W5100.readSnSR(s);
+ if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT))
+ {
+ ret = 0;
+ break;
+ }
+ }
+ while (freesize < ret);
+
+ // copy data
+ W5100.send_data_processing(s, (uint8_t *)buf, ret);
+ W5100.execCmdSn(s, Sock_SEND);
+
+ /* +2008.01 bj */
+ while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
+ {
+ /* m2008.01 [bj] : reduce code */
+ if ( W5100.readSnSR(s) == SnSR::CLOSED )
+ {
+ close(s);
+ return 0;
+ }
+ }
+ /* +2008.01 bj */
+ W5100.writeSnIR(s, SnIR::SEND_OK);
+ return ret;
+}
+
+
+/**
+ * @brief This function is an application I/F function which is used to receive the data in TCP mode.
+ * It continues to wait for data as much as the application wants to receive.
+ *
+ * @return received data size for success else -1.
+ */
+int16_t recv(SOCKET s, uint8_t *buf, int16_t len)
+{
+ // Check how much data is available
+ int16_t ret = W5100.getRXReceivedSize(s);
+ if ( ret == 0 )
+ {
+ // No data available.
+ uint8_t status = W5100.readSnSR(s);
+ if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT )
+ {
+ // The remote end has closed its side of the connection, so this is the eof state
+ ret = 0;
+ }
+ else
+ {
+ // The connection is still up, but there's no data waiting to be read
+ ret = -1;
+ }
+ }
+ else if (ret > len)
+ {
+ ret = len;
+ }
+
+ if ( ret > 0 )
+ {
+ W5100.recv_data_processing(s, buf, ret);
+ W5100.execCmdSn(s, Sock_RECV);
+ }
+ return ret;
+}
+
+
+/**
+ * @brief Returns the first byte in the receive queue (no checking)
+ *
+ * @return
+ */
+uint16_t peek(SOCKET s, uint8_t *buf)
+{
+ W5100.recv_data_processing(s, buf, 1, 1);
+
+ return 1;
+}
+
+
+/**
+ * @brief This function is an application I/F function which is used to send the data for other then TCP mode.
+ * Unlike TCP transmission, The peer's destination address and the port is needed.
+ *
+ * @return This function return send data size for success else -1.
+ */
+uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port)
+{
+ uint16_t ret=0;
+
+ if (len > W5100.SSIZE) ret = W5100.SSIZE; // check size not to exceed MAX size.
+ else ret = len;
+
+ if
+ (
+ ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
+ ((port == 0x00)) ||(ret == 0)
+ )
+ {
+ /* +2008.01 [bj] : added return value */
+ ret = 0;
+ }
+ else
+ {
+ W5100.writeSnDIPR(s, addr);
+ W5100.writeSnDPORT(s, port);
+
+ // copy data
+ W5100.send_data_processing(s, (uint8_t *)buf, ret);
+ W5100.execCmdSn(s, Sock_SEND);
+
+ /* +2008.01 bj */
+ while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
+ {
+ if (W5100.readSnIR(s) & SnIR::TIMEOUT)
+ {
+ /* +2008.01 [bj]: clear interrupt */
+ W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */
+ return 0;
+ }
+ }
+
+ /* +2008.01 bj */
+ W5100.writeSnIR(s, SnIR::SEND_OK);
+ }
+ return ret;
+}
+
+
+/**
+ * @brief This function is an application I/F function which is used to receive the data in other then
+ * TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well.
+ *
+ * @return This function return received data size for success else -1.
+ */
+uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port)
+{
+ uint8_t head[8];
+ uint16_t data_len=0;
+ uint16_t ptr=0;
+
+ if ( len > 0 )
+ {
+ ptr = W5100.readSnRX_RD(s);
+ switch (W5100.readSnMR(s) & 0x07)
+ {
+ case SnMR::UDP :
+ W5100.read_data(s, (uint8_t *)ptr, head, 0x08);
+ ptr += 8;
+ // read peer's IP address, port number.
+ addr[0] = head[0];
+ addr[1] = head[1];
+ addr[2] = head[2];
+ addr[3] = head[3];
+ *port = head[4];
+ *port = (*port << 8) + head[5];
+ data_len = head[6];
+ data_len = (data_len << 8) + head[7];
+
+ W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy.
+ ptr += data_len;
+
+ W5100.writeSnRX_RD(s, ptr);
+ break;
+
+ case SnMR::IPRAW :
+ W5100.read_data(s, (uint8_t *)ptr, head, 0x06);
+ ptr += 6;
+
+ addr[0] = head[0];
+ addr[1] = head[1];
+ addr[2] = head[2];
+ addr[3] = head[3];
+ data_len = head[4];
+ data_len = (data_len << 8) + head[5];
+
+ W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy.
+ ptr += data_len;
+
+ W5100.writeSnRX_RD(s, ptr);
+ break;
+
+ case SnMR::MACRAW:
+ W5100.read_data(s,(uint8_t*)ptr,head,2);
+ ptr+=2;
+ data_len = head[0];
+ data_len = (data_len<<8) + head[1] - 2;
+
+ W5100.read_data(s,(uint8_t*) ptr,buf,data_len);
+ ptr += data_len;
+ W5100.writeSnRX_RD(s, ptr);
+ break;
+
+ default :
+ break;
+ }
+ W5100.execCmdSn(s, Sock_RECV);
+ }
+ return data_len;
+}
+
+
+uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len)
+{
+ uint8_t status=0;
+ uint16_t ret=0;
+
+ if (len > W5100.SSIZE)
+ ret = W5100.SSIZE; // check size not to exceed MAX size.
+ else
+ ret = len;
+
+ if (ret == 0)
+ return 0;
+
+ W5100.send_data_processing(s, (uint8_t *)buf, ret);
+ W5100.execCmdSn(s, Sock_SEND);
+
+ while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
+ {
+ status = W5100.readSnSR(s);
+ if (W5100.readSnIR(s) & SnIR::TIMEOUT)
+ {
+ /* in case of igmp, if send fails, then socket closed */
+ /* if you want change, remove this code. */
+ close(s);
+ return 0;
+ }
+ }
+
+ W5100.writeSnIR(s, SnIR::SEND_OK);
+ return ret;
+}
+
+uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len)
+{
+ uint16_t ret =0;
+ if (len > W5100.getTXFreeSize(s))
+ {
+ ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size.
+ }
+ else
+ {
+ ret = len;
+ }
+ W5100.send_data_processing_offset(s, offset, buf, ret);
+ return ret;
+}
+
+int startUDP(SOCKET s, uint8_t* addr, uint16_t port)
+{
+ if
+ (
+ ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) ||
+ ((port == 0x00))
+ )
+ {
+ return 0;
+ }
+ else
+ {
+ W5100.writeSnDIPR(s, addr);
+ W5100.writeSnDPORT(s, port);
+ return 1;
+ }
+}
+
+int sendUDP(SOCKET s)
+{
+ W5100.execCmdSn(s, Sock_SEND);
+
+ /* +2008.01 bj */
+ while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK )
+ {
+ if (W5100.readSnIR(s) & SnIR::TIMEOUT)
+ {
+ /* +2008.01 [bj]: clear interrupt */
+ W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT));
+ return 0;
+ }
+ }
+
+ /* +2008.01 bj */
+ W5100.writeSnIR(s, SnIR::SEND_OK);
+
+ /* Sent ok */
+ return 1;
+}
+
diff --git a/libraries/Ethernet/utility/socket.h b/libraries/Ethernet/utility/socket.h
new file mode 100755
index 0000000..45e0fb3
--- /dev/null
+++ b/libraries/Ethernet/utility/socket.h
@@ -0,0 +1,41 @@
+#ifndef _SOCKET_H_
+#define _SOCKET_H_
+
+#include "w5100.h"
+
+extern uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode)
+extern void close(SOCKET s); // Close socket
+extern uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection)
+extern void disconnect(SOCKET s); // disconnect the connection
+extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection)
+extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP)
+extern int16_t recv(SOCKET s, uint8_t * buf, int16_t len); // Receive data (TCP)
+extern uint16_t peek(SOCKET s, uint8_t *buf);
+extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW)
+extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW)
+
+extern uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len);
+
+// Functions to allow buffered UDP send (i.e. where the UDP datagram is built up over a
+// number of calls before being sent
+/*
+ @brief This function sets up a UDP datagram, the data for which will be provided by one
+ or more calls to bufferData and then finally sent with sendUDP.
+ @return 1 if the datagram was successfully set up, or 0 if there was an error
+*/
+extern int startUDP(SOCKET s, uint8_t* addr, uint16_t port);
+/*
+ @brief This function copies up to len bytes of data from buf into a UDP datagram to be
+ sent later by sendUDP. Allows datagrams to be built up from a series of bufferData calls.
+ @return Number of bytes successfully buffered
+*/
+uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len);
+/*
+ @brief Send a UDP datagram built up from a sequence of startUDP followed by one or more
+ calls to bufferData.
+ @return 1 if the datagram was successfully sent, or 0 if there was an error
+*/
+int sendUDP(SOCKET s);
+
+#endif
+/* _SOCKET_H_ */
diff --git a/libraries/Ethernet/utility/w5100.cpp b/libraries/Ethernet/utility/w5100.cpp
new file mode 100644
index 0000000..9c748fd
--- /dev/null
+++ b/libraries/Ethernet/utility/w5100.cpp
@@ -0,0 +1,188 @@
+/*
+ * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of either the GNU General Public License version 2
+ * or the GNU Lesser General Public License version 2.1, both as
+ * published by the Free Software Foundation.
+ */
+
+#include <stdio.h>
+#include <string.h>
+#include <avr/interrupt.h>
+
+#include "w5100.h"
+
+// W5100 controller instance
+W5100Class W5100;
+
+#define TX_RX_MAX_BUF_SIZE 2048
+#define TX_BUF 0x1100
+#define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE)
+
+#define TXBUF_BASE 0x4000
+#define RXBUF_BASE 0x6000
+
+void W5100Class::init(void)
+{
+ delay(300);
+
+ SPI.begin();
+ initSS();
+
+ writeMR(1<<RST);
+ writeTMSR(0x55);
+ writeRMSR(0x55);
+
+ for (int i=0; i<MAX_SOCK_NUM; i++) {
+ SBASE[i] = TXBUF_BASE + SSIZE * i;
+ RBASE[i] = RXBUF_BASE + RSIZE * i;
+ }
+}
+
+uint16_t W5100Class::getTXFreeSize(SOCKET s)
+{
+ uint16_t val=0, val1=0;
+ do {
+ val1 = readSnTX_FSR(s);
+ if (val1 != 0)
+ val = readSnTX_FSR(s);
+ }
+ while (val != val1);
+ return val;
+}
+
+uint16_t W5100Class::getRXReceivedSize(SOCKET s)
+{
+ uint16_t val=0,val1=0;
+ do {
+ val1 = readSnRX_RSR(s);
+ if (val1 != 0)
+ val = readSnRX_RSR(s);
+ }
+ while (val != val1);
+ return val;
+}
+
+
+void W5100Class::send_data_processing(SOCKET s, const uint8_t *data, uint16_t len)
+{
+ // This is same as having no offset in a call to send_data_processing_offset
+ send_data_processing_offset(s, 0, data, len);
+}
+
+void W5100Class::send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len)
+{
+ uint16_t ptr = readSnTX_WR(s);
+ ptr += data_offset;
+ uint16_t offset = ptr & SMASK;
+ uint16_t dstAddr = offset + SBASE[s];
+
+ if (offset + len > SSIZE)
+ {
+ // Wrap around circular buffer
+ uint16_t size = SSIZE - offset;
+ write(dstAddr, data, size);
+ write(SBASE[s], data + size, len - size);
+ }
+ else {
+ write(dstAddr, data, len);
+ }
+
+ ptr += len;
+ writeSnTX_WR(s, ptr);
+}
+
+
+void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek)
+{
+ uint16_t ptr;
+ ptr = readSnRX_RD(s);
+ read_data(s, (uint8_t *)ptr, data, len);
+ if (!peek)
+ {
+ ptr += len;
+ writeSnRX_RD(s, ptr);
+ }
+}
+
+void W5100Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len)
+{
+ uint16_t size;
+ uint16_t src_mask;
+ uint16_t src_ptr;
+
+ src_mask = (uint16_t)src & RMASK;
+ src_ptr = RBASE[s] + src_mask;
+
+ if( (src_mask + len) > RSIZE )
+ {
+ size = RSIZE - src_mask;
+ read(src_ptr, (uint8_t *)dst, size);
+ dst += size;
+ read(RBASE[s], (uint8_t *) dst, len - size);
+ }
+ else
+ read(src_ptr, (uint8_t *) dst, len);
+}
+
+
+uint8_t W5100Class::write(uint16_t _addr, uint8_t _data)
+{
+ setSS();
+ SPI.transfer(0xF0);
+ SPI.transfer(_addr >> 8);
+ SPI.transfer(_addr & 0xFF);
+ SPI.transfer(_data);
+ resetSS();
+ return 1;
+}
+
+uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len)
+{
+ for (uint16_t i=0; i<_len; i++)
+ {
+ setSS();
+ SPI.transfer(0xF0);
+ SPI.transfer(_addr >> 8);
+ SPI.transfer(_addr & 0xFF);
+ _addr++;
+ SPI.transfer(_buf[i]);
+ resetSS();
+ }
+ return _len;
+}
+
+uint8_t W5100Class::read(uint16_t _addr)
+{
+ setSS();
+ SPI.transfer(0x0F);
+ SPI.transfer(_addr >> 8);
+ SPI.transfer(_addr & 0xFF);
+ uint8_t _data = SPI.transfer(0);
+ resetSS();
+ return _data;
+}
+
+uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len)
+{
+ for (uint16_t i=0; i<_len; i++)
+ {
+ setSS();
+ SPI.transfer(0x0F);
+ SPI.transfer(_addr >> 8);
+ SPI.transfer(_addr & 0xFF);
+ _addr++;
+ _buf[i] = SPI.transfer(0);
+ resetSS();
+ }
+ return _len;
+}
+
+void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) {
+ // Send command to socket
+ writeSnCR(s, _cmd);
+ // Wait for command to complete
+ while (readSnCR(s))
+ ;
+}
diff --git a/libraries/Ethernet/utility/w5100.h b/libraries/Ethernet/utility/w5100.h
new file mode 100755
index 0000000..153aedb
--- /dev/null
+++ b/libraries/Ethernet/utility/w5100.h
@@ -0,0 +1,400 @@
+/*
+ * Copyright (c) 2010 by Cristian Maglie <c.maglie@bug.st>
+ *
+ * This file is free software; you can redistribute it and/or modify
+ * it under the terms of either the GNU General Public License version 2
+ * or the GNU Lesser General Public License version 2.1, both as
+ * published by the Free Software Foundation.
+ */
+
+#ifndef W5100_H_INCLUDED
+#define W5100_H_INCLUDED
+
+#include <avr/pgmspace.h>
+#include <SPI.h>
+
+#define MAX_SOCK_NUM 4
+
+typedef uint8_t SOCKET;
+
+#define IDM_OR 0x8000
+#define IDM_AR0 0x8001
+#define IDM_AR1 0x8002
+#define IDM_DR 0x8003
+/*
+class MR {
+public:
+ static const uint8_t RST = 0x80;
+ static const uint8_t PB = 0x10;
+ static const uint8_t PPPOE = 0x08;
+ static const uint8_t LB = 0x04;
+ static const uint8_t AI = 0x02;
+ static const uint8_t IND = 0x01;
+};
+*/
+/*
+class IR {
+public:
+ static const uint8_t CONFLICT = 0x80;
+ static const uint8_t UNREACH = 0x40;
+ static const uint8_t PPPoE = 0x20;
+ static const uint8_t SOCK0 = 0x01;
+ static const uint8_t SOCK1 = 0x02;
+ static const uint8_t SOCK2 = 0x04;
+ static const uint8_t SOCK3 = 0x08;
+ static inline uint8_t SOCK(SOCKET ch) { return (0x01 << ch); };
+};
+*/
+
+class SnMR {
+public:
+ static const uint8_t CLOSE = 0x00;
+ static const uint8_t TCP = 0x01;
+ static const uint8_t UDP = 0x02;
+ static const uint8_t IPRAW = 0x03;
+ static const uint8_t MACRAW = 0x04;
+ static const uint8_t PPPOE = 0x05;
+ static const uint8_t ND = 0x20;
+ static const uint8_t MULTI = 0x80;
+};
+
+enum SockCMD {
+ Sock_OPEN = 0x01,
+ Sock_LISTEN = 0x02,
+ Sock_CONNECT = 0x04,
+ Sock_DISCON = 0x08,
+ Sock_CLOSE = 0x10,
+ Sock_SEND = 0x20,
+ Sock_SEND_MAC = 0x21,
+ Sock_SEND_KEEP = 0x22,
+ Sock_RECV = 0x40
+};
+
+/*class SnCmd {
+public:
+ static const uint8_t OPEN = 0x01;
+ static const uint8_t LISTEN = 0x02;
+ static const uint8_t CONNECT = 0x04;
+ static const uint8_t DISCON = 0x08;
+ static const uint8_t CLOSE = 0x10;
+ static const uint8_t SEND = 0x20;
+ static const uint8_t SEND_MAC = 0x21;
+ static const uint8_t SEND_KEEP = 0x22;
+ static const uint8_t RECV = 0x40;
+};
+*/
+
+class SnIR {
+public:
+ static const uint8_t SEND_OK = 0x10;
+ static const uint8_t TIMEOUT = 0x08;
+ static const uint8_t RECV = 0x04;
+ static const uint8_t DISCON = 0x02;
+ static const uint8_t CON = 0x01;
+};
+
+class SnSR {
+public:
+ static const uint8_t CLOSED = 0x00;
+ static const uint8_t INIT = 0x13;
+ static const uint8_t LISTEN = 0x14;
+ static const uint8_t SYNSENT = 0x15;
+ static const uint8_t SYNRECV = 0x16;
+ static const uint8_t ESTABLISHED = 0x17;
+ static const uint8_t FIN_WAIT = 0x18;
+ static const uint8_t CLOSING = 0x1A;
+ static const uint8_t TIME_WAIT = 0x1B;
+ static const uint8_t CLOSE_WAIT = 0x1C;
+ static const uint8_t LAST_ACK = 0x1D;
+ static const uint8_t UDP = 0x22;
+ static const uint8_t IPRAW = 0x32;
+ static const uint8_t MACRAW = 0x42;
+ static const uint8_t PPPOE = 0x5F;
+};
+
+class IPPROTO {
+public:
+ static const uint8_t IP = 0;
+ static const uint8_t ICMP = 1;
+ static const uint8_t IGMP = 2;
+ static const uint8_t GGP = 3;
+ static const uint8_t TCP = 6;
+ static const uint8_t PUP = 12;
+ static const uint8_t UDP = 17;
+ static const uint8_t IDP = 22;
+ static const uint8_t ND = 77;
+ static const uint8_t RAW = 255;
+};
+
+class W5100Class {
+
+public:
+ void init();
+
+ /**
+ * @brief This function is being used for copy the data form Receive buffer of the chip to application buffer.
+ *
+ * It calculate the actual physical address where one has to read
+ * the data from Receive buffer. Here also take care of the condition while it exceed
+ * the Rx memory uper-bound of socket.
+ */
+ void read_data(SOCKET s, volatile uint8_t * src, volatile uint8_t * dst, uint16_t len);
+
+ /**
+ * @brief This function is being called by send() and sendto() function also.
+ *
+ * This function read the Tx write pointer register and after copy the data in buffer update the Tx write pointer
+ * register. User should read upper byte first and lower byte later to get proper value.
+ */
+ void send_data_processing(SOCKET s, const uint8_t *data, uint16_t len);
+ /**
+ * @brief A copy of send_data_processing that uses the provided ptr for the
+ * write offset. Only needed for the "streaming" UDP API, where
+ * a single UDP packet is built up over a number of calls to
+ * send_data_processing_ptr, because TX_WR doesn't seem to get updated
+ * correctly in those scenarios
+ * @param ptr value to use in place of TX_WR. If 0, then the value is read
+ * in from TX_WR
+ * @return New value for ptr, to be used in the next call
+ */
+// FIXME Update documentation
+ void send_data_processing_offset(SOCKET s, uint16_t data_offset, const uint8_t *data, uint16_t len);
+
+ /**
+ * @brief This function is being called by recv() also.
+ *
+ * This function read the Rx read pointer register
+ * and after copy the data from receive buffer update the Rx write pointer register.
+ * User should read upper byte first and lower byte later to get proper value.
+ */
+ void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0);
+
+ inline void setGatewayIp(uint8_t *_addr);
+ inline void getGatewayIp(uint8_t *_addr);
+
+ inline void setSubnetMask(uint8_t *_addr);
+ inline void getSubnetMask(uint8_t *_addr);
+
+ inline void setMACAddress(uint8_t * addr);
+ inline void getMACAddress(uint8_t * addr);
+
+ inline void setIPAddress(uint8_t * addr);
+ inline void getIPAddress(uint8_t * addr);
+
+ inline void setRetransmissionTime(uint16_t timeout);
+ inline void setRetransmissionCount(uint8_t _retry);
+
+ void execCmdSn(SOCKET s, SockCMD _cmd);
+
+ uint16_t getTXFreeSize(SOCKET s);
+ uint16_t getRXReceivedSize(SOCKET s);
+
+
+ // W5100 Registers
+ // ---------------
+private:
+ static uint8_t write(uint16_t _addr, uint8_t _data);
+ static uint16_t write(uint16_t addr, const uint8_t *buf, uint16_t len);
+ static uint8_t read(uint16_t addr);
+ static uint16_t read(uint16_t addr, uint8_t *buf, uint16_t len);
+
+#define __GP_REGISTER8(name, address) \
+ static inline void write##name(uint8_t _data) { \
+ write(address, _data); \
+ } \
+ static inline uint8_t read##name() { \
+ return read(address); \
+ }
+#define __GP_REGISTER16(name, address) \
+ static void write##name(uint16_t _data) { \
+ write(address, _data >> 8); \
+ write(address+1, _data & 0xFF); \
+ } \
+ static uint16_t read##name() { \
+ uint16_t res = read(address); \
+ res = (res << 8) + read(address + 1); \
+ return res; \
+ }
+#define __GP_REGISTER_N(name, address, size) \
+ static uint16_t write##name(uint8_t *_buff) { \
+ return write(address, _buff, size); \
+ } \
+ static uint16_t read##name(uint8_t *_buff) { \
+ return read(address, _buff, size); \
+ }
+
+public:
+ __GP_REGISTER8 (MR, 0x0000); // Mode
+ __GP_REGISTER_N(GAR, 0x0001, 4); // Gateway IP address
+ __GP_REGISTER_N(SUBR, 0x0005, 4); // Subnet mask address
+ __GP_REGISTER_N(SHAR, 0x0009, 6); // Source MAC address
+ __GP_REGISTER_N(SIPR, 0x000F, 4); // Source IP address
+ __GP_REGISTER8 (IR, 0x0015); // Interrupt
+ __GP_REGISTER8 (IMR, 0x0016); // Interrupt Mask
+ __GP_REGISTER16(RTR, 0x0017); // Timeout address
+ __GP_REGISTER8 (RCR, 0x0019); // Retry count
+ __GP_REGISTER8 (RMSR, 0x001A); // Receive memory size
+ __GP_REGISTER8 (TMSR, 0x001B); // Transmit memory size
+ __GP_REGISTER8 (PATR, 0x001C); // Authentication type address in PPPoE mode
+ __GP_REGISTER8 (PTIMER, 0x0028); // PPP LCP Request Timer
+ __GP_REGISTER8 (PMAGIC, 0x0029); // PPP LCP Magic Number
+ __GP_REGISTER_N(UIPR, 0x002A, 4); // Unreachable IP address in UDP mode
+ __GP_REGISTER16(UPORT, 0x002E); // Unreachable Port address in UDP mode
+
+#undef __GP_REGISTER8
+#undef __GP_REGISTER16
+#undef __GP_REGISTER_N
+
+ // W5100 Socket registers
+ // ----------------------
+private:
+ static inline uint8_t readSn(SOCKET _s, uint16_t _addr);
+ static inline uint8_t writeSn(SOCKET _s, uint16_t _addr, uint8_t _data);
+ static inline uint16_t readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
+ static inline uint16_t writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t len);
+
+ static const uint16_t CH_BASE = 0x0400;
+ static const uint16_t CH_SIZE = 0x0100;
+
+#define __SOCKET_REGISTER8(name, address) \
+ static inline void write##name(SOCKET _s, uint8_t _data) { \
+ writeSn(_s, address, _data); \
+ } \
+ static inline uint8_t read##name(SOCKET _s) { \
+ return readSn(_s, address); \
+ }
+#define __SOCKET_REGISTER16(name, address) \
+ static void write##name(SOCKET _s, uint16_t _data) { \
+ writeSn(_s, address, _data >> 8); \
+ writeSn(_s, address+1, _data & 0xFF); \
+ } \
+ static uint16_t read##name(SOCKET _s) { \
+ uint16_t res = readSn(_s, address); \
+ uint16_t res2 = readSn(_s,address + 1); \
+ res = res << 8; \
+ res2 = res2 & 0xFF; \
+ res = res | res2; \
+ return res; \
+ }
+#define __SOCKET_REGISTER_N(name, address, size) \
+ static uint16_t write##name(SOCKET _s, uint8_t *_buff) { \
+ return writeSn(_s, address, _buff, size); \
+ } \
+ static uint16_t read##name(SOCKET _s, uint8_t *_buff) { \
+ return readSn(_s, address, _buff, size); \
+ }
+
+public:
+ __SOCKET_REGISTER8(SnMR, 0x0000) // Mode
+ __SOCKET_REGISTER8(SnCR, 0x0001) // Command
+ __SOCKET_REGISTER8(SnIR, 0x0002) // Interrupt
+ __SOCKET_REGISTER8(SnSR, 0x0003) // Status
+ __SOCKET_REGISTER16(SnPORT, 0x0004) // Source Port
+ __SOCKET_REGISTER_N(SnDHAR, 0x0006, 6) // Destination Hardw Addr
+ __SOCKET_REGISTER_N(SnDIPR, 0x000C, 4) // Destination IP Addr
+ __SOCKET_REGISTER16(SnDPORT, 0x0010) // Destination Port
+ __SOCKET_REGISTER16(SnMSSR, 0x0012) // Max Segment Size
+ __SOCKET_REGISTER8(SnPROTO, 0x0014) // Protocol in IP RAW Mode
+ __SOCKET_REGISTER8(SnTOS, 0x0015) // IP TOS
+ __SOCKET_REGISTER8(SnTTL, 0x0016) // IP TTL
+ __SOCKET_REGISTER16(SnTX_FSR, 0x0020) // TX Free Size
+ __SOCKET_REGISTER16(SnTX_RD, 0x0022) // TX Read Pointer
+ __SOCKET_REGISTER16(SnTX_WR, 0x0024) // TX Write Pointer
+ __SOCKET_REGISTER16(SnRX_RSR, 0x0026) // RX Free Size
+ __SOCKET_REGISTER16(SnRX_RD, 0x0028) // RX Read Pointer
+ __SOCKET_REGISTER16(SnRX_WR, 0x002A) // RX Write Pointer (supported?)
+
+#undef __SOCKET_REGISTER8
+#undef __SOCKET_REGISTER16
+#undef __SOCKET_REGISTER_N
+
+
+private:
+ static const uint8_t RST = 7; // Reset BIT
+
+ static const int SOCKETS = 4;
+ static const uint16_t SMASK = 0x07FF; // Tx buffer MASK
+ static const uint16_t RMASK = 0x07FF; // Rx buffer MASK
+public:
+ static const uint16_t SSIZE = 2048; // Max Tx buffer size
+private:
+ static const uint16_t RSIZE = 2048; // Max Rx buffer size
+ uint16_t SBASE[SOCKETS]; // Tx buffer base address
+ uint16_t RBASE[SOCKETS]; // Rx buffer base address
+
+private:
+#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
+ inline static void initSS() { DDRB |= _BV(4); };
+ inline static void setSS() { PORTB &= ~_BV(4); };
+ inline static void resetSS() { PORTB |= _BV(4); };
+#elif defined(__AVR_ATmega32U4__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB162__)
+ inline static void initSS() { DDRB |= _BV(0); };
+ inline static void setSS() { PORTB &= ~_BV(0); };
+ inline static void resetSS() { PORTB |= _BV(0); };
+#else
+ inline static void initSS() { DDRB |= _BV(2); };
+ inline static void setSS() { PORTB &= ~_BV(2); };
+ inline static void resetSS() { PORTB |= _BV(2); };
+#endif
+
+};
+
+extern W5100Class W5100;
+
+uint8_t W5100Class::readSn(SOCKET _s, uint16_t _addr) {
+ return read(CH_BASE + _s * CH_SIZE + _addr);
+}
+
+uint8_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t _data) {
+ return write(CH_BASE + _s * CH_SIZE + _addr, _data);
+}
+
+uint16_t W5100Class::readSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
+ return read(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
+}
+
+uint16_t W5100Class::writeSn(SOCKET _s, uint16_t _addr, uint8_t *_buf, uint16_t _len) {
+ return write(CH_BASE + _s * CH_SIZE + _addr, _buf, _len);
+}
+
+void W5100Class::getGatewayIp(uint8_t *_addr) {
+ readGAR(_addr);
+}
+
+void W5100Class::setGatewayIp(uint8_t *_addr) {
+ writeGAR(_addr);
+}
+
+void W5100Class::getSubnetMask(uint8_t *_addr) {
+ readSUBR(_addr);
+}
+
+void W5100Class::setSubnetMask(uint8_t *_addr) {
+ writeSUBR(_addr);
+}
+
+void W5100Class::getMACAddress(uint8_t *_addr) {
+ readSHAR(_addr);
+}
+
+void W5100Class::setMACAddress(uint8_t *_addr) {
+ writeSHAR(_addr);
+}
+
+void W5100Class::getIPAddress(uint8_t *_addr) {
+ readSIPR(_addr);
+}
+
+void W5100Class::setIPAddress(uint8_t *_addr) {
+ writeSIPR(_addr);
+}
+
+void W5100Class::setRetransmissionTime(uint16_t _timeout) {
+ writeRTR(_timeout);
+}
+
+void W5100Class::setRetransmissionCount(uint8_t _retry) {
+ writeRCR(_retry);
+}
+
+#endif