Dear community,
I am using arduino uno + ethernet shield.
Arduino is connected to my laptop over an ethernet cable.
On the laptop I am running a simple server.
From Arduino I establish a connection to that server. In particular, I want to work with interrupts, i.e. I dont want to use EthernetClient but rather socket.cpp and W5100.cpp.
I can establish a connection (I see a TCP Handshake in the wireshark and checking the Socket n Status Register is successful), but I am unable to send the data.
Doing a little bit of debugging I figured out that after executing a send command with:
W5100.execCmdSn(s, Sock_SEND);
an SEND_OK bit in the Socket n Interrupt Register is not set ending up in the endless loop.
I don't know what am I doing wrong, may be some settings are not correct. I would be very thankful for any help or advice. Here is my code (I am using Eclipse with AVR Plugin):
#include <Arduino.h>
#include <SPI.h> // needed for Ethernet library communication with the W5100 (Arduino ver>0018)
#include <Ethernet.h>
#include <utility/w5100.h>
#include <utility/socket.h>
#include <util.h>
#include <avr/interrupt.h>
#include "ownutils.h"
#include "tcp.h"
#define PACKET_SIZE 200
SOCKET s; // our socket 0 that will be opened in RAW mode
byte rbuf[1000]; // receive buffer
byte sbuf[1000]; // send buffer
int rbuflen; // length of data to receive
int sbuflen; // length of data to send
// set the src mac address
uint8_t smac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x46, 0x94 };
// set the dst mac address
uint8_t dmac[] = { 0x00, 0x21, 0xcc, 0x70, 0xf3, 0xae};
uint8_t src_ip[4] = { 192, 168, 0, 2};
uint8_t dst_ip[4] = { 192, 168, 0, 1};
uint8_t subnet_mask[4] = { 255, 255, 255, 0};
uint8_t gateway_ip[4] = { 192, 168, 0, 1};
uint8_t* src_mac = smac;
uint8_t* dst_mac = dmac;
uint8_t* dst_ip_addr = dst_ip;
uint8_t* src_ip_addr = src_ip;
uint16_t src_port = 3333;
uint16_t dst_port = 2222;
uint8_t data = 0x49;
uint8_t* pdata;
uint8_t ledPin = 13;
uint32_t counterValue = 0;
uint8_t sreg;
uint16_t asmcounter = 1;
void setup() {
Serial.begin(115200);
Serial.println("\r\nSetup...");
Serial.print("\r\nInitializing the w5100 chip and open a TCP socket...");
W5100.init(); //
W5100.writeSnMR(s, SnMR::TCP); // set type of socket in Mode Register
W5100.setMACAddress(smac); // set own mac address
W5100.setIPAddress(src_ip_addr); // set own ip address
W5100.writeSnPORT(s, src_port); // set own port (for each socket different)
W5100.setSubnetMask(subnet_mask);
W5100.setGatewayIp(gateway_ip);
W5100.writeSnDHAR(s, dst_mac); // set destination mac address
W5100.writeSnDIPR(s, dst_ip_addr); // set destination ip
W5100.writeSnDPORT(s, dst_port); // set destination port
W5100.writeTMSR(0x55);
W5100.writeRMSR(0x55);
W5100.execCmdSn(s, Sock_OPEN);
Serial.print("\r\nDone opening socket");
W5100.execCmdSn(s, Sock_CONNECT);
if ( (W5100.readSnSR(s) & SnSR:: ESTABLISHED) != SnSR::ESTABLISHED) // check Status Register to find out if the connection was established
Serial.print("\r\nConnection established");
else
Serial.print("\r\nCould not establish connection!!!");
Serial.print("\r\nSending Data...");
uint16_t ret=0;
uint8_t len = 1;
if ( len > W5100.SSIZE)
ret = W5100.SSIZE; // check size not to exceed MAX size.
else
ret = len;
if ( (W5100.readSnSR(s) & SnSR:: ESTABLISHED) != SnSR::ESTABLISHED) // check Status Register to find out if the connection was established
Serial.print("\r\nConnection established");
else
Serial.print("\r\nCould is terminated!!!");
if ( (W5100.readSnSR(s) & SnSR:: CLOSE_WAIT) != SnSR::CLOSE_WAIT) // check Status Register to find out if the connection was established
Serial.print("\r\nStill in touch with the remote peer");
else
Serial.print("\r\nConnection termination request is sent by the remote peer but not disconnected");
Serial.print("\r\n ret=");
Serial.print(ret,10);
W5100.send_data_processing(s, pdata, ret); // before executing the send command we need to fill the TX memory
W5100.execCmdSn(s, Sock_SEND);
Serial.print("\r\nExecute send command...");
uint8_t flag = 1;
/* +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);
Serial.print("\r\nConnection closed my the remote peer");
flag = 0;
break;
}
}
/* +2008.01 bj */
if(flag){
W5100.writeSnIR(s, SnIR::SEND_OK);
Serial.print("\r\nData is sent");
}
}
void loop()
{
}
int main(void) {
init();
setup();
while(true) {
loop();
}
}