4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
31 * \defgroup pwm_driver PWM driver
34 * \brief Pulse Width Modulation (PWM) driver.
36 * \author Francesco Sacchi <batt@develer.com>
37 * \author Daniele Basile <asterix@develer.com>
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"
50 #include <cfg/compiler.h>
51 #include "cfg/cfg_pwm.h"
54 * Maximum PWM duty cycle value (100%)
56 #define PWM_MAX_DUTY ((pwm_duty_t)0xFFFF)
57 #define PWM_MAX_PERIOD 0xFFFF
58 #define PWM_MAX_PERIOD_LOG2 16
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%).
65 typedef uint16_t pwm_duty_t;
68 * Type for PWM frequency.
69 * Unit of measure is Hz.
71 typedef uint32_t pwm_freq_t;
73 #if !CFG_PWM_ENABLE_OLD_API || defined(__doxygen__)
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
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
92 * Enum describing PWM polarities.
94 typedef enum PwmPolarity
96 /** High pulse: increasing duty increases the part of the signal at high level. */
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. */
102 /** Negative pulse: same as Low pulse. */
103 PWM_POL_NEGATIVE = PWM_POL_LOW_PULSE,
106 struct PwmHardware; //Fwd declaration
109 * PWM context structure.
117 struct PwmHardware *hw;
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
129 #if CFG_PWM_ENABLE_OLD_API
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.
137 * \see CFG_PWM_ENABLE_OLD_API
141 #include CPU_HEADER(pwm)
142 #include "hw/pwm_map.h"
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.
152 INLINE void pwm_setPolarity(PwmDev dev, bool pol)
154 pwm_hw_setPolarity(dev, pol);
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);
161 /** \} */ //defgroup pwm_old_api
165 * Test function prototypes.
167 * See pwm_test.c for implemntation of these functions.
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);
176 /** \} */ //defgroup pwm_driver
178 #endif /* DRV_PWM_H */