1
0
Fork 0

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
master
mlalondesvn 17 years ago
parent 87e0115d23
commit 27a2de3439

@ -60,8 +60,10 @@ void arpSetAddress(struct netEthAddr* myeth, uint32_t myip)
void arpArpIn(unsigned int len, struct netEthArpHeader* packet) void arpArpIn(unsigned int len, struct netEthArpHeader* packet)
{ {
#ifdef ARP_DEBUG #ifdef ARP_DEBUG
SPrint("Received ARP Request\r\n"); SPrint("Received ARP Request\r\n");
arpPrintHeader( &packet->arp ); #if NET_DEBUG > 3
arpPrintHeader( &packet->arp );
#endif
#endif #endif
// for now, we just reply to requests // for now, we just reply to requests
@ -86,8 +88,10 @@ void arpArpIn(unsigned int len, struct netEthArpHeader* packet)
packet->eth.src = ArpMyAddr.ethaddr; packet->eth.src = ArpMyAddr.ethaddr;
#ifdef ARP_DEBUG #ifdef ARP_DEBUG
SPrint("Sending ARP Reply\r\n"); SPrint("Sending ARP Reply\r\n");
arpPrintHeader( &packet->arp ); #if NET_DEBUG > 3
arpPrintHeader( &packet->arp );
#endif
#endif #endif
// send reply! // send reply!
@ -206,12 +210,16 @@ void arpPrintHeader(struct netArpHeader* packet)
else else
SPrint("UNKNOWN"); SPrint("UNKNOWN");
Serial.println(); Serial.println();
// print source hardware address // print source hardware address
SPrint("SrcHwAddr : "); netPrintEthAddr(&packet->shwaddr); Serial.println(); SPrint("SrcHwAddr : "); netPrintEthAddr(&packet->shwaddr); Serial.println();
// print source protocol address // print source protocol address
SPrint("SrcProtoAddr: "); netPrintIPAddr(HTONL(packet->sipaddr)); Serial.println(); SPrint("SrcProtoAddr: "); netPrintIPAddr(HTONL(packet->sipaddr)); Serial.println();
// print target hardware address // print target hardware address
SPrint("DstHwAddr : "); netPrintEthAddr(&packet->dhwaddr); Serial.println(); SPrint("DstHwAddr : "); netPrintEthAddr(&packet->dhwaddr); Serial.println();
// print target protocol address // print target protocol address
SPrint("DstProtoAddr: "); netPrintIPAddr(HTONL(packet->dipaddr)); Serial.println(); SPrint("DstProtoAddr: "); netPrintIPAddr(HTONL(packet->dipaddr)); Serial.println();
} }

@ -43,15 +43,15 @@
#include "net.h" #include "net.h"
#ifndef ARP_TABLE_SIZE #ifndef ARP_TABLE_SIZE
#define ARP_TABLE_SIZE 8 #define ARP_TABLE_SIZE 4
#endif #endif
#ifndef ARP_CACHE_TIME_TO_LIVE #ifndef ARP_CACHE_TIME_TO_LIVE
#define ARP_CACHE_TIME_TO_LIVE 100 #define ARP_CACHE_TIME_TO_LIVE 100
#endif #endif
#define ARP_DEBUG // #define ARP_DEBUG
#define ARP_DEBUG_PRINT // #define ARP_DEBUG_PRINT
/*! Initialize ARP system. /*! Initialize ARP system.

@ -51,9 +51,9 @@
u08 Enc28j60Bank; u08 Enc28j60Bank;
u16 NextPacketPtr; u16 NextPacketPtr;
void nicInit(void) char nicInit(void)
{ {
enc28j60Init(); return enc28j60Init();
} }
void nicSend(unsigned int len, unsigned char* packet) void nicSend(unsigned int len, unsigned char* packet)
@ -90,7 +90,7 @@ void nicSetMacAddress(uint8_t* macaddr)
enc28j60Write(MAADR0, *macaddr++); enc28j60Write(MAADR0, *macaddr++);
} }
#ifdef NET_DEBUG || NIC_DEBUG #if defined(NET_DEBUG) || defined(NIC_DEBUG)
void nicRegDump(void) void nicRegDump(void)
{ {
enc28j60RegDump(); enc28j60RegDump();
@ -297,7 +297,7 @@ void enc28j60PhyWrite(uint8_t address, uint16_t data)
while(enc28j60Read(MISTAT) & MISTAT_BUSY); while(enc28j60Read(MISTAT) & MISTAT_BUSY);
} }
void enc28j60Init(void) char enc28j60Init(void)
{ {
/** /**
* Enable ENC28J560 Control ports * Enable ENC28J560 Control ports
@ -347,17 +347,23 @@ void enc28j60Init(void)
#endif #endif
#ifdef ENC28J60_LAMPS_MODE #ifdef ENC28J60_LAMPS_MODE
#ifdef DEBUG_ENC_INIT
SPrintln("Custom lamps"); SPrintln("Custom lamps");
#endif
enc28j60PhyWrite(PHLCON, ENC28J60_LAMPS_MODE); enc28j60PhyWrite(PHLCON, ENC28J60_LAMPS_MODE);
#else #else
// Errata #9 correction // Errata #9 correction
if (enc28j60Read(MACON3) & MACON3_FULDPX) if (enc28j60Read(MACON3) & MACON3_FULDPX)
{ {
#ifdef DEBUG_ENC_INIT
SPrintln("Full duplex lamps"); SPrintln("Full duplex lamps");
#endif
enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT); enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT);
} else { } else {
#ifdef DEBUG_ENC_INIT
SPrintln("Half duplex lamps"); SPrintln("Half duplex lamps");
#endif
enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT_HD); enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT_HD);
} }
#endif #endif
@ -480,6 +486,7 @@ void enc28j60Init(void)
// setup duplex ---------------------- // setup duplex ----------------------
*/ */
return 0;
} }
#define ETHERNET_MIN_PACKET_LENGTH 0x3C #define ETHERNET_MIN_PACKET_LENGTH 0x3C
@ -628,7 +635,7 @@ void enc28j60ReceiveOverflowRecover(void)
// recovery completed // recovery completed
} }
#ifdef NET_DEBUG || NIC_DEBUG #if defined(NET_DEBUG) || defined(NIC_DEBUG)
#define SPrint_D(str) SPrint(str); delayMicroseconds(100); #define SPrint_D(str) SPrint(str); delayMicroseconds(100);
void enc28j60RegDump(void) void enc28j60RegDump(void)

