//------------------------------------------------------------------------------
static tOplkError loopMain(tInstance* pInstance_p)
{
    tOplkError ret = kErrorOk;

    // start processing
    if ((ret = oplk_execNmtCommand(kNmtEventSwReset)) != kErrorOk)
        return ret;

    while (1)
    {
        // do background tasks
        if ((ret = oplk_process()) != kErrorOk)
            break;

        // trigger switch off
        if (pInstance_p->fShutdown != FALSE)
        {
            oplk_execNmtCommand(kNmtEventSwitchOff);

            // reset shutdown flag to generate only one switch off command
            pInstance_p->fShutdown = FALSE;
        }

        // exit loop if NMT is in off state
        if (pInstance_p->fGsOff != FALSE)
            break;
    }

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

    // start processing
    if ((ret = oplk_execNmtCommand(kNmtEventSwReset)) != kErrorOk)
        return ret;

    while (1)
    {
        // do background tasks
        if ((ret = oplk_process()) != kErrorOk)
            break;

        if (oplk_checkKernelStack() == FALSE)
        {
            PRINTF("Kernel stack has gone! Exiting...\n");
            instance_l.fShutdown = TRUE;
        }

        // trigger switch off
        if (pInstance_p->fShutdown != FALSE)
        {
            oplk_execNmtCommand(kNmtEventSwitchOff);

            // reset shutdown flag to generate only one switch off command
            pInstance_p->fShutdown = FALSE;
        }

        // exit loop if NMT is in off state
        if (pInstance_p->fGsOff != FALSE)
            break;

        switch (gpio_getAppInput())
        {
            case 0x01:
                PRINTF("KEY0: SwReset\n");
                lcd_printText("KEY0: SwReset", 2);
                ret = oplk_execNmtCommand(kNmtEventSwReset);
                break;

            case 0x02:
                PRINTF("KEY1: SwitchOff\n");
                lcd_printText("KEY1: SwitchOff", 2);
                ret = oplk_execNmtCommand(kNmtEventSwitchOff);
                break;

            default:
                break;
        }

        while (gpio_getAppInput() != 0);
    }

    return ret;
}
//------------------------------------------------------------------------------
static void shutdownPowerlink(void)
{
    UINT                i;

    // NMT_GS_OFF state has not yet been reached
    fGsOff_l = FALSE;

#if !defined(CONFIG_KERNELSTACK_DIRECTLINK) && defined(CONFIG_USE_SYNCTHREAD)
    system_stopSyncThread();
    system_msleep(100);
#endif

    // halt the NMT state machine so the processing of POWERLINK frames stops
    oplk_execNmtCommand(kNmtEventSwitchOff);

    // small loop to implement timeout waiting for thread to terminate
    for (i = 0; i < 1000; i++)
    {
        if (fGsOff_l)
            break;
    }

    printf("Stack is in state off ... Shutdown\n");
    oplk_shutdown();
}
/**
********************************************************************************
\brief  Destructor

Destructs a POWERLINK object.
*******************************************************************************/
Api::~Api()
{
    tOplkError          ret;

    ret = oplk_execNmtCommand(kNmtEventSwitchOff);
    pProcessThread->waitForNmtStateOff();
    ret = oplk_freeProcessImage();
    ret = oplk_shutdown();
}
//------------------------------------------------------------------------------
static tOplkError loopMain(tInstance* pInstance_p)
{
    tOplkError  ret = kErrorOk;
    UINT        checkStack = 0;

    // start processing
    if ((ret = oplk_execNmtCommand(kNmtEventSwReset)) != kErrorOk)
        return ret;

    while (1)
    {
        // do background tasks
        if ((ret = oplk_process()) != kErrorOk)
            break;

        // check the kernel part from time to time
        if (checkStack++ >= CHECK_KERNEL_TIMEOUT)
        {
            checkStack = 0;
            if (oplk_checkKernelStack() == FALSE)
            {
                PRINTF("Kernel stack has gone! Exiting...\n");
                instance_l.fShutdown = TRUE;
            }
        }

        // trigger switch off
        if (pInstance_p->fShutdown != FALSE)
        {
            oplk_execNmtCommand(kNmtEventSwitchOff);

            // reset shutdown flag to generate only one switch off command
            pInstance_p->fShutdown = FALSE;
        }

        // exit loop if NMT is in off state
        if (pInstance_p->fGsOff != FALSE)
            break;
    }

    return ret;
}
Exemple #6
0
/**
********************************************************************************
\brief  Destructor

Destructs a POWERLINK object.
*******************************************************************************/
Api::~Api()
{
    tOplkError  ret;

    pDataInOutThread->stop();
    pDataInOutThread->wait(100);            // wait until thread terminates (max 100ms)

    ret = oplk_execNmtCommand(kNmtEventSwitchOff);
    pProcessThread->waitForNmtStateOff();

    ret = oplk_freeProcessImage();
    ret = oplk_destroy();
    oplk_exit();
}
//------------------------------------------------------------------------------
static void loopMain(void)
{
    tOplkError              ret = kErrorOk;
    char                    cKey = 0;
    BOOL                    fExit = FALSE;

#if !defined(CONFIG_KERNELSTACK_DIRECTLINK)

#if defined(CONFIG_USE_SYNCTHREAD)
    system_startSyncThread(processSync);
#endif

#endif

    // start stack processing by sending a NMT reset command
    ret = oplk_execNmtCommand(kNmtEventSwReset);
    if (ret != kErrorOk)
    {
        return;
    }

    printf("\n-------------------------------\n");
    printf("Press Esc to leave the program\n");
    printf("Press r to reset the node\n");
    printf("-------------------------------\n\n");

    while (!fExit)
    {
        if (console_kbhit())
        {
            cKey = (char)console_getch();
            switch (cKey)
            {
                case 'r':
                    ret = oplk_execNmtCommand(kNmtEventSwReset);
                    if (ret != kErrorOk)
                    {
                        fExit = TRUE;
                    }
                    break;

                case 'c':
                    ret = oplk_execNmtCommand(kNmtEventNmtCycleError);
                    if (ret != kErrorOk)
                    {
                        fExit = TRUE;
                    }
                    break;

                case 0x1B:
                    fExit = TRUE;
                    break;

                default:
                    break;
            }
        }

        if (system_getTermSignalState() == TRUE)
        {
            fExit = TRUE;
            printf("Received termination signal, exiting...\n");
        }

        if (oplk_checkKernelStack() == FALSE)
        {
            fExit = TRUE;
            fprintf(stderr, "Kernel stack has gone! Exiting...\n");
        }

#if defined(CONFIG_USE_SYNCTHREAD) || defined(CONFIG_KERNELSTACK_DIRECTLINK)
        system_msleep(100);
#else
        processSync();
#endif

    }

#if (TARGET_SYSTEM == _WIN32_)
    printf("Press Enter to quit!\n");
    console_getch();
#endif

}
//------------------------------------------------------------------------------
static void loopMain(void)
{
    tOplkError              ret = kErrorOk;
    char                    cKey = 0;
    BOOL                    fExit = FALSE;

#if !defined(CONFIG_KERNELSTACK_DIRECTLINK)

#if defined(CONFIG_USE_SYNCTHREAD)
    system_startSyncThread(processSync);
#endif

#endif

    // start processing
    ret = oplk_execNmtCommand(kNmtEventSwReset);
    if (ret != kErrorOk)
    {
        return;
    }

    printf("start POWERLINK Stack... ok\n");
    printf("Digital I/O interface with openPOWERLINK is ready!\n");
    printf("\n-------------------------------\n");
    printf("Press Esc to leave the programm\n");
    printf("Press r to reset the node\n");
    printf("Press i to increase digital input\n");
    printf("Press d to decrease digital input\n");
    printf("Press p to print digital outputs\n");
    printf("-------------------------------\n\n");

    setupInputs();

    // wait for key hit
    while (!fExit)
    {
        if (console_kbhit())
        {
            cKey = (BYTE)console_getch();

            switch (cKey)
            {
                case 'r':
                    ret = oplk_execNmtCommand(kNmtEventSwReset);
                    if (ret != kErrorOk)
                    {
                        fExit = TRUE;
                    }
                    break;

                case 'i':
                    increaseInputs();
                    break;

                case 'd':
                    decreaseInputs();
                    break;

                case 'p':
                    printOutputs();
                    break;

                case 0x1B:
                    fExit = TRUE;
                    break;

                default:
                    break;
            }
        }

        if (system_getTermSignalState() == TRUE)
        {
            fExit = TRUE;
            printf("Received termination signal, exiting...\n");
        }

        if (oplk_checkKernelStack() == FALSE)
        {
            fExit = TRUE;
            fprintf(stderr, "Kernel stack has gone! Exiting...\n");
        }

#if defined(CONFIG_USE_SYNCTHREAD) || defined(CONFIG_KERNELSTACK_DIRECTLINK)
        system_msleep(100);
#else
        processSync();
#endif
    }

#if (TARGET_SYSTEM == _WIN32_)
    printf("Press Enter to quit!\n");
    console_getch();
#endif

    return;
}
//------------------------------------------------------------------------------
Api::Api(MainWindow* pMainWindow_p, UINT nodeId_p, QString devName_p)
{
    tOplkError          ret;
    State*              pState;
    Output*             pOutput;
    Input*              pInput;
    CnState*            pCnState;

    pState = pMainWindow_p->getStateWidget();
    pOutput = pMainWindow_p->getOutputWidget();
    pInput = pMainWindow_p->getInputWidget();
    pCnState = pMainWindow_p->getCnStateWidget();

    pProcessThread = new ProcessThread(pMainWindow_p);
    QObject::connect(pProcessThread, SIGNAL(oplkStatusChanged(int)),
                     pState, SLOT(setStatusLed(int)));
    QObject::connect(pProcessThread, SIGNAL(nmtStateChanged(const QString&)),
                     pState, SLOT(setNmtStateText(const QString &)));

    QObject::connect(pProcessThread, SIGNAL(nodeAppeared(int)),
                     pInput, SLOT(addNode(int)));
    QObject::connect(pProcessThread, SIGNAL(allNodesRemoved()),
                     pInput, SLOT(removeAllNodes()));
    QObject::connect(pProcessThread, SIGNAL(nodeDisappeared(int)),
                     pInput, SLOT(removeNode(int)));

    QObject::connect(pProcessThread, SIGNAL(nodeAppeared(int)),
                     pOutput, SLOT(addNode(int)));
    QObject::connect(pProcessThread, SIGNAL(nodeDisappeared(int)),
                     pOutput, SLOT(removeNode(int)));
    QObject::connect(pProcessThread, SIGNAL(allNodesRemoved()),
                     pOutput, SLOT(removeAllNodes()));

    QObject::connect(pProcessThread, SIGNAL(nodeAppeared(int)),
                     pCnState, SLOT(addNode(int)));
    QObject::connect(pProcessThread, SIGNAL(nodeDisappeared(int)),
                     pCnState, SLOT(removeNode(int)));
    QObject::connect(pProcessThread, SIGNAL(allNodesRemoved()),
                     pCnState, SLOT(removeAllNodes()));

    QObject::connect(pProcessThread, SIGNAL(nodeStatusChanged(int, int)),
                     pCnState, SLOT(setState(int, int)));

    QObject::connect(pProcessThread, SIGNAL(printLog(const QString&)),
                     pMainWindow_p, SLOT(printlog(const QString&)));


    pDataInOutThread = new DataInOutThread;
    QObject::connect(pDataInOutThread, SIGNAL(processImageOutChanged(int, int)),
                     pOutput, SLOT(setValue(int, int)));
    QObject::connect(pDataInOutThread, SIGNAL(processImageInChanged(int, int)),
                     pInput, SLOT(setLeds(int, int)));

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

    initParam.nodeId = nodeId_p;
    initParam.ipAddress = (IP_ADDR & 0xFFFFFF00) | initParam.nodeId;

    initParam.fAsyncOnly = FALSE;
    initParam.featureFlags = UINT_MAX;
    initParam.cycleLen = CYCLE_LEN;           // required for error detection
    initParam.isochrTxMaxPayload = 256;       // const
    initParam.isochrRxMaxPayload = 256;       // const
    initParam.presMaxLatency = 50000;         // const; only required for IdentRes
    initParam.preqActPayloadLimit = 36;       // required for initialisation (+28 bytes)
    initParam.presActPayloadLimit = 36;       // required for initialisation of Pres frame (+28 bytes)
    initParam.asndMaxLatency = 150000;        // const; only required for IdentRes
    initParam.multiplCylceCnt = 0;            // required for error detection
    initParam.asyncMtu = 1500;                // required to set up max frame size
    initParam.prescaler = 2;                  // required for sync
    initParam.lossOfFrameTolerance = 500000;
    initParam.asyncSlotTimeout = 3000000;
    initParam.waitSocPreq = 150000;
    initParam.deviceType = UINT_MAX;          // NMT_DeviceType_U32
    initParam.vendorId = UINT_MAX;            // NMT_IdentityObject_REC.VendorId_U32
    initParam.productCode = UINT_MAX;         // NMT_IdentityObject_REC.ProductCode_U32
    initParam.revisionNumber = UINT_MAX;      // NMT_IdentityObject_REC.RevisionNo_U32
    initParam.serialNumber = UINT_MAX;        // NMT_IdentityObject_REC.SerialNo_U32

    initParam.subnetMask = SUBNET_MASK;
    initParam.defaultGateway = DEFAULT_GATEWAY;
    sprintf((char*)initParam.sHostname, "%02x-%08x", initParam.nodeId, initParam.vendorId);
    initParam.syncNodeId = C_ADR_SYNC_ON_SOA;
    initParam.fSyncOnPrcNode = FALSE;

    // set callback functions
    initParam.pfnCbEvent = pProcessThread->getEventCbFunc();

    /* write 00:00:00:00:00:00 to MAC address, so that the driver uses the real hardware address */
    OPLK_MEMCPY(initParam.aMacAddress, abMacAddr, sizeof(initParam.aMacAddress));

    // Copy the selected interface string to a local variable
    strcpy(devName_g, devName_p.toStdString().c_str());
    initParam.hwParam.pDevName = devName_g;

#if defined(CONFIG_KERNELSTACK_DIRECTLINK)
    initParam.pfnCbSync = pDataInOutThread->getSyncCbFunc();
#else
    initParam.pfnCbSync  =    NULL;
#endif

    // init POWERLINK
    ret = oplk_init(&initParam);
    if (ret != kErrorOk)
    {
        QMessageBox::critical(0, "POWERLINK demo",
                              QString("Initialization of openPOWERLINK Stack failed.\n") +
                                      "Error code: 0x"+ QString::number(ret, 16) +
                                      "\nThe most common error source are an unsupported Ethernet controller or the kernel module is not loaded."
                                      "\nFor further information please consult the manual.");
        goto Exit;
    }

    ret = oplk_setCdcFilename(pszCdcFilename_g);
    if (ret != kErrorOk)
    {
        goto Exit;
    }

    ret = pDataInOutThread->setupProcessImage();
    if (ret != kErrorOk)
    {
        QMessageBox::critical(0, "POWERLINK demo",
                              QString("Initialization of process image failed.\n") +
                                      "Error code: 0x"+ QString::number(ret, 16));
        goto Exit;
    }
    // start the openPOWERLINK stack
    ret = oplk_execNmtCommand(kNmtEventSwReset);

    // start process thread
    pProcessThread->start();

#if !defined(CONFIG_KERNELSTACK_DIRECTLINK)
    // start data in out thread
    pDataInOutThread->start();
#endif

Exit:
    return;

}