From 27a2de3439cf7c818d05762d20f075278455aa19 Mon Sep 17 00:00:00 2001 From: mlalondesvn Date: Sun, 16 Sep 2007 20:47:42 +0000 Subject: [PATCH] ADDED - NET_CHECKSUM_DEBUG output MODIF - nicInit can now return -1 if it failed to initialize the nic MODIF - All debuging output disabled in this build! FIXED - Bug in NIC debug print definition git-svn-id: svn+ssh://oldsvn/home/mlalondesvn/svn/Ethduino@4 b05466c9-153a-0410-ad00-ea1d4d8a27b5 --- arp.cpp | 16 ++++++++++++---- arp.h | 6 +++--- enc28j60.cpp | 19 +++++++++++++------ enc28j60.h | 5 ++++- icmp.cpp | 12 ++++++++++-- ip.cpp | 15 +++++++++++---- main.h | 2 +- main.pde | 21 +++++++++++++++------ net.cpp | 31 +++---------------------------- net.h | 5 ++++- netstack.h | 2 +- nic.h | 7 +++++-- 12 files changed, 82 insertions(+), 59 deletions(-) diff --git a/arp.cpp b/arp.cpp index 9cad300..5eecc64 100644 --- a/arp.cpp +++ b/arp.cpp @@ -60,8 +60,10 @@ void arpSetAddress(struct netEthAddr* myeth, uint32_t myip) void arpArpIn(unsigned int len, struct netEthArpHeader* packet) { #ifdef ARP_DEBUG - SPrint("Received ARP Request\r\n"); - arpPrintHeader( &packet->arp ); + SPrint("Received ARP Request\r\n"); + #if NET_DEBUG > 3 + arpPrintHeader( &packet->arp ); + #endif #endif // for now, we just reply to requests @@ -86,8 +88,10 @@ void arpArpIn(unsigned int len, struct netEthArpHeader* packet) packet->eth.src = ArpMyAddr.ethaddr; #ifdef ARP_DEBUG - SPrint("Sending ARP Reply\r\n"); - arpPrintHeader( &packet->arp ); + SPrint("Sending ARP Reply\r\n"); + #if NET_DEBUG > 3 + arpPrintHeader( &packet->arp ); + #endif #endif // send reply! @@ -206,12 +210,16 @@ void arpPrintHeader(struct netArpHeader* packet) else SPrint("UNKNOWN"); Serial.println(); + // print source hardware address SPrint("SrcHwAddr : "); netPrintEthAddr(&packet->shwaddr); Serial.println(); + // print source protocol address SPrint("SrcProtoAddr: "); netPrintIPAddr(HTONL(packet->sipaddr)); Serial.println(); + // print target hardware address SPrint("DstHwAddr : "); netPrintEthAddr(&packet->dhwaddr); Serial.println(); + // print target protocol address SPrint("DstProtoAddr: "); netPrintIPAddr(HTONL(packet->dipaddr)); Serial.println(); } diff --git a/arp.h b/arp.h index 4c4c674..fee718b 100644 --- a/arp.h +++ b/arp.h @@ -43,15 +43,15 @@ #include "net.h" #ifndef ARP_TABLE_SIZE -#define ARP_TABLE_SIZE 8 +#define ARP_TABLE_SIZE 4 #endif #ifndef ARP_CACHE_TIME_TO_LIVE #define ARP_CACHE_TIME_TO_LIVE 100 #endif -#define ARP_DEBUG -#define ARP_DEBUG_PRINT +// #define ARP_DEBUG +// #define ARP_DEBUG_PRINT /*! Initialize ARP system. diff --git a/enc28j60.cpp b/enc28j60.cpp index 63ebaf3..03c4ef7 100644 --- a/enc28j60.cpp +++ b/enc28j60.cpp @@ -51,9 +51,9 @@ u08 Enc28j60Bank; u16 NextPacketPtr; -void nicInit(void) +char nicInit(void) { - enc28j60Init(); + return enc28j60Init(); } void nicSend(unsigned int len, unsigned char* packet) @@ -90,7 +90,7 @@ void nicSetMacAddress(uint8_t* macaddr) enc28j60Write(MAADR0, *macaddr++); } -#ifdef NET_DEBUG || NIC_DEBUG +#if defined(NET_DEBUG) || defined(NIC_DEBUG) void nicRegDump(void) { enc28j60RegDump(); @@ -297,7 +297,7 @@ void enc28j60PhyWrite(uint8_t address, uint16_t data) while(enc28j60Read(MISTAT) & MISTAT_BUSY); } -void enc28j60Init(void) +char enc28j60Init(void) { /** * Enable ENC28J560 Control ports @@ -346,18 +346,24 @@ void enc28j60Init(void) SPrintln("PHY reboot done"); #endif -#ifdef ENC28J60_LAMPS_MODE +#ifdef ENC28J60_LAMPS_MODE +#ifdef DEBUG_ENC_INIT SPrintln("Custom lamps"); +#endif enc28j60PhyWrite(PHLCON, ENC28J60_LAMPS_MODE); #else // Errata #9 correction if (enc28j60Read(MACON3) & MACON3_FULDPX) { + #ifdef DEBUG_ENC_INIT SPrintln("Full duplex lamps"); + #endif enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT); } else { + #ifdef DEBUG_ENC_INIT SPrintln("Half duplex lamps"); + #endif enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT_HD); } #endif @@ -480,6 +486,7 @@ void enc28j60Init(void) // setup duplex ---------------------- */ + return 0; } #define ETHERNET_MIN_PACKET_LENGTH 0x3C @@ -628,7 +635,7 @@ void enc28j60ReceiveOverflowRecover(void) // recovery completed } -#ifdef NET_DEBUG || NIC_DEBUG +#if defined(NET_DEBUG) || defined(NIC_DEBUG) #define SPrint_D(str) SPrint(str); delayMicroseconds(100); void enc28j60RegDump(void) diff --git a/enc28j60.h b/enc28j60.h index 46a19d6..a8e980d 100644 --- a/enc28j60.h +++ b/enc28j60.h @@ -302,7 +302,8 @@ uint16_t enc28j60PhyRead(uint8_t address); void enc28j60PhyWrite(uint8_t address, uint16_t data); //! initialize the ethernet interface for transmit/receive -void enc28j60Init(void); +/// \return -1 if there was a problem initializing the NIC, otherwise the value is undefined +char enc28j60Init(void); //! Packet transmit function. /// Sends a packet on the network. It is assumed that the packet is headed by a valid ethernet header. @@ -323,7 +324,9 @@ unsigned int enc28j60PacketReceive(unsigned int maxlen, unsigned char* packet); void enc28j60ReceiveOverflowRecover(void); //! formatted print of important ENC28J60 registers +#if defined(NET_DEBUG) || defined(NIC_DEBUG) void enc28j60RegDump(void); +#endif // ! Hard reset function void enc28j60hardReset(void); diff --git a/icmp.cpp b/icmp.cpp index 5d58e40..dfee688 100644 --- a/icmp.cpp +++ b/icmp.cpp @@ -54,8 +54,16 @@ void icmpEchoRequest(icmpip_hdr* packet) packet->icmp.type = ICMP_TYPE_ECHOREPLY; // recalculate checksum - packet->icmp.icmpchksum = 0; - packet->icmp.icmpchksum = ipChecksum((netIpHeader*)&packet->icmp, htons(packet->ip.len)-IP_HEADER_LEN); + packet->icmp.icmpchksum = 0x0000; + packet->icmp.icmpchksum = ipChecksum((netIpHeader*)&packet->icmp, htons(packet->ip.len)-sizeof(netIpHeader)); + +#ifdef NET_CHECKSUM_DEBUG + SPrintln(" "); + SPrint("ICMP Checksum: 0x"); + Serial.print((uint16_t)htons(packet->icmp.icmpchksum), HEX); + SPrint(" 0x"); + Serial.println((uint16_t)HTONS(packet->icmp.icmpchksum), HEX); +#endif // return to sender tempIp = packet->ip.destipaddr; diff --git a/ip.cpp b/ip.cpp index aabb645..12b1a2a 100644 --- a/ip.cpp +++ b/ip.cpp @@ -87,6 +87,11 @@ void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data) // DO THIS ONLY AFTER ALL CHANGES HAVE BEEN MADE TO IP HEADER ethIpHeader->ip.ipchksum = netChecksum(ðIpHeader->ip, IP_HEADER_LEN); +#ifdef NET_CHECKSUM_DEBUG + SPrint("IP Header Checksum: "); + Serial.println((uint16_t)ethIpHeader->ip.ipchksum, HEX); +#endif + // add ethernet routing // check if we need to send to gateway if( (dstIp & IpMyConfig.netmask) == (IpMyConfig.ip & IpMyConfig.netmask) ) @@ -130,10 +135,12 @@ void udpSend(uint32_t dstIp, uint16_t dstPort, uint16_t len, uint8_t* data) len += UDP_HEADER_LEN; // fill UDP header - udpHeader->destport = HTONS(dstPort); - udpHeader->srcport = HTONS(dstPort); - udpHeader->udplen = htons(len); - udpHeader->udpchksum = 0; + udpHeader->destport = HTONS(dstPort); + udpHeader->srcport = HTONS(dstPort); + udpHeader->udplen = htons(len); + udpHeader->udpchksum = 0x000; + + //udpHeader->udpchksum = ipChecksum((netUdpHeader*)&udpHeader, htons(udpHeader->ip.len)-UDP_HEADER_LEN); #if NET_DEBUG > 6 debugPrintHexTable(UDP_HEADER_LEN, (uint8_t*)udpHeader); diff --git a/main.h b/main.h index 296c175..e6294ff 100644 --- a/main.h +++ b/main.h @@ -26,7 +26,7 @@ // Network options #define IPADDRESS IPDOT(192l,168l,0l,126l) #define NETMASK IPDOT(255l,255l,0l,0l) -#define GATEWAY IPDOT(192l,168l,255l,255l) +#define GATEWAY IPDOT(192l,168l,0l,01l) #define ETHADDR0 0xCC /*'78'*/ #define ETHADDR1 0x00 /*'53'*/ diff --git a/main.pde b/main.pde index 8f2f208..8d2f0dc 100644 --- a/main.pde +++ b/main.pde @@ -112,18 +112,22 @@ void serviceLocal(void) sbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS); break; */ +#if defined(NET_DEBUG) || defined(NIC_DEBUG) case 'a' : arpPrintTable(); break; - case 'd': nicRegDump(); break; - +#endif case 'i': nicInit(); break; + /* + case 'p': + break; + */ case 'n': // Netstack init netstackInit(IPADDRESS, NETMASK, GATEWAY); break; @@ -137,7 +141,7 @@ void serviceLocal(void) Serial.println((byte)(enc28j60Read(EREVID)), BIN); break; - case 'p': + case 'c': SPrintln("IP Configs"); SPrint("IP:"); Serial.println(ipGetConfig()->ip); @@ -168,14 +172,19 @@ void serviceLocal(void) case '?': SPrintln("Usage:"); +#ifdef NET_DEBUG SPrintln("(a) Print ARP table"); +#endif + SPrintln("(b) Reboot NIC"); + SPrintln("(c) Print IP configuration"); +#if defined(NET_DEBUG) || defined(NIC_DEBUG) SPrintln("(d) Dump nic registers"); +#endif SPrintln("(i) Nic initialization"); SPrintln("(n) Netstack initialization"); - SPrintln("(b) Reboot NIC"); - SPrintln("(v) Display NIC revision"); - SPrintln("(p) Print IP configuration"); + //SPrintln("(p) Ping a remote machine"); SPrintln("(u) Send UDP packet"); + SPrintln("(v) Display NIC revision"); SPrintln("(?) Print this help"); break; diff --git a/net.cpp b/net.cpp index 6019732..b89c659 100644 --- a/net.cpp +++ b/net.cpp @@ -31,12 +31,12 @@ uint32_t htonl(uint32_t val) } static uint16_t generateChecksum(uint16_t sum, netIpHeader *data, uint16_t len) -{ +{ uint16_t t; uint32_t *dataptr; uint32_t *last_byte; - + dataptr = (uint32_t *) data; last_byte = (uint32_t *) data + len - 1; @@ -63,39 +63,14 @@ static uint16_t generateChecksum(uint16_t sum, netIpHeader *data, uint16_t len) uint16_t ipChecksum(netIpHeader *data, uint16_t len) { - return htons(generateChecksum(0, (netIpHeader *)data, len)); + return htons(generateChecksum(0, data, len)); } uint16_t netChecksum(netIpHeader *data, uint16_t len) { uint16_t sum; sum = generateChecksum(0, &data[len], len); - SPrintln("uip_ipchksum: sum 0x"); - Serial.println((long int)sum, HEX); return (sum == 0) ? 0xffff : htons(sum); - -/* register uint32_t sum = 0; - - for (;;) { - if (len < 2) - break; - //sum += *((uint16_t *)data)++; - sum += *((uint16_t *)data); - data+=2; - len -= 2; - } - - if (len) - sum += *(uint8_t *) data; - - while ((len = (uint16_t) (sum >> 16)) != 0) - sum = (uint16_t) sum + len; -#if NET_DEBUG > 6 - Serial.print("Checksum: "); - Serial.println((uint16_t) ~sum, HEX); -#endif - return (uint16_t) ~sum; -*/ } void netPrintEthAddr(struct netEthAddr* ethaddr) diff --git a/net.h b/net.h index 3623920..5c2ab35 100644 --- a/net.h +++ b/net.h @@ -26,7 +26,10 @@ #ifndef NET_H #define NET_H -#define NET_DEBUG 7 + + +// #define NET_DEBUG 7 +//#define NET_CHECKSUM_DEBUG #include "avrlibdefs.h" #include "avrlibtypes.h" diff --git a/netstack.h b/netstack.h index 762f658..09530c8 100644 --- a/netstack.h +++ b/netstack.h @@ -44,7 +44,7 @@ #include "ip.h" #include "nic.h" -#define NETSTACK_DEBUG +// #define NETSTACK_DEBUG /// NET_BUFFERSIZE is the common receive/process/transmit buffer. /// - You may override the default NET_BUFFERSIZE by defining an alternate value in global.h. diff --git a/nic.h b/nic.h index 1303acd..842f349 100644 --- a/nic.h +++ b/nic.h @@ -27,7 +27,7 @@ #ifndef NIC_H #define NIC_H -#define NIC_DEBUG +// #define NIC_DEBUG #include @@ -36,7 +36,8 @@ /// the network interface ready to handle \c nicSend() and \c nicPoll() requests. /// \note For some hardware, this command will take a non-negligible amount of /// time (1-2 seconds). -void nicInit(void); +/// \return -1 if there was a problem initializing the NIC, otherwise the value is undefined +char nicInit(void); //! Send packet on network interface. /// Function accepts the length (in bytes) of the data to be sent, and a pointer @@ -67,7 +68,9 @@ void nicSetMacAddress(uint8_t* macaddr); //! Print network interface hardware registers. /// Prints a formatted list of names and values of NIC registers for debugging /// purposes. +#if defined(NET_DEBUG) || defined(NIC_DEBUG) void nicRegDump(void); +#endif //! PHY Chip hard reset function void nicHardReset(void);