buzzerled_dsp56k.h

Go to the documentation of this file.
00001 
00041 /*#*
00042  *#* $Log$
00043  *#* Revision 1.7  2006/07/19 12:56:25  bernie
00044  *#* Convert to new Doxygen style.
00045  *#*
00046  *#* Revision 1.6  2005/11/04 16:20:02  bernie
00047  *#* Fix reference to README.devlib in header.
00048  *#*
00049  *#* Revision 1.5  2005/04/11 19:10:27  bernie
00050  *#* Include top-level headers from cfg/ subdir.
00051  *#*
00052  *#* Revision 1.4  2004/11/16 21:54:43  bernie
00053  *#* Changes for SC Monoboard support.
00054  *#*
00055  *#* Revision 1.3  2004/08/25 14:12:08  rasky
00056  *#* Aggiornato il comment block dei log RCS
00057  *#*
00058  *#* Revision 1.2  2004/06/03 11:27:09  bernie
00059  *#* Add dual-license information.
00060  *#*
00061  *#* Revision 1.1  2004/05/23 18:36:05  bernie
00062  *#* Import buzzerled driver.
00063  *#*
00064  *#*/
00065 
00066 #ifndef DRV_BUZZERLED_DSP56K_H
00067 #define DRV_BUZZERLED_DSP56K_H
00068 
00069 #include <cfg/compiler.h>
00070 #include <hw.h>
00071 #include "pwm.h"
00072 
00073 #if ARCH & ARCH_HECO
00074 
00086 INLINE bool bld_is_inverted_intensity(enum BLD_DEVICE device)
00087 {
00088     return (device == BLD_GREEN_LED
00089             || device == BLD_YELLOW_LED
00090             || device == BLD_RED_LED);
00091 }
00092 
00093 INLINE bool bld_is_pwm(enum BLD_DEVICE device)
00094 {
00095     // Only the buzzer is connected to a PWM
00096     return (device == BLD_BUZZER || device == BLD_READY_LED);
00097 }
00098 
00099 INLINE bool bld_is_timer(enum BLD_DEVICE device)
00100 {
00101     // LEDs are connected to timers
00102     return (device == BLD_GREEN_LED || device == BLD_YELLOW_LED || device == BLD_RED_LED);
00103 }
00104 
00105 INLINE uint16_t bld_get_pwm(enum BLD_DEVICE device)
00106 {
00107     switch (device)
00108     {
00109     default: ASSERT(0);
00110     case BLD_BUZZER: return 5;  // PWMA5
00111     case BLD_READY_LED:  return 9;   // PWMB3
00112     }
00113 }
00114 
00115 
00116 INLINE struct REG_TIMER_STRUCT* bld_get_timer(enum BLD_DEVICE device)
00117 {
00118     switch (device)
00119     {
00120     default: ASSERT(0);
00121     case BLD_GREEN_LED: return &REG_TIMER_B[2];
00122     case BLD_RED_LED: return &REG_TIMER_B[1];
00123     case BLD_YELLOW_LED: return &REG_TIMER_B[3];
00124     }
00125 }
00126 
00127 INLINE void bld_hw_init(void)
00128 {
00129 }
00130 
00131 INLINE void bld_hw_set(enum BLD_DEVICE device, bool enable)
00132 {
00133     if (bld_is_inverted_intensity(device))
00134         enable = !enable;
00135 
00136     // Handle a BLD connected to a PWM
00137     if (bld_is_pwm(device))
00138     {
00139         struct PWM* pwm = pwm_get_handle(bld_get_pwm(device));
00140 
00141         pwm_set_enable(pwm, false);
00142         pwm_set_dutycycle_percent(pwm, (enable ? 50 : 0));
00143         pwm_set_enable(pwm, true);
00144     }
00145     else if (bld_is_timer(device))
00146     {
00147         struct REG_TIMER_STRUCT* timer = bld_get_timer(device);
00148 
00149         // Check that the timer is currently stopped, and the OFLAG is not
00150         //  controlled by another timer. Otherwise, the led is already 
00151         //  controlled by the timer, and we cannot correctly set it 
00152         //  on/off without reprogramming the timer.
00153         ASSERT((timer->CTRL & REG_TIMER_CTRL_MODE_MASK) == REG_TIMER_CTRL_MODE_STOP);
00154         ASSERT(!(timer->SCR & REG_TIMER_SCR_EEOF));
00155 
00156         // Check also that polarity is correct
00157         ASSERT(!(timer->SCR & REG_TIMER_SCR_OPS));
00158 
00159         // Without programming the timer, we have a way to manually force a certain
00160         //  value on the external pin. We also need to enable the output pin.
00161         timer->SCR &= ~REG_TIMER_SCR_VAL_1;
00162         timer->SCR |= REG_TIMER_SCR_OEN |
00163                       REG_TIMER_SCR_FORCE |
00164                       (!enable ? REG_TIMER_SCR_VAL_0 : REG_TIMER_SCR_VAL_1);
00165     }
00166     else
00167         ASSERT(0);
00168 }
00169 
00170 #elif ARCH & ARCH_SC
00171 
00172 // We do not need inline functions here, because constant propagation is not big deal here
00173 void bld_hw_init(void);
00174 void bld_hw_set(enum BLD_DEVICE device, bool enable);
00175 
00176 #endif
00177 
00178 #endif /* DRV_BUZZERLED_DSP56K_H */