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.3 2004/06/03 11:27:09 bernie
19 * Add dual-license information.
21 * Revision 1.2 2004/05/23 18:21:53 bernie
22 * Trim CVS logs and cleanup header info.
30 #include <kern/event.h>
33 #if (ARCH & ARCH_EMUL)
35 int Emul_IsBuzzerOn(void);
36 void Emul_BuzzerOn(void);
37 void Emul_BuzzerOff(void);
38 void Emul_BuzzerInit(void);
40 # define IS_BUZZER_ON (Emul_IsBuzzerOn())
41 # define BUZZER_ON (Emul_BuzzerOn())
42 # define BUZZER_OFF (Emul_BuzzerOff())
43 # define BUZZER_INIT (Emul_BuzzerInit())
45 #elif defined(__AVR__)
47 # define IS_BUZZER_ON (PORTG & BV(PORTG0))
50 * Buzzer manipulation macros
52 * \note Some PORTG functions are being used from
53 * interrupt code, so we must be careful to
54 * avoid race conditions.
59 DISABLE_IRQSAVE(_flags); \
60 PORTG |= BV(PORTG0); \
61 ENABLE_IRQRESTORE(_flags); \
67 DISABLE_IRQSAVE(_flags); \
68 PORTG &= ~BV(PORTG0); \
69 ENABLE_IRQRESTORE(_flags); \
72 # define BUZZER_INIT \
75 DISABLE_IRQSAVE(_flags); \
76 PORTG &= ~BV(PORTG0); \
78 ENABLE_IRQRESTORE(_flags); \
81 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
83 # define IS_BUZZER_ON (cpld->Buzzer & 1)
84 # define BUZZER_ON (cpld->Buzzer = 1)
85 # define BUZZER_OFF (cpld->Buzzer = 0)
86 # define BUZZER_INIT (cpld->Buzzer = 0)
88 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
92 static Timer *buz_timer;
93 static bool buz_timer_running;
94 static time_t buz_repeat_interval;
95 static time_t buz_repeat_duration;
99 * Turn off buzzer, called by software timer
101 static void buz_softint(void)
106 if (buz_repeat_interval)
108 /* Wait for interval time */
109 buz_timer->delay = buz_repeat_interval;
110 timer_add(buz_timer);
113 buz_timer_running = false;
115 else if (buz_repeat_interval)
117 /* Wait for beep time */
119 buz_timer->delay = buz_repeat_duration;
120 timer_add(buz_timer);
123 buz_timer_running = false;
128 * Beep for the specified ms time
130 void buz_beep(time_t time)
134 /* Remove the software interrupt if it was already queued */
135 DISABLE_IRQSAVE(flags);
136 if (buz_timer_running)
137 timer_abort(buz_timer);
142 /* Add software interrupt to turn the buzzer off later */
143 buz_timer_running = true;
144 buz_timer->delay = time;
145 timer_add(buz_timer);
147 ENABLE_IRQRESTORE(flags);
152 * Start buzzer repetition
154 void buz_repeat_start(time_t duration, time_t interval)
156 buz_repeat_interval = interval;
157 buz_repeat_duration = duration;
163 * Stop buzzer repetition
165 void buz_repeat_stop(void)
168 DISABLE_IRQSAVE(flags);
170 /* Remove the software interrupt if it was already queued */
171 if (buz_timer_running)
173 timer_abort(buz_timer);
174 buz_timer_running = false;
177 buz_repeat_interval = 0;
180 ENABLE_IRQRESTORE(flags);
191 /* Inizializza software interrupt */
192 buz_timer = timer_new();
193 ASSERT(buz_timer != NULL);
194 INITEVENT_INT(&buz_timer->expire, (Hook)buz_softint, 0);