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