pwm.c 15 KB

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