A stack which works with or without an Mbed os library. Provides IPv4 or IPv6 with a full 1500 byte buffer.
Dependents: oldheating gps motorhome heating
phy.cpp
- Committer:
- andrewboyson
- Date:
- 2017-05-01
- Revision:
- 13:9cd54f7db57a
- Parent:
- 10:f0854784e960
- Child:
- 14:e75a59c1123d
File content as of revision 13:9cd54f7db57a:
#include "mbed.h" #include "io.h" #include "log.h" #include "eth.h" #include "mac.h" #include "net.h" #include "phy.h" Ethernet eth; DigitalIn phyLinkNeg (P1_25); DigitalIn phySpeedNeg(P1_26); static char buf[1600]; static Timer blinkTimer; static void blink() { blinkTimer.reset(); blinkTimer.start(); } static void updateLeds() { if (blinkTimer.read_ms() < 100) { IoEthLedGrL = 0; IoEthLedYeR = 0; } else { IoEthLedGrL = !phyLinkNeg ; IoEthLedYeR = !phySpeedNeg; blinkTimer.stop(); } } static int readPacket() { int sizeReceived = eth.receive(); if (sizeReceived) { eth.read(buf, sizeReceived); blink(); } return sizeReceived; } static void sendPacket(int sendLength) { eth.write(buf, sendLength); //Make a packet from the header (14 bytes) and the payload int sent = eth.send(); //Send the packet if (sent) blink(); else LogTimeF("Packet of %d bytes not sent\r\n", sendLength); } static bool linkIsUp = false; static void checkLinkIsUp() { static Timer linkUpTimer; if (phyLinkNeg) //link is indicated as being down { linkIsUp = false; linkUpTimer.reset(); linkUpTimer.start(); } else //link is indicated as being up { if (linkUpTimer.read_ms() > 100) { linkIsUp = true; linkUpTimer.stop(); } else { linkUpTimer.start(); } } } void PhyMain() { updateLeds(); checkLinkIsUp(); int size = readPacket(); int action = DO_NOTHING; //If a packet has been received then handle it if (size) action = EthHandlePacket(&buf, &size); //If there is no packet to handle then see if there is a packet waiting to be sent if (action == DO_NOTHING && linkIsUp) { action = EthPollForPacketToSend(&buf, &size); } //If there is a packet to send then send it if (action != DO_NOTHING) { sendPacket(size); } } void PhyInit() { phyLinkNeg.mode(PullUp); phySpeedNeg.mode(PullUp); blink(); eth.address(MacLocal); char text[20]; NetMacToString(MacLocal, sizeof(text), text); LogF("\r\n"); LogTimeF("MAC: %s\r\n", text); }