//------------------------------------------------------------------------------ tOplkError target_cleanup(void) { tOplkError Ret = kErrorOk; /* Disable ftrace debugging */ FTRACE_ENABLE(FALSE); return Ret; }
//------------------------------------------------------------------------------ tOplkError target_init(void) { tOplkError Ret = kErrorOk; sigset_t mask; /* * We have to block the real time signals used by the timer modules so * that they are able to wait on them using sigwaitinfo! */ sigemptyset(&mask); sigaddset(&mask, SIGRTMIN); sigaddset(&mask, SIGRTMIN + 1); pthread_sigmask(SIG_BLOCK, &mask, NULL); /* Enabling ftrace for debugging */ FTRACE_OPEN(); FTRACE_ENABLE(TRUE); return Ret; }
//--------------------------------------------------------------------------- // // Function: main // // Description: main function of demo application // // Parameters: // // Returns: //--------------------------------------------------------------------------- int main (int argc, char **argv) { tEplKernel EplRet = kEplSuccessful; static tEplApiInitParam EplApiInitParam; char* sHostname = HOSTNAME; char cKey = 0; #ifdef CONFIG_POWERLINK_USERSTACK // variables for Pcap char sErr_Msg[ PCAP_ERRBUF_SIZE ]; char devName[128]; pcap_if_t * alldevs; pcap_if_t * seldev; int i = 0; int inum; #endif int opt; #if (TARGET_SYSTEM == _LINUX_) /* get command line parameters */ while ((opt = getopt(argc, argv, "c:l:")) != -1) { switch (opt) { case 'c': uiCycleLen_g = strtoul(optarg, NULL, 10); break; case 'l': pLogFile_g = optarg; break; default: /* '?' */ fprintf (stderr, "Usage: %s [-c CYCLE_TIME] [-l LOGFILE]\n", argv[0]); goto Exit; } } #endif #ifdef CONFIG_POWERLINK_USERSTACK #if (TARGET_SYSTEM == _LINUX_) struct sched_param schedParam; /* adjust process priority */ if (nice (-20) == -1) // push nice level in case we have no RTPreempt { EPL_DBGLVL_ERROR_TRACE("%s() couldn't set nice value! (%s)\n", __func__, strerror(errno)); } schedParam.__sched_priority = MAIN_THREAD_PRIORITY; if (pthread_setschedparam(pthread_self(), SCHED_RR, &schedParam) != 0) { EPL_DBGLVL_ERROR_TRACE("%s() couldn't set thread scheduling parameters! %d\n", __func__, schedParam.__sched_priority); } /* Initialize target specific stuff */ EplTgtInit(); #elif (TARGET_SYSTEM == _WIN32_) // activate realtime priority class SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS); // lower the priority of this thread SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_IDLE); #endif // (TARGET_SYSTEM == _WIN32_) #endif // CONFIG_POWERLINK_USERSTACK #if (TARGET_SYSTEM == _LINUX_) #ifdef SET_CPU_AFFINITY { /* binds all openPOWERLINK threads to the second CPU core */ cpu_set_t affinity; CPU_ZERO(&affinity); CPU_SET(1, &affinity); sched_setaffinity(0, sizeof(cpu_set_t), &affinity); } #endif #endif /* Enabling ftrace for debugging */ FTRACE_OPEN(); FTRACE_ENABLE(TRUE); /* EPL_DBGLVL_ALWAYS_TRACE("%s(): Main Thread Id:%ld\n", __func__, syscall(SYS_gettid)); */ printf("----------------------------------------------------\n"); printf("openPOWERLINK console MN DEMO application\n"); printf("----------------------------------------------------\n"); EPL_MEMSET(&EplApiInitParam, 0, sizeof (EplApiInitParam)); EplApiInitParam.m_uiSizeOfStruct = sizeof (EplApiInitParam); #ifdef CONFIG_POWERLINK_USERSTACK /* Retrieve the device list on the local machine */ if (pcap_findalldevs(&alldevs, sErr_Msg) == -1) { fprintf(stderr, "Error in pcap_findalldevs: %s\n", sErr_Msg); EplRet = kEplNoResource; goto Exit; } PRINTF("--------------------------------------------------\n"); PRINTF("List of Ethernet Cards Found in this System: \n"); PRINTF("--------------------------------------------------\n"); /* Print the list */ for (seldev = alldevs; seldev != NULL; seldev = seldev->next) { PRINTF("%d. ", ++i); if (seldev->description) { PRINTF("%s\n %s\n", seldev->description, seldev->name); } else { PRINTF("%s\n", seldev->name); } } if (i == 0) { PRINTF("\nNo interfaces found! Make sure pcap library is installed.\n"); EplRet = kEplNoResource; goto Exit; } PRINTF("--------------------------------------------------\n"); PRINTF("Select the interface to be used for POWERLINK (1-%d):",i); if (scanf("%d", &inum) == EOF) { pcap_freealldevs(alldevs); EplRet = kEplNoResource; goto Exit; } PRINTF("--------------------------------------------------\n"); if ((inum < 1) || (inum > i)) { PRINTF("\nInterface number out of range.\n"); /* Free the device list */ pcap_freealldevs(alldevs); EplRet = kEplNoResource; goto Exit; } /* Jump to the selected adapter */ for (seldev = alldevs, i = 0; i < (inum - 1); seldev = seldev->next, i++) { // do nothing } strncpy(devName, seldev->name, 127); // pass selected device name to Edrv EplApiInitParam.m_HwParam.m_pszDevName = devName; #endif EplApiInitParam.m_uiNodeId = uiNodeId_g = NODEID; EplApiInitParam.m_dwIpAddress = (0xFFFFFF00 & IP_ADDR) | EplApiInitParam.m_uiNodeId; /* write 00:00:00:00:00:00 to MAC address, so that the driver uses the real hardware address */ EPL_MEMCPY(EplApiInitParam.m_abMacAddress, abMacAddr, sizeof (EplApiInitParam.m_abMacAddress)); EplApiInitParam.m_fAsyncOnly = FALSE; EplApiInitParam.m_dwFeatureFlags = -1; EplApiInitParam.m_dwCycleLen = uiCycleLen_g; // required for error detection EplApiInitParam.m_uiIsochrTxMaxPayload = 256; // const EplApiInitParam.m_uiIsochrRxMaxPayload = 256; // const EplApiInitParam.m_dwPresMaxLatency = 50000; // const; only required for IdentRes EplApiInitParam.m_uiPreqActPayloadLimit = 36; // required for initialisation (+28 bytes) EplApiInitParam.m_uiPresActPayloadLimit = 36; // required for initialisation of Pres frame (+28 bytes) EplApiInitParam.m_dwAsndMaxLatency = 150000; // const; only required for IdentRes EplApiInitParam.m_uiMultiplCycleCnt = 0; // required for error detection EplApiInitParam.m_uiAsyncMtu = 1500; // required to set up max frame size EplApiInitParam.m_uiPrescaler = 2; // required for sync EplApiInitParam.m_dwLossOfFrameTolerance = 500000; EplApiInitParam.m_dwAsyncSlotTimeout = 3000000; EplApiInitParam.m_dwWaitSocPreq = 150000; EplApiInitParam.m_dwDeviceType = -1; // NMT_DeviceType_U32 EplApiInitParam.m_dwVendorId = -1; // NMT_IdentityObject_REC.VendorId_U32 EplApiInitParam.m_dwProductCode = -1; // NMT_IdentityObject_REC.ProductCode_U32 EplApiInitParam.m_dwRevisionNumber = -1; // NMT_IdentityObject_REC.RevisionNo_U32 EplApiInitParam.m_dwSerialNumber = -1; // NMT_IdentityObject_REC.SerialNo_U32 EplApiInitParam.m_dwSubnetMask = SUBNET_MASK; EplApiInitParam.m_dwDefaultGateway = 0; EPL_MEMCPY(EplApiInitParam.m_sHostname, sHostname, sizeof(EplApiInitParam.m_sHostname)); EplApiInitParam.m_uiSyncNodeId = EPL_C_ADR_SYNC_ON_SOA; EplApiInitParam.m_fSyncOnPrcNode = FALSE; // set callback functions EplApiInitParam.m_pfnCbEvent = AppCbEvent; #ifdef CONFIG_POWERLINK_USERSTACK EplApiInitParam.m_pfnObdInitRam = EplObdInitRam; EplApiInitParam.m_pfnCbSync = AppCbSync; #else EplApiInitParam.m_pfnCbSync = NULL; #endif printf("\n\nHello, I'm a Userspace POWERLINK node running as %s!\n (build: %s / %s)\n\n", (uiNodeId_g == EPL_C_ADR_MN_DEF_NODE_ID ? "Managing Node" : "Controlled Node"), __DATE__, __TIME__); // initialize POWERLINK stack printf ("Initializing openPOWERLINK stack...\n"); EplRet = EplApiInitialize(&EplApiInitParam); if(EplRet != kEplSuccessful) { printf("EplApiInitialize() failed (Error:0x%x!\n", EplRet); goto Exit; } // initialize application printf ("Initializing openPOWERLINK application...\n"); EplRet = AppInit(); if(EplRet != kEplSuccessful) { printf("ApiInit() failed!\n"); goto Exit; } #ifdef CONFIG_POWERLINK_USERSTACK /* At this point, we don't need any more the device list. Free it */ pcap_freealldevs(alldevs); EplRet = EplApiSetCdcFilename(pszCdcFilename_g); if(EplRet != kEplSuccessful) { goto Exit; } #else // create event thread if (pthread_create(&eventThreadId, NULL, &powerlinkEventThread, NULL) != 0) { goto Exit; } // create sync thread if (pthread_create(&syncThreadId, NULL, &powerlinkSyncThread, NULL) != 0) { goto Exit; } #endif printf("Initializing process image...\n"); printf("Size of input process image: %ld\n", sizeof(AppProcessImageIn_g)); printf("Size of output process image: %ld\n", sizeof (AppProcessImageOut_g)); AppProcessImageCopyJob_g.m_fNonBlocking = FALSE; AppProcessImageCopyJob_g.m_uiPriority = 0; AppProcessImageCopyJob_g.m_In.m_pPart = &AppProcessImageIn_g; AppProcessImageCopyJob_g.m_In.m_uiOffset = 0; AppProcessImageCopyJob_g.m_In.m_uiSize = sizeof (AppProcessImageIn_g); AppProcessImageCopyJob_g.m_Out.m_pPart = &AppProcessImageOut_g; AppProcessImageCopyJob_g.m_Out.m_uiOffset = 0; AppProcessImageCopyJob_g.m_Out.m_uiSize = sizeof (AppProcessImageOut_g); EplRet = EplApiProcessImageAlloc(sizeof (AppProcessImageIn_g), sizeof (AppProcessImageOut_g), 2, 2); if (EplRet != kEplSuccessful) { goto Exit; } EplRet = EplApiProcessImageSetup(); if (EplRet != kEplSuccessful) { goto Exit; } // start processing EplRet = EplApiExecNmtCommand(kEplNmtEventSwReset); if (EplRet != kEplSuccessful) { goto ExitShutdown; } printf("\n-------------------------------\n"); printf("Press Esc to leave the program\n"); printf("Press r to reset the node\n"); printf("-------------------------------\n\n"); // wait for key hit while (cKey != 0x1B) { if( EplTgtKbhit() ) { cKey = (BYTE) EplTgtGetch(); switch (cKey) { case 'r': { EplRet = EplApiExecNmtCommand(kEplNmtEventSwReset); if (EplRet != kEplSuccessful) { goto ExitShutdown; } break; } case 'c': { EplRet = EplApiExecNmtCommand(kEplNmtEventNmtCycleError); if (EplRet != kEplSuccessful) { goto ExitShutdown; } break; } default: { break; } } } EplTgtMilliSleep( 1500 ); } FTRACE_ENABLE(FALSE); ExitShutdown: // halt the NMT state machine // so the processing of POWERLINK frames stops EplRet = EplApiExecNmtCommand(kEplNmtEventSwitchOff); // delete process image EplRet = EplApiProcessImageFree(); // delete instance for all modules EplRet = EplApiShutdown(); Exit: PRINTF("main(): returns 0x%X\n", EplRet); #if (TARGET_SYSTEM == _WIN32_) PRINTF("Press Enter to quit!\n"); EplTgtGetch(); #endif return EplRet; }