Include top-level headers from cfg/ subdir.
[bertos.git] / drv / buzzer.c
1 /*!
2  * \file
3  * <!--
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.
7  * -->
8  *
9  * \version $Id$
10  *
11  * \brief Buzzer driver (implementation)
12  *
13  * \version $Id$
14  * \author Bernardo Innocenti <bernie@develer.com>
15  */
16
17 /*#*
18  *#* $Log$
19  *#* Revision 1.14  2005/04/11 19:10:27  bernie
20  *#* Include top-level headers from cfg/ subdir.
21  *#*
22  *#* Revision 1.13  2005/02/18 11:20:15  bernie
23  *#* Use mware/event.h; Update copyright info.
24  *#*
25  *#* Revision 1.12  2004/12/13 12:07:06  bernie
26  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
27  *#*
28  *#* Revision 1.11  2004/12/08 09:11:53  bernie
29  *#* Rename time_t to mtime_t.
30  *#*
31  *#* Revision 1.10  2004/10/03 18:38:51  bernie
32  *#* Add missing AVR header; Fix header.
33  *#*
34  *#* Revision 1.9  2004/09/14 21:01:25  bernie
35  *#* Use new AVR port pin names.
36  *#*/
37
38 #include "buzzer.h"
39
40 #include <drv/timer.h>
41 #include <mware/event.h>
42
43 #include <cfg/macros.h> /* BV() */
44 #include <cfg/debug.h>
45 #include <hw.h>
46 #include <cfg/arch_config.h>
47
48
49 #if (ARCH & ARCH_EMUL)
50
51         int Emul_IsBuzzerOn(void);
52         void Emul_BuzzerOn(void);
53         void Emul_BuzzerOff(void);
54         void Emul_BuzzerInit(void);
55
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())
60
61 #elif defined(__AVR__)
62
63         #include <avr/io.h>
64
65         #define IS_BUZZER_ON  (PORTG & BV(PG0))
66
67         /*!
68          * \name Buzzer manipulation macros.
69          *
70          * \note Some PORTG functions are being used from
71          *       interrupt code, so we must be careful to
72          *       avoid race conditions.
73          * \{
74          */
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);)
78         /*\}*/
79
80 #elif defined(__IAR_SYSTEMS_ICC) || defined(__IAR_SYSTEMS_ICC__) /* 80C196 */
81
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)
86
87 #endif /* ARCH, __AVR__, __IAR_SYSTEM_ICC */
88
89
90 /* Local vars */
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;
95
96
97 /*!
98  * Turn off buzzer, called by software timer
99  */
100 static void buz_softint(void)
101 {
102         if (IS_BUZZER_ON)
103         {
104                 BUZZER_OFF;
105                 if (buz_repeat_interval)
106                 {
107                         /* Wait for interval time */
108                         buz_timer.delay = buz_repeat_interval;
109                         timer_add(&buz_timer);
110                 }
111                 else
112                         buz_timer_running = false;
113         }
114         else if (buz_repeat_interval)
115         {
116                 /* Wait for beep time */
117                 BUZZER_ON;
118                 buz_timer.delay = buz_repeat_duration;
119                 timer_add(&buz_timer);
120         }
121         else
122                 buz_timer_running = false;
123 }
124
125
126 /*!
127  * Beep for the specified ms time
128  */
129 void buz_beep(mtime_t time)
130 {
131         cpuflags_t flags;
132         IRQ_SAVE_DISABLE(flags);
133
134         /* Remove the software interrupt if it was already queued */
135         if (buz_timer_running)
136                 timer_abort(&buz_timer);
137
138         /* Turn on buzzer */
139         BUZZER_ON;
140
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);
145
146         IRQ_RESTORE(flags);
147 }
148
149
150 /*!
151  * Start buzzer repetition
152  */
153 void buz_repeat_start(mtime_t duration, mtime_t interval)
154 {
155         buz_repeat_interval = interval;
156         buz_repeat_duration = duration;
157         buz_beep(duration);
158 }
159
160
161 /*!
162  * Stop buzzer repetition
163  */
164 void buz_repeat_stop(void)
165 {
166         cpuflags_t flags;
167         IRQ_SAVE_DISABLE(flags);
168
169         /* Remove the software interrupt if it was already queued */
170         if (buz_timer_running)
171         {
172                 timer_abort(&buz_timer);
173                 buz_timer_running = false;
174         }
175
176         buz_repeat_interval = 0;
177         BUZZER_OFF;
178
179         IRQ_RESTORE(flags);
180 }
181
182
183 /*!
184  * Initialize buzzer
185  */
186 void buz_init(void)
187 {
188         BUZZER_INIT;
189
190         /* Inizializza software interrupt */
191         event_initSoftInt(&buz_timer.expire, (Hook)buz_softint, 0);
192 }