PWM.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2019, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 /*!****************************************************************************
33  * @file PWM.h
34  * @brief Pulse Width Modulation (PWM) driver
35  *
36  * @anchor ti_drivers_PWM_Overview
37  * # Overview #
38  * The PWM driver in TI-RTOS facilitates the generation of Pulse Width
39  * Modulated signals via simple and portable APIs.
40  *
41  * When a PWM instance is opened, the period, duty cycle and idle level are
42  * configured and the PWM is stopped (waveforms not generated until PWM_start()
43  * is called). The maximum period and duty supported is device dependent;
44  * refer to the implementation specific documentation for values.
45  *
46  * PWM outputs are active-high, meaning the duty will control the duration of
47  * high output on the pin (at 0% duty, the output is always low, at 100% duty,
48  * the output is always high).
49  *
50  * <hr>
51  * @anchor ti_drivers_PWM_Usage
52  * # Usage
53  *
54  * This documentation provides a basic @ref ti_drivers_PWM_Synopsis
55  * "usage summary" and a set of @ref ti_drivers_PWM_Examples "examples"
56  * in the form of commented code fragments. Detailed descriptions of the
57  * APIs are provided in subsequent sections.
58  *
59  * @anchor ti_drivers_PWM_Synopsis
60  * ## Synopsis
61  * @anchor ti_drivers_PWM_Synopsis_Code
62  * @code
63  * // Import PWM Driver definitions
64  * #include <ti/drivers/PWM.h>
65  *
66  * PWM_Handle pwm;
67  * PWM_Params pwmParams;
68  * uint32_t dutyValue;
69  *
70  * // Initialize the PWM driver.
71  * PWM_init();
72  *
73  * // Initialize the PWM parameters
74  * PWM_Params_init(&pwmParams);
75  * pwmParams.idleLevel = PWM_IDLE_LOW; // Output low when PWM is not running
76  * pwmParams.periodUnits = PWM_PERIOD_HZ; // Period is in Hz
77  * pwmParams.periodValue = 1e6; // 1MHz
78  * pwmParams.dutyUnits = PWM_DUTY_FRACTION; // Duty is in fractional percentage
79  * pwmParams.dutyValue = 0; // 0% initial duty cycle
80  *
81  * // Open the PWM instance
82  * pwm = PWM_open(CONFIG_PWM0, &pwmParams);
83  *
84  * if (pwm == NULL) {
85  * // PWM_open() failed
86  * while (1);
87  * }
88  *
89  * PWM_start(pwm); // start PWM with 0% duty cycle
90  *
91  * dutyValue = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * 37) / 100);
92  * PWM_setDuty(pwm, dutyValue); // set duty cycle to 37%
93  * @endcode
94  *
95  * <hr>
96  * @anchor ti_drivers_PWM_Examples
97  * # Examples
98  *
99  * @li @ref ti_drivers_PWM_Examples_open "Opening a PWM instance"
100  * @li @ref ti_drivers_PWM_Examples_duty "Setting PWM duty"
101  * @li @ref ti_drivers_PWM_Examples_dutyperiod "Setting PWM Duty and Period"
102  *
103  * @anchor ti_drivers_PWM_Examples_open
104  * # Opening a PWM instance
105  *
106  * @code
107  *
108  * PWM_Handle pwm;
109  * PWM_Params pwmParams;
110  *
111  * PWM_init();
112  *
113  * PWM_Params_init(&pwmParams);
114  * pwmParams.idleLevel = PWM_IDLE_LOW;
115  * pwmParams.periodUnits = PWM_PERIOD_HZ;
116  * pwmParams.periodValue = 1e6;
117  * pwmParams.dutyUnits = PWM_DUTY_FRACTION;
118  * pwmParams.dutyValue = 0;
119  *
120  * pwm = PWM_open(CONFIG_PWM0, &pwmParams);
121  *
122  * if (pwm == NULL) {
123  * // PWM_open() failed
124  * while (1);
125  * }
126  * @endcode
127  *
128  * @anchor ti_drivers_PWM_Examples_duty
129  * # Setting PWM duty
130  *
131  * Once the PWM instance has been opened and started, the primary API used
132  * by the application will be #PWM_setDuty() to control the duty cycle of a
133  * PWM pin:
134  *
135  * Below demonstrates setting the duty cycle in units of #PWM_DUTY_FRACTION
136  * to 45%.
137  *
138  * @code
139  * uint32_t dutyCycle;
140  *
141  * dutyCycle = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * 45) / 100);
142  * PWM_setDuty(pwm, dutyCycle);
143  * @endcode
144  *
145  * @anchor ti_drivers_PWM_Examples_dutyperiod
146  * # Setting PWM Duty and Period
147  *
148  * If an application needs to modify the duty and period of a running timer,
149  * an API is available to set both with as little interim time as possible.
150  * This minimises the possibility that a timeout will occur between one set
151  * call and the other. For low periods or for instances close to timeout, this
152  * API will pause the instance output briefly and must only be called when the
153  * PWM is already running.
154  *
155  * Below demonstrates setting the duty cycle to 75% of the new period (100us).
156  *
157  * @code
158  * uint32_t dutyCycle;
159  * uint32_t periodUs = 100;
160  *
161  * dutyCycle = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * 75) / 100);
162  * PWM_setDutyAndPeriod(pwm, dutyCycle, periodUs);
163  * @endcode
164  *
165  * ### Modes of Operation #
166  *
167  * A PWM instance can be configured to interpret the period as one of three
168  * units:
169  * - #PWM_PERIOD_US: The period is in microseconds.
170  * - #PWM_PERIOD_HZ: The period is in (reciprocal) Hertz.
171  * - #PWM_PERIOD_COUNTS: The period is in timer counts.
172  *
173  * A PWM instance can be configured to interpret the duty as one of three
174  * units:
175  * - #PWM_DUTY_US: The duty is in microseconds.
176  * - #PWM_DUTY_FRACTION: The duty is in a fractional part of
177  * #PWM_DUTY_FRACTION_MAX where 0 is 0% and
178  * #PWM_DUTY_FRACTION_MAX is 100%. See
179  * @ref ti_drivers_PWM_Examples_duty "Setting PWM duty"
180  * above.
181  * - #PWM_DUTY_COUNTS: The period is in timer counts and must be less than
182  * the period.
183  *
184  * The idle level parameter is used to set the output to high/low when the
185  * PWM is not running (stopped or not started). The idle level can be
186  * set to:
187  * - #PWM_IDLE_LOW
188  * - #PWM_IDLE_HIGH
189  *
190  * The default PWM configuration is to set a duty of 0% with a 1MHz frequency.
191  * The default period units are in #PWM_PERIOD_HZ and the default duty units
192  * are in #PWM_DUTY_FRACTION. Finally, the default output idle level is
193  * #PWM_IDLE_LOW. It is the application's responsibility to set the duty for
194  * each PWM output used.
195  *
196  * <hr>
197  * @anchor ti_drivers_PWM_Configuration
198  * # Configuration
199  *
200  * Refer to the @ref driver_configuration "Driver's Configuration" section
201  * for driver configuration information.
202  * <hr>
203  *****************************************************************************
204  */
205 
206 #ifndef ti_drivers_PWM__include
207 #define ti_drivers_PWM__include
208 
209 #include <stdint.h>
210 
211 #ifdef __cplusplus
212 extern "C" {
213 #endif
214 
215 
220 #define PWM_DUTY_FRACTION_MAX ((uint32_t) ~0)
221 
233 #define PWM_CMD_RESERVED (32)
234 
247 #define PWM_STATUS_RESERVED (-32)
248 
256 #define PWM_STATUS_SUCCESS (0)
257 
264 #define PWM_STATUS_ERROR (-1)
265 
273 #define PWM_STATUS_UNDEFINEDCMD (-2)
274 
281 #define PWM_STATUS_INVALID_PERIOD (-3)
282 
289 #define PWM_STATUS_INVALID_DUTY (-4)
290 
295 typedef enum {
301 
306 typedef enum {
316 
320 typedef enum {
323 } PWM_IdleLevel;
324 
333 typedef struct {
334  PWM_Period_Units periodUnits;
335  uint32_t periodValue;
336  PWM_Duty_Units dutyUnits;
337  uint32_t dutyValue;
339  void *custom;
341 } PWM_Params;
342 
346 typedef struct PWM_Config_ *PWM_Handle;
347 
352 typedef void (*PWM_CloseFxn) (PWM_Handle handle);
353 
358 typedef int_fast16_t (*PWM_ControlFxn) (PWM_Handle handle, uint_fast16_t cmd,
359  void *arg);
364 typedef void (*PWM_InitFxn) (PWM_Handle handle);
365 
370 typedef PWM_Handle (*PWM_OpenFxn) (PWM_Handle handle, PWM_Params *params);
371 
376 typedef int_fast16_t (*PWM_SetDutyFxn) (PWM_Handle handle,
377  uint32_t duty);
378 
383 typedef int_fast16_t (*PWM_SetPeriodFxn) (PWM_Handle handle,
384  uint32_t period);
385 
390 typedef int_fast16_t (*PWM_SetDutyAndPeriodFxn) (PWM_Handle handle,
391  uint32_t duty, uint32_t period);
392 
397 typedef void (*PWM_StartFxn) (PWM_Handle handle);
398 
403 typedef void (*PWM_StopFxn) (PWM_Handle handle);
404 
410 typedef struct {
429 } PWM_FxnTable;
430 
438 typedef struct PWM_Config_ {
442  void *object;
444  void const *hwAttrs;
445 } PWM_Config;
446 
459 extern void PWM_close(PWM_Handle handle);
460 
480 extern int_fast16_t PWM_control(PWM_Handle handle, uint_fast16_t cmd,
481  void *arg);
482 
491 extern void PWM_init(void);
492 
509 extern PWM_Handle PWM_open(uint_least8_t index, PWM_Params *params);
510 
523 extern void PWM_Params_init(PWM_Params *params);
524 
545 extern int_fast16_t PWM_setDuty(PWM_Handle handle, uint32_t duty);
546 
566 extern int_fast16_t PWM_setPeriod(PWM_Handle handle, uint32_t period);
567 
599 extern int_fast16_t PWM_setDutyAndPeriod(PWM_Handle handle, uint32_t duty, uint32_t period);
600 
611 extern void PWM_start(PWM_Handle handle);
612 
624 extern void PWM_stop(PWM_Handle handle);
625 
626 #ifdef __cplusplus
627 }
628 #endif
629 #endif /* ti_drivers_PWM__include */
PWM_Duty_Units dutyUnits
Definition: PWM.h:336
int_fast16_t PWM_control(PWM_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a given PWM_Handle.
void PWM_close(PWM_Handle handle)
Function to close a PWM instance specified by the PWM handle.
struct PWM_Config_ * PWM_Handle
A handle that is returned from a PWM_open() call.
Definition: PWM.h:346
PWM_OpenFxn openFxn
Definition: PWM.h:418
PWM_SetDutyFxn setDutyFxn
Definition: PWM.h:420
PWM_Handle PWM_open(uint_least8_t index, PWM_Params *params)
This function opens a given PWM instance and sets the period, duty and idle level to those specified ...
PWM_Duty_Units
PWM duty cycle unit definitions. Refer to device specific implementation if using PWM_DUTY_COUNTS (ra...
Definition: PWM.h:306
uint32_t dutyValue
Definition: PWM.h:337
PWM Global configuration.
Definition: PWM.h:438
PWM_CloseFxn closeFxn
Definition: PWM.h:412
int_fast16_t PWM_setDutyAndPeriod(PWM_Handle handle, uint32_t duty, uint32_t period)
Function to set both the period and the duty cycle of the specified PWM handle. This API must be call...
void PWM_init(void)
This function initializes the PWM module.
Definition: PWM.h:314
PWM_Period_Units
PWM period unit definitions. Refer to device specific implementation if using PWM_PERIOD_COUNTS (raw ...
Definition: PWM.h:295
int_fast16_t(* PWM_ControlFxn)(PWM_Handle handle, uint_fast16_t cmd, void *arg)
A function pointer to a driver specific implementation of PWM_control().
Definition: PWM.h:358
void(* PWM_CloseFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_close().
Definition: PWM.h:352
void(* PWM_StopFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_stop().
Definition: PWM.h:403
The definition of a PWM function table that contains the required set of functions to control a speci...
Definition: PWM.h:410
int_fast16_t(* PWM_SetDutyFxn)(PWM_Handle handle, uint32_t duty)
A function pointer to a driver specific implementation of PWM_setDuty().
Definition: PWM.h:376
PWM_SetPeriodFxn setPeriodFxn
Definition: PWM.h:422
PWM_StopFxn stopFxn
Definition: PWM.h:428
void * object
Definition: PWM.h:442
void const * hwAttrs
Definition: PWM.h:444
Definition: PWM.h:299
Definition: PWM.h:322
void(* PWM_StartFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_start().
Definition: PWM.h:397
PWM_FxnTable const * fxnTablePtr
Definition: PWM.h:440
int_fast16_t(* PWM_SetDutyAndPeriodFxn)(PWM_Handle handle, uint32_t duty, uint32_t period)
A function pointer to a driver specific implementation of PWM_setDutyAndPeriod(). ...
Definition: PWM.h:390
PWM_SetDutyAndPeriodFxn setDutyAndPeriodFxn
Definition: PWM.h:424
void PWM_Params_init(PWM_Params *params)
Function to initialize the PWM_Params structure to default values.
PWM_Handle(* PWM_OpenFxn)(PWM_Handle handle, PWM_Params *params)
A function pointer to a driver specific implementation of PWM_open().
Definition: PWM.h:370
PWM Parameters.
Definition: PWM.h:333
PWM_Period_Units periodUnits
Definition: PWM.h:334
int_fast16_t PWM_setDuty(PWM_Handle handle, uint32_t duty)
Function to set the duty cycle of the specified PWM handle. PWM instances run in active high output m...
void(* PWM_InitFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_init().
Definition: PWM.h:364
struct PWM_Config_ PWM_Config
PWM Global configuration.
Definition: PWM.h:296
PWM_ControlFxn controlFxn
Definition: PWM.h:414
Definition: PWM.h:307
Definition: PWM.h:308
int_fast16_t PWM_setPeriod(PWM_Handle handle, uint32_t period)
Function to set the period of the specified PWM handle. This API can be called while the PWM is runni...
int_fast16_t(* PWM_SetPeriodFxn)(PWM_Handle handle, uint32_t period)
A function pointer to a driver specific implementation of PWM_setPeriod().
Definition: PWM.h:383
uint32_t periodValue
Definition: PWM.h:335
Definition: PWM.h:321
PWM_IdleLevel
Idle output level when PWM is not running (stopped / not started).
Definition: PWM.h:320
PWM_InitFxn initFxn
Definition: PWM.h:416
PWM_StartFxn startFxn
Definition: PWM.h:426
void * custom
Definition: PWM.h:339
void PWM_stop(PWM_Handle handle)
Function to stop the specified PWM handle. Output will set to the idle level specified by params in P...
Definition: PWM.h:297
void PWM_start(PWM_Handle handle)
Function to start the specified PWM handle with current settings.
PWM_IdleLevel idleLevel
Definition: PWM.h:338
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale