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 * ======== PWM.c ========
34 */
35
36 #include <stdbool.h>
37 #include <stdint.h>
38 #include <stdlib.h>
39
40 #include <ti/drivers/dpl/HwiP.h>
41 #include <ti/drivers/PWM.h>
42
43 __attribute__((weak)) extern const PWM_Config PWM_config[];
44 __attribute__((weak)) extern const uint_least8_t PWM_count;
45
46 /* Default PWM parameters structure */
47 const PWM_Params PWM_defaultParams = {
48 .periodUnits = PWM_PERIOD_HZ, /* Period is defined in Hz */
49 .periodValue = 1e6, /* 1MHz */
50 .dutyUnits = PWM_DUTY_FRACTION, /* Duty is fraction of period */
51 .dutyValue = 0, /* 0% duty cycle */
52 .idleLevel = PWM_IDLE_LOW, /* Low idle level */
53 .custom = NULL /* No custom params */
54 };
55
56 static bool isInitialized = false;
57
58 /*
59 * ======== PWM_close ========
60 */
PWM_close(PWM_Handle handle)61 void PWM_close(PWM_Handle handle)
62 {
63 handle->fxnTablePtr->closeFxn(handle);
64 }
65
66 /*
67 * ======== PWM_control ========
68 */
PWM_control(PWM_Handle handle,uint_fast16_t cmd,void * arg)69 int_fast16_t PWM_control(PWM_Handle handle, uint_fast16_t cmd, void *arg)
70 {
71 return handle->fxnTablePtr->controlFxn(handle, cmd, arg);
72 }
73
74 /*
75 * ======== PWM_init ========
76 */
PWM_init(void)77 void PWM_init(void)
78 {
79 uint_least8_t i;
80 uint_fast32_t key;
81
82 key = HwiP_disable();
83
84 if (!isInitialized) {
85 isInitialized = (bool) true;
86
87 /* Call each driver's init function */
88 for (i = 0; i < PWM_count; i++) {
89 PWM_config[i].fxnTablePtr->initFxn((PWM_Handle) &(PWM_config[i]));
90 }
91 }
92
93 HwiP_restore(key);
94 }
95
96 /*
97 * ======== PWM_open ========
98 */
PWM_open(uint_least8_t index,PWM_Params * params)99 PWM_Handle PWM_open(uint_least8_t index, PWM_Params *params)
100 {
101 PWM_Handle handle = NULL;
102
103 if (isInitialized && (index < PWM_count)) {
104 /* If params are NULL use defaults */
105 if (params == NULL) {
106 params = (PWM_Params *) &PWM_defaultParams;
107 }
108
109 /* Get handle for this driver instance */
110 handle = (PWM_Handle) &(PWM_config[index]);
111
112 handle = handle->fxnTablePtr->openFxn(handle, params);
113 }
114
115 return (handle);
116 }
117
118 /*
119 * ======== PWM_Params_init ========
120 */
PWM_Params_init(PWM_Params * params)121 void PWM_Params_init(PWM_Params *params)
122 {
123 *params = PWM_defaultParams;
124 }
125
126 /*
127 * ======== PWM_setDuty ========
128 */
PWM_setDuty(PWM_Handle handle,uint32_t duty)129 int_fast16_t PWM_setDuty(PWM_Handle handle, uint32_t duty)
130 {
131 return(handle->fxnTablePtr->setDutyFxn(handle, duty));
132 }
133
134 /*
135 * ======== PWM_setDuty ========
136 */
PWM_setPeriod(PWM_Handle handle,uint32_t period)137 int_fast16_t PWM_setPeriod(PWM_Handle handle, uint32_t period)
138 {
139 return(handle->fxnTablePtr->setPeriodFxn(handle, period));
140 }
141 /*
142 * ======== PWM_setDutyandPeriod ========
143 */
PWM_setDutyAndPeriod(PWM_Handle handle,uint32_t duty,uint32_t period)144 int_fast16_t PWM_setDutyAndPeriod(PWM_Handle handle, uint32_t duty, uint32_t period)
145 {
146 return(handle->fxnTablePtr->setDutyAndPeriodFxn(handle, duty, period));
147 }
148
149 /*
150 * ======== PWM_start ========
151 */
PWM_start(PWM_Handle handle)152 void PWM_start(PWM_Handle handle)
153 {
154 handle->fxnTablePtr->startFxn(handle);
155 }
156
157 /*
158 * ======== PWM_stop ========
159 */
PWM_stop(PWM_Handle handle)160 void PWM_stop(PWM_Handle handle)
161 {
162 handle->fxnTablePtr->stopFxn(handle);
163 }
164