Statistics
| Branch: | Revision:

scoutos / prex-0.9.0 / usr / sample / alarm / alarm.c @ 03e9c04a

History | View | Annotate | Download (3.03 KB)

1
/*
2
 * Copyright (c) 2005-2007, Kohsuke Ohtani
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms, with or without
6
 * modification, are permitted provided that the following conditions
7
 * are met:
8
 * 1. Redistributions of source code must retain the above copyright
9
 *    notice, this list of conditions and the following disclaimer.
10
 * 2. Redistributions in binary form must reproduce the above copyright
11
 *    notice, this list of conditions and the following disclaimer in the
12
 *    documentation and/or other materials provided with the distribution.
13
 * 3. Neither the name of the author nor the names of any co-contributors
14
 *    may be used to endorse or promote products derived from this software
15
 *    without specific prior written permission.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29

    
30
/*
31
 * alarm.c - sample alarm program.
32
 */
33

    
34
/*
35
 * Expected output:
36
 *
37
 * Alarm sample program
38
 * Start alarm timer
39
 * Ring! count=1 time=1000 msec
40
 * Ring! count=2 time=1200 msec
41
 * Ring! count=3 time=1600 msec
42
 * Ring! count=4 time=2200 msec
43
 * Ring! count=5 time=3000 msec
44
 * Ring! count=6 time=4000 msec
45
 * Ring! count=7 time=5200 msec
46
 * Ring! count=8 time=6600 msec
47
 * Ring! count=9 time=8200 msec
48
 * Ring! count=10 time=10000 msec
49
 * End...
50
 *
51
 */
52

    
53
#include <sys/prex.h>
54
#include <sys/signal.h>
55
#include <stdio.h>
56

    
57
static int hz;
58
static volatile u_long count;
59
static u_long start_tick;
60

    
61
/*
62
 * Re-program alarm timer as count * 200msec.
63
 */
64
static void
65
alarm_handler(int code)
66
{
67
        u_long tick;
68

    
69
        if (code == SIGALRM) {
70
                count++;
71
                if (count > 10) {
72
                        printf("End...\n");
73
                        task_terminate(task_self());
74
                }
75
                timer_alarm(count * 200, 0);
76
                sys_time(&tick);
77
                printf("Ring! count=%ld time=%d msec\n", count,
78
                       (u_int)((tick - start_tick) * 1000 / hz));
79
        }
80
        exception_return();
81
}
82

    
83
int
84
main(int argc, char *argv[])
85
{
86
        struct timerinfo info;
87
        int code;
88

    
89
        printf("Alarm sample program\n");
90

    
91
        sys_info(INFO_TIMER, &info);
92
        if (info.hz == 0)
93
                panic("can not get timer tick rate");
94
        hz = info.hz;
95

    
96
        /*
97
         * Install alarm handler
98
         */
99
        exception_setup(alarm_handler);
100
        timer_sleep(2000, 0);
101

    
102
        /*
103
         * Kick first alarm timer.
104
         */
105
        printf("Start alarm timer\n");
106
        count = 0;
107
        sys_time(&start_tick);
108
        timer_alarm(1000, 0);
109

    
110
        /* Wait alarm */
111
        while (count < 10) {
112
                exception_wait(&code);
113
        }
114
        printf("Test completed...\n");
115
        return 0;
116
}