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 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Buzzer driver
18 *#* Revision 1.9 2004/09/14 21:01:25 bernie
19 *#* Use new AVR port pin names.
21 *#* Revision 1.8 2004/08/25 14:12:08 rasky
22 *#* Aggiornato il comment block dei log RCS
24 *#* Revision 1.7 2004/08/24 16:53:43 bernie
25 *#* Add missing headers.
27 *#* Revision 1.6 2004/06/07 18:10:06 aleph
28 *#* Remove free pool of timers; use user-provided Timer structure instead
30 *#* Revision 1.5 2004/06/07 15:54:23 aleph
31 *#* Update to new event.h naming
33 *#* Revision 1.4 2004/06/06 16:09:22 bernie
34 *#* Reformat (from project_ks).
36 *#* Revision 1.3 2004/06/03 11:27:09 bernie
37 *#* Add dual-license information.
39 *#* Revision 1.2 2004/05/23 18:21:53 bernie
40 *#* Trim CVS logs and cleanup header info.
46 #include <drv/timer.h>
47 #include <kern/event.h>
50 #include <arch_config.h>
53 #if (ARCH & ARCH_EMUL)
55 int Emul_IsBuzzerOn(void);
56 void Emul_BuzzerOn(void);
57 void Emul_BuzzerOff(void);
58 void Emul_BuzzerInit(void);
60 #define IS_BUZZER_ON (Emul_IsBuzzerOn())
61 #define BUZZER_ON (Emul_BuzzerOn())
62 #define BUZZER_OFF (Emul_BuzzerOff())
63 #define BUZZER_INIT (Emul_BuzzerInit())
65 #elif defined(__AVR__)
67 #define IS_BUZZER_ON (PORTG & BV(PG0))
70 * Buzzer manipulation macros
72 * \note Some PORTG functions are being used from
73 * interrupt code, so we must be careful to
74 * avoid race conditions.
79 DISABLE_IRQSAVE(_flags); \
81 ENABLE_IRQRESTORE(_flags); \
87 DISABLE_IRQSAVE(_flags); \
89 ENABLE_IRQRESTORE(_flags); \
95 DISABLE_IRQSAVE(_flags); \
98 ENABLE_IRQRESTORE(_flags); \
101 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
103 #define IS_BUZZER_ON (cpld->Buzzer & 1)
104 #define BUZZER_ON (cpld->Buzzer = 1)
105 #define BUZZER_OFF (cpld->Buzzer = 0)
106 #define BUZZER_INIT (cpld->Buzzer = 0)
108 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
112 static Timer buz_timer;
113 static bool buz_timer_running;
114 static time_t buz_repeat_interval;
115 static time_t buz_repeat_duration;
119 * Turn off buzzer, called by software timer
121 static void buz_softint(void)
126 if (buz_repeat_interval)
128 /* Wait for interval time */
129 buz_timer.delay = buz_repeat_interval;
130 timer_add(&buz_timer);
133 buz_timer_running = false;
135 else if (buz_repeat_interval)
137 /* Wait for beep time */
139 buz_timer.delay = buz_repeat_duration;
140 timer_add(&buz_timer);
143 buz_timer_running = false;
148 * Beep for the specified ms time
150 void buz_beep(time_t time)
154 /* Remove the software interrupt if it was already queued */
155 DISABLE_IRQSAVE(flags);
156 if (buz_timer_running)
157 timer_abort(&buz_timer);
162 /* Add software interrupt to turn the buzzer off later */
163 buz_timer_running = true;
164 buz_timer.delay = time;
165 timer_add(&buz_timer);
167 ENABLE_IRQRESTORE(flags);
172 * Start buzzer repetition
174 void buz_repeat_start(time_t duration, time_t interval)
176 buz_repeat_interval = interval;
177 buz_repeat_duration = duration;
183 * Stop buzzer repetition
185 void buz_repeat_stop(void)
188 DISABLE_IRQSAVE(flags);
190 /* Remove the software interrupt if it was already queued */
191 if (buz_timer_running)
193 timer_abort(&buz_timer);
194 buz_timer_running = false;
197 buz_repeat_interval = 0;
200 ENABLE_IRQRESTORE(flags);
211 /* Inizializza software interrupt */
212 event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);