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