Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features: setting socket memory sizes and skipping read bytes #3

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 3 additions & 1 deletion Ethernet/Ethernet.cpp → Ethernet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 0, };
int EthernetClass::begin(void)
{
byte mac_address[6] ={0,};
_dhcp = new DhcpClass();

if(_dhcp == NULL)
_dhcp = new DhcpClass();

// Initialise the basic info
W5100.init();
Expand Down
2 changes: 2 additions & 0 deletions Ethernet/Ethernet.h → Ethernet.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class EthernetClass {
IPAddress _dnsServerAddress;
DhcpClass* _dhcp;
public:
EthernetClass() : _dhcp(NULL) {}

static uint8_t _state[MAX_SOCK_NUM];
static uint16_t _server_port[MAX_SOCK_NUM];

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 2 additions & 7 deletions Ethernet/EthernetUdp.cpp → EthernetUdp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ int EthernetUDP::parsePacket()
// discard any remaining bytes in the last packet
flush();

if (W5100.getRXReceivedSize(_sock) > 0)
{
//HACK - hand-parse the UDP packet using TCP recv method
uint8_t tmpBuf[8];
int ret =0;
Expand All @@ -139,9 +137,6 @@ int EthernetUDP::parsePacket()
ret = _remaining;
}
return ret;
}
// There aren't any packets available
return 0;
}

int EthernetUDP::read()
Expand Down Expand Up @@ -210,9 +205,9 @@ void EthernetUDP::flush()
// should only occur if recv fails after telling us the data is there, lets
// hope the w5100 always behaves :)

while (_remaining)
if (_remaining)
{
read();
W5100.skip(_sock, _remaining);
}
}

File renamed without changes.
File renamed without changes.
File renamed without changes.
42 changes: 42 additions & 0 deletions keywords.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#######################################
# Syntax Coloring Map For Ethernet
#######################################

#######################################
# Datatypes (KEYWORD1)
#######################################

Ethernet KEYWORD1
EthernetClient KEYWORD1
EthernetServer KEYWORD1
IPAddress KEYWORD1

#######################################
# Methods and Functions (KEYWORD2)
#######################################

status KEYWORD2
connect KEYWORD2
write KEYWORD2
available KEYWORD2
read KEYWORD2
peek KEYWORD2
flush KEYWORD2
stop KEYWORD2
connected KEYWORD2
begin KEYWORD2
beginPacket KEYWORD2
endPacket KEYWORD2
parsePacket KEYWORD2
remoteIP KEYWORD2
remotePort KEYWORD2
setRXMemorySizes KEYWORD2
setTXMemorySizes KEYWORD2

#######################################
# Constants (LITERAL1)
#######################################

W5100_ETHERNET_SHIELD LITERAL1
W5200_ETHERNET_SHIELD LITERAL1
W5500_ETHERNET_SHIELD LITERAL1
File renamed without changes.
10 changes: 5 additions & 5 deletions Ethernet/utility/socket.cpp → utility/socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len)
uint16_t ret=0;
uint16_t freesize=0;

if (len > W5100.SSIZE)
ret = W5100.SSIZE; // check size not to exceed MAX size.
if (len > W5100.SSIZE[s])
ret = W5100.SSIZE[s]; // check size not to exceed MAX size.
else
ret = len;

Expand Down Expand Up @@ -199,7 +199,7 @@ uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint1
{
uint16_t ret=0;

if (len > W5100.SSIZE) ret = W5100.SSIZE; // check size not to exceed MAX size.
if (len > W5100.SSIZE[s]) ret = W5100.SSIZE[s]; // check size not to exceed MAX size.
else ret = len;

if
Expand Down Expand Up @@ -316,8 +316,8 @@ 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.
if (len > W5100.SSIZE[s])
ret = W5100.SSIZE[s]; // check size not to exceed MAX size.
else
ret = len;

Expand Down
File renamed without changes.
65 changes: 55 additions & 10 deletions Ethernet/utility/w5100.cpp → utility/w5100.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,49 @@ void W5100Class::init(void)
initSS();

writeMR(1<<RST);
writeTMSR(0x55);
writeRMSR(0x55);

uint16_t sizes[MAX_SOCK_NUM] = {2048,2048,2048,2048};
setTXMemorySizes(sizes);
setRXMemorySizes(sizes);
}

