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 ' return 0;' >> applet/$(TARGET).cpp
echo '}' >> applet/$(TARGET).cpp
echo >> applet/$(TARGET).cpp
elf: applet/$(TARGET).elf
hex: applet/$(TARGET).hex

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

@ -42,8 +42,8 @@ void debugPrintHexTable(u16 length, u08 *buffer)
buf = buffer;
// 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_P(PSTR(" ----------------------------------------------- ---- ASCII -----\r\n"));
SPrint(" 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF\r\n");
SPrint(" ----------------------------------------------- ---- ASCII -----\r\n");
// print the data
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
// print spaces
SPrint_P(PSTR(" "));
SPrint(" ");
}
}

@ -67,7 +67,7 @@ void dhcpIn(unsigned int len, struct netDhcpHeader* packet)
// get DHCP server ID
dhcpGetOption(packet->options, DHCP_OPT_SERVERID, 4, &sid);
#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
// 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);
#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
// send DHCP request
DhcpServerIP = htonl(sid);
@ -110,11 +110,11 @@ void dhcpIn(unsigned int len, struct netDhcpHeader* packet)
ipSetConfig(htonl(packet->bootp.yiaddr), netmask, gateway);
#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));
// print info
ipPrintConfig(ipGetConfig());
SPrint_P(PSTR("LeaseTm : ")); Serial.print(DhcpLeaseTime); Serial.println();
SPrint("LeaseTm : "); Serial.print(DhcpLeaseTime); Serial.println();
#endif
}
}
@ -146,7 +146,7 @@ void dhcpRequest(void)
dhcpSetOption(packet->options, DHCP_OPT_DHCPMSGTYPE, 1, &val);
#ifdef DHCP_DEBUG
SPrint_P(PSTR("DHCP: Sending Query\r\n"));
SPrint("DHCP: Sending Query\r\n");
//dhcpPrintHeader(packet);
#endif
@ -187,7 +187,7 @@ void dhcpRelease(void)
optptr = dhcpSetOption(optptr, DHCP_OPT_REQUESTEDIP, 4, &packet->bootp.ciaddr);
#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);
#endif
@ -273,28 +273,28 @@ uint8_t* dhcpSetOption(uint8_t* options, uint8_t optcode, uint8_t optlen, uint32
#ifdef DHCP_DEBUG_PRINT
void dhcpPrintHeader(struct netDhcpHeader* packet)
{
SPrint_P(PSTR("DHCP Packet:\r\n"));
SPrint("DHCP Packet:\r\n");
// print op
SPrint_P(PSTR("Op : "));
SPrint("Op : ");
switch(packet->bootp.op)
{
case BOOTP_OP_BOOTREQUEST: SPrint_P(PSTR("BOOTREQUEST")); break;
case BOOTP_OP_BOOTREPLY: SPrint_P(PSTR("BOOTREPLY")); break;
default: SPrint_P(PSTR("UNKNOWN")); break;
case BOOTP_OP_BOOTREQUEST: SPrint("BOOTREQUEST"); break;
case BOOTP_OP_BOOTREPLY: SPrint("BOOTREPLY"); break;
default: SPrint("UNKNOWN"); break;
}
Serial.println();
// 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
SPrint_P(PSTR("ClIpAddr: ")); netPrintIPAddr(htonl(packet->bootp.ciaddr)); Serial.println();
SPrint("ClIpAddr: "); netPrintIPAddr(htonl(packet->bootp.ciaddr)); Serial.println();
// 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
SPrint_P(PSTR("SvIpAddr: ")); netPrintIPAddr(htonl(packet->bootp.siaddr)); Serial.println();
SPrint("SvIpAddr: "); netPrintIPAddr(htonl(packet->bootp.siaddr)); Serial.println();
// 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
SPrint_P(PSTR("ClHwAddr: ")); netPrintEthAddr((struct netEthAddr*)packet->bootp.chaddr); Serial.println();
SPrint("ClHwAddr: "); netPrintEthAddr((struct netEthAddr*)packet->bootp.chaddr); Serial.println();
}
#endif

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

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

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

@ -18,21 +18,6 @@
#include "avrlibdefs.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
**/

@ -6,25 +6,40 @@
#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;
// prototypes
void netstackUDPIPProcess(unsigned int len, udpip_hdr* packet)
{
SPrint_P(PSTR("-1"));
SPrint("-1");
u16 payloadlen=0;
SPrint_P(PSTR("0"));
SPrint("0");
u08* payloaddata=0;
SPrint_P(PSTR("1"));
SPrint("1");
u16 i;
SPrint_P(PSTR("2"));
SPrint("2");
// get UDP payload length
payloadlen = htons(packet->udp.udplen);
payloadlen -= 8; // subtract header
// get UDP payload data
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);
// 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)
{
SPrint_P(PSTR("Rvd tcp Packet: len="));
SPrint("Rvd tcp Packet: len=");
Serial.println((unsigned int)len, DEC);
}
void processCommand(u16 len, u08* data)
{
SPrint_P(PSTR("Rvd UDP : CMD="));
SPrint("Rvd UDP : CMD=");
Serial.print(data[0], HEX);
SPrint_P(PSTR(" ARG0="));
SPrint(" ARG0=");
Serial.println(data[1], HEX);
// do something based on command
@ -71,7 +86,7 @@ void processCommand(u16 len, u08* data)
PORTC = data[1];
break;
default:
SPrintln_P(PSTR("Unkown UDP command"));
SPrintln("Unkown UDP command");
break;
}
}
@ -91,113 +106,78 @@ void serviceLocal(void)
{
/*
case 'a': // Assert CS
{
cbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS);
} break;
break;
case 's': // reSet CS
{
sbi(ENC28J60_CONTROL_PORT, ENC28J60_CONTROL_CS);
} break;
break;
*/
case 'a' :
{
arpPrintTable();
} break;
break;
case 'd':
{
nicRegDump();
} break;
break;
case 'i':
{
nicInit();
} break;
break;
case 'n': // Netstack init
{
nicSoftReset();
netstackInit(IPADDRESS, NETMASK, GATEWAY);
} break;
break;
case 'b': // reBoot
{
nicReboot();
} break;
break;
case 'v': // reVision
{
SPrint_P(PSTR("RevID: "));
SPrint("RevID: ");
Serial.println((byte)(enc28j60Read(EREVID)), BIN);
} break;
break;
case 'p':
{
SPrintln_P(PSTR("IP Configs"));
SPrint_P(PSTR("IP:"));
SPrintln("IP Configs");
SPrint("IP:");
Serial.println(ipGetConfig()->ip);
SPrint_P(PSTR("Gateway:"));
SPrint("Gateway:");
Serial.println(ipGetConfig()->gateway);
SPrint_P(PSTR("Netmask:"));
SPrint("Netmask:");
Serial.println(ipGetConfig()->netmask);
} break;
break;
/*
case 'x':
{
char b[2048] = "a";
unsigned int i = 0;
Serial.println();
for (i = 1; 1; i++) {
SPrint_P(PSTR(".")); delay(250);
SPrint("."); delay(250);
b[i] = 255;
}
}
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':
{
SPrintln_P(PSTR("Sending UDP packet"));
SPrintln("Sending UDP packet");
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]);
SPrintln_P(PSTR("UDP Sent"));
} break;
SPrintln("UDP Sent");
break;
case '?':
{
SPrintln_P(PSTR("Usage:"));
SPrintln_P(PSTR("(a) Print ARP table"));
SPrintln_P(PSTR("(d) Dump nic registers"));
SPrintln_P(PSTR("(i) Nic initialization"));
SPrintln_P(PSTR("(n) Netstack initialization"));
SPrintln_P(PSTR("(b) Reboot NIC"));
SPrintln_P(PSTR("(v) Display NIC revision"));
SPrintln_P(PSTR("(p) Print IP configuration"));
SPrintln_P(PSTR("(l) Display lamps register"));
SPrintln_P(PSTR("(1-4) Set lamps register"));
SPrintln_P(PSTR("(u) Send UDP packet"));
SPrintln_P(PSTR("(?) Print this help"));
} break;
SPrintln("Usage:");
SPrintln("(a) Print ARP table");
SPrintln("(d) Dump nic registers");
SPrintln("(i) Nic initialization");
SPrintln("(n) Netstack initialization");
SPrintln("(b) Reboot NIC");
SPrintln("(v) Display NIC revision");
SPrintln("(p) Print IP configuration");
SPrintln("(u) Send UDP packet");
SPrintln("(?) Print this help");
break;
case '\n':
default:
@ -206,7 +186,7 @@ void serviceLocal(void)
delay(10);
// print new prompt
SPrint_P(PSTR("cmd> "));
SPrint("cmd> ");
}
}
@ -218,11 +198,11 @@ void setup(void)
delay(100);
// init network stack
SPrintln_P(PSTR("Initializing Network Stack!"));
SPrintln("Initializing Network Stack!");
netstackInit(IPADDRESS, NETMASK, GATEWAY);
SPrint_P(PSTR("Net Stack is up!"));
SPrint_P(PSTR("\r\ncmd> "));
SPrint("Net Stack is up!");
SPrint("\r\ncmd> ");
}
void loop(void)

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

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

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

@ -2,7 +2,23 @@
#define isDef_programStrings
#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
void SPrint_P(const char *data);

Loading…
Cancel
Save