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.
11 * \brief Buzzer driver (implementation)
14 * \author Bernardo Innocenti <bernie@develer.com>
19 *#* Revision 1.11 2004/12/08 09:11:53 bernie
20 *#* Rename time_t to mtime_t.
22 *#* Revision 1.10 2004/10/03 18:38:51 bernie
23 *#* Add missing AVR header; Fix header.
25 *#* Revision 1.9 2004/09/14 21:01:25 bernie
26 *#* Use new AVR port pin names.
28 *#* Revision 1.8 2004/08/25 14:12:08 rasky
29 *#* Aggiornato il comment block dei log RCS
31 *#* Revision 1.7 2004/08/24 16:53:43 bernie
32 *#* Add missing headers.
34 *#* Revision 1.6 2004/06/07 18:10:06 aleph
35 *#* Remove free pool of timers; use user-provided Timer structure instead
37 *#* Revision 1.5 2004/06/07 15:54:23 aleph
38 *#* Update to new event.h naming
40 *#* Revision 1.4 2004/06/06 16:09:22 bernie
41 *#* Reformat (from project_ks).
43 *#* Revision 1.3 2004/06/03 11:27:09 bernie
44 *#* Add dual-license information.
46 *#* Revision 1.2 2004/05/23 18:21:53 bernie
47 *#* Trim CVS logs and cleanup header info.
53 #include <drv/timer.h>
54 #include <kern/event.h>
57 #include <arch_config.h>
60 #if (ARCH & ARCH_EMUL)
62 int Emul_IsBuzzerOn(void);
63 void Emul_BuzzerOn(void);
64 void Emul_BuzzerOff(void);
65 void Emul_BuzzerInit(void);
67 #define IS_BUZZER_ON (Emul_IsBuzzerOn())
68 #define BUZZER_ON (Emul_BuzzerOn())
69 #define BUZZER_OFF (Emul_BuzzerOff())
70 #define BUZZER_INIT (Emul_BuzzerInit())
72 #elif defined(__AVR__)
76 #define IS_BUZZER_ON (PORTG & BV(PG0))
79 * Buzzer manipulation macros
81 * \note Some PORTG functions are being used from
82 * interrupt code, so we must be careful to
83 * avoid race conditions.
88 DISABLE_IRQSAVE(_flags); \
90 ENABLE_IRQRESTORE(_flags); \
96 DISABLE_IRQSAVE(_flags); \
98 ENABLE_IRQRESTORE(_flags); \
101 #define BUZZER_INIT \
104 DISABLE_IRQSAVE(_flags); \
107 ENABLE_IRQRESTORE(_flags); \
110 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
112 #define IS_BUZZER_ON (cpld->Buzzer & 1)
113 #define BUZZER_ON (cpld->Buzzer = 1)
114 #define BUZZER_OFF (cpld->Buzzer = 0)
115 #define BUZZER_INIT (cpld->Buzzer = 0)
117 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
121 static Timer buz_timer;
122 static bool buz_timer_running;
123 static mtime_t buz_repeat_interval;
124 static mtime_t buz_repeat_duration;
128 * Turn off buzzer, called by software timer
130 static void buz_softint(void)
135 if (buz_repeat_interval)
137 /* Wait for interval time */
138 buz_timer.delay = buz_repeat_interval;
139 timer_add(&buz_timer);
142 buz_timer_running = false;
144 else if (buz_repeat_interval)
146 /* Wait for beep time */
148 buz_timer.delay = buz_repeat_duration;
149 timer_add(&buz_timer);
152 buz_timer_running = false;
157 * Beep for the specified ms time
159 void buz_beep(mtime_t time)
163 /* Remove the software interrupt if it was already queued */
164 DISABLE_IRQSAVE(flags);
165 if (buz_timer_running)
166 timer_abort(&buz_timer);
171 /* Add software interrupt to turn the buzzer off later */
172 buz_timer_running = true;
173 buz_timer.delay = time;
174 timer_add(&buz_timer);
176 ENABLE_IRQRESTORE(flags);
181 * Start buzzer repetition
183 void buz_repeat_start(mtime_t duration, mtime_t interval)
185 buz_repeat_interval = interval;
186 buz_repeat_duration = duration;
192 * Stop buzzer repetition
194 void buz_repeat_stop(void)
197 DISABLE_IRQSAVE(flags);
199 /* Remove the software interrupt if it was already queued */
200 if (buz_timer_running)
202 timer_abort(&buz_timer);
203 buz_timer_running = false;
206 buz_repeat_interval = 0;
209 ENABLE_IRQRESTORE(flags);
220 /* Inizializza software interrupt */
221 event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);