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.4 2004/06/06 16:09:22 bernie
19 * Reformat (from project_ks).
21 * Revision 1.3 2004/06/03 11:27:09 bernie
22 * Add dual-license information.
24 * Revision 1.2 2004/05/23 18:21:53 bernie
25 * Trim CVS logs and cleanup header info.
33 #include <kern/event.h>
36 #if (ARCH & ARCH_EMUL)
38 int Emul_IsBuzzerOn(void);
39 void Emul_BuzzerOn(void);
40 void Emul_BuzzerOff(void);
41 void Emul_BuzzerInit(void);
43 #define IS_BUZZER_ON (Emul_IsBuzzerOn())
44 #define BUZZER_ON (Emul_BuzzerOn())
45 #define BUZZER_OFF (Emul_BuzzerOff())
46 #define BUZZER_INIT (Emul_BuzzerInit())
48 #elif defined(__AVR__)
50 #define IS_BUZZER_ON (PORTG & BV(PORTG0))
53 * Buzzer manipulation macros
55 * \note Some PORTG functions are being used from
56 * interrupt code, so we must be careful to
57 * avoid race conditions.
62 DISABLE_IRQSAVE(_flags); \
63 PORTG |= BV(PORTG0); \
64 ENABLE_IRQRESTORE(_flags); \
70 DISABLE_IRQSAVE(_flags); \
71 PORTG &= ~BV(PORTG0); \
72 ENABLE_IRQRESTORE(_flags); \
78 DISABLE_IRQSAVE(_flags); \
79 PORTG &= ~BV(PORTG0); \
81 ENABLE_IRQRESTORE(_flags); \
84 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
86 #define IS_BUZZER_ON (cpld->Buzzer & 1)
87 #define BUZZER_ON (cpld->Buzzer = 1)
88 #define BUZZER_OFF (cpld->Buzzer = 0)
89 #define BUZZER_INIT (cpld->Buzzer = 0)
91 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
95 static Timer *buz_timer;
96 static bool buz_timer_running;
97 static time_t buz_repeat_interval;
98 static time_t buz_repeat_duration;
102 * Turn off buzzer, called by software timer
104 static void buz_softint(void)
109 if (buz_repeat_interval)
111 /* Wait for interval time */
112 buz_timer->delay = buz_repeat_interval;
113 timer_add(buz_timer);
116 buz_timer_running = false;
118 else if (buz_repeat_interval)
120 /* Wait for beep time */
122 buz_timer->delay = buz_repeat_duration;
123 timer_add(buz_timer);
126 buz_timer_running = false;
131 * Beep for the specified ms time
133 void buz_beep(time_t time)
137 /* Remove the software interrupt if it was already queued */
138 DISABLE_IRQSAVE(flags);
139 if (buz_timer_running)
140 timer_abort(buz_timer);
145 /* Add software interrupt to turn the buzzer off later */
146 buz_timer_running = true;
147 buz_timer->delay = time;
148 timer_add(buz_timer);
150 ENABLE_IRQRESTORE(flags);
155 * Start buzzer repetition
157 void buz_repeat_start(time_t duration, time_t interval)
159 buz_repeat_interval = interval;
160 buz_repeat_duration = duration;
166 * Stop buzzer repetition
168 void buz_repeat_stop(void)
171 DISABLE_IRQSAVE(flags);
173 /* Remove the software interrupt if it was already queued */
174 if (buz_timer_running)
176 timer_abort(buz_timer);
177 buz_timer_running = false;
180 buz_repeat_interval = 0;
183 ENABLE_IRQRESTORE(flags);
194 /* Inizializza software interrupt */
195 buz_timer = timer_new();
196 ASSERT(buz_timer != NULL);
197 INITEVENT_INT(&buz_timer->expire, (Hook)buz_softint, 0);