1
0
Fork 0

MODIF - Program strings (SPrint_P(PSTR())) are now macros to reduce code size and help cleaness

MODIF - Significantly reduced the code site (down 1k) of enc28j60RegDump() using a macro
MODIF - nicRegDump is now only avaiable in debug mode
MODIF - main's included file moved out of the main.h and into the main sketch


git-svn-id: svn+ssh://oldsvn/home/mlalondesvn/svn/Ethduino@3 b05466c9-153a-0410-ad00-ea1d4d8a27b5
master
mlalondesvn 17 years ago
parent 3f7c54bc34
commit 87e0115d23

@ -264,6 +264,7 @@ applet_files: $(TARGET).pde
echo ' init();' >> applet/$(TARGET).cpp echo ' init();' >> applet/$(TARGET).cpp
echo ' return 0;' >> applet/$(TARGET).cpp echo ' return 0;' >> applet/$(TARGET).cpp
echo '}' >> applet/$(TARGET).cpp echo '}' >> applet/$(TARGET).cpp
echo >> applet/$(TARGET).cpp
elf: applet/$(TARGET).elf elf: applet/$(TARGET).elf
hex: applet/$(TARGET).hex hex: applet/$(TARGET).hex

@ -60,7 +60,7 @@ 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_P(PSTR("Received ARP Request\r\n")); SPrint("Received ARP Request\r\n");
arpPrintHeader( &packet->arp ); arpPrintHeader( &packet->arp );
#endif #endif
@ -86,7 +86,7 @@ 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_P(PSTR("Sending ARP Reply\r\n")); SPrint("Sending ARP Reply\r\n");
arpPrintHeader( &packet->arp ); arpPrintHeader( &packet->arp );
#endif #endif
@ -193,27 +193,27 @@ int arpMatchIp(uint32_t ipaddr)
#ifdef ARP_DEBUG_PRINT #ifdef ARP_DEBUG_PRINT
void arpPrintHeader(struct netArpHeader* packet) void arpPrintHeader(struct netArpHeader* packet)
{ {
SPrint_P(PSTR("ARP Packet:\r\n")); SPrint("ARP Packet:\r\n");
#if NET_DEBUG > 6 #if NET_DEBUG > 6
debugPrintHexTable(60, (unsigned char*)&packet); debugPrintHexTable(60, (unsigned char*)&packet);
#endif #endif
// print operation type // print operation type
SPrint_P(PSTR("Operation : ")); SPrint("Operation : ");
if(packet->opcode == htons(ARP_OPCODE_REQUEST)) if(packet->opcode == htons(ARP_OPCODE_REQUEST))
SPrint_P(PSTR("REQUEST")); SPrint("REQUEST");
else if(packet->opcode == htons(ARP_OPCODE_REPLY)) else if(packet->opcode == htons(ARP_OPCODE_REPLY))
SPrint_P(PSTR("REPLY")); SPrint("REPLY");
else else
SPrint_P(PSTR("UNKNOWN")); SPrint("UNKNOWN");
Serial.println(); Serial.println();
// print source hardware address // print source hardware address
SPrint_P(PSTR("SrcHwAddr : ")); netPrintEthAddr(&packet->shwaddr); Serial.println(); SPrint("SrcHwAddr : "); netPrintEthAddr(&packet->shwaddr); Serial.println();
// print source protocol address // print source protocol address
SPrint_P(PSTR("SrcProtoAddr: ")); netPrintIPAddr(HTONL(packet->sipaddr)); Serial.println(); SPrint("SrcProtoAddr: "); netPrintIPAddr(HTONL(packet->sipaddr)); Serial.println();
// print target hardware address // print target hardware address
SPrint_P(PSTR("DstHwAddr : ")); netPrintEthAddr(&packet->dhwaddr); Serial.println(); SPrint("DstHwAddr : "); netPrintEthAddr(&packet->dhwaddr); Serial.println();
// print target protocol address // print target protocol address
SPrint_P(PSTR("DstProtoAddr: ")); netPrintIPAddr(HTONL(packet->dipaddr)); Serial.println(); SPrint("DstProtoAddr: "); netPrintIPAddr(HTONL(packet->dipaddr)); Serial.println();
} }
@ -222,14 +222,14 @@ void arpPrintTable(void)
uint8_t i; uint8_t i;
// print ARP table // print ARP table
SPrint_P(PSTR("Time Eth Address IP Address\r\n")); SPrint("Time Eth Address IP Address\r\n");
SPrint_P(PSTR("---------------------------------------\r\n")); SPrint("---------------------------------------\r\n");
for(i=0; i<ARP_TABLE_SIZE; i++) for(i=0; i<ARP_TABLE_SIZE; i++)
{ {
Serial.print(ArpTable[i].time); Serial.print(ArpTable[i].time);
SPrint_P(PSTR(" ")); SPrint(" ");
netPrintEthAddr(&ArpTable[i].ethaddr); netPrintEthAddr(&ArpTable[i].ethaddr);
SPrint_P(PSTR(" ")); SPrint(" ");
netPrintIPAddr(ArpTable[i].ipaddr); netPrintIPAddr(ArpTable[i].ipaddr);
Serial.println(); Serial.println();
} }

@ -42,8 +42,8 @@ void debugPrintHexTable(u16 length, u08 *buffer)
buf = buffer; buf = buffer;
// print the low order address indicies and ASCII header // print the low order address indicies and ASCII header
SPrint_P(PSTR(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\r\n")); SPrint(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\r\n");
SPrint_P(PSTR(" ----------------------------------------------- ---- ASCII -----\r\n")); SPrint(" ----------------------------------------------- ---- ASCII -----\r\n");
// print the data // print the data
for(j=0; j<((length+15)>>4); j++) for(j=0; j<((length+15)>>4); j++)
@ -66,7 +66,7 @@ void debugPrintHexTable(u16 length, u08 *buffer)
{ {
// we're past the end of the data's length // we're past the end of the data's length
// print spaces // print spaces
SPrint_P(PSTR(" ")); SPrint(" ");
} }
} }

