4 * Copyright 2003, 2004, 2005 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 README.devlib for information.
11 * \brief Buzzer driver (implementation)
14 * \author Bernardo Innocenti <bernie@develer.com>
19 *#* Revision 1.14 2005/04/11 19:10:27 bernie
20 *#* Include top-level headers from cfg/ subdir.
22 *#* Revision 1.13 2005/02/18 11:20:15 bernie
23 *#* Use mware/event.h; Update copyright info.
25 *#* Revision 1.12 2004/12/13 12:07:06 bernie
26 *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
28 *#* Revision 1.11 2004/12/08 09:11:53 bernie
29 *#* Rename time_t to mtime_t.
31 *#* Revision 1.10 2004/10/03 18:38:51 bernie
32 *#* Add missing AVR header; Fix header.
34 *#* Revision 1.9 2004/09/14 21:01:25 bernie
35 *#* Use new AVR port pin names.
40 #include <drv/timer.h>
41 #include <mware/event.h>
43 #include <cfg/macros.h> /* BV() */
44 #include <cfg/debug.h>
46 #include <cfg/arch_config.h>
49 #if (ARCH & ARCH_EMUL)
51 int Emul_IsBuzzerOn(void);
52 void Emul_BuzzerOn(void);
53 void Emul_BuzzerOff(void);
54 void Emul_BuzzerInit(void);
56 #define IS_BUZZER_ON (Emul_IsBuzzerOn())
57 #define BUZZER_ON (Emul_BuzzerOn())
58 #define BUZZER_OFF (Emul_BuzzerOff())
59 #define BUZZER_INIT (Emul_BuzzerInit())
61 #elif defined(__AVR__)
65 #define IS_BUZZER_ON (PORTG & BV(PG0))
68 * \name Buzzer manipulation macros.
70 * \note Some PORTG functions are being used from
71 * interrupt code, so we must be careful to
72 * avoid race conditions.
75 #define BUZZER_ON ATOMIC(PORTG |= BV(PG0))
76 #define BUZZER_OFF ATOMIC(PORTG &= ~BV(PG0))
77 #define BUZZER_INIT ATOMIC(PORTG &= ~BV(PG0); DDRG |= BV(PG0);)
80 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
82 #define IS_BUZZER_ON (cpld->Buzzer & 1)
83 #define BUZZER_ON (cpld->Buzzer = 1)
84 #define BUZZER_OFF (cpld->Buzzer = 0)
85 #define BUZZER_INIT (cpld->Buzzer = 0)
87 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
91 static Timer buz_timer;
92 static bool buz_timer_running;
93 static mtime_t buz_repeat_interval;
94 static mtime_t buz_repeat_duration;
98 * Turn off buzzer, called by software timer
100 static void buz_softint(void)
105 if (buz_repeat_interval)
107 /* Wait for interval time */
108 buz_timer.delay = buz_repeat_interval;
109 timer_add(&buz_timer);
112 buz_timer_running = false;
114 else if (buz_repeat_interval)
116 /* Wait for beep time */
118 buz_timer.delay = buz_repeat_duration;
119 timer_add(&buz_timer);
122 buz_timer_running = false;
127 * Beep for the specified ms time
129 void buz_beep(mtime_t time)
132 IRQ_SAVE_DISABLE(flags);
134 /* Remove the software interrupt if it was already queued */
135 if (buz_timer_running)
136 timer_abort(&buz_timer);
141 /* Add software interrupt to turn the buzzer off later */
142 buz_timer_running = true;
143 buz_timer.delay = time;
144 timer_add(&buz_timer);
151 * Start buzzer repetition
153 void buz_repeat_start(mtime_t duration, mtime_t interval)
155 buz_repeat_interval = interval;
156 buz_repeat_duration = duration;
162 * Stop buzzer repetition
164 void buz_repeat_stop(void)
167 IRQ_SAVE_DISABLE(flags);
169 /* Remove the software interrupt if it was already queued */
170 if (buz_timer_running)
172 timer_abort(&buz_timer);
173 buz_timer_running = false;
176 buz_repeat_interval = 0;
190 /* Inizializza software interrupt */
191 event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);