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.6 2004/06/07 18:10:06 aleph
19 * Remove free pool of timers; use user-provided Timer structure instead
21 * Revision 1.5 2004/06/07 15:54:23 aleph
22 * Update to new event.h naming
24 * Revision 1.4 2004/06/06 16:09:22 bernie
25 * Reformat (from project_ks).
27 * Revision 1.3 2004/06/03 11:27:09 bernie
28 * Add dual-license information.
30 * Revision 1.2 2004/05/23 18:21:53 bernie
31 * Trim CVS logs and cleanup header info.
39 #include <kern/event.h>
42 #if (ARCH & ARCH_EMUL)
44 int Emul_IsBuzzerOn(void);
45 void Emul_BuzzerOn(void);
46 void Emul_BuzzerOff(void);
47 void Emul_BuzzerInit(void);
49 #define IS_BUZZER_ON (Emul_IsBuzzerOn())
50 #define BUZZER_ON (Emul_BuzzerOn())
51 #define BUZZER_OFF (Emul_BuzzerOff())
52 #define BUZZER_INIT (Emul_BuzzerInit())
54 #elif defined(__AVR__)
56 #define IS_BUZZER_ON (PORTG & BV(PORTG0))
59 * Buzzer manipulation macros
61 * \note Some PORTG functions are being used from
62 * interrupt code, so we must be careful to
63 * avoid race conditions.
68 DISABLE_IRQSAVE(_flags); \
69 PORTG |= BV(PORTG0); \
70 ENABLE_IRQRESTORE(_flags); \
76 DISABLE_IRQSAVE(_flags); \
77 PORTG &= ~BV(PORTG0); \
78 ENABLE_IRQRESTORE(_flags); \
84 DISABLE_IRQSAVE(_flags); \
85 PORTG &= ~BV(PORTG0); \
87 ENABLE_IRQRESTORE(_flags); \
90 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
92 #define IS_BUZZER_ON (cpld->Buzzer & 1)
93 #define BUZZER_ON (cpld->Buzzer = 1)
94 #define BUZZER_OFF (cpld->Buzzer = 0)
95 #define BUZZER_INIT (cpld->Buzzer = 0)
97 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
101 static Timer buz_timer;
102 static bool buz_timer_running;
103 static time_t buz_repeat_interval;
104 static time_t buz_repeat_duration;
108 * Turn off buzzer, called by software timer
110 static void buz_softint(void)
115 if (buz_repeat_interval)
117 /* Wait for interval time */
118 buz_timer.delay = buz_repeat_interval;
119 timer_add(&buz_timer);
122 buz_timer_running = false;
124 else if (buz_repeat_interval)
126 /* Wait for beep time */
128 buz_timer.delay = buz_repeat_duration;
129 timer_add(&buz_timer);
132 buz_timer_running = false;
137 * Beep for the specified ms time
139 void buz_beep(time_t time)
143 /* Remove the software interrupt if it was already queued */
144 DISABLE_IRQSAVE(flags);
145 if (buz_timer_running)
146 timer_abort(&buz_timer);
151 /* Add software interrupt to turn the buzzer off later */
152 buz_timer_running = true;
153 buz_timer.delay = time;
154 timer_add(&buz_timer);
156 ENABLE_IRQRESTORE(flags);
161 * Start buzzer repetition
163 void buz_repeat_start(time_t duration, time_t interval)
165 buz_repeat_interval = interval;
166 buz_repeat_duration = duration;
172 * Stop buzzer repetition
174 void buz_repeat_stop(void)
177 DISABLE_IRQSAVE(flags);
179 /* Remove the software interrupt if it was already queued */
180 if (buz_timer_running)
182 timer_abort(&buz_timer);
183 buz_timer_running = false;
186 buz_repeat_interval = 0;
189 ENABLE_IRQRESTORE(flags);
200 /* Inizializza software interrupt */
201 event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);