@ -67,7 +67,7 @@ void dhcpIn(unsigned int len, struct netDhcpHeader* packet)
// get DHCP server ID // get DHCP server ID
dhcpGetOption(packet->options, DHCP_OPT_SERVERID, 4, &sid); dhcpGetOption(packet->options, DHCP_OPT_SERVERID, 4, &sid);
#ifdef DHCP_DEBUG #ifdef DHCP_DEBUG
SPrint_P(PSTR("DHCP: Got offer from server ")); netPrintIPAddr(htonl(sid)); Serial.println(); SPrint("DHCP: Got offer from server "); netPrintIPAddr(htonl(sid)); Serial.println();
#endif #endif
// build DHCP request (on top of this reply) // build DHCP request (on top of this reply)
@ -87,7 +87,7 @@ void dhcpIn(unsigned int len, struct netDhcpHeader* packet)
optptr = dhcpSetOption(optptr, DHCP_OPT_PARAMREQLIST, 4, &val); optptr = dhcpSetOption(optptr, DHCP_OPT_PARAMREQLIST, 4, &val);
#ifdef DHCP_DEBUG #ifdef DHCP_DEBUG
SPrint_P(PSTR("DHCP: Sending request in response to offer\r\n")); SPrint("DHCP: Sending request in response to offer\r\n");
#endif #endif
// send DHCP request // send DHCP request
DhcpServerIP = htonl(sid); DhcpServerIP = htonl(sid);
@ -110,11 +110,11 @@ void dhcpIn(unsigned int len, struct netDhcpHeader* packet)
ipSetConfig(htonl(packet->bootp.yiaddr), netmask, gateway); ipSetConfig(htonl(packet->bootp.yiaddr), netmask, gateway);
#ifdef DHCP_DEBUG #ifdef DHCP_DEBUG
SPrint_P(PSTR("DHCP: Got request ACK, bind complete\r\n")); SPrint("DHCP: Got request ACK, bind complete\r\n");
//debugPrintHexTable(len-DHCP_HEADER_LEN, (packet->options)); //debugPrintHexTable(len-DHCP_HEADER_LEN, (packet->options));
// print info // print info
ipPrintConfig(ipGetConfig()); ipPrintConfig(ipGetConfig());
SPrint_P(PSTR("LeaseTm : ")); Serial.print(DhcpLeaseTime); Serial.println(); SPrint("LeaseTm : "); Serial.print(DhcpLeaseTime); Serial.println();
#endif #endif
} }
} }
@ -146,7 +146,7 @@ void dhcpRequest(void)
dhcpSetOption(packet->options, DHCP_OPT_DHCPMSGTYPE, 1, &val); dhcpSetOption(packet->options, DHCP_OPT_DHCPMSGTYPE, 1, &val);
#ifdef DHCP_DEBUG #ifdef DHCP_DEBUG
SPrint_P(PSTR("DHCP: Sending Query\r\n")); SPrint("DHCP: Sending Query\r\n");
//dhcpPrintHeader(packet); //dhcpPrintHeader(packet);
#endif #endif
@ -187,7 +187,7 @@ void dhcpRelease(void)
optptr = dhcpSetOption(optptr, DHCP_OPT_REQUESTEDIP, 4, &packet->bootp.ciaddr); optptr = dhcpSetOption(optptr, DHCP_OPT_REQUESTEDIP, 4, &packet->bootp.ciaddr);
#ifdef DHCP_DEBUG #ifdef DHCP_DEBUG
SPrint_P(PSTR("DHCP: Sending Release to ")); netPrintIPAddr(DhcpServerIP); Serial.println(); SPrint("DHCP: Sending Release to "); netPrintIPAddr(DhcpServerIP); Serial.println();
//dhcpPrintHeader(packet); //dhcpPrintHeader(packet);
#endif #endif
@ -273,28 +273,28 @@ uint8_t* dhcpSetOption(uint8_t* options, uint8_t optcode, uint8_t optlen, uint32
#ifdef DHCP_DEBUG_PRINT #ifdef DHCP_DEBUG_PRINT
void dhcpPrintHeader(struct netDhcpHeader* packet) void dhcpPrintHeader(struct netDhcpHeader* packet)
{ {
SPrint_P(PSTR("DHCP Packet:\r\n")); SPrint("DHCP Packet:\r\n");
// print op // print op
SPrint_P(PSTR("Op : ")); SPrint("Op : ");
switch(packet->bootp.op) switch(packet->bootp.op)
{ {
case BOOTP_OP_BOOTREQUEST: SPrint_P(PSTR("BOOTREQUEST")); break; case BOOTP_OP_BOOTREQUEST: SPrint("BOOTREQUEST"); break;
case BOOTP_OP_BOOTREPLY: SPrint_P(PSTR("BOOTREPLY")); break; case BOOTP_OP_BOOTREPLY: SPrint("BOOTREPLY"); break;
default: SPrint_P(PSTR("UNKNOWN")); break; default: SPrint("UNKNOWN"); break;
} }
Serial.println(); Serial.println();
// print transaction ID // print transaction ID
SPrint_P(PSTR("XID : 0x")); Serial.printu32(packet->bootp.xid); Serial.println(); SPrint("XID : 0x"); Serial.printu32(packet->bootp.xid); Serial.println();
// print client IP address // print client IP address
SPrint_P(PSTR("ClIpAddr: ")); netPrintIPAddr(htonl(packet->bootp.ciaddr)); Serial.println(); SPrint("ClIpAddr: "); netPrintIPAddr(htonl(packet->bootp.ciaddr)); Serial.println();
// print 'your' IP address // print 'your' IP address
SPrint_P(PSTR("YrIpAddr: ")); netPrintIPAddr(htonl(packet->bootp.yiaddr)); Serial.println(); SPrint("YrIpAddr: "); netPrintIPAddr(htonl(packet->bootp.yiaddr)); Serial.println();
// print server IP address // print server IP address
SPrint_P(PSTR("SvIpAddr: ")); netPrintIPAddr(htonl(packet->bootp.siaddr)); Serial.println(); SPrint("SvIpAddr: "); netPrintIPAddr(htonl(packet->bootp.siaddr)); Serial.println();
// print gateway IP address // print gateway IP address
SPrint_P(PSTR("GwIpAddr: ")); netPrintIPAddr(htonl(packet->bootp.giaddr)); Serial.println(); SPrint("GwIpAddr: "); netPrintIPAddr(htonl(packet->bootp.giaddr)); Serial.println();
// print client hardware address // print client hardware address
SPrint_P(PSTR("ClHwAddr: ")); netPrintEthAddr((struct netEthAddr*)packet->bootp.chaddr); Serial.println(); SPrint("ClHwAddr: "); netPrintEthAddr((struct netEthAddr*)packet->bootp.chaddr); Serial.println();
} }
#endif #endif

@ -28,6 +28,8 @@
#include "programStrings.h" #include "programStrings.h"
// include configuration // include configuration
#include "nic.h"
#include "net.h"
#include "enc28j60conf.h" #include "enc28j60conf.h"
#include "enc28j60.h" #include "enc28j60.h"
@ -88,10 +90,12 @@ void nicSetMacAddress(uint8_t* macaddr)
enc28j60Write(MAADR0, *macaddr++); enc28j60Write(MAADR0, *macaddr++);
} }
#ifdef NET_DEBUG || NIC_DEBUG
void nicRegDump(void) void nicRegDump(void)
{ {
enc28j60RegDump(); enc28j60RegDump();
} }
#endif
void nicHardReset(void) void nicHardReset(void)
{ {
@ -333,33 +337,33 @@ void enc28j60Init(void)
delay(1); delay(1);
#ifdef DEBUG_ENC_INIT #ifdef DEBUG_ENC_INIT
SPrintln_P(PSTR("SPI Enabled")); SPrintln("SPI Enabled");
#endif #endif
// Reboot the ENC28J60 // Reboot the ENC28J60
enc28j60Reboot(); enc28j60Reboot();
#ifdef DEBUG_ENC_INIT #ifdef DEBUG_ENC_INIT
SPrintln_P(PSTR("PHY reboot done")); SPrintln("PHY reboot done");
#endif #endif
#ifdef ENC28J60_LAMPS_MODE #ifdef ENC28J60_LAMPS_MODE
SPrintln_P(PSTR("Custom lamps")); SPrintln("Custom lamps");
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)
{ {
SPrintln_P(PSTR("Full duplex lamps")); SPrintln("Full duplex lamps");
enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT); enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT);
} else { } else {
SPrintln_P(PSTR("Half duplex lamps")); SPrintln("Half duplex lamps");
enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT_HD); enc28j60PhyWrite(PHLCON, PHLCON_DEFAULT_HD);
} }
#endif #endif
#ifdef DEBUG_ENC_INIT #ifdef DEBUG_ENC_INIT
SPrintln_P(PSTR("Lamps set")); SPrintln("Lamps set");
#endif #endif
// do bank 0 stuff // do bank 0 stuff
// initialize receive buffer // initialize receive buffer
@ -443,7 +447,7 @@ void enc28j60Init(void)
enc28j60WriteOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN); enc28j60WriteOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);
#ifdef DEBUG_ENC_INIT #ifdef DEBUG_ENC_INIT
SPrintln_P(PSTR("Banks robbed")); SPrintln("Banks robbed");
#endif #endif
/* /*
enc28j60PhyWrite(PHLCON, 0x0AA2); enc28j60PhyWrite(PHLCON, 0x0AA2);
@ -539,12 +543,20 @@ void enc28j60PacketSend(uint16_t len, uint8_t* packet)
// send the contents of the transmit buffer onto the network // send the contents of the transmit buffer onto the network
enc28j60WriteOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRTS); enc28j60WriteOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_TXRTS);
// From errata Rev B.4
// if( (enc28j60Read(EIR) & EIR_TXERIF) ){ // if( (enc28j60Read(EIR) & EIR_TXERIF) ){
// enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_TXRTS); // enc28j60WriteOp(ENC28J60_BIT_FIELD_CLR, ECON1, ECON1_TXRTS);
// } // }
} }
#ifndef enc28j60ReadNextPtr
#define enc28j60ReadNextPtr(ptr) do { ptr = enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0); \
ptr |= enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0)<<8; \
} while(0)
#endif
uint16_t enc28j60PacketReceive(uint16_t maxlen, uint8_t* packet) uint16_t enc28j60PacketReceive(uint16_t maxlen, uint8_t* packet)
{ {
uint16_t rxstat; uint16_t rxstat;
@ -564,16 +576,13 @@ uint16_t enc28j60PacketReceive(uint16_t maxlen, uint8_t* packet)
enc28j60Write(ERDPTH, (NextPacketPtr)>>8); enc28j60Write(ERDPTH, (NextPacketPtr)>>8);
// read the next packet pointer // read the next packet pointer
NextPacketPtr = enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0); enc28j60ReadNextPtr(NextPacketPtr);
NextPacketPtr |= enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0)<<8;
// read the packet length // read the packet length
len = enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0); enc28j60ReadNextPtr(len);
len |= enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0)<<8;
// read the receive status // read the receive status
rxstat = enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0); enc28j60ReadNextPtr(rxstat);
rxstat |= enc28j60ReadOp(ENC28J60_READ_BUF_MEM, 0)<<8;
// limit retrieve length // limit retrieve length
// (we reduce the MAC-reported length by 4 to remove the CRC) // (we reduce the MAC-reported length by 4 to remove the CRC)
@ -619,95 +628,98 @@ void enc28j60ReceiveOverflowRecover(void)
// recovery completed // recovery completed
} }
#ifdef NET_DEBUG || NIC_DEBUG
#define SPrint_D(str) SPrint(str); delayMicroseconds(100);
void enc28j60RegDump(void) void enc28j60RegDump(void)
{ {
// unsigned char macaddr[6]; // unsigned char macaddr[6];
// result = ax88796Read(TR); // result = ax88796Read(TR);
// SPrint_P(PSTR("Media State: ")); // SPrint("Media State: ");
// if(!(result & AUTOD)) // if(!(result & AUTOD))
// SPrint_P(PSTR("Autonegotiation\r\n")); // SPrint("Autonegotiation\r\n");
// else if(result & RST_B) // else if(result & RST_B)
// SPrint_P(PSTR("PHY in Reset \r\n")); // SPrint("PHY in Reset \r\n");
// else if(!(result & RST_10B)) // else if(!(result & RST_10B))
// SPrint_P(PSTR("10BASE-T \r\n")); // SPrint("10BASE-T \r\n");
// else if(!(result & RST_TXB)) // else if(!(result & RST_TXB))
// SPrint_P(PSTR("100BASE-T \r\n")); // SPrint("100BASE-T \r\n");
SPrint_P(PSTR("RevID: ")); delay(5); SPrint_D("RevID: ");
Serial.println(enc28j60Read(EREVID), HEX); delay(5); Serial.println(enc28j60Read(EREVID), HEX);
SPrint_P(PSTR("Cntrl: ECON1 ECON2 ESTAT EIR EIE\r\n")); delay(5); SPrint_D("Cntrl: ECON1 ECON2 ESTAT EIR EIE\r\n");
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ECON1), HEX); delay(5); Serial.print(enc28j60Read(ECON1), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ECON2), HEX); delay(5); Serial.print(enc28j60Read(ECON2), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ESTAT), HEX); delay(5); Serial.print(enc28j60Read(ESTAT), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(EIR), HEX); delay(5); Serial.print(enc28j60Read(EIR), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(EIE), HEX); delay(5); Serial.print(enc28j60Read(EIE), HEX);
Serial.println(); delay(5); Serial.println();
SPrint_P(PSTR("MAC : MACON1 MACON2 MACON3 MACON4 MAC-Address\r\n")); delay(5); SPrint_D("MAC : MACON1 MACON2 MACON3 MACON4 MAC-Address\r\n");
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MACON1), HEX); delay(5); Serial.print(enc28j60Read(MACON1), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MACON2), HEX); delay(5); Serial.print(enc28j60Read(MACON2), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MACON3), HEX); delay(5); Serial.print(enc28j60Read(MACON3), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MACON4), HEX); delay(5); Serial.print(enc28j60Read(MACON4), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MAADR5), HEX); delay(5); Serial.print(enc28j60Read(MAADR5), HEX);
Serial.print(enc28j60Read(MAADR4), HEX); delay(5); Serial.print(enc28j60Read(MAADR4), HEX);
Serial.print(enc28j60Read(MAADR3), HEX); delay(5); Serial.print(enc28j60Read(MAADR3), HEX);
Serial.print(enc28j60Read(MAADR2), HEX); delay(5); Serial.print(enc28j60Read(MAADR2), HEX);
Serial.print(enc28j60Read(MAADR1), HEX); delay(5); Serial.print(enc28j60Read(MAADR1), HEX);
Serial.print(enc28j60Read(MAADR0), HEX); delay(5); Serial.print(enc28j60Read(MAADR0), HEX);
Serial.println(); delay(5); Serial.println();
SPrint_P(PSTR("Rx : ERXST ERXND ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\r\n")); delay(5); SPrint_D("Rx : ERXST ERXND ERXWRPT ERXRDPT ERXFCON EPKTCNT MAMXFL\r\n");
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ERXSTH), HEX); delay(5); Serial.print(enc28j60Read(ERXSTH), HEX);
Serial.print(enc28j60Read(ERXSTL), HEX); delay(5); Serial.print(enc28j60Read(ERXSTL), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ERXNDH), HEX); delay(5); Serial.print(enc28j60Read(ERXNDH), HEX);
Serial.print(enc28j60Read(ERXNDL), HEX); delay(5); Serial.print(enc28j60Read(ERXNDL), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ERXWRPTH), HEX); delay(5); Serial.print(enc28j60Read(ERXWRPTH), HEX);
Serial.print(enc28j60Read(ERXWRPTL), HEX); delay(5); Serial.print(enc28j60Read(ERXWRPTL), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ERXRDPTH), HEX); delay(5); Serial.print(enc28j60Read(ERXRDPTH), HEX);
Serial.print(enc28j60Read(ERXRDPTL), HEX); delay(5); Serial.print(enc28j60Read(ERXRDPTL), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ERXFCON), HEX); delay(5); Serial.print(enc28j60Read(ERXFCON), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(EPKTCNT), HEX); delay(5); Serial.print(enc28j60Read(EPKTCNT), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MAMXFLH), HEX); delay(5); Serial.print(enc28j60Read(MAMXFLH), HEX);
Serial.print(enc28j60Read(MAMXFLL), HEX); delay(5); Serial.print(enc28j60Read(MAMXFLL), HEX);
Serial.println(); delay(5); Serial.println();
SPrint_P(PSTR("Tx : ETXST ETXND MACLCON1 MACLCON2 MAPHSUP\r\n")); delay(5); SPrint_D("Tx : ETXST ETXND MACLCON1 MACLCON2 MAPHSUP\r\n");
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ETXSTH), HEX); delay(5); Serial.print(enc28j60Read(ETXSTH), HEX);
Serial.print(enc28j60Read(ETXSTL), HEX); delay(5); Serial.print(enc28j60Read(ETXSTL), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(ETXNDH), HEX); delay(5); Serial.print(enc28j60Read(ETXNDH), HEX);
Serial.print(enc28j60Read(ETXNDL), HEX); delay(5); Serial.print(enc28j60Read(ETXNDL), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MACLCON1), HEX); delay(5); Serial.print(enc28j60Read(MACLCON1), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MACLCON2), HEX); delay(5); Serial.print(enc28j60Read(MACLCON2), HEX);
SPrint_P(PSTR(" ")); delay(5); SPrint_D(" ");
Serial.print(enc28j60Read(MAPHSUP), HEX); delay(5); Serial.print(enc28j60Read(MAPHSUP), HEX);
Serial.println(); delay(5); Serial.println();
//delay_ms(25); delay(5); SPrint_D("PHLCON: 00");
delay(25); Serial.println((long int)(enc28j60PhyRead(PHLCON)), BIN);
delay(1);
} }
#endif

@ -25,14 +25,11 @@
#include "programStrings.h" #include "programStrings.h"
#include "HardwareSerial.h" #include "HardwareSerial.h"
#endif #endif
//extern void nicSend(unsigned int len, unsigned char* packet);
// global variables
// functions // functions
void icmpInit(void) void icmpInit(void)
{ {
} }
void icmpIpIn(icmpip_hdr* packet) void icmpIpIn(icmpip_hdr* packet)
@ -59,8 +56,6 @@ void icmpEchoRequest(icmpip_hdr* packet)
// recalculate checksum // recalculate checksum
packet->icmp.icmpchksum = 0; packet->icmp.icmpchksum = 0;
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)-IP_HEADER_LEN);
SPrint_P(PSTR("ICMP Checksum: "));
Serial.println((long int)packet->icmp.icmpchksum, HEX);
// return to sender // return to sender
tempIp = packet->ip.destipaddr; tempIp = packet->ip.destipaddr;
@ -68,15 +63,12 @@ void icmpEchoRequest(icmpip_hdr* packet)
packet->ip.srcipaddr = tempIp; packet->ip.srcipaddr = tempIp;
// Set the proper transaction id // Set the proper transaction id
tempId = packet->ip.ipid; tempId = packet->ip.ipid;
packet->ip.ipid = tempId; packet->ip.ipid = tempId;
SPrint_P(PSTR("ipID: "));
Serial.println((long int)tempId, HEX);
tempId = packet->icmp.id; tempId = packet->icmp.id;
packet->icmp.id = tempId; packet->icmp.id = tempId;
SPrint_P(PSTR("icmp.id: "));
Serial.println((long int)tempId, HEX);
// add ethernet routing // add ethernet routing
arpIpOut((struct netEthIpHeader*)(((u08*)packet)-ETH_HEADER_LEN), 0); arpIpOut((struct netEthIpHeader*)(((u08*)packet)-ETH_HEADER_LEN), 0);
@ -94,21 +86,21 @@ void icmpEchoRequest(icmpip_hdr* packet)
#ifdef ICMP_DEBUG_PRINT #ifdef ICMP_DEBUG_PRINT
void icmpPrintHeader(icmpip_hdr* packet) void icmpPrintHeader(icmpip_hdr* packet)
{ {
SPrint_P(PSTR("ICMP Packet:\r\n")); SPrint("ICMP Packet:\r\n");
// print source IP address // print source IP address
SPrint_P(PSTR("SrcIpAddr: ")); netPrintIPAddr(htonl(packet->ip.srcipaddr)); Serial.println(); SPrint("SrcIpAddr: "); netPrintIPAddr(htonl(packet->ip.srcipaddr)); Serial.println();
// print dest IP address // print dest IP address
SPrint_P(PSTR("DstIpAddr: ")); netPrintIPAddr(htonl(packet->ip.destipaddr)); Serial.println(); SPrint("DstIpAddr: "); netPrintIPAddr(htonl(packet->ip.destipaddr)); Serial.println();
// print type // print type
SPrint_P(PSTR("Type : ")); SPrint("Type : ");
switch(packet->icmp.type) switch(packet->icmp.type)
{ {
case ICMP_TYPE_ECHOREQUEST: SPrint_P(PSTR("ECHO REQUEST")); break; case ICMP_TYPE_ECHOREQUEST: SPrint("ECHO REQUEST"); break;
case ICMP_TYPE_ECHOREPLY: SPrint_P(PSTR("ECHO REPLY")); break; case ICMP_TYPE_ECHOREPLY: SPrint("ECHO REPLY"); break;
default: SPrint_P(PSTR("UNKNOWN")); break; default: SPrint("UNKNOWN"); break;
} }
Serial.println(); Serial.println();
// print code // print code
SPrint_P(PSTR("Code : 0x")); Serial.print(packet->icmp.icode); Serial.println(); SPrint("Code : 0x"); Serial.print(packet->icmp.icode); Serial.println();
} }
#endif #endif

@ -72,20 +72,20 @@ void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data)
len += IP_HEADER_LEN; len += IP_HEADER_LEN;
// fill IP header // fill IP header
ethIpHeader->ip.destipaddr = HTONL(dstIp); ethIpHeader->ip.destipaddr = HTONL(dstIp);
ethIpHeader->ip.srcipaddr = HTONL(IpMyConfig.ip); ethIpHeader->ip.srcipaddr = HTONL(IpMyConfig.ip);
ethIpHeader->ip.proto = protocol; ethIpHeader->ip.proto = protocol;
ethIpHeader->ip.len = htons(len); ethIpHeader->ip.len = htons(len);
ethIpHeader->ip.vhl = 0x45; ethIpHeader->ip.vhl = 0x45;
ethIpHeader->ip.tos = 0; ethIpHeader->ip.tos = 0;
ethIpHeader->ip.ipid = 0; ethIpHeader->ip.ipid = 0;
ethIpHeader->ip.ipoffset = 0; ethIpHeader->ip.ipoffset = 0;
ethIpHeader->ip.ttl = IP_TIME_TO_LIVE; ethIpHeader->ip.ttl = IP_TIME_TO_LIVE;
ethIpHeader->ip.ipchksum = 0; ethIpHeader->ip.ipchksum = 0x0000;
// calculate and apply IP checksum // calculate and apply IP checksum
// 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);
// add ethernet routing // add ethernet routing
// check if we need to send to gateway // check if we need to send to gateway
@ -93,14 +93,14 @@ void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data)
{ {
arpIpOut(ethIpHeader,0); // local send arpIpOut(ethIpHeader,0); // local send
#ifdef NET_DEBUG #ifdef NET_DEBUG
SPrint_P(PSTR("Sending IP packet on local net\r\n")); SPrint("Sending IP packet on local net\r\n");
#endif #endif
} }
else else
{ {
arpIpOut(ethIpHeader,IpMyConfig.gateway); // gateway send arpIpOut(ethIpHeader,IpMyConfig.gateway); // gateway send
#ifdef NET_DEBUG #ifdef NET_DEBUG
SPrint_P(PSTR("Sending IP packet to gateway\r\n")); SPrint("Sending IP packet to gateway\r\n");
#endif #endif
} }
@ -143,7 +143,7 @@ void udpSend(uint32_t dstIp, uint16_t dstPort, uint16_t len, uint8_t* data)
void ipPrintConfig(struct ipConfig* config) void ipPrintConfig(struct ipConfig* config)
{ {
SPrint_P(PSTR("IP Addr : ")); netPrintIPAddr(config->ip); Serial.println(); SPrint("IP Addr : "); netPrintIPAddr(config->ip); Serial.println();
SPrint_P(PSTR("Netmask : ")); netPrintIPAddr(config->netmask); Serial.println(); SPrint("Netmask : "); netPrintIPAddr(config->netmask); Serial.println();
SPrint_P(PSTR("Gateway : ")); netPrintIPAddr(config->gateway); Serial.println(); SPrint("Gateway : "); netPrintIPAddr(config->gateway); Serial.println();
} }

@ -18,21 +18,6 @@
#include "avrlibdefs.h" #include "avrlibdefs.h"
#include "avrlibtypes.h" #include "avrlibtypes.h"
/**
* Network support
**/
// PHY level
#include "nic.h"
#include "enc28j60.h"
// Net Stack
#include "net.h"
#include "netstack.h"
#include "arp.h"
#include "ip.h"
#include "icmp.h"
/** /**
* Definitions * Definitions
**/ **/