void W5100Class::setTXMemorySizes(uint16_t * sizes)
{
uint8_t msr = 0;
for (int i=0; i<MAX_SOCK_NUM; i++) {
SSIZE[i] = sizes[i];
uint16_t size = sizes[i] >> 10;
uint8_t log2 = 0;
while (size >>= 1) ++log2;
msr |= log2 << (i * 2);
}
writeTMSR(msr);

SMASK[0] = SSIZE[0] - 1;
SBASE[0] = TXBUF_BASE;
for (int i=1; i<MAX_SOCK_NUM; i++) {
SMASK[i] = SSIZE[i] - 1;
SBASE[i] = SBASE[i-1] + SSIZE[i];
}
}

void W5100Class::setRXMemorySizes(uint16_t * sizes)
{
uint8_t msr = 0;
for (int i=0; i<MAX_SOCK_NUM; i++) {
SBASE[i] = TXBUF_BASE + SSIZE * i;
RBASE[i] = RXBUF_BASE + RSIZE * i;
RSIZE[i] = sizes[i];
uint16_t size = sizes[i] >> 10;
uint8_t log2 = 0;
while (size >>= 1) ++log2;
msr |= log2 << (i * 2);
}
writeRMSR(msr);

RMASK[0] = RSIZE[0] - 1;
RBASE[0] = RXBUF_BASE;
for (int i=1; i<MAX_SOCK_NUM; i++) {
RMASK[i] = RSIZE[i] - 1;
RBASE[i] = RBASE[i-1] + RSIZE[i];
}
}

Expand Down Expand Up @@ -78,13 +115,13 @@ void W5100Class::send_data_processing_offset(SOCKET s, uint16_t data_offset, con
{
uint16_t ptr = readSnTX_WR(s);
ptr += data_offset;
uint16_t offset = ptr & SMASK;
uint16_t offset = ptr & SMASK[s];
uint16_t dstAddr = offset + SBASE[s];

if (offset + len > SSIZE)
if (offset + len > SSIZE[s])
{
// Wrap around circular buffer
uint16_t size = SSIZE - offset;
uint16_t size = SSIZE[s] - offset;
write(dstAddr, data, size);
write(SBASE[s], data + size, len - size);
}
Expand All @@ -109,18 +146,26 @@ void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uin
}
}

