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