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