buz_init(): Restore IRQ protection as in project_bko.
[bertos.git] / drv / buzzer.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 1999, 2003 Bernardo Innocenti <bernie@develer.com>
6  * This file is part of DevLib - See README.devlib for information.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \brief Buzzer driver (implementation)
12  *
13  * \version $Id$
14  * \author Bernardo Innocenti <bernie@develer.com>
15  * \author Francesco Sacchi <batt@develer.com>
16  */
17
18 /*#*
19  *#* $Log$
20  *#* Revision 1.16  2005/11/04 16:19:33  bernie
21  *#* buz_init(): Restore IRQ protection as in project_bko.
22  *#*
23  *#* Revision 1.15  2005/06/27 21:25:50  bernie
24  *#* Modularize hardware access; Port to new timer interface.
25  *#*
26  *#* Revision 1.14  2005/04/11 19:10:27  bernie
27  *#* Include top-level headers from cfg/ subdir.
28  *#*
29  *#* Revision 1.13  2005/02/18 11:20:15  bernie
30  *#* Use mware/event.h; Update copyright info.
31  *#*
32  *#* Revision 1.12  2004/12/13 12:07:06  bernie
33  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
34  *#*
35  *#* Revision 1.11  2004/12/08 09:11:53  bernie
36  *#* Rename time_t to mtime_t.
37  *#*
38  *#* Revision 1.10  2004/10/03 18:38:51  bernie
39  *#* Add missing AVR header; Fix header.
40  *#*
41  *#* Revision 1.9  2004/09/14 21:01:25  bernie
42  *#* Use new AVR port pin names.
43  *#*/
44
45
46 #include <hw_buzzer.h>
47 #include <drv/buzzer.h>
48
49 #include <drv/timer.h>
50 #include <drv/sipo.h>
51
52 #include <mware/event.h>
53
54 #include <cfg/debug.h>
55
56
57 /* Local vars */
58 static Timer buz_timer;
59 static bool buz_timer_running;
60 static mtime_t buz_repeat_interval;
61 static mtime_t buz_repeat_duration;
62
63
64 /*!
65  * Turn off buzzer, called by software timer
66  */
67 static void buz_softint(void)
68 {
69         if (IS_BUZZER_ON)
70         {
71                 BUZZER_OFF;
72                 if (buz_repeat_interval)
73                 {
74                         /* Wait for interval time */
75                         timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_interval));
76                         timer_add(&buz_timer);
77                 }
78                 else
79                         buz_timer_running = false;
80         }
81         else if (buz_repeat_interval)
82         {
83                 /* Wait for beep time */
84                 BUZZER_ON;
85                 timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_duration));
86                 timer_add(&buz_timer);
87         }
88         else
89                 buz_timer_running = false;
90 }
91
92
93 /*!
94  * Beep for the specified ms time
95  */
96 void buz_beep(mtime_t time)
97 {
98         cpuflags_t flags;
99         IRQ_SAVE_DISABLE(flags);
100
101         /* Remove the software interrupt if it was already queued */
102         if (buz_timer_running)
103                 timer_abort(&buz_timer);
104
105         /* Turn on buzzer */
106         BUZZER_ON;
107
108         /* Add software interrupt to turn the buzzer off later */
109         buz_timer_running = true;
110         timer_setDelay(&buz_timer, ms_to_ticks(time));
111         timer_add(&buz_timer);
112
113         IRQ_RESTORE(flags);
114 }
115
116
117 /*!
118  * Start buzzer repetition
119  */
120 void buz_repeat_start(mtime_t duration, mtime_t interval)
121 {
122         buz_repeat_interval = interval;
123         buz_repeat_duration = duration;
124         buz_beep(duration);
125 }
126
127
128 /*!
129  * Stop buzzer repetition
130  */
131 void buz_repeat_stop(void)
132 {
133         cpuflags_t flags;
134         IRQ_SAVE_DISABLE(flags);
135
136         /* Remove the software interrupt if it was already queued */
137         if (buz_timer_running)
138         {
139                 timer_abort(&buz_timer);
140                 buz_timer_running = false;
141         }
142
143         buz_repeat_interval = 0;
144         BUZZER_OFF;
145
146         IRQ_RESTORE(flags);
147 }
148
149
150 /*!
151  * Initialize buzzer.
152  */
153 void buz_init(void)
154 {
155         cpuflags_t flags;
156         IRQ_SAVE_DISABLE(flags);
157
158         BUZZER_HW_INIT;
159
160         IRQ_RESTORE(flags);
161
162         /* Init software interrupt. */
163         timer_set_event_softint(&buz_timer, (Hook)buz_softint, 0);
164 }