pwm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: pwm.c
  5. *
  6. * Description: pwm driver
  7. *
  8. * Modification history:
  9. * 2014/5/1, v1.0 create this file.
  10. *******************************************************************************/
  11. // ESP32 has own pwm driver in libdriver.a
  12. #ifdef __ESP8266__
  13. #include "platform.h"
  14. #include "ets_sys.h"
  15. #include "os_type.h"
  16. #include "osapi.h"
  17. #include "gpio.h"
  18. #include "hw_timer.h"
  19. #include "esp_misc.h"
  20. #include "user_interface.h"
  21. #include "driver/pwm.h"
  22. // #define PWM_DBG os_printf
  23. #define PWM_DBG
  24. // Enabling the next line will cause the interrupt handler to toggle
  25. // this output pin during processing so that the timing is obvious
  26. //
  27. // #define PWM_DBG_PIN 13 // GPIO7
  28. #ifdef PWM_DBG_PIN
  29. #define PWM_DBG_PIN_HIGH() GPIO_REG_WRITE(GPIO_OUT_W1TS_ADDRESS, 1 << PWM_DBG_PIN)
  30. #define PWM_DBG_PIN_LOW() GPIO_REG_WRITE(GPIO_OUT_W1TC_ADDRESS, 1 << PWM_DBG_PIN)
  31. #else
  32. #define PWM_DBG_PIN_HIGH()
  33. #define PWM_DBG_PIN_LOW()
  34. #endif
  35. LOCAL struct pwm_single_param pwm_single_toggle[2][PWM_CHANNEL + 1];
  36. LOCAL struct pwm_single_param *pwm_single;
  37. LOCAL struct pwm_param pwm;
  38. // LOCAL uint8 pwm_out_io_num[PWM_CHANNEL] = {PWM_0_OUT_IO_NUM, PWM_1_OUT_IO_NUM, PWM_2_OUT_IO_NUM};
  39. LOCAL int8 pwm_out_io_num[PWM_CHANNEL] = {-1, -1, -1, -1, -1, -1};
  40. LOCAL uint8 pwm_channel_toggle[2];
  41. LOCAL uint8 *pwm_channel;
  42. // Toggle flips between 1 and 0 when we make updates so that the interrupt code
  43. // cn switch cleanly between the two states. The cinterrupt handler uses either
  44. // the pwm_single_toggle[0] or pwm_single_toggle[1]
  45. // pwm_toggle indicates which state should be used on the *next* timer interrupt
  46. // freq boundary.
  47. LOCAL uint8 pwm_toggle = 1;
  48. LOCAL volatile uint8 pwm_current_toggle = 1;
  49. LOCAL uint8 pwm_timer_down = 1;
  50. LOCAL uint8 pwm_current_channel = 0;
  51. LOCAL uint16 pwm_gpio = 0;
  52. LOCAL uint8 pwm_channel_num = 0;
  53. LOCAL void ICACHE_RAM_ATTR pwm_tim1_intr_handler(uint32_t p);
  54. #define TIMER_OWNER ((uint32_t) 'P')
  55. LOCAL void ICACHE_FLASH_ATTR
  56. pwm_insert_sort(struct pwm_single_param pwm[], uint8 n)
  57. {
  58. uint8 i;
  59. for (i = 1; i < n; i++) {
  60. if (pwm[i].h_time < pwm[i - 1].h_time) {
  61. int8 j = i - 1;
  62. struct pwm_single_param tmp;
  63. os_memcpy(&tmp, &pwm[i], sizeof(struct pwm_single_param));
  64. while (tmp.h_time < pwm[j].h_time) {
  65. os_memcpy(&pwm[j + 1], &pwm[j], sizeof(struct pwm_single_param));
  66. j--;
  67. if (j < 0) {
  68. break;
  69. }
  70. }
  71. os_memcpy(&pwm[j + 1], &tmp, sizeof(struct pwm_single_param));
  72. }
  73. }
  74. }
  75. // Returns FALSE if we cannot start
  76. bool ICACHE_FLASH_ATTR
  77. pwm_start(void)
  78. {
  79. uint8 i, j;
  80. PWM_DBG("--Function pwm_start() is called\n");
  81. PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num);
  82. PWM_DBG("pwm_out_io_num[0]:%d,[1]:%d,[2]:%d\n",pwm_out_io_num[0],pwm_out_io_num[1],pwm_out_io_num[2]);
  83. PWM_DBG("pwm.period:%d,pwm.duty[0]:%d,[1]:%d,[2]:%d\n",pwm.period,pwm.duty[0],pwm.duty[1],pwm.duty[2]);
  84. // First we need to make sure that the interrupt handler is running
  85. // out of the same set of params as we expect
  86. while (!pwm_timer_down && pwm_toggle != pwm_current_toggle) {
  87. os_delay_us(100);
  88. }
  89. if (pwm_timer_down) {
  90. pwm_toggle = pwm_current_toggle;
  91. }
  92. uint8_t new_toggle = pwm_toggle ^ 0x01;
  93. struct pwm_single_param *local_single = pwm_single_toggle[new_toggle];
  94. uint8 *local_channel = &pwm_channel_toggle[new_toggle];
  95. // step 1: init PWM_CHANNEL+1 channels param
  96. for (i = 0; i < pwm_channel_num; i++) {
  97. uint32 us = pwm.period * pwm.duty[i] / PWM_DEPTH;
  98. local_single[i].h_time = US_TO_RTC_TIMER_TICKS(us);
  99. PWM_DBG("i:%d us:%d ht:%d\n",i,us,local_single[i].h_time);
  100. local_single[i].gpio_set = 0;
  101. local_single[i].gpio_clear = 1 << pin_num[pwm_out_io_num[i]];
  102. }
  103. local_single[pwm_channel_num].h_time = US_TO_RTC_TIMER_TICKS(pwm.period);
  104. local_single[pwm_channel_num].gpio_set = pwm_gpio;
  105. local_single[pwm_channel_num].gpio_clear = 0;
  106. PWM_DBG("i:%d period:%d ht:%d\n",pwm_channel_num,pwm.period,local_single[pwm_channel_num].h_time);
  107. // step 2: sort, small to big
  108. pwm_insert_sort(local_single, pwm_channel_num + 1);
  109. *local_channel = pwm_channel_num + 1;
  110. PWM_DBG("1channel:%d,single[0]:%d,[1]:%d,[2]:%d,[3]:%d\n",*local_channel,local_single[0].h_time,local_single[1].h_time,local_single[2].h_time,local_single[3].h_time);
  111. // step 3: combine same duty channels (or nearly the same duty). If there is
  112. // under 2 us between pwm outputs, then treat them as the same.
  113. for (i = pwm_channel_num; i > 0; i--) {
  114. if (local_single[i].h_time <= local_single[i - 1].h_time + US_TO_RTC_TIMER_TICKS(2)) {
  115. local_single[i - 1].gpio_set |= local_single[i].gpio_set;
  116. local_single[i - 1].gpio_clear |= local_single[i].gpio_clear;
  117. for (j = i + 1; j < *local_channel; j++) {
  118. os_memcpy(&local_single[j - 1], &local_single[j], sizeof(struct pwm_single_param));
  119. }
  120. (*local_channel)--;
  121. }
  122. }
  123. PWM_DBG("2channel:%d,single[0]:%d,[1]:%d,[2]:%d,[3]:%d\n",*local_channel,local_single[0].h_time,local_single[1].h_time,local_single[2].h_time,local_single[3].h_time);
  124. // step 4: cacl delt time
  125. for (i = *local_channel - 1; i > 0; i--) {
  126. local_single[i].h_time -= local_single[i - 1].h_time;
  127. }
  128. // step 5: last channel needs to clean
  129. local_single[*local_channel-1].gpio_clear = 0;
  130. // step 6: if first channel duty is 0, remove it
  131. if (local_single[0].h_time == 0) {
  132. local_single[*local_channel - 1].gpio_set &= ~local_single[0].gpio_clear;
  133. local_single[*local_channel - 1].gpio_clear |= local_single[0].gpio_clear;
  134. for (i = 1; i < *local_channel; i++) {
  135. os_memcpy(&local_single[i - 1], &local_single[i], sizeof(struct pwm_single_param));
  136. }
  137. (*local_channel)--;
  138. }
  139. // Make the new ones active
  140. pwm_toggle = new_toggle;
  141. // if timer is down, need to set gpio and start timer
  142. if (pwm_timer_down == 1) {
  143. pwm_channel = local_channel;
  144. pwm_single = local_single;
  145. pwm_current_toggle = pwm_toggle;
  146. // start
  147. gpio_output_set(local_single[0].gpio_set, local_single[0].gpio_clear, pwm_gpio, 0);
  148. // yeah, if all channels' duty is 0 or 255, don't need to start timer, otherwise start...
  149. if (*local_channel != 1) {
  150. PWM_DBG("Need to setup timer\n");
  151. if (!platform_hw_timer_init(TIMER_OWNER, NMI_SOURCE, FALSE)) {
  152. return FALSE;
  153. }
  154. pwm_timer_down = 0;
  155. platform_hw_timer_set_func(TIMER_OWNER, pwm_tim1_intr_handler, 0);
  156. platform_hw_timer_arm_ticks(TIMER_OWNER, local_single[0].h_time);
  157. } else {
  158. PWM_DBG("Timer left idle\n");
  159. platform_hw_timer_close(TIMER_OWNER);
  160. }
  161. } else {
  162. // ensure that all outputs are outputs
  163. gpio_output_set(0, 0, pwm_gpio, 0);
  164. }
  165. #ifdef PWM_DBG_PIN
  166. // Enable as output
  167. gpio_output_set(0, 0, 1 << PWM_DBG_PIN, 0);
  168. #endif
  169. PWM_DBG("3channel:%d,single[0]:%d,[1]:%d,[2]:%d,[3]:%d\n",*local_channel,local_single[0].h_time,local_single[1].h_time,local_single[2].h_time,local_single[3].h_time);
  170. return TRUE;
  171. }
  172. /******************************************************************************
  173. * FunctionName : pwm_set_duty
  174. * Description : set each channel's duty params
  175. * Parameters : uint8 duty : 0 ~ PWM_DEPTH
  176. * uint8 channel : channel index
  177. * Returns : NONE
  178. *******************************************************************************/
  179. void ICACHE_FLASH_ATTR
  180. pwm_set_duty(uint16 duty, uint8 channel)
  181. {
  182. uint8 i;
  183. for(i=0;i<pwm_channel_num;i++){
  184. if(pwm_out_io_num[i] == channel){
  185. channel = i;
  186. break;
  187. }
  188. }
  189. if(i==pwm_channel_num) // non found
  190. return;
  191. if (duty < 1) {
  192. pwm.duty[channel] = 0;
  193. } else if (duty >= PWM_DEPTH) {
  194. pwm.duty[channel] = PWM_DEPTH;
  195. } else {
  196. pwm.duty[channel] = duty;
  197. }
  198. }
  199. /******************************************************************************
  200. * FunctionName : pwm_set_freq
  201. * Description : set pwm frequency
  202. * Parameters : uint16 freq : 100hz typically
  203. * Returns : NONE
  204. *******************************************************************************/
  205. void ICACHE_FLASH_ATTR
  206. pwm_set_freq(uint16 freq, uint8 channel)
  207. {
  208. if (freq > PWM_FREQ_MAX) {
  209. pwm.freq = PWM_FREQ_MAX;
  210. } else if (freq < 1) {
  211. pwm.freq = 1;
  212. } else {
  213. pwm.freq = freq;
  214. }
  215. pwm.period = PWM_1S / pwm.freq;
  216. }
  217. /******************************************************************************
  218. * FunctionName : pwm_set_freq_duty
  219. * Description : set pwm frequency and each channel's duty
  220. * Parameters : uint16 freq : 100hz typically
  221. * uint16 *duty : each channel's duty
  222. * Returns : NONE
  223. *******************************************************************************/
  224. LOCAL void ICACHE_FLASH_ATTR
  225. pwm_set_freq_duty(uint16 freq, uint16 *duty)
  226. {
  227. uint8 i;
  228. pwm_set_freq(freq, 0);
  229. for (i = 0; i < PWM_CHANNEL; i++) {
  230. // pwm_set_duty(duty[i], i);
  231. if(pwm_out_io_num[i] != -1)
  232. pwm_set_duty(duty[i], pwm_out_io_num[i]);
  233. }
  234. }
  235. /******************************************************************************
  236. * FunctionName : pwm_get_duty
  237. * Description : get duty of each channel
  238. * Parameters : uint8 channel : channel index
  239. * Returns : NONE
  240. *******************************************************************************/
  241. uint16 ICACHE_FLASH_ATTR
  242. pwm_get_duty(uint8 channel)
  243. {
  244. uint8 i;
  245. for(i=0;i<pwm_channel_num;i++){
  246. if(pwm_out_io_num[i] == channel){
  247. channel = i;
  248. break;
  249. }
  250. }
  251. if(i==pwm_channel_num) // non found
  252. return 0;
  253. return pwm.duty[channel];
  254. }
  255. /******************************************************************************
  256. * FunctionName : pwm_get_freq
  257. * Description : get pwm frequency
  258. * Parameters : NONE
  259. * Returns : uint16 : pwm frequency
  260. *******************************************************************************/
  261. uint16 ICACHE_FLASH_ATTR
  262. pwm_get_freq(uint8 channel)
  263. {
  264. return pwm.freq;
  265. }
  266. /******************************************************************************
  267. * FunctionName : pwm_period_timer
  268. * Description : pwm period timer function, output high level,
  269. * start each channel's high level timer
  270. * Parameters : NONE
  271. * Returns : NONE
  272. *******************************************************************************/
  273. LOCAL void ICACHE_RAM_ATTR
  274. pwm_tim1_intr_handler(uint32_t p)
  275. {
  276. (void)p;
  277. PWM_DBG_PIN_HIGH();
  278. int offset = 0;
  279. while (1) {
  280. if (pwm_current_channel >= (*pwm_channel - 1)) {
  281. pwm_single = pwm_single_toggle[pwm_toggle];
  282. pwm_channel = &pwm_channel_toggle[pwm_toggle];
  283. pwm_current_toggle = pwm_toggle;
  284. gpio_output_set(pwm_single[*pwm_channel - 1].gpio_set,
  285. pwm_single[*pwm_channel - 1].gpio_clear,
  286. 0,
  287. 0);
  288. pwm_current_channel = 0;
  289. if (*pwm_channel == 1) {
  290. pwm_timer_down = 1;
  291. break;
  292. }
  293. } else {
  294. gpio_output_set(pwm_single[pwm_current_channel].gpio_set,
  295. pwm_single[pwm_current_channel].gpio_clear,
  296. 0, 0);
  297. pwm_current_channel++;
  298. }
  299. int next_time = pwm_single[pwm_current_channel].h_time;
  300. // Delay now holds the time (in ticks) since when the last timer expiry was
  301. PWM_DBG_PIN_LOW();
  302. int delay = platform_hw_timer_get_delay_ticks(TIMER_OWNER) + 4 - offset;
  303. offset += next_time;
  304. next_time = next_time - delay;
  305. if (next_time > US_TO_RTC_TIMER_TICKS(4)) {
  306. PWM_DBG_PIN_HIGH();
  307. platform_hw_timer_arm_ticks(TIMER_OWNER, next_time);
  308. break;
  309. }
  310. PWM_DBG_PIN_HIGH();
  311. }
  312. PWM_DBG_PIN_LOW();
  313. }
  314. /******************************************************************************
  315. * FunctionName : pwm_init
  316. * Description : pwm gpio, params and timer initialization
  317. * Parameters : uint16 freq : pwm freq param
  318. * uint16 *duty : each channel's duty
  319. * Returns : void
  320. *******************************************************************************/
  321. void ICACHE_FLASH_ATTR
  322. pwm_init(uint16 freq, uint16 *duty)
  323. {
  324. uint8 i;
  325. // PIN_FUNC_SELECT(PWM_0_OUT_IO_MUX, PWM_0_OUT_IO_FUNC);
  326. // PIN_FUNC_SELECT(PWM_1_OUT_IO_MUX, PWM_1_OUT_IO_FUNC);
  327. // PIN_FUNC_SELECT(PWM_2_OUT_IO_MUX, PWM_2_OUT_IO_FUNC);
  328. // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_0_OUT_IO_NUM), 0);
  329. // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_1_OUT_IO_NUM), 0);
  330. // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_2_OUT_IO_NUM), 0);
  331. for (i = 0; i < PWM_CHANNEL; i++) {
  332. // pwm_gpio |= (1 << pwm_out_io_num[i]);
  333. pwm_gpio = 0;
  334. pwm.duty[i] = 0;
  335. }
  336. pwm_set_freq(500, 0);
  337. // pwm_set_freq_duty(freq, duty);
  338. pwm_start();
  339. PWM_DBG("pwm_init returning\n");
  340. }
  341. bool ICACHE_FLASH_ATTR
  342. pwm_add(uint8 channel){
  343. PWM_DBG("--Function pwm_add() is called. channel:%d\n", channel);
  344. PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num);
  345. PWM_DBG("pwm_out_io_num[0]:%d,[1]:%d,[2]:%d\n",pwm_out_io_num[0],pwm_out_io_num[1],pwm_out_io_num[2]);
  346. PWM_DBG("pwm.duty[0]:%d,[1]:%d,[2]:%d\n",pwm.duty[0],pwm.duty[1],pwm.duty[2]);
  347. uint8 i;
  348. for(i=0;i<PWM_CHANNEL;i++){
  349. if(pwm_out_io_num[i]==channel) // already exist
  350. return true;
  351. if(pwm_out_io_num[i] == -1){ // empty exist
  352. pwm_out_io_num[i] = channel;
  353. pwm.duty[i] = 0;
  354. pwm_gpio |= (1 << pin_num[channel]);
  355. PIN_FUNC_SELECT(pin_mux[channel], pin_func[channel]);
  356. GPIO_REG_WRITE(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[channel])), GPIO_REG_READ(GPIO_PIN_ADDR(GPIO_ID_PIN(pin_num[channel]))) & (~ GPIO_PIN_PAD_DRIVER_SET(GPIO_PAD_DRIVER_ENABLE))); //disable open drain;
  357. pwm_channel_num++;
  358. return true;
  359. }
  360. }
  361. return false;
  362. }
  363. bool ICACHE_FLASH_ATTR
  364. pwm_delete(uint8 channel){
  365. PWM_DBG("--Function pwm_delete() is called. channel:%d\n", channel);
  366. PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num);
  367. PWM_DBG("pwm_out_io_num[0]:%d,[1]:%d,[2]:%d\n",pwm_out_io_num[0],pwm_out_io_num[1],pwm_out_io_num[2]);
  368. PWM_DBG("pwm.duty[0]:%d,[1]:%d,[2]:%d\n",pwm.duty[0],pwm.duty[1],pwm.duty[2]);
  369. uint8 i,j;
  370. for(i=0;i<pwm_channel_num;i++){
  371. if(pwm_out_io_num[i]==channel){ // exist
  372. pwm_out_io_num[i] = -1;
  373. pwm_gpio &= ~(1 << pin_num[channel]); //clear the bit
  374. for(j=i;j<pwm_channel_num-1;j++){
  375. pwm_out_io_num[j] = pwm_out_io_num[j+1];
  376. pwm.duty[j] = pwm.duty[j+1];
  377. }
  378. pwm_out_io_num[pwm_channel_num-1] = -1;
  379. pwm.duty[pwm_channel_num-1] = 0;
  380. pwm_channel_num--;
  381. return true;
  382. }
  383. }
  384. // non found
  385. return true;
  386. }
  387. bool ICACHE_FLASH_ATTR
  388. pwm_exist(uint8 channel){
  389. PWM_DBG("--Function pwm_exist() is called. channel:%d\n", channel);
  390. PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num);
  391. PWM_DBG("pwm_out_io_num[0]:%d,[1]:%d,[2]:%d\n",pwm_out_io_num[0],pwm_out_io_num[1],pwm_out_io_num[2]);
  392. PWM_DBG("pwm.duty[0]:%d,[1]:%d,[2]:%d\n",pwm.duty[0],pwm.duty[1],pwm.duty[2]);
  393. uint8 i;
  394. for(i=0;i<PWM_CHANNEL;i++){
  395. if(pwm_out_io_num[i]==channel) // exist
  396. return true;
  397. }
  398. return false;
  399. }
  400. #endif