buzzer.c

Go to the documentation of this file.
00001 
00043 /*#*
00044  *#* $Log$
00045  *#* Revision 1.19  2006/07/19 12:56:25  bernie
00046  *#* Convert to new Doxygen style.
00047  *#*
00048  *#* Revision 1.18  2006/02/17 21:15:25  bernie
00049  *#* Add MOD_CHECK() checks.
00050  *#*
00051  *#* Revision 1.17  2006/02/10 12:30:18  bernie
00052  *#* Push interrupt protection inside hw module.
00053  *#*
00054  *#* Revision 1.16  2005/11/04 16:19:33  bernie
00055  *#* buz_init(): Restore IRQ protection as in project_bko.
00056  *#*
00057  *#* Revision 1.15  2005/06/27 21:25:50  bernie
00058  *#* Modularize hardware access; Port to new timer interface.
00059  *#*
00060  *#* Revision 1.14  2005/04/11 19:10:27  bernie
00061  *#* Include top-level headers from cfg/ subdir.
00062  *#*
00063  *#* Revision 1.13  2005/02/18 11:20:15  bernie
00064  *#* Use mware/event.h; Update copyright info.
00065  *#*
00066  *#* Revision 1.12  2004/12/13 12:07:06  bernie
00067  *#* DISABLE_IRQSAVE/ENABLE_IRQRESTORE: Convert to IRQ_SAVE_DISABLE/IRQ_RESTORE.
00068  *#*
00069  *#* Revision 1.11  2004/12/08 09:11:53  bernie
00070  *#* Rename time_t to mtime_t.
00071  *#*
00072  *#* Revision 1.10  2004/10/03 18:38:51  bernie
00073  *#* Add missing AVR header; Fix header.
00074  *#*
00075  *#* Revision 1.9  2004/09/14 21:01:25  bernie
00076  *#* Use new AVR port pin names.
00077  *#*/
00078 
00079 #include "buzzer.h"
00080 
00081 #include <hw_buzzer.h>
00082 #include <drv/timer.h>
00083 
00084 #include <mware/event.h>
00085 
00086 #include <cfg/debug.h>
00087 #include <cfg/module.h>
00088 
00089 
00090 /* Local vars */
00091 static Timer buz_timer;
00092 static bool buz_timer_running;
00093 static mtime_t buz_repeat_interval;
00094 static mtime_t buz_repeat_duration;
00095 
00096 
00100 static void buz_softint(void)
00101 {
00102     if (IS_BUZZER_ON)
00103     {
00104         BUZZER_OFF;
00105         if (buz_repeat_interval)
00106         {
00107             /* Wait for interval time */
00108             timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_interval));
00109             timer_add(&buz_timer);
00110         }
00111         else
00112             buz_timer_running = false;
00113     }
00114     else if (buz_repeat_interval)
00115     {
00116         /* Wait for beep time */
00117         BUZZER_ON;
00118         timer_setDelay(&buz_timer, ms_to_ticks(buz_repeat_duration));
00119         timer_add(&buz_timer);
00120     }
00121     else
00122         buz_timer_running = false;
00123 }
00124 
00125 
00129 void buz_beep(mtime_t time)
00130 {
00131     cpuflags_t flags;
00132     IRQ_SAVE_DISABLE(flags);
00133 
00134     /* Remove the software interrupt if it was already queued */
00135     if (buz_timer_running)
00136         timer_abort(&buz_timer);
00137 
00138     /* Turn on buzzer */
00139     BUZZER_ON;
00140 
00141     /* Add software interrupt to turn the buzzer off later */
00142     buz_timer_running = true;
00143     timer_setDelay(&buz_timer, ms_to_ticks(time));
00144     timer_add(&buz_timer);
00145 
00146     IRQ_RESTORE(flags);
00147 }
00148 
00149 
00153 void buz_repeat_start(mtime_t duration, mtime_t interval)
00154 {
00155     buz_repeat_interval = interval;
00156     buz_repeat_duration = duration;
00157     buz_beep(duration);
00158 }
00159 
00160 
00164 void buz_repeat_stop(void)
00165 {
00166     cpuflags_t flags;
00167     IRQ_SAVE_DISABLE(flags);
00168 
00169     /* Remove the software interrupt if it was already queued */
00170     if (buz_timer_running)
00171     {
00172         timer_abort(&buz_timer);
00173         buz_timer_running = false;
00174     }
00175 
00176     buz_repeat_interval = 0;
00177     BUZZER_OFF;
00178 
00179     IRQ_RESTORE(flags);
00180 }
00181 
00182 MOD_DEFINE(buzzer)
00183 
00184 
00187 void buz_init(void)
00188 {
00189     MOD_CHECK(timer);
00190 
00191     BUZZER_HW_INIT;
00192 
00193     /* Init software interrupt. */
00194     timer_set_event_softint(&buz_timer, (Hook)buz_softint, 0);
00195 
00196     MOD_INIT(buzzer);
00197 }