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.8 2004/08/25 14:12:08 rasky
19 *#* Aggiornato il comment block dei log RCS
21 *#* Revision 1.7 2004/08/24 16:53:43 bernie
22 *#* Add missing headers.
24 *#* Revision 1.6 2004/06/07 18:10:06 aleph
25 *#* Remove free pool of timers; use user-provided Timer structure instead
27 *#* Revision 1.5 2004/06/07 15:54:23 aleph
28 *#* Update to new event.h naming
30 *#* Revision 1.4 2004/06/06 16:09:22 bernie
31 *#* Reformat (from project_ks).
33 *#* Revision 1.3 2004/06/03 11:27:09 bernie
34 *#* Add dual-license information.
36 *#* Revision 1.2 2004/05/23 18:21:53 bernie
37 *#* Trim CVS logs and cleanup header info.
43 #include <drv/timer.h>
44 #include <drv/kdebug.h>
45 #include <kern/event.h>
47 #include <arch_config.h>
50 #if (ARCH & ARCH_EMUL)
52 int Emul_IsBuzzerOn(void);
53 void Emul_BuzzerOn(void);
54 void Emul_BuzzerOff(void);
55 void Emul_BuzzerInit(void);
57 #define IS_BUZZER_ON (Emul_IsBuzzerOn())
58 #define BUZZER_ON (Emul_BuzzerOn())
59 #define BUZZER_OFF (Emul_BuzzerOff())
60 #define BUZZER_INIT (Emul_BuzzerInit())
62 #elif defined(__AVR__)
64 #define IS_BUZZER_ON (PORTG & BV(PORTG0))
67 * Buzzer manipulation macros
69 * \note Some PORTG functions are being used from
70 * interrupt code, so we must be careful to
71 * avoid race conditions.
76 DISABLE_IRQSAVE(_flags); \
77 PORTG |= BV(PORTG0); \
78 ENABLE_IRQRESTORE(_flags); \
84 DISABLE_IRQSAVE(_flags); \
85 PORTG &= ~BV(PORTG0); \
86 ENABLE_IRQRESTORE(_flags); \
92 DISABLE_IRQSAVE(_flags); \
93 PORTG &= ~BV(PORTG0); \
95 ENABLE_IRQRESTORE(_flags); \
98 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
100 #define IS_BUZZER_ON (cpld->Buzzer & 1)
101 #define BUZZER_ON (cpld->Buzzer = 1)
102 #define BUZZER_OFF (cpld->Buzzer = 0)
103 #define BUZZER_INIT (cpld->Buzzer = 0)
105 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
109 static Timer buz_timer;
110 static bool buz_timer_running;
111 static time_t buz_repeat_interval;
112 static time_t buz_repeat_duration;
116 * Turn off buzzer, called by software timer
118 static void buz_softint(void)
123 if (buz_repeat_interval)
125 /* Wait for interval time */
126 buz_timer.delay = buz_repeat_interval;
127 timer_add(&buz_timer);
130 buz_timer_running = false;
132 else if (buz_repeat_interval)
134 /* Wait for beep time */
136 buz_timer.delay = buz_repeat_duration;
137 timer_add(&buz_timer);
140 buz_timer_running = false;
145 * Beep for the specified ms time
147 void buz_beep(time_t time)
151 /* Remove the software interrupt if it was already queued */
152 DISABLE_IRQSAVE(flags);
153 if (buz_timer_running)
154 timer_abort(&buz_timer);
159 /* Add software interrupt to turn the buzzer off later */
160 buz_timer_running = true;
161 buz_timer.delay = time;
162 timer_add(&buz_timer);
164 ENABLE_IRQRESTORE(flags);
169 * Start buzzer repetition
171 void buz_repeat_start(time_t duration, time_t interval)
173 buz_repeat_interval = interval;
174 buz_repeat_duration = duration;
180 * Stop buzzer repetition
182 void buz_repeat_stop(void)
185 DISABLE_IRQSAVE(flags);
187 /* Remove the software interrupt if it was already queued */
188 if (buz_timer_running)
190 timer_abort(&buz_timer);
191 buz_timer_running = false;
194 buz_repeat_interval = 0;
197 ENABLE_IRQRESTORE(flags);
208 /* Inizializza software interrupt */
209 event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);