Refactor to use new protocol module and sipo.
[bertos.git] / bertos / drv / pwm.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  * \defgroup pwm_driver PWM driver
32  * \ingroup drivers
33  * \{
34  * \brief Pulse Width Modulation (PWM) driver.
35  *
36  * \author Francesco Sacchi <batt@develer.com>
37  * \author Daniele Basile <asterix@develer.com>
38  *
39  * $WIZ$ module_name = "pwm"
40  * $WIZ$ module_configuration = "bertos/cfg/cfg_pwm.h"
41  * $WIZ$ module_hw = "bertos/hw/pwm_map.h"
42  * $WIZ$ module_supports = "not avr and not cm3"
43  */
44
45 #ifndef DRV_PWM_H
46 #define DRV_PWM_H
47
48 #include <cpu/attr.h>
49
50 #include <cfg/compiler.h>
51 #include "cfg/cfg_pwm.h"
52
53 /**
54  * Maximum PWM duty cycle value (100%)
55  */
56 #define PWM_MAX_DUTY              ((pwm_duty_t)0xFFFF)
57 #define PWM_MAX_PERIOD                         0xFFFF
58 #define PWM_MAX_PERIOD_LOG2                        16
59
60 /**
61  * Type for PWM duty cycle.
62  * The value is represented as a 16bit unsigned integer, so it ranges from 0 (0%)
63  * to PWM_MAX_DUTY (0xFFFF = 100%).
64  */
65 typedef uint16_t pwm_duty_t;
66
67 /**
68  * Type for PWM frequency.
69  * Unit of measure is Hz.
70  */
71 typedef uint32_t pwm_freq_t;
72
73 #if !CFG_PWM_ENABLE_OLD_API || defined(__doxygen__)
74         /**
75          * \defgroup pwm_api PWM API
76          * With this driver you can control a device with multiple PWM channels.
77          * You can enable/disable each channel indipendently and also set frequency
78          * and duty cycle.
79          *
80          * API usage example:
81          * \code
82          * Pwm pwm; // declare a context structure
83          * pwm_init(&pwm, 0); // init pwm channel 0
84          * pwm_setFrequency(&pwm, 1000); // Set frequency of channel 0 to 1000Hz
85          * pwm_setDuty(&pwm, 0x7FFF); // Set duty to 50% (0xFFFF/2)
86          * pwm_enable(&pwm, true); // Activate the output
87          * \endcode
88          * \{
89          */
90
91         /**
92          * Enum describing PWM polarities.
93          */
94         typedef enum PwmPolarity
95         {
96                 /** High pulse: increasing duty increases the part of the signal at high level. */
97                 PWM_POL_HIGH_PULSE,
98                 /** Positive pulse: same as High pulse. */
99                 PWM_POL_POSITIVE = PWM_POL_HIGH_PULSE,
100                 /** Low pulse: increasing duty increases the part of the signal at low level. */
101                 PWM_POL_LOW_PULSE,
102                 /** Negative pulse: same as Low pulse. */
103                 PWM_POL_NEGATIVE = PWM_POL_LOW_PULSE,
104         } PwmPolarity;
105
106         struct PwmHardware; //Fwd declaration
107
108         /**
109          * PWM context structure.
110          */
111         typedef struct Pwm
112         {
113                 unsigned ch;
114                 pwm_duty_t duty;
115                 PwmPolarity pol;
116                 bool enabled;
117                 struct PwmHardware *hw;
118         } Pwm;
119
120         void pwm_setDuty(Pwm *ctx, pwm_duty_t duty);
121         void pwm_setFrequency(Pwm *ctx, pwm_freq_t freq);
122         void pwm_setPolarity(Pwm *ctx, PwmPolarity pol);
123         void pwm_enable(Pwm *ctx, bool state);
124         void pwm_init(Pwm *ctx, unsigned channel);
125         /** \} */ //defgroup pwm_api
126 #endif
127
128
129 #if CFG_PWM_ENABLE_OLD_API
130         /**
131          * \defgroup pwm_old_api Old PWM API
132          * This API has strong limititations, so it has been deprecated.
133          * It is active by default for backward compatibility reasons, but
134          * for new projects please use the new PWM API.
135          * In order to disable this API, check CFG_PWM_ENABLE_OLD_API.
136          * \see pwm_api
137          * \see CFG_PWM_ENABLE_OLD_API
138          * \{
139          */
140
141         #include CPU_HEADER(pwm)
142         #include "hw/pwm_map.h"
143
144         /**
145          * Set PWM polarity of pwm \p dev.
146          * \param dev PWM channel.
147          * \param pol if false positive polarity pulses are generated,
148          *            if true negative polarity pulses are generated.
149          * \warning This function has to be called with PWM disabled, otherwise
150          *          the output value will be undefined.
151          */
152         INLINE void pwm_setPolarity(PwmDev dev, bool pol)
153         {
154                 pwm_hw_setPolarity(dev, pol);
155         }
156
157         void pwm_setDuty(PwmDev dev, pwm_duty_t duty);
158         void pwm_setFrequency(PwmDev dev, pwm_freq_t freq);
159         void pwm_enable(PwmDev dev, bool state);
160         void pwm_init(void);
161         /** \} */ //defgroup pwm_old_api
162 #endif
163
164 /*
165  * Test function prototypes.
166  *
167  * See pwm_test.c for implemntation of these functions.
168  */
169 void pwm_testRun(void);
170 int pwm_testSetup(void);
171 /* For backward compatibility */
172 #define pwm_testSetUp() pwm_testSetup()
173 int pwm_testTearDown(void);
174
175
176 /** \} */ //defgroup pwm_driver
177
178 #endif /* DRV_PWM_H */