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