Modularize hardware access; Port to new timer interface.
[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.15  2005/06/27 21:25:50  bernie
21  *#* Modularize hardware access; Port to new timer interface.
22  *#*
23  *#* Revision 1.14  2005/04/11 19:10:27  bernie
24  *#* Include top-level headers from cfg/ subdir.
25  *#*
26  *#* Revision 1.13  2005/02/18 11:20:15  bernie
27  *#* Use mware/event.h; Update copyright info.
28  *#*
29  *#* Revision 1.12  2004/12/13 12:07:06  bernie
30  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
31  *#*
32  *#* Revision 1.11  2004/12/08 09:11:53  bernie
33  *#* Rename time_t to mtime_t.
34  *#*
35  *#* Revision 1.10  2004/10/03 18:38:51  bernie
36  *#* Add missing AVR header; Fix header.
37  *#*
38  *#* Revision 1.9  2004/09/14 21:01:25  bernie
39  *#* Use new AVR port pin names.
40  *#*/
41
42
43 #include <hw_buzzer.h>
44 #include <drv/buzzer.h>
45
46 #include <drv/timer.h>
47 #include <drv/sipo.h>
48
49 #include <mware/event.h>
50
51 #include <cfg/debug.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
147 /*!
148  * Initialize buzzer
149  */
150 void buz_init(void)
151 {
152         BUZZER_INIT;
153
154         BUZZER_HW_INIT;
155         /* Inizializza software interrupt */
156         timer_set_event_softint(&buz_timer, (Hook)buz_softint, 0);
157 }