@ -6,25 +6,40 @@
#include "main.h" #include "main.h"
/**
* Network support
**/
// PHY level
#include "nic.h"
#include "enc28j60.h"
// Net Stack
#include "net.h"
#include "netstack.h"
#include "arp.h"
#include "ip.h"
#include "icmp.h"
struct netEthAddr myEthAddress; struct netEthAddr myEthAddress;
// prototypes // prototypes
void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet) void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet)
{ {
SPrint_P(PSTR("-1")); SPrint("-1");
u16 payloadlen=0; u16 payloadlen=0;
SPrint_P(PSTR("0")); SPrint("0");
u08* payloaddata=0; u08* payloaddata=0;
SPrint_P(PSTR("1")); SPrint("1");
u16 i; u16 i;
SPrint_P(PSTR("2")); SPrint("2");
// get UDP payload length // get UDP payload length
payloadlen = htons(packet->udp.udplen); payloadlen = htons(packet->udp.udplen);
payloadlen -= 8; // subtract header payloadlen -= 8; // subtract header
// get UDP payload data // get UDP payload data
payloaddata = &((unsigned char*)packet)[IP_HEADER_LEN+UDP_HEADER_LEN]; payloaddata = &((unsigned char*)packet)[IP_HEADER_LEN+UDP_HEADER_LEN];
SPrint_P(PSTR("UDP packet, len: ")); SPrint("UDP packet, len: ");
Serial.println((unsigned int)len, DEC); Serial.println((unsigned int)len, DEC);
// debugPrintHexTable(len, (unsigned char*)packet); // debugPrintHexTable(len, (unsigned char*)packet);
@ -49,16 +64,16 @@ void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet)
void netstackTCPIPProcess(unsigned int len, tcpip_hdr* packet) void netstackTCPIPProcess(unsigned int len, tcpip_hdr* packet)
{ {
SPrint_P(PSTR("Rvd tcp Packet: len=")); SPrint("Rvd tcp Packet: len=");
Serial.println((unsigned int)len, DEC); Serial.println((unsigned int)len, DEC);
} }
void processCommand(u16 len, u08* data) void processCommand(u16 len, u08* data)
{ {
SPrint_P(PSTR("Rvd UDP : CMD=")); SPrint("Rvd UDP : CMD=");
Serial.print(data[0], HEX); Serial.print(data[0], HEX);
SPrint_P(PSTR(" ARG0=")); SPrint(" ARG0=");
Serial.println(data[1], HEX); Serial.println(data[1], HEX);
// do something based on command // do something based on command
@ -71,7 +86,7 @@ void processCommand(u16 len, u08* data)
PORTC = data[1]; PORTC = data[1];
break; break;
default: default:
SPrintln_P(PSTR("Unkown UDP command")); SPrintln("Unkown UDP command");
break; break;
} }
} }
@ -91,113 +106,78 @@ void serviceLocal(void)
{ {
/* /*
case 'a': // Assert CS case 'a': // Assert CS
{
cbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS); cbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS);
} break; break;
case 's': // reSet CS case 's': // reSet CS
{
sbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS); sbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS);
} break; break;
*/ */
case 'a' : case 'a' :
{
arpPrintTable(); arpPrintTable();
} break; break;
case 'd': case 'd':
{
nicRegDump(); nicRegDump();
} break; break;
case 'i': case 'i':
{
nicInit(); nicInit();
} break; break;
case 'n': // Netstack init case 'n': // Netstack init
{
nicSoftReset();
netstackInit(IPADDRESS, NETMASK, GATEWAY); netstackInit(IPADDRESS, NETMASK, GATEWAY);
} break; break;
case 'b': // reBoot case 'b': // reBoot
{
nicReboot(); nicReboot();
} break; break;
case 'v': // reVision case 'v': // reVision
{ SPrint("RevID: ");
SPrint_P(PSTR("RevID: "));
Serial.println((byte)(enc28j60Read(EREVID)), BIN); Serial.println((byte)(enc28j60Read(EREVID)), BIN);
} break; break;
case 'p': case 'p':
{ SPrintln("IP Configs");
SPrintln_P(PSTR("IP Configs")); SPrint("IP:");
SPrint_P(PSTR("IP:"));
Serial.println(ipGetConfig()->ip); Serial.println(ipGetConfig()->ip);
SPrint_P(PSTR("Gateway:")); SPrint("Gateway:");
Serial.println(ipGetConfig()->gateway); Serial.println(ipGetConfig()->gateway);
SPrint_P(PSTR("Netmask:")); SPrint("Netmask:");
Serial.println(ipGetConfig()->netmask); Serial.println(ipGetConfig()->netmask);
} break; break;
/* /*
case 'x': case 'x':
{
char b[2048] = "a"; char b[2048] = "a";
unsigned int i = 0; unsigned int i = 0;
Serial.println(); Serial.println();
for (i = 1; 1; i++) { for (i = 1; 1; i++) {
SPrint_P(PSTR(".")); delay(250); SPrint("."); delay(250);
b[i] = 255; b[i] = 255;
} }
}
break; break;
*/ */
case '1':
enc28j60PhyWrite(PHLCON, 0x3992); // Off/On
break;
case '2':
enc28j60PhyWrite(PHLCON, 0x3882); // On/On
break;
case '3':
// A: Display link status and transmit/receive activity B: Display link status and receive activity
enc28j60PhyWrite(PHLCON, 0x3DC2);
break;
case '4':
enc28j60PhyWrite(PHLCON, 0x3DE2); // Default(?)
break;
case 'l': // read LED
SPrint_P(PSTR("PHLCON: 00"));
Serial.println((long int)(enc28j60PhyRead(PHLCON)), BIN);
break;
case 'u': case 'u':
{ SPrintln("Sending UDP packet");
SPrintln_P(PSTR("Sending UDP packet"));
strcpy(buffer[ETH_HEADER_LEN+IP_HEADER_LEN+UDP_HEADER_LEN], "hello"); strcpy(buffer[ETH_HEADER_LEN+IP_HEADER_LEN+UDP_HEADER_LEN], "hello");
SPrintln_P(PSTR("Buffered string")); SPrintln("Buffered string");
udpSend(IPDOT(192l,168l,0l,12l), CONTROL_PORT, 6, (uint8_t*)buffer[ETH_HEADER_LEN+IP_HEADER_LEN+UDP_HEADER_LEN]); udpSend(IPDOT(192l,168l,0l,12l), CONTROL_PORT, 6, (uint8_t*)buffer[ETH_HEADER_LEN+IP_HEADER_LEN+UDP_HEADER_LEN]);
SPrintln_P(PSTR("UDP Sent")); SPrintln("UDP Sent");
} break; break;
case '?': case '?':
{ SPrintln("Usage:");
SPrintln_P(PSTR("Usage:")); SPrintln("(a) Print ARP table");
SPrintln_P(PSTR("(a) Print ARP table")); SPrintln("(d) Dump nic registers");
SPrintln_P(PSTR("(d) Dump nic registers")); SPrintln("(i) Nic initialization");
SPrintln_P(PSTR("(i) Nic initialization")); SPrintln("(n) Netstack initialization");
SPrintln_P(PSTR("(n) Netstack initialization")); SPrintln("(b) Reboot NIC");
SPrintln_P(PSTR("(b) Reboot NIC")); SPrintln("(v) Display NIC revision");
SPrintln_P(PSTR("(v) Display NIC revision")); SPrintln("(p) Print IP configuration");
SPrintln_P(PSTR("(p) Print IP configuration")); SPrintln("(u) Send UDP packet");
SPrintln_P(PSTR("(l) Display lamps register")); SPrintln("(?) Print this help");
SPrintln_P(PSTR("(1-4) Set lamps register")); break;
SPrintln_P(PSTR("(u) Send UDP packet"));
SPrintln_P(PSTR("(?) Print this help"));
} break;
case '\n': case '\n':
default: default:
@ -206,7 +186,7 @@ void serviceLocal(void)
delay(10); delay(10);
// print new prompt // print new prompt
SPrint_P(PSTR("cmd> ")); SPrint("cmd> ");
} }
} }
@ -218,11 +198,11 @@ void setup(void)
delay(100); delay(100);
// init network stack // init network stack
SPrintln_P(PSTR("Initializing Network Stack!")); SPrintln("Initializing Network Stack!");
netstackInit(IPADDRESS, NETMASK, GATEWAY); netstackInit(IPADDRESS, NETMASK, GATEWAY);
SPrint_P(PSTR("Net Stack is up!")); SPrint("Net Stack is up!");
SPrint_P(PSTR("\r\ncmd> ")); SPrint("\r\ncmd> ");
} }
void loop(void) void loop(void)

