DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
[bertos.git] / drv / buzzer.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2003, 2004 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 devlib/README 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  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.12  2004/12/13 12:07:06  bernie
20  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
21  *#*
22  *#* Revision 1.11  2004/12/08 09:11:53  bernie
23  *#* Rename time_t to mtime_t.
24  *#*
25  *#* Revision 1.10  2004/10/03 18:38:51  bernie
26  *#* Add missing AVR header; Fix header.
27  *#*
28  *#* Revision 1.9  2004/09/14 21:01:25  bernie
29  *#* Use new AVR port pin names.
30  *#*
31  *#* Revision 1.8  2004/08/25 14:12:08  rasky
32  *#* Aggiornato il comment block dei log RCS
33  *#*
34  *#* Revision 1.7  2004/08/24 16:53:43  bernie
35  *#* Add missing headers.
36  *#*
37  *#* Revision 1.6  2004/06/07 18:10:06  aleph
38  *#* Remove free pool of timers; use user-provided Timer structure instead
39  *#*
40  *#* Revision 1.5  2004/06/07 15:54:23  aleph
41  *#* Update to new event.h naming
42  *#*
43  *#* Revision 1.4  2004/06/06 16:09:22  bernie
44  *#* Reformat (from project_ks).
45  *#*
46  *#* Revision 1.3  2004/06/03 11:27:09  bernie
47  *#* Add dual-license information.
48  *#*
49  *#* Revision 1.2  2004/05/23 18:21:53  bernie
50  *#* Trim CVS logs and cleanup header info.
51  *#*
52  *#*/
53
54 #include "buzzer.h"
55
56 #include <drv/timer.h>
57 #include <kern/event.h>
58 #include <debug.h>
59 #include <hw.h>
60 #include <arch_config.h>
61
62
63 #if (ARCH & ARCH_EMUL)
64
65         int Emul_IsBuzzerOn(void);
66         void Emul_BuzzerOn(void);
67         void Emul_BuzzerOff(void);
68         void Emul_BuzzerInit(void);
69
70         #define IS_BUZZER_ON  (Emul_IsBuzzerOn())
71         #define BUZZER_ON     (Emul_BuzzerOn())
72         #define BUZZER_OFF    (Emul_BuzzerOff())
73         #define BUZZER_INIT   (Emul_BuzzerInit())
74
75 #elif defined(__AVR__)
76
77         #include <avr/io.h>
78
79         #define IS_BUZZER_ON  (PORTG & BV(PG0))
80
81         /*!
82          * \name Buzzer manipulation macros.
83          *
84          * \note Some PORTG functions are being used from
85          *       interrupt code, so we must be careful to
86          *       avoid race conditions.
87          * \{
88          */
89         #define BUZZER_ON ATOMIC(PORTG |= BV(PG0))
90         #define BUZZER_OFF ATOMIC(PORTG &= ~BV(PG0))
91         #define BUZZER_INIT ATOMIC(PORTG &= ~BV(PG0); DDRG |= BV(PG0);)
92         /*\}*/
93
94 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
95
96         #define IS_BUZZER_ON  (cpld->Buzzer & 1)
97         #define BUZZER_ON     (cpld->Buzzer = 1)
98         #define BUZZER_OFF    (cpld->Buzzer = 0)
99         #define BUZZER_INIT   (cpld->Buzzer = 0)
100
101 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
102
103
104 /* Local vars */
105 static Timer buz_timer;
106 static bool buz_timer_running;
107 static mtime_t buz_repeat_interval;
108 static mtime_t buz_repeat_duration;
109
110
111 /*!
112  * Turn off buzzer, called by software timer
113  */
114 static void buz_softint(void)
115 {
116         if (IS_BUZZER_ON)
117         {
118                 BUZZER_OFF;
119                 if (buz_repeat_interval)
120                 {
121                         /* Wait for interval time */
122                         buz_timer.delay = buz_repeat_interval;
123                         timer_add(&buz_timer);
124                 }
125                 else
126                         buz_timer_running = false;
127         }
128         else if (buz_repeat_interval)
129         {
130                 /* Wait for beep time */
131                 BUZZER_ON;
132                 buz_timer.delay = buz_repeat_duration;
133                 timer_add(&buz_timer);
134         }
135         else
136                 buz_timer_running = false;
137 }
138
139
140 /*!
141  * Beep for the specified ms time
142  */
143 void buz_beep(mtime_t time)
144 {
145         cpuflags_t flags;
146         IRQ_SAVE_DISABLE(flags);
147
148         /* Remove the software interrupt if it was already queued */
149         if (buz_timer_running)
150                 timer_abort(&buz_timer);
151
152         /* Turn on buzzer */
153         BUZZER_ON;
154
155         /* Add software interrupt to turn the buzzer off later */
156         buz_timer_running = true;
157         buz_timer.delay = time;
158         timer_add(&buz_timer);
159
160         IRQ_RESTORE(flags);
161 }
162
163
164 /*!
165  * Start buzzer repetition
166  */
167 void buz_repeat_start(mtime_t duration, mtime_t interval)
168 {
169         buz_repeat_interval = interval;
170         buz_repeat_duration = duration;
171         buz_beep(duration);
172 }
173
174
175 /*!
176  * Stop buzzer repetition
177  */
178 void buz_repeat_stop(void)
179 {
180         cpuflags_t flags;
181         IRQ_SAVE_DISABLE(flags);
182
183         /* Remove the software interrupt if it was already queued */
184         if (buz_timer_running)
185         {
186                 timer_abort(&buz_timer);
187                 buz_timer_running = false;
188         }
189
190         buz_repeat_interval = 0;
191         BUZZER_OFF;
192
193         IRQ_RESTORE(flags);
194 }
195
196
197 /*!
198  * Initialize buzzer
199  */
200 void buz_init(void)
201 {
202         BUZZER_INIT;
203
204         /* Inizializza software interrupt */
205         event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);
206 }