Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaning up #134

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions EtherCard.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
#include "enc28j60.h"
#include "net.h"

const uint8_t allOnes[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Used for hardware (MAC) and IP broadcast addresses

/** This type definition defines the structure of a UDP server event handler callback funtion */
typedef void (*UdpServerCallback)(
uint16_t dest_port, ///< Port the packet was sent to
Expand Down
14 changes: 5 additions & 9 deletions dhcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ typedef struct {
// options
} DHCPdata;

#define DHCP_SRC_PORT 67
#define DHCP_DEST_PORT 68
#define DHCP_SRC_PORT 68
#define DHCP_DEST_PORT 67

// timeouts im ms
#define DHCP_REQUEST_TIMEOUT 10000
Expand All @@ -103,8 +103,6 @@ static byte* bufPtr;
static uint8_t dhcpCustomOptionNum = 0;
static DhcpOptionCallback dhcpCustomOptionCallback = NULL;

// static uint8_t allOnes[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

static void addToBuf (byte b) {
*bufPtr++ = b;
}
Expand Down Expand Up @@ -145,13 +143,11 @@ static void addBytes (byte len, const byte* data) {

static void send_dhcp_message (void) {

uint8_t allOnes[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

memset(gPB, 0, UDP_DATA_P + sizeof( DHCPdata ));

EtherCard::udpPrepare(DHCP_DEST_PORT,
EtherCard::udpPrepare(DHCP_SRC_PORT,
(dhcpState == DHCP_STATE_BOUND ? EtherCard::dhcpip : allOnes),
DHCP_SRC_PORT); // SRC<->DST ??
DHCP_DEST_PORT);

if (dhcpState != DHCP_STATE_BOUND)
EtherCard::copyMac(gPB + ETH_DST_MAC, allOnes); //force broadcast mac
Expand Down Expand Up @@ -258,7 +254,7 @@ static bool dhcp_received_message_type (uint16_t len, byte msgType) {
// Map struct onto payload
DHCPdata *dhcpPtr = (DHCPdata*) (gPB + UDP_DATA_P);

if (len >= 70 && gPB[UDP_SRC_PORT_L_P] == DHCP_SRC_PORT &&
if (len >= 70 && gPB[UDP_SRC_PORT_L_P] == DHCP_DEST_PORT && //If the packet is coming from where we sent it
dhcpPtr->xid == currentXid ) {

byte *ptr = (byte*) (dhcpPtr + 1) + 4;
Expand Down
16 changes: 2 additions & 14 deletions enc28j60.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ bool ENC28J60::broadcast_enabled = false;
#define EPKTCNT (0x19|0x20)
// Bank 2 registers
#define MACON1 (0x00|0x40|0x80)
#define MACON2 (0x01|0x40|0x80)
#define MACON3 (0x02|0x40|0x80)
#define MACON4 (0x03|0x40|0x80)
#define MABBIPG (0x04|0x40|0x80)
Expand Down Expand Up @@ -153,13 +152,6 @@ bool ENC28J60::broadcast_enabled = false;
#define MACON1_RXPAUS 0x04
#define MACON1_PASSALL 0x02
#define MACON1_MARXEN 0x01
// ENC28J60 MACON2 Register Bit Definitions
#define MACON2_MARST 0x80
#define MACON2_RNDRST 0x40
#define MACON2_MARXRST 0x08
#define MACON2_RFUNRST 0x04
#define MACON2_MATXRST 0x02
#define MACON2_TFUNRST 0x01
// ENC28J60 MACON3 Register Bit Definitions
#define MACON3_PADCFG2 0x80
#define MACON3_PADCFG1 0x40
Expand Down Expand Up @@ -383,11 +375,8 @@ byte ENC28J60::initialize (uint16_t size, const byte* macaddr, byte csPin) {
writeReg(ERXND, RXSTOP_INIT);
writeReg(ETXST, TXSTART_INIT);
writeReg(ETXND, TXSTOP_INIT);
enableBroadcast(); // change to add ERXFCON_BCEN recommended by epam
writeReg(EPMM0, 0x303f);
writeReg(EPMCS, 0xf7f9);
writeRegByte(MACON1, MACON1_MARXEN|MACON1_TXPAUS|MACON1_RXPAUS);
writeRegByte(MACON2, 0x00);
broadcast_enabled = true;
writeRegByte(MACON1, MACON1_MARXEN);
writeOp(ENC28J60_BIT_FIELD_SET, MACON3,
MACON3_PADCFG0|MACON3_TXCRCEN|MACON3_FRMLNEN);
writeReg(MAIPG, 0x0C12);
Expand All @@ -401,7 +390,6 @@ byte ENC28J60::initialize (uint16_t size, const byte* macaddr, byte csPin) {
writeRegByte(MAADR0, macaddr[5]);
writePhy(PHCON2, PHCON2_HDLDIS);
SetBank(ECON1);
writeOp(ENC28J60_BIT_FIELD_SET, EIE, EIE_INTIE|EIE_PKTIE);
writeOp(ENC28J60_BIT_FIELD_SET, ECON1, ECON1_RXEN);

byte rev = readRegByte(EREVID);
Expand Down
2 changes: 1 addition & 1 deletion enc28j60.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ENC28J60 {

/** @brief Initialise network interface
* @param size Size of data buffer
* @param macaddr Pointer to 4 byte hardware (MAC) address
* @param macaddr Pointer to 6 byte hardware (MAC) address
* @param csPin Arduino pin used for chip select (enable network interface SPI bus). Default = 8
* @return <i>uint8_t</i> ENC28J60 firmware version or zero on failure.
*/
Expand Down
1 change: 0 additions & 1 deletion tcpip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ static unsigned long SEQ; // TCP/IP sequence number
const unsigned char arpreqhdr[] PROGMEM = { 0,1,8,0,6,4,0,1 }; // ARP request header
const unsigned char iphdr[] PROGMEM = { 0x45,0,0,0x82,0,0,0x40,0,0x20 }; //IP header
const unsigned char ntpreqhdr[] PROGMEM = { 0xE3,0,4,0xFA,0,1,0,0,0,1 }; //NTP request header
const uint8_t allOnes[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; // Used for hardware (MAC) and IP broadcast addresses

static void fill_checksum(uint8_t dest, uint8_t off, uint16_t len,uint8_t type) {
const uint8_t* ptr = gPB + off;
Expand Down