pwm.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 "user_interface.h"
  17. #include "driver/pwm.h"
  18. // #define PWM_DBG os_printf
  19. #define PWM_DBG
  20. LOCAL struct pwm_single_param pwm_single_toggle[2][PWM_CHANNEL + 1];
  21. LOCAL struct pwm_single_param *pwm_single;
  22. LOCAL struct pwm_param pwm;
  23. // LOCAL uint8 pwm_out_io_num[PWM_CHANNEL] = {PWM_0_OUT_IO_NUM, PWM_1_OUT_IO_NUM, PWM_2_OUT_IO_NUM};
  24. LOCAL int8 pwm_out_io_num[PWM_CHANNEL] = {-1, -1, -1, -1, -1, -1};
  25. LOCAL uint8 pwm_channel_toggle[2];
  26. LOCAL uint8 *pwm_channel;
  27. LOCAL uint8 pwm_toggle = 1;
  28. LOCAL uint8 pwm_timer_down = 1;
  29. LOCAL uint8 pwm_current_channel = 0;
  30. LOCAL uint16 pwm_gpio = 0;
  31. LOCAL uint8 pwm_channel_num = 0;
  32. //XXX: 0xffffffff/(80000000/16)=35A
  33. #define US_TO_RTC_TIMER_TICKS(t) \
  34. ((t) ? \
  35. (((t) > 0x35A) ? \
  36. (((t)>>2) * ((APB_CLK_FREQ>>4)/250000) + ((t)&0x3) * ((APB_CLK_FREQ>>4)/1000000)) : \
  37. (((t) *(APB_CLK_FREQ>>4)) / 1000000)) : \
  38. 0)
  39. //FRC1
  40. #define FRC1_ENABLE_TIMER BIT7
  41. typedef enum {
  42. DIVDED_BY_1 = 0,
  43. DIVDED_BY_16 = 4,
  44. DIVDED_BY_256 = 8,
  45. } TIMER_PREDIVED_MODE;
  46. typedef enum {
  47. TM_LEVEL_INT = 1,
  48. TM_EDGE_INT = 0,
  49. } TIMER_INT_MODE;
  50. LOCAL void ICACHE_FLASH_ATTR
  51. pwm_insert_sort(struct pwm_single_param pwm[], uint8 n)
  52. {
  53. uint8 i;
  54. for (i = 1; i < n; i++) {
  55. if (pwm[i].h_time < pwm[i - 1].h_time) {
  56. int8 j = i - 1;
  57. struct pwm_single_param tmp;
  58. os_memcpy(&tmp, &pwm[i], sizeof(struct pwm_single_param));
  59. os_memcpy(&pwm[i], &pwm[i - 1], sizeof(struct pwm_single_param));
  60. while (tmp.h_time < pwm[j].h_time) {
  61. os_memcpy(&pwm[j + 1], &pwm[j], sizeof(struct pwm_single_param));
  62. j--;
  63. if (j < 0) {
  64. break;
  65. }
  66. }
  67. os_memcpy(&pwm[j + 1], &tmp, sizeof(struct pwm_single_param));
  68. }
  69. }
  70. }
  71. LOCAL volatile uint8 critical = 0;
  72. #define LOCK_PWM(c) do { \
  73. while( (c)==1 ); \
  74. (c) = 1; \
  75. } while (0)
  76. #define UNLOCK_PWM(c) do { \
  77. (c) = 0; \
  78. } while (0)
  79. void ICACHE_FLASH_ATTR
  80. pwm_start(void)
  81. {
  82. uint8 i, j;
  83. PWM_DBG("--Function pwm_start() is called\n");
  84. PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num);
  85. 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]);
  86. 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]);
  87. LOCK_PWM(critical); // enter critical
  88. struct pwm_single_param *local_single = pwm_single_toggle[pwm_toggle ^ 0x01];
  89. uint8 *local_channel = &pwm_channel_toggle[pwm_toggle ^ 0x01];
  90. // step 1: init PWM_CHANNEL+1 channels param
  91. for (i = 0; i < pwm_channel_num; i++) {
  92. uint32 us = pwm.period * pwm.duty[i] / PWM_DEPTH;
  93. local_single[i].h_time = US_TO_RTC_TIMER_TICKS(us);
  94. PWM_DBG("i:%d us:%d ht:%d\n",i,us,local_single[i].h_time);
  95. local_single[i].gpio_set = 0;
  96. local_single[i].gpio_clear = 1 << pin_num[pwm_out_io_num[i]];
  97. }
  98. local_single[pwm_channel_num].h_time = US_TO_RTC_TIMER_TICKS(pwm.period);
  99. local_single[pwm_channel_num].gpio_set = pwm_gpio;
  100. local_single[pwm_channel_num].gpio_clear = 0;
  101. PWM_DBG("i:%d period:%d ht:%d\n",pwm_channel_num,pwm.period,local_single[pwm_channel_num].h_time);
  102. // step 2: sort, small to big
  103. pwm_insert_sort(local_single, pwm_channel_num + 1);
  104. *local_channel = pwm_channel_num + 1;
  105. 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);
  106. // step 3: combine same duty channels
  107. for (i = pwm_channel_num; i > 0; i--) {
  108. if (local_single[i].h_time == local_single[i - 1].h_time) {
  109. local_single[i - 1].gpio_set |= local_single[i].gpio_set;
  110. local_single[i - 1].gpio_clear |= local_single[i].gpio_clear;
  111. for (j = i + 1; j < *local_channel; j++) {
  112. os_memcpy(&local_single[j - 1], &local_single[j], sizeof(struct pwm_single_param));
  113. }
  114. (*local_channel)--;
  115. }
  116. }
  117. 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);
  118. // step 4: cacl delt time
  119. for (i = *local_channel - 1; i > 0; i--) {
  120. local_single[i].h_time -= local_single[i - 1].h_time;
  121. }
  122. // step 5: last channel needs to clean
  123. local_single[*local_channel-1].gpio_clear = 0;
  124. // step 6: if first channel duty is 0, remove it
  125. if (local_single[0].h_time == 0) {
  126. local_single[*local_channel - 1].gpio_set &= ~local_single[0].gpio_clear;
  127. local_single[*local_channel - 1].gpio_clear |= local_single[0].gpio_clear;
  128. for (i = 1; i < *local_channel; i++) {
  129. os_memcpy(&local_single[i - 1], &local_single[i], sizeof(struct pwm_single_param));
  130. }
  131. (*local_channel)--;
  132. }
  133. // if timer is down, need to set gpio and start timer
  134. if (pwm_timer_down == 1) {
  135. pwm_channel = local_channel;
  136. pwm_single = local_single;
  137. // start
  138. gpio_output_set(local_single[0].gpio_set, local_single[0].gpio_clear, pwm_gpio, 0);
  139. // yeah, if all channels' duty is 0 or 255, don't need to start timer, otherwise start...
  140. if (*local_channel != 1) {
  141. pwm_timer_down = 0;
  142. RTC_REG_WRITE(FRC1_LOAD_ADDRESS, local_single[0].h_time);
  143. }
  144. }
  145. if (pwm_toggle == 1) {
  146. pwm_toggle = 0;
  147. } else {
  148. pwm_toggle = 1;
  149. }
  150. UNLOCK_PWM(critical); // leave critical
  151. 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);
  152. }
  153. /******************************************************************************
  154. * FunctionName : pwm_set_duty
  155. * Description : set each channel's duty params
  156. * Parameters : uint8 duty : 0 ~ PWM_DEPTH
  157. * uint8 channel : channel index
  158. * Returns : NONE
  159. *******************************************************************************/
  160. void ICACHE_FLASH_ATTR
  161. pwm_set_duty(uint16 duty, uint8 channel)
  162. {
  163. uint8 i;
  164. for(i=0;i<pwm_channel_num;i++){
  165. if(pwm_out_io_num[i] == channel){
  166. channel = i;
  167. break;
  168. }
  169. }
  170. if(i==pwm_channel_num) // non found
  171. return;
  172. LOCK_PWM(critical); // enter critical
  173. if (duty < 1) {
  174. pwm.duty[channel] = 0;
  175. } else if (duty >= PWM_DEPTH) {
  176. pwm.duty[channel] = PWM_DEPTH;
  177. } else {
  178. pwm.duty[channel] = duty;
  179. }
  180. UNLOCK_PWM(critical); // leave critical
  181. }
  182. /******************************************************************************
  183. * FunctionName : pwm_set_freq
  184. * Description : set pwm frequency
  185. * Parameters : uint16 freq : 100hz typically
  186. * Returns : NONE
  187. *******************************************************************************/
  188. void ICACHE_FLASH_ATTR
  189. pwm_set_freq(uint16 freq, uint8 channel)
  190. {
  191. LOCK_PWM(critical); // enter critical
  192. if (freq > PWM_FREQ_MAX) {
  193. pwm.freq = PWM_FREQ_MAX;
  194. } else if (freq < 1) {
  195. pwm.freq = 1;
  196. } else {
  197. pwm.freq = freq;
  198. }
  199. pwm.period = PWM_1S / pwm.freq;
  200. UNLOCK_PWM(critical); // leave critical
  201. }
  202. /******************************************************************************
  203. * FunctionName : pwm_set_freq_duty
  204. * Description : set pwm frequency and each channel's duty
  205. * Parameters : uint16 freq : 100hz typically
  206. * uint16 *duty : each channel's duty
  207. * Returns : NONE
  208. *******************************************************************************/
  209. LOCAL void ICACHE_FLASH_ATTR
  210. pwm_set_freq_duty(uint16 freq, uint16 *duty)
  211. {
  212. uint8 i;
  213. pwm_set_freq(freq, 0);
  214. for (i = 0; i < PWM_CHANNEL; i++) {
  215. // pwm_set_duty(duty[i], i);
  216. if(pwm_out_io_num[i] != -1)
  217. pwm_set_duty(duty[i], pwm_out_io_num[i]);
  218. }
  219. }
  220. /******************************************************************************
  221. * FunctionName : pwm_get_duty
  222. * Description : get duty of each channel
  223. * Parameters : uint8 channel : channel index
  224. * Returns : NONE
  225. *******************************************************************************/
  226. uint16 ICACHE_FLASH_ATTR
  227. pwm_get_duty(uint8 channel)
  228. {
  229. uint8 i;
  230. for(i=0;i<pwm_channel_num;i++){
  231. if(pwm_out_io_num[i] == channel){
  232. channel = i;
  233. break;
  234. }
  235. }
  236. if(i==pwm_channel_num) // non found
  237. return 0;
  238. return pwm.duty[channel];
  239. }
  240. /******************************************************************************
  241. * FunctionName : pwm_get_freq
  242. * Description : get pwm frequency
  243. * Parameters : NONE
  244. * Returns : uint16 : pwm frequency
  245. *******************************************************************************/
  246. uint16 ICACHE_FLASH_ATTR
  247. pwm_get_freq(uint8 channel)
  248. {
  249. return pwm.freq;
  250. }
  251. /******************************************************************************
  252. * FunctionName : pwm_period_timer
  253. * Description : pwm period timer function, output high level,
  254. * start each channel's high level timer
  255. * Parameters : NONE
  256. * Returns : NONE
  257. *******************************************************************************/
  258. LOCAL void ICACHE_RAM_ATTR
  259. pwm_tim1_intr_handler(void *p)
  260. {
  261. (void)p;
  262. uint8 local_toggle = pwm_toggle; // pwm_toggle may change outside
  263. RTC_CLR_REG_MASK(FRC1_INT_ADDRESS, FRC1_INT_CLR_MASK);
  264. if (pwm_current_channel >= (*pwm_channel - 1)) { // *pwm_channel may change outside
  265. pwm_single = pwm_single_toggle[local_toggle];
  266. pwm_channel = &pwm_channel_toggle[local_toggle];
  267. gpio_output_set(pwm_single[*pwm_channel - 1].gpio_set,
  268. pwm_single[*pwm_channel - 1].gpio_clear,
  269. pwm_gpio,
  270. 0);
  271. pwm_current_channel = 0;
  272. if (*pwm_channel != 1) {
  273. RTC_REG_WRITE(FRC1_LOAD_ADDRESS, pwm_single[pwm_current_channel].h_time);
  274. } else {
  275. pwm_timer_down = 1;
  276. }
  277. } else {
  278. gpio_output_set(pwm_single[pwm_current_channel].gpio_set,
  279. pwm_single[pwm_current_channel].gpio_clear,
  280. pwm_gpio, 0);
  281. pwm_current_channel++;
  282. RTC_REG_WRITE(FRC1_LOAD_ADDRESS, pwm_single[pwm_current_channel].h_time);
  283. }
  284. }
  285. /******************************************************************************
  286. * FunctionName : pwm_init
  287. * Description : pwm gpio, params and timer initialization
  288. * Parameters : uint16 freq : pwm freq param
  289. * uint16 *duty : each channel's duty
  290. * Returns : NONE
  291. *******************************************************************************/
  292. void ICACHE_FLASH_ATTR
  293. pwm_init(uint16 freq, uint16 *duty)
  294. {
  295. uint8 i;
  296. RTC_REG_WRITE(FRC1_CTRL_ADDRESS, //FRC2_AUTO_RELOAD|
  297. DIVDED_BY_16
  298. | FRC1_ENABLE_TIMER
  299. | TM_EDGE_INT);
  300. RTC_REG_WRITE(FRC1_LOAD_ADDRESS, 0);
  301. // PIN_FUNC_SELECT(PWM_0_OUT_IO_MUX, PWM_0_OUT_IO_FUNC);
  302. // PIN_FUNC_SELECT(PWM_1_OUT_IO_MUX, PWM_1_OUT_IO_FUNC);
  303. // PIN_FUNC_SELECT(PWM_2_OUT_IO_MUX, PWM_2_OUT_IO_FUNC);
  304. // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_0_OUT_IO_NUM), 0);
  305. // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_1_OUT_IO_NUM), 0);
  306. // GPIO_OUTPUT_SET(GPIO_ID_PIN(PWM_2_OUT_IO_NUM), 0);
  307. for (i = 0; i < PWM_CHANNEL; i++) {
  308. // pwm_gpio |= (1 << pwm_out_io_num[i]);
  309. pwm_gpio = 0;
  310. pwm.duty[i] = 0;
  311. }
  312. pwm_set_freq(500, 0);
  313. // pwm_set_freq_duty(freq, duty);
  314. pwm_start();
  315. ETS_FRC_TIMER1_INTR_ATTACH(pwm_tim1_intr_handler, NULL);
  316. TM1_EDGE_INT_ENABLE();
  317. ETS_FRC1_INTR_ENABLE();
  318. }
  319. bool ICACHE_FLASH_ATTR
  320. pwm_add(uint8 channel){
  321. PWM_DBG("--Function pwm_add() is called. channel:%d\n", channel);
  322. PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num);
  323. 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]);
  324. PWM_DBG("pwm.duty[0]:%d,[1]:%d,[2]:%d\n",pwm.duty[0],pwm.duty[1],pwm.duty[2]);
  325. uint8 i;
  326. for(i=0;i<PWM_CHANNEL;i++){
  327. if(pwm_out_io_num[i]==channel) // already exist
  328. return true;
  329. if(pwm_out_io_num[i] == -1){ // empty exist
  330. LOCK_PWM(critical); // enter critical
  331. pwm_out_io_num[i] = channel;
  332. pwm.duty[i] = 0;
  333. pwm_gpio |= (1 << pin_num[channel]);
  334. PIN_FUNC_SELECT(pin_mux[channel], pin_func[channel]);
  335. 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;
  336. pwm_channel_num++;
  337. UNLOCK_PWM(critical); // leave critical
  338. return true;
  339. }
  340. }
  341. return false;
  342. }
  343. bool ICACHE_FLASH_ATTR
  344. pwm_delete(uint8 channel){
  345. PWM_DBG("--Function pwm_delete() is called. channel:%d\n", channel);
  346. PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num);
  347. 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]);
  348. PWM_DBG("pwm.duty[0]:%d,[1]:%d,[2]:%d\n",pwm.duty[0],pwm.duty[1],pwm.duty[2]);
  349. uint8 i,j;
  350. for(i=0;i<pwm_channel_num;i++){
  351. if(pwm_out_io_num[i]==channel){ // exist
  352. LOCK_PWM(critical); // enter critical
  353. pwm_out_io_num[i] = -1;
  354. pwm_gpio &= ~(1 << pin_num[channel]); //clear the bit
  355. for(j=i;j<pwm_channel_num-1;j++){
  356. pwm_out_io_num[j] = pwm_out_io_num[j+1];
  357. pwm.duty[j] = pwm.duty[j+1];
  358. }
  359. pwm_out_io_num[pwm_channel_num-1] = -1;
  360. pwm.duty[pwm_channel_num-1] = 0;
  361. pwm_channel_num--;
  362. UNLOCK_PWM(critical); // leave critical
  363. return true;
  364. }
  365. }
  366. // non found
  367. return true;
  368. }
  369. bool ICACHE_FLASH_ATTR
  370. pwm_exist(uint8 channel){
  371. PWM_DBG("--Function pwm_exist() is called. channel:%d\n", channel);
  372. PWM_DBG("pwm_gpio:%x,pwm_channel_num:%d\n",pwm_gpio,pwm_channel_num);
  373. 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]);
  374. PWM_DBG("pwm.duty[0]:%d,[1]:%d,[2]:%d\n",pwm.duty[0],pwm.duty[1],pwm.duty[2]);
  375. uint8 i;
  376. for(i=0;i<PWM_CHANNEL;i++){
  377. if(pwm_out_io_num[i]==channel) // exist
  378. return true;
  379. }
  380. return false;
  381. }