659542c7ac68e19534361b2f6fa2b3a249b821b6
[bertos.git] / drv / buzzerled_dsp56k.h
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 Hardware support for buzzers and leds in DSP56K-based boards
10  *
11  * \version $Id$
12  *
13  * \author Giovanni Bajo <rasky@develer.com>
14  */
15
16 /*
17  * $Log$
18  * Revision 1.1  2004/05/23 18:36:05  bernie
19  * Import buzzerled driver.
20  *
21  */
22
23 #include <compiler.h>
24 #include <hw.h>
25 #include "pwm.h"
26
27 INLINE void bld_hw_init(void)
28 {
29 }
30
31 INLINE void bld_hw_set(enum BLD_DEVICE device, bool enable)
32 {
33         if (bld_is_inverted_intensity(device))
34                 enable = !enable;
35
36         // Handle a BLD connected to a PWM
37         if (bld_is_pwm(device))
38         {
39                 struct PWM* pwm = pwm_get_handle(bld_get_pwm(device));
40
41                 pwm_set_enable(pwm, false);
42                 pwm_set_dutycycle_percent(pwm, (enable ? 50 : 0));
43                 pwm_set_enable(pwm, true);
44         }
45         else if (bld_is_timer(device))
46         {
47                 struct REG_TIMER_STRUCT* timer = bld_get_timer(device);
48
49                 // Check that the timer is currently stopped, and the OFLAG is not
50                 //  controlled by another timer. Otherwise, the led is already 
51                 //  controlled by the timer, and we cannot correctly set it 
52                 //  on/off without reprogramming the timer.
53                 ASSERT((timer->CTRL & REG_TIMER_CTRL_MODE_MASK) == REG_TIMER_CTRL_MODE_STOP);
54                 ASSERT(!(timer->SCR & REG_TIMER_SCR_EEOF));
55
56                 // Check also that polarity is correct
57                 ASSERT(!(timer->SCR & REG_TIMER_SCR_OPS));
58
59                 // Without programming the timer, we have a way to manually force a certain
60                 //  value on the external pin. We also need to enable the output pin.
61                 timer->SCR &= ~REG_TIMER_SCR_VAL_1;
62                 timer->SCR |= REG_TIMER_SCR_OEN |
63                               REG_TIMER_SCR_FORCE |
64                               (!enable ? REG_TIMER_SCR_VAL_0 : REG_TIMER_SCR_VAL_1);
65         }
66         else
67                 ASSERT(0);
68 }
69