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.12 2004/12/13 12:07:06 bernie
20 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
22 *#* Revision 1.11 2004/12/08 09:11:53 bernie
23 *#* Rename time_t to mtime_t.
25 *#* Revision 1.10 2004/10/03 18:38:51 bernie
26 *#* Add missing AVR header; Fix header.
28 *#* Revision 1.9 2004/09/14 21:01:25 bernie
29 *#* Use new AVR port pin names.
31 *#* Revision 1.8 2004/08/25 14:12:08 rasky
32 *#* Aggiornato il comment block dei log RCS
34 *#* Revision 1.7 2004/08/24 16:53:43 bernie
35 *#* Add missing headers.
37 *#* Revision 1.6 2004/06/07 18:10:06 aleph
38 *#* Remove free pool of timers; use user-provided Timer structure instead
40 *#* Revision 1.5 2004/06/07 15:54:23 aleph
41 *#* Update to new event.h naming
43 *#* Revision 1.4 2004/06/06 16:09:22 bernie
44 *#* Reformat (from project_ks).
46 *#* Revision 1.3 2004/06/03 11:27:09 bernie
47 *#* Add dual-license information.
49 *#* Revision 1.2 2004/05/23 18:21:53 bernie
50 *#* Trim CVS logs and cleanup header info.
56 #include <drv/timer.h>
57 #include <kern/event.h>
60 #include <arch_config.h>
63 #if (ARCH & ARCH_EMUL)
65 int Emul_IsBuzzerOn(void);
66 void Emul_BuzzerOn(void);
67 void Emul_BuzzerOff(void);
68 void Emul_BuzzerInit(void);
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())
75 #elif defined(__AVR__)
79 #define IS_BUZZER_ON (PORTG & BV(PG0))
82 * \name Buzzer manipulation macros.
84 * \note Some PORTG functions are being used from
85 * interrupt code, so we must be careful to
86 * avoid race conditions.
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);)
94 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
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)
101 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
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;
112 * Turn off buzzer, called by software timer
114 static void buz_softint(void)
119 if (buz_repeat_interval)
121 /* Wait for interval time */
122 buz_timer.delay = buz_repeat_interval;
123 timer_add(&buz_timer);
126 buz_timer_running = false;
128 else if (buz_repeat_interval)
130 /* Wait for beep time */
132 buz_timer.delay = buz_repeat_duration;
133 timer_add(&buz_timer);
136 buz_timer_running = false;
141 * Beep for the specified ms time
143 void buz_beep(mtime_t time)
146 IRQ_SAVE_DISABLE(flags);
148 /* Remove the software interrupt if it was already queued */
149 if (buz_timer_running)
150 timer_abort(&buz_timer);
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);
165 * Start buzzer repetition
167 void buz_repeat_start(mtime_t duration, mtime_t interval)
169 buz_repeat_interval = interval;
170 buz_repeat_duration = duration;
176 * Stop buzzer repetition
178 void buz_repeat_stop(void)
181 IRQ_SAVE_DISABLE(flags);
183 /* Remove the software interrupt if it was already queued */
184 if (buz_timer_running)
186 timer_abort(&buz_timer);
187 buz_timer_running = false;
190 buz_repeat_interval = 0;
204 /* Inizializza software interrupt */
205 event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);