Refactor BeRTOS to be in his own directory.
[bertos.git] / 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  * \version $Id$
37  *
38  * \author Giovanni Bajo <rasky@develer.com>
39  */
40
41 /*#*
42  *#* $Log$
43  *#* Revision 1.7  2006/07/19 12:56:25  bernie
44  *#* Convert to new Doxygen style.
45  *#*
46  *#* Revision 1.6  2005/11/04 16:20:02  bernie
47  *#* Fix reference to README.devlib in header.
48  *#*
49  *#* Revision 1.5  2005/04/11 19:10:27  bernie
50  *#* Include top-level headers from cfg/ subdir.
51  *#*
52  *#* Revision 1.4  2004/11/16 21:54:43  bernie
53  *#* Changes for SC Monoboard support.
54  *#*
55  *#* Revision 1.3  2004/08/25 14:12:08  rasky
56  *#* Aggiornato il comment block dei log RCS
57  *#*
58  *#* Revision 1.2  2004/06/03 11:27:09  bernie
59  *#* Add dual-license information.
60  *#*
61  *#* Revision 1.1  2004/05/23 18:36:05  bernie
62  *#* Import buzzerled driver.
63  *#*
64  *#*/
65
66 #ifndef DRV_BUZZERLED_DSP56K_H
67 #define DRV_BUZZERLED_DSP56K_H
68
69 #include <cfg/compiler.h>
70 #include <hw.h>
71 #include "pwm.h"
72
73 #if ARCH & ARCH_HECO
74
75 /**
76  * \name Connection of the leds to the DSP:
77  * <pre>
78  *   Led       Line    DSP Pin
79  *   ---------------------------
80  *   YELLOW    T2      HOME1/TB3
81  *   GREEN     T3      INDX1/TB2
82  *   RED       T4      PHB1/TB1
83  * </pre>
84  */
85
86 INLINE bool bld_is_inverted_intensity(enum BLD_DEVICE device)
87 {
88         return (device == BLD_GREEN_LED
89                 || device == BLD_YELLOW_LED
90                 || device == BLD_RED_LED);
91 }
92
93 INLINE bool bld_is_pwm(enum BLD_DEVICE device)
94 {
95         // Only the buzzer is connected to a PWM
96         return (device == BLD_BUZZER || device == BLD_READY_LED);
97 }
98
99 INLINE bool bld_is_timer(enum BLD_DEVICE device)
100 {
101         // LEDs are connected to timers
102         return (device == BLD_GREEN_LED || device == BLD_YELLOW_LED || device == BLD_RED_LED);
103 }
104
105 INLINE uint16_t bld_get_pwm(enum BLD_DEVICE device)
106 {
107         switch (device)
108         {
109         default: ASSERT(0);
110         case BLD_BUZZER: return 5;  // PWMA5
111         case BLD_READY_LED:  return 9;   // PWMB3
112         }
113 }
114
115
116 INLINE struct REG_TIMER_STRUCT* bld_get_timer(enum BLD_DEVICE device)
117 {
118         switch (device)
119         {
120         default: ASSERT(0);
121         case BLD_GREEN_LED: return &REG_TIMER_B[2];
122         case BLD_RED_LED: return &REG_TIMER_B[1];
123         case BLD_YELLOW_LED: return &REG_TIMER_B[3];
124         }
125 }
126
127 INLINE void bld_hw_init(void)
128 {
129 }
130
131 INLINE void bld_hw_set(enum BLD_DEVICE device, bool enable)
132 {
133         if (bld_is_inverted_intensity(device))
134                 enable = !enable;
135
136         // Handle a BLD connected to a PWM
137         if (bld_is_pwm(device))
138         {
139                 struct PWM* pwm = pwm_get_handle(bld_get_pwm(device));
140
141                 pwm_set_enable(pwm, false);
142                 pwm_set_dutycycle_percent(pwm, (enable ? 50 : 0));
143                 pwm_set_enable(pwm, true);
144         }
145         else if (bld_is_timer(device))
146         {
147                 struct REG_TIMER_STRUCT* timer = bld_get_timer(device);
148
149                 // Check that the timer is currently stopped, and the OFLAG is not
150                 //  controlled by another timer. Otherwise, the led is already 
151                 //  controlled by the timer, and we cannot correctly set it 
152                 //  on/off without reprogramming the timer.
153                 ASSERT((timer->CTRL & REG_TIMER_CTRL_MODE_MASK) == REG_TIMER_CTRL_MODE_STOP);
154                 ASSERT(!(timer->SCR & REG_TIMER_SCR_EEOF));
155
156                 // Check also that polarity is correct
157                 ASSERT(!(timer->SCR & REG_TIMER_SCR_OPS));
158
159                 // Without programming the timer, we have a way to manually force a certain
160                 //  value on the external pin. We also need to enable the output pin.
161                 timer->SCR &= ~REG_TIMER_SCR_VAL_1;
162                 timer->SCR |= REG_TIMER_SCR_OEN |
163                               REG_TIMER_SCR_FORCE |
164                               (!enable ? REG_TIMER_SCR_VAL_0 : REG_TIMER_SCR_VAL_1);
165         }
166         else
167                 ASSERT(0);
168 }
169
170 #elif ARCH & ARCH_SC
171
172 // We do not need inline functions here, because constant propagation is not big deal here
173 void bld_hw_init(void);
174 void bld_hw_set(enum BLD_DEVICE device, bool enable);
175
176 #endif
177
178 #endif /* DRV_BUZZERLED_DSP56K_H */