4 * Copyright 2003,2004 Develer S.r.l. (http://www.develer.com/)
5 * Copyright 1999,2003 Bernardo Innocenti <bernie@develer.com>
11 * \author Bernardo Innocenti <bernie@develer.com>
13 * \brief Buzzer driver
18 * Revision 1.2 2004/05/23 18:21:53 bernie
19 * Trim CVS logs and cleanup header info.
27 #include <kern/event.h>
30 #if (ARCH & ARCH_EMUL)
32 int Emul_IsBuzzerOn(void);
33 void Emul_BuzzerOn(void);
34 void Emul_BuzzerOff(void);
35 void Emul_BuzzerInit(void);
37 # define IS_BUZZER_ON (Emul_IsBuzzerOn())
38 # define BUZZER_ON (Emul_BuzzerOn())
39 # define BUZZER_OFF (Emul_BuzzerOff())
40 # define BUZZER_INIT (Emul_BuzzerInit())
42 #elif defined(__AVR__)
44 # define IS_BUZZER_ON (PORTG & BV(PORTG0))
47 * Buzzer manipulation macros
49 * \note Some PORTG functions are being used from
50 * interrupt code, so we must be careful to
51 * avoid race conditions.
56 DISABLE_IRQSAVE(_flags); \
57 PORTG |= BV(PORTG0); \
58 ENABLE_IRQRESTORE(_flags); \
64 DISABLE_IRQSAVE(_flags); \
65 PORTG &= ~BV(PORTG0); \
66 ENABLE_IRQRESTORE(_flags); \
69 # define BUZZER_INIT \
72 DISABLE_IRQSAVE(_flags); \
73 PORTG &= ~BV(PORTG0); \
75 ENABLE_IRQRESTORE(_flags); \
78 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
80 # define IS_BUZZER_ON (cpld->Buzzer & 1)
81 # define BUZZER_ON (cpld->Buzzer = 1)
82 # define BUZZER_OFF (cpld->Buzzer = 0)
83 # define BUZZER_INIT (cpld->Buzzer = 0)
85 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
89 static Timer *buz_timer;
90 static bool buz_timer_running;
91 static time_t buz_repeat_interval;
92 static time_t buz_repeat_duration;
96 * Turn off buzzer, called by software timer
98 static void buz_softint(void)
103 if (buz_repeat_interval)
105 /* Wait for interval time */
106 buz_timer->delay = buz_repeat_interval;
107 timer_add(buz_timer);
110 buz_timer_running = false;
112 else if (buz_repeat_interval)
114 /* Wait for beep time */
116 buz_timer->delay = buz_repeat_duration;
117 timer_add(buz_timer);
120 buz_timer_running = false;
125 * Beep for the specified ms time
127 void buz_beep(time_t time)
131 /* Remove the software interrupt if it was already queued */
132 DISABLE_IRQSAVE(flags);
133 if (buz_timer_running)
134 timer_abort(buz_timer);
139 /* Add software interrupt to turn the buzzer off later */
140 buz_timer_running = true;
141 buz_timer->delay = time;
142 timer_add(buz_timer);
144 ENABLE_IRQRESTORE(flags);
149 * Start buzzer repetition
151 void buz_repeat_start(time_t duration, time_t interval)
153 buz_repeat_interval = interval;
154 buz_repeat_duration = duration;
160 * Stop buzzer repetition
162 void buz_repeat_stop(void)
165 DISABLE_IRQSAVE(flags);
167 /* Remove the software interrupt if it was already queued */
168 if (buz_timer_running)
170 timer_abort(buz_timer);
171 buz_timer_running = false;
174 buz_repeat_interval = 0;
177 ENABLE_IRQRESTORE(flags);
188 /* Inizializza software interrupt */
189 buz_timer = timer_new();
190 ASSERT(buz_timer != NULL);
191 INITEVENT_INT(&buz_timer->expire, (Hook)buz_softint, 0);