]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/pwm.c
pwm.c - remove unused code
[bike-lights.git] / firmware / pwm.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <util/delay.h>
4
5 #include "lights.h"
6
7 static uint16_t pwm[N_PWMLEDS];
8 static volatile unsigned char step;
9
10 static void enable_pll()
11 {
12         /* Async clock */
13         PLLCSR = _BV(PLLE);
14
15         /* Synchronize to the phase lock */
16         _delay_us(100);
17         while ((PLLCSR & _BV(PLOCK)) == 0)
18                 ;
19         PLLCSR |= _BV(PCKE);
20 }
21
22 void init_pwm()
23 {
24         int i;
25
26         step = 0;
27
28         for (i = 0; i < N_PWMLEDS; i++)
29                 pwm[i] = 0;
30
31         enable_pll();
32
33         // PWM channel D is inverted, ...
34         TCCR1C = _BV(COM1D1) | _BV(COM1D0) | _BV(PWM1D);
35         // PWM channels A and B are not
36         TCCR1A = _BV(COM1A1) | _BV(COM1B1) | _BV(PWM1A) | _BV(PWM1B);
37         TCCR1D = 0;
38         TCCR1B = _BV(CS10);                     // no clock prescaling
39
40         TC1H = PWM_MAX >> 8;
41         OCR1C = PWM_MAX & 0xFF;                         // TOP value
42
43         TC1H = PWM_MAX >> 8;            // PWM3 is inverted
44         OCR1D = PWM_MAX & 0xFF;
45
46         TC1H = 0x00;
47         OCR1B = OCR1A = 0;              // initial stride is 0
48
49         DDRB  &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 )); // tristate it
50         PORTB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 )); // set to zero
51 }
52
53 void susp_pwm()
54 {
55         unsigned char i;
56
57         for (i = 0; i < N_PWMLEDS; i++)
58                 pwm[i] = 0;
59
60         DDRB &= ~(_BV( PB1 ) | _BV( PB3 ) | _BV( PB5 ));
61         TCCR1D = TCCR1C = TCCR1B = TCCR1A = 0;
62         TIMSK = 0;
63         TIFR = 0;
64
65         PLLCSR &= ~(_BV(PLLE) | _BV(PCKE));
66 }
67
68 void pwm_off(unsigned char n)
69 {
70         pwm[n] = 0;
71
72         switch (n) {
73         case 0: DDRB &= ~_BV(PB1); break;
74         case 1: DDRB &= ~_BV(PB3); break;
75         case 2: DDRB &= ~_BV(PB5); break;
76         }
77 }
78
79 static void pwm_update_hw(unsigned char n)
80 {
81         unsigned char hi, lo;
82         uint16_t stride = (pwm[n] + step) >> PWM_STEP_SHIFT;
83
84         if (n == 2)
85                 stride = PWM_MAX - stride;
86
87         hi = stride >> 8;
88         lo = stride & 0xFF;
89
90         switch (n) {
91         case 0:
92                 TC1H = hi;
93                 OCR1A = lo;
94                 DDRB |= _BV(PB1);
95                 break;
96         case 1:
97                 TC1H = hi;
98                 OCR1B = lo;
99                 DDRB |= _BV(PB3);
100                 break;
101         case 2:
102                 TC1H = hi;
103                 OCR1D = lo;
104                 DDRB |= _BV(PB5);
105                 break;
106         }
107 }
108
109 void pwm_set(unsigned char n, uint16_t stride)
110 {
111         if (((stride + (1 << PWM_STEP_SHIFT)) >> PWM_STEP_SHIFT) >= PWM_MAX)
112                 stride = PWM_MAX << PWM_STEP_SHIFT;
113
114         pwm[n] = stride;
115         pwm_update_hw(n);
116 }
117
118 void pwm_timer()
119 {
120         unsigned char i;
121
122         if (++step >= (1 << PWM_STEP_SHIFT))
123                 step = 0;
124
125         for (i = 0; i < N_PWMLEDS; i++)
126                 if (pwm[i])
127                         pwm_update_hw(i);
128 }
129