www.pudn.com > Demo C.rar > timerDemo.c
/*--------------------------------------------------------------------------- [TITLE] Demo [FILE] [VSN] [CREATED] [LASTCHNGD] [COPYRIGHT] Copyright 2001 (C) CPS Europe B.V. [AUTHOR] Michel Hendriks [PURPOSE] [NOTE] ---------------------------------------------------------------------------- [VERSION HISTORY] [DATE] [WHO] [COMMENT] ----------------------------------------------------------------------------*/ /* Include files ------------------------------------------------------------*/ #include/* our OS */ #include /* task control */ #include /* signals */ #include /* printf */ #include /* sysClkRateGet */ #include /* timers */ #include /* logMsg */ #include /* watchdog routines */ /* Constants ----------------------------------------------------------------*/ /* Type Definitions ---------------------------------------------------------*/ /* External data ------------------------------------------------------------*/ /* Public data --------------------------------------------------------------*/ /* Private vars -------------------------------------------------------------*/ static BOOL wait; static WDOG_ID wdogId; /* Compile time checks ------------------------------------------------------*/ /* Prototyping for private routines -----------------------------------------*/ /* Implementation -----------------------------------------------------------*/ /*----------------------------------------------------------------------------- Description : WatchDog ISR handler Parameters : Globals Changed: Returns : -----------------------------------------------------------------------------*/ static void watchDogISR(void) { logMsg( "timedemo: WatchDogISR called\n", 1, 2, 3, 4, 5, 6 ); } /*----------------------------------------------------------------------------- Description : Signal handler for SIGALRM (Timer Handler) Parameters : Globals Changed: Returns : -----------------------------------------------------------------------------*/ static void timerHandler ( timer_t timerid, /* expired timer ID */ int arg /* user argument */ ) { logMsg( "timedemo: Timer 0x%x expired (arg = 0x%x)\n", (int)timerid, arg, 3, 4, 5, 6 ); wait = FALSE; } /*----------------------------------------------------------------------------- Description : Main entry for this demo Parameters : Globals Changed: Returns : OK -----------------------------------------------------------------------------*/ STATUS timedemo( void ) { timer_t timerId ; /* id for the posix timer */ struct itimerspec timeToSet ; /* time to be set */ STATUS retval = OK; wait = TRUE; /* * Start POSIX timer demo */ /* Set the time to the desired value */ timeToSet.it_value.tv_sec = 5 ; /* initial (one shot) value */ timeToSet.it_value.tv_nsec = 0 ; timeToSet.it_interval.tv_sec = 0 ; /* reload (repetitive) value */ timeToSet.it_interval.tv_nsec = 0 ; if( timer_create( CLOCK_REALTIME, NULL, &timerId ) == ERROR ) { perror( "timedemo: Error in creating the timer\n" ); retval = ERROR; } if( timer_connect( timerId, &timerHandler, 2) == ERROR ) { perror( "timedemo: Error in connecting the timer\n" ); retval = ERROR; } else if( timer_settime( timerId, CLOCK_REALTIME, &timeToSet, NULL ) == ERROR ) { perror( "timedemo: Error in setting the timer\n" ); retval = ERROR; } else { if ( taskDelay(sysClkRateGet() * 20) == ERROR ) { perror( "timedemo: taskDelay expired early\n" ); retval = ERROR; } { struct timespec time_delay; struct timespec time_remaining; time_delay.tv_sec = 2; time_delay.tv_nsec = 500000000L; if ( nanosleep( &time_delay, &time_remaining ) == ERROR ) { perror( "timedemo: nanosleep expired early\n" ); retval = ERROR; } else { logMsg( "timedemo: nanosleep expired normally\n", 1, 2, 3, 4, 5, 6 ); } } /* while ( wait ) { } */ } /* Cleanup */ if (timer_delete (timerId) == ERROR) { perror( "timedemo: Error in deleting the timer\n" ); retval = ERROR; } /* * Start watchdog timer demo */ wdogId = wdCreate(); if ( wdogId == NULL ) { perror( "timedemo: couldn't create watchdog\n" ); retval = ERROR; } else if ( wdStart( wdogId, sysClkRateGet() * 5, (FUNCPTR)watchDogISR, 0 ) == ERROR ) { perror( "timedemo: couldn't start watchdog\n" ); retval = ERROR; } else { if ( taskDelay(sysClkRateGet() * 20) == ERROR ) { perror( "timedemo: taskDelay expired early\n" ); retval = ERROR; } } /* Cleanup */ if ( wdDelete( wdogId ) == ERROR ) { perror( "timedemo: couldn't delete watchdog\n" ); retval = ERROR; } return( retval ); }