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@13:9cd54f7db57a, 2017-05-01 (annotated)
- Committer:
- andrewboyson
- Date:
- Mon May 01 18:20:55 2017 +0000
- Revision:
- 13:9cd54f7db57a
- Parent:
- 10:f0854784e960
- Child:
- 14:e75a59c1123d
Added ability to read DNS queries with encoded IP addresses
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
andrewboyson | 0:faa09bd4e6bf | 1 | #include "mbed.h" |
andrewboyson | 0:faa09bd4e6bf | 2 | #include "io.h" |
andrewboyson | 4:31fa7d50722c | 3 | #include "log.h" |
andrewboyson | 0:faa09bd4e6bf | 4 | #include "eth.h" |
andrewboyson | 13:9cd54f7db57a | 5 | #include "mac.h" |
andrewboyson | 2:849103b5a16d | 6 | #include "net.h" |
andrewboyson | 7:b794780e33b4 | 7 | #include "phy.h" |
andrewboyson | 0:faa09bd4e6bf | 8 | |
andrewboyson | 0:faa09bd4e6bf | 9 | Ethernet eth; |
andrewboyson | 0:faa09bd4e6bf | 10 | |
andrewboyson | 2:849103b5a16d | 11 | DigitalIn phyLinkNeg (P1_25); |
andrewboyson | 2:849103b5a16d | 12 | DigitalIn phySpeedNeg(P1_26); |
andrewboyson | 0:faa09bd4e6bf | 13 | |
andrewboyson | 0:faa09bd4e6bf | 14 | |
andrewboyson | 13:9cd54f7db57a | 15 | static char buf[1600]; |
andrewboyson | 0:faa09bd4e6bf | 16 | |
andrewboyson | 0:faa09bd4e6bf | 17 | static Timer blinkTimer; |
andrewboyson | 0:faa09bd4e6bf | 18 | |
andrewboyson | 0:faa09bd4e6bf | 19 | static void blink() |
andrewboyson | 0:faa09bd4e6bf | 20 | { |
andrewboyson | 0:faa09bd4e6bf | 21 | blinkTimer.reset(); |
andrewboyson | 0:faa09bd4e6bf | 22 | blinkTimer.start(); |
andrewboyson | 0:faa09bd4e6bf | 23 | } |
andrewboyson | 0:faa09bd4e6bf | 24 | |
andrewboyson | 0:faa09bd4e6bf | 25 | static void updateLeds() |
andrewboyson | 0:faa09bd4e6bf | 26 | { |
andrewboyson | 0:faa09bd4e6bf | 27 | if (blinkTimer.read_ms() < 100) |
andrewboyson | 0:faa09bd4e6bf | 28 | { |
andrewboyson | 0:faa09bd4e6bf | 29 | IoEthLedGrL = 0; |
andrewboyson | 0:faa09bd4e6bf | 30 | IoEthLedYeR = 0; |
andrewboyson | 0:faa09bd4e6bf | 31 | } |
andrewboyson | 0:faa09bd4e6bf | 32 | else |
andrewboyson | 0:faa09bd4e6bf | 33 | { |
andrewboyson | 2:849103b5a16d | 34 | IoEthLedGrL = !phyLinkNeg ; |
andrewboyson | 2:849103b5a16d | 35 | IoEthLedYeR = !phySpeedNeg; |
andrewboyson | 0:faa09bd4e6bf | 36 | blinkTimer.stop(); |
andrewboyson | 0:faa09bd4e6bf | 37 | } |
andrewboyson | 0:faa09bd4e6bf | 38 | } |
andrewboyson | 0:faa09bd4e6bf | 39 | static int readPacket() |
andrewboyson | 0:faa09bd4e6bf | 40 | { |
andrewboyson | 0:faa09bd4e6bf | 41 | int sizeReceived = eth.receive(); |
andrewboyson | 2:849103b5a16d | 42 | if (sizeReceived) |
andrewboyson | 2:849103b5a16d | 43 | { |
andrewboyson | 2:849103b5a16d | 44 | eth.read(buf, sizeReceived); |
andrewboyson | 2:849103b5a16d | 45 | blink(); |
andrewboyson | 2:849103b5a16d | 46 | } |
andrewboyson | 0:faa09bd4e6bf | 47 | return sizeReceived; |
andrewboyson | 0:faa09bd4e6bf | 48 | } |
andrewboyson | 0:faa09bd4e6bf | 49 | |
andrewboyson | 0:faa09bd4e6bf | 50 | static void sendPacket(int sendLength) |
andrewboyson | 0:faa09bd4e6bf | 51 | { |
andrewboyson | 0:faa09bd4e6bf | 52 | eth.write(buf, sendLength); //Make a packet from the header (14 bytes) and the payload |
andrewboyson | 2:849103b5a16d | 53 | int sent = eth.send(); //Send the packet |
andrewboyson | 2:849103b5a16d | 54 | if (sent) blink(); |
andrewboyson | 4:31fa7d50722c | 55 | else LogTimeF("Packet of %d bytes not sent\r\n", sendLength); |
andrewboyson | 0:faa09bd4e6bf | 56 | } |
andrewboyson | 0:faa09bd4e6bf | 57 | |
andrewboyson | 2:849103b5a16d | 58 | static bool linkIsUp = false; |
andrewboyson | 2:849103b5a16d | 59 | static void checkLinkIsUp() |
andrewboyson | 2:849103b5a16d | 60 | { |
andrewboyson | 2:849103b5a16d | 61 | static Timer linkUpTimer; |
andrewboyson | 2:849103b5a16d | 62 | if (phyLinkNeg) //link is indicated as being down |
andrewboyson | 2:849103b5a16d | 63 | { |
andrewboyson | 2:849103b5a16d | 64 | linkIsUp = false; |
andrewboyson | 2:849103b5a16d | 65 | linkUpTimer.reset(); |
andrewboyson | 2:849103b5a16d | 66 | linkUpTimer.start(); |
andrewboyson | 2:849103b5a16d | 67 | } |
andrewboyson | 2:849103b5a16d | 68 | else //link is indicated as being up |
andrewboyson | 2:849103b5a16d | 69 | { |
andrewboyson | 2:849103b5a16d | 70 | if (linkUpTimer.read_ms() > 100) |
andrewboyson | 2:849103b5a16d | 71 | { |
andrewboyson | 2:849103b5a16d | 72 | linkIsUp = true; |
andrewboyson | 2:849103b5a16d | 73 | linkUpTimer.stop(); |
andrewboyson | 2:849103b5a16d | 74 | } |
andrewboyson | 2:849103b5a16d | 75 | else |
andrewboyson | 2:849103b5a16d | 76 | { |
andrewboyson | 2:849103b5a16d | 77 | linkUpTimer.start(); |
andrewboyson | 2:849103b5a16d | 78 | } |
andrewboyson | 2:849103b5a16d | 79 | } |
andrewboyson | 2:849103b5a16d | 80 | } |
andrewboyson | 2:849103b5a16d | 81 | |
andrewboyson | 2:849103b5a16d | 82 | void PhyMain() |
andrewboyson | 0:faa09bd4e6bf | 83 | { |
andrewboyson | 0:faa09bd4e6bf | 84 | updateLeds(); |
andrewboyson | 0:faa09bd4e6bf | 85 | |
andrewboyson | 2:849103b5a16d | 86 | checkLinkIsUp(); |
andrewboyson | 2:849103b5a16d | 87 | |
andrewboyson | 7:b794780e33b4 | 88 | int size = readPacket(); |
andrewboyson | 13:9cd54f7db57a | 89 | |
andrewboyson | 9:91dae5300a4d | 90 | int action = DO_NOTHING; |
andrewboyson | 0:faa09bd4e6bf | 91 | |
andrewboyson | 0:faa09bd4e6bf | 92 | //If a packet has been received then handle it |
andrewboyson | 7:b794780e33b4 | 93 | if (size) action = EthHandlePacket(&buf, &size); |
andrewboyson | 0:faa09bd4e6bf | 94 | |
andrewboyson | 0:faa09bd4e6bf | 95 | //If there is no packet to handle then see if there is a packet waiting to be sent |
andrewboyson | 9:91dae5300a4d | 96 | if (action == DO_NOTHING && linkIsUp) |
andrewboyson | 2:849103b5a16d | 97 | { |
andrewboyson | 10:f0854784e960 | 98 | action = EthPollForPacketToSend(&buf, &size); |
andrewboyson | 2:849103b5a16d | 99 | } |
andrewboyson | 0:faa09bd4e6bf | 100 | |
andrewboyson | 0:faa09bd4e6bf | 101 | //If there is a packet to send then send it |
andrewboyson | 13:9cd54f7db57a | 102 | if (action != DO_NOTHING) |
andrewboyson | 13:9cd54f7db57a | 103 | { |
andrewboyson | 13:9cd54f7db57a | 104 | sendPacket(size); |
andrewboyson | 13:9cd54f7db57a | 105 | } |
andrewboyson | 0:faa09bd4e6bf | 106 | } |
andrewboyson | 0:faa09bd4e6bf | 107 | |
andrewboyson | 2:849103b5a16d | 108 | void PhyInit() |
andrewboyson | 0:faa09bd4e6bf | 109 | { |
andrewboyson | 2:849103b5a16d | 110 | phyLinkNeg.mode(PullUp); |
andrewboyson | 2:849103b5a16d | 111 | phySpeedNeg.mode(PullUp); |
andrewboyson | 0:faa09bd4e6bf | 112 | blink(); |
andrewboyson | 13:9cd54f7db57a | 113 | eth.address(MacLocal); |
andrewboyson | 0:faa09bd4e6bf | 114 | char text[20]; |
andrewboyson | 13:9cd54f7db57a | 115 | NetMacToString(MacLocal, sizeof(text), text); |
andrewboyson | 4:31fa7d50722c | 116 | LogF("\r\n"); |
andrewboyson | 4:31fa7d50722c | 117 | LogTimeF("MAC: %s\r\n", text); |
andrewboyson | 0:faa09bd4e6bf | 118 | } |