Remove \version svn tag.
[bertos.git] / bertos / cpu / dsp56k / drv / buzzerled_dsp56k.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  *
32  * -->
33  *
34  * \brief Hardware support for buzzers and leds in DSP56K-based boards
35  *
36  *
37  * \author Giovanni Bajo <rasky@develer.com>
38  */
39
40 /*#*
41  *#* $Log$
42  *#* Revision 1.7  2006/07/19 12:56:25  bernie
43  *#* Convert to new Doxygen style.
44  *#*
45  *#* Revision 1.6  2005/11/04 16:20:02  bernie
46  *#* Fix reference to README.devlib in header.
47  *#*
48  *#* Revision 1.5  2005/04/11 19:10:27  bernie
49  *#* Include top-level headers from cfg/ subdir.
50  *#*
51  *#* Revision 1.4  2004/11/16 21:54:43  bernie
52  *#* Changes for SC Monoboard support.
53  *#*
54  *#* Revision 1.3  2004/08/25 14:12:08  rasky
55  *#* Aggiornato il comment block dei log RCS
56  *#*
57  *#* Revision 1.2  2004/06/03 11:27:09  bernie
58  *#* Add dual-license information.
59  *#*
60  *#* Revision 1.1  2004/05/23 18:36:05  bernie
61  *#* Import buzzerled driver.
62  *#*
63  *#*/
64
65 #ifndef DRV_BUZZERLED_DSP56K_H
66 #define DRV_BUZZERLED_DSP56K_H
67
68 #include <cfg/compiler.h>
69 #include <hw.h>
70 #include "pwm.h"
71
72 #if ARCH & ARCH_HECO
73
74 /**
75  * \name Connection of the leds to the DSP:
76  * <pre>
77  *   Led       Line    DSP Pin
78  *   ---------------------------
79  *   YELLOW    T2      HOME1/TB3
80  *   GREEN     T3      INDX1/TB2
81  *   RED       T4      PHB1/TB1
82  * </pre>
83  */
84
85 INLINE bool bld_is_inverted_intensity(enum BLD_DEVICE device)
86 {
87         return (device == BLD_GREEN_LED
88                 || device == BLD_YELLOW_LED
89                 || device == BLD_RED_LED);
90 }
91
92 INLINE bool bld_is_pwm(enum BLD_DEVICE device)
93 {
94         // Only the buzzer is connected to a PWM
95         return (device == BLD_BUZZER || device == BLD_READY_LED);
96 }
97
98 INLINE bool bld_is_timer(enum BLD_DEVICE device)
99 {
100         // LEDs are connected to timers
101         return (device == BLD_GREEN_LED || device == BLD_YELLOW_LED || device == BLD_RED_LED);
102 }
103
104 INLINE uint16_t bld_get_pwm(enum BLD_DEVICE device)
105 {
106         switch (device)
107         {
108         default: ASSERT(0);
109         case BLD_BUZZER: return 5;  // PWMA5
110         case BLD_READY_LED:  return 9;   // PWMB3
111         }
112 }
113
114
115 INLINE struct REG_TIMER_STRUCT* bld_get_timer(enum BLD_DEVICE device)
116 {
117         switch (device)
118         {
119         default: ASSERT(0);
120         case BLD_GREEN_LED: return &REG_TIMER_B[2];
121         case BLD_RED_LED: return &REG_TIMER_B[1];
122         case BLD_YELLOW_LED: return &REG_TIMER_B[3];
123         }
124 }
125
126 INLINE void bld_hw_init(void)
127 {
128 }
129
130 INLINE void bld_hw_set(enum BLD_DEVICE device, bool enable)
131 {
132         if (bld_is_inverted_intensity(device))
133                 enable = !enable;
134
135         // Handle a BLD connected to a PWM
136         if (bld_is_pwm(device))
137         {
138                 struct PWM* pwm = pwm_get_handle(bld_get_pwm(device));
139
140                 pwm_set_enable(pwm, false);
141                 pwm_set_dutycycle_percent(pwm, (enable ? 50 : 0));
142                 pwm_set_enable(pwm, true);
143         }
144         else if (bld_is_timer(device))
145         {
146                 struct REG_TIMER_STRUCT* timer = bld_get_timer(device);
147
148                 // Check that the timer is currently stopped, and the OFLAG is not
149                 //  controlled by another timer. Otherwise, the led is already 
150                 //  controlled by the timer, and we cannot correctly set it 
151                 //  on/off without reprogramming the timer.
152                 ASSERT((timer->CTRL & REG_TIMER_CTRL_MODE_MASK) == REG_TIMER_CTRL_MODE_STOP);
153                 ASSERT(!(timer->SCR & REG_TIMER_SCR_EEOF));
154
155                 // Check also that polarity is correct
156                 ASSERT(!(timer->SCR & REG_TIMER_SCR_OPS));
157
158                 // Without programming the timer, we have a way to manually force a certain
159                 //  value on the external pin. We also need to enable the output pin.
160                 timer->SCR &= ~REG_TIMER_SCR_VAL_1;
161                 timer->SCR |= REG_TIMER_SCR_OEN |
162                               REG_TIMER_SCR_FORCE |
163                               (!enable ? REG_TIMER_SCR_VAL_0 : REG_TIMER_SCR_VAL_1);
164         }
165         else
166                 ASSERT(0);
167 }
168
169 #elif ARCH & ARCH_SC
170
171 // We do not need inline functions here, because constant propagation is not big deal here
172 void bld_hw_init(void);
173 void bld_hw_set(enum BLD_DEVICE device, bool enable);
174
175 #endif
176
177 #endif /* DRV_BUZZERLED_DSP56K_H */