1 /**
2  * \file
3  *
4  * \brief SAM PWM Driver for SAMB11
5  *
6  * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #ifndef PWM_H_INCLUDED
47 #define PWM_H_INCLUDED
48 
49 /**
50  * \defgroup asfdoc_samb_pwm_group SAM Pulse Width Modulation Driver (PWM)
51  *
52  * This driver for Atmel&reg; | SMART SAM devices provides an interface for the
53  * configuration and management of the device's Pulse Width Modulation Module
54  * (PWM) functionality for waveform generation operations.
55  *
56  * The following peripherals are used by this module:
57  *  - PWM (Pulse Width Modulation)
58  *
59  * The following devices can use this module:
60  *  - Atmel | SMART SAM B11
61  *
62  * The outline of this documentation is as follows:
63  *  - \ref asfdoc_samb_pwm_prerequisites
64  *  - \ref asfdoc_samb_pwm_module_overview
65  *  - \ref asfdoc_samb_pwm_special_considerations
66  *  - \ref asfdoc_samb_pwm_extra_info
67  *  - \ref asfdoc_samb_pwm_examples
68  *  - \ref asfdoc_samb_pwm_api_overview
69  *
70  *
71  * \section asfdoc_samb_pwm_prerequisites Prerequisites
72  *
73  * There are no prerequisites for this module.
74  *
75  *
76  * \section asfdoc_samb_pwm_module_overview Module Overview
77  *
78  * PWM is a flexible fully configurable module which has the following features:
79  *  - 4 independent configurable pulse width modulator (PWM) blocks.
80  *  - It can be connected to an external on board low pass filter to produce
81  *    different analog control voltages depending on the configured duty cycle.
82  *  - The base frequency of PWM block is the XO clock (26MHz) or the RC oscillator.
83  *  - Different duty cycles could be obtained
84  *  - The frequency of the PWM pulse is programmable in steps according
85  *    to the following relation:
86  * \f[
87  *    f_{PWM} = \frac{f_{PWM_{base}}} {64 \times 2^i}
88  * \f]
89  * \f[
90  *   i = 0 - 8
91  * \f]
92  *
93  * f<SUB>PWM</SUB><SUB>base</SUB> can be selected to have different values
94  * according to the following table. Minimum and maximum fre-quencies supported
95  * for each clock selection is listed in the table as well.
96  *
97  * <table>
98  *  <caption>Frequency support</caption>
99  *  <tr>
100  *    <th>f<SUB>PWM</SUB><SUB>base</SUB></th>
101  *    <th>Maximum frequency</th>
102  *    <th>Minimum frequency</th>
103  *  </tr>
104  *  <tr>
105  *    <td>26 MHz</td>
106  *    <td>406.25 KHz</td>
107  *    <td>1.586 KHz</td>
108  *  </tr>
109  *  <tr>
110  *    <td>13 MHz</td>
111  *    <td>203.125 KHz</td>
112  *    <td>793.45 Hz</td>
113  *  </tr>
114   *  <tr>
115  *    <td>6.5 MHz</td>
116  *    <td>101.562 KHz</td>
117  *    <td>396.72 Hz</td>
118  *  </tr>
119   *  <tr>
120  *    <td>3.25 MHz</td>
121  *    <td>50.781 KHz</td>
122  *    <td>198.36 Hz</td>
123  *  </tr>
124  * </table>
125  *
126  * \section asfdoc_samb_pwm_special_considerations Special Considerations
127  *
128  * There are no special considerations for this module.
129  *
130  * \section asfdoc_samb_pwm_extra_info Extra Information
131  *
132  * For extra information, see \ref asfdoc_samb_pwm_extra. This includes:
133  *  - \ref asfdoc_samb_pwm_extra_acronyms
134  *  - \ref asfdoc_samb_pwm_extra_dependencies
135  *  - \ref asfdoc_samb_pwm_extra_errata
136  *  - \ref asfdoc_samb_pwm_extra_history
137  *
138  *
139  * \section asfdoc_samb_pwm_examples Examples
140  *
141  * For a list of examples related to this driver, see
142  * \ref asfdoc_samb_pwm_exqsg.
143  *
144  *
145  * \section asfdoc_samb_pwm_api_overview API Overview
146  * @{
147  */
148 
149 #include <compiler.h>
150 #include "gpio.h"
151 
152 #ifdef __cplusplus
153 extern "C" {
154 #endif
155 
156 /**
157  *  \brief PWM device configuration enum.
158  *
159  *  Enum for the possible settings of pwm device configuration.
160  */
161 enum pwm_device_select {
162 	/** PWM module 0 */
163 	PWM0 = 0,
164 	/** PWM module 1 */
165 	PWM1,
166 	/** PWM module 2 */
167 	PWM2,
168 	/** PWM module 3 */
169 	PWM3,
170 };
171 
172 /**
173  *  \brief PWM sample method configuration enum.
174  *
175  *  Enum for the possible settings of pwm sample method configuration.
176  */
177 enum pwm_sample_method {
178 	/** Samples agcdata at >= 1024 cycles and does not lose precision */
179 	PWM_SAMPLE_METHOD_0 = 0,
180 	/** Samples at PWM period but will lose LSBs if less than 1024 */
181 	PWM_SAMPLE_METHOD_1,
182 };
183 
184 /**
185  *  \brief PWM period configuration enum.
186  *
187  *  Enum for the possible settings of pwm period configuration.
188  */
189 enum pwm_period {
190 	/** PWM perid 0 */
191 	PWM_PERIOD_0 = 0,
192 	/** PWM perid 1 */
193 	PWM_PERIOD_1,
194 	/** PWM perid 2 */
195 	PWM_PERIOD_2,
196 	/** PWM perid 3 */
197 	PWM_PERIOD_3,
198 	/** PWM perid 4 */
199 	PWM_PERIOD_4,
200 	/** PWM perid 5 */
201 	PWM_PERIOD_5,
202 	/** PWM perid 6 */
203 	PWM_PERIOD_6,
204 	/** PWM perid 7 */
205 	PWM_PERIOD_7,
206 	/** PWM perid 8 */
207 	PWM_PERIOD_8,
208 };
209 
210 /**
211  *  \brief PWM clock select configuration enum.
212  *
213  *  Enum for the possible settings of pwm clock select configuration.
214  */
215 enum pwm_clock_select {
216 	/** Clock 26MHz */
217 	PWM_CLOCK_SELECT_26_0 = 0,
218 	/** Clock 13MHz */
219 	PWM_CLOCK_SELECT_13_0,
220 	/** Clock 6.5MHz */
221 	PWM_CLOCK_SELECT_6_5,
222 	/** Clock 3.25MHz */
223 	PWM_CLOCK_SELECT_3_25,
224 };
225 
226 /**
227  *  \brief PWM configuration structure.
228  *
229  *  Configuration structure for a PWM instance. This structure should be
230  *  initialized by the \ref pwm_get_config_defaults() function before being
231  *  modified by the user application.
232  */
233 struct pwm_config {
234 	/** 1 to inverse the polarity */
235 	bool output_polarity;
236 	/** AGC data format */
237 	bool agcdata_format;
238 	/** Sample method */
239 	enum pwm_sample_method sample_method;
240 	/** Programmable PWM update period */
241 	enum pwm_period period;
242 	/** This value specifies the duty cycle(%) */
243 	uint8_t duty_cycle;
244 	/** PWM Source Clock Frequency Select */
245 	enum pwm_clock_select clock_select;
246 	/** PWM PAD pin number */
247 	uint32_t pin_number_pad;
248 	/** PWM PAD pinmux selection */
249 	uint32_t pinmux_sel_pad;
250 };
251 
252 /** \name Configuration and initialization
253  * @{
254  */
255 
256 void pwm_get_config_defaults(struct pwm_config *const config);
257 void pwm_set_duty_cycle(enum pwm_device_select device_select, \
258 					uint8_t duty_cycle);
259 void pwm_set_period(enum pwm_device_select device_select, \
260 					enum pwm_period period);
261 enum status_code pwm_init(enum pwm_device_select device_select, \
262 					const struct pwm_config *const config);
263 
264 /** @} */
265 
266 /**
267  * \name Enable/Disable and Reset
268  * @{
269  */
270 
271 void pwm_enable(enum pwm_device_select device_select);
272 void pwm_disable(enum pwm_device_select device_select);
273 void pwm_reset(enum pwm_device_select device_select);
274 
275 /** @} */
276 
277 /** @}*/
278 
279 #ifdef __cplusplus
280 }
281 #endif
282 
283 
284 /**
285  * \page asfdoc_samb_pwm_extra Extra Information for PWM Driver
286  *
287  * \section asfdoc_samb_pwm_extra_acronyms Acronyms
288  * Below is a table listing the acronyms used in this module, along with their
289  * intended meanings.
290  *
291  * <table>
292  *	<tr>
293  *		<th>Acronym</th>
294  *		<th>Description</th>
295  *	</tr>
296  *	<tr>
297  *		<td>PWM</td>
298  *		<td>Pulse Width Modulation</td>
299  *	</tr>
300  * </table>
301  *
302  *
303  * \section asfdoc_samb_pwm_extra_dependencies Dependencies
304  * There are no dependencies related to this driver.
305  *
306  *
307  * \section asfdoc_samb_pwm_extra_errata Errata
308  * There are no errata related to this driver.
309  *
310  *
311  * \section asfdoc_samb_pwm_extra_history Module History
312  * An overview of the module history is presented in the table below, with
313  * details on the enhancements and fixes made to the module since its first
314  * release. The current version of this corresponds to the newest version in
315  * the table.
316  *
317  * <table>
318  *  <tr>
319  *      <th>Changelog</th>
320  *  </tr>
321  *  <tr>
322  *      <td>Initial Release</td>
323  *  </tr>
324  * </table>
325  */
326 
327 /**
328  * \page asfdoc_samb_pwm_exqsg Examples for PWM Driver
329  *
330  * This is a list of the available Quick Start guides (QSGs) and example
331  * applications for \ref asfdoc_samb_pwm_group. QSGs are simple examples with
332  * step-by-step instructions to configure and use this driver in a selection of
333  * use cases. Note that QSGs can be compiled as a standalone application or be
334  * added to the user application.
335  *
336  *  - \subpage asfdoc_samb_pwm_basic_use_case
337  *
338  * \page asfdoc_samb_pwm_document_revision_history Document Revision History
339  *
340  * <table>
341  *  <tr>
342  *    <th>Doc. Rev.</td>
343  *    <th>Date</td>
344  *    <th>Comments</td>
345  *  </tr>
346  *  <tr>
347  *    <td>A</td>
348  *    <td>09/2015</td>
349  *    <td>Initial release</td>
350  *  </tr>
351  * </table>
352  */
353 
354 #endif
355