ledc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #include <esp_types.h>
  14. #include "esp_intr.h"
  15. #include "freertos/FreeRTOS.h"
  16. #include "freertos/semphr.h"
  17. #include "freertos/xtensa_api.h"
  18. #include "soc/gpio_sig_map.h"
  19. #include "driver/ledc.h"
  20. #include "esp_log.h"
  21. static const char* LEDC_TAG = "LEDC";
  22. static portMUX_TYPE ledc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  23. #define LEDC_CHECK(a, str, ret_val) if (!(a)) { \
  24. ESP_LOGE(LEDC_TAG,"%s:%d (%s):%s", __FILE__, __LINE__, __FUNCTION__, str); \
  25. return (ret_val); \
  26. }
  27. esp_err_t ledc_timer_set(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t div_num, uint32_t bit_num, ledc_clk_src_t clk_src)
  28. {
  29. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  30. LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
  31. portENTER_CRITICAL(&ledc_spinlock);
  32. LEDC.timer_group[speed_mode].timer[timer_sel].conf.div_num = div_num;
  33. LEDC.timer_group[speed_mode].timer[timer_sel].conf.tick_sel = clk_src;
  34. LEDC.timer_group[speed_mode].timer[timer_sel].conf.bit_num = bit_num;
  35. if(speed_mode != LEDC_HIGH_SPEED_MODE) {
  36. LEDC.timer_group[speed_mode].timer[timer_sel].conf.low_speed_update = 1;
  37. }
  38. portEXIT_CRITICAL(&ledc_spinlock);
  39. return ESP_OK;
  40. }
  41. static esp_err_t ledc_duty_config(ledc_mode_t speed_mode, uint32_t channel_num, uint32_t hpoint_val, uint32_t duty_val,
  42. uint32_t duty_direction, uint32_t duty_num, uint32_t duty_cycle, uint32_t duty_scale)
  43. {
  44. portENTER_CRITICAL(&ledc_spinlock);
  45. LEDC.channel_group[speed_mode].channel[channel_num].hpoint.hpoint = hpoint_val;
  46. LEDC.channel_group[speed_mode].channel[channel_num].duty.duty = duty_val;
  47. LEDC.channel_group[speed_mode].channel[channel_num].conf1.val = ((duty_direction & LEDC_DUTY_INC_HSCH0_V) << LEDC_DUTY_INC_HSCH0_S) |
  48. ((duty_num & LEDC_DUTY_NUM_HSCH0_V) << LEDC_DUTY_NUM_HSCH0_S) |
  49. ((duty_cycle & LEDC_DUTY_CYCLE_HSCH0_V) << LEDC_DUTY_CYCLE_HSCH0_S) |
  50. ((duty_scale & LEDC_DUTY_SCALE_HSCH0_V) << LEDC_DUTY_SCALE_HSCH0_S);
  51. portEXIT_CRITICAL(&ledc_spinlock);
  52. return ESP_OK;
  53. }
  54. esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx)
  55. {
  56. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  57. LEDC_CHECK(timer_idx <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
  58. portENTER_CRITICAL(&ledc_spinlock);
  59. LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel = timer_idx;
  60. portEXIT_CRITICAL(&ledc_spinlock);
  61. return ESP_OK;
  62. }
  63. esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel)
  64. {
  65. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  66. LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
  67. portENTER_CRITICAL(&ledc_spinlock);
  68. LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 1;
  69. LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 0;
  70. portEXIT_CRITICAL(&ledc_spinlock);
  71. return ESP_OK;
  72. }
  73. esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel)
  74. {
  75. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  76. LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
  77. portENTER_CRITICAL(&ledc_spinlock);
  78. LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 1;
  79. portEXIT_CRITICAL(&ledc_spinlock);
  80. return ESP_OK;
  81. }
  82. esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel)
  83. {
  84. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  85. LEDC_CHECK(timer_sel <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
  86. portENTER_CRITICAL(&ledc_spinlock);
  87. LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 0;
  88. portEXIT_CRITICAL(&ledc_spinlock);
  89. return ESP_OK;
  90. }
  91. static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, uint32_t channel, ledc_intr_type_t type)
  92. {
  93. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  94. uint32_t value;
  95. uint32_t intr_type = type;
  96. portENTER_CRITICAL(&ledc_spinlock);
  97. value = LEDC.int_ena.val;
  98. if(intr_type == LEDC_INTR_FADE_END) {
  99. LEDC.int_ena.val = value | BIT(LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S + channel);
  100. } else {
  101. LEDC.int_ena.val = (value & (~(BIT(LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S + channel))));
  102. }
  103. portEXIT_CRITICAL(&ledc_spinlock);
  104. return ESP_OK;
  105. }
  106. esp_err_t ledc_isr_register(uint32_t ledc_intr_num, void (*fn)(void*), void * arg)
  107. {
  108. LEDC_CHECK(fn, "ledc isr null", ESP_ERR_INVALID_ARG);
  109. portENTER_CRITICAL(&ledc_spinlock);
  110. ESP_INTR_DISABLE(ledc_intr_num);
  111. intr_matrix_set(xPortGetCoreID(), ETS_LEDC_INTR_SOURCE, ledc_intr_num);
  112. xt_set_interrupt_handler(ledc_intr_num, fn, arg);
  113. ESP_INTR_ENABLE(ledc_intr_num);
  114. portEXIT_CRITICAL(&ledc_spinlock);
  115. return ESP_OK;
  116. }
  117. esp_err_t ledc_timer_config(ledc_timer_config_t* timer_conf)
  118. {
  119. int freq_hz = timer_conf->freq_hz;
  120. int bit_num = timer_conf->bit_num;
  121. int timer_num = timer_conf->timer_num;
  122. int speed_mode = timer_conf->speed_mode;
  123. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  124. if(freq_hz == 0 || bit_num == 0 || bit_num > LEDC_TIMER_15_BIT) {
  125. ESP_LOGE(LEDC_TAG, "freq_hz=%u bit_num=%u", freq_hz, bit_num);
  126. return ESP_ERR_INVALID_ARG;
  127. }
  128. if(timer_num > LEDC_TIMER_3) {
  129. ESP_LOGE(LEDC_TAG, "invalid timer #%u", timer_num);
  130. return ESP_ERR_INVALID_ARG;
  131. }
  132. esp_err_t ret = ESP_OK;
  133. uint32_t precision = (0x1 << bit_num); // 2**depth
  134. // Try calculating divisor based on LEDC_APB_CLK
  135. ledc_clk_src_t timer_clk_src = LEDC_APB_CLK;
  136. // div_param is a Q10.8 fixed point value
  137. uint64_t div_param = ((uint64_t) LEDC_APB_CLK_HZ << 8) / freq_hz / precision;
  138. if (div_param < 256) {
  139. // divisor is too low
  140. ESP_LOGE(LEDC_TAG, "requested frequency and bit depth can not be achieved, try reducing freq_hz or bit_num. div_param=%d", (uint32_t) div_param);
  141. ret = ESP_FAIL;
  142. }
  143. if (div_param > LEDC_DIV_NUM_HSTIMER0_V) {
  144. // APB_CLK results in divisor which too high. Try using REF_TICK as clock source.
  145. timer_clk_src = LEDC_REF_TICK;
  146. div_param = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
  147. if(div_param < 256 || div_param > LEDC_DIV_NUM_HSTIMER0_V) {
  148. ESP_LOGE(LEDC_TAG, "requested frequency and bit depth can not be achieved, try increasing freq_hz or bit_num. div_param=%d", (uint32_t) div_param);
  149. ret = ESP_FAIL;
  150. }
  151. }
  152. // set timer parameters
  153. ledc_timer_set(speed_mode, timer_num, div_param, bit_num, timer_clk_src);
  154. // reset timer
  155. ledc_timer_rst(speed_mode, timer_num);
  156. return ret;
  157. }
  158. esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc_channel)
  159. {
  160. LEDC_CHECK(ledc_channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
  161. LEDC_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "ledc GPIO output number error", ESP_ERR_INVALID_ARG);
  162. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  163. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
  164. gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
  165. if(speed_mode == LEDC_HIGH_SPEED_MODE) {
  166. gpio_matrix_out(gpio_num, LEDC_HS_SIG_OUT0_IDX + ledc_channel, 0, 0);
  167. } else {
  168. ESP_LOGE(LEDC_TAG, "low speed mode is not implemented");
  169. return ESP_ERR_NOT_SUPPORTED;
  170. }
  171. return ESP_OK;
  172. }
  173. esp_err_t ledc_channel_config(ledc_channel_config_t* ledc_conf)
  174. {
  175. uint32_t speed_mode = ledc_conf->speed_mode;
  176. uint32_t gpio_num = ledc_conf->gpio_num;
  177. uint32_t ledc_channel = ledc_conf->channel;
  178. uint32_t timer_select = ledc_conf->timer_sel;
  179. uint32_t intr_type = ledc_conf->intr_type;
  180. uint32_t duty = ledc_conf->duty;
  181. LEDC_CHECK(ledc_channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
  182. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  183. LEDC_CHECK(GPIO_IS_VALID_OUTPUT_GPIO(gpio_num), "ledc GPIO output number error", ESP_ERR_INVALID_ARG);
  184. LEDC_CHECK(timer_select <= LEDC_TIMER_3, "ledc timer error", ESP_ERR_INVALID_ARG);
  185. periph_module_enable(PERIPH_LEDC_MODULE);
  186. esp_err_t ret = ESP_OK;
  187. /*set channel parameters*/
  188. /* channel parameters decide how the waveform looks like in one period*/
  189. /* set channel duty, duty range is (0 ~ ((2 ** bit_num) - 1))*/
  190. ledc_set_duty(speed_mode, ledc_channel, duty);
  191. /*update duty settings*/
  192. ledc_update_duty(speed_mode, ledc_channel);
  193. /*bind the channel with the timer*/
  194. ledc_bind_channel_timer(speed_mode, ledc_channel, timer_select);
  195. /*set interrupt type*/
  196. ledc_enable_intr_type(speed_mode, ledc_channel, intr_type);
  197. ESP_LOGI(LEDC_TAG, "LEDC_PWM CHANNEL %1u|GPIO %02u|Duty %04u|Time %01u",
  198. ledc_channel, gpio_num, duty, timer_select
  199. );
  200. /*set LEDC signal in gpio matrix*/
  201. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
  202. gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
  203. gpio_matrix_out(gpio_num, LEDC_HS_SIG_OUT0_IDX + ledc_channel, 0, 0);
  204. return ret;
  205. }
  206. esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
  207. {
  208. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  209. LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
  210. portENTER_CRITICAL(&ledc_spinlock);
  211. LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 1;
  212. LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 1;
  213. portEXIT_CRITICAL(&ledc_spinlock);
  214. return ESP_OK;
  215. }
  216. esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level)
  217. {
  218. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  219. LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
  220. portENTER_CRITICAL(&ledc_spinlock);
  221. LEDC.channel_group[speed_mode].channel[channel].conf0.idle_lv = idle_level & 0x1;
  222. LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 0;
  223. LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 0;
  224. portEXIT_CRITICAL(&ledc_spinlock);
  225. return ESP_OK;
  226. }
  227. esp_err_t ledc_set_fade(ledc_mode_t speed_mode, uint32_t channel, uint32_t duty, ledc_duty_direction_t fade_direction,
  228. uint32_t step_num, uint32_t duty_cyle_num, uint32_t duty_scale)
  229. {
  230. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  231. LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
  232. LEDC_CHECK(fade_direction <= LEDC_DUTY_DIR_INCREASE, "ledc fade direction error", ESP_ERR_INVALID_ARG);
  233. if(step_num > LEDC_DUTY_NUM_HSCH0_V || duty_cyle_num > LEDC_DUTY_CYCLE_HSCH0_V || duty_scale > LEDC_DUTY_SCALE_HSCH0_V) {
  234. ESP_LOGE(LEDC_TAG, "step_num=%u duty_cyle_num=%u duty_scale=%u", step_num, duty_cyle_num, duty_scale);
  235. return ESP_ERR_INVALID_ARG;
  236. }
  237. ledc_duty_config(speed_mode,
  238. channel, //uint32_t chan_num,
  239. 0, //uint32_t hpoint_val,
  240. duty << 4, //uint32_t duty_val,the least 4 bits are decimal part
  241. fade_direction, //uint32_t increase,
  242. step_num, //uint32_t duty_num,
  243. duty_cyle_num, //uint32_t duty_cycle,
  244. duty_scale //uint32_t duty_scale
  245. );
  246. return ESP_OK;
  247. }
  248. esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty)
  249. {
  250. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  251. LEDC_CHECK(channel <= LEDC_CHANNEL_7, "ledc channel error", ESP_ERR_INVALID_ARG);
  252. ledc_duty_config(speed_mode,
  253. channel, //uint32_t chan_num,
  254. 0, //uint32_t hpoint_val,
  255. duty << 4, //uint32_t duty_val,the least 4 bits are decimal part
  256. 1, //uint32_t increase,
  257. 1, //uint32_t duty_num,
  258. 1, //uint32_t duty_cycle,
  259. 0 //uint32_t duty_scale
  260. );
  261. return ESP_OK;
  262. }
  263. int ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
  264. {
  265. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", (-1));
  266. uint32_t duty = (LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> 4);
  267. return duty;
  268. }
  269. esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz)
  270. {
  271. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", ESP_ERR_INVALID_ARG);
  272. portENTER_CRITICAL(&ledc_spinlock);
  273. esp_err_t ret = ESP_OK;
  274. uint32_t div_num = 0;
  275. uint32_t bit_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.bit_num;
  276. uint32_t timer_source_clk = LEDC.timer_group[speed_mode].timer[timer_num].conf.tick_sel;
  277. uint32_t precision = (0x1 << bit_num);
  278. if(timer_source_clk == LEDC_APB_CLK) {
  279. div_num = ((uint64_t) LEDC_APB_CLK_HZ << 8) / freq_hz / precision;
  280. } else {
  281. div_num = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
  282. }
  283. if(div_num <= 256 || div_num > LEDC_DIV_NUM_HSTIMER0) {
  284. ESP_LOGE(LEDC_TAG, "div param err,div_param=%u", div_num);
  285. ret = ESP_FAIL;
  286. }
  287. LEDC.timer_group[speed_mode].timer[timer_num].conf.div_num = div_num;
  288. portEXIT_CRITICAL(&ledc_spinlock);
  289. return ret;
  290. }
  291. uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num)
  292. {
  293. LEDC_CHECK(speed_mode < LEDC_SPEED_MODE_MAX, "ledc mode error", (0));
  294. portENTER_CRITICAL(&ledc_spinlock);
  295. uint32_t freq = 0;
  296. uint32_t timer_source_clk = LEDC.timer_group[speed_mode].timer[timer_num].conf.tick_sel;
  297. uint32_t bit_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.bit_num;
  298. uint32_t div_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.div_num;
  299. uint32_t precision = (0x1 << bit_num);
  300. if(timer_source_clk == LEDC_APB_CLK) {
  301. freq = ((uint64_t) LEDC_APB_CLK_HZ << 8) / precision / div_num;
  302. } else {
  303. freq = ((uint64_t) LEDC_REF_CLK_HZ << 8) / precision / div_num;
  304. }
  305. portEXIT_CRITICAL(&ledc_spinlock);
  306. return freq;
  307. }