@ -302,7 +302,8 @@ uint16_t enc28j60PhyRead(uint8_t address);
void enc28j60PhyWrite(uint8_t address, uint16_t data); void enc28j60PhyWrite(uint8_t address, uint16_t data);
//! initialize the ethernet interface for transmit/receive //! 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. //! Packet transmit function.
/// Sends a packet on the network. It is assumed that the packet is headed by a valid ethernet header. /// 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); void enc28j60ReceiveOverflowRecover(void);
//! formatted print of important ENC28J60 registers //! formatted print of important ENC28J60 registers
#if defined(NET_DEBUG) || defined(NIC_DEBUG)
void enc28j60RegDump(void); void enc28j60RegDump(void);
#endif
// ! Hard reset function // ! Hard reset function
void enc28j60hardReset(void); void enc28j60hardReset(void);

@ -54,8 +54,16 @@ void icmpEchoRequest(icmpip_hdr* packet)
packet->icmp.type = ICMP_TYPE_ECHOREPLY; packet->icmp.type = ICMP_TYPE_ECHOREPLY;
// recalculate checksum // recalculate checksum
packet->icmp.icmpchksum = 0; packet->icmp.icmpchksum = 0x0000;
packet->icmp.icmpchksum = ipChecksum((netIpHeader*)&packet->icmp, htons(packet->ip.len)-IP_HEADER_LEN); 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 // return to sender
tempIp = packet->ip.destipaddr; tempIp = packet->ip.destipaddr;

@ -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 // DO THIS ONLY AFTER ALL CHANGES HAVE BEEN MADE TO IP HEADER
ethIpHeader->ip.ipchksum = netChecksum(&ethIpHeader->ip, IP_HEADER_LEN); ethIpHeader->ip.ipchksum = netChecksum(&ethIpHeader->ip, IP_HEADER_LEN);
#ifdef NET_CHECKSUM_DEBUG
SPrint("IP Header Checksum: ");
Serial.println((uint16_t)ethIpHeader->ip.ipchksum, HEX);
#endif
// add ethernet routing // add ethernet routing
// check if we need to send to gateway // check if we need to send to gateway
if( (dstIp & IpMyConfig.netmask) == (IpMyConfig.ip & IpMyConfig.netmask) ) 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; len += UDP_HEADER_LEN;
// fill UDP header // fill UDP header
udpHeader->destport = HTONS(dstPort); udpHeader->destport = HTONS(dstPort);
udpHeader->srcport = HTONS(dstPort); udpHeader->srcport = HTONS(dstPort);
udpHeader->udplen = htons(len); udpHeader->udplen = htons(len);
udpHeader->udpchksum = 0; udpHeader->udpchksum = 0x000;
//udpHeader->udpchksum = ipChecksum((netUdpHeader*)&udpHeader, htons(udpHeader->ip.len)-UDP_HEADER_LEN);
#if NET_DEBUG > 6 #if NET_DEBUG > 6
debugPrintHexTable(UDP_HEADER_LEN, (uint8_t*)udpHeader); debugPrintHexTable(UDP_HEADER_LEN, (uint8_t*)udpHeader);

