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

Committer:
andrewboyson
Date:
Thu Jan 11 17:38:21 2018 +0000
Revision:
61:aad055f1b0d1
Parent:
link/link.cpp@59:e0e556c8bd46
Child:
63:9d67a5eaa93c
Removed dependence on Mbed OS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
andrewboyson 61:aad055f1b0d1 1 #include <stdbool.h>
andrewboyson 61:aad055f1b0d1 2
andrewboyson 61:aad055f1b0d1 3 #include "peripherals.h"
andrewboyson 59:e0e556c8bd46 4 #include "log.h"
andrewboyson 59:e0e556c8bd46 5 #include "eth.h"
andrewboyson 59:e0e556c8bd46 6 #include "action.h"
andrewboyson 59:e0e556c8bd46 7 #include "mac.h"
andrewboyson 59:e0e556c8bd46 8 #include "net.h"
andrewboyson 59:e0e556c8bd46 9 #include "link.h"
andrewboyson 59:e0e556c8bd46 10 #include "nic.h"
andrewboyson 59:e0e556c8bd46 11
andrewboyson 61:aad055f1b0d1 12 #define phyLinkNeg_MASK 1UL << 25 //P1.25 input
andrewboyson 61:aad055f1b0d1 13 #define phySpeedNeg_MASK 1UL << 26 //P1.26 input
andrewboyson 61:aad055f1b0d1 14 #define ethLedGrL_MASK 1UL << 31 //P1.31 ==> p20 output
andrewboyson 61:aad055f1b0d1 15 #define ethLedYeR_MASK 1UL << 30 //P1.30 ==> p19 output
andrewboyson 61:aad055f1b0d1 16
andrewboyson 59:e0e556c8bd46 17
andrewboyson 59:e0e556c8bd46 18 bool LinkTrace = false;
andrewboyson 59:e0e556c8bd46 19
andrewboyson 59:e0e556c8bd46 20 static int count = 0;
andrewboyson 59:e0e556c8bd46 21 static void lights()
andrewboyson 59:e0e556c8bd46 22 {
andrewboyson 59:e0e556c8bd46 23 if (count)
andrewboyson 59:e0e556c8bd46 24 {
andrewboyson 61:aad055f1b0d1 25 LPC_GPIO1->FIOCLR = ethLedGrL_MASK;
andrewboyson 61:aad055f1b0d1 26 LPC_GPIO1->FIOCLR = ethLedYeR_MASK;
andrewboyson 59:e0e556c8bd46 27 count--;
andrewboyson 59:e0e556c8bd46 28 }
andrewboyson 59:e0e556c8bd46 29 else
andrewboyson 59:e0e556c8bd46 30 {
andrewboyson 61:aad055f1b0d1 31 if (LPC_GPIO1->FIOPIN & phyLinkNeg_MASK) LPC_GPIO1->FIOCLR = ethLedGrL_MASK;
andrewboyson 61:aad055f1b0d1 32 else LPC_GPIO1->FIOSET = ethLedGrL_MASK;
andrewboyson 61:aad055f1b0d1 33 if (LPC_GPIO1->FIOPIN & phySpeedNeg_MASK) LPC_GPIO1->FIOCLR = ethLedYeR_MASK;
andrewboyson 61:aad055f1b0d1 34 else LPC_GPIO1->FIOSET = ethLedYeR_MASK;
andrewboyson 59:e0e556c8bd46 35 }
andrewboyson 59:e0e556c8bd46 36 }
andrewboyson 59:e0e556c8bd46 37 static void lightsBlink()
andrewboyson 59:e0e556c8bd46 38 {
andrewboyson 59:e0e556c8bd46 39 count = 500; //==50ms at 100ns per scan
andrewboyson 59:e0e556c8bd46 40 }
andrewboyson 59:e0e556c8bd46 41 void LinkMain()
andrewboyson 59:e0e556c8bd46 42 {
andrewboyson 59:e0e556c8bd46 43 //Flash lights
andrewboyson 59:e0e556c8bd46 44 lights();
andrewboyson 59:e0e556c8bd46 45
andrewboyson 59:e0e556c8bd46 46 if (!NicLinkIsUp()) return;
andrewboyson 59:e0e556c8bd46 47
andrewboyson 59:e0e556c8bd46 48 //Handle packets
andrewboyson 59:e0e556c8bd46 49 int sizeRx;
andrewboyson 59:e0e556c8bd46 50 int sizeTx;
andrewboyson 59:e0e556c8bd46 51 char* pRx = NicGetReceivedPacketOrNull(&sizeRx);
andrewboyson 59:e0e556c8bd46 52 char* pTx = NicGetTransmitPacketOrNull(&sizeTx);
andrewboyson 59:e0e556c8bd46 53
andrewboyson 59:e0e556c8bd46 54 int action = DO_NOTHING;
andrewboyson 59:e0e556c8bd46 55
andrewboyson 59:e0e556c8bd46 56 NetTraceHostResetMatched();
andrewboyson 59:e0e556c8bd46 57
andrewboyson 59:e0e556c8bd46 58 if (pRx)
andrewboyson 59:e0e556c8bd46 59 {
andrewboyson 59:e0e556c8bd46 60 lightsBlink();
andrewboyson 59:e0e556c8bd46 61
andrewboyson 59:e0e556c8bd46 62 if (pTx) action = EthHandlePacket(pRx, sizeRx, pTx, &sizeTx);
andrewboyson 59:e0e556c8bd46 63
andrewboyson 59:e0e556c8bd46 64 NicReleaseReceivedPacket();
andrewboyson 59:e0e556c8bd46 65 }
andrewboyson 59:e0e556c8bd46 66 if (pTx)
andrewboyson 59:e0e556c8bd46 67 {
andrewboyson 59:e0e556c8bd46 68 if (!action) action = EthPollForPacketToSend(pTx, &sizeTx);
andrewboyson 59:e0e556c8bd46 69
andrewboyson 59:e0e556c8bd46 70 if ( action)
andrewboyson 59:e0e556c8bd46 71 {
andrewboyson 59:e0e556c8bd46 72 lightsBlink();
andrewboyson 59:e0e556c8bd46 73 NicSendTransmitPacket(sizeTx);
andrewboyson 59:e0e556c8bd46 74 }
andrewboyson 59:e0e556c8bd46 75 }
andrewboyson 59:e0e556c8bd46 76 }
andrewboyson 59:e0e556c8bd46 77
andrewboyson 59:e0e556c8bd46 78 void LinkInit()
andrewboyson 59:e0e556c8bd46 79 {
andrewboyson 61:aad055f1b0d1 80 LPC_GPIO1->FIODIR |= ethLedGrL_MASK; //Set the direction to 1 == output
andrewboyson 61:aad055f1b0d1 81 LPC_GPIO1->FIODIR |= ethLedYeR_MASK; //Set the direction to 1 == output
andrewboyson 59:e0e556c8bd46 82 NicInit();
andrewboyson 59:e0e556c8bd46 83 NicLinkAddress(MacLocal);
andrewboyson 59:e0e556c8bd46 84 if (LinkTrace)
andrewboyson 59:e0e556c8bd46 85 {
andrewboyson 59:e0e556c8bd46 86 Log("\r\n");
andrewboyson 59:e0e556c8bd46 87 LogTime("MAC: ");
andrewboyson 59:e0e556c8bd46 88 MacLog(MacLocal);
andrewboyson 59:e0e556c8bd46 89 Log("\r\n");
andrewboyson 59:e0e556c8bd46 90 }
andrewboyson 59:e0e556c8bd46 91 }