merge
[aversive.git] / modules / hardware / pwm / pwm.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: pwm.c,v 1.8.4.8 2008-04-06 17:33:57 zer0 Exp $
19  *
20  */
21
22 /* Droids-corp, Eirbot, Microb Technology 2005 - Zer0
23  * Implementation for PWM
24  */
25 /** \file pwm.c
26  *  \brief Implementation for the PWM module.
27  *
28  *  \todo Test the module.
29  *
30  *  \test The modul fonctions and had been tested by Lamygalle.
31  *
32  * This module provides functions for using a pwm 
33  */
34
35 #include <aversive.h>
36
37 #include "pwm.h"
38
39 #include <aversive/parts.h>
40 #include <aversive/timers.h>
41
42 #include <pwm_config.h>
43
44 #include "timers_synch.h"
45
46 #if _PWM_CONFIG_VERSION_ != 2 
47 #warning "You are using an old version of pwm_config.h file"
48 #warning "_PWM_CONFIG_VERSION_ is != 2"
49 #warning "Look in modules/base/pwm/config directory to import changes"
50 #warning "You should now use TIMERx_PRESCALER_DIV_XX from"
51 #warning "include/aversive/parts.h that is arch specific"
52 #endif
53
54
55 #ifdef TIMER1_MODE
56
57 #if (TIMER1_MODE == TIMER_16_MODE_PWM_10)
58 #define TIMER1_PWM_BITS 10
59 #elif (TIMER1_MODE == TIMER_16_MODE_PWM_9)
60 #define TIMER1_PWM_BITS 9
61 #elif (TIMER1_MODE == TIMER_16_MODE_PWM_8)
62 #define TIMER1_PWM_BITS 8
63 #else
64 #error TIMER1 mode not valid, check pwm_config.h
65 #endif
66
67 #endif // TIMER1_MODE
68
69 #ifdef TIMER3_MODE
70
71 #if (TIMER3_MODE == TIMER_16_MODE_PWM_10)
72 #define TIMER3_PWM_BITS 10
73 #elif (TIMER3_MODE == TIMER_16_MODE_PWM_9)
74 #define TIMER3_PWM_BITS 9
75 #elif (TIMER3_MODE == TIMER_16_MODE_PWM_8)
76 #define TIMER3_PWM_BITS 8
77 #else
78 #error TIMER3 mode not valid, check pwm_config.h
79 #endif
80
81 #endif // TIMER3_MODE
82
83 #if (PWM0_MODE & PWM_SIGNED)
84 #define pwm0_init_ddr()                                          \
85       sbi(DDR(PWM0_SIGN_PORT), PWM0_SIGN_BIT);                   \
86       cbi(PWM0_SIGN_PORT, PWM0_SIGN_BIT);
87 #if (PWM0_MODE & PWM_SIGN_INVERTED)
88 #define pwm0_sign_set()   sbi(PWM0_SIGN_PORT, PWM0_SIGN_BIT);
89 #define pwm0_sign_reset() cbi(PWM0_SIGN_PORT, PWM0_SIGN_BIT);
90 #else /* (PWM0_MODE & PWM_SIGN_INVERTED) */
91 #define pwm0_sign_set()   cbi(PWM0_SIGN_PORT, PWM0_SIGN_BIT);
92 #define pwm0_sign_reset() sbi(PWM0_SIGN_PORT, PWM0_SIGN_BIT);
93 #endif /* (PWM0_MODE & PWM_SIGN_INVERTED) */
94 #else /* (PWM0_MODE & PWM_SIGNED) */
95 #define pwm0_init_ddr()
96 #define pwm0_sign_set()
97 #define pwm0_sign_reset()
98 #endif /* (PWM0_MODE & PWM_SIGNED) */
99
100 #if (PWM1A_MODE & PWM_SIGNED)
101 #define pwm1A_init_ddr()                                          \
102       sbi(DDR(PWM1A_SIGN_PORT), PWM1A_SIGN_BIT);                  \
103       cbi(PWM1A_SIGN_PORT, PWM1A_SIGN_BIT);
104 #if (PWM1A_MODE & PWM_SIGN_INVERTED)
105 #define pwm1A_sign_set()   sbi(PWM1A_SIGN_PORT, PWM1A_SIGN_BIT);
106 #define pwm1A_sign_reset() cbi(PWM1A_SIGN_PORT, PWM1A_SIGN_BIT);
107 #else /* (PWM1A_MODE & PWM_SIGN_INVERTED) */
108 #define pwm1A_sign_set()   cbi(PWM1A_SIGN_PORT, PWM1A_SIGN_BIT);
109 #define pwm1A_sign_reset() sbi(PWM1A_SIGN_PORT, PWM1A_SIGN_BIT);
110 #endif /* (PWM1A_MODE & PWM_SIGN_INVERTED) */
111 #else /* (PWM1A_MODE & PWM_SIGNED) */
112 #define pwm1A_init_ddr()
113 #define pwm1A_sign_set()
114 #define pwm1A_sign_reset()
115 #endif /* (PWM1A_MODE & PWM_SIGNED) */
116
117 #if (PWM1B_MODE & PWM_SIGNED)
118 #define pwm1B_init_ddr()                                          \
119       sbi(DDR(PWM1B_SIGN_PORT), PWM1B_SIGN_BIT);                  \
120       cbi(PWM1B_SIGN_PORT, PWM1B_SIGN_BIT);
121 #if (PWM1B_MODE & PWM_SIGN_INVERTED)
122 #define pwm1B_sign_set()   sbi(PWM1B_SIGN_PORT, PWM1B_SIGN_BIT);
123 #define pwm1B_sign_reset() cbi(PWM1B_SIGN_PORT, PWM1B_SIGN_BIT);
124 #else /* (PWM1B_MODE & PWM_SIGN_INVERTED) */
125 #define pwm1B_sign_set()   cbi(PWM1B_SIGN_PORT, PWM1B_SIGN_BIT);
126 #define pwm1B_sign_reset() sbi(PWM1B_SIGN_PORT, PWM1B_SIGN_BIT);
127 #endif /* (PWM1B_MODE & PWM_SIGN_INVERTED) */
128 #else /* (PWM1B_MODE & PWM_SIGNED) */
129 #define pwm1B_init_ddr()
130 #define pwm1B_sign_set()
131 #define pwm1B_sign_reset()
132 #endif /* (PWM1B_MODE & PWM_SIGNED) */
133
134 #if (PWM1C_MODE & PWM_SIGNED)
135 #define pwm1C_init_ddr()                                          \
136       sbi(DDR(PWM1C_SIGN_PORT), PWM1C_SIGN_BIT);                  \
137       cbi(PWM1C_SIGN_PORT, PWM1C_SIGN_BIT);
138 #if (PWM1C_MODE & PWM_SIGN_INVERTED)
139 #define pwm1C_sign_set()   sbi(PWM1C_SIGN_PORT, PWM1C_SIGN_BIT);
140 #define pwm1C_sign_reset() cbi(PWM1C_SIGN_PORT, PWM1C_SIGN_BIT);
141 #else /* (PWM1C_MODE & PWM_SIGN_INVERTED) */
142 #define pwm1C_sign_set()   cbi(PWM1C_SIGN_PORT, PWM1C_SIGN_BIT);
143 #define pwm1C_sign_reset() sbi(PWM1C_SIGN_PORT, PWM1C_SIGN_BIT);
144 #endif /* (PWM1C_MODE & PWM_SIGN_INVERTED) */
145 #else /* (PWM1C_MODE & PWM_SIGNED) */
146 #define pwm1C_init_ddr()
147 #define pwm1C_sign_set()
148 #define pwm1C_sign_reset()
149 #endif /* (PWM1C_MODE & PWM_SIGNED) */
150
151 #if (PWM2_MODE & PWM_SIGNED)
152 #define pwm2_init_ddr()                                          \
153       sbi(DDR(PWM2_SIGN_PORT), PWM2_SIGN_BIT);                   \
154       cbi(PWM2_SIGN_PORT, PWM2_SIGN_BIT);
155 #if (PWM2_MODE & PWM_SIGN_INVERTED)
156 #define pwm2_sign_set()   sbi(PWM2_SIGN_PORT, PWM2_SIGN_BIT);
157 #define pwm2_sign_reset() cbi(PWM2_SIGN_PORT, PWM2_SIGN_BIT);
158 #else /* (PWM2_MODE & PWM_SIGN_INVERTED) */
159 #define pwm2_sign_set()   cbi(PWM2_SIGN_PORT, PWM2_SIGN_BIT);
160 #define pwm2_sign_reset() sbi(PWM2_SIGN_PORT, PWM2_SIGN_BIT);
161 #endif /* (PWM2_MODE & PWM_SIGN_INVERTED) */
162 #else /* (PWM2_MODE & PWM_SIGNED) */
163 #define pwm2_init_ddr()
164 #define pwm2_sign_set()
165 #define pwm2_sign_reset()
166 #endif /* (PWM2_MODE & PWM_SIGNED) */
167
168 #if (PWM3A_MODE & PWM_SIGNED)
169 #define pwm3A_init_ddr()                                          \
170       sbi(DDR(PWM3A_SIGN_PORT), PWM3A_SIGN_BIT);                  \
171       cbi(PWM3A_SIGN_PORT, PWM3A_SIGN_BIT);
172 #if (PWM3A_MODE & PWM_SIGN_INVERTED)
173 #define pwm3A_sign_set()   sbi(PWM3A_SIGN_PORT, PWM3A_SIGN_BIT);
174 #define pwm3A_sign_reset() cbi(PWM3A_SIGN_PORT, PWM3A_SIGN_BIT);
175 #else /* (PWM3A_MODE & PWM_SIGN_INVERTED) */
176 #define pwm3A_sign_set()   cbi(PWM3A_SIGN_PORT, PWM3A_SIGN_BIT);
177 #define pwm3A_sign_reset() sbi(PWM3A_SIGN_PORT, PWM3A_SIGN_BIT);
178 #endif /* (PWM3A_MODE & PWM_SIGN_INVERTED) */
179 #else /* (PWM3A_MODE & PWM_SIGNED) */
180 #define pwm3A_init_ddr()
181 #define pwm3A_sign_set()
182 #define pwm3A_sign_reset()
183 #endif /* (PWM3A_MODE & PWM_SIGNED) */
184
185 #if (PWM3B_MODE & PWM_SIGNED)
186 #define pwm3B_init_ddr()                                          \
187       sbi(DDR(PWM3B_SIGN_PORT), PWM3B_SIGN_BIT);                  \
188       cbi(PWM3B_SIGN_PORT, PWM3B_SIGN_BIT);
189 #if (PWM3B_MODE & PWM_SIGN_INVERTED)
190 #define pwm3B_sign_set()   sbi(PWM3B_SIGN_PORT, PWM3B_SIGN_BIT);
191 #define pwm3B_sign_reset() cbi(PWM3B_SIGN_PORT, PWM3B_SIGN_BIT);
192 #else /* (PWM3B_MODE & PWM_SIGN_INVERTED) */
193 #define pwm3B_sign_set()   cbi(PWM3B_SIGN_PORT, PWM3B_SIGN_BIT);
194 #define pwm3B_sign_reset() sbi(PWM3B_SIGN_PORT, PWM3B_SIGN_BIT);
195 #endif /* (PWM3B_MODE & PWM_SIGN_INVERTED) */
196 #else /* (PWM3B_MODE & PWM_SIGNED) */
197 #define pwm3B_init_ddr()
198 #define pwm3B_sign_set()
199 #define pwm3B_sign_reset()
200 #endif /* (PWM3B_MODE & PWM_SIGNED) */
201
202 #if (PWM3C_MODE & PWM_SIGNED)
203 #define pwm3C_init_ddr()                                          \
204       sbi(DDR(PWM3C_SIGN_PORT), PWM3C_SIGN_BIT);                  \
205       cbi(PWM3C_SIGN_PORT, PWM3C_SIGN_BIT);
206 #if (PWM3C_MODE & PWM_SIGN_INVERTED)
207 #define pwm3C_sign_set()   sbi(PWM3C_SIGN_PORT, PWM3C_SIGN_BIT);
208 #define pwm3C_sign_reset() cbi(PWM3C_SIGN_PORT, PWM3C_SIGN_BIT);
209 #else /* (PWM3C_MODE & PWM_SIGN_INVERTED) */
210 #define pwm3C_sign_set()   cbi(PWM3C_SIGN_PORT, PWM3C_SIGN_BIT);
211 #define pwm3C_sign_reset() sbi(PWM3C_SIGN_PORT, PWM3C_SIGN_BIT);
212 #endif /* (PWM3C_MODE & PWM_SIGN_INVERTED) */
213 #else /* (PWM3C_MODE & PWM_SIGNED) */
214 #define pwm3C_init_ddr()
215 #define pwm3C_sign_set()
216 #define pwm3C_sign_reset()
217 #endif /* (PWM3C_MODE & PWM_SIGNED) */
218
219
220 #define pwm_invert_value(mode, value)                                      \
221 do {                                                                       \
222         if ( mode & PWM_SPECIAL_SIGN_MODE ) {                              \
223                 value = value & PWM_MAX;                                   \
224         }                                                                  \
225         else {                                                             \
226                 value = -value;                                            \
227         }                                                                  \
228 } while(0)
229
230
231 #define pwm_timer_8bits_init(n, mode, prescale, timer_mode)                    \
232 do {                                                                           \
233         TCCR##n  =  ( ( ( timer_mode & 0x01 )?( 1 << WGM##n##0 ):0   )  |      \
234               ( ( timer_mode & 0x02 )?( 1 << WGM##n##1 ):0   )  |              \
235                       ( ( prescale  << CS##n##0  ) ) );                        \
236         if(mode & PWM_REVERSE) {                                               \
237                 TCCR##n = ( ( (TCCR##n) & (~(0x03 << COM##n##0)) ) |           \
238                             (0x01  << COM##n##0) );                            \
239         }                                                                      \
240         else {                                                                 \
241         TCCR##n = ( ( (TCCR##n) & (~(0x03 << COM##n##0)) ) |                   \
242                     (0x02  << COM##n##0) );                                    \
243         }                                                                      \
244         OCR##n = 0x00;                                                         \
245         sbi(OCR##n##_DDR,OCR##n##_BIT);                                        \
246         pwm##n##_init_ddr();                                                   \
247 } while(0)
248      
249 /***********************************************************/
250      
251      
252 #define pwm_timer_16bits_init(n, m, mode, prescale, timer_mode)                \
253 do {                                                                           \
254         TCCR##n##A =   (TCCR##n##A |                                           \
255                         ( ( timer_mode & 0x01 )?( 1 << WGM##n##0 ):0   )  |    \
256                         ( ( timer_mode & 0x02 )?( 1 << WGM##n##1 ):0   )  )  ; \
257                                                                                \
258         TCCR##n##B =  ( ( ( timer_mode & 0x04 )?( 1 << WGM##n##2 ):0   )  |    \
259                         ( ( timer_mode & 0x08 )?( 1 << WGM##n##3 ):0   )  |    \
260                         ( ( prescale  << CS##n##0  )  )  );                    \
261         if(mode & PWM_REVERSE) {                                               \
262                 TCCR##n##A = ( ( (TCCR##n##A) &                                \
263                                  (~(0x03 << COM##n##m##0)) ) |                 \
264                                (0x01  << COM##n##m##0) );                      \
265         }                                                                      \
266         else {                                                                 \
267                 TCCR##n##A = ( ( (TCCR##n##A) &                                \
268                                  (~(0x03 << COM##n##m##0)) ) |                 \
269                                (0x02  << COM##n##m##0) );                      \
270         }                                                                      \
271         OCR##n##m = 0;                                                         \
272         sbi(OCR##n##m##_DDR,OCR##n##m##_BIT);                                  \
273         pwm##n##m##_init_ddr();                                                \
274 } while(0)
275      
276 /***********************************************************/
277      
278
279 #define pwm_timer_8bits_set(n, value, mode)                                    \
280 do {                                                                           \
281         MAX(value, PWM_MAX);                                                   \
282         if ( mode & PWM_SIGNED ) {                                             \
283                 MIN(value, PWM_MIN);                                           \
284                 if (value < 0) {                                               \
285                         pwm##n##_sign_set();                                   \
286                         pwm_invert_value(mode, value);                         \
287                 }                                                              \
288                 else {                                                         \
289                         pwm##n##_sign_reset();                                 \
290                 }                                                              \
291         }                                                                      \
292         else {                                                                 \
293                 MIN(value, 0);                                                 \
294         }                                                                      \
295         OCR##n =(uint8_t) ( value >> ( PWM_SIGNIFICANT_BITS - 8 ));            \
296 } while(0)
297
298 /***********************************************************/
299  
300
301 #define pwm_timer_16bits_set(n, m, value, mode)                                \
302 do {                                                                           \
303         MAX(value, PWM_MAX);                                                   \
304         if ( mode & PWM_SIGNED ) {                                             \
305                 MIN(value, PWM_MIN);                                           \
306                 if (value < 0) {                                               \
307                         pwm##n##m##_sign_set();                                \
308                         pwm_invert_value(mode, value);                         \
309                 }                                                              \
310                 else {                                                         \
311                         pwm##n##m##_sign_reset();                              \
312                 }                                                              \
313         }                                                                      \
314         else {                                                                 \
315                 MIN(value, 0);                                                 \
316         }                                                                      \
317         OCR##n##m = ( value >> ( PWM_SIGNIFICANT_BITS                          \
318                                  -  TIMER##n##_PWM_BITS )) ;                   \
319 } while(0)
320
321
322 /***********************************************************/
323
324 #if (defined PWM0_NUM) && (defined PWM0_ENABLED)
325 void pwm_init_0(void)
326 {
327   uint8_t flags;
328
329   IRQ_LOCK(flags);
330   pwm_timer_8bits_init(0,PWM0_MODE,TIMER0_PRESCALE, TIMER0_MODE);
331   IRQ_UNLOCK(flags);
332 }
333
334 /***********************************************************/
335
336 void pwm_set_0(int16_t value)
337 {
338   uint8_t flags;
339
340   IRQ_LOCK(flags);
341   pwm_timer_8bits_set(0,value,PWM0_MODE);
342   IRQ_UNLOCK(flags);
343 }
344 #endif // (defined PWM0_NUM) && (defined PWM0_ENABLED)
345
346 /***********************************************************/
347
348
349 /***********************************************************/
350
351 #if (defined PWM1A_NUM) && (defined PWM1A_ENABLED)
352 void pwm_init_1A(void)
353 {
354   uint8_t flags;
355
356   IRQ_LOCK(flags);
357   pwm_timer_16bits_init(1,A,PWM1A_MODE,TIMER1_PRESCALE, TIMER1_MODE);
358   IRQ_UNLOCK(flags);
359 }
360
361
362 /***********************************************************/
363
364 void pwm_set_1A(int16_t value)
365 {
366   uint8_t flags;
367
368   IRQ_LOCK(flags);
369   pwm_timer_16bits_set(1,A,value,PWM1A_MODE);
370   IRQ_UNLOCK(flags);
371 }
372 #endif // (defined PWM1A_NUM) && (defined PWM1A_ENABLED)
373
374
375
376 /***********************************************************/
377
378
379 /***********************************************************/
380
381 #if (defined PWM1B_NUM) && (defined PWM1B_ENABLED)
382 void pwm_init_1B(void)
383 {
384   uint8_t flags;
385
386   IRQ_LOCK(flags);
387   pwm_timer_16bits_init(1,B,PWM1B_MODE,TIMER1_PRESCALE, TIMER1_MODE);
388   IRQ_UNLOCK(flags);
389 }
390
391
392 /***********************************************************/
393
394 void pwm_set_1B(int16_t value)
395 {
396   uint8_t flags;
397
398   IRQ_LOCK(flags);
399   pwm_timer_16bits_set(1,B,value,PWM1B_MODE);
400   IRQ_UNLOCK(flags);
401 }
402 #endif // (defined PWM1B_NUM) && (defined PWM1B_ENABLED)
403
404
405
406 /***********************************************************/
407
408
409 /***********************************************************/
410
411 #if (defined PWM1C_NUM) && (defined PWM1C_ENABLED)
412 void pwm_init_1C(void)
413 {
414   uint8_t flags;
415
416   IRQ_LOCK(flags);
417   pwm_timer_16bits_init(1,C,PWM1C_MODE,TIMER1_PRESCALE, TIMER1_MODE);
418   IRQ_UNLOCK(flags);
419 }
420
421
422 /***********************************************************/
423
424 void pwm_set_1C(int16_t value)
425 {
426   uint8_t flags;
427
428   IRQ_LOCK(flags);
429   pwm_timer_16bits_set(1,C,value, PWM1C_MODE);
430   IRQ_UNLOCK(flags);
431 }
432 #endif // (defined PWM1C_NUM) && (defined PWM1C_ENABLED)
433
434
435
436 /***********************************************************/
437
438
439 /***********************************************************/
440
441 #if (defined PWM2_NUM) && (defined PWM2_ENABLED)
442 void pwm_init_2(void)
443 {
444   uint8_t flags;
445
446   IRQ_LOCK(flags);
447   pwm_timer_8bits_init(2,PWM2_MODE,TIMER2_PRESCALE, TIMER2_MODE);
448   IRQ_UNLOCK(flags);
449 }
450
451
452 /***********************************************************/
453
454 void pwm_set_2(int16_t value)
455 {
456   uint8_t flags;
457
458   IRQ_LOCK(flags);
459   pwm_timer_8bits_set(2,value,PWM2_MODE);
460   IRQ_UNLOCK(flags);
461 }
462 #endif // (defined PWM2_NUM) && (defined PWM2_ENABLED)
463
464
465
466 /***********************************************************/
467
468
469 /***********************************************************/
470
471 #if (defined PWM3A_NUM) && (defined PWM3A_ENABLED)
472 void pwm_init_3A(void)
473 {
474   uint8_t flags;
475
476   IRQ_LOCK(flags);
477   pwm_timer_16bits_init(3,A,PWM3A_MODE,TIMER3_PRESCALE, TIMER3_MODE);
478   IRQ_UNLOCK(flags);
479 }
480
481
482 /***********************************************************/
483
484 void pwm_set_3A(int16_t value)
485 {
486   uint8_t flags;
487
488   IRQ_LOCK(flags);
489   pwm_timer_16bits_set(3,A,value,PWM3A_MODE);
490   IRQ_UNLOCK(flags);
491 }
492 #endif // (defined PWM3A_NUM) && (defined PWM3A_ENABLED)
493
494
495
496 /***********************************************************/
497
498
499 /***********************************************************/
500
501 #if (defined PWM3B_NUM) && (defined PWM3B_ENABLED)
502 void pwm_init_3B(void)
503 {
504   uint8_t flags;
505
506   IRQ_LOCK(flags);
507   pwm_timer_16bits_init(3,B,PWM3B_MODE,TIMER3_PRESCALE, TIMER3_MODE);
508   IRQ_UNLOCK(flags);
509 }
510
511
512 /***********************************************************/
513
514 void pwm_set_3B(int16_t value)
515 {
516   uint8_t flags;
517
518   IRQ_LOCK(flags);
519   pwm_timer_16bits_set(3,B,value,PWM3B_MODE);
520   IRQ_UNLOCK(flags);
521 }
522 #endif // (defined PWM3B_NUM) && (defined PWM3B_ENABLED)
523
524
525
526 /***********************************************************/
527
528
529 /***********************************************************/
530
531 #if (defined PWM3C_NUM) && (defined PWM3C_ENABLED)
532 void pwm_init_3C(void)
533 {
534   uint8_t flags;
535
536   IRQ_LOCK(flags);
537   pwm_timer_16bits_init(3,C,PWM3C_MODE,TIMER3_PRESCALE, TIMER3_MODE);
538   IRQ_UNLOCK(flags);
539 }
540
541
542 /***********************************************************/
543
544 void pwm_set_3C(int16_t value)
545 {
546   uint8_t flags;
547
548   IRQ_LOCK(flags);
549   pwm_timer_16bits_set(3,C,value,PWM3C_MODE);
550   IRQ_UNLOCK(flags);
551 }
552 #endif // (defined PWM3C_NUM) && (defined PWM3C_ENABLED)
553
554
555 /***********************************************************/
556
557
558 /***********************************************************/
559
560 // global init
561 void pwm_init(void)
562 {
563 #if (defined PWM0_NUM) && (defined PWM0_ENABLED)
564   pwm_init_0();
565 #endif
566
567 #if (defined PWM1A_NUM) && (defined PWM1A_ENABLED)
568   pwm_init_1A();
569 #endif
570 #if (defined PWM1B_NUM) && (defined PWM1B_ENABLED)
571   pwm_init_1B();
572 #endif
573 #if (defined PWM1C_NUM) && (defined PWM1C_ENABLED)
574   pwm_init_1C();
575 #endif
576
577 #if (defined PWM2_NUM) && (defined PWM2_ENABLED)
578   pwm_init_2();
579 #endif
580
581 #if (defined PWM3A_NUM) && (defined PWM3A_ENABLED)
582   pwm_init_3A();
583 #endif
584 #if (defined PWM3B_NUM) && (defined PWM3B_ENABLED)
585   pwm_init_3B();
586 #endif
587 #if (defined PWM3C_NUM) && (defined PWM3C_ENABLED)
588   pwm_init_3C();
589 #endif
590
591
592 #if ( (defined TIMER0_SYNCH) || (defined TIMER1_SYNCH) || (defined TIMER2_SYNCH) || (defined TIMER3_SYNCH) )
593   pwm_synch();
594 #endif // ( (defined TIMER0_SYNCH) || (defined TIMER1_SYNCH) || (defined TIMER2_SYNCH) || (defined TIMER3_SYNCH) )
595
596
597 }
598 /***********************************************************/
599
600
601 /***********************************************************/
602
603 /* global SET FUNCTION : we use a (void *) to be compliant with
604  * control_system functions. For instance pwm_set((void *)(PWM1B_NUM), x) 
605  * is equivalent to pwm_set_1B(x) */
606 void pwm_set(void * data, int32_t value)
607 {
608   uint8_t num = (uint8_t)(int)data;
609   S_MAX(value, 0x7FFF);
610
611   switch(num)
612     {
613 #if (defined PWM0_NUM) && (defined PWM0_ENABLED)
614     case PWM0_NUM:
615       pwm_set_0(value);
616       break;
617 #endif
618
619 #if (defined PWM1A_NUM) && (defined PWM1A_ENABLED)
620     case PWM1A_NUM:
621       pwm_set_1A(value);
622       break;
623 #endif
624 #if (defined PWM1B_NUM) && (defined PWM1B_ENABLED)
625     case PWM1B_NUM:
626       pwm_set_1B(value);
627       break;
628 #endif
629 #if (defined PWM1C_NUM) && (defined PWM1C_ENABLED)
630     case PWM1C_NUM:
631       pwm_set_1C(value);
632       break;
633 #endif
634
635 #if (defined PWM2_NUM) && (defined PWM2_ENABLED)
636     case PWM2_NUM:
637       pwm_set_2(value);
638       break;
639 #endif
640
641 #if (defined PWM3A_NUM) && (defined PWM3A_ENABLED)
642     case PWM3A_NUM:
643       pwm_set_3A(value);
644       break;
645 #endif
646 #if (defined PWM3B_NUM) && (defined PWM3B_ENABLED)
647     case PWM3B_NUM:
648       pwm_set_3B(value);
649       break;
650 #endif
651 #if (defined PWM3C_NUM) && (defined PWM3C_ENABLED)
652     case PWM3C_NUM:
653       pwm_set_3C(value);
654       break;
655 #endif
656
657     default:
658       break;
659     }
660
661 }
662
663
664