/* pwm.c */
#define PWM_MAX 0x1E4 /* This should be different than ADC frequency 125 kHz */
+#define PWM_STEP_SHIFT 2 /* second parameter of pwm_set is shifted by
+ * PWM_STEP_SHIFT bits to the right before setting
+ * into HW */
void init_pwm();
void susp_pwm();
void pwm_off(unsigned char n);
void pwm_set(unsigned char n, uint16_t stride);
+void pwm_timer();
/* tmr.c */
extern volatile uint16_t jiffies;
#include "lights.h"
+static uint16_t pwm[N_PWMLEDS];
+static volatile unsigned char step;
+
void init_pwm()
{
+ int i;
+
+ step = 0;
+
+ for (i = 0; i < N_PWMLEDS; i++)
+ pwm[n] = 0;
+
/* Async clock */
PLLCSR = _BV(PLLE);
void susp_pwm()
{
+ unsigned char i;
+
+ for (i = 0; i < N_PWMLEDS; i++)
+ pwm[i] = 0;
+
DDRB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 ));
TCCR1D = TCCR1C = TCCR1B = TCCR1A = 0;
TIMSK = 0;
void pwm_off(unsigned char n)
{
+ pwm[n] = 0;
+
switch (n) {
case 0: DDRB &= ~_BV(PB1); break;
case 1: DDRB &= ~_BV(PB3); break;
}
}
-void pwm_set(unsigned char n, uint16_t stride)
+static void pwm_update_hw(unsigned char n)
{
unsigned char hi, lo;
-
- if (stride > PWM_MAX)
- stride = PWM_MAX;
+ uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT;
if (n == 2)
stride = PWM_MAX - stride;
}
}
+void pwm_set(unsigned char n, uint16_t stride)
+{
+ if (((stride + (1 << PWM_STEP_SHIFT)) >> PWM_STEP_SHIFT) >= PWM_MAX)
+ stride = PWM_MAX << PWM_STEP_SHIFT;
+
+ pwm[n] = stride;
+ pwm_update_hw(n);
+}
+
+void pwm_timer()
+{
+ unsigned char i;
+
+ if (++step >= (1 << PWM_STEP_SHIFT))
+ step = 0;
+
+ for (i = 0; i < N_PWMLEDS; i++)
+ if (pwm[n])
+ pwm_update_hw(n);
+}
+
#if 0
static void inline pwm_handler()
{