timer_posix.c

Go to the documentation of this file.
00001 
00037 //#include <cfg/compiler.h> // hptime.t
00038 #include <os/hptime.h>
00039 #include <kern/irq.h>     // irq_register()
00040 
00041 #if !CONFIG_KERN_IRQ
00042 #include <signal.h>       // sigaction()
00043 #include <string.h>       // memset()
00044 #endif
00045 #include <sys/time.h>     // setitimer()
00046 
00047 
00048 // Forward declaration for the user interrupt server routine.
00049 void timer_isr(int);
00050 
00052 static void timer_hw_init(void)
00053 {
00054     #if CONFIG_KERN_IRQ
00055         irq_register(SIGALRM, (void (*)(void))timer_isr);
00056     #else // ! CONFIG_KERN_IRQ
00057         struct sigaction sa;
00058         memset(&sa, 0, sizeof(sa));
00059 
00060         // Setup interrupt callback
00061         sa.sa_handler = timer_isr;
00062         sigemptyset(&sa.sa_mask);
00063         sigaddset(&sa.sa_mask, SIGALRM);
00064         sa.sa_flags = SA_RESTART;
00065         sigaction(SIGALRM, &sa, NULL);
00066     #endif // CONFIG_KERN_IRQ
00067 
00068     // Setup POSIX realtime timer to interrupt every 1/TIMER_TICKS_PER_SEC.
00069     static const struct itimerval itv =
00070     {
00071         { 0, 1000000 / TIMER_TICKS_PER_SEC }, /* it_interval */
00072         { 0, 1000000 / TIMER_TICKS_PER_SEC }  /* it_value */
00073     };
00074     setitimer(ITIMER_REAL, &itv, NULL);
00075 }
00076 
00077 static void timer_hw_cleanup(void)
00078 {
00079     static const struct itimerval itv =
00080     {
00081         { 0, 0 }, /* it_interval */
00082         { 0, 0 }  /* it_value */
00083     };
00084     setitimer(ITIMER_REAL, &itv, NULL);
00085     signal(SIGALRM, SIG_DFL);
00086 }
00087 
00088 INLINE hptime_t timer_hw_hpread(void)
00089 {
00090     return hptime_get();
00091 }
00092 
00093 #define timer_hw_triggered() (true)