rusEFI
The most advanced open source ECU
Loading...
Searching...
No Matches
hal_pwm_lld.h
Go to the documentation of this file.
1/*
2 ChibiOS/HAL - Copyright (C) 2014 Adam J. Porter
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17/**
18 * @file KE1xF/pwm_lld.h
19 * @brief KINETIS PWM subsystem low level driver header.
20 * @author andreika <prometheus.pcb@gmail.com>
21 *
22 * @addtogroup PWM
23 * @{
24 */
25
26#ifndef HAL_PWM_LLD_H_
27#define HAL_PWM_LLD_H_
28
29#if HAL_USE_PWM || defined(__DOXYGEN__)
30
31/*===========================================================================*/
32/* Driver constants. */
33/*===========================================================================*/
34
35/**
36 * @brief Number of PWM channels per PWM driver.
37 */
38#define PWM_CHANNELS 8
39
40/*===========================================================================*/
41/* Driver pre-compile time settings. */
42/*===========================================================================*/
43
44#if !defined(KINETIS_PWM_USE_FTM0)
45 #define KINETIS_PWM_USE_FTM0 FALSE
46#endif
47
48#if !defined(KINETIS_PWM_USE_FTM1)
49 #define KINETIS_PWM_USE_FTM1 FALSE
50#endif
51
52#if !defined(KINETIS_PWM_USE_FTM2)
53 #define KINETIS_PWM_USE_FTM2 FALSE
54#endif
55
56#if !defined(KINETIS_PWM_USE_FTM3)
57 #define KINETIS_PWM_USE_FTM3 FALSE
58#endif
59
60/**
61 * @brief FTM0 interrupt priority level setting.
62 */
63#if !defined(KINETIS_PWM_FTM0_PRIORITY) || defined(__DOXYGEN__)
64#define KINETIS_PWM_FTM0_PRIORITY 12
65#endif
66
67/**
68 * @brief FTM1 interrupt priority level setting.
69 */
70#if !defined(KINETIS_PWM_FTM1_PRIORITY) || defined(__DOXYGEN__)
71#define KINETIS_PWM_FTM1_PRIORITY 12
72#endif
73
74/**
75 * @brief FTM2 interrupt priority level setting.
76 */
77#if !defined(KINETIS_PWM_FTM2_PRIORITY) || defined(__DOXYGEN__)
78#define KINETIS_PWM_FTM2_PRIORITY 12
79#endif
80
81/**
82 * @brief FTM3 interrupt priority level setting.
83 */
84#if !defined(KINETIS_PWM_FTM3_PRIORITY) || defined(__DOXYGEN__)
85#define KINETIS_PWM_FTM3_PRIORITY 12
86#endif
87
88/** @} */
89
90/**
91 * @name Configuration options
92 * @{
93 */
94/**
95 * @brief If advanced timer features switch.
96 * @details If set to @p TRUE the advanced features for TIM1 and TIM8 are
97 * enabled.
98 * @note The default is @p TRUE.
99 */
100#if !defined(KINETIS_PWM_USE_ADVANCED) || defined(__DOXYGEN__)
101#define KINETIS_PWM_USE_ADVANCED FALSE
102#endif
103/** @} */
104
105/*===========================================================================*/
106/* Configuration checks. */
107/*===========================================================================*/
108
109#if !KINETIS_PWM_USE_FTM0 && !KINETIS_PWM_USE_FTM1 && !KINETIS_PWM_USE_FTM2 && !KINETIS_PWM_USE_FTM3
110#error "PWM driver activated but no FTM peripheral assigned"
111#endif
112
113/*===========================================================================*/
114/* Driver data structures and types. */
115/*===========================================================================*/
116
117/**
118 * @brief Type of a PWM mode.
119 */
120typedef uint32_t pwmmode_t;
121
122/**
123 * @brief Type of a PWM channel.
124 */
125typedef uint8_t pwmchannel_t;
126
127/**
128 * @brief Type of a channels mask.
129 */
130typedef uint32_t pwmchnmsk_t;
131
132/**
133 * @brief Type of a PWM counter.
134 */
135typedef uint16_t pwmcnt_t;
136
137/**
138 * @brief Type of a PWM driver channel configuration structure.
139 */
140typedef struct {
141 /**
142 * @brief Channel active logic level.
143 */
145
146 /**
147 * @brief Channel callback pointer.
148 * @note This callback is invoked on the channel compare event. If set to
149 * @p NULL then the callback is disabled.
150 */
151 pwmcallback_t callback;
152 /* End of the mandatory fields.*/
154
155/**
156 * @brief Type of a PWM driver configuration structure.
157 */
158typedef struct {
159 /**
160 * @brief Timer clock in Hz.
161 * @note The low level can use assertions in order to catch invalid
162 * frequency specifications.
163 */
164 uint32_t frequency;
165 /**
166 * @brief PWM period in ticks.
167 * @note The low level can use assertions in order to catch invalid
168 * period specifications.
169 */
171 /**
172 * @brief Periodic callback pointer.
173 * @note This callback is invoked on PWM counter reset. If set to
174 * @p NULL then the callback is disabled.
175 */
176 pwmcallback_t callback;
177 /**
178 * @brief Channels configurations.
179 */
181 /* End of the mandatory fields.*/
182 /* [andreika]: these are STM32-compatible */
183 /**
184 * @brief TIM CR2 register initialization data.
185 * @note The value of this field should normally be equal to zero.
186 */
187 uint32_t cr2;
188 /**
189 * @brief TIM DIER register initialization data.
190 * @note The value of this field should normally be equal to zero.
191 * @note Only the DMA-related bits can be specified in this field.
192 */
193 uint32_t dier;
194} PWMConfig;
195
196/**
197 * @brief Structure representing a PWM driver.
198 */
199struct PWMDriver {
200 /**
201 * @brief Driver state.
202 */
203 pwmstate_t state;
204 /**
205 * @brief Current driver configuration data.
206 */
208 /**
209 * @brief Current PWM period in ticks.
210 */
212 /**
213 * @brief Mask of the enabled channels.
214 */
216 /**
217 * @brief Number of channels in this instance.
218 */
220#if defined(PWM_DRIVER_EXT_FIELDS)
221 PWM_DRIVER_EXT_FIELDS
222#endif
223 /* End of the mandatory fields.*/
224 /**
225 * @brief Pointer to the FTM registers block.
226 */
227 FTM_TypeDef *ftm;
228};
229
230/*===========================================================================*/
231/* Driver macros. */
232/*===========================================================================*/
233
234/**
235 * @brief Changes the period the PWM peripheral.
236 * @details This function changes the period of a PWM unit that has already
237 * been activated using @p pwmStart().
238 * @pre The PWM unit must have been activated using @p pwmStart().
239 * @post The PWM unit period is changed to the new value.
240 * @note The function has effect at the next cycle start.
241 * @note If a period is specified that is shorter than the pulse width
242 * programmed in one of the channels then the behavior is not
243 * guaranteed.
244 *
245 * @param[in] pwmp pointer to a @p PWMDriver object
246 * @param[in] period new cycle time in ticks
247 *
248 * @notapi
249 */
250#define pwm_lld_change_period(pwmp, period) \
251 do { \
252 (pwmp)->ftm->MOD = ((period) - 1); \
253 pwmp->ftm->PWMLOAD = FTM_PWMLOAD_LDOK_MASK;\
254 } while(0)
255
256/*===========================================================================*/
257/* External declarations. */
258/*===========================================================================*/
259
260#if KINETIS_PWM_USE_FTM0 || defined(__DOXYGEN__)
261extern PWMDriver PWMD1;
262#endif
263#if KINETIS_PWM_USE_FTM1 || defined(__DOXYGEN__)
264extern PWMDriver PWMD2;
265#endif
266#if KINETIS_PWM_USE_FTM2 || defined(__DOXYGEN__)
267extern PWMDriver PWMD3;
268#endif
269#if KINETIS_PWM_USE_FTM3 || defined(__DOXYGEN__)
270extern PWMDriver PWMD4;
271#endif
272
273#ifdef __cplusplus
274extern "C" {
275#endif
276 void pwm_lld_init(void);
277 void pwm_lld_start(PWMDriver *pwmp);
278 void pwm_lld_stop(PWMDriver *pwmp);
281 pwmcnt_t width);
289#ifdef __cplusplus
290}
291#endif
292
293#endif /* HAL_USE_PWM */
294
295#endif /* HAL_PWM_LLD_H_ */
296
297/** @} */
uint16_t channel
Definition adc_inputs.h:104
static GppwmChannel channels[GPPWM_CHANNELS]
Definition gppwm.cpp:6
void pwm_lld_disable_periodic_notification(PWMDriver *pwmp)
Disables the periodic activation edge notification.
PWMDriver PWMD2
PWMD2 driver identifier.
Definition hal_pwm_lld.c:57
void pwm_lld_stop(PWMDriver *pwmp)
Deactivates the PWM peripheral.
void pwm_lld_disable_channel_notification(PWMDriver *pwmp, pwmchannel_t channel)
Disables a channel de-activation edge notification.
void pwm_lld_disable_channel(PWMDriver *pwmp, pwmchannel_t channel)
Disables a PWM channel and its notification.
void pwm_lld_init(void)
Low level PWM driver initialization.
void pwm_lld_start(PWMDriver *pwmp)
Configures and activates the PWM peripheral.
void pwm_lld_enable_channel(PWMDriver *pwmp, pwmchannel_t channel, pwmcnt_t width)
Enables a PWM channel.
void pwm_lld_enable_periodic_notification(PWMDriver *pwmp)
Enables the periodic activation edge notification.
void pwm_lld_enable_channel_notification(PWMDriver *pwmp, pwmchannel_t channel)
Enables a channel de-activation edge notification.
PWMDriver PWMD1
PWMD1 driver identifier.
Definition hal_pwm_lld.c:49
PWMDriver PWMD4
PWMD4 driver identifier.
Definition hal_pwm_lld.c:73
PWMDriver PWMD3
PWMD3 driver identifier.
Definition hal_pwm_lld.c:65
uint32_t pwmmode_t
Type of a PWM mode.
uint8_t pwmchannel_t
Type of a PWM channel.
uint32_t pwmchnmsk_t
Type of a channels mask.
uint16_t pwmcnt_t
Type of a PWM counter.
Type of a PWM driver channel configuration structure.
pwmcallback_t callback
Channel callback pointer.
pwmmode_t mode
Channel active logic level.
Type of a PWM driver configuration structure.
pwmcallback_t callback
Periodic callback pointer.
uint32_t dier
TIM DIER register initialization data.
uint32_t frequency
Timer clock in Hz.
uint32_t cr2
TIM CR2 register initialization data.
pwmcnt_t period
PWM period in ticks.
Structure representing a PWM driver.
pwmcnt_t period
Current PWM period in ticks.
PWM_DRIVER_EXT_FIELDS FTM_TypeDef * ftm
Pointer to the FTM registers block.
pwmchannel_t channels
Number of channels in this instance.
pwmchnmsk_t enabled
Mask of the enabled channels.
const PWMConfig * config
Current driver configuration data.
pwmstate_t state
Driver state.