uIP 1.0 based webserver for LPC1114 + ENC28J60

Dependencies:   mbed TMP102

Revision:
3:a2715e9c7737
Parent:
1:b4d28172cacd
--- a/dev-enc28j60/enc28j60.cpp	Sat Jun 21 11:54:24 2014 +0000
+++ b/dev-enc28j60/enc28j60.cpp	Mon Jun 30 16:00:08 2014 +0000
@@ -157,7 +157,8 @@
 	// perform system reset
 	this->writeOp(ENC28J60_SOFT_RESET, 0, ENC28J60_SOFT_RESET);
 	// check CLKRDY bit to see if reset is complete
-	wait_us(50);
+	/* Fix Errata */
+	wait_ms(1);
 	while(!(this->read(ESTAT) & ESTAT_CLKRDY));
 
 	// do bank 0 stuff
@@ -167,6 +168,9 @@
 	NextPacketPtr = RXSTART_INIT;
 	this->write(ERXSTL, RXSTART_INIT&0xFF);
 	this->write(ERXSTH, RXSTART_INIT>>8);
+	// set read pointer address
+	//this->write(ERDPTL, 0);
+	//this->write(ERDPTH, 0);
 	// set receive pointer address
 	this->write(ERXRDPTL, RXSTART_INIT&0xFF);
 	this->write(ERXRDPTH, RXSTART_INIT>>8);
@@ -252,6 +256,7 @@
 {
 	//Errata: Transmit Logic reset
 	this->writeOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRST);
+	wait_us(10);
 	this->writeOp(ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_TXRST);
 
 	// Set the write pointer to start of transmit buffer area
@@ -269,6 +274,11 @@
 	
 	// send the contents of the transmit buffer onto the network
 	this->writeOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRTS);
+	
+	while(this->readOp(ENC28J60_BIT_FIELD_SET, ECON1) & ECON1_TXRTS)
+	{
+		wait_us(10);
+	}
 }
 
 void ENC28J60::packetSend2(unsigned int len1, unsigned char* packet1, unsigned int len2, unsigned char* packet2)
@@ -302,7 +312,12 @@
 
 	// check if a packet has been received and buffered
 	if( !(this->read(EIR) & EIR_PKTIF) )
-		return 0;
+	{
+		if(this->read(EPKTCNT) == 0)
+		{
+			return 0;
+		}
+	}
 	
 	// Make absolutely certain that any previous packet was discarded	
 	//if( WasDiscarded == FALSE)
@@ -332,11 +347,21 @@
 
 	// Move the RX read pointer to the start of the next received packet
 	// This frees the memory we just read out
-	this->write(ERXRDPTL, (NextPacketPtr));
-	this->write(ERXRDPTH, (NextPacketPtr)>>8);
+	/* Fix Errata */
+	if(NextPacketPtr == RXSTART_INIT)
+	{
+		this->write(ERXRDPTL, (RXSTOP_INIT));
+		this->write(ERXRDPTH, (RXSTOP_INIT)>>8);
+	}
+	else
+	{
+		this->write(ERXRDPTL, (NextPacketPtr));
+		this->write(ERXRDPTH, (NextPacketPtr)>>8);
+	}
 
 	// decrement the packet counter indicate we are done with this packet
 	this->writeOp(ENC28J60_BIT_FIELD_SET, ECON2, ECON2_PKTDEC);
+	wait_us(11);
 
 	return len;
 }
@@ -352,35 +377,35 @@
 {
 	printf("RevID: 0x%x\n", this->read(EREVID));
 
-	printf("Cntrl:\n");
+	printf("Ctrl: ");
 	printf("ECON1=%x ",this->read(ECON1));
 	printf("ECON2=%x ",this->read(ECON2));
 	printf("ESTAT=%x ",this->read(ESTAT));
-	printf("EIR=%x\n",this->read(EIR));
+	printf("EIR=%x ",this->read(EIR));
 	printf("EIE=%x\n",this->read(EIE));
 
-	printf("MAC:\n");
+	printf("MAC:  ");
 	printf("MACON1=%x ",this->read(MACON1));
 	printf("MACON2=%x ",this->read(MACON2));
 	printf("MACON3=%x ",this->read(MACON3));
-	printf("MACON4=%x\n",this->read(MACON4));
+	printf("MACON4=%x ",this->read(MACON4));
 	printf("MAD=%x%x\n",
 		this->read(MAADR5)*0x1000000+this->read(MAADR4)*0x10000+this->read(MAADR3)*0x100+this->read(MAADR2),
 		this->read(MAADR1)*0x1000000+this->read(MAADR0)*0x10000+0xffff);
 
-	printf("Rx:\n");
-	printf("ERXST=%x ",this->read(ERXSTH)*0x100+this->read(ERXSTL));
-	printf("ERXND=%x ",this->read(ERXNDH)*0x100+this->read(ERXNDL));
+	printf("Rx:   ");
+	printf("ERXST=%04x ",this->read(ERXSTH)*0x100+this->read(ERXSTL));
+	printf("ERXND=%04x ",this->read(ERXNDH)*0x100+this->read(ERXNDL));
 	printf("ERXWRPT=%x ",this->read(ERXWRPTH)*0x100+this->read(ERXWRPTL));
-	printf("ERXRDPT=%x\n",this->read(ERXRDPTH)*0x100+this->read(ERXRDPTL));
+	printf("ERXRDPT=%x ",this->read(ERXRDPTH)*0x100+this->read(ERXRDPTL));
 	printf("ERXFCON=%x ",this->read(ERXFCON));
 	printf("EPKTCNT=%x ",this->read(EPKTCNT));
-	printf("MAMXFL=%x ",this->read(MAMXFLH)*0x100+this->read(MAMXFLL));
+	printf("MAMXFL=%x\n",this->read(MAMXFLH)*0x100+this->read(MAMXFLL));
 
-	printf("Tx:\n");
-	printf("ETXST=%x ",this->read(ETXSTH)*0x100+this->read(ETXSTL));
-	printf("ETXND=%x ",this->read(ETXNDH)*0x100+this->read(ETXNDL));
+	printf("Tx:   ");
+	printf("ETXST=%04x ",this->read(ETXSTH)*0x100+this->read(ETXSTL));
+	printf("ETXND=%04x ",this->read(ETXNDH)*0x100+this->read(ETXNDL));
 	printf("MACLCON1=%x ",this->read(MACLCON1));
-	printf("MACLCON2=%x\n",this->read(MACLCON2));
+	printf("MACLCON2=%x ",this->read(MACLCON2));
 	printf("MAPHSUP=%x\n",this->read(MAPHSUP));
 }