ntruchsess / arduino_uip

UIPEthernet: A plugin-replacement of the stock Arduino Ethernet library for ENC28J60 shields and breakout boards. Full support for persistent (streaming) TCP-connections and UDP (Client and Server each), ARP, ICMP, DHCP and DNS. Build around Adam Dunkels uIP Stack. Further developed version can be found on https://github.com/UIPEthernet/UIPEthernet
489 stars 211 forks source link

simple UIPEthernet EchoServer example do not work #39

Closed pat1 closed 10 years ago

pat1 commented 10 years ago

I get the last git for arduino 1.05 with this diff:

git diff 
diff --git a/UIPEthernet.cpp b/UIPEthernet.cpp
index ca59e93..30f527b 100644
--- a/UIPEthernet.cpp
+++ b/UIPEthernet.cpp
@@ -403,7 +403,7 @@ UIPEthernetClass::upper_layer_chksum(uint8_t proto)
       Serial.print(UIP_IPH_LEN + UIP_LLH_LEN + upper_layer_memlen);
       Serial.print(F("-"));
       Serial.print(UIP_IPH_LEN + UIP_LLH_LEN + upper_layer_len);
-      Serial.print(F"]: "));
+      Serial.print(F("]: "));
       Serial.println(htons(sum),HEX);
 #endif
     }
diff --git a/UIPEthernet.h b/UIPEthernet.h
index 94502f1..17d91a9 100644
--- a/UIPEthernet.h
+++ b/UIPEthernet.h
@@ -35,10 +35,10 @@ extern "C"
 #include "utility/uip.h"
 }

-//#define UIPETHERNET_DEBUG
-//#define UIPETHERNET_DEBUG_CHKSUM
-//#define UIPETHERNET_DEBUG_UDP
-//#define UIPETHERNET_DEBUG_CLIENT
+#define UIPETHERNET_DEBUG
+#define UIPETHERNET_DEBUG_CHKSUM
+#define UIPETHERNET_DEBUG_UDP
+#define UIPETHERNET_DEBUG_CLIENT

 #define UIPETHERNET_FREEPACKET 1
 #define UIPETHERNET_SENDPACKET 2

This is like the EchoServer example program:

#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPClient.h>

EthernetServer server = EthernetServer(1000);

void setup()
{
  Serial.begin(9600);
  Serial.println("Started");

  uint8_t mac[6] = {0x00,0x01,0x02,0x03,0x04,0x05};
  IPAddress myIP(192,168,1,6);

  Ethernet.begin(mac,myIP);

  server.begin();
  Serial.println("Started server");
}

void loop()
{
  size_t size;
  if (EthernetClient client = server.available())
    {
      if (client)
        {
          while((size = client.available()) > 0)
            {
              uint8_t* msg = (uint8_t*)malloc(size);
              client.read(msg,size);
              Serial.write(msg,size);
              //client.write(msg,size);
          free(msg);
            }
        }
    }
}

the size and memory seems ok:

avr-size -C --mcu=atmega328p .build/nano328/firmware.elf
AVR Memory Usage
----------------
Device: atmega328p
Program:   23300 bytes (71.1% Full)
(.text + .data + .bootloader)

Data:       1227 bytes (59.9% Full)
(.data + .bss + .noinit)

Run it and I get this in the serial terminal:

Terminal ready
Started
Started server
--------------
              receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: FFFF
chksum uip_buf[34-54]: 9B3D
network_send uip_buf (uip_len): 54, packet: 1
freeing packet: 255
--------------
              receivePacket: 255
readPacket type IP, uip_len: 74
chksum uip_buf[34-74]: FFFF
chksum uip_buf[34-58]: 5D93
network_send uip_buf (uip_len): 58, packet: 1
freeing packet: 255
--------------
              receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: FFFF
UIPClient uip_connected
UIPClient allocated state: 16
freeing packet: 255
--------------
              receivePacket: 255
readPacket type IP, uip_len: 62
chksum uip_buf[34-54]: B9C2
chksum uip_packet(255)[54-62]: FFFF
UIPClient uip_newdata, uip_len:8
chksum uip_buf[34-54]: 5C7F
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 1 0 0 0 0 
ciccio
--------------
              receivePacket: 255
readPacket type IP, uip_len: 61
chksum uip_buf[34-54]: 2DCE
chksum uip_packet(255)[54-61]: FFFF
UIPClient uip_newdata, uip_len:7
chksum uip_buf[34-54]: 637F
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0 
eatblock(1): 1 1 0 0 0 -> 1 1 0 0 0
.... 

and so on for infinite time

ntruchsess commented 10 years ago

the diff above is somewhat scrambled by github markup interpreting the code. Would you be able to just create a branch in your UIPEthernet-fork, commit the code there and link hier in this issue?

pat1 commented 10 years ago

sorry, the diif format is usally for me ... I have uncommented those lines in UIPEthernet.cpp

define UIPETHERNET_DEBUG

define UIPETHERNET_DEBUG_CHKSUM

define UIPETHERNET_DEBUG_UDP

define UIPETHERNET_DEBUG_CLIENT

and correct a wroang sintax line

ntruchsess commented 10 years ago

the diff-format is not a problem by itself, but if you just paste code into comment-fields github tries to interpret this as markup which results in unusuable code like these three includes that have the filename stripped:

include

include

include

As a result I cannot just copy your code to my IDE and execute, but have to manually correct it. Having to correct a diff before it can be applied doesn't help a lot. (Your second comment that summarizes your changes does a better job). Will check later on real hardware though...

ntruchsess commented 10 years ago

ok, checked with latest master, but cannot reproduce. You have to be a bit careful with this example not to send to much data in a single chunk as the malloc might corrupt memory if you happen to allocate to much. (Ok, your log-output indicates you just sent 'ciccio' which shouldn't be to much though...) I did merge fix #44 today, possible this makes a difference though.

Started
Started server
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: FFFF
chksum uip_buf[34-54]: CF84
network_send uip_buf (uip_len): 54, packet: 1
freeing packet: 255
--------------
receivePacket: 255
readPacket type IP, uip_len: 74
chksum uip_buf[34-74]: FFFF
chksum uip_buf[34-58]: 40E1
network_send uip_buf (uip_len): 58, packet: 1
freeing packet: 255
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: FFFF
UIPClient uip_connected
UIPClient::all_data[0], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[1], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[2], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[3], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient allocated state: 10000
freeing packet: 255
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: 212F
chksum uip_packet(255)[54-60]: FFFF
UIPClient uip_newdata, uip_len:6
chksum uip_buf[34-54]: 3DCD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
blah
--------------
receivePacket: 255
readPacket type IP, uip_len: 61
chksum uip_buf[34-54]: 23BC
chksum uip_packet(255)[54-61]: FFFF
UIPClient uip_newdata, uip_len:7
chksum uip_buf[34-54]: 44CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
blubb
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: 1C0B
chksum uip_packet(255)[54-60]: FFFF
UIPClient uip_newdata, uip_len:6
chksum uip_buf[34-54]: 4ACD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
test
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: 231C
chksum uip_packet(255)[54-60]: FFFF
UIPClient uip_newdata, uip_len:6
chksum uip_buf[34-54]: 50CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
nohc
--------------
receivePacket: 255
readPacket type IP, uip_len: 67
chksum uip_buf[34-54]: DD71
chksum uip_packet(255)[54-67]: FFFF
UIPClient uip_newdata, uip_len:13
chksum uip_buf[34-54]: 5DCD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
ahjovgflhis
--------------
receivePacket: 255
readPacket type IP, uip_len: 66
chksum uip_buf[34-54]: 8993
chksum uip_packet(255)[54-66]: FFFF
UIPClient uip_newdata, uip_len:12
chksum uip_buf[34-54]: 69CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
lashgökas
--------------
receivePacket: 255
readPacket type IP, uip_len: 62
chksum uip_buf[34-54]: AABD
chksum uip_packet(255)[54-62]: FFFF
UIPClient uip_newdata, uip_len:8
chksum uip_buf[34-54]: 71CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
aolsgh
--------------
receivePacket: 255
readPacket type IP, uip_len: 63
chksum uip_buf[34-54]: B150
chksum uip_packet(255)[54-63]: FFFF
UIPClient uip_newdata, uip_len:9
chksum uip_buf[34-54]: 7ACD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
laewghl
--------------
receivePacket: 255
readPacket type IP, uip_len: 62
chksum uip_buf[34-54]: B8B6
chksum uip_packet(255)[54-62]: FFFF
UIPClient uip_newdata, uip_len:8
chksum uip_buf[34-54]: 82CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
dslakh
--------------
receivePacket: 255
readPacket type IP, uip_len: 65
chksum uip_buf[34-54]: 38E3
chksum uip_packet(255)[54-65]: FFFF
UIPClient uip_newdata, uip_len:11
chksum uip_buf[34-54]: 8DCD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
ewlkhqaew
--------------
receivePacket: 255
readPacket type IP, uip_len: 62
chksum uip_buf[34-54]: C1BC
chksum uip_packet(255)[54-62]: FFFF
UIPClient uip_newdata, uip_len:8
chksum uip_buf[34-54]: 95CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
aklahg
--------------
receivePacket: 255
readPacket type IP, uip_len: 66
chksum uip_buf[34-54]: DAE5
chksum uip_packet(255)[54-66]: FFFF
UIPClient uip_newdata, uip_len:12
chksum uip_buf[34-54]: A1CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
wkigfldhas
--------------
receivePacket: 255
readPacket type IP, uip_len: 68
chksum uip_buf[34-54]: 7C7E
chksum uip_packet(255)[54-68]: FFFF
UIPClient uip_newdata, uip_len:14
chksum uip_buf[34-54]: AFCD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
asldkflajsdf
--------------
receivePacket: 255
readPacket type IP, uip_len: 66
chksum uip_buf[34-54]: DAF7
chksum uip_packet(255)[54-66]: FFFF
UIPClient uip_newdata, uip_len:12
chksum uip_buf[34-54]: BBCD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
asdlfadsjf
--------------
receivePacket: 255
readPacket type IP, uip_len: 65
chksum uip_buf[34-54]: 3FDF
chksum uip_packet(255)[54-65]: FFFF
UIPClient uip_newdata, uip_len:11
chksum uip_buf[34-54]: C6CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
whellkerh
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: B22
chksum uip_packet(255)[54-60]: FFFF
UIPClient uip_newdata, uip_len:6
chksum uip_buf[34-54]: CCCD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
huhu
--------------
receivePacket: 255
readPacket type IP, uip_len: 62
chksum uip_buf[34-54]: B9C2
chksum uip_packet(255)[54-62]: FFFF
UIPClient uip_newdata, uip_len:8
chksum uip_buf[34-54]: D4CD
network_send uip_buf (uip_len): 54, packet: 2
freeing packet: 255
eatblock(1): 1 0 0 0 0 -> 0 0 0 0 0 
ciccio
--------------
receivePacket: 255
readPacket type ARP, uip_len: 60
network_send uip_buf (uip_len): 42, packet: 1
freeing packet: 255
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: FFFF
UIPClient uip_closed
UIPClient::all_data[0], state:10000
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[1], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[2], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[3], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
after UIPClient uip_closed
UIPClient::all_data[0], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[1], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[2], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
UIPClient::all_data[3], state:0
packets_in: 0 0 0 0 0 
packets_out: 0 0 0 0 0 
out_pos: 0
chksum uip_buf[34-54]: D6CD
network_send uip_buf (uip_len): 54, packet: 1
freeing packet: 255
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: FFFF
chksum uip_buf[34-54]: D5CD
network_send uip_buf (uip_len): 54, packet: 1
freeing packet: 255
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: FFFF
freeing packet: 255
--------------
receivePacket: 255
readPacket type IP, uip_len: 60
chksum uip_buf[34-54]: FFFF
chksum uip_buf[34-54]: EC3D
network_send uip_buf (uip_len): 54, packet: 1
freeing packet: 255
pat1 commented 10 years ago

I have tried with arduino mega 2560 with this wiring http://en.code-bude.net/2013/06/22/how-to-use-enc28j60-ethernet-shield-with-arduino-mega-2560/ and I get he same wrong result as with arduino uno. With ethercard and arduino uno (pin 10 changed with pin 8) I have no problems. I think there is somethings wrong in my tests. Which hardware do you use ? And gcc version ? avr-gcc (Fedora 4.8.2-1.fc19) 4.8.2 Thanks for spending time in this project !

ntruchsess commented 10 years ago

I use the gcc that is includet with Arduino IDE:

/arduino-1.0.5/hardware/tools/avr/bin/avr-g++ --version
avr-g++ (GCC) 4.3.2

Arduino 1.0.5 generates these parameters for g++:

/arduino-1.0.5/hardware/tools/avr/bin/avr-g++ -c -g -Os -Wall -fno-exceptions -ffunction-sections -fdata-sections -mmcu=atmega328p -DF_CPU=16000000L -MMD -DUSB_VID=null -DUSB_PID=null -DARDUINO=105 -I/arduino-1.0.5/hardware/arduino/cores/arduino -I/home/arduino-1.0.5/hardware/arduino/variants/standard /tmp/build7944688409721316257.tmp/sketch_feb11a.cpp -o /tmp/build7944688409721316257.tmp/sketch_feb11a.cpp.o 

On Software-side I use both Arduino 1.0.5 and Arduino 1.5.5 (on Ubuntu 13.10), hardware is Uno R3, Mega2560, Due and Nano. On the mega CS is pin 53 by default (if not configured different in Enc28J60Ethernet.h), but as your log indicates it can talk to the enc28J60 receiving packets. It seems to be this line https://github.com/ntruchsess/arduino_uip/blob/master/UIPClient.cpp#L534 in UIPClient::_eatblock()-method that behaves differently on your side. Maybe this is due to different compiler-version - we would have to check and compare the generated machine-instructions for that.

nilrog commented 10 years ago

If _eatblock() is suspected then it could very well be issue #34 that is the culprit. It's behavior is undefined and thus depends on the compiler. So what works in one compiler might break in another compiler.

pat1 commented 10 years ago

to have a confirm I would try avr-g++ 4.3.2 and get the test working ... or if you prefer you can send me the binary for arduino Uno R3 or Mega2560. If the test pass we can try to compare the assembler from elf files ...

pat1 commented 10 years ago

Pull Request #46 fix the problem for simple UIPEthernet EchoServer example and gcc 4.8. Good. I have others warnings reported at #34 #35 #36 #37 #38 that should be the cause of others ploblems I have in other applications.

nilrog commented 10 years ago

I have patches for all the other compiler warnings ready that I can submit when i'm back home tonight. I have been running my sketch with those patches over night and they seem to work ok.