Doc fixes.
[bertos.git] / drv / buzzer.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2003, 2004, 2005 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 1999, 2003 Bernardo Innocenti <bernie@develer.com>
31  *
32  * -->
33  *
34  * \version $Id$
35  *
36  * \brief Buzzer driver (implementation)
37  *
38  * \version $Id$
39  * \author Bernardo Innocenti <bernie@develer.com>
40  * \author Francesco Sacchi <batt@develer.com>
41  */
42
43 /*#*
44  *#* $Log$
45  *#* Revision 1.19  2006/07/19 12:56:25  bernie
46  *#* Convert to new Doxygen style.
47  *#*
48  *#* Revision 1.18  2006/02/17 21:15:25  bernie
49  *#* Add MOD_CHECK() checks.
50  *#*
51  *#* Revision 1.17  2006/02/10 12:30:18  bernie
52  *#* Push interrupt protection inside hw module.
53  *#*
54  *#* Revision 1.16  2005/11/04 16:19:33  bernie
55  *#* buz_init(): Restore IRQ protection as in project_bko.
56  *#*
57  *#* Revision 1.15  2005/06/27 21:25:50  bernie
58  *#* Modularize hardware access; Port to new timer interface.
59  *#*
60  *#* Revision 1.14  2005/04/11 19:10:27  bernie
61  *#* Include top-level headers from cfg/ subdir.
62  *#*
63  *#* Revision 1.13  2005/02/18 11:20:15  bernie
64  *#* Use mware/event.h; Update copyright info.
65  *#*
66  *#* Revision 1.12  2004/12/13 12:07:06  bernie
67  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
68  *#*
69  *#* Revision 1.11  2004/12/08 09:11:53  bernie
70  *#* Rename time_t to mtime_t.
71  *#*
72  *#* Revision 1.10  2004/10/03 18:38:51  bernie
73  *#* Add missing AVR header; Fix header.
74  *#*
75  *#* Revision 1.9  2004/09/14 21:01:25  bernie
76  *#* Use new AVR port pin names.
77  *#*/
78
79 #include "buzzer.h"
80
81 #include <hw_buzzer.h>
82 #include <drv/timer.h>
83
84 #include <mware/event.h>
85
86 #include <cfg/debug.h>
87 #include <cfg/module.h>
88
89
90 /* Local vars */
91 static Timer buz_timer;
92 static bool buz_timer_running;
93 static mtime_t buz_repeat_interval;
94 static mtime_t buz_repeat_duration;
95
96
97 /**
98  * Turn off buzzer, called by software timer
99  */
100 static void buz_softint(void)
101 {
102         if (IS_BUZZER_ON)
103         {
104                 BUZZER_OFF;
105                 if (buz_repeat_interval)
106                 {
107                         /* Wait for interval time */
108                         timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_interval));
109                         timer_add(&buz_timer);
110                 }
111                 else
112                         buz_timer_running = false;
113         }
114         else if (buz_repeat_interval)
115         {
116                 /* Wait for beep time */
117                 BUZZER_ON;
118                 timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_duration));
119                 timer_add(&buz_timer);
120         }
121         else
122                 buz_timer_running = false;
123 }
124
125
126 /**
127  * Beep for the specified ms time
128  */
129 void buz_beep(mtime_t time)
130 {
131         cpuflags_t flags;
132         IRQ_SAVE_DISABLE(flags);
133
134         /* Remove the software interrupt if it was already queued */
135         if (buz_timer_running)
136                 timer_abort(&buz_timer);
137
138         /* Turn on buzzer */
139         BUZZER_ON;
140
141         /* Add software interrupt to turn the buzzer off later */
142         buz_timer_running = true;
143         timer_setDelay(&buz_timer, ms_to_ticks(time));
144         timer_add(&buz_timer);
145
146         IRQ_RESTORE(flags);
147 }
148
149
150 /**
151  * Start buzzer repetition
152  */
153 void buz_repeat_start(mtime_t duration, mtime_t interval)
154 {
155         buz_repeat_interval = interval;
156         buz_repeat_duration = duration;
157         buz_beep(duration);
158 }
159
160
161 /**
162  * Stop buzzer repetition
163  */
164 void buz_repeat_stop(void)
165 {
166         cpuflags_t flags;
167         IRQ_SAVE_DISABLE(flags);
168
169         /* Remove the software interrupt if it was already queued */
170         if (buz_timer_running)
171         {
172                 timer_abort(&buz_timer);
173                 buz_timer_running = false;
174         }
175
176         buz_repeat_interval = 0;
177         BUZZER_OFF;
178
179         IRQ_RESTORE(flags);
180 }
181
182 MOD_DEFINE(buzzer)
183
184 /**
185  * Initialize buzzer.
186  */
187 void buz_init(void)
188 {
189         MOD_CHECK(timer);
190
191         BUZZER_HW_INIT;
192
193         /* Init software interrupt. */
194         timer_set_event_softint(&buz_timer, (Hook)buz_softint, 0);
195
196         MOD_INIT(buzzer);
197 }