ledc.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. // Copyright 2015-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. #pragma once
  14. #include "esp_err.h"
  15. #include "esp_intr_alloc.h"
  16. #include "soc/soc.h"
  17. #include "hal/ledc_types.h"
  18. #include "driver/gpio.h"
  19. #include "driver/periph_ctrl.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #define LEDC_APB_CLK_HZ (APB_CLK_FREQ)
  24. #define LEDC_REF_CLK_HZ (REF_CLK_FREQ)
  25. #define LEDC_ERR_DUTY (0xFFFFFFFF)
  26. #define LEDC_ERR_VAL (-1)
  27. typedef intr_handle_t ledc_isr_handle_t;
  28. /**
  29. * @brief LEDC channel configuration
  30. * Configure LEDC channel with the given channel/output gpio_num/interrupt/source timer/frequency(Hz)/LEDC duty resolution
  31. *
  32. * @param ledc_conf Pointer of LEDC channel configure struct
  33. *
  34. * @return
  35. * - ESP_OK Success
  36. * - ESP_ERR_INVALID_ARG Parameter error
  37. */
  38. esp_err_t ledc_channel_config(const ledc_channel_config_t* ledc_conf);
  39. /**
  40. * @brief LEDC timer configuration
  41. * Configure LEDC timer with the given source timer/frequency(Hz)/duty_resolution
  42. *
  43. * @param timer_conf Pointer of LEDC timer configure struct
  44. *
  45. * @return
  46. * - ESP_OK Success
  47. * - ESP_ERR_INVALID_ARG Parameter error
  48. * - ESP_FAIL Can not find a proper pre-divider number base on the given frequency and the current duty_resolution.
  49. */
  50. esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf);
  51. /**
  52. * @brief LEDC update channel parameters
  53. * @note Call this function to activate the LEDC updated parameters.
  54. * After ledc_set_duty, we need to call this function to update the settings.
  55. * @note ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to
  56. * control one LEDC channel in different tasks at the same time.
  57. * A thread-safe version of API is ledc_set_duty_and_update
  58. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  59. * @param channel LEDC channel (0-7), select from ledc_channel_t
  60. *
  61. * @return
  62. * - ESP_OK Success
  63. * - ESP_ERR_INVALID_ARG Parameter error
  64. *
  65. */
  66. esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel);
  67. /**
  68. * @brief Set LEDC output gpio.
  69. *
  70. * @param gpio_num The LEDC output gpio
  71. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  72. * @param ledc_channel LEDC channel (0-7), select from ledc_channel_t
  73. *
  74. * @return
  75. * - ESP_OK Success
  76. * - ESP_ERR_INVALID_ARG Parameter error
  77. */
  78. esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc_channel);
  79. /**
  80. * @brief LEDC stop.
  81. * Disable LEDC output, and set idle level
  82. *
  83. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  84. * @param channel LEDC channel (0-7), select from ledc_channel_t
  85. * @param idle_level Set output idle level after LEDC stops.
  86. *
  87. * @return
  88. * - ESP_OK Success
  89. * - ESP_ERR_INVALID_ARG Parameter error
  90. */
  91. esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level);
  92. /**
  93. * @brief LEDC set channel frequency (Hz)
  94. *
  95. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  96. * @param timer_num LEDC timer index (0-3), select from ledc_timer_t
  97. * @param freq_hz Set the LEDC frequency
  98. *
  99. * @return
  100. * - ESP_OK Success
  101. * - ESP_ERR_INVALID_ARG Parameter error
  102. * - ESP_FAIL Can not find a proper pre-divider number base on the given frequency and the current duty_resolution.
  103. */
  104. esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz);
  105. /**
  106. * @brief LEDC get channel frequency (Hz)
  107. *
  108. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  109. * @param timer_num LEDC timer index (0-3), select from ledc_timer_t
  110. *
  111. * @return
  112. * - 0 error
  113. * - Others Current LEDC frequency
  114. */
  115. uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num);
  116. /**
  117. * @brief LEDC set duty and hpoint value
  118. * Only after calling ledc_update_duty will the duty update.
  119. * @note ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to
  120. * control one LEDC channel in different tasks at the same time.
  121. * A thread-safe version of API is ledc_set_duty_and_update
  122. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  123. * Other duty operations will have to wait until the fade operation has finished.
  124. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  125. * @param channel LEDC channel (0-7), select from ledc_channel_t
  126. * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
  127. * @param hpoint Set the LEDC hpoint value(max: 0xfffff)
  128. *
  129. * @return
  130. * - ESP_OK Success
  131. * - ESP_ERR_INVALID_ARG Parameter error
  132. */
  133. esp_err_t ledc_set_duty_with_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint);
  134. /**
  135. * @brief LEDC get hpoint value, the counter value when the output is set high level.
  136. *
  137. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  138. * @param channel LEDC channel (0-7), select from ledc_channel_t
  139. * @return
  140. * - LEDC_ERR_VAL if parameter error
  141. * - Others Current hpoint value of LEDC channel
  142. */
  143. int ledc_get_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel);
  144. /**
  145. * @brief LEDC set duty
  146. * This function do not change the hpoint value of this channel. if needed, please call ledc_set_duty_with_hpoint.
  147. * only after calling ledc_update_duty will the duty update.
  148. * @note ledc_set_duty, ledc_set_duty_with_hpoint and ledc_update_duty are not thread-safe, do not call these functions to
  149. * control one LEDC channel in different tasks at the same time.
  150. * A thread-safe version of API is ledc_set_duty_and_update.
  151. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  152. * Other duty operations will have to wait until the fade operation has finished.
  153. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  154. * @param channel LEDC channel (0-7), select from ledc_channel_t
  155. * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
  156. *
  157. * @return
  158. * - ESP_OK Success
  159. * - ESP_ERR_INVALID_ARG Parameter error
  160. */
  161. esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty);
  162. /**
  163. * @brief LEDC get duty
  164. *
  165. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  166. * @param channel LEDC channel (0-7), select from ledc_channel_t
  167. *
  168. * @return
  169. * - LEDC_ERR_DUTY if parameter error
  170. * - Others Current LEDC duty
  171. */
  172. uint32_t ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel);
  173. /**
  174. * @brief LEDC set gradient
  175. * Set LEDC gradient, After the function calls the ledc_update_duty function, the function can take effect.
  176. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  177. * Other duty operations will have to wait until the fade operation has finished.
  178. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  179. * @param channel LEDC channel (0-7), select from ledc_channel_t
  180. * @param duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution)]
  181. * @param fade_direction Set the direction of the gradient
  182. * @param step_num Set the number of the gradient
  183. * @param duty_cycle_num Set how many LEDC tick each time the gradient lasts
  184. * @param duty_scale Set gradient change amplitude
  185. *
  186. * @return
  187. * - ESP_OK Success
  188. * - ESP_ERR_INVALID_ARG Parameter error
  189. */
  190. esp_err_t ledc_set_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, ledc_duty_direction_t fade_direction,
  191. uint32_t step_num, uint32_t duty_cycle_num, uint32_t duty_scale);
  192. /**
  193. * @brief Register LEDC interrupt handler, the handler is an ISR.
  194. * The handler will be attached to the same CPU core that this function is running on.
  195. *
  196. * @param fn Interrupt handler function.
  197. * @param arg User-supplied argument passed to the handler function.
  198. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  199. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  200. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will
  201. * be returned here.
  202. *
  203. * @return
  204. * - ESP_OK Success
  205. * - ESP_ERR_INVALID_ARG Function pointer error.
  206. */
  207. esp_err_t ledc_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, ledc_isr_handle_t *handle);
  208. /**
  209. * @brief Configure LEDC settings
  210. *
  211. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  212. * @param timer_sel Timer index (0-3), there are 4 timers in LEDC module
  213. * @param clock_divider Timer clock divide value, the timer clock is divided from the selected clock source
  214. * @param duty_resolution Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution)]
  215. * @param clk_src Select LEDC source clock.
  216. *
  217. * @return
  218. * - (-1) Parameter error
  219. * - Other Current LEDC duty
  220. */
  221. esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t clock_divider, uint32_t duty_resolution, ledc_clk_src_t clk_src);
  222. /**
  223. * @brief Reset LEDC timer
  224. *
  225. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  226. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  227. *
  228. * @return
  229. * - ESP_ERR_INVALID_ARG Parameter error
  230. * - ESP_OK Success
  231. */
  232. esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, ledc_timer_t timer_sel);
  233. /**
  234. * @brief Pause LEDC timer counter
  235. *
  236. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  237. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  238. *
  239. * @return
  240. * - ESP_ERR_INVALID_ARG Parameter error
  241. * - ESP_OK Success
  242. *
  243. */
  244. esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, ledc_timer_t timer_sel);
  245. /**
  246. * @brief Resume LEDC timer
  247. *
  248. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  249. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  250. *
  251. * @return
  252. * - ESP_ERR_INVALID_ARG Parameter error
  253. * - ESP_OK Success
  254. */
  255. esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, ledc_timer_t timer_sel);
  256. /**
  257. * @brief Bind LEDC channel with the selected timer
  258. *
  259. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  260. * @param channel LEDC channel index (0-7), select from ledc_channel_t
  261. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  262. *
  263. * @return
  264. * - ESP_ERR_INVALID_ARG Parameter error
  265. * - ESP_OK Success
  266. */
  267. esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_timer_t timer_sel);
  268. /**
  269. * @brief Set LEDC fade function.
  270. * @note Call ledc_fade_func_install() once before calling this function.
  271. * Call ledc_fade_start() after this to start fading.
  272. * @note ledc_set_fade_with_step, ledc_set_fade_with_time and ledc_fade_start are not thread-safe, do not call these functions to
  273. * control one LEDC channel in different tasks at the same time.
  274. * A thread-safe version of API is ledc_set_fade_step_and_start
  275. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  276. * Other duty operations will have to wait until the fade operation has finished.
  277. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. ,
  278. * @param channel LEDC channel index (0-7), select from ledc_channel_t
  279. * @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
  280. * @param scale Controls the increase or decrease step scale.
  281. * @param cycle_num increase or decrease the duty every cycle_num cycles
  282. *
  283. * @return
  284. * - ESP_ERR_INVALID_ARG Parameter error
  285. * - ESP_OK Success
  286. * - ESP_ERR_INVALID_STATE Fade function not installed.
  287. * - ESP_FAIL Fade function init error
  288. */
  289. esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num);
  290. /**
  291. * @brief Set LEDC fade function, with a limited time.
  292. * @note Call ledc_fade_func_install() once before calling this function.
  293. * Call ledc_fade_start() after this to start fading.
  294. * @note ledc_set_fade_with_step, ledc_set_fade_with_time and ledc_fade_start are not thread-safe, do not call these functions to
  295. * control one LEDC channel in different tasks at the same time.
  296. * A thread-safe version of API is ledc_set_fade_step_and_start
  297. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  298. * Other duty operations will have to wait until the fade operation has finished.
  299. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. ,
  300. * @param channel LEDC channel index (0-7), select from ledc_channel_t
  301. * @param target_duty Target duty of fading.( 0 - (2 ** duty_resolution - 1)))
  302. * @param max_fade_time_ms The maximum time of the fading ( ms ).
  303. *
  304. * @return
  305. * - ESP_ERR_INVALID_ARG Parameter error
  306. * - ESP_OK Success
  307. * - ESP_ERR_INVALID_STATE Fade function not installed.
  308. * - ESP_FAIL Fade function init error
  309. */
  310. esp_err_t ledc_set_fade_with_time(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int max_fade_time_ms);
  311. /**
  312. * @brief Install LEDC fade function. This function will occupy interrupt of LEDC module.
  313. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  314. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  315. *
  316. * @return
  317. * - ESP_OK Success
  318. * - ESP_ERR_INVALID_STATE Fade function already installed.
  319. */
  320. esp_err_t ledc_fade_func_install(int intr_alloc_flags);
  321. /**
  322. * @brief Uninstall LEDC fade function.
  323. *
  324. */
  325. void ledc_fade_func_uninstall(void);
  326. /**
  327. * @brief Start LEDC fading.
  328. * @note Call ledc_fade_func_install() once before calling this function.
  329. * Call this API right after ledc_set_fade_with_time or ledc_set_fade_with_step before to start fading.
  330. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  331. * Other duty operations will have to wait until the fade operation has finished.
  332. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  333. * @param channel LEDC channel number
  334. * @param fade_mode Whether to block until fading done.
  335. *
  336. * @return
  337. * - ESP_OK Success
  338. * - ESP_ERR_INVALID_STATE Fade function not installed.
  339. * - ESP_ERR_INVALID_ARG Parameter error.
  340. */
  341. esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t fade_mode);
  342. /**
  343. * @brief A thread-safe API to set duty for LEDC channel and return when duty updated.
  344. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  345. * Other duty operations will have to wait until the fade operation has finished.
  346. *
  347. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  348. * @param channel LEDC channel (0-7), select from ledc_channel_t
  349. * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
  350. * @param hpoint Set the LEDC hpoint value(max: 0xfffff)
  351. *
  352. */
  353. esp_err_t ledc_set_duty_and_update(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, uint32_t hpoint);
  354. /**
  355. * @brief A thread-safe API to set and start LEDC fade function, with a limited time.
  356. * @note Call ledc_fade_func_install() once, before calling this function.
  357. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  358. * Other duty operations will have to wait until the fade operation has finished.
  359. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  360. * @param channel LEDC channel index (0-7), select from ledc_channel_t
  361. * @param target_duty Target duty of fading.( 0 - (2 ** duty_resolution - 1)))
  362. * @param max_fade_time_ms The maximum time of the fading ( ms ).
  363. * @param fade_mode choose blocking or non-blocking mode
  364. * @return
  365. * - ESP_ERR_INVALID_ARG Parameter error
  366. * - ESP_OK Success
  367. * - ESP_ERR_INVALID_STATE Fade function not installed.
  368. * - ESP_FAIL Fade function init error
  369. */
  370. esp_err_t ledc_set_fade_time_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t max_fade_time_ms, ledc_fade_mode_t fade_mode);
  371. /**
  372. * @brief A thread-safe API to set and start LEDC fade function.
  373. * @note Call ledc_fade_func_install() once before calling this function.
  374. * @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
  375. * Other duty operations will have to wait until the fade operation has finished.
  376. * @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
  377. * @param channel LEDC channel index (0-7), select from ledc_channel_t
  378. * @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
  379. * @param scale Controls the increase or decrease step scale.
  380. * @param cycle_num increase or decrease the duty every cycle_num cycles
  381. * @param fade_mode choose blocking or non-blocking mode
  382. * @return
  383. * - ESP_ERR_INVALID_ARG Parameter error
  384. * - ESP_OK Success
  385. * - ESP_ERR_INVALID_STATE Fade function not installed.
  386. * - ESP_FAIL Fade function init error
  387. */
  388. esp_err_t ledc_set_fade_step_and_start(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, uint32_t scale, uint32_t cycle_num, ledc_fade_mode_t fade_mode);
  389. #ifdef __cplusplus
  390. }
  391. #endif