ledc_hal.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. /*******************************************************************************
  14. * NOTICE
  15. * The hal is not public api, don't use in application code.
  16. * See readme.md in soc/include/hal/readme.md
  17. ******************************************************************************/
  18. // The HAL layer for LEDC.
  19. // There is no parameter check in the hal layer, so the caller must ensure the correctness of the parameters.
  20. #pragma once
  21. #include "hal/ledc_ll.h"
  22. #include "hal/ledc_types.h"
  23. /**
  24. * Context that should be maintained by both the driver and the HAL
  25. */
  26. typedef struct {
  27. ledc_dev_t *dev;
  28. ledc_mode_t speed_mode;
  29. } ledc_hal_context_t;
  30. /**
  31. * @brief Set LEDC low speed timer clock
  32. *
  33. * @param hal Context of the HAL layer
  34. * @param slow_clk_sel LEDC low speed timer clock source
  35. *
  36. * @return None
  37. */
  38. #define ledc_hal_set_slow_clk_sel(hal, slow_clk_sel) ledc_ll_set_slow_clk_sel((hal)->dev, slow_clk_sel)
  39. /**
  40. * @brief Get LEDC low speed timer clock
  41. *
  42. * @param hal Context of the HAL layer
  43. * @param slow_clk_sel LEDC low speed timer clock source
  44. *
  45. * @return None
  46. */
  47. #define ledc_hal_get_slow_clk_sel(hal, slow_clk_sel) ledc_ll_get_slow_clk_sel((hal)->dev, slow_clk_sel)
  48. /**
  49. * @brief Update LEDC low speed timer
  50. *
  51. * @param hal Context of the HAL layer
  52. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  53. *
  54. * @return None
  55. */
  56. #define ledc_hal_ls_timer_update(hal, timer_sel) ledc_ll_ls_timer_update((hal)->dev, (hal)->speed_mode, timer_sel)
  57. /**
  58. * @brief Reset LEDC 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_timer_rst(hal, timer_sel) ledc_ll_timer_rst((hal)->dev, (hal)->speed_mode, timer_sel)
  66. /**
  67. * @brief Pause LEDC timer
  68. *
  69. * @param hal Context of the HAL layer
  70. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  71. *
  72. * @return None
  73. */
  74. #define ledc_hal_timer_pause(hal, timer_sel) ledc_ll_timer_pause((hal)->dev, (hal)->speed_mode, timer_sel)
  75. /**
  76. * @brief Resume LEDC timer
  77. *
  78. * @param hal Context of the HAL layer
  79. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  80. *
  81. * @return None
  82. */
  83. #define ledc_hal_timer_resume(hal, timer_sel) ledc_ll_timer_resume((hal)->dev, (hal)->speed_mode, timer_sel)
  84. /**
  85. * @brief Set LEDC timer clock divider
  86. *
  87. * @param hal Context of the HAL layer
  88. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  89. * @param clock_divider Timer clock divide value, the timer clock is divided from the selected clock source
  90. *
  91. * @return None
  92. */
  93. #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)
  94. /**
  95. * @brief Get LEDC timer clock divider
  96. *
  97. * @param hal Context of the HAL layer
  98. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  99. * @param clock_divider Timer clock divide value, the timer clock is divided from the selected clock source
  100. *
  101. * @return None
  102. */
  103. #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)
  104. /**
  105. * @brief Set LEDC timer clock source
  106. *
  107. * @param hal Context of the HAL layer
  108. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  109. * @param clk_src Timer clock source
  110. *
  111. * @return None
  112. */
  113. #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)
  114. /**
  115. * @brief Get LEDC timer clock source
  116. *
  117. * @param hal Context of the HAL layer
  118. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  119. * @param clk_src Pointer to accept the timer clock source
  120. *
  121. * @return None
  122. */
  123. #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)
  124. /**
  125. * @brief Set LEDC duty resolution
  126. *
  127. * @param hal Context of the HAL layer
  128. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  129. * @param duty_resolution Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution)]
  130. *
  131. * @return None
  132. */
  133. #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)
  134. /**
  135. * @brief Get LEDC duty resolution
  136. *
  137. * @param hal Context of the HAL layer
  138. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  139. * @param duty_resolution Pointer to accept the resolution of duty setting in number of bits.
  140. *
  141. * @return None
  142. */
  143. #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)
  144. /**
  145. * @brief Get LEDC max duty
  146. *
  147. * @param hal Context of the HAL layer
  148. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  149. * @param max_duty Pointer to accept the max duty
  150. *
  151. * @return None
  152. */
  153. #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)
  154. /**
  155. * @brief Get LEDC hpoint value
  156. *
  157. * @param hal Context of the HAL layer
  158. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  159. * @param hpoint_val Pointer to accept the LEDC hpoint value(max: 0xfffff)
  160. *
  161. * @return None
  162. */
  163. #define ledc_hal_get_hpoint(hal, channel_num, hpoint_val) ledc_ll_get_hpoint((hal)->dev, (hal)->speed_mode, channel_num, hpoint_val)
  164. /**
  165. * @brief Set LEDC the integer part of duty value
  166. *
  167. * @param hal Context of the HAL layer
  168. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  169. * @param duty_val LEDC duty value, the range of duty setting is [0, (2**duty_resolution)]
  170. *
  171. * @return None
  172. */
  173. #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)
  174. /**
  175. * @brief Set the output enable
  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 sig_out_en The output enable status
  180. *
  181. * @return None
  182. */
  183. #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)
  184. /**
  185. * @brief Set the duty start
  186. *
  187. * @param hal Context of the HAL layer
  188. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  189. * @param duty_start The duty start
  190. *
  191. * @return None
  192. */
  193. #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)
  194. /**
  195. * @brief Set output idle level
  196. *
  197. * @param hal Context of the HAL layer
  198. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  199. * @param idle_level The output idle level
  200. *
  201. * @return None
  202. */
  203. #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)
  204. /**
  205. * @brief Set fade end interrupt enable
  206. *
  207. * @param hal Context of the HAL layer
  208. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  209. * @param fade_end_intr_en The fade end interrupt enable status
  210. *
  211. * @return None
  212. */
  213. #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)
  214. /**
  215. * @brief Set timer index of the specified channel
  216. *
  217. * @param hal Context of the HAL layer
  218. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  219. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  220. *
  221. * @return None
  222. */
  223. #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)
  224. /**
  225. * @brief Get timer index of the specified channel
  226. *
  227. * @param hal Context of the HAL layer
  228. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  229. * @param timer_sel Pointer to accept the LEDC timer index
  230. *
  231. * @return None
  232. */
  233. #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)
  234. /**
  235. * @brief Init the LEDC hal. This function should be called first before other hal layer function is called
  236. *
  237. * @param hal Context of the HAL layer
  238. * @param speed_mode speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mod
  239. *
  240. * @return None
  241. */
  242. void ledc_hal_init(ledc_hal_context_t *hal, ledc_mode_t speed_mode);
  243. /**
  244. * @brief Update channel configure when select low speed mode
  245. *
  246. * @param hal Context of the HAL layer
  247. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  248. *
  249. * @return None
  250. */
  251. void ledc_hal_ls_channel_update(ledc_hal_context_t *hal, ledc_channel_t channel_num);
  252. /**
  253. * @brief Set LEDC hpoint value
  254. *
  255. * @param hal Context of the HAL layer
  256. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  257. * @param hpoint_val LEDC hpoint value(max: 0xfffff)
  258. *
  259. * @return None
  260. */
  261. void ledc_hal_set_hpoint(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t hpoint_val);
  262. /**
  263. * @brief Get LEDC duty value
  264. *
  265. * @param hal Context of the HAL layer
  266. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  267. * @param duty_val Pointer to accept the LEDC duty value
  268. *
  269. * @return None
  270. */
  271. void ledc_hal_get_duty(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t *duty_val);
  272. /**
  273. * @brief Set LEDC duty change direction
  274. *
  275. * @param hal Context of the HAL layer
  276. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  277. * @param duty_direction LEDC duty change direction, increase or decrease
  278. *
  279. * @return None
  280. */
  281. void ledc_hal_set_duty_direction(ledc_hal_context_t *hal, ledc_channel_t channel_num, ledc_duty_direction_t duty_direction);
  282. /**
  283. * @brief Set the number of increased or decreased times
  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 duty_num The number of increased or decreased times
  288. *
  289. * @return None
  290. */
  291. void ledc_hal_set_duty_num(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t duty_num);
  292. /**
  293. * @brief Set the duty cycles of increase or decrease
  294. *
  295. * @param hal Context of the HAL layer
  296. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  297. * @param duty_cycle The duty cycles
  298. *
  299. * @return None
  300. */
  301. void ledc_hal_set_duty_cycle(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t duty_cycle);
  302. /**
  303. * @brief Set the step scale of increase or decrease
  304. *
  305. * @param hal Context of the HAL layer
  306. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  307. * @param duty_scale The step scale
  308. *
  309. * @return None
  310. */
  311. void ledc_hal_set_duty_scale(ledc_hal_context_t *hal, ledc_channel_t channel_num, uint32_t duty_scale);
  312. /**
  313. * @brief Get interrupt status of the specified channel
  314. *
  315. * @param hal Context of the HAL layer
  316. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  317. * @param intr_status Pointer to accept the interrupt status
  318. *
  319. * @return None
  320. */
  321. void ledc_hal_get_fade_end_intr_status(ledc_hal_context_t *hal, uint32_t *intr_status);
  322. /**
  323. * @brief Clear interrupt status of the specified channel
  324. *
  325. * @param hal Context of the HAL layer
  326. * @param channel_num LEDC channel index (0-7), select from ledc_channel_t
  327. *
  328. * @return None
  329. */
  330. void ledc_hal_clear_fade_end_intr_status(ledc_hal_context_t *hal, ledc_channel_t channel_num);
  331. /**
  332. * @brief Get clock config of LEDC timer
  333. *
  334. * @param hal Context of the HAL layer
  335. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  336. * @param clk_cfg Pointer to accept clock config
  337. *
  338. * @return None
  339. */
  340. void ledc_hal_get_clk_cfg(ledc_hal_context_t *hal, ledc_timer_t timer_sel, ledc_clk_cfg_t *clk_cfg);
  341. /**
  342. * @brief Config low speed timer clock source with clock config
  343. *s
  344. * @param hal Context of the HAL layer
  345. * @param clk_cfg clock config
  346. *
  347. * @return None
  348. */
  349. void ledc_hal_set_slow_clk(ledc_hal_context_t *hal, ledc_clk_cfg_t clk_cfg);