f7dfcefcaae1124c99ed69c6e09d9b83d764f35f
[bertos.git] / drv / buzzerled.c
1 /*!
2  * \file
3  * <!--
4  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
5  * Copyright 2004 Giovanni Bajo
6  * All Rights Reserved.
7  * -->
8  *
9  * \brief Generic library to handle buzzers and leds
10  *
11  * This library is divided into three different layers:
12  *
13  *  - The topmost portable layer is buzzerled.[ch] which exposes a common API
14  *    enable/disable the devices. Basically, it handles the asynchronism to
15  *    implement bld_beep and bld_repeat.
16  *  - The middle layer is CPU-specific and exposes a single main function which
17  *    turns on/off each device.
18  *  - The lower layer is board-specific and communicates with the middle layer
19  *    with any required API. The idea is that devices can be tied to the CPU in
20  *    many different ways (many different pins), so this part should describe
21  *    which devices are present, and how they are connected.
22  *
23  * \version $Id$
24  *
25  * \author Giovanni Bajo <rasky@develer.com>
26  */
27
28 /*
29  * $Log$
30  * Revision 1.1  2004/05/23 18:36:05  bernie
31  * Import buzzerled driver.
32  *
33  */
34
35 #include "buzzerled.h"
36 #include "timer.h"
37
38 #if defined(__m56800__)
39         #include "buzzerled_dsp56k.h"
40 #else
41         #error Unsupported architecture
42 #endif
43
44 static struct Timer timers[NUM_BLDS];
45 static bool timer_go[NUM_BLDS];
46
47 INLINE enum BLD_DEVICE hook_parm_to_device(void* parm)
48 {
49         struct Timer* t = (struct Timer*)parm;
50         int num_bld = t - &timers[0];
51
52         ASSERT(num_bld >= 0);
53         ASSERT(num_bld < NUM_BLDS);
54
55         return (enum BLD_DEVICE)num_bld;
56 }
57
58 static void hook_turn_off(void* parm)
59 {
60         enum BLD_DEVICE num_bld = hook_parm_to_device(parm);
61         bld_set(num_bld, false);
62 }
63
64 void bld_init(void)
65 {
66         bld_hw_init();
67 }
68
69 void bld_set(enum BLD_DEVICE device, bool enable)
70 {
71         bld_hw_set(device, enable);
72 }
73
74 void bld_beep(enum BLD_DEVICE device, uint16_t duration)
75 {
76         struct Timer* t = &timers[device];
77         timer_set_delay(t, duration);
78         timer_set_event_softint(t, hook_turn_off, t);
79         timer_add(t);
80
81         bld_set(device, true);
82 }
83
84 void bld_beep_and_wait(enum BLD_DEVICE device, uint16_t duration)
85 {
86         bld_set(device, true);
87         timer_delay(duration);
88         bld_set(device, false);
89 }
90