|
@@ -3,11 +3,8 @@
|
|
* Contains functions to allow for networking
|
|
* Contains functions to allow for networking
|
|
*
|
|
*
|
|
* Functions from this library can be used to operate up to 8 sockets
|
|
* Functions from this library can be used to operate up to 8 sockets
|
|
-* Application based code should not be in this library
|
|
|
|
*/
|
|
*/
|
|
|
|
|
|
-// uses stdlib.c
|
|
|
|
-
|
|
|
|
// Wiznet W5500 Op Codes
|
|
// Wiznet W5500 Op Codes
|
|
#define WIZNET_WRITE_COMMON 0x04 //opcode to write to one of the common block of registers
|
|
#define WIZNET_WRITE_COMMON 0x04 //opcode to write to one of the common block of registers
|
|
#define WIZNET_READ_COMMON 0x00 //opcode to read one of the common block of registers
|
|
#define WIZNET_READ_COMMON 0x00 //opcode to read one of the common block of registers
|
|
@@ -108,13 +105,14 @@
|
|
#define WIZNET_MAX_RBUF 2048 // buffer for receiving data (max rx packet size!)
|
|
#define WIZNET_MAX_RBUF 2048 // buffer for receiving data (max rx packet size!)
|
|
#define WIZNET_MAX_TBUF 2048 // buffer for sending data (max tx packet size!)
|
|
#define WIZNET_MAX_TBUF 2048 // buffer for sending data (max tx packet size!)
|
|
|
|
|
|
|
|
+#define WIZNET_WAIT_BUFFER_TIMEOUT_MS 1000
|
|
|
|
|
|
//-------------------
|
|
//-------------------
|
|
//BASIC READ AND WRITE FUNCTIONS
|
|
//BASIC READ AND WRITE FUNCTIONS
|
|
//-------------------
|
|
//-------------------
|
|
|
|
|
|
-// Sets SPI3_CS low
|
|
|
|
-void WizSpiBeginTransfer()
|
|
|
|
|
|
+// Set wiznet CS low
|
|
|
|
+void wiz_spi_begin_transfer()
|
|
{
|
|
{
|
|
asm(
|
|
asm(
|
|
"; backup regs\n"
|
|
"; backup regs\n"
|
|
@@ -129,11 +127,11 @@ void WizSpiBeginTransfer()
|
|
"; restore regs\n"
|
|
"; restore regs\n"
|
|
"pop r2\n"
|
|
"pop r2\n"
|
|
"pop r1\n"
|
|
"pop r1\n"
|
|
- );
|
|
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
-// Sets SPI3_CS high
|
|
|
|
-void WizSpiEndTransfer()
|
|
|
|
|
|
+// Set wiznet CS high
|
|
|
|
+void wiz_spi_end_transfer()
|
|
{
|
|
{
|
|
asm(
|
|
asm(
|
|
"; backup regs\n"
|
|
"; backup regs\n"
|
|
@@ -148,13 +146,11 @@ void WizSpiEndTransfer()
|
|
"; restore regs\n"
|
|
"; restore regs\n"
|
|
"pop r2\n"
|
|
"pop r2\n"
|
|
"pop r1\n"
|
|
"pop r1\n"
|
|
- );
|
|
|
|
|
|
+ );
|
|
}
|
|
}
|
|
|
|
|
|
-// write dataByte and return read value
|
|
|
|
-// write 0x00 for a read
|
|
|
|
-// Writes byte over SPI3 to W5500
|
|
|
|
-word WizSpiTransfer(word dataByte)
|
|
|
|
|
|
+// SPI write to wiznet chip
|
|
|
|
+word wiz_spi_transfer(word dataByte)
|
|
{
|
|
{
|
|
word retval = 0;
|
|
word retval = 0;
|
|
asm(
|
|
asm(
|
|
@@ -162,142 +158,136 @@ word WizSpiTransfer(word dataByte)
|
|
"write 0 r2 r4 ; write r4 over SPI3\n"
|
|
"write 0 r2 r4 ; write r4 over SPI3\n"
|
|
"read 0 r2 r2 ; read return value\n"
|
|
"read 0 r2 r2 ; read return value\n"
|
|
"write -4 r14 r2 ; write to stack to return\n"
|
|
"write -4 r14 r2 ; write to stack to return\n"
|
|
- );
|
|
|
|
|
|
+ );
|
|
|
|
|
|
return retval;
|
|
return retval;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
// Write data to W5500
|
|
// Write data to W5500
|
|
-void wizWrite(word addr, word cb, char* buf, word len)
|
|
|
|
|
|
+void wiz_write(word addr, word cb, char *buf, word len)
|
|
{
|
|
{
|
|
- WizSpiBeginTransfer();
|
|
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
|
|
// Send address
|
|
// Send address
|
|
- word addrMSB = (unsigned) addr >> 8;
|
|
|
|
- WizSpiTransfer(addrMSB); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
|
|
+ word addr_msb = (unsigned)addr >> 8;
|
|
|
|
+ wiz_spi_transfer(addr_msb);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
|
|
// Send control byte
|
|
// Send control byte
|
|
- WizSpiTransfer(cb);
|
|
|
|
|
|
+ wiz_spi_transfer(cb);
|
|
|
|
|
|
// Send data
|
|
// Send data
|
|
word i;
|
|
word i;
|
|
for (i = 0; i < len; i++)
|
|
for (i = 0; i < len; i++)
|
|
{
|
|
{
|
|
- WizSpiTransfer(buf[i]);
|
|
|
|
|
|
+ wiz_spi_transfer(buf[i]);
|
|
}
|
|
}
|
|
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
// Write single byte to W5500
|
|
// Write single byte to W5500
|
|
-word wizWriteSingle(word addr, word cb, word data)
|
|
|
|
|
|
+word wiz_write_single(word addr, word cb, word data)
|
|
{
|
|
{
|
|
- WizSpiBeginTransfer();
|
|
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
|
|
// Send address
|
|
// Send address
|
|
- word addrMSB = (unsigned) addr >> 8;
|
|
|
|
- WizSpiTransfer(addrMSB); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
|
|
+ word addr_msb = (unsigned)addr >> 8;
|
|
|
|
+ wiz_spi_transfer(addr_msb);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
|
|
// Send control byte
|
|
// Send control byte
|
|
- WizSpiTransfer(cb);
|
|
|
|
|
|
+ wiz_spi_transfer(cb);
|
|
|
|
|
|
// Send data
|
|
// Send data
|
|
- WizSpiTransfer(data);
|
|
|
|
|
|
+ wiz_spi_transfer(data);
|
|
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
|
|
|
|
return data;
|
|
return data;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
// Write two bytes to W5500
|
|
// Write two bytes to W5500
|
|
-void wizWriteDouble(word addr, word cb, word data)
|
|
|
|
|
|
+void wiz_write_double(word addr, word cb, word data)
|
|
{
|
|
{
|
|
- WizSpiBeginTransfer();
|
|
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
|
|
// Send address
|
|
// Send address
|
|
- word addrMSB = (unsigned) addr >> 8;
|
|
|
|
- WizSpiTransfer(addrMSB); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
|
|
+ word addr_msb = (unsigned)addr >> 8;
|
|
|
|
+ wiz_spi_transfer(addr_msb);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
|
|
// Send control byte
|
|
// Send control byte
|
|
- WizSpiTransfer(cb);
|
|
|
|
|
|
+ wiz_spi_transfer(cb);
|
|
|
|
|
|
// Send data
|
|
// Send data
|
|
- word dataMSB = (unsigned) data >> 8;
|
|
|
|
- WizSpiTransfer(dataMSB);
|
|
|
|
- WizSpiTransfer(data);
|
|
|
|
|
|
+ word dataMSB = (unsigned)data >> 8;
|
|
|
|
+ wiz_spi_transfer(dataMSB);
|
|
|
|
+ wiz_spi_transfer(data);
|
|
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-void wizRead(word addr, word cb, char* buf, word len)
|
|
|
|
-{
|
|
|
|
- WizSpiBeginTransfer();
|
|
|
|
|
|
+void wiz_read(word addr, word cb, char *buf, word len)
|
|
|
|
+{
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
|
|
// Send address
|
|
// Send address
|
|
- word addrMSB = (unsigned) addr >> 8;
|
|
|
|
- WizSpiTransfer(addrMSB); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
|
|
+ word addr_msb = (unsigned)addr >> 8;
|
|
|
|
+ wiz_spi_transfer(addr_msb);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
|
|
// Send control byte
|
|
// Send control byte
|
|
- WizSpiTransfer(cb);
|
|
|
|
|
|
+ wiz_spi_transfer(cb);
|
|
|
|
|
|
// Read data
|
|
// Read data
|
|
word i;
|
|
word i;
|
|
for (i = 0; i < len; i++)
|
|
for (i = 0; i < len; i++)
|
|
{
|
|
{
|
|
- buf[i] = WizSpiTransfer(0);
|
|
|
|
|
|
+ buf[i] = wiz_spi_transfer(0);
|
|
}
|
|
}
|
|
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-word wizReadSingle(word addr, word cb)
|
|
|
|
-{
|
|
|
|
- WizSpiBeginTransfer();
|
|
|
|
|
|
+word wiz_read_single(word addr, word cb)
|
|
|
|
+{
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
|
|
// Send address
|
|
// Send address
|
|
- word addrMSB = (unsigned) addr >> 8;
|
|
|
|
- WizSpiTransfer(addrMSB); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
|
|
+ word addr_msb = (unsigned)addr >> 8;
|
|
|
|
+ wiz_spi_transfer(addr_msb);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
|
|
// Send control byte
|
|
// Send control byte
|
|
- WizSpiTransfer(cb);
|
|
|
|
|
|
+ wiz_spi_transfer(cb);
|
|
|
|
|
|
// Read data
|
|
// Read data
|
|
- word retval = WizSpiTransfer(0);
|
|
|
|
|
|
+ word retval = wiz_spi_transfer(0);
|
|
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
|
|
|
|
// Return read value
|
|
// Return read value
|
|
return retval;
|
|
return retval;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-word wizReadDouble(word addr, word cb)
|
|
|
|
-{
|
|
|
|
- WizSpiBeginTransfer();
|
|
|
|
|
|
+word wiz_read_double(word addr, word cb)
|
|
|
|
+{
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
|
|
// Send address
|
|
// Send address
|
|
- word addrMSB = (unsigned) addr >> 8;
|
|
|
|
- WizSpiTransfer(addrMSB); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
|
|
+ word addr_msb = (unsigned)addr >> 8;
|
|
|
|
+ wiz_spi_transfer(addr_msb);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
|
|
// Send control byte
|
|
// Send control byte
|
|
- WizSpiTransfer(cb);
|
|
|
|
|
|
+ wiz_spi_transfer(cb);
|
|
|
|
|
|
// Read data
|
|
// Read data
|
|
- word retval = WizSpiTransfer(0) << 8;
|
|
|
|
- retval = retval + WizSpiTransfer(0);
|
|
|
|
|
|
+ word retval = wiz_spi_transfer(0) << 8;
|
|
|
|
+ retval = retval + wiz_spi_transfer(0);
|
|
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
|
|
|
|
// Return read value
|
|
// Return read value
|
|
return retval;
|
|
return retval;
|
|
@@ -309,91 +299,88 @@ word wizReadDouble(word addr, word cb)
|
|
//-------------------
|
|
//-------------------
|
|
|
|
|
|
// Send a command cmd to socket s
|
|
// Send a command cmd to socket s
|
|
-void wizCmd(word s, word cmd)
|
|
|
|
|
|
+void wiz_send_cmd(word s, word cmd)
|
|
{
|
|
{
|
|
- //wizWriteSingle(WIZNET_SnCR, WIZNET_WRITE_SnR, cmd);
|
|
|
|
- WizSpiBeginTransfer();
|
|
|
|
- WizSpiTransfer(0); //msByte
|
|
|
|
- WizSpiTransfer(WIZNET_SnCR); //lsByte
|
|
|
|
- WizSpiTransfer(WIZNET_WRITE_SnR + (s << 5));
|
|
|
|
- WizSpiTransfer(cmd);
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
-
|
|
|
|
- // wait untill done
|
|
|
|
- while ( wizReadSingle(WIZNET_SnCR, WIZNET_READ_SnR) );
|
|
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
+ wiz_spi_transfer(0);
|
|
|
|
+ wiz_spi_transfer(WIZNET_SnCR);
|
|
|
|
+ wiz_spi_transfer(WIZNET_WRITE_SnR + (s << 5));
|
|
|
|
+ wiz_spi_transfer(cmd);
|
|
|
|
+ wiz_spi_end_transfer();
|
|
|
|
+
|
|
|
|
+ // Wait until done
|
|
|
|
+ while (wiz_read_single(WIZNET_SnCR, WIZNET_READ_SnR));
|
|
}
|
|
}
|
|
|
|
|
|
// Write 8 bits to a sockets control register
|
|
// Write 8 bits to a sockets control register
|
|
-void wizSetSockReg8(word s, word addr, word val)
|
|
|
|
|
|
+void wiz_set_sock_reg_8(word s, word addr, word val)
|
|
{
|
|
{
|
|
- //wizWriteSingle(addr, WIZNET_WRITE_SnR, val);
|
|
|
|
- WizSpiBeginTransfer();
|
|
|
|
- WizSpiTransfer(0); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
- WizSpiTransfer(WIZNET_WRITE_SnR + (s << 5));
|
|
|
|
- WizSpiTransfer(val);
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
+ wiz_spi_transfer(0);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
+ wiz_spi_transfer(WIZNET_WRITE_SnR + (s << 5));
|
|
|
|
+ wiz_spi_transfer(val);
|
|
|
|
+ wiz_spi_end_transfer();
|
|
}
|
|
}
|
|
|
|
|
|
// Read 8 bits from a sockets control register
|
|
// Read 8 bits from a sockets control register
|
|
-word wizGetSockReg8(word s, word addr){
|
|
|
|
- //return wizReadSingle(addr, WIZNET_READ_SnR);
|
|
|
|
- WizSpiBeginTransfer();
|
|
|
|
|
|
+word wiz_get_sock_reg_8(word s, word addr)
|
|
|
|
+{
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
|
|
// Send address
|
|
// Send address
|
|
- word addrMSB = (unsigned) addr >> 8;
|
|
|
|
- WizSpiTransfer(addrMSB); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
|
|
+ word addr_msb = (unsigned) addr >> 8;
|
|
|
|
+ wiz_spi_transfer(addr_msb);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
|
|
word cb = WIZNET_READ_SnR + (s << 5);
|
|
word cb = WIZNET_READ_SnR + (s << 5);
|
|
-
|
|
|
|
|
|
+
|
|
// Send control byte
|
|
// Send control byte
|
|
- WizSpiTransfer(cb);
|
|
|
|
|
|
+ wiz_spi_transfer(cb);
|
|
|
|
|
|
// Read data
|
|
// Read data
|
|
- word retval = WizSpiTransfer(0);
|
|
|
|
|
|
+ word retval = wiz_spi_transfer(0);
|
|
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
|
|
|
|
// Return read value
|
|
// Return read value
|
|
return retval;
|
|
return retval;
|
|
}
|
|
}
|
|
|
|
|
|
// Write 16 bits to a sockets control register
|
|
// Write 16 bits to a sockets control register
|
|
-void wizSetSockReg16(word s, word addr, word val)
|
|
|
|
|
|
+void wiz_set_sock_reg_16(word s, word addr, word val)
|
|
{
|
|
{
|
|
- //wizWriteDouble(addr, WIZNET_WRITE_SnR + (s << 5), val);
|
|
|
|
- WizSpiBeginTransfer();
|
|
|
|
- WizSpiTransfer(0); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
- WizSpiTransfer(WIZNET_WRITE_SnR + (s << 5));
|
|
|
|
- word valMSB = (unsigned) val >> 8;
|
|
|
|
- WizSpiTransfer(valMSB);
|
|
|
|
- WizSpiTransfer(val);
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
+ wiz_spi_transfer(0);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
+ wiz_spi_transfer(WIZNET_WRITE_SnR + (s << 5));
|
|
|
|
+ word val_msb = (unsigned) val >> 8;
|
|
|
|
+ wiz_spi_transfer(val_msb);
|
|
|
|
+ wiz_spi_transfer(val);
|
|
|
|
+ wiz_spi_end_transfer();
|
|
}
|
|
}
|
|
|
|
|
|
// Read 16 bits from a sockets control register
|
|
// Read 16 bits from a sockets control register
|
|
-word wizGetSockReg16(word s, word addr)
|
|
|
|
|
|
+word wiz_get_sock_reg_16(word s, word addr)
|
|
{
|
|
{
|
|
- //return wizReadDouble(addr, WIZNET_READ_SnR + (s << 5));
|
|
|
|
- WizSpiBeginTransfer();
|
|
|
|
|
|
+ //return wiz_read_double(addr, WIZNET_READ_SnR + (s << 5));
|
|
|
|
+ wiz_spi_begin_transfer();
|
|
|
|
|
|
// Send address
|
|
// Send address
|
|
- word addrMSB = (unsigned) addr >> 8;
|
|
|
|
- WizSpiTransfer(addrMSB); //msByte
|
|
|
|
- WizSpiTransfer(addr); //lsByte
|
|
|
|
|
|
+ word addr_msb = (unsigned) addr >> 8;
|
|
|
|
+ wiz_spi_transfer(addr_msb);
|
|
|
|
+ wiz_spi_transfer(addr);
|
|
|
|
|
|
word cb = WIZNET_READ_SnR + (s << 5);
|
|
word cb = WIZNET_READ_SnR + (s << 5);
|
|
|
|
|
|
// Send control byte
|
|
// Send control byte
|
|
- WizSpiTransfer(cb);
|
|
|
|
|
|
+ wiz_spi_transfer(cb);
|
|
|
|
|
|
// Read data
|
|
// Read data
|
|
- word retval = WizSpiTransfer(0) << 8;
|
|
|
|
- retval = retval + WizSpiTransfer(0);
|
|
|
|
|
|
+ word retval = wiz_spi_transfer(0) << 8;
|
|
|
|
+ retval = retval + wiz_spi_transfer(0);
|
|
|
|
|
|
- WizSpiEndTransfer();
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
|
|
|
|
// Return read value
|
|
// Return read value
|
|
return retval;
|
|
return retval;
|
|
@@ -405,157 +392,142 @@ word wizGetSockReg16(word s, word addr)
|
|
//-------------------
|
|
//-------------------
|
|
|
|
|
|
// Initialize W5500 chip
|
|
// Initialize W5500 chip
|
|
-void wiz_Init(char* ip_addr, char* gateway_addr, char* mac_addr, char* sub_mask)
|
|
|
|
|
|
+void wiz_init(char *ip_addr, char *gateway_addr, char *mac_addr, char *sub_mask)
|
|
{
|
|
{
|
|
- WizSpiEndTransfer();
|
|
|
|
- delay(10);
|
|
|
|
-
|
|
|
|
- wizWrite(WIZNET_SIPR, WIZNET_WRITE_COMMON, ip_addr, 4);
|
|
|
|
- wizWrite(WIZNET_GAR, WIZNET_WRITE_COMMON, gateway_addr, 4);
|
|
|
|
- wizWrite(WIZNET_SHAR, WIZNET_WRITE_COMMON, mac_addr, 6);
|
|
|
|
- wizWrite(WIZNET_SUBR, WIZNET_WRITE_COMMON, sub_mask, 4);
|
|
|
|
|
|
+ wiz_spi_end_transfer();
|
|
|
|
+ wiz_write(WIZNET_SIPR, WIZNET_WRITE_COMMON, ip_addr, 4);
|
|
|
|
+ wiz_write(WIZNET_GAR, WIZNET_WRITE_COMMON, gateway_addr, 4);
|
|
|
|
+ wiz_write(WIZNET_SHAR, WIZNET_WRITE_COMMON, mac_addr, 6);
|
|
|
|
+ wiz_write(WIZNET_SUBR, WIZNET_WRITE_COMMON, sub_mask, 4);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-// Initialize socket s for TCP
|
|
|
|
-void wizInitSocketTCP(word s, word port)
|
|
|
|
|
|
+// Initialize socket s for TCP host mode
|
|
|
|
+void wiz_init_socket_tcp_host(word s, word port)
|
|
{
|
|
{
|
|
- wizCmd(s, WIZNET_CR_CLOSE);
|
|
|
|
- wizSetSockReg8 (s, WIZNET_SnIR, 0xFF); //reset interrupt register
|
|
|
|
- wizSetSockReg8 (s, WIZNET_SnMR, WIZNET_MR_TCP); //set mode register to tcp
|
|
|
|
- wizSetSockReg16 (s, WIZNET_SnPORT, port); //set tcp port
|
|
|
|
- wizCmd(s, WIZNET_CR_OPEN);
|
|
|
|
- wizCmd(s, WIZNET_CR_LISTEN);
|
|
|
|
- delay(10); //wait a bit to make sure the socket is in the correct state (technically not necessary)
|
|
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_CLOSE);
|
|
|
|
+ wiz_set_sock_reg_8(s, WIZNET_SnIR, 0xFF); // Reset interrupt register
|
|
|
|
+ wiz_set_sock_reg_8(s, WIZNET_SnMR, WIZNET_MR_TCP); // Set mode register to tcp
|
|
|
|
+ wiz_set_sock_reg_16(s, WIZNET_SnPORT, port); // Set tcp port
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_OPEN);
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_LISTEN);
|
|
|
|
+ while (wiz_get_sock_reg_8(s, WIZNET_SnSR) != WIZNET_SOCK_LISTEN);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-// Initialize socket s for TCP client
|
|
|
|
-void wizInitSocketTCPClient(word s, word port)
|
|
|
|
|
|
+// Initialize socket s for TCP client mode
|
|
|
|
+void wiz_init_socket_tcp_client(word s, word port)
|
|
{
|
|
{
|
|
- wizCmd(s, WIZNET_CR_CLOSE);
|
|
|
|
- wizSetSockReg8 (s, WIZNET_SnIR, 0xFF); //reset interrupt register
|
|
|
|
- wizSetSockReg8 (s, WIZNET_SnMR, WIZNET_MR_TCP); //set mode register to tcp
|
|
|
|
- wizSetSockReg16 (s, WIZNET_SnPORT, port); //set tcp port
|
|
|
|
- wizCmd(s, WIZNET_CR_OPEN);
|
|
|
|
- delay(10); //wait a bit to make sure the socket is in the correct state (technically not necessary)
|
|
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_CLOSE);
|
|
|
|
+ wiz_set_sock_reg_8(s, WIZNET_SnIR, 0xFF); // Reset interrupt register
|
|
|
|
+ wiz_set_sock_reg_8(s, WIZNET_SnMR, WIZNET_MR_TCP); // Set mode register to tcp
|
|
|
|
+ wiz_set_sock_reg_16(s, WIZNET_SnPORT, port); // Set tcp port
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_OPEN);
|
|
|
|
+ while (wiz_get_sock_reg_8(s, WIZNET_SnSR) != WIZNET_SOCK_INIT);
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
//-------------------
|
|
//-------------------
|
|
//W5500 READING AND WRITING FUNCTIONS
|
|
//W5500 READING AND WRITING FUNCTIONS
|
|
//-------------------
|
|
//-------------------
|
|
|
|
|
|
-
|
|
|
|
-// from memory, so no need for unsigned comparisons (because we have less than 2GB RAM)
|
|
|
|
-word wizWriteDataFromMemory(word s, char* buf, word buflen)
|
|
|
|
|
|
+// Write data from buf of length buflen to socket s
|
|
|
|
+// Returns 1 if successful, 0 if not
|
|
|
|
+word wiz_write_data(word s, char *buf, word buflen)
|
|
{
|
|
{
|
|
// Make sure there is something to send
|
|
// Make sure there is something to send
|
|
- if (buflen <= 0)
|
|
|
|
|
|
+ if (buflen == 0)
|
|
{
|
|
{
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
- word bytesSent = 0;
|
|
|
|
|
|
+ word bytes_sent = 0;
|
|
|
|
|
|
- // loop until all bytes are sent
|
|
|
|
- while (bytesSent != buflen)
|
|
|
|
|
|
+ // Loop until all bytes are sent
|
|
|
|
+ while (bytes_sent != buflen)
|
|
{
|
|
{
|
|
-
|
|
|
|
- if (wizGetSockReg8(s, WIZNET_SnSR) == WIZNET_SOCK_CLOSED)
|
|
|
|
|
|
+ if (wiz_get_sock_reg_8(s, WIZNET_SnSR) == WIZNET_SOCK_CLOSED)
|
|
{
|
|
{
|
|
- //uprintln("connection closed");
|
|
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
|
|
- word partToSend = buflen - bytesSent;
|
|
|
|
- if (partToSend > WIZNET_MAX_TBUF)
|
|
|
|
- partToSend = WIZNET_MAX_TBUF;
|
|
|
|
-
|
|
|
|
- // Make sure there is room in the transmit buffer for what we want to send
|
|
|
|
- word txfree = wizGetSockReg16(s, WIZNET_SnTX_FSR); // Size of the available buffer area
|
|
|
|
|
|
+ // Send in chunks of WIZNET_MAX_TBUF
|
|
|
|
+ word part_to_send = buflen - bytes_sent;
|
|
|
|
+ if (part_to_send > WIZNET_MAX_TBUF)
|
|
|
|
+ {
|
|
|
|
+ part_to_send = WIZNET_MAX_TBUF;
|
|
|
|
+ }
|
|
|
|
|
|
|
|
+ // Wait until there is room in the transmit buffer for what we want to send
|
|
|
|
+ word txfree = wiz_get_sock_reg_16(s, WIZNET_SnTX_FSR);
|
|
word timeout = 0;
|
|
word timeout = 0;
|
|
- while (txfree < partToSend)
|
|
|
|
|
|
+ while (txfree < part_to_send)
|
|
{
|
|
{
|
|
- timeout++; // Increase timeout counter
|
|
|
|
- delay(1); // Wait a bit
|
|
|
|
- txfree = wizGetSockReg16(s, WIZNET_SnTX_FSR); // Size of the available buffer area
|
|
|
|
-
|
|
|
|
- // After a second
|
|
|
|
- if (timeout > 1000)
|
|
|
|
|
|
+ timeout++;
|
|
|
|
+ delay(1);
|
|
|
|
+ txfree = wiz_get_sock_reg_16(s, WIZNET_SnTX_FSR);
|
|
|
|
+
|
|
|
|
+ if (timeout > WIZNET_WAIT_BUFFER_TIMEOUT_MS)
|
|
{
|
|
{
|
|
- wizCmd(s, WIZNET_CR_DISCON); // Disconnect the connection
|
|
|
|
- //uprintln("timeout");
|
|
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_DISCON); // Disconnect
|
|
return 0;
|
|
return 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
- // Space is available so we will send the buffer
|
|
|
|
- word txwr = wizGetSockReg16(s, WIZNET_SnTX_WR); // Read the Tx Write Pointer
|
|
|
|
|
|
+ // Read the Tx Write Pointer
|
|
|
|
+ word txwr = wiz_get_sock_reg_16(s, WIZNET_SnTX_WR);
|
|
|
|
|
|
// Write the outgoing data to the transmit buffer
|
|
// Write the outgoing data to the transmit buffer
|
|
- wizWrite(txwr, WIZNET_WRITE_SnTX + (s << 5), buf + bytesSent, partToSend);
|
|
|
|
|
|
+ wiz_write(txwr, WIZNET_WRITE_SnTX + (s << 5), buf + bytes_sent, part_to_send);
|
|
|
|
|
|
- // update the buffer pointer
|
|
|
|
- word newSize = txwr + partToSend;
|
|
|
|
- wizSetSockReg16(s, WIZNET_SnTX_WR, newSize);
|
|
|
|
|
|
+ // Update the buffer pointer
|
|
|
|
+ word newSize = txwr + part_to_send;
|
|
|
|
+ wiz_set_sock_reg_16(s, WIZNET_SnTX_WR, newSize);
|
|
|
|
|
|
- // Now Send the SEND command which tells the wiznet the pointer is updated
|
|
|
|
- wizCmd(s, WIZNET_CR_SEND);
|
|
|
|
|
|
+ // Now send the SEND command which tells the wiznet the pointer is updated
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_SEND);
|
|
|
|
|
|
// Update the amount of bytes sent
|
|
// Update the amount of bytes sent
|
|
- bytesSent += partToSend;
|
|
|
|
|
|
+ bytes_sent += part_to_send;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
return 1;
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
-// Read received data
|
|
|
|
-word wizReadRecvData(word s, char* buf, word buflen)
|
|
|
|
|
|
+// Read received data on socket s of length buflen to buf
|
|
|
|
+void wiz_read_recv_data(word s, char *buf, word buflen)
|
|
{
|
|
{
|
|
if (buflen == 0)
|
|
if (buflen == 0)
|
|
{
|
|
{
|
|
- return 1;
|
|
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
if (buflen > WIZNET_MAX_RBUF) // If the request size > WIZNET_MAX_RBUF, truncate it to prevent overflow
|
|
if (buflen > WIZNET_MAX_RBUF) // If the request size > WIZNET_MAX_RBUF, truncate it to prevent overflow
|
|
{
|
|
{
|
|
- //uprintln("W: Received too large TCP data");
|
|
|
|
- buflen = WIZNET_MAX_RBUF; // - 1; // -1 Because room for 0 terminator
|
|
|
|
|
|
+ buflen = WIZNET_MAX_RBUF;
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+
|
|
// Get the address where the wiznet is holding the data
|
|
// Get the address where the wiznet is holding the data
|
|
- word rxrd = wizGetSockReg16(s, WIZNET_SnRX_RD);
|
|
|
|
|
|
+ word rxrd = wiz_get_sock_reg_16(s, WIZNET_SnRX_RD);
|
|
|
|
|
|
// Read the data into the buffer
|
|
// Read the data into the buffer
|
|
- wizRead(rxrd, WIZNET_READ_SnRX + (s << 5), buf, buflen);
|
|
|
|
|
|
+ wiz_read(rxrd, WIZNET_READ_SnRX + (s << 5), buf, buflen);
|
|
|
|
|
|
// Remove read data from rxbuffer to make space for new data
|
|
// Remove read data from rxbuffer to make space for new data
|
|
word nsize = rxrd + buflen;
|
|
word nsize = rxrd + buflen;
|
|
- wizSetSockReg16(s, WIZNET_SnRX_RD, nsize); //replace read data pointer
|
|
|
|
- //tell the wiznet we have retrieved the data
|
|
|
|
- wizCmd(s, WIZNET_CR_RECV);
|
|
|
|
|
|
+ wiz_set_sock_reg_16(s, WIZNET_SnRX_RD, nsize); // replace read data pointer
|
|
|
|
+ // Tell the wiznet we have retrieved the data
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_RECV);
|
|
|
|
|
|
- // Terminate buffer for printing in case the data was a string
|
|
|
|
- *(buf + buflen) = 0;
|
|
|
|
-
|
|
|
|
- return 1;
|
|
|
|
|
|
+ return;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
// Remove the received data
|
|
// Remove the received data
|
|
-void wizFlush(word s, word rsize)
|
|
|
|
|
|
+void wiz_flush(word s, word rsize)
|
|
{
|
|
{
|
|
if (rsize > 0)
|
|
if (rsize > 0)
|
|
{
|
|
{
|
|
- word rxrd = wizGetSockReg16(s, WIZNET_SnRX_RD); //retrieve read data pointer
|
|
|
|
|
|
+ word rxrd = wiz_get_sock_reg_16(s, WIZNET_SnRX_RD); // Retrieve read data pointer
|
|
word nsize = rxrd + rsize;
|
|
word nsize = rxrd + rsize;
|
|
- wizSetSockReg16(s, WIZNET_SnRX_RD, nsize); //replace read data pointer
|
|
|
|
- //tell the wiznet we have retrieved the data
|
|
|
|
- wizCmd(s, WIZNET_CR_RECV);
|
|
|
|
|
|
+ wiz_set_sock_reg_16(s, WIZNET_SnRX_RD, nsize); // Replace read data pointer
|
|
|
|
+ // Tell the wiznet we have retrieved the data
|
|
|
|
+ wiz_send_cmd(s, WIZNET_CR_RECV);
|
|
}
|
|
}
|
|
}
|
|
}
|