a57af7a04a201bfbcf621312d0b137bf5c941e69
[bertos.git] / bertos / 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 #include "buzzer.h"
44
45 #include "hw/hw_buzzer.h"
46 #include <drv/timer.h>
47
48 #include <mware/event.h>
49
50 #include <cfg/debug.h>
51 #include <cfg/module.h>
52
53
54 /* Local vars */
55 static Timer buz_timer;
56 static bool buz_timer_running;
57 static mtime_t buz_repeat_interval;
58 static mtime_t buz_repeat_duration;
59
60
61 /**
62  * Turn off buzzer, called by software timer
63  */
64 static void buz_softint(void)
65 {
66         if (IS_BUZZER_ON)
67         {
68                 BUZZER_OFF;
69                 if (buz_repeat_interval)
70                 {
71                         /* Wait for interval time */
72                         timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_interval));
73                         timer_add(&buz_timer);
74                 }
75                 else
76                         buz_timer_running = false;
77         }
78         else if (buz_repeat_interval)
79         {
80                 /* Wait for beep time */
81                 BUZZER_ON;
82                 timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_duration));
83                 timer_add(&buz_timer);
84         }
85         else
86                 buz_timer_running = false;
87 }
88
89
90 /**
91  * Beep for the specified ms time
92  */
93 void buz_beep(mtime_t time)
94 {
95         cpuflags_t flags;
96         IRQ_SAVE_DISABLE(flags);
97
98         /* Remove the software interrupt if it was already queued */
99         if (buz_timer_running)
100                 timer_abort(&buz_timer);
101
102         /* Turn on buzzer */
103         BUZZER_ON;
104
105         /* Add software interrupt to turn the buzzer off later */
106         buz_timer_running = true;
107         timer_setDelay(&buz_timer, ms_to_ticks(time));
108         timer_add(&buz_timer);
109
110         IRQ_RESTORE(flags);
111 }
112
113
114 /**
115  * Start buzzer repetition
116  */
117 void buz_repeat_start(mtime_t duration, mtime_t interval)
118 {
119         buz_repeat_interval = interval;
120         buz_repeat_duration = duration;
121         buz_beep(duration);
122 }
123
124
125 /**
126  * Stop buzzer repetition
127  */
128 void buz_repeat_stop(void)
129 {
130         cpuflags_t flags;
131         IRQ_SAVE_DISABLE(flags);
132
133         /* Remove the software interrupt if it was already queued */
134         if (buz_timer_running)
135         {
136                 timer_abort(&buz_timer);
137                 buz_timer_running = false;
138         }
139
140         buz_repeat_interval = 0;
141         BUZZER_OFF;
142
143         IRQ_RESTORE(flags);
144 }
145
146 MOD_DEFINE(buzzer)
147
148 /**
149  * Initialize buzzer.
150  */
151 void buz_init(void)
152 {
153         MOD_CHECK(timer);
154
155         BUZZER_HW_INIT;
156
157         /* Init software interrupt. */
158         timer_set_event_softint(&buz_timer, (Hook)buz_softint, 0);
159
160         MOD_INIT(buzzer);
161 }