@ -70,7 +70,7 @@ 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_P(PSTR("uip_ipchksum: sum 0x")); SPrintln("uip_ipchksum: sum 0x");
Serial.println((long int)sum, HEX); Serial.println((long int)sum, HEX);
return (sum == 0) ? 0xffff : htons(sum); return (sum == 0) ? 0xffff : htons(sum);
@ -116,65 +116,65 @@ void netPrintEthAddr(struct netEthAddr* ethaddr)
void netPrintIPAddr(uint32_t ipaddr) void netPrintIPAddr(uint32_t ipaddr)
{ {
Serial.print(((unsigned char*)&ipaddr)[3], DEC); Serial.print(((unsigned char*)&ipaddr)[3], DEC);
SPrint_P(PSTR(".")); SPrint(".");
Serial.print(((unsigned char*)&ipaddr)[2], DEC); Serial.print(((unsigned char*)&ipaddr)[2], DEC);
SPrint_P(PSTR(".")); SPrint(".");
Serial.print(((unsigned char*)&ipaddr)[1], DEC); Serial.print(((unsigned char*)&ipaddr)[1], DEC);
SPrint_P(PSTR(".")); SPrint(".");
Serial.print(((unsigned char*)&ipaddr)[0], DEC); Serial.print(((unsigned char*)&ipaddr)[0], DEC);
} }
/* /*
void netPrintEthHeader(struct netEthHeader* eth_hdr) void netPrintEthHeader(struct netEthHeader* eth_hdr)
{ {
SPrint_P(PSTR("Eth Packet Type: 0x")); SPrint("Eth Packet Type: 0x");
Serial.print(eth_hdr->type); Serial.print(eth_hdr->type);
SPrint_P(PSTR(" SRC:")); SPrint(" SRC:");
netPrintEthAddr(&eth_hdr->src); netPrintEthAddr(&eth_hdr->src);
SPrint_P(PSTR("->DST:")); SPrint("->DST:");
netPrintEthAddr(&eth_hdr->dest); netPrintEthAddr(&eth_hdr->dest);
Serial.println(); Serial.println();
} }
void netPrintIpHeader(struct netIpHeader* ipheader) void netPrintIpHeader(struct netIpHeader* ipheader)
{ {
SPrint_P(PSTR("IP Header\r\n")); SPrint("IP Header\r\n");
SPrint_P(PSTR("Ver : %d\r\n", (ipheader->vhl)>>4); SPrint_P(PSTR("Ver : %d\r\n", (ipheader->vhl)>>4);
SPrint_P(PSTR("Length : %d\r\n", htons(ipheader->len)); SPrint_P(PSTR("Length : %d\r\n", htons(ipheader->len));
if(ipheader->proto == IP_PROTO_ICMP) if(ipheader->proto == IP_PROTO_ICMP)
SPrint_P(PSTR("Protocol: ICMP\r\n")); SPrint("Protocol: ICMP\r\n");
else if(ipheader->proto == IP_PROTO_TCP) else if(ipheader->proto == IP_PROTO_TCP)
SPrint_P(PSTR("Protocol: TCP\r\n")); SPrint("Protocol: TCP\r\n");
else if(ipheader->proto == IP_PROTO_UDP) else if(ipheader->proto == IP_PROTO_UDP)
SPrint_P(PSTR("Protocol: UDP\r\n")); SPrint("Protocol: UDP\r\n");
else else
SPrint_P(PSTR("Protocol: %d\r\n", ipheader->proto); SPrint_P(PSTR("Protocol: %d\r\n", ipheader->proto);
SPrint_P(PSTR("SourceIP: ")); netPrintIPAddr(htonl(ipheader->srcipaddr)); Serial.println(); SPrint("SourceIP: "); netPrintIPAddr(htonl(ipheader->srcipaddr)); Serial.println();
SPrint_P(PSTR("Dest IP: ")); netPrintIPAddr(htonl(ipheader->destipaddr)); Serial.println(); SPrint("Dest IP: "); netPrintIPAddr(htonl(ipheader->destipaddr)); Serial.println();
} }
void netPrintTcpHeader(struct netTcpHeader* tcpheader) void netPrintTcpHeader(struct netTcpHeader* tcpheader)
{ {
SPrint_P(PSTR("TCP Header\r\n")); SPrint("TCP Header\r\n");
SPrint_P(PSTR("Src Port: %d\r\n", htons(tcpheader->srcport)); SPrint_P(PSTR("Src Port: %d\r\n", htons(tcpheader->srcport));
SPrint_P(PSTR("Dst Port: %d\r\n", htons(tcpheader->destport)); SPrint_P(PSTR("Dst Port: %d\r\n", htons(tcpheader->destport));
SPrint_P(PSTR("Seq Num : 0x")); Serial.printu32(htonl(tcpheader->seqno)); Serial.println(); SPrint("Seq Num : 0x"); Serial.printu32(htonl(tcpheader->seqno)); Serial.println();
SPrint_P(PSTR("Ack Num : 0x")); Serial.printu32(htonl(tcpheader->ackno)); Serial.println(); SPrint("Ack Num : 0x"); Serial.printu32(htonl(tcpheader->ackno)); Serial.println();
SPrint_P(PSTR("Flags : ")); SPrint("Flags : ");
if(tcpheader->flags & TCP_FLAGS_FIN) if(tcpheader->flags & TCP_FLAGS_FIN)
SPrint_P(PSTR("FIN ")); SPrint("FIN ");
if(tcpheader->flags & TCP_FLAGS_SYN) if(tcpheader->flags & TCP_FLAGS_SYN)
SPrint_P(PSTR("SYN ")); SPrint("SYN ");
if(tcpheader->flags & TCP_FLAGS_RST) if(tcpheader->flags & TCP_FLAGS_RST)
SPrint_P(PSTR("RST ")); SPrint("RST ");
if(tcpheader->flags & TCP_FLAGS_PSH) if(tcpheader->flags & TCP_FLAGS_PSH)
SPrint_P(PSTR("PSH ")); SPrint("PSH ");
if(tcpheader->flags & TCP_FLAGS_ACK) if(tcpheader->flags & TCP_FLAGS_ACK)
SPrint_P(PSTR("ACK ")); SPrint("ACK ");
if(tcpheader->flags & TCP_FLAGS_URG) if(tcpheader->flags & TCP_FLAGS_URG)
SPrint_P(PSTR("URG ")); SPrint("URG ");
Serial.println(); Serial.println();
} }
*/ */

