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