void W5100Class::skip(SOCKET s, uint16_t len)
{
uint16_t ptr;
ptr = readSnRX_RD(s);
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_mask = (uint16_t)src & RMASK[s];
src_ptr = RBASE[s] + src_mask;

if( (src_mask + len) > RSIZE )
if( (src_mask + len) > RSIZE[s] )
{
size = RSIZE - src_mask;
size = RSIZE[s] - src_mask;
read(src_ptr, (uint8_t *)dst, size);
dst += size;
read(RBASE[s], (uint8_t *) dst, len - size);
Expand Down
21 changes: 15 additions & 6 deletions Ethernet/utility/w5100.h → utility/w5100.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
typedef uint8_t SOCKET;

//#define W5100_ETHERNET_SHIELD // Arduino Ethenret Shield and Compatibles ...
//#define W5200_ETHERNET_SHIELD // WIZ820io, W5200 Ethernet Shield
#define W5500_ETHERNET_SHIELD // WIZ550io, ioShield series of WIZnet
#define W5200_ETHERNET_SHIELD // WIZ820io, W5200 Ethernet Shield
//#define W5500_ETHERNET_SHIELD // WIZ550io, ioShield series of WIZnet

#if defined(W5500_ETHERNET_SHIELD)
#define WIZ550io_WITH_MACADDRESS // Use assigned MAC address of WIZ550io
Expand Down Expand Up @@ -183,6 +183,13 @@ class W5100Class {
*/
void recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek = 0);

/**
* @brief Moves the Rx read pointer forward
*
* This function allows you to skip a number of bytes in the Rx pointer register.
*/
void skip(SOCKET s, uint16_t len);

inline void setGatewayIp(uint8_t *_addr);
inline void getGatewayIp(uint8_t *_addr);

Expand All @@ -203,6 +210,8 @@ class W5100Class {
uint16_t getTXFreeSize(SOCKET s);
uint16_t getRXReceivedSize(SOCKET s);

void setTXMemorySizes(uint16_t * sizes);
void setRXMemorySizes(uint16_t * sizes);

// W5100 Registers
// ---------------
Expand Down Expand Up @@ -327,12 +336,12 @@ class W5100Class {
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
uint16_t SMASK[SOCKETS]; // Tx buffer MASK
uint16_t RMASK[SOCKETS]; // Rx buffer MASK
public:
static const uint16_t SSIZE = 2048; // Max Tx buffer size
uint16_t SSIZE[SOCKETS]; // Max Tx buffer size
private:
static const uint16_t RSIZE = 2048; // Max Rx buffer size
uint16_t RSIZE[SOCKETS]; // Max Rx buffer size
uint16_t SBASE[SOCKETS]; // Tx buffer base address
uint16_t RBASE[SOCKETS]; // Rx buffer base address

Expand Down
51 changes: 41 additions & 10 deletions Ethernet/utility/w5200.cpp → utility/w5200.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,36 @@ void W5200Class::init(void)

writeMR(1<<RST);

uint16_t sizes[MAX_SOCK_NUM] = {2048,2048,2048,2048,2048,2048,2048,2048};
setTXMemorySizes(sizes);
setRXMemorySizes(sizes);
}

void W5200Class::setTXMemorySizes(uint16_t * sizes)
{
for (int i=0; i<MAX_SOCK_NUM; i++) {
write((0x4000 + i * 0x100 + 0x001F), 2);
write((0x4000 + i * 0x100 + 0x001E), 2);
SSIZE[i] = sizes[i];
write((0x4000 + i * 0x100 + 0x001F), SSIZE[i] >> 10);
}
SMASK[0] = SSIZE[0] - 1;
SBASE[0] = TXBUF_BASE;
for (int i=1; i<MAX_SOCK_NUM; i++) {
SMASK[i] = SSIZE[i] - 1;
SBASE[i] = SBASE[i-1] + SSIZE[i];
}
}

void W5200Class::setRXMemorySizes(uint16_t * sizes)
{
for (int i=0; i<MAX_SOCK_NUM; i++) {
SBASE[i] = TXBUF_BASE + SSIZE * i;
RBASE[i] = RXBUF_BASE + RSIZE * i;
RSIZE[i] = sizes[i];
write((0x4000 + i * 0x100 + 0x001E), RSIZE[i] >> 10);
}
RMASK[0] = RSIZE[0] - 1;
RBASE[0] = RXBUF_BASE;
for (int i=1; i<MAX_SOCK_NUM; i++) {
RMASK[i] = RSIZE[i] - 1;
RBASE[i] = RBASE[i-1] + RSIZE[i];
}
}

Expand Down Expand Up @@ -78,13 +101,13 @@ void W5200Class::send_data_processing_offset(SOCKET s, uint16_t data_offset, con
{
uint16_t ptr = readSnTX_WR(s);
ptr += data_offset;
uint16_t offset = ptr & SMASK;
uint16_t offset = ptr & SMASK[s];
uint16_t dstAddr = offset + SBASE[s];

if (offset + len > SSIZE)
if (offset + len > SSIZE[s])
{
// Wrap around circular buffer
uint16_t size = SSIZE - offset;
uint16_t size = SSIZE[s] - offset;
write(dstAddr, data, size);
write(SBASE[s], data + size, len - size);
}
Expand All @@ -109,18 +132,26 @@ void W5200Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uin
}
}

void W5200Class::skip(SOCKET s, uint16_t len)
{
uint16_t ptr;
ptr = readSnRX_RD(s);
ptr += len;
writeSnRX_RD(s, ptr);
}

void W5200Class::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_mask = (uint16_t)src & RMASK[s];
src_ptr = RBASE[s] + src_mask;

if( (src_mask + len) > RSIZE )
if( (src_mask + len) > RSIZE[s] )
{
size = RSIZE - src_mask;
size = RSIZE[s] - src_mask;
read(src_ptr, (uint8_t *)dst, size);
dst += size;
read(RBASE[s], (uint8_t *) dst, len - size);
Expand Down
Loading