ledc.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  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. //TODO: to use APIs in esp_log.h.
  21. #define LEDC_DBG_WARING_ENABLE (0)
  22. #define LEDC_DBG_ERROR_ENABLE (0)
  23. #define LEDC_INFO_ENABLE (0)
  24. #define LEDC_DBG_ENABLE (0)
  25. //DBG INFOR
  26. #if LEDC_DBG_ENABLE
  27. #define LEDC_DBG(format,...) do{\
  28. ets_printf("[dbg][%s#%u]",__FUNCTION__,__LINE__);\
  29. ets_printf(format,##__VA_ARGS__);\
  30. }while(0)
  31. #else
  32. #define LEDC_DBG(...)
  33. #endif
  34. #if LEDC_INFO_ENABLE
  35. #define LEDC_INFO(format,...) do{\
  36. ets_printf("[info][%s#%u]",__FUNCTION__,__LINE__);\
  37. ets_printf(format,##__VA_ARGS__);\
  38. }while(0)
  39. #else
  40. #define LEDC_INFO(...)
  41. #endif
  42. #if LEDC_DBG_WARING_ENABLE
  43. #define LEDC_WARING(format,...) do{\
  44. ets_printf("[waring][%s#%u]",__FUNCTION__,__LINE__);\
  45. ets_printf(format,##__VA_ARGS__);\
  46. }while(0)
  47. #else
  48. #define LEDC_WARING(...)
  49. #endif
  50. #if LEDC_DBG_ERROR_ENABLE
  51. #define LEDC_ERROR(format,...) do{\
  52. ets_printf("[error][%s#%u]",__FUNCTION__,__LINE__);\
  53. ets_printf(format,##__VA_ARGS__);\
  54. }while(0)
  55. #else
  56. #define LEDC_ERROR(...)
  57. #endif
  58. static portMUX_TYPE ledc_spinlock = portMUX_INITIALIZER_UNLOCKED;
  59. static bool ledc_is_valid_channel(uint32_t channel)
  60. {
  61. if(channel > LEDC_CHANNEL_7) {
  62. LEDC_ERROR("LEDC CHANNEL ERR: %d\n",channel);
  63. return false;
  64. }
  65. return true;
  66. }
  67. static bool ledc_is_valid_mode(uint32_t mode)
  68. {
  69. if(mode >= LEDC_SPEED_MODE_MAX) {
  70. LEDC_ERROR("LEDC MODE ERR: %d\n",mode);
  71. return false;
  72. }
  73. return true;
  74. }
  75. static bool ledc_is_valid_timer(int timer)
  76. {
  77. if(timer > LEDC_TIMER_3) {
  78. LEDC_ERROR("LEDC TIMER ERR: %d\n", timer);
  79. return false;
  80. }
  81. return true;
  82. }
  83. 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)
  84. {
  85. if(!ledc_is_valid_mode(speed_mode)) {
  86. return ESP_ERR_INVALID_ARG;
  87. }
  88. if(!ledc_is_valid_timer(timer_sel)) {
  89. return ESP_ERR_INVALID_ARG;
  90. }
  91. portENTER_CRITICAL(&ledc_spinlock);
  92. LEDC.timer_group[speed_mode].timer[timer_sel].conf.div_num = div_num;
  93. LEDC.timer_group[speed_mode].timer[timer_sel].conf.tick_sel = clk_src;
  94. LEDC.timer_group[speed_mode].timer[timer_sel].conf.bit_num = bit_num;
  95. if(speed_mode != LEDC_HIGH_SPEED_MODE) {
  96. LEDC.timer_group[speed_mode].timer[timer_sel].conf.low_speed_update = 1;
  97. }
  98. portEXIT_CRITICAL(&ledc_spinlock);
  99. return ESP_OK;
  100. }
  101. static esp_err_t ledc_duty_config(ledc_mode_t speed_mode, uint32_t channel_num, uint32_t hpoint_val, uint32_t duty_val,
  102. uint32_t duty_direction, uint32_t duty_num, uint32_t duty_cycle, uint32_t duty_scale)
  103. {
  104. portENTER_CRITICAL(&ledc_spinlock);
  105. LEDC.channel_group[speed_mode].channel[channel_num].hpoint.hpoint = hpoint_val;
  106. LEDC.channel_group[speed_mode].channel[channel_num].duty.duty = duty_val;
  107. LEDC.channel_group[speed_mode].channel[channel_num].conf1.val = ((duty_direction & LEDC_DUTY_INC_HSCH0_V) << LEDC_DUTY_INC_HSCH0_S) |
  108. ((duty_num & LEDC_DUTY_NUM_HSCH0_V) << LEDC_DUTY_NUM_HSCH0_S) |
  109. ((duty_cycle & LEDC_DUTY_CYCLE_HSCH0_V) << LEDC_DUTY_CYCLE_HSCH0_S) |
  110. ((duty_scale & LEDC_DUTY_SCALE_HSCH0_V) << LEDC_DUTY_SCALE_HSCH0_S);
  111. portEXIT_CRITICAL(&ledc_spinlock);
  112. return ESP_OK;
  113. }
  114. esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx)
  115. {
  116. if(!ledc_is_valid_mode(speed_mode)) {
  117. return ESP_ERR_INVALID_ARG;
  118. }
  119. if(!ledc_is_valid_timer(timer_idx)) {
  120. return ESP_ERR_INVALID_ARG;
  121. }
  122. portENTER_CRITICAL(&ledc_spinlock);
  123. LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel = timer_idx;
  124. portEXIT_CRITICAL(&ledc_spinlock);
  125. return ESP_OK;
  126. }
  127. esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel)
  128. {
  129. if(!ledc_is_valid_mode(speed_mode)) {
  130. return ESP_ERR_INVALID_ARG;
  131. }
  132. if(!ledc_is_valid_timer(timer_sel)) {
  133. return ESP_ERR_INVALID_ARG;
  134. }
  135. portENTER_CRITICAL(&ledc_spinlock);
  136. LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 1;
  137. LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 0;
  138. portEXIT_CRITICAL(&ledc_spinlock);
  139. return ESP_OK;
  140. }
  141. esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel)
  142. {
  143. if(!ledc_is_valid_mode(speed_mode)) {
  144. return ESP_ERR_INVALID_ARG;
  145. }
  146. if(!ledc_is_valid_timer(timer_sel)) {
  147. return ESP_ERR_INVALID_ARG;
  148. }
  149. portENTER_CRITICAL(&ledc_spinlock);
  150. LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 1;
  151. portEXIT_CRITICAL(&ledc_spinlock);
  152. return ESP_OK;
  153. }
  154. esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel)
  155. {
  156. if(!ledc_is_valid_mode(speed_mode)) {
  157. return ESP_ERR_INVALID_ARG;
  158. }
  159. if(!ledc_is_valid_timer(timer_sel)) {
  160. return ESP_ERR_INVALID_ARG;
  161. }
  162. portENTER_CRITICAL(&ledc_spinlock);
  163. LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 0;
  164. portEXIT_CRITICAL(&ledc_spinlock);
  165. return ESP_OK;
  166. }
  167. static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, uint32_t channel, ledc_intr_type_t type)
  168. {
  169. if(!ledc_is_valid_mode(speed_mode)) {
  170. return ESP_ERR_INVALID_ARG;
  171. }
  172. uint32_t value;
  173. uint32_t intr_type = type;
  174. portENTER_CRITICAL(&ledc_spinlock);
  175. value = LEDC.int_ena.val;
  176. if(intr_type == LEDC_INTR_FADE_END) {
  177. LEDC.int_ena.val = value | BIT(LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S + channel);
  178. } else {
  179. LEDC.int_ena.val = (value & (~(BIT(LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S + channel))));
  180. }
  181. portEXIT_CRITICAL(&ledc_spinlock);
  182. return ESP_OK;
  183. }
  184. esp_err_t ledc_isr_register(uint32_t ledc_intr_num, void (*fn)(void*), void * arg)
  185. {
  186. if(fn == NULL) {
  187. return ESP_ERR_INVALID_ARG;
  188. }
  189. portENTER_CRITICAL(&ledc_spinlock);
  190. ESP_INTR_DISABLE(ledc_intr_num);
  191. intr_matrix_set(xPortGetCoreID(), ETS_LEDC_INTR_SOURCE, ledc_intr_num);
  192. xt_set_interrupt_handler(ledc_intr_num, fn, arg);
  193. ESP_INTR_ENABLE(ledc_intr_num);
  194. portEXIT_CRITICAL(&ledc_spinlock);
  195. return ESP_OK;
  196. }
  197. esp_err_t ledc_timer_config(ledc_timer_config_t* timer_conf)
  198. {
  199. int freq_hz = timer_conf->freq_hz;
  200. int bit_num = timer_conf->bit_num;
  201. int timer_num = timer_conf->timer_num;
  202. int speed_mode = timer_conf->speed_mode;
  203. if(!ledc_is_valid_mode(speed_mode)) {
  204. return ESP_ERR_INVALID_ARG;
  205. }
  206. if(freq_hz == 0 || bit_num == 0 || bit_num > LEDC_TIMER_15_BIT) {
  207. LEDC_ERROR("freq_hz=%u bit_num=%u\n", freq_hz, bit_num);
  208. return ESP_ERR_INVALID_ARG;
  209. }
  210. if(timer_num > LEDC_TIMER_3) {
  211. LEDC_ERROR("Time Select %u\n", timer_num);
  212. return ESP_ERR_INVALID_ARG;
  213. }
  214. esp_err_t ret = ESP_OK;
  215. uint32_t precision = (0x1 << bit_num); //2**depth
  216. uint64_t div_param = ((uint64_t) LEDC_APB_CLK_HZ << 8) / freq_hz / precision; //8bit fragment
  217. int timer_clk_src;
  218. /*Fail ,because the div_num overflow or too small*/
  219. if(div_param <= 256 || div_param > LEDC_DIV_NUM_HSTIMER0_V) { //REF TICK
  220. /*Selet the reference tick*/
  221. div_param = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
  222. if(div_param <= 256 || div_param > LEDC_DIV_NUM_HSTIMER0_V) {
  223. LEDC_ERROR("div param err,div_param=%u\n", div_param);
  224. ret = ESP_FAIL;
  225. }
  226. timer_clk_src = LEDC_REF_TICK;
  227. } else { //APB TICK
  228. timer_clk_src = LEDC_APB_CLK;
  229. }
  230. /*set timer parameters*/
  231. /*timer settings decide the clk of counter and the period of PWM*/
  232. ledc_timer_set(speed_mode, timer_num, div_param, bit_num, timer_clk_src);
  233. /* reset timer.*/
  234. ledc_timer_rst(speed_mode, timer_num);
  235. return ret;
  236. }
  237. esp_err_t ledc_channel_config(ledc_channel_config_t* ledc_conf)
  238. {
  239. uint32_t speed_mode = ledc_conf->speed_mode;
  240. uint32_t gpio_num = ledc_conf->gpio_num;
  241. uint32_t ledc_channel = ledc_conf->channel;
  242. uint32_t timer_select = ledc_conf->timer_sel;
  243. uint32_t intr_type = ledc_conf->intr_type;
  244. uint32_t duty = ledc_conf->duty;
  245. if(!ledc_is_valid_channel(ledc_channel)) {
  246. return ESP_ERR_INVALID_ARG;
  247. }
  248. if(!ledc_is_valid_mode(speed_mode)) {
  249. return ESP_ERR_INVALID_ARG;
  250. }
  251. if(!GPIO_IS_VALID_OUTPUT_GPIO(gpio_num)) {
  252. LEDC_ERROR("GPIO number error: IO%d\n ", gpio_num);
  253. return ESP_ERR_INVALID_ARG;
  254. }
  255. if(timer_select > LEDC_TIMER_3) {
  256. LEDC_ERROR("Time Select %u\n", timer_select);
  257. return ESP_ERR_INVALID_ARG;
  258. }
  259. esp_err_t ret = ESP_OK;
  260. /*set channel parameters*/
  261. /* channel parameters decide how the waveform looks like in one period*/
  262. /* set channel duty, duty range is (0 ~ ((2 ** bit_num) - 1))*/
  263. ledc_set_duty(speed_mode, ledc_channel, duty);
  264. /*update duty settings*/
  265. ledc_update_duty(speed_mode, ledc_channel);
  266. /*bind the channel with the timer*/
  267. ledc_bind_channel_timer(speed_mode, ledc_channel, timer_select);
  268. /*set interrupt type*/
  269. ledc_enable_intr_type(speed_mode, ledc_channel, intr_type);
  270. LEDC_INFO("LEDC_PWM CHANNEL %1u|GPIO %02u|Duty %04u|Time %01u\n",
  271. ledc_channel, gpio_num, duty, timer_select
  272. );
  273. /*set LEDC signal in gpio matrix*/
  274. PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
  275. gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
  276. gpio_matrix_out(gpio_num, LEDC_HS_SIG_OUT0_IDX + ledc_channel, 0, 0);
  277. return ret;
  278. }
  279. esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
  280. {
  281. if(!ledc_is_valid_mode(speed_mode)) {
  282. return ESP_ERR_INVALID_ARG;
  283. }
  284. if(!ledc_is_valid_channel(channel)) {
  285. return ESP_ERR_INVALID_ARG;
  286. }
  287. portENTER_CRITICAL(&ledc_spinlock);
  288. LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 1;
  289. LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 1;
  290. portEXIT_CRITICAL(&ledc_spinlock);
  291. return ESP_OK;
  292. }
  293. esp_err_t ledc_stop(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t idle_level)
  294. {
  295. if(!ledc_is_valid_mode(speed_mode)) {
  296. return ESP_ERR_INVALID_ARG;
  297. }
  298. if(!ledc_is_valid_channel(channel)) {
  299. return ESP_ERR_INVALID_ARG;
  300. }
  301. portENTER_CRITICAL(&ledc_spinlock);
  302. LEDC.channel_group[speed_mode].channel[channel].conf0.idle_lv = idle_level & 0x1;
  303. LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 0;
  304. LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 0;
  305. portEXIT_CRITICAL(&ledc_spinlock);
  306. return ESP_OK;
  307. }
  308. esp_err_t ledc_set_fade(ledc_mode_t speed_mode, uint32_t channel, uint32_t duty, ledc_duty_direction_t fade_direction,
  309. uint32_t step_num, uint32_t duty_cyle_num, uint32_t duty_scale)
  310. {
  311. if(!ledc_is_valid_mode(speed_mode)) {
  312. return ESP_ERR_INVALID_ARG;
  313. }
  314. if(!ledc_is_valid_channel(channel)) {
  315. return ESP_ERR_INVALID_ARG;
  316. }
  317. if(fade_direction > LEDC_DUTY_DIR_INCREASE) {
  318. LEDC_ERROR("Duty direction err\n");
  319. return ESP_ERR_INVALID_ARG;
  320. }
  321. if(step_num > LEDC_DUTY_NUM_HSCH0_V || duty_cyle_num > LEDC_DUTY_CYCLE_HSCH0_V || duty_scale > LEDC_DUTY_SCALE_HSCH0_V) {
  322. LEDC_ERROR("step_num=%u duty_cyle_num=%u duty_scale=%u\n", step_num, duty_cyle_num, duty_scale);
  323. return ESP_ERR_INVALID_ARG;
  324. }
  325. ledc_duty_config(speed_mode,
  326. channel, //uint32_t chan_num,
  327. 0, //uint32_t hpoint_val,
  328. duty << 4, //uint32_t duty_val,the least 4 bits are decimal part
  329. fade_direction, //uint32_t increase,
  330. step_num, //uint32_t duty_num,
  331. duty_cyle_num, //uint32_t duty_cycle,
  332. duty_scale //uint32_t duty_scale
  333. );
  334. return ESP_OK;
  335. }
  336. esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t duty)
  337. {
  338. if(!ledc_is_valid_mode(speed_mode)) {
  339. return ESP_ERR_INVALID_ARG;
  340. }
  341. if(!ledc_is_valid_channel(channel)) {
  342. return ESP_ERR_INVALID_ARG;
  343. }
  344. ledc_duty_config(speed_mode,
  345. channel, //uint32_t chan_num,
  346. 0, //uint32_t hpoint_val,
  347. duty << 4, //uint32_t duty_val,the least 4 bits are decimal part
  348. 1, //uint32_t increase,
  349. 1, //uint32_t duty_num,
  350. 1, //uint32_t duty_cycle,
  351. 0 //uint32_t duty_scale
  352. );
  353. return ESP_OK;
  354. }
  355. int ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel)
  356. {
  357. if(!ledc_is_valid_mode(speed_mode)) {
  358. return -1;
  359. }
  360. uint32_t duty = (LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> 4);
  361. return duty;
  362. }
  363. esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz)
  364. {
  365. if(!ledc_is_valid_mode(speed_mode)) {
  366. return ESP_ERR_INVALID_ARG;
  367. }
  368. portENTER_CRITICAL(&ledc_spinlock);
  369. esp_err_t ret = ESP_OK;
  370. uint32_t div_num = 0;
  371. uint32_t bit_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.bit_num;
  372. uint32_t timer_source_clk = LEDC.timer_group[speed_mode].timer[timer_num].conf.tick_sel;
  373. uint32_t precision = (0x1 << bit_num);
  374. if(timer_source_clk == LEDC_APB_CLK) {
  375. div_num = ((uint64_t) LEDC_APB_CLK_HZ << 8) / freq_hz / precision;
  376. } else {
  377. div_num = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
  378. }
  379. if(div_num <= 256 || div_num > LEDC_DIV_NUM_HSTIMER0) {
  380. LEDC_ERROR("div param err,div_param=%u\n", div_num);
  381. ret = ESP_FAIL;
  382. }
  383. LEDC.timer_group[speed_mode].timer[timer_num].conf.div_num = div_num;
  384. portEXIT_CRITICAL(&ledc_spinlock);
  385. return ret;
  386. }
  387. uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num)
  388. {
  389. if(!ledc_is_valid_mode(speed_mode)) {
  390. return 0;
  391. }
  392. portENTER_CRITICAL(&ledc_spinlock);
  393. uint32_t freq = 0;
  394. uint32_t timer_source_clk = LEDC.timer_group[speed_mode].timer[timer_num].conf.tick_sel;
  395. uint32_t bit_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.bit_num;
  396. uint32_t div_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.div_num;
  397. uint32_t precision = (0x1 << bit_num);
  398. if(timer_source_clk == LEDC_APB_CLK) {
  399. freq = ((uint64_t) LEDC_APB_CLK_HZ << 8) / precision / div_num;
  400. } else {
  401. freq = ((uint64_t) LEDC_REF_CLK_HZ << 8) / precision / div_num;
  402. }
  403. portEXIT_CRITICAL(&ledc_spinlock);
  404. return freq;
  405. }