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.c

Committer:
andrewboyson
Date:
2018-01-11
Revision:
61:aad055f1b0d1
Parent:
link/link.cpp@ 59:e0e556c8bd46
Child:
63:9d67a5eaa93c

File content as of revision 61:aad055f1b0d1:

#include <stdbool.h>

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

#define  phyLinkNeg_MASK 1UL << 25 //P1.25 input
#define phySpeedNeg_MASK 1UL << 26 //P1.26 input
#define   ethLedGrL_MASK 1UL << 31 //P1.31 ==> p20 output
#define   ethLedYeR_MASK 1UL << 30 //P1.30 ==> p19 output


bool LinkTrace = false;

static int count = 0;
static void lights()
{
    if (count)
    {
        LPC_GPIO1->FIOCLR = ethLedGrL_MASK;
        LPC_GPIO1->FIOCLR = ethLedYeR_MASK;
        count--;
    }
    else
    {
        if (LPC_GPIO1->FIOPIN &  phyLinkNeg_MASK) LPC_GPIO1->FIOCLR = ethLedGrL_MASK;
        else                                      LPC_GPIO1->FIOSET = ethLedGrL_MASK;
        if (LPC_GPIO1->FIOPIN & phySpeedNeg_MASK) LPC_GPIO1->FIOCLR = ethLedYeR_MASK;
        else                                      LPC_GPIO1->FIOSET = ethLedYeR_MASK;
    }
}
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()
{
    LPC_GPIO1->FIODIR |= ethLedGrL_MASK; //Set the direction to 1 == output
    LPC_GPIO1->FIODIR |= ethLedYeR_MASK; //Set the direction to 1 == output
    NicInit();
    NicLinkAddress(MacLocal);
    if (LinkTrace)
    {
        Log("\r\n");
        LogTime("MAC: ");
        MacLog(MacLocal);
        Log("\r\n");
    }
}