app_pwm.h
Go to the documentation of this file.
1 /**
2 ****************************************************************************************
3 *
4 * @file app_pwm.h
5 * @author BLE Driver Team
6 * @brief Header file containing functions prototypes of PWM app library.
7 *
8 ****************************************************************************************
9 * @attention
10 #####Copyright (c) 2019 GOODIX
11 All rights reserved.
12
13 Redistribution and use in source and binary forms, with or without
14 modification, are permitted provided that the following conditions are met:
15 * Redistributions of source code must retain the above copyright
16 notice, this list of conditions and the following disclaimer.
17 * Redistributions in binary form must reproduce the above copyright
18 notice, this list of conditions and the following disclaimer in the
19 documentation and/or other materials provided with the distribution.
20 * Neither the name of GOODIX nor the names of its contributors may be used
21 to endorse or promote products derived from this software without
22 specific prior written permission.
23
24 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDERS AND CONTRIBUTORS BE
28 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 POSSIBILITY OF SUCH DAMAGE.
35 ****************************************************************************************
36 */
37
38 /** @addtogroup PERIPHERAL Peripheral Driver
39 * @{
40 */
41
42 /** @addtogroup APP_DRIVER APP DRIVER
43 * @{
44 */
45
46 /** @defgroup APP_PWM PWM
47 * @brief PWM APP module driver.
48 * @{
49 */
50 #ifndef _APP_PWM_H_
51 #define _APP_PWM_H_
52
53 #include "grx_hal.h"
54 #include "app_io.h"
55 #include "app_drv_error.h"
56 #include "app_drv_config.h"
57 #ifdef APP_PWM_CODING_ENABLE
58 #include "app_dma.h"
59 #endif
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
65 #ifdef HAL_PWM_MODULE_ENABLED
66
67 /** @addtogroup APP_PWM_DEFINE Defines
68 * @{
69 */
70
71 #define APP_PWM_PIN_ENABLE 1 /**< PWM pin enable */
72 #define APP_PWM_PIN_DISABLE 0 /**< PWM pin disable */
73
74 /** @} */
75
76 /** @addtogroup APP_PWM_ENUM Enumerations
77 * @{
78 */
79
80 /**
81 * @brief PWM module Enumerations definition
82 */
83 typedef enum
84 {
85 APP_PWM_ID_0, /**< PWM module 0. */
86 APP_PWM_ID_1, /**< PWM module 1. */
87 APP_PWM_ID_MAX /**< Only for check parameter, not used as input parameters. */
88 } app_pwm_id_t;
89
90 /**
91 * @brief PWM active channel Enumerations definition
92 */
93 typedef enum
94 {
95 APP_PWM_ACTIVE_CHANNEL_A = 0x01, /**< The active channel is A. */
96 APP_PWM_ACTIVE_CHANNEL_B = 0x02, /**< The active channel is B. */
97 APP_PWM_ACTIVE_CHANNEL_C = 0x04, /**< The active channel is C. */
98 APP_PWM_ACTIVE_CHANNEL_ALL = 0x07, /**< The active channels are ALL. */
99 APP_PWM_ACTIVE_CHANNEL_CLEARED = 0x00 /**< All active channels are cleared. */
100 } app_pwm_active_channel_t;
101
102 /**@brief App pwm state types. */
103 typedef enum
104 {
105 APP_PWM_INVALID = 0,
106 APP_PWM_ACTIVITY,
107 #ifdef APP_DRIVER_WAKEUP_CALL_FUN
108 APP_PWM_SLEEP,
109 #endif
110
111 } app_pwm_state_t;
112
113 /**@brief App pwm module state types. */
114 typedef enum
115 {
116 APP_PWM_STOP = 0,
117 APP_PWM_START,
118 } app_pwm_module_state_t;
119
120 #ifdef APP_PWM_CODING_ENABLE
121 /**
122 * @brief PWM event Enumerations definition
123 */
124 typedef enum
125 {
126 APP_PWM_CHANNEL_A_ERROR, /**< Channel A error in coding mode. */
127 APP_PWM_CHANNEL_B_ERROR, /**< Channel B error in coding mode. */
128 APP_PWM_CHANNEL_C_ERROR, /**< Channel C error in coding mode. */
129 APP_PWM_CODING_DONE, /**< Coding done in coding mode. */
130 APP_PWM_CODING_LOAD, /**< Coding load in coding mode. */
131 } app_pwm_evt_type_t;
132 #endif
133
134 /** @} */
135
136 /** @addtogroup APP_PWM_STRUCTURES Structures
137 * @{
138 */
139
140 /**
141 * @brief PWM IO configuration Structures
142 */
143 typedef struct
144 {
145 app_io_type_t type; /**< Specifies the type of PWM IO. */
146 app_io_mux_t mux; /**< Specifies the Peripheral to be connected to the selected pins. */
147 uint32_t pin; /**< Specifies the IO pins to be configured.
148 This parameter can be any value of @ref GR5xxx_pins. */
149 app_io_pull_t pull; /**< Specifies the Pull-up or Pull-Down activation for the selected pins. */
150 uint8_t enable; /**< Enable or disable the pin. */
151 } app_pwm_pin_t;
152
153 /**
154 * @brief PWM configuration Structures
155 */
156 typedef struct
157 {
158 app_pwm_pin_t channel_a; /**< Set the configuration of PWM channel A pin. */
159 app_pwm_pin_t channel_b; /**< Set the configuration of PWM channel B pin. */
160 app_pwm_pin_t channel_c; /**< Set the configuration of PWM channel C pin. */
161 } app_pwm_pin_cfg_t;
162
163 /**
164 * @brief PWM Channel init Structure definition
165 */
166 typedef struct
167 {
168 uint8_t duty; /**< Specifies the duty in PWM output mode.
169 This parameter must be a number between 0 ~ 100.*/
170
171 uint8_t drive_polarity; /**< Specifies the drive polarity in PWM output mode.
172 This parameter can be a value of @ref PWM_Drive_Polarity.*/
173 #ifdef APP_PWM_STOP_SPEC_CH_ENABLE
174 uint32_t fstoplvl; /**< Specifies the PWM io level when stop.
175 This parameter can be a value of @ref PWM_STOP_LVL */
176 #endif
177 } app_pwm_channel_init_t;
178
179 #ifdef APP_PWM_CODING_ENABLE
180 /**
181 * @brief PWM operate mode Enumerations definition
182 */
183 typedef struct
184 {
185 dma_regs_t *pwm_dma_instance; /**< Specifies the DMA instance.*/
186 dma_channel_t pwm_dma_channel; /**< Specifies the dma channel of PWM. */
187 } app_pwm_mode_t;
188
189 /**@brief App pwm dma state types. */
190 typedef enum
191 {
192 APP_PWM_DMA_INVALID = 0,
193 APP_PWM_DMA_ACTIVITY,
194 } app_pwm_dma_state_t;
195
196 /**
197 * @brief PWM event structure definition
198 */
199 typedef struct
200 {
201 app_pwm_evt_type_t type; /**< Type of event. */
202 uint32_t error_code; /**< Error code */
203 } app_pwm_evt_t;
204
205 /**
206 * @brief PWM event callback definition
207 */
208 typedef void (*app_pwm_evt_handler_t)(app_pwm_evt_t *p_evt);
209 #endif
210
211 /**
212 * @brief PWM device structure definition
213 */
214 typedef struct
215 {
216 #ifdef APP_PWM_CODING_ENABLE
217 app_pwm_evt_handler_t evt_handler;
218 #endif
219 app_pwm_pin_cfg_t *p_pin_cfg; /**< PWM configuration Structures. */
220 app_pwm_state_t pwm_state; /**< App pwm state types. */
221 app_pwm_module_state_t pwm_module_state; /**< App pwm module state types. */
222 #ifdef APP_PWM_CODING_ENABLE
223 app_pwm_dma_state_t dma_state; /**< App pwm dma state. */
224 dma_id_t dma_id[1];
225 #endif
226 pwm_handle_t handle; /**< PWM handle Structure. */
227 } pwm_env_t;
228
229 /**
230 * @brief PWM parameters structure definition
231 */
232 typedef struct
233 {
234 app_pwm_id_t id; /**< specified PWM module ID. */
235 app_pwm_pin_cfg_t pin_cfg; /**< the pin configuration information for the specified PWM module. */
236 app_pwm_active_channel_t active_channel; /**< PWM operate mode. */
237 #ifdef APP_PWM_CODING_ENABLE
238 app_pwm_mode_t use_mode; /**< PWM operate mode. */
239 #endif
240 pwm_init_t init; /**< PWM communication parameters. */
241 pwm_env_t pwm_env; /**< PWM device structure definition. */
242 } app_pwm_params_t;
243 /** @} */
244
245 /* Exported functions --------------------------------------------------------*/
246 /** @addtogroup APP_PWM_DRIVER_FUNCTIONS Functions
247 * @{
248 */
249
250 /**
251 ****************************************************************************************
252 * @brief Initialize the APP PWM DRIVER.
253 *
254 * @param[in] p_params: Pointer to app_pwm_params_t parameter which contains the
255 * configuration information for the specified PWM module.
256 *
257 * @return Result of initialization.
258 ****************************************************************************************
259 */
260 #ifdef APP_PWM_CODING_ENABLE
261 uint16_t app_pwm_init(app_pwm_params_t *p_params, app_pwm_evt_handler_t evt_handler);
262 #else
263 uint16_t app_pwm_init(app_pwm_params_t *p_params);
264 #endif
265
266 /**
267 ****************************************************************************************
268 * @brief De-initialize the APP PWM DRIVER.
269 *
270 * @param[in] id: De-initialize for a specific ID.
271 *
272 * @return Result of De-initialization.
273 ****************************************************************************************
274 */
275 uint16_t app_pwm_deinit(app_pwm_id_t id);
276
277
278 /**
279 ****************************************************************************************
280 * @brief Starts the PWM signal generation on the output.
281 *
282 * @param[in] id: which PWM module want to output.
283 *
284 * @return Result of operation.
285 ****************************************************************************************
286 */
287 uint16_t app_pwm_start(app_pwm_id_t id);
288
289
290 /**
291 ****************************************************************************************
292 * @brief Stops the PWM signal generation on the output.
293 *
294 * @param[in] id: which PWM module want to stop output.
295 *
296 * @return Result of operation.
297 ****************************************************************************************
298 */
299 uint16_t app_pwm_stop(app_pwm_id_t id);
300
301 /**
302 ****************************************************************************************
303 * @brief Update the PWM frequency on the output.
304 *
305 * @param[in] id: which PWM module want to config.
306 * @param[in] freq: This parameter ranges between min = 0 and max = SystemFreq / 2.
307 *
308 * @return Result of operation.
309 ****************************************************************************************
310 */
311 uint16_t app_pwm_update_freq(app_pwm_id_t id, uint32_t freq);
312
313 /**
314 ****************************************************************************************
315 * @brief Initialize the PWM channels according to the specified parameters.
316 *
317 * @param[in] id: which PWM module want to config.
318 * @param[in] channel: PWM Channels to be configured.
319 * @param[in] p_config: PWM Channels configuration structure.
320 *
321 * @return Result of operation.
322 ****************************************************************************************
323 */
324 uint16_t app_pwm_config_channel(app_pwm_id_t id, app_pwm_active_channel_t channel, app_pwm_channel_init_t *p_config);
325
326 #ifdef APP_PWM_CODING_ENABLE
327 /**
328 ****************************************************************************************
329 * @brief Resume the PWM signal generation on the output.
330 *
331 * @param[in] id: which PWM module want to output.
332 *
333 * @return Result of operation.
334 ****************************************************************************************
335 */
336 uint16_t app_pwm_resume(app_pwm_id_t id);
337
338 /**
339 ****************************************************************************************
340 * @brief Pause the PWM signal generation on the output.
341 *
342 * @param[in] id: which PWM module want to stop output.
343 *
344 * @return Result of operation.
345 ****************************************************************************************
346 */
347 uint16_t app_pwm_pause(app_pwm_id_t id);
348 #endif
349
350 #ifdef APP_PWM_STOP_SPEC_CH_ENABLE
351 /**
352 ****************************************************************************************
353 * @brief Set the specified PWM channel inactive
354 *
355 * @param[in] id: PWM ID.
356 * @param[in] channel: PWM Channels to be configured.
357 *
358 * @return Result of operation.
359 ****************************************************************************************
360 */
361 uint16_t app_pwm_inactive_channel(app_pwm_id_t id, app_pwm_active_channel_t channel);
362 #endif
363
364 /**
365 ****************************************************************************************
366 * @brief Return the PWM handle.
367 *
368 * @param[in] id: PWM ID.
369 *
370 * @return Pointer to the specified ID's PWM handle.
371 ****************************************************************************************
372 */
373 pwm_handle_t *app_pwm_get_handle(app_pwm_id_t id);
374
375 #ifdef APP_PWM_CODING_ENABLE
376 /**
377 ****************************************************************************************
378 * @brief Set coding data in one channel mode
379 *
380 * @param[in] id: which PWM module want to config.
381 * @param[in] coding_data: every bit represent compa0 or compa1 in coding data.
382 *
383 * @return Result of operation.
384 ****************************************************************************************
385 */
386 uint16_t app_pwm_set_coding_data_in_one_channel(app_pwm_id_t id, uint32_t coding_data);
387
388 /**
389 ****************************************************************************************
390 * @brief Set coding data in three channels mode
391 *
392 * @param[in] id: which PWM module want to config.
393 * @param[in] coding_data0: every bit represent compa0 or compa1 in coding data0.
394 * @param[in] coding_data1: every bit represent compb0 or compb1 in coding data1.
395 * @param[in] coding_data2: every bit represent compc0 or compc1 in coding data2.
396 *
397 * @return Result of operation.
398 ****************************************************************************************
399 */
400 uint16_t app_pwm_set_coding_data_in_three_channels(app_pwm_id_t id, uint32_t coding_data0, uint32_t coding_data1, uint32_t coding_data2);
401
402 /**
403 ****************************************************************************************
404 * @brief Start generate one channel wave form in Interrupt mode
405 *
406 * @param[in] id: which PWM module want to config.
407 * @param[in] coding_data: every bit represent compa0 or compa1 in coding data.
408 *
409 * @return Result of operation.
410 ****************************************************************************************
411 */
412 uint16_t app_pwm_start_coding_in_one_channel(app_pwm_id_t id, uint32_t coding_data);
413
414 /**
415 ****************************************************************************************
416 * @brief Start generate three channels wave form in Interrupt mode
417 *
418 * @param[in] id: which PWM module want to config.
419 * @param[in] coding_data0: every bit represent compa0 or compa1 in coding data0.
420 * @param[in] coding_data1: every bit represent compb0 or compb1 in coding data1.
421 * @param[in] coding_data2: every bit represent compc0 or compc1 in coding data2.
422 *
423 * @return Result of operation.
424 ****************************************************************************************
425 */
426 uint16_t app_pwm_start_coding_in_three_channels(app_pwm_id_t id, uint32_t coding_data0, uint32_t coding_data1, uint32_t coding_data2);
427 #endif
428
429 /** @} */
430
431 #endif
432
433 #ifdef __cplusplus
434 }
435 #endif
436
437 #endif
438
439 /** @} */
440
441 /** @} */
442
443 /** @} */
444
APP_PWM_ACTIVE_CHANNEL_A
@ APP_PWM_ACTIVE_CHANNEL_A
Definition: app_pwm.h:95
app_pwm_channel_init_t
PWM Channel init Structure definition.
Definition: app_pwm.h:167
app_pwm_params_t::init
pwm_init_t init
Definition: app_pwm.h:240
app_pwm_pin_cfg_t::channel_a
app_pwm_pin_t channel_a
Definition: app_pwm.h:158
app_pwm_pin_cfg_t::channel_b
app_pwm_pin_t channel_b
Definition: app_pwm.h:159
app_pwm_get_handle
pwm_handle_t * app_pwm_get_handle(app_pwm_id_t id)
Return the PWM handle.
app_pwm_active_channel_t
app_pwm_active_channel_t
PWM active channel Enumerations definition.
Definition: app_pwm.h:94
app_pwm_pin_t
PWM IO configuration Structures.
Definition: app_pwm.h:144
pwm_init_t
PWM Init Structure definition.
Definition: hal_pwm.h:223
app_pwm_update_freq
uint16_t app_pwm_update_freq(app_pwm_id_t id, uint32_t freq)
Update the PWM frequency on the output.
app_pwm_pin_t::enable
uint8_t enable
Definition: app_pwm.h:150
app_pwm_params_t::pwm_env
pwm_env_t pwm_env
Definition: app_pwm.h:241
app_pwm_pin_t::pin
uint32_t pin
Definition: app_pwm.h:147
APP_PWM_ACTIVE_CHANNEL_ALL
@ APP_PWM_ACTIVE_CHANNEL_ALL
Definition: app_pwm.h:98
pwm_env_t::p_pin_cfg
app_pwm_pin_cfg_t * p_pin_cfg
Definition: app_pwm.h:219
app_pwm_init
uint16_t app_pwm_init(app_pwm_params_t *p_params)
Initialize the APP PWM DRIVER.
app_io_pull_t
app_io_pull_t
GPIO pull Enumerations definition.
Definition: app_io.h:176
app_io_type_t
app_io_type_t
GPIO type Enumerations definition.
Definition: app_io.h:142
APP_PWM_ACTIVE_CHANNEL_C
@ APP_PWM_ACTIVE_CHANNEL_C
Definition: app_pwm.h:97
app_pwm_state_t
app_pwm_state_t
App pwm state types.
Definition: app_pwm.h:104
app_pwm_params_t::id
app_pwm_id_t id
Definition: app_pwm.h:234
APP_PWM_ACTIVITY
@ APP_PWM_ACTIVITY
Definition: app_pwm.h:106
APP_PWM_ID_0
@ APP_PWM_ID_0
Definition: app_pwm.h:85
app_io.h
Header file containing functions prototypes of GPIO app library.
app_pwm_start
uint16_t app_pwm_start(app_pwm_id_t id)
Starts the PWM signal generation on the output.
app_pwm_pin_t::pull
app_io_pull_t pull
Definition: app_pwm.h:149
app_pwm_inactive_channel
uint16_t app_pwm_inactive_channel(app_pwm_id_t id, app_pwm_active_channel_t channel)
Set the specified PWM channel inactive.
app_pwm_channel_init_t::fstoplvl
uint32_t fstoplvl
Definition: app_pwm.h:174
grx_hal.h
This file contains all the functions prototypes for the HAL module driver.
app_pwm_id_t
app_pwm_id_t
PWM module Enumerations definition.
Definition: app_pwm.h:84
app_pwm_module_state_t
app_pwm_module_state_t
App pwm module state types.
Definition: app_pwm.h:115
APP_PWM_START
@ APP_PWM_START
Definition: app_pwm.h:117
APP_PWM_ID_1
@ APP_PWM_ID_1
Definition: app_pwm.h:86
pwm_handle_t
PWM handle Structure definition.
Definition: hal_pwm.h:247
app_pwm_stop
uint16_t app_pwm_stop(app_pwm_id_t id)
Stops the PWM signal generation on the output.
pwm_env_t::pwm_module_state
app_pwm_module_state_t pwm_module_state
Definition: app_pwm.h:221
APP_PWM_ACTIVE_CHANNEL_B
@ APP_PWM_ACTIVE_CHANNEL_B
Definition: app_pwm.h:96
app_pwm_channel_init_t::duty
uint8_t duty
Definition: app_pwm.h:168
app_pwm_channel_init_t::drive_polarity
uint8_t drive_polarity
Definition: app_pwm.h:171
app_dma.h
Header file containing functions prototypes of DMA app library.
app_pwm_params_t
PWM parameters structure definition.
Definition: app_pwm.h:233
pwm_env_t
PWM device structure definition.
Definition: app_pwm.h:215
app_pwm_pin_cfg_t
PWM configuration Structures.
Definition: app_pwm.h:157
app_pwm_config_channel
uint16_t app_pwm_config_channel(app_pwm_id_t id, app_pwm_active_channel_t channel, app_pwm_channel_init_t *p_config)
Initialize the PWM channels according to the specified parameters.
link_node::channel
uint32_t channel
Definition: app_adc.h:166
app_pwm_pin_t::type
app_io_type_t type
Definition: app_pwm.h:145
pwm_env_t::pwm_state
app_pwm_state_t pwm_state
Definition: app_pwm.h:220
pwm_env_t::handle
pwm_handle_t handle
Definition: app_pwm.h:226
app_io_mux_t
app_io_mux_t
GPIO mux Enumerations definition.
Definition: app_io.h:248
app_pwm_params_t::pin_cfg
app_pwm_pin_cfg_t pin_cfg
Definition: app_pwm.h:235
APP_PWM_STOP
@ APP_PWM_STOP
Definition: app_pwm.h:116
APP_PWM_ID_MAX
@ APP_PWM_ID_MAX
Definition: app_pwm.h:87
app_pwm_deinit
uint16_t app_pwm_deinit(app_pwm_id_t id)
De-initialize the APP PWM DRIVER.
app_pwm_pin_cfg_t::channel_c
app_pwm_pin_t channel_c
Definition: app_pwm.h:160
app_pwm_params_t::active_channel
app_pwm_active_channel_t active_channel
Definition: app_pwm.h:236
app_drv_error.h
Header file of app driver error code.
app_drv_config.h
Header file of app driver config code.
APP_PWM_INVALID
@ APP_PWM_INVALID
Definition: app_pwm.h:105
app_pwm_pin_t::mux
app_io_mux_t mux
Definition: app_pwm.h:146
APP_PWM_ACTIVE_CHANNEL_CLEARED
@ APP_PWM_ACTIVE_CHANNEL_CLEARED
Definition: app_pwm.h:99
dma_id_t
int16_t dma_id_t
DMA id definition.
Definition: app_dma.h:98
dma_channel_t
dma_channel_t
HAL DMA Channel Enumerations definition.
Definition: hal_dma.h:95