1 // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
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 /*******************************************************************************
16  * NOTICE
17  * The hal is not public api, don't use in application code.
18  * See readme.md in hal/include/hal/readme.md
19  ******************************************************************************/
20 
21 // The HAL layer for LEDC.
22 // There is no parameter check in the hal layer, so the caller must ensure the correctness of the parameters.
23 
24 #pragma once
25 
26 #include "hal/ledc_ll.h"
27 #include "hal/ledc_types.h"
28 
29 /**
30  * Context that should be maintained by both the driver and the HAL
31  */
32 typedef struct {
33     ledc_dev_t *dev;
34     ledc_mode_t speed_mode;
35 } ledc_hal_context_t;
36 
37 /**
38  * @brief Set LEDC low speed timer clock
39  *
40  * @param hal Context of the HAL layer
41  * @param slow_clk_sel LEDC low speed timer clock source
42  *
43  * @return None
44  */
45 #define ledc_hal_set_slow_clk_sel(hal, slow_clk_sel)  ledc_ll_set_slow_clk_sel((hal)->dev, slow_clk_sel)
46 
47 /**
48  * @brief Get LEDC low speed timer clock
49  *
50  * @param hal Context of the HAL layer
51  * @param slow_clk_sel LEDC low speed timer clock source
52  *
53  * @return None
54  */
55 #define ledc_hal_get_slow_clk_sel(hal, slow_clk_sel)  ledc_ll_get_slow_clk_sel((hal)->dev, slow_clk_sel)
56 
57 /**
58  * @brief Update LEDC low speed timer
59  *
60  * @param hal Context of the HAL layer
61  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
62  *
63  * @return None
64  */
65 #define ledc_hal_ls_timer_update(hal, timer_sel)  ledc_ll_ls_timer_update((hal)->dev, (hal)->speed_mode, timer_sel)
66 
67 /**
68  * @brief Reset LEDC timer
69  *
70  * @param hal Context of the HAL layer
71  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
72  *
73  * @return None
74  */
75 #define ledc_hal_timer_rst(hal, timer_sel)  ledc_ll_timer_rst((hal)->dev, (hal)->speed_mode, timer_sel)
76 
77 /**
78  * @brief Pause LEDC timer
79  *
80  * @param hal Context of the HAL layer
81  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
82  *
83  * @return None
84  */
85 #define ledc_hal_timer_pause(hal, timer_sel)  ledc_ll_timer_pause((hal)->dev, (hal)->speed_mode, timer_sel)
86 
87 /**
88  * @brief Resume LEDC timer
89  *
90  * @param hal Context of the HAL layer
91  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
92  *
93  * @return None
94  */
95 #define ledc_hal_timer_resume(hal, timer_sel)  ledc_ll_timer_resume((hal)->dev, (hal)->speed_mode, timer_sel)
96 
97 /**
98  * @brief Set LEDC timer clock divider
99  *
100  * @param hal Context of the HAL layer
101  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
102  * @param clock_divider Timer clock divide value, the timer clock is divided from the selected clock source
103  *
104  * @return None
105  */
106 #define ledc_hal_set_clock_divider(hal, timer_sel, clock_divider)  ledc_ll_set_clock_divider((hal)->dev, (hal)->speed_mode, timer_sel, clock_divider)
107 
108 /**
109  * @brief Get LEDC timer clock divider
110  *
111  * @param hal Context of the HAL layer
112  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
113  * @param clock_divider Timer clock divide value, the timer clock is divided from the selected clock source
114  *
115  * @return None
116  */
117 #define ledc_hal_get_clock_divider(hal, timer_sel, clock_divider)  ledc_ll_get_clock_divider((hal)->dev, (hal)->speed_mode, timer_sel, clock_divider)
118 
119 /**
120  * @brief Set LEDC timer clock source
121  *
122  * @param hal Context of the HAL layer
123  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
124  * @param clk_src Timer clock source
125  *
126  * @return None
127  */
128 #define ledc_hal_set_clock_source(hal, timer_sel, clk_src)  ledc_ll_set_clock_source((hal)->dev, (hal)->speed_mode, timer_sel, clk_src)
129 
130 /**
131  * @brief Get LEDC timer clock source
132  *
133  * @param hal Context of the HAL layer
134  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
135  * @param clk_src Pointer to accept the timer clock source
136  *
137  * @return None
138  */
139 #define ledc_hal_get_clock_source(hal, timer_sel, clk_src)  ledc_ll_get_clock_source((hal)->dev, (hal)->speed_mode, timer_sel, clk_src)
140 
141 /**
142  * @brief Set LEDC duty resolution
143  *
144  * @param hal Context of the HAL layer
145  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
146  * @param duty_resolution Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution)]
147  *
148  * @return None
149  */
150 #define ledc_hal_set_duty_resolution(hal, timer_sel, duty_resolution)  ledc_ll_set_duty_resolution((hal)->dev, (hal)->speed_mode, timer_sel, duty_resolution)
151 
152 /**
153  * @brief Get LEDC duty resolution
154  *
155  * @param hal Context of the HAL layer
156  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
157  * @param duty_resolution Pointer to accept the resolution of duty setting in number of bits.
158  *
159  * @return None
160  */
161 #define ledc_hal_get_duty_resolution(hal, timer_sel, duty_resolution)  ledc_ll_get_duty_resolution((hal)->dev, (hal)->speed_mode, timer_sel, duty_resolution)
162 
163 /**
164  * @brief Get LEDC max duty
165  *
166  * @param hal Context of the HAL layer
167  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
168  * @param max_duty Pointer to accept the max duty
169  *
170  * @return None
171  */
172 #define ledc_hal_get_max_duty(hal, channel_num, max_duty)  ledc_ll_get_max_duty((hal)->dev, (hal)->speed_mode, channel_num, max_duty)
173 
174 /**
175  * @brief Get LEDC hpoint value
176  *
177  * @param hal Context of the HAL layer
178  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
179  * @param hpoint_val Pointer to accept the LEDC hpoint value(max: 0xfffff)
180  *
181  * @return None
182  */
183 #define ledc_hal_get_hpoint(hal, channel_num, hpoint_val)  ledc_ll_get_hpoint((hal)->dev, (hal)->speed_mode, channel_num, hpoint_val)
184 
185 /**
186  * @brief Set LEDC the integer part of duty value
187  *
188  * @param hal Context of the HAL layer
189  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
190  * @param duty_val LEDC duty value, the range of duty setting is [0, (2**duty_resolution)]
191  *
192  * @return None
193  */
194 #define ledc_hal_set_duty_int_part(hal, channel_num, duty_val)  ledc_ll_set_duty_int_part((hal)->dev, (hal)->speed_mode, channel_num, duty_val)
195 
196 /**
197  * @brief Set the output enable
198  *
199  * @param hal Context of the HAL layer
200  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
201  * @param sig_out_en The output enable status
202  *
203  * @return None
204  */
205 #define ledc_hal_set_sig_out_en(hal, channel_num, sig_out_en)  ledc_ll_set_sig_out_en((hal)->dev, (hal)->speed_mode, channel_num, sig_out_en)
206 
207 /**
208  * @brief Set the duty start
209  *
210  * @param hal Context of the HAL layer
211  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
212  * @param duty_start The duty start
213  *
214  * @return None
215  */
216 #define ledc_hal_set_duty_start(hal, channel_num, duty_start)  ledc_ll_set_duty_start((hal)->dev, (hal)->speed_mode, channel_num, duty_start)
217 
218 /**
219  * @brief Set output idle level
220  *
221  * @param hal Context of the HAL layer
222  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
223  * @param idle_level The output idle level
224  *
225  * @return None
226  */
227 #define ledc_hal_set_idle_level(hal, channel_num, idle_level)  ledc_ll_set_idle_level((hal)->dev, (hal)->speed_mode, channel_num, idle_level)
228 
229 /**
230  * @brief Set fade end interrupt enable
231  *
232  * @param hal Context of the HAL layer
233  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
234  * @param fade_end_intr_en The fade end interrupt enable status
235  *
236  * @return None
237  */
238 #define ledc_hal_set_fade_end_intr(hal, channel_num, fade_end_intr_en)  ledc_ll_set_fade_end_intr((hal)->dev, (hal)->speed_mode, channel_num, fade_end_intr_en)
239 
240 /**
241  * @brief Set timer index of the specified channel
242  *
243  * @param hal Context of the HAL layer
244  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
245  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
246  *
247  * @return None
248  */
249 #define ledc_hal_bind_channel_timer(hal, channel_num, timer_sel)  ledc_ll_bind_channel_timer((hal)->dev, (hal)->speed_mode, channel_num, timer_sel)
250 
251 /**
252  * @brief Get timer index of the specified channel
253  *
254  * @param hal Context of the HAL layer
255  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
256  * @param timer_sel Pointer to accept the LEDC timer index
257  *
258  * @return None
259  */
260 #define ledc_hal_get_channel_timer(hal, channel_num, timer_sel)  ledc_ll_get_channel_timer((hal)->dev, (hal)->speed_mode, channel_num, timer_sel)
261 
262 /**
263  * @brief Init the LEDC hal. This function should be called first before other hal layer function is called
264  *
265  * @param hal Context of the HAL layer
266  * @param speed_mode speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mod
267  *
268  * @return None
269  */
270 void ledc_hal_init(ledc_hal_context_t *hal, ledc_mode_t speed_mode);
271 
272 /**
273  * @brief Update channel configure when select low speed mode
274  *
275  * @param hal Context of the HAL layer
276  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
277  *
278  * @return None
279  */
280 void ledc_hal_ls_channel_update(ledc_hal_context_t *hal, ledc_channel_t channel_num);
281 
282 /**
283  * @brief Set LEDC hpoint value
284  *
285  * @param hal Context of the HAL layer
286  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
287  * @param hpoint_val LEDC hpoint value(max: 0xfffff)
288  *
289  * @return None
290  */
291 void ledc_hal_set_hpoint(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t hpoint_val);
292 
293 /**
294  * @brief Get LEDC duty value
295  *
296  * @param hal Context of the HAL layer
297  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
298  * @param duty_val Pointer to accept the LEDC duty value
299  *
300  * @return None
301  */
302 void ledc_hal_get_duty(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t *duty_val);
303 
304 /**
305  * @brief Set LEDC duty change direction
306  *
307  * @param hal Context of the HAL layer
308  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
309  * @param duty_direction LEDC duty change direction, increase or decrease
310  *
311  * @return None
312  */
313 void ledc_hal_set_duty_direction(ledc_hal_context_t *hal, ledc_channel_t channel_num, ledc_duty_direction_t duty_direction);
314 
315 /**
316  * @brief Set the number of increased or decreased times
317  *
318  * @param hal Context of the HAL layer
319  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
320  * @param duty_num The number of increased or decreased times
321  *
322  * @return None
323  */
324 void ledc_hal_set_duty_num(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t duty_num);
325 
326 /**
327  * @brief Set the duty cycles of increase or decrease
328  *
329  * @param hal Context of the HAL layer
330  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
331  * @param duty_cycle The duty cycles
332  *
333  * @return None
334  */
335 void ledc_hal_set_duty_cycle(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t duty_cycle);
336 
337 /**
338  * @brief Set the step scale of increase or decrease
339  *
340  * @param hal Context of the HAL layer
341  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
342  * @param duty_scale The step scale
343  *
344  * @return None
345  */
346 void ledc_hal_set_duty_scale(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t duty_scale);
347 
348 /**
349  * @brief Get interrupt status of the specified channel
350  *
351  * @param hal Context of the HAL layer
352  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
353  * @param intr_status Pointer to accept the interrupt status
354  *
355  * @return None
356  */
357 void ledc_hal_get_fade_end_intr_status(ledc_hal_context_t *hal, uint32_t *intr_status);
358 
359 /**
360  * @brief Clear interrupt status of the specified channel
361  *
362  * @param hal Context of the HAL layer
363  * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
364  *
365  * @return None
366  */
367 void ledc_hal_clear_fade_end_intr_status(ledc_hal_context_t *hal, ledc_channel_t channel_num);
368 
369 /**
370  * @brief Get clock config of LEDC timer
371  *
372  * @param hal Context of the HAL layer
373  * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
374  * @param clk_cfg Pointer to accept clock config
375  *
376  * @return None
377  */
378 void ledc_hal_get_clk_cfg(ledc_hal_context_t *hal, ledc_timer_t timer_sel, ledc_clk_cfg_t *clk_cfg);
379 
380 /**
381  * @brief Config low speed timer clock source with clock config
382  *s
383  * @param hal Context of the HAL layer
384  * @param clk_cfg clock config
385  *
386  * @return None
387  */
388 void ledc_hal_set_slow_clk(ledc_hal_context_t *hal, ledc_clk_cfg_t clk_cfg);
389