void RtxInit(void) { unsigned long temp; LARGE_INTEGER physAddr; LARGE_INTEGER waiting; LARGE_INTEGER liPeriod; LARGE_INTEGER waiting2; LARGE_INTEGER liPeriod2; // Shared memory hSharedMemory = RtOpenSharedMemory( SHM_MAP_WRITE, // access mode FALSE, // don't care L"Can Shared Data", // name of shared memory (VOID **)&pSharedMemory); // shared memory data address if (hSharedMemory == NULL) { RtWprintf(L"\n>>> RtOpenSharedMemory error = %d\n", GetLastError()); ExitProcess(1); } else RtWprintf(L"\n>>> RtOpenSharedMemory is OK..!!"); // Memory mapping temp = baseAddr; temp = temp << 4; physAddr.QuadPart = temp; vAddress = RtMapMemory(physAddr, 0xDFFF, 1); if(vAddress==NULL) RtWprintf(L"\n>>> Failure on RtMapMemory..!\n"); else; // Timer for 2 msec (500 Hz) liPeriod.QuadPart = timeInt; waiting.QuadPart = 30000000; if (! (hTimer = RtCreateTimer( NULL, // security 0, // stack size - 0 uses default TimerHandler, // timer handler NULL, // NULL context (argument to handler) RT_PRIORITY_MAX, // priority CLOCK_2) )) // RTX HAL timer { RtWprintf(L"\n>>> RtCreateTimer for 2 msec (500 Hz) error = %d\n", GetLastError()); ExitProcess(1); } else; if (! RtSetTimerRelative( hTimer, // hTimer &waiting, // pExpiration &liPeriod) ) // pInterval { RtWprintf(L"\n>>> RtSetTimerRelative error (2 msec or 500 Hz= %d\n",GetLastError()); ExitProcess(1); } else; // Timer for 4 msec (100 Hz) liPeriod2.QuadPart = timeInt2; waiting2.QuadPart = 30000000+timeInt; if (! (hTimer2 = RtCreateTimer( NULL, // security 0, // stack size - 0 uses default TimerHandler2, // timer handler NULL, // NULL context (argument to handler) RT_PRIORITY_MAX-1, // priority CLOCK_2) )) // RTX HAL timer { RtWprintf(L"\n>>> RtCreateTimer for 10 msec (100 Hz) error = %d\n", GetLastError()); ExitProcess(1); } else; if (! RtSetTimerRelative( hTimer2, // hTimer &waiting2, // pExpiration &liPeriod2) ) // pInterval { RtWprintf(L"\n>>> RtSetTimerRelative error (10 msec or 100 Hz= %d\n",GetLastError()); ExitProcess(1); } else; }
void _cdecl main(int argc, char **argv, char **envp) { //// Test C_CircBuffer //C_CircBuffer buff; //char pStr[512]; // //for(int i = 0 ; i < 50 ; i++) //{ // // Write to circular buffer // unsigned int err = buff.Write("Test string"); // // Read // err = buff.Read(pStr); // printf("%s", pStr); //} logMsg.LoggingStart(); hThread1 = CreateThread(0, 0, ThreadHandler, NULL, CREATE_SUSPENDED, 0); // Handle thread creation fail if(hThread1 == NULL) { printf_s( "Error:\tCould not create 'hThread1'.\n"); ExitProcess(0); } hThread2 = CreateThread(0, 0, ThreadHandler2, NULL, CREATE_SUSPENDED, 0); // Handle thread creation fail if(hThread2 == NULL) { printf_s( "Error:\tCould not create 'hThread2'.\n"); ExitProcess(0); } printf( "OK\t\t'hThread1' and 'hThread1' created. Handlet = 0x%X\n", hThread1); // Set thread prority if(RtSetThreadPriority(hThread1, RT_PRIORITY_MAX - 5)) { printf("OK\t\t'hThread1' priority was set to: RT_PRIORITY_MAX - 5.\n"); } else { CloseHandle(hThread1); printf("Error:\tPriority of 'hThread1' wasn't set\n"); ExitProcess(0); } // Set thread prority if(RtSetThreadPriority(hThread2, RT_PRIORITY_MAX - 5)) { printf("OK\t\t'hThread2' priority was set to: RT_PRIORITY_MAX - 5.\n"); } else { CloseHandle(hThread2); printf("Error:\tPriority of 'hThread2' wasn't set\n"); ExitProcess(0); } // Start hThread1 if(RtResumeThread(hThread1) != 0XFFFFFFFF) { printf("OK\t\t'hThread1' resumed.\n"); } else { CloseHandle(hThread1); printf("Error:\t'hThread1' cannot be resumed.\n"); ExitProcess(0); } // Start hThread2 if(RtResumeThread(hThread2) != 0XFFFFFFFF) { printf("OK\t\t'hThread2' resumed.\n"); } else { CloseHandle(hThread2); printf("Error:\t'hThread2' cannot be resumed.\n"); ExitProcess(0); } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% // Test asyn. log message logMsg.PushMessage("Test message 1", SEVERITY_MAX - 1); logMsg.PushMessage("Test message 2", SEVERITY_MAX - 1); logMsg.PushMessage("Test message 3", SEVERITY_MAX - 1); // Timer test timerCounter = 0; LARGE_INTEGER timerLastTime, minTmrPeriod, tmrPeriod; timerLastTime.QuadPart = 0; char strMessage[256]; hTimerPWM = RtCreateTimer(NULL, 0, OnTimerPWMTick, NULL, RT_PRIORITY_MAX, CLOCK_3); // Create timer if(hTimerPWM == NULL) { sprintf_s(strMessage, 256, "Error: Timer was not created.\r\n"); logMsg.PushMessage(strMessage, SEVERITY_MAX - 1); } if(!RtGetClockTimerPeriod(CLOCK_3, &minTmrPeriod)) // Get minimum timer period { sprintf_s(strMessage, 256, "Error: Timer period was not set.\r\n"); logMsg.PushMessage(strMessage, SEVERITY_MAX - 1); } tmrPeriod.QuadPart = minTmrPeriod.QuadPart * 10; printf("\nPeriod = %I64d\n", tmrPeriod.QuadPart); RtSetTimerRelative(hTimerPWM, &tmrPeriod, &tmrPeriod); // Set required (1ms) timer period Sleep(1000); // 1s LARGE_INTEGER cancelCode; RtCancelTimer(hTimerPWM, &cancelCode); // Close timer if(hTimerPWM != NULL)RtDeleteTimer(hTimerPWM); // Delete timer //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% logMsg.LoggingStop(); // Waiting for thread end bool allThreadsEnded; DWORD ExitCode; while(1) { allThreadsEnded = TRUE; if(hThread1 != NULL || hThread1 != NULL) { if ( GetExitCodeThread(hThread1,&ExitCode) == TRUE ) { if(ExitCode != STILL_ACTIVE) { printf("OK\t\t'hThread1' has ended.\n"); } else { allThreadsEnded = FALSE; RtSleep(10); } } else { DWORD err = GetLastError(); printf("Error:\tGetLastError()=%04x\n", err); } if ( GetExitCodeThread(hThread1,&ExitCode) == TRUE ) { if(ExitCode != STILL_ACTIVE) { printf("OK\t\t'hThread2' has ended.\n"); } else { allThreadsEnded = FALSE; RtSleep(10); } } else { DWORD err = GetLastError(); printf("Error:\tGetLastError()=%04x\n", err); } } if(allThreadsEnded)break; } ExitProcess(0); }