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.7 2004/08/24 16:53:43 bernie
19 * Add missing headers.
21 * Revision 1.6 2004/06/07 18:10:06 aleph
22 * Remove free pool of timers; use user-provided Timer structure instead
24 * Revision 1.5 2004/06/07 15:54:23 aleph
25 * Update to new event.h naming
27 * Revision 1.4 2004/06/06 16:09:22 bernie
28 * Reformat (from project_ks).
30 * Revision 1.3 2004/06/03 11:27:09 bernie
31 * Add dual-license information.
33 * Revision 1.2 2004/05/23 18:21:53 bernie
34 * Trim CVS logs and cleanup header info.
40 #include <drv/timer.h>
41 #include <drv/kdebug.h>
42 #include <kern/event.h>
44 #include <arch_config.h>
47 #if (ARCH & ARCH_EMUL)
49 int Emul_IsBuzzerOn(void);
50 void Emul_BuzzerOn(void);
51 void Emul_BuzzerOff(void);
52 void Emul_BuzzerInit(void);
54 #define IS_BUZZER_ON (Emul_IsBuzzerOn())
55 #define BUZZER_ON (Emul_BuzzerOn())
56 #define BUZZER_OFF (Emul_BuzzerOff())
57 #define BUZZER_INIT (Emul_BuzzerInit())
59 #elif defined(__AVR__)
61 #define IS_BUZZER_ON (PORTG & BV(PORTG0))
64 * Buzzer manipulation macros
66 * \note Some PORTG functions are being used from
67 * interrupt code, so we must be careful to
68 * avoid race conditions.
73 DISABLE_IRQSAVE(_flags); \
74 PORTG |= BV(PORTG0); \
75 ENABLE_IRQRESTORE(_flags); \
81 DISABLE_IRQSAVE(_flags); \
82 PORTG &= ~BV(PORTG0); \
83 ENABLE_IRQRESTORE(_flags); \
89 DISABLE_IRQSAVE(_flags); \
90 PORTG &= ~BV(PORTG0); \
92 ENABLE_IRQRESTORE(_flags); \
95 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
97 #define IS_BUZZER_ON (cpld->Buzzer & 1)
98 #define BUZZER_ON (cpld->Buzzer = 1)
99 #define BUZZER_OFF (cpld->Buzzer = 0)
100 #define BUZZER_INIT (cpld->Buzzer = 0)
102 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
106 static Timer buz_timer;
107 static bool buz_timer_running;
108 static time_t buz_repeat_interval;
109 static time_t buz_repeat_duration;
113 * Turn off buzzer, called by software timer
115 static void buz_softint(void)
120 if (buz_repeat_interval)
122 /* Wait for interval time */
123 buz_timer.delay = buz_repeat_interval;
124 timer_add(&buz_timer);
127 buz_timer_running = false;
129 else if (buz_repeat_interval)
131 /* Wait for beep time */
133 buz_timer.delay = buz_repeat_duration;
134 timer_add(&buz_timer);
137 buz_timer_running = false;
142 * Beep for the specified ms time
144 void buz_beep(time_t time)
148 /* Remove the software interrupt if it was already queued */
149 DISABLE_IRQSAVE(flags);
150 if (buz_timer_running)
151 timer_abort(&buz_timer);
156 /* Add software interrupt to turn the buzzer off later */
157 buz_timer_running = true;
158 buz_timer.delay = time;
159 timer_add(&buz_timer);
161 ENABLE_IRQRESTORE(flags);
166 * Start buzzer repetition
168 void buz_repeat_start(time_t duration, time_t interval)
170 buz_repeat_interval = interval;
171 buz_repeat_duration = duration;
177 * Stop buzzer repetition
179 void buz_repeat_stop(void)
182 DISABLE_IRQSAVE(flags);
184 /* Remove the software interrupt if it was already queued */
185 if (buz_timer_running)
187 timer_abort(&buz_timer);
188 buz_timer_running = false;
191 buz_repeat_interval = 0;
194 ENABLE_IRQRESTORE(flags);
205 /* Inizializza software interrupt */
206 event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);