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.10 2004/10/03 18:38:51 bernie
20 *#* Add missing AVR header; Fix header.
22 *#* Revision 1.9 2004/09/14 21:01:25 bernie
23 *#* Use new AVR port pin names.
25 *#* Revision 1.8 2004/08/25 14:12:08 rasky
26 *#* Aggiornato il comment block dei log RCS
28 *#* Revision 1.7 2004/08/24 16:53:43 bernie
29 *#* Add missing headers.
31 *#* Revision 1.6 2004/06/07 18:10:06 aleph
32 *#* Remove free pool of timers; use user-provided Timer structure instead
34 *#* Revision 1.5 2004/06/07 15:54:23 aleph
35 *#* Update to new event.h naming
37 *#* Revision 1.4 2004/06/06 16:09:22 bernie
38 *#* Reformat (from project_ks).
40 *#* Revision 1.3 2004/06/03 11:27:09 bernie
41 *#* Add dual-license information.
43 *#* Revision 1.2 2004/05/23 18:21:53 bernie
44 *#* Trim CVS logs and cleanup header info.
50 #include <drv/timer.h>
51 #include <kern/event.h>
54 #include <arch_config.h>
57 #if (ARCH & ARCH_EMUL)
59 int Emul_IsBuzzerOn(void);
60 void Emul_BuzzerOn(void);
61 void Emul_BuzzerOff(void);
62 void Emul_BuzzerInit(void);
64 #define IS_BUZZER_ON (Emul_IsBuzzerOn())
65 #define BUZZER_ON (Emul_BuzzerOn())
66 #define BUZZER_OFF (Emul_BuzzerOff())
67 #define BUZZER_INIT (Emul_BuzzerInit())
69 #elif defined(__AVR__)
73 #define IS_BUZZER_ON (PORTG & BV(PG0))
76 * Buzzer manipulation macros
78 * \note Some PORTG functions are being used from
79 * interrupt code, so we must be careful to
80 * avoid race conditions.
85 DISABLE_IRQSAVE(_flags); \
87 ENABLE_IRQRESTORE(_flags); \
93 DISABLE_IRQSAVE(_flags); \
95 ENABLE_IRQRESTORE(_flags); \
101 DISABLE_IRQSAVE(_flags); \
104 ENABLE_IRQRESTORE(_flags); \
107 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
109 #define IS_BUZZER_ON (cpld->Buzzer & 1)
110 #define BUZZER_ON (cpld->Buzzer = 1)
111 #define BUZZER_OFF (cpld->Buzzer = 0)
112 #define BUZZER_INIT (cpld->Buzzer = 0)
114 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
118 static Timer buz_timer;
119 static bool buz_timer_running;
120 static time_t buz_repeat_interval;
121 static time_t buz_repeat_duration;
125 * Turn off buzzer, called by software timer
127 static void buz_softint(void)
132 if (buz_repeat_interval)
134 /* Wait for interval time */
135 buz_timer.delay = buz_repeat_interval;
136 timer_add(&buz_timer);
139 buz_timer_running = false;
141 else if (buz_repeat_interval)
143 /* Wait for beep time */
145 buz_timer.delay = buz_repeat_duration;
146 timer_add(&buz_timer);
149 buz_timer_running = false;
154 * Beep for the specified ms time
156 void buz_beep(time_t time)
160 /* Remove the software interrupt if it was already queued */
161 DISABLE_IRQSAVE(flags);
162 if (buz_timer_running)
163 timer_abort(&buz_timer);
168 /* Add software interrupt to turn the buzzer off later */
169 buz_timer_running = true;
170 buz_timer.delay = time;
171 timer_add(&buz_timer);
173 ENABLE_IRQRESTORE(flags);
178 * Start buzzer repetition
180 void buz_repeat_start(time_t duration, time_t interval)
182 buz_repeat_interval = interval;
183 buz_repeat_duration = duration;
189 * Stop buzzer repetition
191 void buz_repeat_stop(void)
194 DISABLE_IRQSAVE(flags);
196 /* Remove the software interrupt if it was already queued */
197 if (buz_timer_running)
199 timer_abort(&buz_timer);
200 buz_timer_running = false;
203 buz_repeat_interval = 0;
206 ENABLE_IRQRESTORE(flags);
217 /* Inizializza software interrupt */
218 event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);