PWM.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2018, 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 PWM driver interface
35  *
36  * To use the PWM driver, ensure that the correct driver library for your
37  * device is linked in and include this header file as follows:
38  * @code
39  * #include <ti/drivers/PWM.h>
40  * @endcode
41  *
42  * This module serves as the main interface for applications. Its purpose
43  * is to redirect the PWM APIs to specific driver implementations
44  * which are specified using a pointer to a #PWM_FxnTable.
45  *
46  * # Overview #
47  * The PWM driver in TI-RTOS facilitates the generation of Pulse Width
48  * Modulated signals via simple and portable APIs. PWM instances must be
49  * opened by calling PWM_open() while passing in a PWM index and a parameters
50  * data structure.
51  *
52  * The driver APIs serve as an interface to a typical TI-RTOS application.
53  * The specific peripheral implementations are responsible for creating all OS
54  * specific primitives to allow for thread-safe operation.
55  *
56  * When a PWM instance is opened, the period, duty cycle and idle level are
57  * configured and the PWM is stopped (waveforms not generated until PWM_start()
58  * is called). The maximum period and duty supported is device dependent;
59  * refer to the implementation specific documentation for values.
60  *
61  * PWM outputs are active-high, meaning the duty will control the duration of
62  * high output on the pin (at 0% duty, the output is always low, at 100% duty,
63  * the output is always high).
64  *
65  * # Usage #
66  *
67  * @code
68  * PWM_Handle pwm;
69  * PWM_Params pwmParams;
70  * uint32_t dutyValue;
71  *
72  * // Initialize the PWM driver.
73  * PWM_init();
74  *
75  * // Initialize the PWM parameters
76  * PWM_Params_init(&pwmParams);
77  * pwmParams.idleLevel = PWM_IDLE_LOW; // Output low when PWM is not running
78  * pwmParams.periodUnits = PWM_PERIOD_HZ; // Period is in Hz
79  * pwmParams.periodValue = 1e6; // 1MHz
80  * pwmParams.dutyUnits = PWM_DUTY_FRACTION; // Duty is in fractional percentage
81  * pwmParams.dutyValue = 0; // 0% initial duty cycle
82  *
83  * // Open the PWM instance
84  * pwm = PWM_open(Board_PWM0, &pwmParams);
85  *
86  * if (pwm == NULL) {
87  * // PWM_open() failed
88  * while (1);
89  * }
90  *
91  * PWM_start(pwm); // start PWM with 0% duty cycle
92  *
93  * dutyValue = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * 37) / 100);
94  * PWM_setDuty(pwm, dutyValue); // set duty cycle to 37%
95  * @endcode
96  *
97  * Details for the example code above are described in the following
98  * subsections.
99  *
100  * ### PWM Driver Configuration #
101  *
102  * In order to use the PWM APIs, the application is required
103  * to provide device-specific PWM configuration in the Board.c file.
104  * The PWM driver interface defines a configuration data structure:
105  *
106  * @code
107  * typedef struct PWM_Config_ {
108  * PWM_FxnTable const *fxnTablePtr;
109  * void *object;
110  * void const *hwAttrs;
111  * } PWM_Config;
112  * @endcode
113  *
114  * The application must declare an array of PWM_Config elements, named
115  * PWM_config[]. Each element of PWM_config[] is populated with
116  * pointers to a device specific PWM driver implementation's function
117  * table, driver object, and hardware attributes. The hardware attributes
118  * define properties such as which pin will be driven, and which timer peripheral
119  * will be used. Each element in PWM_config[] corresponds to
120  * a PWM instance, and none of the elements should have NULL pointers.
121  *
122  * Additionally, the PWM driver interface defines a global integer variable
123  * 'PWM_count' which is initialized to the number of PWM instances the
124  * application has defined in the PWM_Config array.
125  *
126  * You will need to check the device-specific PWM driver implementation's
127  * header file for example configuration. Please also refer to the
128  * Board.c file of any of your examples to see the PWM configuration.
129  *
130  * ### Initializing the PWM Driver #
131  *
132  * PWM_init() must be called before any other PWM APIs. This function
133  * calls the device implementation's PWM initialization function, for each
134  * element of PWM_config[].
135  *
136  * ### Opening the PWM Driver #
137  *
138  * Opening a PWM requires four steps:
139  * 1. Create and initialize a PWM_Params structure.
140  * 2. Fill in the desired parameters.
141  * 3. Call PWM_open(), passing the index of the PWM in the PWM_config
142  * structure, and the address of the PWM_Params structure. The
143  * PWM instance is specified by the index in the PWM_config structure.
144  * 4. Check that the PWM handle returned by PWM_open() is non-NULL,
145  * and save it. The handle will be used to read and write to the
146  * PWM you just opened.
147  *
148  * Only one PWM index can be used at a time; calling PWM_open() a second
149  * time with the same index previously passed to PWM_open() will result in
150  * an error. You can, though, re-use the index if the instance is closed
151  * via PWM_close().
152  * In the example code, Board_PWM0 is passed to PWM_open(). This macro
153  * is defined in the example's Board.h file.
154  *
155  * ### Modes of Operation #
156  *
157  * A PWM instance can be configured to interpret the period as one of three
158  * units:
159  * - #PWM_PERIOD_US: The period is in microseconds.
160  * - #PWM_PERIOD_HZ: The period is in (reciprocal) Hertz.
161  * - #PWM_PERIOD_COUNTS: The period is in timer counts.
162  *
163  * A PWM instance can be configured to interpret the duty as one of three
164  * units:
165  * - #PWM_DUTY_US: The duty is in microseconds.
166  * - #PWM_DUTY_FRACTION: The duty is in a fractional part of the period
167  * where 0 is 0% and #PWM_DUTY_FRACTION_MAX is 100%.
168  * - #PWM_DUTY_COUNTS: The period is in timer counts and must be less than
169  * the period.
170  *
171  * The idle level parameter is used to set the output to high/low when the
172  * PWM is not running (stopped or not started). The idle level can be
173  * set to:
174  * - #PWM_IDLE_LOW
175  * - #PWM_IDLE_HIGH
176  *
177  * The default PWM configuration is to set a duty of 0% with a 1MHz frequency.
178  * The default period units are in PWM_PERIOD_HZ and the default duty units
179  * are in PWM_DUTY_FRACTION. Finally, the default output idle level is
180  * PWM_IDLE_LOW. It is the application's responsibility to set the duty for
181  * each PWM output used.
182  *
183  * ### Controlling the PWM Duty Cycle #
184  *
185  * Once the PWM instance has been opened and started, the primary API used
186  * by the application will be #PWM_setDuty() to control the duty cycle of a
187  * PWM pin:
188  *
189  * Below demonstrates setting the duty cycle to 45%.
190  *
191  * @code
192  * uint32_t dutyCycle;
193  *
194  * dutyCycle = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * 45) / 100);
195  * PWM_setDuty(pwm, dutyCycle);
196  * @endcode
197  *
198  * ### Setting Duty and Period on a Running Instance ###
199  *
200  * If an application needs to modify the duty and period of a running timer,
201  * an API is available to set both with as little interim time as possible.
202  * This minimises the possibility that a timeout will occur between one set
203  * call and the other. For low periods or for instances close to timeout, this
204  * API will pause the instance output briefly and must only be called when the
205  * PWM is already running.
206  *
207  * Below demonstrates setting the duty cycle to 75% of the new period (100us).
208  *
209  * @code
210  * uint32_t dutyCycle;
211  * uint32_t periodUs = 100;
212  *
213  * dutyCycle = (uint32_t) (((uint64_t) PWM_DUTY_FRACTION_MAX * 75) / 100);
214  * PWM_setDutyAndPeriod(pwm, dutyCycle, periodUs);
215  * @endcode
216  *
217  * # Implementation #
218  *
219  * The PWM driver interface module is joined (at link time) to an
220  * array of PWM_Config data structures named *PWM_config*.
221  * PWM_config is implemented in the application with each entry being a
222  * PWM instance. Each entry in *PWM_config* contains a:
223  * - (PWM_FxnTable *) to a set of functions that implement a PWM peripheral
224  * - (void *) data object that is associated with the PWM_FxnTable
225  * - (void *) hardware attributes that are associated with the PWM_FxnTable
226  *
227  * The PWM APIs are redirected to the device specific implementations
228  * using the PWM_FxnTable pointer of the PWM_config entry.
229  * In order to use device specific functions of the PWM driver directly,
230  * link in the correct driver library for your device and include the
231  * device specific PWM driver header file (which in turn includes PWM.h).
232  * For example, for the MSP432 family of devices, you would include the
233  * following header file:
234  * @code
235  * #include <ti/drivers/pwm/PWMTimerMSP432.h>
236  * @endcode
237  *
238  *****************************************************************************
239  */
240 
241 #ifndef ti_drivers_PWM__include
242 #define ti_drivers_PWM__include
243 
244 #ifdef __cplusplus
245 extern "C" {
246 #endif
247 
248 #include <stdint.h>
249 
254 #define PWM_DUTY_FRACTION_MAX ((uint32_t) ~0)
255 
267 #define PWM_CMD_RESERVED (32)
268 
281 #define PWM_STATUS_RESERVED (-32)
282 
290 #define PWM_STATUS_SUCCESS (0)
291 
298 #define PWM_STATUS_ERROR (-1)
299 
307 #define PWM_STATUS_UNDEFINEDCMD (-2)
308 
315 #define PWM_STATUS_INVALID_PERIOD (-3)
316 
323 #define PWM_STATUS_INVALID_DUTY (-4)
324 
329 typedef enum PWM_Period_Units_ {
335 
340 typedef enum PWM_Duty_Units_ {
348 
352 typedef enum PWM_IdleLevel_ {
355 } PWM_IdleLevel;
356 
365 typedef struct PWM_Params_ {
366  PWM_Period_Units periodUnits;
367  uint32_t periodValue;
368  PWM_Duty_Units dutyUnits;
369  uint32_t dutyValue;
371  void *custom;
373 } PWM_Params;
374 
378 typedef struct PWM_Config_ *PWM_Handle;
379 
384 typedef void (*PWM_CloseFxn) (PWM_Handle handle);
385 
390 typedef int_fast16_t (*PWM_ControlFxn) (PWM_Handle handle, uint_fast16_t cmd,
391  void *arg);
396 typedef void (*PWM_InitFxn) (PWM_Handle handle);
397 
402 typedef PWM_Handle (*PWM_OpenFxn) (PWM_Handle handle, PWM_Params *params);
403 
408 typedef int_fast16_t (*PWM_SetDutyFxn) (PWM_Handle handle,
409  uint32_t duty);
410 
415 typedef int_fast16_t (*PWM_SetPeriodFxn) (PWM_Handle handle,
416  uint32_t period);
417 
422 typedef int_fast16_t (*PWM_SetDutyAndPeriodFxn) (PWM_Handle handle,
423  uint32_t duty, uint32_t period);
424 
429 typedef void (*PWM_StartFxn) (PWM_Handle handle);
430 
435 typedef void (*PWM_StopFxn) (PWM_Handle handle);
436 
442 typedef struct PWM_FxnTable_ {
461 } PWM_FxnTable;
462 
470 typedef struct PWM_Config_ {
474  void *object;
476  void const *hwAttrs;
477 } PWM_Config;
478 
491 extern void PWM_close(PWM_Handle handle);
492 
512 extern int_fast16_t PWM_control(PWM_Handle handle, uint_fast16_t cmd,
513  void *arg);
514 
523 extern void PWM_init(void);
524 
541 extern PWM_Handle PWM_open(uint_least8_t index, PWM_Params *params);
542 
555 extern void PWM_Params_init(PWM_Params *params);
556 
577 extern int_fast16_t PWM_setDuty(PWM_Handle handle, uint32_t duty);
578 
598 extern int_fast16_t PWM_setPeriod(PWM_Handle handle, uint32_t period);
599 
630 extern int_fast16_t PWM_setDutyAndPeriod(PWM_Handle handle, uint32_t duty, uint32_t period);
631 
642 extern void PWM_start(PWM_Handle handle);
643 
655 extern void PWM_stop(PWM_Handle handle);
656 
657 #ifdef __cplusplus
658 }
659 #endif
660 #endif /* ti_drivers_PWM__include */
struct PWM_Params_ PWM_Params
PWM Parameters.
PWM_InitFxn initFxn
Definition: PWM.h:448
Definition: PWM.h:346
int_fast16_t PWM_control(PWM_Handle handle, uint_fast16_t cmd, void *arg)
Function performs implementation specific features on a given PWM_Handle.
uint32_t dutyValue
Definition: PWM.h:369
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:378
PWM_Duty_Units dutyUnits
Definition: PWM.h:368
PWM_IdleLevel_
Idle output level when PWM is not running (stopped / not started).
Definition: PWM.h:352
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 ...
uint32_t periodValue
Definition: PWM.h:367
PWM_SetDutyFxn setDutyFxn
Definition: PWM.h:452
PWM_Duty_Units_
PWM duty cycle unit definitions. Refer to device specific implementation if using PWM_DUTY_COUNTS (ra...
Definition: PWM.h:340
PWM Global configuration.
Definition: PWM.h:470
PWM_Period_Units_
PWM period unit definitions. Refer to device specific implementation if using PWM_PERIOD_COUNTS (raw ...
Definition: PWM.h:329
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...
Definition: PWM.h:342
Definition: PWM.h:354
void PWM_init(void)
This function initializes the PWM module.
Definition: PWM.h:341
Definition: PWM.h:330
The definition of a PWM function table that contains the required set of functions to control a speci...
Definition: PWM.h:442
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:390
void(* PWM_CloseFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_close().
Definition: PWM.h:384
PWM_Period_Units periodUnits
Definition: PWM.h:366
PWM_StartFxn startFxn
Definition: PWM.h:458
void(* PWM_StopFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_stop().
Definition: PWM.h:435
Definition: PWM.h:331
PWM_SetDutyAndPeriodFxn setDutyAndPeriodFxn
Definition: PWM.h:456
PWM_IdleLevel idleLevel
Definition: PWM.h:370
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:408
PWM Parameters.
Definition: PWM.h:365
enum PWM_Period_Units_ PWM_Period_Units
PWM period unit definitions. Refer to device specific implementation if using PWM_PERIOD_COUNTS (raw ...
void * object
Definition: PWM.h:474
void const * hwAttrs
Definition: PWM.h:476
PWM_StopFxn stopFxn
Definition: PWM.h:460
void(* PWM_StartFxn)(PWM_Handle handle)
A function pointer to a driver specific implementation of PWM_start().
Definition: PWM.h:429
PWM_FxnTable const * fxnTablePtr
Definition: PWM.h:472
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:422
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:402
PWM_ControlFxn controlFxn
Definition: PWM.h:446
Definition: PWM.h:353
Definition: PWM.h:333
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:396
struct PWM_Config_ PWM_Config
PWM Global configuration.
enum PWM_IdleLevel_ PWM_IdleLevel
Idle output level when PWM is not running (stopped / not started).
PWM_SetPeriodFxn setPeriodFxn
Definition: PWM.h:454
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:415
void * custom
Definition: PWM.h:371
PWM_CloseFxn closeFxn
Definition: PWM.h:444
struct PWM_FxnTable_ PWM_FxnTable
The definition of a PWM function table that contains the required set of functions to control a speci...
PWM_OpenFxn openFxn
Definition: PWM.h:450
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...
void PWM_start(PWM_Handle handle)
Function to start the specified PWM handle with current settings.
enum PWM_Duty_Units_ PWM_Duty_Units
PWM duty cycle unit definitions. Refer to device specific implementation if using PWM_DUTY_COUNTS (ra...
© Copyright 1995-2018, Texas Instruments Incorporated. All rights reserved.
Trademarks | Privacy policy | Terms of use | Terms of sale