@ -26,7 +26,7 @@
// Network options // Network options
#define IPADDRESS IPDOT(192l,168l,0l,126l) #define IPADDRESS IPDOT(192l,168l,0l,126l)
#define NETMASK IPDOT(255l,255l,0l,0l) #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 ETHADDR0 0xCC /*'78'*/
#define ETHADDR1 0x00 /*'53'*/ #define ETHADDR1 0x00 /*'53'*/

@ -112,18 +112,22 @@ void serviceLocal(void)
sbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS); sbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS);
break; break;
*/ */
#if defined(NET_DEBUG) || defined(NIC_DEBUG)
case 'a' : case 'a' :
arpPrintTable(); arpPrintTable();
break; break;
case 'd': case 'd':
nicRegDump(); nicRegDump();
break; break;
#endif
case 'i': case 'i':
nicInit(); nicInit();
break; break;
/*
case 'p':
break;
*/
case 'n': // Netstack init case 'n': // Netstack init
netstackInit(IPADDRESS, NETMASK, GATEWAY); netstackInit(IPADDRESS, NETMASK, GATEWAY);
break; break;
@ -137,7 +141,7 @@ void serviceLocal(void)
Serial.println((byte)(enc28j60Read(EREVID)), BIN); Serial.println((byte)(enc28j60Read(EREVID)), BIN);
break; break;
case 'p': case 'c':
SPrintln("IP Configs"); SPrintln("IP Configs");
SPrint("IP:"); SPrint("IP:");
Serial.println(ipGetConfig()->ip); Serial.println(ipGetConfig()->ip);
@ -168,14 +172,19 @@ void serviceLocal(void)
case '?': case '?':
SPrintln("Usage:"); SPrintln("Usage:");
#ifdef NET_DEBUG
SPrintln("(a) Print ARP table"); 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"); SPrintln("(d) Dump nic registers");
#endif
SPrintln("(i) Nic initialization"); SPrintln("(i) Nic initialization");
SPrintln("(n) Netstack initialization"); SPrintln("(n) Netstack initialization");
SPrintln("(b) Reboot NIC"); //SPrintln("(p) Ping a remote machine");
SPrintln("(v) Display NIC revision");
SPrintln("(p) Print IP configuration");
SPrintln("(u) Send UDP packet"); SPrintln("(u) Send UDP packet");
SPrintln("(v) Display NIC revision");
SPrintln("(?) Print this help"); SPrintln("(?) Print this help");
break; break;

@ -63,39 +63,14 @@ static uint16_t generateChecksum(uint16_t sum, netIpHeader *data, uint16_t len)
uint16_t ipChecksum(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 netChecksum(netIpHeader *data, uint16_t len)
{ {
uint16_t sum; uint16_t sum;
sum = generateChecksum(0, &data[len], len); sum = generateChecksum(0, &data[len], len);
SPrintln("uip_ipchksum: sum 0x");
Serial.println((long int)sum, HEX);
return (sum == 0) ? 0xffff : htons(sum); 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) void netPrintEthAddr(struct netEthAddr* ethaddr)

@ -26,7 +26,10 @@
#ifndef NET_H #ifndef NET_H
#define NET_H #define NET_H
#define NET_DEBUG 7
// #define NET_DEBUG 7
//#define NET_CHECKSUM_DEBUG
#include "avrlibdefs.h" #include "avrlibdefs.h"
#include "avrlibtypes.h" #include "avrlibtypes.h"

@ -44,7 +44,7 @@
#include "ip.h" #include "ip.h"
#include "nic.h" #include "nic.h"
#define NETSTACK_DEBUG // #define NETSTACK_DEBUG
/// NET_BUFFERSIZE is the common receive/process/transmit buffer. /// NET_BUFFERSIZE is the common receive/process/transmit buffer.
/// - You may override the default NET_BUFFERSIZE by defining an alternate value in global.h. /// - You may override the default NET_BUFFERSIZE by defining an alternate value in global.h.

@ -27,7 +27,7 @@
#ifndef NIC_H #ifndef NIC_H
#define NIC_H #define NIC_H
#define NIC_DEBUG // #define NIC_DEBUG
#include <inttypes.h> #include <inttypes.h>
@ -36,7 +36,8 @@
/// the network interface ready to handle \c nicSend() and \c nicPoll() requests. /// 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 /// \note For some hardware, this command will take a non-negligible amount of
/// time (1-2 seconds). /// 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. //! Send packet on network interface.
/// Function accepts the length (in bytes) of the data to be sent, and a pointer /// 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. //! Print network interface hardware registers.
/// Prints a formatted list of names and values of NIC registers for debugging /// Prints a formatted list of names and values of NIC registers for debugging
/// purposes. /// purposes.
#if defined(NET_DEBUG) || defined(NIC_DEBUG)
void nicRegDump(void); void nicRegDump(void);
#endif
//! PHY Chip hard reset function //! PHY Chip hard reset function
void nicHardReset(void); void nicHardReset(void);

Loading…
Cancel
Save