config: update test config files to current aversive config.in
[aversive.git] / modules / hardware / pwm / timers_synch.c
1 /*  
2  *  Copyright Droids Corporation, Microb Technology, Eirbot (2005)
3  * 
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 2 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  *
18  *  Revision : $Id: timers_synch.c,v 1.4.4.2 2006-11-30 22:00:31 zer0 Exp $
19  *
20  */
21
22
23 /** \file timers_synch.c
24  *  \brief this code synchronizes the timers in order to optain synchronous PWMs
25  *
26  *  \todo Test this function on various uC
27  *  \todo modify if there exists other prescaler configurations
28  *
29  *  \test This feature is not 100% shure for the moment, but has been tested on M32 and M128
30  *
31  *
32  * this code synchronizes the timers in order to optain synchronous PWMs,
33  * such a feature can be used for driving 3-phase motors with PWMS from different timers
34  * 
35  * 
36  * to synch PWMs you need to enshure that the timers have same prescales,
37  * and the same PWM mode
38  * 
39  * There is one little side effect : if there are common prescalers in your controller they will all be reset
40  */
41
42
43   
44 #include <avr/io.h>
45 #include <pwm.h> // for config
46
47 #include <timers_synch.h>
48
49
50
51 #if ( (defined TIMER0_SYNCH) || (defined TIMER1_SYNCH) || (defined TIMER2_SYNCH) || (defined TIMER3_SYNCH) )
52
53
54 void pwm_synch(void)
55 {
56         uint8_t flags;
57
58 // computations of prescale for sync
59 #ifdef TIMER0_SYNCH
60   #undef PWM_SYNCH_PRESCALE
61   #define PWM_SYNCH_PRESCALE TIMER0_PRESCALE
62 #endif
63 #ifdef TIMER1_SYNCH
64   #undef PWM_SYNCH_PRESCALE
65   #define PWM_SYNCH_PRESCALE TIMER1_PRESCALE
66 #endif
67 #ifdef TIMER2_SYNCH
68   #undef PWM_SYNCH_PRESCALE
69   #define PWM_SYNCH_PRESCALE TIMER2_PRESCALE
70 #endif
71 #ifdef TIMER3_SYNCH
72   #undef PWM_SYNCH_PRESCALE
73   #define PWM_SYNCH_PRESCALE TIMER3_PRESCALE
74 #endif
75
76 // verify that all the prescales are the same
77
78 #ifdef TIMER0_SYNCH
79   #if (PWM_SYNCH_PRESCALE  != TIMER0_PRESCALE)
80     #error TIMER0: not the same prescaler for synchronized PWMs, please verify your pwm_config.h
81   #endif
82 #endif
83 #ifdef TIMER1_SYNCH
84   #if (PWM_SYNCH_PRESCALE  != TIMER1_PRESCALE)
85     #error TIMER1: not the same prescaler for synchronized PWMs, please verify your pwm_config.h
86   #endif
87 #endif
88 #ifdef TIMER2_SYNCH
89   #if (PWM_SYNCH_PRESCALE  != TIMER2_PRESCALE)
90     #error TIMER2: not the same prescaler for synchronized PWMs, please verify your pwm_config.h
91   #endif
92 #endif
93 #ifdef TIMER3_SYNCH
94   #if (PWM_SYNCH_PRESCALE  != TIMER3_PRESCALE)
95     #error TIMER3: not the same prescaler for synchronized PWMs, please verify your pwm_config.h
96   #endif
97 #endif
98
99
100
101 // step between timer activations
102
103 #if (PWM_SYNCH_PRESCALE  == TIMER_8_PRESCALE_1)
104   #define STEP 1
105 #else
106   #define STEP 0
107 #endif
108
109
110   
111   IRQ_LOCK(flags);
112   
113   {
114   //***************************************************
115   // register declarations
116   //***************************************************
117   
118 #ifdef TIMER0_SYNCH
119   register uint8_t tccr0;
120 #endif
121 #ifdef TIMER1_SYNCH
122   register uint8_t tccr1b;
123 #endif
124 #ifdef TIMER2_SYNCH
125   register uint8_t tccr2;
126 #endif
127 #ifdef TIMER3_SYNCH
128   register uint8_t tccr3b;
129 #endif
130
131   register uint8_t sfior;
132
133   //***************************************************
134   // save state and stop timers
135   //***************************************************
136   
137 #ifdef TIMER0_SYNCH
138   tccr0 = TCCR0;
139   TCCR0 = 0x00;
140 #endif
141 #ifdef TIMER1_SYNCH
142  tccr1b = TCCR1B;
143  TCCR1B = 0x00;
144 #endif
145 #ifdef TIMER2_SYNCH
146   tccr2 = TCCR2;
147   TCCR2 = 0x00;
148 #endif
149 #ifdef TIMER3_SYNCH
150   tccr3b = TCCR3B;
151   TCCR3B = 0x00;
152 #endif
153
154   //***************************************************
155   // the timers are now stopped, we set the TCNT's to given values
156   // set timers to given values
157   // the timers would then be startes one after another,
158   // and synchronize themselves while starting with the right offset
159   //***************************************************
160
161 #ifdef TIMER0_SYNCH
162   TCNT0 = 0;
163 #endif
164 #ifdef TIMER1_SYNCH
165   TCNT1H = 0;
166   TCNT1L = 1l * STEP;
167 #endif
168 #ifdef TIMER2_SYNCH
169   TCNT2 = 2l * STEP;
170 #endif
171 #ifdef TIMER3_SYNCH
172   TCNT3H = 0;
173   TCNT3L = 4l * STEP; // one more, cause TCCR acess takes one instruction more (see TCCR3B acess, down)
174 #endif
175
176   //***************************************************
177   // resetting prescalers (some could influence unwanted timers, enshure that there are no problems with that )
178   // this is done this manner to be absolutely synchronous
179   //***************************************************
180
181   sfior = SFIOR;
182
183   // mega 128
184 #if (defined PSR321) && ( (defined TIMER3_SYNCH) || (defined TIMER2_SYNCH)  || (defined TIMER1_SYNCH) )
185   sfior |= (1<<PSR321);
186 #endif
187 #if (defined PSR0) && (defined TIMER0_SYNCH)
188   sfior |= (1<<PSR0);
189 #endif
190   // mega  163, 32 ...
191 #if (defined PSR10) && ( (defined TIMER1_SYNCH) || (defined TIMER0_SYNCH))
192   sfior |= (1<<PSR10);
193 #endif
194 #if (defined PSR2) && (defined TIMER2_SYNCH)
195   sfior |= (1<<PSR2);
196 #endif
197
198   //////////////////////////////////////////////////
199   // here begins the time critical  section
200   // this all is done within less than 8 cycles, to assure function even with a prescaler set to 8
201   //////////////////////////////////////////////////
202   
203   
204   SFIOR = sfior;
205   
206   
207   //***************************************************
208   // running timers
209   //***************************************************
210   
211 #ifdef TIMER0_SYNCH
212   TCCR0 = tccr0;
213 #else
214   nop();
215 #endif
216 #ifdef TIMER1_SYNCH
217   TCCR1B = tccr1b;
218 #else
219   nop();
220 #endif
221 #ifdef TIMER2_SYNCH
222   TCCR2 = tccr2;
223 #else
224   nop();
225 #endif
226 #ifdef TIMER3_SYNCH
227   TCCR3B = tccr3b; // this instruction takes 2 cycles (on ATMEGA128)
228 #else
229   /* nop(); // optimized, not necessary, coz nothing after
230   nop(); */
231 #endif
232
233   //////////////////////////////////////////////////
234   // here ends the time critical  section
235   // this all is done within less than 8 cycles, to assure function even with a prescaler set to 8
236   //////////////////////////////////////////////////
237
238
239
240
241   }
242   IRQ_UNLOCK(flags);
243
244
245 }
246
247 #endif // ( (defined TIMER0_SYNCH) || (defined TIMER1_SYNCH) || (defined TIMER2_SYNCH) || (defined TIMER3_SYNCH) )