• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 HiSilicon (Shanghai) Technologies CO., LIMITED.
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  * Description: Provides HAL PWM \n
16  *
17  * History: \n
18  * 2022-09-16, Create file. \n
19  */
20 #ifndef HAL_PWM_H
21 #define HAL_PWM_H
22 
23 #include <stdint.h>
24 #include "errcode.h"
25 #include "pwm_porting.h"
26 
27 #ifdef __cplusplus
28 #if __cplusplus
29 extern "C" {
30 #endif /* __cplusplus */
31 #endif /* __cplusplus */
32 
33 /**
34  * @defgroup drivers_hal_pwm_api PWM
35  * @ingroup  drivers_hal_pwm
36  * @{
37  */
38 
39 /**
40  * @if Eng
41  * @brief  Definition of PWM actions.
42  * @else
43  * @brief  PWM动作模式定义。
44  * @endif
45  */
46 typedef enum {
47     PWM_ACTION_START,
48     PWM_ACTION_STOP,
49     PWM_ACTION_REFRESH,
50     PWM_ACTION_CONTINUE_SET,
51     PWM_ACTION_CONTINUE_CLR
52 } pwm_action_t;
53 
54 /**
55  * @if Eng
56  * @brief  Definition of the set low/high level time ID of hal PWM.
57  * @else
58  * @brief  PWM设置高低电平时间ID定义。
59  * @endif
60  */
61 typedef enum hal_pwm_set_time_id {
62     PWM_SET_LOW_TIME = 0,       /*!< @if Eng Set PWM low level time.
63                                      @else   设置PWM低电平时间。 @endif */
64     PWM_SET_HIGH_TIME,          /*!< @if Eng Set  PWM high level time.
65                                      @else   设置PWM高电平时间。 @endif */
66     PWM_SET_OFFSET_TIME,        /*!< @if Eng PWM offset time.
67                                      @else   PWM相位。 @endif */
68     PWM_SET_TIEM_MAX,
69     PWM_SET_TIME_ID_INVALID = 0xFF
70 } hal_pwm_set_time_id_t;
71 
72  /**
73  * @if Eng
74  * @brief  Definition of hal PWM callback type.
75  * @param  [in]  channel PWM device.
76  * @retval ERRCODE_SUCC   Success.
77  * @retval Other        Failure. For details, see @ref errcode_t.
78  * @else
79  * @brief  PWM hal层回调类型定义。
80  * @param  [in]  channel PWM设备。
81  * @retval ERRCODE_SUCC 成功。
82  * @retval Other        失败,参考 @ref errcode_t 。
83  * @endif
84  */
85 typedef errcode_t (*hal_pwm_callback_t)(uint8_t channel);
86 
87 /**
88  * @if Eng
89  * @brief  Initialize device for hal PWM.
90  * @retval ERRCODE_SUCC   Success.
91  * @retval Other        Failure. For details, see @ref errcode_t.
92  * @else
93  * @brief  HAL层PWM的初始化接口。
94  * @retval ERRCODE_SUCC 成功。
95  * @retval Other        失败,参考 @ref errcode_t 。
96  * @endif
97  */
98 typedef errcode_t (*hal_pwm_init_t)(void);
99 
100 /**
101  * @if Eng
102  * @brief  Deinitialize device for hal PWM.
103  * @retval ERRCODE_SUCC   Success.
104  * @retval Other        Failure. For details, see @ref errcode_t.
105  * @else
106  * @brief  HAL层PWM的去初始化接口。
107  * @retval ERRCODE_SUCC 成功。
108  * @retval Other        失败,参考 @ref errcode_t 。
109  * @endif
110  */
111 typedef void (*hal_pwm_deinit_t)(void);
112 
113  /**
114  * @if Eng
115  * @brief  Set the PWM low/high level time.
116  * @param  [in]  id Low/high level time ID.
117  * @param  [in]  channel PWM device.
118  * @param  [in]  time  The value to set the low/high time of PWM.
119  * @else
120  * @brief  设置PWM高低电平时间。
121  * @param  [in]  id 高低电平时间ID。
122  * @param  [in]  channel PWM设备。
123  * @param  [in]  time  用于设置PWM高低电平时间的值。
124  * @endif
125  */
126 typedef void (*hal_pwm_set_time_t)(hal_pwm_set_time_id_t id, pwm_channel_t channel, uint32_t time);
127 
128  /**
129  * @if Eng
130  * @brief  Set the PWM repeat cycles.
131  * @param  [in]  channel PWM device.
132  * @param  [in]  sycles  The value to set repeat cycles.
133  * @else
134  * @brief  设置PWM重复周期。
135  * @param  [in]  channel PWM设备。
136  * @param  [in]  sycles  重复周期值。
137  * @endif
138  */
139 typedef void (*hal_pwm_set_cycles_t)(pwm_channel_t channel, uint16_t sycles);
140 
141  /**
142  * @if Eng
143  * @brief  Set the PWM action.
144  * @param  [in]  channel PWM device.
145  * @param  [in]  action  PWM action.
146  * @else
147  * @brief  设置PWM动作模式。
148  * @param  [in]  channel PWM设备。
149  * @param  [in]  action  PWM动作模式。
150  * @endif
151  */
152 typedef void (*hal_pwm_set_action_t)(uint8_t channel, pwm_action_t action);
153 
154  /**
155  * @if Eng
156  * @brief  Clear the PWM interrupt.
157  * @param  [in]  channel PWM device.
158  * @else
159  * @brief  清除PWM中断。
160  * @param  [in]  channel PWM设备。
161  * @endif
162  */
163 typedef void (*hal_pwm_clear_t)(pwm_channel_t channel);
164 
165  /**
166  * @if Eng
167  * @brief  Register a callback asociated with a PWM interrupt cause.
168  * @param  [in]  channel PWM device.
169  * @param  [in]  callback  The interrupt callback to register.
170  * @else
171  * @brief  注册与PWM中断原因关联的回调。
172  * @param  [in]  channel PWM设备。
173  * @param  [in]  callback  寄存器的中断回调。
174  * @endif
175  */
176 typedef void (*hal_pwm_register_t)(pwm_channel_t channel, hal_pwm_callback_t callback);
177 
178  /**
179  * @if Eng
180  * @brief  Unregister a previously registered callback for a PWM device.
181  * @param  [in]  channel PWM device.
182  * @else
183  * @brief  为PWM设备注销先前注册的回调。
184  * @param  [in]  channel PWM设备。
185  * @endif
186  */
187 typedef void (*hal_pwm_unregister_t)(pwm_channel_t channel);
188 
189 #if defined(CONFIG_PWM_USING_V151)
190  /**
191  * @if Eng
192  * @brief  Assigning channels to PWM groups, one group can have more than one channel,
193  *         the same channel cannot be in different groups at the same time.
194  * @param  [in]  group PWM group.
195  * @param  [in]  channel PWM device.
196  * @else
197  * @brief  为PWM组分配通道,一个组可以有多个通道,同一个通道不能同时位于不同的组里。
198  * @param  [in]  group   PWM组。
199  * @param  [in]  channel PWM设备。
200  * @endif
201  */
202 typedef void (*hal_pwm_set_group_t)(pwm_v151_group_t group, uint16_t channel);
203 
204 #if defined(CONFIG_PWM_PRELOAD)
205   /**
206  * @if Eng
207  * @brief  config pwm preload param.
208  * @param  [in]  group PWM group.
209  * @else
210  * @brief  为PWM配置预加载参数。
211  * @param  [in]  group   PWM组。
212  * @endif
213  */
214 typedef void (*hal_pwm_config_preload_t)(pwm_v151_group_t group);
215 #endif /* CONFIG_PWM_PRELOAD */
216 #endif /* CONFIG_PWM_USING_V151 */
217 
218 /**
219  * @if Eng
220  * @brief  Interface between PWM driver and PWM hal.
221  * @else
222  * @brief  Driver层PWM和HAL层PWM的接口。
223  * @endif
224  */
225 typedef struct {
226     hal_pwm_init_t   init;                      /*!< @if Eng Init device interface.
227                                                     @else   HAL层PWM的初始化接口。 @endif */
228     hal_pwm_deinit_t deinit;                    /*!< @if Eng Deinit device interface.
229                                                     @else   HAL层PWM去初始化接口。 @endif */
230     hal_pwm_set_time_t   set_time;              /*!< @if Eng Control device interface.
231                                                     @else   HAL层PWM设置高低电平时间接口。 @endif */
232     hal_pwm_set_cycles_t  set_cycles;           /*!< @if Eng Control device interface.
233                                                     @else   HAL层PWM设置周期接口。 @endif */
234     hal_pwm_set_action_t  set_action;           /*!< @if Eng Control device interface.
235                                                     @else   HAL层PWM设置动作模式接口。 @endif */
236     hal_pwm_clear_t   isrclear;                 /*!< @if Eng Control device interface.
237                                                     @else   HAL层PWM清除中断接口。 @endif */
238     hal_pwm_register_t   registerfunc;          /*!< @if Eng Control device interface.
239                                                     @else   HAL层PWM注册回调函数接口。 @endif */
240     hal_pwm_unregister_t   unregisterfunc;      /*!< @if Eng Control device interface.
241                                                     @else   HAL层PWM去注册回调函数接口。 @endif */
242 #if defined(CONFIG_PWM_USING_V151)
243     hal_pwm_set_group_t   set_group;            /*!< @if Eng Control device interface.
244                                                     @else   HAL层PWM通道分组函数接口。 @endif */
245 #if defined(CONFIG_PWM_PRELOAD)
246     hal_pwm_config_preload_t    config_preload;
247 #endif /* CONFIG_PWM_PRELOAD */
248 #endif /* CONFIG_PWM_USING_V151 */
249 } hal_pwm_funcs_t;
250 
251 /**
252  * @if Eng
253  * @brief  Register @ref hal_pwm_funcs_t into the g_hal_pwms_funcs.
254  * @param  [out] funcs Interface between PWM driver and PWM hal.
255  * @retval ERRCODE_SUCC   Success.
256  * @retval Other        Failure. For details, see @ref errcode_t.
257  * @else
258  * @brief  注册 @ref hal_pwm_funcs_t 到 g_hal_pwms_funcs 。
259  * @param  [out] funcs Driver层PWM和HAL层PWM的接口实例。
260  * @retval ERRCODE_SUCC 成功。
261  * @retval Other        失败,参考 @ref errcode_t 。
262  * @endif
263  */
264 errcode_t hal_pwm_register_funcs(hal_pwm_funcs_t *funcs);
265 
266 /**
267  * @if Eng
268  * @brief  Unregister @ref hal_pwm_funcs_t from the g_hal_pwms_funcs.
269  * @return ERRCODE_SUCC   Success.
270  * @retval Other        Failure. For details, see @ref errcode_t.
271  * @else
272  * @brief  从g_hal_pwms_funcs注销 @ref hal_pwm_funcs_t 。
273  * @return ERRCODE_SUCC 成功。
274  * @retval Other        失败,参考 @ref errcode_t 。
275  * @endif
276  */
277 errcode_t hal_pwm_unregister_funcs(void);
278 
279 /**
280  * @if Eng
281  * @brief  Get interface between PWM driver and PWM hal, see @ref hal_pwm_funcs_t.
282  * @return Interface between PWM driver and PWM hal, see @ref hal_pwm_funcs_t.
283  * @else
284  * @brief  获取Driver层pwm和HAL层PWM的接口实例,参考 @ref hal_pwm_funcs_t 。
285  * @return Driver层pwm和HAL层PWM的接口实例,参考 @ref hal_pwm_funcs_t 。
286  * @endif
287  */
288 hal_pwm_funcs_t *hal_pwm_get_funcs(void);
289 
290 /**
291  * @if Eng
292  * @brief  Init the pwm which will set the base address of registers.
293  * @retval ERRCODE_SUCC   Success.
294  * @retval Other          Failure. For details, see @ref errcode_t.
295  * @else
296  * @brief  初始化PWM,设置寄存器的基地址。
297  * @retval ERRCODE_SUCC 成功。
298  * @retval Other        失败,参考 @ref errcode_t 。
299  * @endif
300  */
301 errcode_t hal_pwm_regs_init(void);
302 
303 /**
304  * @if Eng
305  * @brief  Deinit the hal_drv_pwm which will clear the base address of registers has been
306  *         set by @ref hal_pwm_regs_init.
307  * @else
308  * @brief  去初始化,然后清除在 @ref hal_pwm_regs_init 中设置的寄存器地址。
309  * @endif
310  */
311 void hal_pwm_regs_deinit(void);
312 
313 /**
314  * @}
315  */
316 
317 #ifdef __cplusplus
318 #if __cplusplus
319 }
320 #endif /* __cplusplus */
321 #endif /* __cplusplus */
322 
323 #endif