• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 #pragma once
16 #include <common/bk_include.h>
17 #include <driver/hal/hal_pwm_types.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24  * @brief PWM APIs
25  * @addtogroup bk_api_pwm PWM API group
26  * @{
27  */
28 
29 /**
30  * @brief PWM defines
31  * @defgroup bk_api_pwm_defs macos
32  * @ingroup bk_api_pwm
33  * @{
34  */
35 
36 #define BK_ERR_PWM_CLOCK           (BK_ERR_PWM_BASE - 1) /**< PWM global clock type not supported */
37 #define BK_ERR_PWM_CHAN_CLOCK      (BK_ERR_PWM_BASE - 2) /**< PWM channel clock type not supported */
38 #define BK_ERR_PWM_CHAN_ID         (BK_ERR_PWM_BASE - 3) /**< PWM channel number is invalid */
39 #define BK_ERR_PWM_PERIOD_DUTY     (BK_ERR_PWM_BASE - 4) /**< PWM all duty cycle > period cycle */
40 #define BK_ERR_PWM_CAPTURE_EDGE    (BK_ERR_PWM_BASE - 5) /**< PWM capture edge is invalid */
41 #define BK_ERR_PWM_NOT_INIT        (BK_ERR_PWM_BASE - 6) /**< PWM driver not init */
42 #define BK_ERR_PWM_CHAN_NOT_INIT   (BK_ERR_PWM_BASE - 7) /**< PWM channel not init */
43 #define BK_ERR_PWM_CHAN_NOT_START  (BK_ERR_PWM_BASE - 8) /**< PWM channel not init */
44 #define BK_ERR_PWM_GROUP_ID        (BK_ERR_PWM_BASE - 9) /**< PWM group ID is invalid */
45 #define BK_ERR_PWM_GROUP_EXIST     (BK_ERR_PWM_BASE - 10) /**< PWM group already exists */
46 #define BK_ERR_PWM_GROUP_CHAN_USED (BK_ERR_PWM_BASE - 11) /**< PWM channel is used by other group */
47 #define BK_ERR_PWM_GROUP_NOT_EXIST (BK_ERR_PWM_BASE - 12) /**< PWM group doesn't exist */
48 #define BK_ERR_PWM_GROUP_DUTY      (BK_ERR_PWM_BASE - 13) /**< PWM group cycle invalid */
49 #define BK_ERR_PWM_GROUP_SAME_CHAN (BK_ERR_PWM_BASE - 14) /**< PWM group channel is same */
50 #define BK_ERR_PWM_INVALID_GPIO_MODE (BK_ERR_PWM_BASE - 15) /**< PWM invalid gpio mode */
51 
52 /**
53  * @brief default PWM configuration
54  */
55 #define PWM_DEFAULT_PWM_CONFIG() {\
56 	.period_cycle = 0,\
57 	.duty_cycle = 0,\
58 	.duty2_cycle = 0,\
59 	.duty3_cycle = 0,\
60 }
61 
62 /**
63  * @brief default PWM capture configuration
64  */
65 #define PWM_DEFAULT_PWM_CAPTURE_CONFIG() {\
66 	.isr = NULL,\
67 }
68 
69 typedef struct {
70 	uint32_t period_cycle;  /**< PWM period cycle, unit is (1/clock_frequency) */
71 	uint32_t duty_cycle;    /**< PWM duty cycle, unit is (1/clock_frequency) */
72 	uint32_t duty2_cycle;   /**< PWM duty2 cycle, unit is (1/clock_frequency) */
73 	uint32_t duty3_cycle;   /**< PWM duty2 cycle, unit is (1/clock_frequency) */
74 	uint32_t reserved[4];   /**< Reserved for future extend, must set to 0 */
75 } pwm_init_config_t;
76 
77 typedef struct {
78 	uint32_t period_cycle;  /**< PWM period cycle, unit is (1/clock_frequency) */
79 	uint32_t duty_cycle;    /**< PWM duty cycle, unit is (1/clock_frequency) */
80 	uint32_t duty2_cycle;   /**< PWM duty2 cycle, unit is (1/clock_frequency) */
81 	uint32_t duty3_cycle;   /**< PWM duty3 cycle, unit is (1/clock_frequency) */
82 } pwm_period_duty_config_t;
83 
84 /**
85  * @brief PWM interrupt service routine
86  */
87 typedef void (*pwm_isr_t)(pwm_chan_t chan);
88 
89 typedef struct {
90 	pwm_capture_edge_t edge; /**< PWM chan capture edge */
91 	pwm_isr_t isr;           /**< PWM capture interrupt routine */
92 } pwm_capture_init_config_t;
93 
94 typedef struct {
95 	pwm_chan_t chan1;          /**< The first PWM channel ID in a group */
96 	pwm_chan_t chan2;          /**< The second PWM channel ID in a group */
97 	uint32_t period_cycle;     /**< PWM Group period cycle */
98 	uint32_t chan1_duty_cycle; /**< Duty cycle of chan1 */
99 	uint32_t chan2_duty_cycle; /**< Duty cycle of chan2 */
100 } pwm_group_init_config_t;
101 
102 typedef struct {
103 	uint32_t period_cycle;     /**< PWM Group period cycle */
104 	uint32_t chan1_duty_cycle; /**< Duty cycle of channel 1 */
105 	uint32_t chan2_duty_cycle; /**< Duty cycle of channel 2 */
106 } pwm_group_config_t;
107 
108 /**
109  * @brief PWM group type
110  */
111 typedef uint8_t pwm_group_t;
112 
113 #define PWM_GROUP_ID_INVALID       (0xFF) /**< Invalid PWM group ID */
114 
115 /**
116  * @}
117  */
118 
119 /**
120  * @}
121  */
122 
123 #ifdef __cplusplus
124 }
125 #endif
126