ledc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // Copyright 2015-2016 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. #ifndef _DRIVER_LEDC_H_
  14. #define _DRIVER_LEDC_H_
  15. #include "esp_err.h"
  16. #include "soc/soc.h"
  17. #include "driver/gpio.h"
  18. #include "driver/periph_ctrl.h"
  19. #include "esp_intr_alloc.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. #define LEDC_APB_CLK_HZ (APB_CLK_FREQ)
  24. #define LEDC_REF_CLK_HZ (1*1000000)
  25. #define LEDC_ERR_DUTY (0xFFFFFFFF)
  26. typedef enum {
  27. LEDC_HIGH_SPEED_MODE = 0, /*!< LEDC high speed speed_mode */
  28. LEDC_LOW_SPEED_MODE, /*!< LEDC low speed speed_mode */
  29. LEDC_SPEED_MODE_MAX, /*!< LEDC speed limit */
  30. } ledc_mode_t;
  31. typedef enum {
  32. LEDC_INTR_DISABLE = 0, /*!< Disable LEDC interrupt */
  33. LEDC_INTR_FADE_END, /*!< Enable LEDC interrupt */
  34. } ledc_intr_type_t;
  35. typedef enum {
  36. LEDC_DUTY_DIR_DECREASE = 0, /*!< LEDC duty decrease direction */
  37. LEDC_DUTY_DIR_INCREASE = 1, /*!< LEDC duty increase direction */
  38. } ledc_duty_direction_t;
  39. typedef enum {
  40. LEDC_REF_TICK = 0, /*!< LEDC timer clock divided from reference tick (1Mhz) */
  41. LEDC_APB_CLK, /*!< LEDC timer clock divided from APB clock (80Mhz) */
  42. } ledc_clk_src_t;
  43. typedef enum {
  44. LEDC_TIMER_0 = 0, /*!< LEDC timer 0 */
  45. LEDC_TIMER_1, /*!< LEDC timer 1 */
  46. LEDC_TIMER_2, /*!< LEDC timer 2 */
  47. LEDC_TIMER_3, /*!< LEDC timer 3 */
  48. } ledc_timer_t;
  49. typedef enum {
  50. LEDC_CHANNEL_0 = 0, /*!< LEDC channel 0 */
  51. LEDC_CHANNEL_1, /*!< LEDC channel 1 */
  52. LEDC_CHANNEL_2, /*!< LEDC channel 2 */
  53. LEDC_CHANNEL_3, /*!< LEDC channel 3 */
  54. LEDC_CHANNEL_4, /*!< LEDC channel 4 */
  55. LEDC_CHANNEL_5, /*!< LEDC channel 5 */
  56. LEDC_CHANNEL_6, /*!< LEDC channel 6 */
  57. LEDC_CHANNEL_7, /*!< LEDC channel 7 */
  58. LEDC_CHANNEL_MAX,
  59. } ledc_channel_t;
  60. typedef enum {
  61. LEDC_TIMER_10_BIT = 10, /*!< LEDC PWM duty resolution of 10 bits */
  62. LEDC_TIMER_11_BIT = 11, /*!< LEDC PWM duty resolution of 11 bits */
  63. LEDC_TIMER_12_BIT = 12, /*!< LEDC PWM duty resolution of 12 bits */
  64. LEDC_TIMER_13_BIT = 13, /*!< LEDC PWM duty resolution of 13 bits */
  65. LEDC_TIMER_14_BIT = 14, /*!< LEDC PWM duty resolution of 14 bits */
  66. LEDC_TIMER_15_BIT = 15, /*!< LEDC PWM duty resolution of 15 bits */
  67. } ledc_timer_bit_t;
  68. typedef enum {
  69. LEDC_FADE_NO_WAIT = 0, /*!< LEDC fade function will return immediately */
  70. LEDC_FADE_WAIT_DONE, /*!< LEDC fade function will block until fading to the target duty */
  71. LEDC_FADE_MAX,
  72. } ledc_fade_mode_t;
  73. /**
  74. * @brief Configuration parameters of LEDC channel for ledc_channel_config function
  75. */
  76. typedef struct {
  77. int gpio_num; /*!< the LEDC output gpio_num, if you want to use gpio16, gpio_num = 16 */
  78. ledc_mode_t speed_mode; /*!< LEDC speed speed_mode, high-speed mode or low-speed mode */
  79. ledc_channel_t channel; /*!< LEDC channel (0 - 7) */
  80. ledc_intr_type_t intr_type; /*!< configure interrupt, Fade interrupt enable or Fade interrupt disable */
  81. ledc_timer_t timer_sel; /*!< Select the timer source of channel (0 - 3) */
  82. uint32_t duty; /*!< LEDC channel duty, the range of duty setting is [0, (2**duty_resolution) - 1] */
  83. } ledc_channel_config_t;
  84. /**
  85. * @brief Configuration parameters of LEDC Timer timer for ledc_timer_config function
  86. */
  87. typedef struct {
  88. ledc_mode_t speed_mode; /*!< LEDC speed speed_mode, high-speed mode or low-speed mode */
  89. ledc_timer_bit_t duty_resolution; /*!< LEDC channel duty resolution */
  90. ledc_timer_t timer_num; /*!< The timer source of channel (0 - 3) */
  91. uint32_t freq_hz; /*!< LEDC timer frequency (Hz) */
  92. } ledc_timer_config_t;
  93. typedef intr_handle_t ledc_isr_handle_t;
  94. /**
  95. * @brief LEDC channel configuration
  96. * Configure LEDC channel with the given channel/output gpio_num/interrupt/source timer/frequency(Hz)/LEDC duty resolution
  97. *
  98. * @param ledc_conf Pointer of LEDC channel configure struct
  99. *
  100. * @return
  101. * - ESP_OK Success
  102. * - ESP_ERR_INVALID_ARG Parameter error
  103. */
  104. esp_err_t ledc_channel_config(const ledc_channel_config_t* ledc_conf);
  105. /**
  106. * @brief LEDC timer configuration
  107. * Configure LEDC timer with the given source timer/frequency(Hz)/duty_resolution
  108. *
  109. * @param timer_conf Pointer of LEDC timer configure struct
  110. *
  111. * @return
  112. * - ESP_OK Success
  113. * - ESP_ERR_INVALID_ARG Parameter error
  114. * - ESP_FAIL Can not find a proper pre-divider number base on the given frequency and the current duty_resolution.
  115. */
  116. esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf);
  117. /**
  118. * @brief LEDC update channel parameters
  119. * Call this function to activate the LEDC updated parameters.
  120. * After ledc_set_duty, ledc_set_fade, we need to call this function to update the settings.
  121. *
  122. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode,
  123. * @param channel LEDC channel (0-7), select from ledc_channel_t
  124. *
  125. * @return
  126. * - ESP_OK Success
  127. * - ESP_ERR_INVALID_ARG Parameter error
  128. *
  129. */
  130. esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel);
  131. /**
  132. * @brief LEDC stop.
  133. * Disable LEDC output, and set idle level
  134. *
  135. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  136. * @param channel LEDC channel (0-7), select from ledc_channel_t
  137. * @param idle_level Set output idle level after LEDC stops.
  138. *
  139. * @return
  140. * - ESP_OK Success
  141. * - ESP_ERR_INVALID_ARG Parameter error
  142. */
  143. esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level);
  144. /**
  145. * @brief LEDC set channel frequency (Hz)
  146. *
  147. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  148. * @param timer_num LEDC timer index (0-3), select from ledc_timer_t
  149. * @param freq_hz Set the LEDC frequency
  150. *
  151. * @return
  152. * - ESP_OK Success
  153. * - ESP_ERR_INVALID_ARG Parameter error
  154. * - ESP_FAIL Can not find a proper pre-divider number base on the given frequency and the current duty_resolution.
  155. */
  156. esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz);
  157. /**
  158. * @brief LEDC get channel frequency (Hz)
  159. *
  160. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  161. * @param timer_num LEDC timer index (0-3), select from ledc_timer_t
  162. *
  163. * @return
  164. * - 0 error
  165. * - Others Current LEDC frequency
  166. */
  167. uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num);
  168. /**
  169. * @brief LEDC set duty
  170. * Only after calling ledc_update_duty will the duty update.
  171. *
  172. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  173. * @param channel LEDC channel (0-7), select from ledc_channel_t
  174. * @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution) - 1]
  175. *
  176. * @return
  177. * - ESP_OK Success
  178. * - ESP_ERR_INVALID_ARG Parameter error
  179. */
  180. esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty);
  181. /**
  182. * @brief LEDC get duty
  183. *
  184. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  185. * @param channel LEDC channel (0-7), select from ledc_channel_t
  186. *
  187. * @return
  188. * - LEDC_ERR_DUTY if parameter error
  189. * - Others Current LEDC duty
  190. */
  191. uint32_t ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel);
  192. /**
  193. * @brief LEDC set gradient
  194. * Set LEDC gradient, After the function calls the ledc_update_duty function, the function can take effect.
  195. *
  196. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  197. * @param channel LEDC channel (0-7), select from ledc_channel_t
  198. * @param duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution) - 1]
  199. * @param gradule_direction Set the direction of the gradient
  200. * @param step_num Set the number of the gradient
  201. * @param duty_cyle_num Set how many LEDC tick each time the gradient lasts
  202. * @param duty_scale Set gradient change amplitude
  203. *
  204. * @return
  205. * - ESP_OK Success
  206. * - ESP_ERR_INVALID_ARG Parameter error
  207. */
  208. esp_err_t ledc_set_fade(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty, ledc_duty_direction_t gradule_direction,
  209. uint32_t step_num, uint32_t duty_cyle_num, uint32_t duty_scale);
  210. /**
  211. * @brief Register LEDC interrupt handler, the handler is an ISR.
  212. * The handler will be attached to the same CPU core that this function is running on.
  213. *
  214. * @param fn Interrupt handler function.
  215. * @param arg User-supplied argument passed to the handler function.
  216. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  217. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  218. * @param arg Parameter for handler function
  219. * @param handle Pointer to return handle. If non-NULL, a handle for the interrupt will
  220. * be returned here.
  221. *
  222. * @return
  223. * - ESP_OK Success
  224. * - ESP_ERR_INVALID_ARG Function pointer error.
  225. */
  226. esp_err_t ledc_isr_register(void (*fn)(void*), void * arg, int intr_alloc_flags, ledc_isr_handle_t *handle);
  227. /**
  228. * @brief Configure LEDC settings
  229. *
  230. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  231. * @param timer_sel Timer index (0-3), there are 4 timers in LEDC module
  232. * @param clock_divider Timer clock divide value, the timer clock is divided from the selected clock source
  233. * @param duty_resolution Resolution of duty setting in number of bits. The range of duty values is [0, (2**duty_resolution) - 1]
  234. * @param clk_src Select LEDC source clock.
  235. *
  236. * @return
  237. * - (-1) Parameter error
  238. * - Other Current LEDC duty
  239. */
  240. 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);
  241. /**
  242. * @brief Reset LEDC timer
  243. *
  244. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  245. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  246. *
  247. * @return
  248. * - ESP_ERR_INVALID_ARG Parameter error
  249. * - ESP_OK Success
  250. */
  251. esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel);
  252. /**
  253. * @brief Pause LEDC timer counter
  254. *
  255. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  256. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  257. *
  258. * @return
  259. * - ESP_ERR_INVALID_ARG Parameter error
  260. * - ESP_OK Success
  261. *
  262. */
  263. esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel);
  264. /**
  265. * @brief Resume LEDC timer
  266. *
  267. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  268. * @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
  269. *
  270. * @return
  271. * - ESP_ERR_INVALID_ARG Parameter error
  272. * - ESP_OK Success
  273. */
  274. esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel);
  275. /**
  276. * @brief Bind LEDC channel with the selected timer
  277. *
  278. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  279. * @param channel LEDC channel index (0-7), select from ledc_channel_t
  280. * @param timer_idx LEDC timer index (0-3), select from ledc_timer_t
  281. *
  282. * @return
  283. * - ESP_ERR_INVALID_ARG Parameter error
  284. * - ESP_OK Success
  285. */
  286. esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx);
  287. /**
  288. * @brief Set LEDC fade function. Should call ledc_fade_func_install() before calling this function.
  289. * Call ledc_fade_start() after this to start fading.
  290. *
  291. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode,
  292. * @param channel LEDC channel index (0-7), select from ledc_channel_t
  293. * @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
  294. * @param scale Controls the increase or decrease step scale.
  295. * @param cycle_num increase or decrease the duty every cycle_num cycles
  296. *
  297. * @return
  298. * - ESP_ERR_INVALID_ARG Parameter error
  299. * - ESP_OK Success
  300. * - ESP_ERR_INVALID_STATE Fade function not installed.
  301. * - ESP_FAIL Fade function init error
  302. */
  303. esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t target_duty, int scale, int cycle_num);
  304. /**
  305. * @brief Set LEDC fade function, with a limited time. Should call ledc_fade_func_install() before calling this function.
  306. * Call ledc_fade_start() after this to start fading.
  307. *
  308. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode,
  309. * @param channel LEDC channel index (0-7), select from ledc_channel_t
  310. * @param target_duty Target duty of fading.( 0 - (2 ** duty_resolution - 1)))
  311. * @param max_fade_time_ms The maximum time of the fading ( ms ).
  312. *
  313. * @return
  314. * - ESP_ERR_INVALID_ARG Parameter error
  315. * - ESP_OK Success
  316. * - ESP_ERR_INVALID_STATE Fade function not installed.
  317. * - ESP_FAIL Fade function init error
  318. */
  319. 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);
  320. /**
  321. * @brief Install ledc fade function. This function will occupy interrupt of LEDC module.
  322. *
  323. * @param intr_alloc_flags Flags used to allocate the interrupt. One or multiple (ORred)
  324. * ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info.
  325. *
  326. * @return
  327. * - ESP_OK Success
  328. * - ESP_ERR_INVALID_STATE Fade function already installed.
  329. */
  330. esp_err_t ledc_fade_func_install(int intr_alloc_flags);
  331. /**
  332. * @brief Uninstall LEDC fade function.
  333. *
  334. */
  335. void ledc_fade_func_uninstall();
  336. /**
  337. * @brief Start LEDC fading.
  338. *
  339. * @param speed_mode Select the LEDC speed_mode, high-speed mode and low-speed mode
  340. * @param channel LEDC channel number
  341. * @param wait_done Whether to block until fading done.
  342. *
  343. * @return
  344. * - ESP_OK Success
  345. * - ESP_ERR_INVALID_STATE Fade function not installed.
  346. * - ESP_ERR_INVALID_ARG Parameter error.
  347. */
  348. esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_fade_mode_t wait_done);
  349. #ifdef __cplusplus
  350. }
  351. #endif
  352. #endif /* _DRIVER_LEDC_H_ */