#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
+#include <util/atomic.h>
#include "lights.h"
-#define PWM_MAX 0x0FF
+#define PWM_STEP_SHIFT 2 /* sub-LSB precision */
+#define PWM_TOP (((PWM_MAX) + (4 << (PWM_STEP_SHIFT))) >> (PWM_STEP_SHIFT))
+#if PWM_TOP > 0x3FF
+#error PWM_TOP too high
+#endif
-void init_pwm()
+static uint16_t pwm[N_PWMLEDS];
+static volatile unsigned char step;
+
+static void enable_pll()
{
/* Async clock */
PLLCSR = _BV(PLLE);
/* Synchronize to the phase lock */
- _delay_ms(1);
+ _delay_us(100);
while ((PLLCSR & _BV(PLOCK)) == 0)
;
PLLCSR |= _BV(PCKE);
+}
+
+void init_pwm()
+{
+ int i;
+
+ step = 0;
+
+ for (i = 0; i < N_PWMLEDS; i++)
+ pwm[i] = 0;
+
+ enable_pll();
// PWM channel D is inverted, ...
TCCR1C = _BV(COM1D1) | _BV(COM1D0) | _BV(PWM1D);
TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B);
TCCR1D = 0;
TCCR1B = _BV(CS10); // no clock prescaling
- TC1H = PWM_MAX >> 8;
- OCR1C = PWM_MAX & 0xFF; // TOP value
- TC1H = PWM_MAX >> 8;
- OCR1D = PWM_MAX & 0xFF;
+ TC1H = PWM_TOP >> 8;
+ OCR1C = PWM_TOP & 0xFF; // TOP value
+
+ TC1H = PWM_TOP >> 8; // PWM3 is inverted
+ OCR1D = PWM_TOP & 0xFF;
TC1H = 0x00;
OCR1B = OCR1A = 0; // initial stride is 0
PORTB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 )); // set to zero
}
+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;
+ TIFR = 0;
+
+ PLLCSR &= ~(_BV(PLLE) | _BV(PCKE));
+}
+
void pwm_off(unsigned char n)
{
- switch (n) {
- case 0: DDRB &= ~_BV(PB1); break;
- case 1: DDRB &= ~_BV(PB3); break;
- case 2: DDRB &= ~_BV(PB5); break;
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ pwm[n] = 0;
+
+ switch (n) {
+ case 0: DDRB &= ~_BV(PB1); break;
+ case 1: DDRB &= ~_BV(PB3); break;
+ case 2: DDRB &= ~_BV(PB5); break;
+ }
}
}
-void pwm_set(unsigned char n, unsigned char stride)
+static void pwm_update_hw(unsigned char n)
{
- TC1H = 0x00;
+ unsigned char hi, lo;
+ uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT;
+
+ if (n == 2)
+ stride = PWM_TOP - stride;
+
+ hi = stride >> 8;
+ lo = stride & 0xFF;
+
switch (n) {
case 0:
- OCR1A = stride;
- DDRB |= _BV(PB1);
+ TC1H = hi;
+ OCR1A = lo;
break;
- case 1: OCR1B = stride;
- DDRB |= _BV(PB3);
+ case 1:
+ TC1H = hi;
+ OCR1B = lo;
break;
- case 2: {
- uint16_t s16 = PWM_MAX - (uint16_t)stride;
- volatile unsigned char hi, lo;
- hi = s16 >> 8;
- lo = s16 & 0xFF;
- TC1H = hi;
- OCR1D = lo;
- DDRB |= _BV(PB5);
- }
+ case 2:
+ TC1H = hi;
+ OCR1D = lo;
break;
}
}
-#if 0
-static void inline pwm_handler()
+void pwm_set(unsigned char n, uint16_t stride)
{
- OCR1A = pwmval[0];
- OCR1B = pwmval[1];
- OCR1D = pwmval[2];
- TIMSK &= ~_BV(TOIE1);
+ if (stride > PWM_MAX)
+ stride = PWM_MAX;
+
+ ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
+ pwm[n] = stride;
+
+ pwm_update_hw(n);
+
+ switch(n) {
+ case 0: DDRB |= _BV(PB1); break;
+ case 1: DDRB |= _BV(PB3); break;
+ case 2: DDRB |= _BV(PB5); break;
+ }
+ }
}
-ISR(TIMER1_OVF_vect)
+void pwm_timer()
{
- pwm_handler();
+ unsigned char i;
+
+ if (++step >= (1 << PWM_STEP_SHIFT))
+ step = 0;
+
+ for (i = 0; i < N_PWMLEDS; i++)
+ if (pwm[i])
+ pwm_update_hw(i);
}
-#endif