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(Board_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(Board_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 to 45%.
136  *
137  * @code
138  * uint32_t dutyCycle;
139  *
140  * dutyCycle = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * 45) / 100);
141  * PWM_setDuty(pwm, dutyCycle);
142  * @endcode
143  *
144  * @anchor ti_drivers_PWM_Examples_dutyperiod
145  * # Setting PWM Duty and Period
146  *
147  * If an application needs to modify the duty and period of a running timer,
148  * an API is available to set both with as little interim time as possible.
149  * This minimises the possibility that a timeout will occur between one set
150  * call and the other. For low periods or for instances close to timeout, this
151  * API will pause the instance output briefly and must only be called when the
152  * PWM is already running.
153  *
154  * Below demonstrates setting the duty cycle to 75% of the new period (100us).
155  *
156  * @code
157  * uint32_t dutyCycle;
158  * uint32_t periodUs = 100;
159  *
160  * dutyCycle = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * 75) / 100);
161  * PWM_setDutyAndPeriod(pwm, dutyCycle, periodUs);
162  * @endcode
163  *
164  * ### Modes of Operation #
165  *
166  * A PWM instance can be configured to interpret the period as one of three
167  * units:
168  * - #PWM_PERIOD_US: The period is in microseconds.
169  * - #PWM_PERIOD_HZ: The period is in (reciprocal) Hertz.
170  * - #PWM_PERIOD_COUNTS: The period is in timer counts.
171  *
172  * A PWM instance can be configured to interpret the duty as one of three
173  * units:
174  * - #PWM_DUTY_US: The duty is in microseconds.
175  * - #PWM_DUTY_FRACTION: The duty is in a fractional part of the period
176  * where 0 is 0% and #PWM_DUTY_FRACTION_MAX is 100%.
177  * - #PWM_DUTY_COUNTS: The period is in timer counts and must be less than
178  * the period.
179  *
180  * The idle level parameter is used to set the output to high/low when the
181  * PWM is not running (stopped or not started). The idle level can be
182  * set to:
183  * - #PWM_IDLE_LOW
184  * - #PWM_IDLE_HIGH
185  *
186  * The default PWM configuration is to set a duty of 0% with a 1MHz frequency.
187  * The default period units are in #PWM_PERIOD_HZ and the default duty units
188  * are in #PWM_DUTY_FRACTION. Finally, the default output idle level is
189  * #PWM_IDLE_LOW. It is the application's responsibility to set the duty for
190  * each PWM output used.
191  *
192  * <hr>
193  * @anchor ti_drivers_PWM_Configuration
194  * # Configuration
195  *
196  * Refer to the @ref driver_configuration "Driver's Configuration" section
197  * for driver configuration information.
198  * <hr>
199  *****************************************************************************
200  */
201 
202 #ifndef ti_drivers_PWM__include
203 #define ti_drivers_PWM__include
204 
205 #include <stdint.h>
206 
207 #ifdef __cplusplus
208 extern "C" {
209 #endif
210 
211 
216 #define PWM_DUTY_FRACTION_MAX ((uint32_t) ~0)
217 
229 #define PWM_CMD_RESERVED (32)
230 
243 #define PWM_STATUS_RESERVED (-32)
244 
252 #define PWM_STATUS_SUCCESS (0)
253 
260 #define PWM_STATUS_ERROR (-1)
261 
269 #define PWM_STATUS_UNDEFINEDCMD (-2)
270 
277 #define PWM_STATUS_INVALID_PERIOD (-3)
278 
285 #define PWM_STATUS_INVALID_DUTY (-4)
286 
291 typedef enum {
297 
302 typedef enum {
310 
314 typedef enum {
317 } PWM_IdleLevel;
318 
327 typedef struct {
328  PWM_Period_Units periodUnits;
329  uint32_t periodValue;
330  PWM_Duty_Units dutyUnits;
331  uint32_t dutyValue;
333  void *custom;
335 } PWM_Params;
336 
340 typedef struct PWM_Config_ *PWM_Handle;
341 
346 typedef void (*PWM_CloseFxn) (PWM_Handle handle);
347 
352 typedef int_fast16_t (*PWM_ControlFxn) (PWM_Handle handle, uint_fast16_t cmd,
353  void *arg);
358 typedef void (*PWM_InitFxn) (PWM_Handle handle);
359 
364 typedef PWM_Handle (*PWM_OpenFxn) (PWM_Handle handle, PWM_Params *params);
365 
370 typedef int_fast16_t (*PWM_SetDutyFxn) (PWM_Handle handle,
371  uint32_t duty);
372 
377 typedef int_fast16_t (*PWM_SetPeriodFxn) (PWM_Handle handle,
378  uint32_t period);
379 
384 typedef int_fast16_t (*PWM_SetDutyAndPeriodFxn) (PWM_Handle handle,
385  uint32_t duty, uint32_t period);
386 
391 typedef void (*PWM_StartFxn) (PWM_Handle handle);
392 
397 typedef void (*PWM_StopFxn) (PWM_Handle handle);
398 
404 typedef struct PWM_FxnTable_ {
423 } PWM_FxnTable;
424 
432 typedef struct PWM_Config_ {
436  void *object;
438  void const *hwAttrs;
439 } PWM_Config;
440 
453 extern void PWM_close(PWM_Handle handle);
454 
474 extern int_fast16_t PWM_control(PWM_Handle handle, uint_fast16_t cmd,
475  void *arg);
476 
485 extern void PWM_init(void);
486 
503 extern PWM_Handle PWM_open(uint_least8_t index, PWM_Params *params);
504 
517 extern void PWM_Params_init(PWM_Params *params);
518 
539 extern int_fast16_t PWM_setDuty(PWM_Handle handle, uint32_t duty);
540 
560 extern int_fast16_t PWM_setPeriod(PWM_Handle handle, uint32_t period);
561 
593 extern int_fast16_t PWM_setDutyAndPeriod(PWM_Handle handle, uint32_t duty, uint32_t period);
594 
605 extern void PWM_start(PWM_Handle handle);
606 
618 extern void PWM_stop(PWM_Handle handle);
619 
620 #ifdef __cplusplus
621 }
622 #endif
623 #endif /* ti_drivers_PWM__include */
PWM_Duty_Units dutyUnits
Definition: PWM.h:330
PWM_InitFxn initFxn
Definition: PWM.h:410
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:340
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_SetDutyFxn setDutyFxn
Definition: PWM.h:414
PWM_Duty_Units
PWM duty cycle unit definitions. Refer to device specific implementation if using PWM_DUTY_COUNTS (ra...
Definition: PWM.h:302
uint32_t dutyValue
Definition: PWM.h:331
PWM Global configuration.
Definition: PWM.h:432
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:308
PWM_Period_Units
PWM period unit definitions. Refer to device specific implementation if using PWM_PERIOD_COUNTS (raw ...
Definition: PWM.h:291
The definition of a PWM function table that contains the required set of functions to control a speci...
Definition: PWM.h:404
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:352
void(* PWM_CloseFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_close().
Definition: PWM.h:346
PWM_StartFxn startFxn
Definition: PWM.h:420
void(* PWM_StopFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_stop().
Definition: PWM.h:397
PWM_SetDutyAndPeriodFxn setDutyAndPeriodFxn
Definition: PWM.h:418
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:370
void * object
Definition: PWM.h:436
void const * hwAttrs
Definition: PWM.h:438
Definition: PWM.h:295
Definition: PWM.h:316
PWM_StopFxn stopFxn
Definition: PWM.h:422
void(* PWM_StartFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_start().
Definition: PWM.h:391
PWM_FxnTable const * fxnTablePtr
Definition: PWM.h:434
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:384
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:364
PWM Parameters.
Definition: PWM.h:327
PWM_ControlFxn controlFxn
Definition: PWM.h:408
PWM_Period_Units periodUnits
Definition: PWM.h:328
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:358
struct PWM_Config_ PWM_Config
PWM Global configuration.
Definition: PWM.h:292
Definition: PWM.h:303
PWM_SetPeriodFxn setPeriodFxn
Definition: PWM.h:416
Definition: PWM.h:304
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:377
uint32_t periodValue
Definition: PWM.h:329
Definition: PWM.h:315
PWM_IdleLevel
Idle output level when PWM is not running (stopped / not started).
Definition: PWM.h:314
PWM_CloseFxn closeFxn
Definition: PWM.h:406
struct PWM_FxnTable_ PWM_FxnTable
The definition of a PWM function table that contains the required set of functions to control a speci...
void * custom
Definition: PWM.h:333
PWM_OpenFxn openFxn
Definition: PWM.h:412
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:293
void PWM_start(PWM_Handle handle)
Function to start the specified PWM handle with current settings.
PWM_IdleLevel idleLevel
Definition: PWM.h:332
© Copyright 1995-2019, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale