Zoltan Hudak / RawEthernet

Dependents:   RawEthernet_Hello

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RawEthernet.h Source File

RawEthernet.h

00001 #ifndef RAW_ETHERNET_H
00002 #define RAW_ETHERNET_H
00003 
00004 #include "mbed.h"
00005 
00006 //------------------------------------------------------------------------------
00007 // Functions for using an ENC28J60 ethernet controller, optimized for 
00008 // communication speed.
00009 //
00010 // Written by Rogier Schouten for AVR micro controllers http://www.rogiershikes.tk
00011 // Based on code by Guido Socher http://www.tuxgraphics.org
00012 // Idea modified and further updated by Guido Socher
00013 // Ported to MBED by Zoltan Hudak hudakz@outlook.com
00014 // License: GPL V2
00015 //
00016 // Enables to read sensor data or control IO-ports with less than a millisecond delay. 
00017 // The trick is in removing the TCP/IP overhead and use raw Ethernet frames. 
00018 // A Linux application using raw network sockets is controlling this slave device.
00019 //
00020 // Assumptions:
00021 // - Max. payload data: 255 bytes per packet
00022 // - The network consists of a master PC and one or more slave devices. 
00023 //   Only plain hubs and switches are allowed between PC and ethernet device.
00024 //   Note that some wlan routers have internal switches which switch only
00025 //   IP packets. They will discard plain ethernet frames. A normal 100Mbit 
00026 //   office/workgroup switch will however work.
00027 // 
00028 // Based on these assumptions, we can optimize the protocol for faster communication:
00029 // - Master and slave send unicast packets.
00030 // - We use raw ethernet and no higher-level protocol such as UDP or TCP/IP
00031 // - We use the EtherType field of a packet as a length field. Actually, we only
00032 //   use one byte of it since we have max 255 length packets.
00033 //  
00034 // Furthermore, there are a few code optimizations:
00035 // - Minimize communication between ENC and MCU.
00036 // - No unnecessary memory bank checks
00037 // 
00038 
00039 class RawEthernet
00040 {
00041 public:
00042     /**
00043      * Constructor
00044      *
00045      * \param mosi mbed pin to use SPI
00046      * \param miso mbed pin to use SPI
00047      * \param sclk mbed pin to use SPI
00048      * \param cs pin to select the ENC28J60 chip
00049      */
00050     RawEthernet(PinName mosi, PinName miso, PinName sclk, PinName cs, uint8_t myMac[6], uint8_t myIp[4]);
00051     void     linkTo(uint8_t remoteMac[6]);
00052     void     packetSend(uint16_t len, uint8_t* packet);
00053     bool     isLinkUp(void);
00054     uint8_t  receive(uint8_t* buf, uint8_t maxlen);
00055     void     gratuitousArp();
00056     void     send(uint8_t* buf, uint8_t len);
00057     uint8_t  getRev(void);
00058 protected:
00059     uint8_t  readOp(uint8_t op, uint8_t address);
00060     void     writeOp(uint8_t op, uint8_t address, uint8_t data);
00061     uint8_t  readReg(uint8_t address);
00062     void     writeReg(uint8_t address, uint8_t data);
00063     void     setBank(uint8_t address);
00064     void     readBuffer(uint16_t len, uint8_t* data);
00065     void     writeBuffer(uint16_t len, uint8_t* data);
00066     uint16_t phyReadH(uint8_t address);
00067     void     phyWrite(uint8_t address, uint16_t data);
00068 private:
00069     SPI         _spi;
00070     DigitalOut  _cs;
00071     uint8_t     _bank;
00072     int16_t     _nextPacketPtr;
00073     uint8_t     _lastTransmitPayloadLen;
00074     uint8_t     _mac[6];
00075     uint8_t     _ip[4];
00076     uint8_t     _remoteMac[6];
00077     static char _arpReqHdr[10];
00078 };
00079 #endif // RAW_ETHERNET_H