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

link/link.cpp

Committer:
andrewboyson
Date:
2017-12-14
Revision:
59:e0e556c8bd46

File content as of revision 59:e0e556c8bd46:

#include   "mbed.h"
#include     "io.h"
#include    "log.h"
#include    "eth.h"
#include "action.h"
#include    "mac.h"
#include    "net.h"
#include   "link.h"
#include    "nic.h"

DigitalIn phyLinkNeg (P1_25);
DigitalIn phySpeedNeg(P1_26);

bool LinkTrace = false;

static int count = 0;
static void lights()
{
    if (count)
    {
        IoEthLedGrL = false;
        IoEthLedYeR = false;
        count--;
    }
    else
    {
        IoEthLedGrL = !phyLinkNeg;
        IoEthLedYeR = !phySpeedNeg;
    }
}
static void lightsBlink()
{
    count = 500; //==50ms at 100ns per scan
}
void LinkMain()
{
    //Flash lights
    lights();
    
    if (!NicLinkIsUp()) return;
    
    //Handle packets
    int sizeRx;
    int sizeTx;
    char* pRx = NicGetReceivedPacketOrNull(&sizeRx);
    char* pTx = NicGetTransmitPacketOrNull(&sizeTx);
    
    int action = DO_NOTHING;
    
    NetTraceHostResetMatched();
    
    if (pRx)
    {
        lightsBlink();
        
        if (pTx) action = EthHandlePacket(pRx, sizeRx, pTx, &sizeTx);
    
        NicReleaseReceivedPacket();
    }
    if (pTx)
    {
        if (!action) action = EthPollForPacketToSend(pTx, &sizeTx);
    
        if ( action)
        {
            lightsBlink();
            NicSendTransmitPacket(sizeTx);
        }
    }
}

void LinkInit()
{
    phyLinkNeg.mode(PullUp);
    phySpeedNeg.mode(PullUp);
    NicInit();
    NicLinkAddress(MacLocal);
    if (LinkTrace)
    {
        Log("\r\n");
        LogTime("MAC: ");
        MacLog(MacLocal);
        Log("\r\n");
    }
}