Advertisement
Guest User

Untitled

a guest
Feb 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.79 KB | None | 0 0
  1. LOCAL struct pwm_single_param pwm_single_toggle[2][PWM_CHANNEL + 1];
  2. LOCAL struct pwm_single_param *pwm_single;
  3.  
  4. LOCAL struct pwm_param0 pwm;
  5.  
  6. //LOCAL uint8 pwm_out_io_num[PWM_CHANNEL] = {PWM_0_OUT_IO_NUM, PWM_1_OUT_IO_NUM, PWM_2_OUT_IO_NUM};
  7.  
  8. LOCAL uint8 pwm_channel_toggle[2];
  9. LOCAL uint8 *pwm_channel;
  10.  
  11. LOCAL uint8 pwm_toggle = 1;
  12. LOCAL uint8 pwm_timer_down = 1;
  13.  
  14. LOCAL uint8 pwm_current_channel = 0;
  15.  
  16. LOCAL uint16 pwm_gpio = 0;
  17.  
  18. //XXX: 0xffffffff/(80000000/16)=35A
  19. #define US_TO_RTC_TIMER_TICKS(t)          \
  20.     ((t) ?                                   \
  21.      (((t) > 0x35A) ?                   \
  22.       (((t)>>2) * ((APB_CLK_FREQ>>4)/250000) + ((t)&0x3) * ((APB_CLK_FREQ>>4)/1000000))  :    \
  23.       (((t) *(APB_CLK_FREQ>>4)) / 1000000)) :    \
  24.      0)
  25.  
  26. //FRC1
  27. #define FRC1_ENABLE_TIMER  BIT7
  28.  
  29. typedef enum {
  30.     DIVDED_BY_1 = 0,
  31.     DIVDED_BY_16 = 4,
  32.     DIVDED_BY_256 = 8,
  33. } TIMER_PREDIVED_MODE;
  34.  
  35. typedef enum {
  36.     TM_LEVEL_INT = 1,
  37.     TM_EDGE_INT   = 0,
  38. } TIMER_INT_MODE;
  39.  
  40. LOCAL void ICACHE_FLASH_ATTR
  41. pwm_insert_sort(struct pwm_single_param pwm[], uint8 n)
  42. {
  43.     uint8 i;
  44.  
  45.     for (i = 1; i < n; i++) {
  46.         if (pwm[i].h_time < pwm[i - 1].h_time) {
  47.             int8 j = i - 1;
  48.             struct pwm_single_param tmp;
  49.  
  50.             os_memcpy(&tmp, &pwm[i], sizeof(struct pwm_single_param));
  51.             os_memcpy(&pwm[i], &pwm[i - 1], sizeof(struct pwm_single_param));
  52.  
  53.             while (tmp.h_time < pwm[j].h_time) {
  54.                 os_memcpy(&pwm[j + 1], &pwm[j], sizeof(struct pwm_single_param));
  55.                 j--;
  56.                 if (j < 0) {
  57.                     break;
  58.                 }
  59.             }
  60.  
  61.             os_memcpy(&pwm[j + 1], &tmp, sizeof(struct pwm_single_param));
  62.         }
  63.     }
  64. }
  65.  
  66. void ICACHE_FLASH_ATTR
  67. pwm_start_iot(void)
  68. {
  69.     uint8 i, j;
  70.  
  71.     struct pwm_single_param *local_single = pwm_single_toggle[pwm_toggle ^ 0x01];
  72.     uint8 *local_channel = &pwm_channel_toggle[pwm_toggle ^ 0x01];
  73.  
  74.     // step 1: init PWM_CHANNEL+1 channels param
  75.     for (i = 0; i < PWM_CHANNELN; i++) {
  76.         uint32 us = pwm.period * pwm.duty[i] / PWM_DEPTH;
  77.         local_single[i].h_time = US_TO_RTC_TIMER_TICKS(us);
  78.         local_single[i].gpio_set = 0;
  79.         local_single[i].gpio_clear = 1 << pwm_out_io_num[i];
  80.     }
  81.  
  82.     local_single[PWM_CHANNELN].h_time = US_TO_RTC_TIMER_TICKS(pwm.period);
  83.     local_single[PWM_CHANNELN].gpio_set = pwm_gpio;
  84.     local_single[PWM_CHANNELN].gpio_clear = 0;
  85.  
  86.     // step 2: sort, small to big
  87.     pwm_insert_sort(local_single, PWM_CHANNELN + 1);
  88.  
  89.     *local_channel = PWM_CHANNELN + 1;
  90.  
  91.     // step 3: combine same duty channels
  92.     for (i = PWM_CHANNELN; i > 0; i--) {
  93.         if (local_single[i].h_time == local_single[i - 1].h_time) {
  94.             local_single[i - 1].gpio_set |= local_single[i].gpio_set;
  95.             local_single[i - 1].gpio_clear |= local_single[i].gpio_clear;
  96.  
  97.             for (j = i + 1; j < *local_channel; j++) {
  98.                 os_memcpy(&local_single[j - 1], &local_single[j], sizeof(struct pwm_single_param));
  99.             }
  100.  
  101.             (*local_channel)--;
  102.         }
  103.     }
  104.  
  105.     // step 4: cacl delt time
  106.     for (i = *local_channel - 1; i > 0; i--) {
  107.         local_single[i].h_time -= local_single[i - 1].h_time;
  108.     }
  109.  
  110.     // step 5: last channel needs to clean
  111.     local_single[*local_channel-1].gpio_clear = 0;
  112.  
  113.     // step 6: if first channel duty is 0, remove it
  114.     if (local_single[0].h_time == 0) {
  115.         local_single[*local_channel - 1].gpio_set &= ~local_single[0].gpio_clear;
  116.         local_single[*local_channel - 1].gpio_clear |= local_single[0].gpio_clear;
  117.  
  118.         for (i = 1; i < *local_channel; i++) {
  119.             os_memcpy(&local_single[i - 1], &local_single[i], sizeof(struct pwm_single_param));
  120.         }
  121.  
  122.         (*local_channel)--;
  123.     }
  124.  
  125.     // if timer is down, need to set gpio and start timer
  126.     if (pwm_timer_down == 1) {
  127.         pwm_channel = local_channel;
  128.         pwm_single = local_single;
  129.         // start
  130.         gpio_output_set(local_single[0].gpio_set, local_single[0].gpio_clear, pwm_gpio, 0);
  131.  
  132.         // yeah, if all channels' duty is 0 or 255, don't need to start timer, otherwise start...
  133.         if (*local_channel != 1) {
  134.             pwm_timer_down = 0;
  135.             RTC_REG_WRITE(FRC1_LOAD_ADDRESS, local_single[0].h_time);
  136.         }
  137.     }
  138.  
  139.     if (pwm_toggle == 1) {
  140.         pwm_toggle = 0;
  141.     } else {
  142.         pwm_toggle = 1;
  143.     }
  144. }
  145.  
  146. /******************************************************************************
  147.  * FunctionName : pwm_set_duty
  148.  * Description  : set each channel's duty params
  149.  * Parameters   : uint8 duty    : 0 ~ PWM_DEPTH
  150.  *                uint8 channel : channel index
  151.  * Returns      : NONE
  152. *******************************************************************************/
  153. void ICACHE_FLASH_ATTR
  154. pwm_set_duty_iot(uint8 duty, uint8 channel)
  155. {
  156.     if (duty < 1) {
  157.         pwm.duty[channel] = 0;
  158.     } else if (duty >= PWM_DEPTH) {
  159.         pwm.duty[channel] = PWM_DEPTH;
  160.     } else {
  161.         pwm.duty[channel] = duty;
  162.     }
  163. }
  164.  
  165. /******************************************************************************
  166.  * FunctionName : pwm_set_freq
  167.  * Description  : set pwm frequency
  168.  * Parameters   : uint16 freq : 100hz typically
  169.  * Returns      : NONE
  170. *******************************************************************************/
  171. void ICACHE_FLASH_ATTR
  172. pwm_set_freq(uint16 freq)
  173. {
  174.     if (freq > 1000) {
  175.         pwm.freq = 500;
  176.     } else if (freq < 1) {
  177.         pwm.freq = 1;
  178.     } else {
  179.         pwm.freq = freq;
  180.     }
  181.  
  182.     pwm.period = PWM_1S / pwm.freq;
  183. }
  184.  
  185. /******************************************************************************
  186.  * FunctionName : pwm_set_freq_duty
  187.  * Description  : set pwm frequency and each channel's duty
  188.  * Parameters   : uint16 freq : 100hz typically
  189.  *                uint8 *duty : each channel's duty
  190.  * Returns      : NONE
  191. *******************************************************************************/
  192. LOCAL void ICACHE_FLASH_ATTR
  193. pwm_set_freq_duty(uint16 freq, uint8* duty)
  194. {
  195.     uint8 i;
  196.  
  197.     pwm_set_freq(freq);
  198.  
  199.     for (i = 0; i < PWM_CHANNEL; i++) {
  200.         pwm_set_duty_iot(duty[i], i);
  201.     }
  202. }
  203.  
  204. /******************************************************************************
  205.  * FunctionName : pwm_get_duty
  206.  * Description  : get duty of each channel
  207.  * Parameters   : uint8 channel : channel index
  208.  * Returns      : NONE
  209. *******************************************************************************/
  210. uint8 ICACHE_FLASH_ATTR
  211. pwm_get_duty_iot(uint8 channel)
  212. {
  213.     return pwm.duty[channel];
  214. }
  215.  
  216. /******************************************************************************
  217.  * FunctionName : pwm_get_freq
  218.  * Description  : get pwm frequency
  219.  * Parameters   : NONE
  220.  * Returns      : uint16 : pwm frequency
  221. *******************************************************************************/
  222. uint16 ICACHE_FLASH_ATTR
  223. pwm_get_freq(void)
  224. {
  225.     return pwm.freq;
  226. }
  227.  
  228. /******************************************************************************
  229.  * FunctionName : pwm_period_timer
  230.  * Description  : pwm period timer function, output high level,
  231.  *                start each channel's high level timer
  232.  * Parameters   : NONE
  233.  * Returns      : NONE
  234. *******************************************************************************/
  235. LOCAL void
  236. pwm_tim1_intr_handler(void)
  237. {
  238.     RTC_CLR_REG_MASK(FRC1_INT_ADDRESS, FRC1_INT_CLR_MASK);
  239.  
  240.     if (pwm_current_channel == (*pwm_channel - 1)) {
  241.         pwm_single = pwm_single_toggle[pwm_toggle];
  242.         pwm_channel = &pwm_channel_toggle[pwm_toggle];
  243.  
  244.         gpio_output_set(pwm_single[*pwm_channel - 1].gpio_set,
  245.                         pwm_single[*pwm_channel - 1].gpio_clear,
  246.                         pwm_gpio,
  247.                         0);
  248.  
  249.         pwm_current_channel = 0;
  250.  
  251.         if (*pwm_channel != 1) {
  252.             RTC_REG_WRITE(FRC1_LOAD_ADDRESS, pwm_single[pwm_current_channel].h_time);
  253.         } else {
  254.             pwm_timer_down = 1;
  255.         }
  256.     } else {
  257.         gpio_output_set(pwm_single[pwm_current_channel].gpio_set,
  258.                         pwm_single[pwm_current_channel].gpio_clear,
  259.                         pwm_gpio, 0);
  260.  
  261.         pwm_current_channel++;
  262.         RTC_REG_WRITE(FRC1_LOAD_ADDRESS, pwm_single[pwm_current_channel].h_time);
  263.     }
  264. }
  265.  
  266. /******************************************************************************
  267.  * FunctionName : pwm_init
  268.  * Description  : pwm gpio, params and timer initialization
  269.  * Parameters   : uint16 freq : pwm freq param
  270.  *                uint8 *duty : each channel's duty
  271.  * Returns      : NONE
  272. *******************************************************************************/
  273. void ICACHE_FLASH_ATTR
  274. pwm_init_iot()
  275. {
  276.  
  277.  // uint16 freq=500;
  278.   //uint8 duty[]={0,0,0};
  279.  
  280.     uint8 i;
  281.  
  282.     RTC_REG_WRITE(FRC1_CTRL_ADDRESS,  //FRC2_AUTO_RELOAD|
  283.                   DIVDED_BY_16
  284.                   | FRC1_ENABLE_TIMER
  285.                   | TM_EDGE_INT);
  286.     RTC_REG_WRITE(FRC1_LOAD_ADDRESS, 0);
  287.  
  288.    // PIN_FUNC_SELECT(PWM_0_OUT_IO_MUX, PWM_0_OUT_IO_FUNC);
  289.    // PIN_FUNC_SELECT(PWM_1_OUT_IO_MUX, PWM_1_OUT_IO_FUNC);
  290.    // PIN_FUNC_SELECT(PWM_2_OUT_IO_MUX, PWM_2_OUT_IO_FUNC);
  291.  
  292.     for (i = 0; i < PWM_CHANNELN; i++) {
  293. #if !gpioinite
  294.       PIN_FUNC_SELECT(PERIPHSDT[pwm_out_io_num[i]],FUNC_GPIODT[pwm_out_io_num[i]]);
  295. #endif
  296.         pwm_gpio |= (1 << pwm_out_io_num[i]);
  297.     }
  298.  
  299.     load_flash_param(0+1,&dinamic_saved, sizeof(struct dinamic_saved_param));
  300.     pwm_set_freq_duty(freqPWM, dinamic_saved.pwmfl);
  301.     //pwm_set_freq_duty(freq, duty);
  302.  
  303.     pwm_start_iot();
  304.  
  305.      ETS_FRC_TIMER1_INTR_ATTACH(pwm_tim1_intr_handler, NULL);
  306.    //ETS_FRC_TIMER1_NMI_INTR_ATTACH(pwm_tim1_intr_handler);
  307.    
  308.     TM1_EDGE_INT_ENABLE();
  309.     ETS_FRC1_INTR_ENABLE();
  310. }
  311.  
  312. void ICACHE_FLASH_ATTR
  313. pwm_stop()
  314. {
  315.     TM1_EDGE_INT_DISABLE();
  316.     ETS_FRC1_INTR_DISABLE();
  317. }
  318. ///
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement