Index: firmware/FSC/src/w5100_spi_interface.c
===================================================================
--- firmware/FSC/src/w5100_spi_interface.c	(revision 10911)
+++ firmware/FSC/src/w5100_spi_interface.c	(revision 11667)
@@ -12,38 +12,50 @@
 // INTERFACE CONTROL VARS
 
-bool w5100_needs_reset = true;
 bool w5100_needs_init = true;
 bool w5100_ready = false;
 
+U16 last_samurai = 0x0000;
+
 U08 w5100_caretaker() {
 U08 socket_status;
-	if (w5100_needs_reset)
-		w5100_reset();
-		
+	
 	socket_status = w5100_sock_status();
+	//usart_write_str((pU08)"ss:");
+	//usart_write_U08_hex(socket_status);
+	//usart_write_char(' ');
 	
 	if (socket_status == SR_SOCK_ESTABLISHED) {
 		w5100_ready = true;
-		
 		// everything is fine... 
-		return 0;
-	}
-	else if(socket_status < SR_SOCK_ESTABLISHED) {
-	// it might be LISTENING ... or not even OPEN
-		if (socket_status == SR_SOCK_CLOSED) {
+	}
+	else if (socket_status == SR_SOCK_LISTEN) { 
+		w5100_ready = false;
+		// wait for connection
+	}
+	else {
+		socket_status = w5100_sock_status();
+		//usart_write_str((pU08)"ss2:");
+		//usart_write_U08_hex(socket_status);
+		//usart_write_char(' ');
+			
+		if (socket_status == SR_SOCK_ESTABLISHED) {
+		w5100_ready = true;
+		// everything is fine... 
+		}
+		else if (socket_status == SR_SOCK_LISTEN) { 
+		w5100_ready = false;
+		// wait for connection
+		}
+		else
+		{
+		// this should never happen
+		w5100_ready = false;
+			w5100_reset();
 			w5100_init();
 		}
-		if (socket_status == SR_SOCK_INIT) {
-			w5100_init();
-		}
-		if (socket_status == SR_SOCK_LISTEN) {
-			
-		}
-	}	
-	else { // all other socket states need a reset
-		w5100_needs_reset = true;
-	}
-w5100_ready = false;
-return 1;
+	}
+	
+	//usart_write_crlf();
+return socket_status;
 }
 
@@ -54,5 +66,4 @@
 	PORTB |= 1<<PB2; //#reset = HIGH --> W5100 is active
 	_delay_ms(5);		// give it 5ms to accomodate.
-	w5100_needs_reset = false;
 }
 
@@ -165,6 +176,6 @@
 {
 U16 freesize;
-freesize=w5100_read(S0_TX_FSR0);
-freesize = freesize << 8;
+freesize=w5100_read(S0_TX_FSR0);		// datasheet: user should read upper byte first, and lower byte later
+freesize = freesize << 8;				// 				to get correct value
 freesize += w5100_read(S0_TX_FSR1);
 return freesize;
@@ -195,8 +206,24 @@
 {
 U16 received_size;
-received_size=w5100_read(S0_RX_RSR0);
-received_size = received_size << 8;
-received_size += w5100_read(S0_RX_RSR1);   // S0_RX_RSR1 is the least significant byte ... 
-return received_size;
+U08 highbyte, lowbyte;
+	highbyte = w5100_read(S0_RX_RSR0);
+	lowbyte = w5100_read(S0_RX_RSR1);
+	received_size = (highbyte<<8) | lowbyte;
+	
+//	usart_write_str((pU08)"hi:");
+//	usart_write_U08_hex(highbyte);
+//	usart_write_crlf();
+//	
+//	usart_write_str((pU08)"lo:");
+//	usart_write_U08_hex(lowbyte);
+//	usart_write_crlf();
+//	
+//	usart_write_str((pU08)"total:");
+//	usart_write_U16_hex(received_size);
+//	usart_write_crlf();
+	
+//	_delay_ms(200);
+	
+	return received_size;
 }
 
@@ -205,5 +232,5 @@
 {
 U16 readpointer;
-readpointer=w5100_read(S0_RX_RD0);
+readpointer=w5100_read(S0_RX_RD0);		
 readpointer = readpointer << 8;
 readpointer += w5100_read(S0_RX_RD1);
@@ -217,7 +244,6 @@
 U08 high_byte = (value>>8);
 U08 low_byte = value&0x00FF;
+w5100_write(S0_TX_WR0, high_byte);
 w5100_write(S0_TX_WR1, low_byte);
-w5100_write(S0_TX_WR0, high_byte);
-
 }
 
@@ -227,5 +253,4 @@
 U08 high_byte = (value>>8);
 U08 low_byte = value&0x00FF;
-
 w5100_write(S0_RX_RD0, high_byte);
 w5100_write(S0_RX_RD1, low_byte);
@@ -255,4 +280,15 @@
 	U16 start_address =  S0_RX_BASE + offset;
 
+	usart_write_str((pU08)"lrp:");
+	usart_write_U16_hex(last_RX_read_pointer);
+	usart_write_crlf();
+	usart_write_str((pU08)"off:");
+	usart_write_U16_hex(offset);
+	usart_write_crlf();
+	usart_write_str((pU08)"sad:");
+	usart_write_U16_hex(start_address);
+	usart_write_crlf();
+	
+	
 	if ((offset + NumBytes) > (S0_RX_MASK + 1) )  // if data is turned over in RX-mem
 	{
@@ -277,5 +313,10 @@
 
 	// inform W5100 about how much data was read out.
-	set_S0_RX_RD(last_RX_read_pointer + NumBytes);
+	U16 new_read_pointer =  last_RX_read_pointer + NumBytes;
+	usart_write_str((pU08)"nrp:");
+	usart_write_U16_hex(new_read_pointer);
+	usart_write_crlf();
+	
+	set_S0_RX_RD(new_read_pointer);
 	w5100_write ( S0_CR, CR_RECV );
 	
@@ -305,5 +346,22 @@
 U16 w5100_set_TX(U08* string, U16 NumBytes) {
 
-	U16 freesize = get_S0_TX_FSR();
+	U08 repetitions;
+	U16 freesize;
+	U16 last_TX_write_pointer;
+	
+	//usart_write_str((pU08)"S0_IR:");
+	//usart_write_U08_hex( w5100_read(S0_IR) );
+	//usart_write_crlf();
+	
+	freesize = get_S0_TX_FSR();
+	for (repetitions=0 ; repetitions < 3;  ) // increase is *inside the loop* 
+	{
+		if ( freesize != get_S0_TX_FSR()) {
+			 freesize = get_S0_TX_FSR();
+		} else {
+			repetitions++;
+		}
+	}
+	
 	if (freesize == 0)
 	{
@@ -311,21 +369,39 @@
 	}
 	
-	U16 last_TX_write_pointer = get_S0_TX_WR();
+	last_TX_write_pointer = get_S0_TX_WR();
+	for (repetitions=0 ; repetitions < 3;  ) // increase is *inside the loop* 
+	{
+		if ( last_TX_write_pointer != get_S0_TX_WR()) {
+			 last_TX_write_pointer = get_S0_TX_WR();
+		} else {
+			repetitions++;
+		}
+	}
+	
 	U16 offset = last_TX_write_pointer & S0_TX_MASK;
 	U16 start_address =  S0_TX_BASE + offset;
 
-	usart_write_crlf();
-	usart_write_crlf();
-	usart_write_str((pU08)"lwp :");
-	usart_write_U16_hex(last_TX_write_pointer);
-	usart_write_crlf();
-	
-	usart_write_str((pU08)"off :");
-	usart_write_U16_hex(offset);
-	usart_write_crlf();
-	
-	usart_write_str((pU08)"stad:");
-	usart_write_U16_hex(start_address);
-	usart_write_crlf();
+	if (last_samurai != last_TX_write_pointer)
+			usart_write_str((pU08)"Hilfeeeeeeeeeeeeeee\n");
+	
+	//usart_write_crlf();
+	//usart_write_char('|');usart_write_U16_hex( last_TX_write_pointer );
+	//usart_write_char('|');usart_write_U16_hex( offset );
+	//usart_write_char('|');usart_write_U16_hex( start_address );
+	
+	
+	//usart_write_crlf();
+	//usart_write_crlf();
+	//usart_write_str((pU08)"lwp :");
+	//usart_write_U16_hex(last_TX_write_pointer);
+	//usart_write_crlf();
+	//
+	//usart_write_str((pU08)"off :");
+	//usart_write_U16_hex(offset);
+	//usart_write_crlf();
+	//
+	//usart_write_str((pU08)"stad:");
+	//usart_write_U16_hex(start_address);
+	//usart_write_crlf();
 	
 
@@ -345,8 +421,8 @@
 		NumBytes = freesize;
 	}
-
+	//usart_write_char('|');usart_write_U16_hex( NumBytes );
+	
 	// now calculate the offset address
 	// calculated according to W5100 datasheet page: 44
-
 	if ((offset + NumBytes) > (S0_RX_MASK + 1) )  // if data is turned over in RX-mem
 	{
@@ -356,9 +432,9 @@
 		{
 			w5100_write(start_address + i, string[i]);
-			usart_write_str((pU08)"wr:");
-			usart_write_U16_hex(start_address + i);
-			usart_write_char(' ');
-			usart_write_char(string[i]);
-			usart_write_crlf();
+			//usart_write_str((pU08)"wr:");
+			//usart_write_U16_hex(start_address + i);
+			//usart_write_char(' ');
+			//usart_write_char(string[i]);
+			//usart_write_crlf();
 
 		}
@@ -366,9 +442,9 @@
 		{
 			w5100_write(S0_RX_BASE + i, string[upper_size+i]);
-			usart_write_str((pU08)"wr:");
-			usart_write_U16_hex(S0_RX_BASE + i);
-			usart_write_char(' ');
-			usart_write_char(string[upper_size+i]);
-			usart_write_crlf();
+			//usart_write_str((pU08)"wr:");
+			//usart_write_U16_hex(S0_RX_BASE + i);
+			//usart_write_char(' ');
+			//usart_write_char(string[upper_size+i]);
+			//usart_write_crlf();
 
 		}
@@ -379,9 +455,9 @@
 		{
 			w5100_write(start_address + i, string[i]);
-			usart_write_str((pU08)"wr:");
-			usart_write_U16_hex(start_address + i);
-			usart_write_char(' ');
-			usart_write_char(string[i]);
-			usart_write_crlf();
+			//usart_write_str((pU08)"wr:");
+			//usart_write_U16_hex(start_address + i);
+			//usart_write_char(' ');
+			//usart_write_char(string[i]);
+			//usart_write_crlf();
 
 		}
@@ -389,11 +465,23 @@
 
 	// inform W5100 about how much data was written.
-	set_S0_TX_WR(last_TX_write_pointer + NumBytes);
-		usart_write_str((pU08)"wrpt:");
-		usart_write_U16_hex(last_TX_write_pointer + NumBytes);
-		usart_write_crlf();
+	U16 new_write_pointer = last_TX_write_pointer + NumBytes;
+	last_samurai = new_write_pointer;
+	
+	
+	set_S0_TX_WR(new_write_pointer);
+	//usart_write_char('|');usart_write_U16_hex( new_write_pointer );
+	
+	//usart_write_char('|');
+	//usart_write_crlf();
+	
+		
+		//usart_write_str((pU08)"wrpt:");
+		//usart_write_U16_hex(last_TX_write_pointer + NumBytes);
+		//usart_write_crlf();
 
 	// tell it to send now the data away
 	w5100_write( S0_CR, CR_SEND);
+	
+	
 	
 	return NumBytes;
@@ -427,2 +515,9 @@
 }
 
+void eth_write_lf(void) {
+	if (eth_write_index < ETH_WRITE_BUFFER_SIZE) {
+		eth_write_buffer[eth_write_index++] = '\n';
+	}
+	w5100_set_TX(eth_write_buffer, eth_write_index);
+	eth_write_index = 0;
+}
