//------------------------------------------------------------------------------
static tOplkError eventCbPowerlink(tOplkApiEventType EventType_p,
                                   tOplkApiEventArg* pEventArg_p, void* pUserArg_p)
{
    tOplkError                      ret = kErrorOk;
    tOplkApiEventReceivedNonPlk*    pFrameInfo = &pEventArg_p->receivedEth;

    UNUSED_PARAMETER(pUserArg_p);

    switch (EventType_p)
    {
        case kOplkApiEventNmtStateChange:
            lcd_printNmtState(pEventArg_p->nmtStateChange.newNmtState);

            switch (pEventArg_p->nmtStateChange.newNmtState)
            {
                case kNmtGsOff:
                    // NMT state machine was shut down
                    ret = kErrorShutdown;

                    // NMT off state is reached
                    instance_l.fGsOff = TRUE;
                    break;

                case kNmtCsBasicEthernet:
                    // ARP demo: Send request to MN
                    arp_sendRequest((0xFFFFFF00 & IP_ADDR) | C_ADR_MN_DEF_NODE_ID);
                    break;

                default:
                    break;
            }
            break;

        case kOplkApiEventReceivedNonPlk:
            if (arp_processReceive(pFrameInfo->pFrame, pFrameInfo->frameSize) == 0)
                return kErrorOk;

            // If you get here, the received Ethernet frame is no ARP frame.
            // Here you can call other protocol stacks for processing.

            ret = kErrorOk; // Frame wasn't processed, so simply dump it.
            break;

        case kOplkApiEventDefaultGwChange:
            // ARP demo: Set default gateway and send request
            arp_setDefGateway(pEventArg_p->defaultGwChange.defaultGateway);

            arp_sendRequest(pEventArg_p->defaultGwChange.defaultGateway);
            break;

        default:
            break;
    }
    return ret;
}
//------------------------------------------------------------------------------
static tOplkError eventCbPowerlink(tOplkApiEventType EventType_p,
                                   tOplkApiEventArg* pEventArg_p, void* pUserArg_p)
{
    tOplkError ret = kErrorOk;

    UNUSED_PARAMETER(pUserArg_p);

    // Handle ARP demo:
    // - If a node is found (e.g. plug in a CN), then an ARP request is sent
    //   to this node. There is no timeout handling done.
    // - If an Ethernet frame is received, it is forwarded to process reply.
    //   The function prints the ARP reply information.
    if (EventType_p == kOplkApiEventNode)
    {
        tOplkApiEventNode* pNode = &pEventArg_p->nodeEvent;

        // Note: This is a demonstration to generate non-POWERLINK frames.
        if (pNode->nodeEvent == kNmtNodeEventFound)
        {
            arp_sendRequest((0xFFFFFF00 & IP_ADDR) | pNode->nodeId);
        }
    }
    else if (EventType_p == kOplkApiEventReceivedNonPlk)
    {
        tOplkApiEventReceivedNonPlk* pFrameInfo = &pEventArg_p->receivedEth;

        // Note: This is a demonstration how to forward Ethernet frames to upper
        //       layers. Instead you can insert an IP stack and forward the
        //       pFrameInfo content to it. Frames that are sent from the IP
        //       stack to lower layers must be forwarded with the function
        //       \ref oplk_sendEthFrame.

        // Forward received frame to ARP processing
        if (arp_processReceive(pFrameInfo->pFrame, pFrameInfo->frameSize) == 0)
            return kErrorOk;

        // If you get here, the received Ethernet frame is no ARP frame.
        // Here you can call other protocol stacks for processing.

        ret = kErrorOk; // Frame wasn't processed, so simply dump it.
    }
    else if (EventType_p == kOplkApiEventDefaultGwChange)
    {
        // ARP demo: Set default gateway and send request
        arp_setDefGateway(pEventArg_p->defaultGwChange.defaultGateway);

        arp_sendRequest(pEventArg_p->defaultGwChange.defaultGateway);
    }

    return ret;
}
//------------------------------------------------------------------------------
static tOplkError initPowerlink(tInstance* pInstance_p)
{
    tOplkError                  ret = kErrorOk;
    static tOplkApiInitParam    initParam;

    PRINTF("Initializing openPOWERLINK stack...\n");

    memset(&initParam, 0, sizeof(initParam));
    initParam.sizeOfInitParam = sizeof(initParam);

    initParam.nodeId = pInstance_p->nodeId;
    initParam.ipAddress = (0xFFFFFF00 & IP_ADDR) | initParam.nodeId;

    memcpy(initParam.aMacAddress, pInstance_p->aMacAddr, sizeof(initParam.aMacAddress));

    initParam.fAsyncOnly              = FALSE;
    initParam.featureFlags            = -1;
    initParam.cycleLen                = pInstance_p->cycleLen;  // required for error detection
    initParam.isochrTxMaxPayload      = 36;                     // const
    initParam.isochrRxMaxPayload      = 36;                     // const
    initParam.presMaxLatency          = 2000;                   // const; only required for IdentRes
    initParam.asndMaxLatency          = 2000;                   // const; only required for IdentRes
    initParam.preqActPayloadLimit     = 36;                     // required for initialization (+28 bytes)
    initParam.presActPayloadLimit     = 36;                     // required for initialization of Pres frame (+28 bytes)
    initParam.multiplCylceCnt         = 0;                      // required for error detection
    initParam.asyncMtu                = 300;                    // required to set up max frame size
    initParam.prescaler               = 2;                      // required for sync
    initParam.lossOfFrameTolerance    = 100000;
    initParam.asyncSlotTimeout        = 3000000;
    initParam.waitSocPreq             = 0;
    initParam.deviceType              = -1;               // NMT_DeviceType_U32
    initParam.vendorId                = -1;               // NMT_IdentityObject_REC.VendorId_U32
    initParam.productCode             = -1;               // NMT_IdentityObject_REC.ProductCode_U32
    initParam.revisionNumber          = -1;               // NMT_IdentityObject_REC.RevisionNo_U32
    initParam.serialNumber            = -1;               // NMT_IdentityObject_REC.SerialNo_U32
    initParam.applicationSwDate       = 0;
    initParam.applicationSwTime       = 0;
    initParam.subnetMask              = SUBNET_MASK;
    initParam.defaultGateway          = DEFAULT_GATEWAY;
    sprintf((char*)initParam.sHostname, "%02x-%08x", initParam.nodeId, initParam.vendorId);
    initParam.syncNodeId              = C_ADR_SYNC_ON_SOC;
    initParam.fSyncOnPrcNode          = FALSE;

    // set callback functions
    initParam.pfnCbEvent = processEvents;
    initParam.pfnCbSync  = processSync;

    // initialize POWERLINK stack
    ret = oplk_init(&initParam);
    if (ret != kErrorOk)
    {
        PRINTF("oplk_init() failed with \"%s\"\n(Error:0x%x!)\n", debugstr_getRetValStr(ret), ret);
        return ret;
    }

    // Set real MAC address to ARP module
    oplk_getEthMacAddr(initParam.aMacAddress);
    arp_setMacAddr(initParam.aMacAddress);

    // Set IP address to ARP module
    arp_setIpAddr(initParam.ipAddress);

    // Set default gateway to ARP module
    arp_setDefGateway(initParam.defaultGateway);

    return kErrorOk;
}
示例#4
0
//------------------------------------------------------------------------------
static tOplkError eventCbPowerlink(tOplkApiEventType eventType_p,
                                   tOplkApiEventArg* pEventArg_p, void* pUserArg_p)
{
    tOplkError                      ret = kErrorOk;
    tOplkApiEventReceivedNonPlk*    pFrameInfo = &pEventArg_p->receivedEth;

    UNUSED_PARAMETER(pUserArg_p);

    switch (eventType_p)
    {
        case kOplkApiEventNmtStateChange:
            lcd_printNmtState(pEventArg_p->nmtStateChange.newNmtState);

            switch (pEventArg_p->nmtStateChange.newNmtState)
            {
                case kNmtGsOff:
                    // NMT state machine was shut down
                    ret = kErrorShutdown;

                    // NMT off state is reached
                    instance_l.fGsOff = TRUE;
                    break;

                case kNmtCsBasicEthernet:
                    // ARP demo: Send request to MN
                    arp_sendRequest((0xFFFFFF00 & IP_ADDR) | C_ADR_MN_DEF_NODE_ID);
                    break;

                case kNmtCsPreOperational2:
                     // automatic change to kEplNmtCsReadyToOperate can be
                     // prevented with:
                     // ret = kErrorReject;
                     // As soon as application is ready for OPERATIONAL state,
                     // execute the following function call:
                     // oplk_execNmtCommand(kNmtEventEnterReadyToOperate);
                     break;

                default:
                    break;
            }
            break;

        case kOplkApiEventReceivedNonPlk:
            if (arp_processReceive(pFrameInfo->pFrame, pFrameInfo->frameSize) == 0)
                return kErrorOk;

            // If you get here, the received Ethernet frame is no ARP frame.
            // Here you can call other protocol stacks for processing.

            ret = kErrorOk; // Frame wasn't processed, so simply dump it.
            break;

        case kOplkApiEventDefaultGwChange:
            // ARP demo: Set default gateway and send request
            arp_setDefGateway(pEventArg_p->defaultGwChange.defaultGateway);

            arp_sendRequest(pEventArg_p->defaultGwChange.defaultGateway);
            break;

        default:
            break;
    }
    return ret;
}