@ -28,17 +28,17 @@ void netstackInit(uint32_t ipaddress, uint32_t netmask, uint32_t gatewayip)
{ {
// init network device driver // init network device driver
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("Initializing Network Device\r\n")); SPrint("Initializing Network Device\r\n");
#endif #endif
nicInit(); nicInit();
// init ARP // init ARP
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("Initializing ARP cache\r\n")); SPrint("Initializing ARP cache\r\n");
#endif #endif
arpInit(); arpInit();
// init addressing // init addressing
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("Initializing Addressing\r\n")); SPrint("Initializing Addressing\r\n");
#endif #endif
ipSetConfig(ipaddress, netmask, gatewayip); ipSetConfig(ipaddress, netmask, gatewayip);
} }
@ -61,31 +61,31 @@ int netstackService(void)
ethPacket = (struct netEthHeader*)&NetBuffer[0]; ethPacket = (struct netEthHeader*)&NetBuffer[0];
#if NET_DEBUG >= 5 #if NET_DEBUG >= 5
SPrint_P(PSTR("Received packet len: ")); SPrint("Received packet len: ");
Serial.print((unsigned int)len); Serial.print((unsigned int)len);
SPrint_P(PSTR(", type:")); SPrint(", type:");
if(ethPacket->type == htons(ETHTYPE_IP)) if(ethPacket->type == htons(ETHTYPE_IP))
{ {
SPrint_P(PSTR("IP")); SPrint("IP");
} }
else if(ethPacket->type == htons(ETHTYPE_ARP)) else if(ethPacket->type == htons(ETHTYPE_ARP))
{ {
SPrint_P(PSTR("ARP")); SPrint("ARP");
} }
SPrint_P(PSTR("Packet Contents\r\n")); SPrint("Packet Contents\r\n");
debugPrintHexTable(len, NetBuffer); debugPrintHexTable(len, NetBuffer);
#endif #endif
if(ethPacket->type == htons(ETHTYPE_IP)) if(ethPacket->type == htons(ETHTYPE_IP))
{ {
// process an IP packet // process an IP packet
#ifdef NETSTACK_DEBUG
SPrint_P(PSTR("NET Rx: IP packet\r\n"));
#endif
// add the source to the ARP cache // add the source to the ARP cache
// also correctly set the ethernet packet length before processing? // also correctly set the ethernet packet length before processing?
arpIpIn((struct netEthIpHeader*)&NetBuffer[0]); arpIpIn((struct netEthIpHeader*)&NetBuffer[0]);
#ifdef NETSTACK_DEBUG
SPrint("NET Rx: IP packet\r\n");
arpPrintTable(); arpPrintTable();
#endif
netstackIPProcess( len-ETH_HEADER_LEN, (ip_hdr*)&NetBuffer[ETH_HEADER_LEN] ); netstackIPProcess( len-ETH_HEADER_LEN, (ip_hdr*)&NetBuffer[ETH_HEADER_LEN] );
} }
@ -93,9 +93,9 @@ int netstackService(void)
{ {
// process an ARP packet // process an ARP packet
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("NET Rx: ARP packet\r\n")); SPrint("NET Rx: ARP packet\r\n");
#endif
arpPrintTable(); arpPrintTable();
#endif
arpArpIn(len, ((struct netEthArpHeader*)&NetBuffer[0]) ); arpArpIn(len, ((struct netEthArpHeader*)&NetBuffer[0]) );
} }
} }
@ -105,7 +105,7 @@ int netstackService(void)
void netstackIPProcess(unsigned int len, ip_hdr* packet) void netstackIPProcess(unsigned int len, ip_hdr* packet)
{ {
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("entering netstackIPProcess\r\n")); SPrint("entering netstackIPProcess\r\n");
//icmpPrintHeader((icmpip_hdr*)packet); //icmpPrintHeader((icmpip_hdr*)packet);
#endif #endif
// check IP addressing, stop processing if not for me and not a broadcast // check IP addressing, stop processing if not for me and not a broadcast
@ -118,7 +118,7 @@ void netstackIPProcess(unsigned int len, ip_hdr* packet)
if( packet->proto == IP_PROTO_ICMP ) if( packet->proto == IP_PROTO_ICMP )
{ {
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("NET Rx: ICMP/IP packet\r\n")); SPrint("NET Rx: ICMP/IP packet\r\n");
//icmpPrintHeader((icmpip_hdr*)packet); //icmpPrintHeader((icmpip_hdr*)packet);
#endif #endif
icmpIpIn((icmpip_hdr*)packet); icmpIpIn((icmpip_hdr*)packet);
@ -126,7 +126,7 @@ void netstackIPProcess(unsigned int len, ip_hdr* packet)
else if( packet->proto == IP_PROTO_UDP ) else if( packet->proto == IP_PROTO_UDP )
{ {
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("NET Rx: UDP/IP packet\r\n")); SPrint("NET Rx: UDP/IP packet\r\n");
//debugPrintHexTable(NetBufferLen-14, &NetBuffer[14]); //debugPrintHexTable(NetBufferLen-14, &NetBuffer[14]);
#endif #endif
netstackUDPIPProcess(len, ((udpip_hdr*)packet) ); netstackUDPIPProcess(len, ((udpip_hdr*)packet) );
@ -134,14 +134,14 @@ void netstackIPProcess(unsigned int len, ip_hdr* packet)
else if( packet->proto == IP_PROTO_TCP ) else if( packet->proto == IP_PROTO_TCP )
{ {
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("NET Rx: TCP/IP packet\r\n")); SPrint("NET Rx: TCP/IP packet\r\n");
#endif #endif
netstackTCPIPProcess(len, ((tcpip_hdr*)packet) ); netstackTCPIPProcess(len, ((tcpip_hdr*)packet) );
} }
else else
{ {
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("NET Rx: IP packet\r\n")); SPrint("NET Rx: IP packet\r\n");
#endif #endif
} }
} }
@ -149,13 +149,13 @@ void netstackIPProcess(unsigned int len, ip_hdr* packet)
void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet) void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet)
{ {
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("NetStack UDP/IP Rx Dummy Handler\r\n")); SPrint("NetStack UDP/IP Rx Dummy Handler\r\n");
#endif #endif
} }
void netstackTCPIPProcess(unsigned int len, tcpip_hdr* packet) void netstackTCPIPProcess(unsigned int len, tcpip_hdr* packet)
{ {
#ifdef NETSTACK_DEBUG #ifdef NETSTACK_DEBUG
SPrint_P(PSTR("NetStack TCP/IP Rx Dummy Handler\r\n")); SPrint("NetStack TCP/IP Rx Dummy Handler\r\n");
#endif #endif
} }

@ -27,6 +27,8 @@
#ifndef NIC_H #ifndef NIC_H
#define NIC_H #define NIC_H
#define NIC_DEBUG
#include <inttypes.h> #include <inttypes.h>
//! Initialize network interface hardware. //! Initialize network interface hardware.

@ -2,7 +2,23 @@
#define isDef_programStrings #define isDef_programStrings
#ifndef nop #ifndef nop
#define nop() asm volatile ("nop") #define nop() asm volatile ("nop")
#endif
#ifndef SPrint
#define SPrint(str) SPrint_P(PSTR(str))
#endif
#ifndef SPrintln
#define SPrintln(str) SPrintln_P(PSTR(str))
#endif
#ifndef SPrintHex
#define SPrintHex(str) SPrintHex_P(PSTR(str))
#endif
#ifndef SPrint
#define SPrintlnHez(str) SPrintlnHex_P(PSTR(str))
#endif #endif
void SPrint_P(const char *data); void SPrint_P(const char *data);

Loading…
Cancel
Save