LEDC_DUTY_DIR_INCREASE = 1, /*LEDC duty increase direction */
} ledc_duty_direction_t;
typedef enum {
- LEDC_REF_TICK = 0, // 1MhZ
- LEDC_APB_CLK, //80Mhz
-}ledc_timer_src_t;
+ LEDC_REF_TICK = 0, /*LEDC timer clock divided from reference tick(1Mhz) */
+ LEDC_APB_CLK, /*LEDC timer clock divided from APB clock(80Mhz)*/
+} ledc_clk_src_t;
typedef enum {
- LEDC_TIMER0 = 0, /*LEDC source time TIME0 */
- LEDC_TIMER1, /*LEDC source time TIME1 */
- LEDC_TIMER2, /*LEDC source time TIME2 */
- LEDC_TIMER3, /*LEDC source time TIME3 */
-} ledc_timer_source_t;
+ LEDC_TIMER0 = 0, /*LEDC source timer TIMER0 */
+ LEDC_TIMER1, /*LEDC source timer TIMER1 */
+ LEDC_TIMER2, /*LEDC source timer TIMER2 */
+ LEDC_TIMER3, /*LEDC source timer TIMER3 */
+} ledc_timer_t;
typedef enum {
LEDC_CHANNEL_0 = 0, /*LEDC channel 0 */
LEDC_CHANNEL_1, /*LEDC channel 1 */
LEDC_CHANNEL_7, /*LEDC channel 7 */
} ledc_channel_t;
typedef enum {
- LEDC_DUTY_DEPTH_10_BIT = 10, /*LEDC PWM depth 10Bit */
- LEDC_DUTY_DEPTH_11_BIT = 11, /*LEDC PWM depth 11Bit */
- LEDC_DUTY_DEPTH_12_BIT = 12, /*LEDC PWM depth 12Bit */
- LEDC_DUTY_DEPTH_13_BIT = 13, /*LEDC PWM depth 13Bit */
- LEDC_DUTY_DEPTH_14_BIT = 14, /*LEDC PWM depth 14Bit */
- LEDC_DUTY_DEPTH_15_BIT = 15, /*LEDC PWM depth 15Bit */
-} ledc_duty_depth_t;
+ LEDC_TIMER_10_BIT = 10, /*LEDC PWM depth 10Bit */
+ LEDC_TIMER_11_BIT = 11, /*LEDC PWM depth 11Bit */
+ LEDC_TIMER_12_BIT = 12, /*LEDC PWM depth 12Bit */
+ LEDC_TIMER_13_BIT = 13, /*LEDC PWM depth 13Bit */
+ LEDC_TIMER_14_BIT = 14, /*LEDC PWM depth 14Bit */
+ LEDC_TIMER_15_BIT = 15, /*LEDC PWM depth 15Bit */
+} ledc_timer_bit_t;
typedef struct ledc_channel_t_config {
- int gpio_num; /*the LEDC output gpio_num, if you want to use gpio16, ledc_config_t.gpio_num = 16*/
- ledc_mode_t speed_mode; /*LEDC speed speed_mode*/
+ int gpio_num; /*the LEDC output gpio_num, if you want to use gpio16, gpio_num = 16*/
+ ledc_mode_t speed_mode; /*LEDC speed speed_mode, high-speed mode or low-speed mode*/
ledc_channel_t channel; /*LEDC channel(0 - 7)*/
- ledc_intr_type_t intr_type; /*configure interrupt , Fade interrupt enable or Fade interrupt disable*/
- ledc_timer_source_t timer_src; /*Select the timer source of channel (0 - 3)*/
+ ledc_intr_type_t intr_type; /*configure interrupt, Fade interrupt enable or Fade interrupt disable*/
+ ledc_timer_t timer_sel; /*Select the timer source of channel (0 - 3)*/
uint32_t freq_hz; /*LEDC channel frequency(Hz)*/
- uint32_t duty; /*LEDC channel duty,the duty range is [0,(2**duty_depth) - 1],*/
- ledc_duty_depth_t duty_depth; /*LEDC channel duty depth*/
+ uint32_t duty; /*LEDC channel duty, the duty range is [0, (2**bit_num) - 1], */
+ ledc_timer_bit_t bit_num; /*LEDC channel duty depth*/
} ledc_config_t;
/**
* @brief LEDC common configuration
*
- * User this Function,configure LEDC with the given channel/output gpio_num/interrupt/source timer/frequency(Hz)/LEDC depth
+ * User this Function, configure LEDC with the given channel/output gpio_num/interrupt/source timer/frequency(Hz)/LEDC depth
*
* @param[in] ledc_config_t
* ledc_config_t.speed_mode : LEDC speed speed_mode
* ledc_config_t.gpio_num : LEDC output gpio_num, if you want to use gpio16, ledc_config_t.gpio_num = 16
* ledc_config_t.channel : LEDC channel(0 - 7)
- * ledc_config_t.intr_type : configure interrupt , Fade interrupt enable or Fade interrupt disable
- * ledc_config_t.timer_src : Select the timer source of channel (0 - 3)
- * When different channel ,select same timer ,their freq_hz and duty_depth must be the same
+ * ledc_config_t.intr_type : configure interrupt, Fade interrupt enable or Fade interrupt disable
+ * ledc_config_t.timer_sel : Select the timer source of channel (0 - 3)
+ * When different channel, select same timer, their freq_hz and bit_num must be the same
* ledc_config_t.freq_hz : LEDC channel frequency(Hz),
- * When different channel ,select same time ,their freq_hz and duty_depth must be same
- * ledc_config_t.duty : LEDC channel duty,the duty range is [0,(2**duty_depth) - 1],
- * ledc_config_t.duty_depth : LEDC channel duty depth
- * When different channel ,select same time ,their freq_hz and duty_depth must be same
+ * When different channel, select same time, their freq_hz and bit_num must be same
+ * ledc_config_t.duty : LEDC channel duty, the duty range is [0, (2**bit_num) - 1],
+ * ledc_config_t.bit_num : LEDC channel duty depth
+ * When different channel, select same time, their freq_hz and bit_num must be same
* @return ESP_OK: success
* ESP_ERR_INVALID_ARG: parameter error
- * ESP_FAIL: Can not find a proper pre-devider number base on the given frequency and the current duty_depth.
+ * ESP_FAIL: Can not find a proper pre-divider number base on the given frequency and the current bit_num.
*
*/
esp_err_t ledc_config(ledc_config_t* ledc_conf);
/**
- * @brief LEDC start
+ * @brief LEDC update channel parameters
*
* Call this function to activate the LEDC updated parameters.
- * After ledc_set_duty,ledc_set_fade, we need to call this function to update the settings.
+ * After ledc_set_duty, ledc_set_fade, we need to call this function to update the settings.
*
- * @param[in] speed_mode : select the LEDC speed_mode, high-speed speed_mode and low-speed speed_mode,now we only support high-speed speed_mode ,next will add low-speed speed_mode
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
*
* @param[in] channel : LEDC channel(0-7)
*
/**
* @brief LEDC stop
*
- * Disable LEDC output,and set idle level
+ * Disable LEDC output, and set idle level
*
- * @param[in] speed_mode : select the LEDC speed_mode,high-speed speed_mode and low-speed speed_mode,now we only support high-speed speed_mode ,next will add low-speed speed_mode
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
*
* @param[in] channel : LEDC channel(0-7)
*
*
* Set LEDC frequency(Hz)
*
- * @param[in] speed_mode : select the LEDC speed_mode,high-speed speed_mode and low-speed speed_mode,now we only support high-speed speed_mode ,next will add low-speed speed_mode
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
*
- * @param[in] channel : current channel(0-7)
+ * @param[in] timer_num : LEDC timer index(0-3)
*
* @param[in] freq_hz : set the LEDC frequency
*
* @return ESP_OK: success
* ESP_ERR_INVALID_ARG: parameter error
- * ESP_FAIL: Can not find a proper pre-devider number base on the given frequency and the current duty_depth.
+ * ESP_FAIL: Can not find a proper pre-divider number base on the given frequency and the current bit_num.
*/
-esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t freq_hz);
+esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz);
/**
* @brief LEDC get channel frequency(Hz)
*
- * @param[in] speed_mode : select the LEDC speed_mode,high-speed speed_mode and low-speed speed_mode,now we only support high-speed speed_mode ,next will add low-speed speed_mode
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
*
- * @param[in] channel : LEDC channel(0-7)
+ * @param[in] timer_num : LEDC timer index(0-3)
*
* @return 0 : error
* others : current LEDC frequency
*
*/
-uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_channel_t channel);
+uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num);
/**
* @brief LEDC set duty
*
- * Set LEDC duty ,After the function calls the ledc_update function, the function can take effect.
+ * Set LEDC duty, After the function calls the ledc_update function, the function can take effect.
*
- * @param[in] speed_mode : select the LEDC speed_mode,high-speed speed_mode and low-speed speed_mode,now we only support high-speed speed_mode ,next will add low-speed speed_mode
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
*
- * @param[in] channel : LEDC channel(0-7)
+ * @param[in] channel : LEDC channel(0-7)
*
- * @param[in] duty : set the LEDC duty ,the duty range is [0,(2**duty_depth) - 1]
+ * @param[in] duty : set the LEDC duty, the duty range is [0, (2**bit_num) - 1]
*
* @return ESP_OK: success
* ESP_ERR_INVALID_ARG: parameter error
/**
* @brief LEDC get duty
*
- * @param[in] speed_mode : select the LEDC speed_mode,high-speed speed_mode and low-speed speed_mode,now we only support high-speed speed_mode ,next will add low-speed speed_mode
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
*
* @param[in] channel : LEDC channel(0-7)
*
/**
* @brief LEDC set gradient
*
- * Set LEDC gradient , After the function calls the ledc_update function , the function can take effect.
+ * Set LEDC gradient, After the function calls the ledc_update function, the function can take effect.
*
- * @param[in] speed_mode : select the LEDC speed_mode,high-speed speed_mode and low-speed speed_mode,now we only support high-speed speed_mode ,next will add low-speed speed_mode
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
*
* @param[in] channel : LEDC channel(0-7)
*
- * @param[in] duty : set the start of the gradient duty , the duty range is [0,(2**duty_depth) - 1]
+ * @param[in] duty : set the start of the gradient duty, the duty range is [0, (2**bit_num) - 1]
*
* @param[in] gradule_direction : set the direction of the gradient
*
* Users should know that which CPU is running and then pick a INUM that is not used by system.
* We can find the information of INUM and interrupt level in soc.h.
* TODO: to move INUM options to menu_config
- * @parameter uint32_t ledc_intr_num : LEDC interrupt number,check the info in soc.h, and please see the core-isa.h for more details
+ * @parameter uint32_t ledc_intr_num : LEDC interrupt number, check the info in soc.h, and please see the core-isa.h for more details
* @parameter void (* fn)(void* ) : interrupt handler function.
* Note that the handler function MUST be defined with attribution of "IRAM_ATTR".
* @parameter void * arg : parameter for handler function
*
* @return ESP_OK : success ;
- * ESP_ERR_INVALID_ARG : fucntion ptr error.
+ * ESP_ERR_INVALID_ARG : function ptr error.
*/
esp_err_t ledc_isr_register(uint32_t ledc_intr_num, void (*fn)(void*), void * arg);
+/**
+ * @brief configure LEDC settings
+ *
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
+ *
+ * @param[in] timer_sel : timer index(0-3), there are 4 timers in LEDC module
+ *
+ * @param[in] div_num : timer clock divide number, the timer clock is divided from the selected clock source
+ *
+ * @param[in] bit_num : the count number of one period, counter range is 0 ~ ((2 ** bit_num) - 1)
+ *
+ * @param[in] clk_src : select LEDC source clock.
+ *
+ * @return -1: parameter error
+ * other value: current LEDC duty
+ *
+ */
+esp_err_t ledc_timer_config(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t div_num, uint32_t bit_num, ledc_clk_src_t clk_src);
+
+/**
+ * @brief reset LEDC timer
+ *
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
+ *
+ * @param[in] timer_sel : LEDC timer index(0-3)
+ *
+ *
+ * @return ESP_ERR_INVALID_ARG: parameter error
+ * ESP_OK: success
+ *
+ */
+esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel);
+/**
+ * @brief pause LEDC timer counter
+ *
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
+ *
+ * @param[in] timer_sel : LEDC timer index(0-3)
+ *
+ *
+ * @return ESP_ERR_INVALID_ARG: parameter error
+ * ESP_OK: success
+ *
+ */
+esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel);
+/**
+ * @brief pause LEDC timer resume
+ *
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
+ *
+ * @param[in] timer_sel : LEDC timer index(0-3)
+ *
+ *
+ * @return ESP_ERR_INVALID_ARG: parameter error
+ * ESP_OK: success
+ *
+ */
+esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel);
+/**
+ * @brief bind LEDC channel with the selected timer
+ *
+ * @param[in] speed_mode : select the LEDC speed_mode, high-speed mode and low-speed mode, now we only support high-speed mode. We will access low-speed mode in next version
+ *
+ * @param[in] channel : LEDC channel index(0-7)
+ *
+ * @param[in] timer_idx : LEDC timer index(0-3)
+ *
+ *
+ * @return ESP_ERR_INVALID_ARG: parameter error
+ * ESP_OK: success
+ *
+ */
+esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx);
/***************************EXAMPLE**********************************
*
* ----------------EXAMPLE OF LEDC SETTING ---------------------
* ledc_config_t ledc_conf = {
* .channel = LEDC_CHANNEL_0; //set LEDC channel 0
- * .duty = 1000; //set the duty for initialization.(duty range is 0 ~ ((2**duty_depth)-1)
- * .freq_hz = 1000; //set frequency , e.g.,1KHz
+ * .duty = 1000; //set the duty for initialization.(duty range is 0 ~ ((2**bit_num)-1)
+ * .freq_hz = 1000; //set frequency, e.g., 1KHz
* .gpio_num = 16; //GPIO number
- * .intr_type = LEDC_INTR_FADE_END; //GPIO INTR TYPE, as an example,we enable fade_end interrupt here.
- * .duty_depth = LEDC_DUTY_DEPTH_12_BIT; //set duty_depth , (duty range is 0 ~ ((2**duty_depth)-1)
+ * .intr_type = LEDC_INTR_FADE_END; //GPIO INTR TYPE, as an example, we enable fade_end interrupt here.
+ * .bit_num = LEDC_TIMER_12_BIT; //set bit_num, (duty range is 0 ~ ((2**bit_num)-1)
* .speed_mode = LEDC_HIGH_SPEED_MODE; //set LEDC mode, from ledc_mode_t
- * .timer_src = LEDC_TIMER0; //set LEDC timer source, if different channel use one timer, the frequency and duty_depth of these channels should be the same
+ * .timer_sel = LEDC_TIMER0; //set LEDC timer source, if different channel use one timer, the frequency and bit_num of these channels should be the same
* }
* ledc_config(&ledc_conf); //setup the configuration
* ----------------EXAMPLE OF SETTING DUTY --- -----------------
* uint32_t ledc_channel = LEDC_CHANNEL_0; //LEDC channel(0-73)
- * uint32_t duty = 2000; //duty range is 0 ~ ((2**duty_depth)-1)
- * LEDC_set_duty(LEDC_HIGH_SPEED_MODE,ledc_channel,duty); //set speed mode, channel, and duty.
- * ledc_update(LEDC_HIGH_SPEED_MODE,ledc_channel); //after set duty, we need to call ledc_update to update the settings.
+ * uint32_t duty = 2000; //duty range is 0 ~ ((2**bit_num)-1)
+ * LEDC_set_duty(LEDC_HIGH_SPEED_MODE, ledc_channel, duty); //set speed mode, channel, and duty.
+ * ledc_update(LEDC_HIGH_SPEED_MODE, ledc_channel); //after set duty, we need to call ledc_update to update the settings.
*
*
* ----------------EXAMPLE OF LEDC INTERRUPT ------------------
* uint32_t intr_st = LEDC.int_st.val; //read LEDC interrupt status.
*
* //you will find which channels have triggered fade_end interrupt here,
- * //then , you can post some event to RTOS queue to process the event.
+ * //then, you can post some event to RTOS queue to process the event.
* //later we will add a queue in the driver code.
*
* LEDC.int_clr.val = intr_st; //clear LEDC interrupt status.
return 1;
}
-static esp_err_t ledc_timer_config(ledc_mode_t speed_mode, uint32_t timer_sel, uint32_t div_num, uint32_t timer_lim,ledc_timer_source_t clk)
+static int ledc_is_valid_timer(int timer)
+{
+ if(timer > LEDC_TIMER3) {
+ LEDC_ERROR("LEDC TIMER ERR: %d\n", timer);
+ return 0;
+ }
+ return 1;
+}
+
+esp_err_t ledc_timer_config(ledc_mode_t speed_mode, ledc_timer_t timer_sel, uint32_t div_num, uint32_t bit_num, ledc_clk_src_t clk_src)
{
if(!ledc_is_valid_mode(speed_mode)) {
return ESP_ERR_INVALID_ARG;
}
+ if(!ledc_is_valid_timer(timer_sel)) {
+ return ESP_ERR_INVALID_ARG;
+ }
portENTER_CRITICAL(&ledc_spinlock);
- LEDC.high_speed_timer[timer_sel].conf.div_num = div_num;
- LEDC.high_speed_timer[timer_sel].conf.tick_sel = clk;
- LEDC.high_speed_timer[timer_sel].conf.timer_lim = timer_lim;
+ LEDC.timer_group[speed_mode].timer[timer_sel].conf.div_num = div_num;
+ LEDC.timer_group[speed_mode].timer[timer_sel].conf.tick_sel = clk_src;
+ LEDC.timer_group[speed_mode].timer[timer_sel].conf.bit_num = bit_num;
+ if(speed_mode == LEDC_HIGH_SPEED_MODE) {
+ LEDC.timer_group[speed_mode].timer[timer_sel].conf.low_speed_update = 1;
+ }
portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
}
static esp_err_t ledc_duty_config(ledc_mode_t speed_mode, uint32_t channel_num, uint32_t hpoint_val, uint32_t duty_val,
uint32_t duty_direction, uint32_t duty_num, uint32_t duty_cycle, uint32_t duty_scale)
+{
+ portENTER_CRITICAL(&ledc_spinlock);
+ LEDC.channel_group[speed_mode].channel[channel_num].hpoint.hpoint = hpoint_val;
+ LEDC.channel_group[speed_mode].channel[channel_num].duty.duty = duty_val;
+ LEDC.channel_group[speed_mode].channel[channel_num].conf1.val = ((duty_direction & LEDC_DUTY_INC_HSCH0_V) << LEDC_DUTY_INC_HSCH0_S) |
+ ((duty_num & LEDC_DUTY_NUM_HSCH0_V) << LEDC_DUTY_NUM_HSCH0_S) |
+ ((duty_cycle & LEDC_DUTY_CYCLE_HSCH0_V) << LEDC_DUTY_CYCLE_HSCH0_S) |
+ ((duty_scale & LEDC_DUTY_SCALE_HSCH0_V) << LEDC_DUTY_SCALE_HSCH0_S);
+ portEXIT_CRITICAL(&ledc_spinlock);
+ return ESP_OK;
+}
+
+esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx)
+{
+ if(!ledc_is_valid_mode(speed_mode)) {
+ return ESP_ERR_INVALID_ARG;
+ }
+ if(!ledc_is_valid_timer(timer_idx)) {
+ return ESP_ERR_INVALID_ARG;
+ }
+ portENTER_CRITICAL(&ledc_spinlock);
+ LEDC.channel_group[speed_mode].channel[channel].conf0.timer_sel = timer_idx;
+ portEXIT_CRITICAL(&ledc_spinlock);
+ return ESP_OK;
+}
+
+esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel)
{
if(!ledc_is_valid_mode(speed_mode)) {
return ESP_ERR_INVALID_ARG;
}
+ if(!ledc_is_valid_timer(timer_sel)) {
+ return ESP_ERR_INVALID_ARG;
+ }
portENTER_CRITICAL(&ledc_spinlock);
- LEDC.high_speed_channel[channel_num].hpoint.hpoint = hpoint_val;
- LEDC.high_speed_channel[channel_num].duty.duty = duty_val;
- LEDC.high_speed_channel[channel_num].conf1.duty_inc = duty_direction;
- LEDC.high_speed_channel[channel_num].conf1.duty_num = duty_num;
- LEDC.high_speed_channel[channel_num].conf1.duty_cycle = duty_cycle;
- LEDC.high_speed_channel[channel_num].conf1.duty_scale = duty_scale;
+ LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 1;
+ LEDC.timer_group[speed_mode].timer[timer_sel].conf.rst = 0;
portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
}
-static esp_err_t ledc_set_channel_timer(ledc_mode_t speed_mode, uint32_t channel, uint32_t timer_idx)
+esp_err_t ledc_timer_pause(ledc_mode_t speed_mode, uint32_t timer_sel)
{
if(!ledc_is_valid_mode(speed_mode)) {
return ESP_ERR_INVALID_ARG;
}
+ if(!ledc_is_valid_timer(timer_sel)) {
+ return ESP_ERR_INVALID_ARG;
+ }
portENTER_CRITICAL(&ledc_spinlock);
- LEDC.high_speed_channel[channel].conf0.timer_sel = timer_idx;
+ LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 1;
portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
}
-static esp_err_t ledc_timer_rst(ledc_mode_t speed_mode, uint32_t timer_sel)
+esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, uint32_t timer_sel)
{
if(!ledc_is_valid_mode(speed_mode)) {
return ESP_ERR_INVALID_ARG;
}
+ if(!ledc_is_valid_timer(timer_sel)) {
+ return ESP_ERR_INVALID_ARG;
+ }
portENTER_CRITICAL(&ledc_spinlock);
- LEDC.high_speed_timer[timer_sel].conf.rst = 1;
- LEDC.high_speed_timer[timer_sel].conf.rst = 0;
+ LEDC.timer_group[speed_mode].timer[timer_sel].conf.pause = 0;
portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
}
+
static esp_err_t ledc_enable_intr_type(ledc_mode_t speed_mode, uint32_t channel, ledc_intr_type_t type)
{
if(!ledc_is_valid_mode(speed_mode)) {
uint32_t intr_type = type;
portENTER_CRITICAL(&ledc_spinlock);
value = LEDC.int_ena.val;
- if(intr_type & LEDC_INTR_FADE_END) {
- LEDC.int_ena.val = value | BIT(8 + channel);
+ if(intr_type == LEDC_INTR_FADE_END) {
+ LEDC.int_ena.val = value | BIT(LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S + channel);
} else {
- LEDC.int_ena.val = (value & (~(BIT(8 + channel))));
+ LEDC.int_ena.val = (value & (~(BIT(LEDC_DUTY_CHNG_END_HSCH0_INT_ENA_S + channel))));
}
portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
uint32_t gpio_num = ledc_conf->gpio_num;
uint32_t ledc_channel = ledc_conf->channel;
uint32_t freq_hz = ledc_conf->freq_hz;
- uint32_t timer_select = ledc_conf->timer_src;
- uint32_t duty_depth = ledc_conf->duty_depth;
+ uint32_t timer_select = ledc_conf->timer_sel;
+ uint32_t bit_num = ledc_conf->bit_num;
uint32_t intr_type = ledc_conf->intr_type;
uint32_t duty = ledc_conf->duty;
uint32_t div_param = 0;
uint32_t precision = 0;
int timer_clk_src = 0;
+
if(!ledc_is_valid_channel(ledc_channel)) {
return ESP_ERR_INVALID_ARG;
}
LEDC_ERROR("GPIO number error: IO%d\n ", gpio_num);
return ESP_ERR_INVALID_ARG;
}
- if(freq_hz == 0 || duty_depth == 0 || duty_depth > LEDC_DUTY_DEPTH_15_BIT) {
- LEDC_ERROR("freq_hz=%u duty_depth=%u\n", div_param, duty_depth);
+ if(freq_hz == 0 || bit_num == 0 || bit_num > LEDC_TIMER_15_BIT) {
+ LEDC_ERROR("freq_hz=%u bit_num=%u\n", div_param, bit_num);
return ESP_ERR_INVALID_ARG;
}
if(timer_select > LEDC_TIMER3) {
}
portENTER_CRITICAL(&ledc_spinlock);
esp_err_t ret = ESP_OK;
- /*gpio matrix ledc pwm signal*/
- PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
- gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
- gpio_matrix_out(gpio_num, LEDC_HS_SIG_OUT0_IDX + ledc_channel, 0, 0);
- /*configure ledc param*/
- /*calculate the div_param and select which base clock and first we will select the apb_clk */
- precision = (0x1 << duty_depth); //2**depth
+ precision = (0x1 << bit_num); //2**depth
div_param = ((uint64_t) LEDC_APB_CLK_HZ << 8) / freq_hz / precision; //8bit fragment
- /*Fail ,because the div num overflow or too small*/
+ /*Fail ,because the div_num overflow or too small*/
if(div_param <= 256 || div_param > LEDC_DIV_NUM_HSTIMER0_V) { //REF TICK
/*Selet the reference tick*/
div_param = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
ret = ESP_FAIL;
}
timer_clk_src = LEDC_REF_TICK;
- ledc_timer_config(speed_mode, timer_select, div_param, duty_depth, timer_clk_src);
- ledc_set_channel_timer(speed_mode, ledc_channel, timer_select);
} else { //APB TICK
timer_clk_src = LEDC_APB_CLK;
- ledc_timer_config(speed_mode, timer_select, div_param, duty_depth, timer_clk_src);
- ledc_set_channel_timer(speed_mode, ledc_channel, timer_select);
}
+ //1. set timer parameters
+ // timer settings decide the clk of counter and the period of PWM
+ ledc_timer_config(speed_mode, timer_select, div_param, bit_num, timer_clk_src);
+ // reset timer.
ledc_timer_rst(speed_mode, timer_select);
+ //2. set channel parameters
+ // channel parameters decide how the waveform looks like in one period
+ // set channel duty, duty range is (0 ~ ((2 ** bit_num) - 1))
ledc_set_duty(speed_mode, ledc_channel, duty);
+ //update duty settings
+ ledc_update(speed_mode, ledc_channel);
+ //3. bind the channel with the timer
+ ledc_bind_channel_timer(speed_mode, ledc_channel, timer_select);
+ //4. set interrupt type
ledc_enable_intr_type(speed_mode, ledc_channel, intr_type);
LEDC_INFO("LEDC_PWM CHANNEL %1u|GPIO %02u|FreHz %05u|Duty %04u|Depth %04u|Time %01u|SourceClk %01u|Divparam %u\n",
- ledc_channel, gpio_num, freq_hz, duty, duty_depth, timer_select, timer_clk_src, div_param
+ ledc_channel, gpio_num, freq_hz, duty, bit_num, timer_select, timer_clk_src, div_param
);
- ledc_update(speed_mode, ledc_channel);
+ /*5. set LEDC signal in gpio matrix*/
+ PIN_FUNC_SELECT(GPIO_PIN_MUX_REG[gpio_num], PIN_FUNC_GPIO);
+ gpio_set_direction(gpio_num, GPIO_MODE_OUTPUT);
+ gpio_matrix_out(gpio_num, LEDC_HS_SIG_OUT0_IDX + ledc_channel, 0, 0);
portEXIT_CRITICAL(&ledc_spinlock);
return ret;
}
return ESP_ERR_INVALID_ARG;
}
portENTER_CRITICAL(&ledc_spinlock);
- LEDC.high_speed_channel[channel].conf0.sig_out_en = 1;
- LEDC.high_speed_channel[channel].conf1.duty_start = 1;
+ LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 1;
+ LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 1;
portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
}
return ESP_ERR_INVALID_ARG;
}
portENTER_CRITICAL(&ledc_spinlock);
- LEDC.high_speed_channel[channel].conf0.idle_lv = idle_level;
- LEDC.high_speed_channel[channel].conf0.sig_out_en = 0;
- LEDC.high_speed_channel[channel].conf1.duty_start = 0;
+ LEDC.channel_group[speed_mode].channel[channel].conf0.idle_lv = idle_level & 0x1;
+ LEDC.channel_group[speed_mode].channel[channel].conf0.sig_out_en = 0;
+ LEDC.channel_group[speed_mode].channel[channel].conf1.duty_start = 0;
portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
}
LEDC_ERROR("step_num=%u duty_cyle_num=%u duty_scale=%u\n", step_num, duty_cyle_num, duty_scale);
return ESP_ERR_INVALID_ARG;
}
- portENTER_CRITICAL(&ledc_spinlock);
ledc_duty_config(speed_mode,
channel, //uint32_t chan_num,
0, //uint32_t hpoint_val,
duty_cyle_num, //uint32_t duty_cycle,
duty_scale //uint32_t duty_scale
);
- portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
}
if(!ledc_is_valid_channel(channel)) {
return ESP_ERR_INVALID_ARG;
}
- portENTER_CRITICAL(&ledc_spinlock);
ledc_duty_config(speed_mode,
channel, //uint32_t chan_num,
0, //uint32_t hpoint_val,
1, //uint32_t duty_cycle,
0 //uint32_t duty_scale
);
- portEXIT_CRITICAL(&ledc_spinlock);
return ESP_OK;
}
if(!ledc_is_valid_mode(speed_mode)) {
return -1;
}
- uint32_t duty = (LEDC.high_speed_channel[channel].duty_rd.duty_read >> 4);
+ uint32_t duty = (LEDC.channel_group[speed_mode].channel[channel].duty_rd.duty_read >> 4);
return duty;
}
-esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t freq_hz)
+esp_err_t ledc_set_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num, uint32_t freq_hz)
{
if(!ledc_is_valid_mode(speed_mode)) {
return ESP_ERR_INVALID_ARG;
}
- if(!ledc_is_valid_channel(channel)) {
- return ESP_ERR_INVALID_ARG;
- }
portENTER_CRITICAL(&ledc_spinlock);
esp_err_t ret = ESP_OK;
uint32_t div_num = 0;
- /*Select timer*/
- uint32_t timer_select = LEDC.high_speed_channel[channel].conf0.timer_sel;
- /*Get timer limit*/
- uint32_t duty_depth = LEDC.high_speed_timer[timer_select].conf.timer_lim;
- uint32_t timer_source_clk = LEDC.high_speed_timer[timer_select].conf.tick_sel;
- uint32_t precision = (0x1 << duty_depth);
+ uint32_t bit_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.bit_num;
+ uint32_t timer_source_clk = LEDC.timer_group[speed_mode].timer[timer_num].conf.tick_sel;
+ uint32_t precision = (0x1 << bit_num);
if(timer_source_clk == LEDC_APB_CLK) {
div_num = ((uint64_t) LEDC_APB_CLK_HZ << 8) / freq_hz / precision;
} else {
div_num = ((uint64_t) LEDC_REF_CLK_HZ << 8) / freq_hz / precision;
}
if(div_num <= 256 || div_num > LEDC_DIV_NUM_HSTIMER0) {
- LEDC_ERROR("channel %u,div param err,div_param=%u\n", channel, div_num);
+ LEDC_ERROR("div param err,div_param=%u\n", div_num);
ret = ESP_FAIL;
}
- LEDC.high_speed_timer[timer_select].conf.div_num = div_num;
+ LEDC.timer_group[speed_mode].timer[timer_num].conf.div_num = div_num;
portEXIT_CRITICAL(&ledc_spinlock);
return ret;
}
-uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_channel_t channel)
+uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num)
{
if(!ledc_is_valid_mode(speed_mode)) {
return 0;
}
- if(!ledc_is_valid_channel(channel)) {
- return 0;
- }
portENTER_CRITICAL(&ledc_spinlock);
uint32_t freq = 0;
- uint32_t timer_select = LEDC.high_speed_channel[channel].conf0.timer_sel;
- uint32_t timer_source_clk = LEDC.high_speed_timer[timer_select].conf.tick_sel;
- uint32_t duty_depth = LEDC.high_speed_timer[timer_select].conf.timer_lim;
- uint32_t div_num = LEDC.high_speed_timer[timer_select].conf.div_num;
- uint32_t precision = (0x1 << duty_depth);
+ uint32_t timer_source_clk = LEDC.timer_group[speed_mode].timer[timer_num].conf.tick_sel;
+ uint32_t bit_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.bit_num;
+ uint32_t div_num = LEDC.timer_group[speed_mode].timer[timer_num].conf.div_num;
+ uint32_t precision = (0x1 << bit_num);
if(timer_source_clk == LEDC_APB_CLK) {
freq = ((uint64_t) LEDC_APB_CLK_HZ << 8) / precision / div_num;
} else {
#ifndef _SOC_LEDC_STRUCT_H_
#define _SOC_LEDC_STRUCT_H_
typedef volatile struct {
- struct{
- union {
- struct {
- uint32_t timer_sel: 2; /*There are four high speed timers the two bits are used to select one of them for high speed channel. 2'b00: seletc hstimer0. 2'b01: select hstimer1. 2'b10: select hstimer2. 2'b11: select hstimer3.*/
- uint32_t sig_out_en: 1; /*This is the output enable control bit for high speed channel*/
- uint32_t idle_lv: 1; /*This bit is used to control the output value when high speed channel is off.*/
- uint32_t reserved4: 27;
- uint32_t clk_en: 1; /*This bit is clock gating control signal. when software configure LED_PWM internal registers it controls the register clock.*/
- };
- uint32_t val;
- } conf0;
- union {
- struct {
- uint32_t hpoint: 20; /*The output value changes to high when htimerx(x=[0 3]) selected by high speed channel has reached reg_hpoint_hsch0[19:0]*/
- uint32_t reserved20: 12;
- };
- uint32_t val;
- } hpoint;
- union {
- struct {
- uint32_t duty: 25; /*The register is used to control output duty. When hstimerx(x=[0 3]) chosen by high speed channel has reached reg_lpoint_hsch0 the output signal changes to low. reg_lpoint_hsch0=(reg_hpoint_hsch0[19:0]+reg_duty_hsch0[24:4]) (1) reg_lpoint_hsch0=(reg_hpoint_hsch0[19:0]+reg_duty_hsch0[24:4] +1) (2) The least four bits in this register represent the decimal part and determines when to choose (1) or (2)*/
- uint32_t reserved25: 7;
- };
- uint32_t val;
- } duty;
- union {
- struct {
- uint32_t duty_scale:10; /*This register controls the increase or decrease step scale for high speed channel.*/
- uint32_t duty_cycle:10; /*This register is used to increase or decrease the duty every reg_duty_cycle_hsch0 cycles for high speed channel.*/
- uint32_t duty_num: 10; /*This register is used to control the number of increased or decreased times for high speed channel.*/
- uint32_t duty_inc: 1; /*This register is used to increase the duty of output signal or decrease the duty of output signal for high speed channel.*/
- uint32_t duty_start: 1; /*When reg_duty_num_hsch0 reg_duty_cycle_hsch0 and reg_duty_scale_hsch0 has been configured. these register won't take effect until set reg_duty_start_hsch0. this bit is automatically cleared by hardware.*/
- };
- uint32_t val;
- } conf1;
- union {
- struct {
- uint32_t duty_read: 25; /*This register represents the current duty of the output signal for high speed channel.*/
- uint32_t reserved25: 7;
- };
- uint32_t val;
- } duty_rd;
- } high_speed_channel[8];
- struct{
- union {
- struct {
- uint32_t timer_sel: 2; /*There are four low speed timers the two bits are used to select one of them for low speed channel. 2'b00: seletc lstimer0. 2'b01: select lstimer1. 2'b10: select lstimer2. 2'b11: select lstimer3.*/
- uint32_t sig_out_en: 1; /*This is the output enable control bit for low speed channel.*/
- uint32_t idle_lv: 1; /*This bit is used to control the output value when low speed channel is off.*/
- uint32_t para_up: 1; /*This bit is used to update register LEDC_LSCH0_HPOINT and LEDC_LSCH0_DUTY for low speed channel.*/
- uint32_t reserved5: 27;
- };
- uint32_t val;
- } conf0;
- union {
- struct {
- uint32_t hpoint: 20; /*The output value changes to high when lstimerx(x=[0 3]) selected by low speed channel has reached reg_hpoint_lsch0[19:0]*/
- uint32_t reserved20: 12;
- };
- uint32_t val;
- } hpoint;
- union {
- struct {
- uint32_t duty: 25; /*The register is used to control output duty. When lstimerx(x=[0 3]) choosed by low speed channel has reached reg_lpoint_lsch0 the output signal changes to low. reg_lpoint_lsch0=(reg_hpoint_lsch0[19:0]+reg_duty_lsch0[24:4]) (1) reg_lpoint_lsch0=(reg_hpoint_lsch0[19:0]+reg_duty_lsch0[24:4] +1) (2) The least four bits in this register represent the decimal part and determines when to choose (1) or (2)*/
- uint32_t reserved25: 7;
- };
- uint32_t val;
- } duty;
- union {
- struct {
- uint32_t duty_scale:10; /*This register controls the increase or decrease step scale for low speed channel.*/
- uint32_t duty_cycle:10; /*This register is used to increase or decrease the duty every reg_duty_cycle_lsch0 cycles for low speed channel.*/
- uint32_t duty_num: 10; /*This register is used to control the num of increased or decreased times for low speed channel6.*/
- uint32_t duty_inc: 1; /*This register is used to increase the duty of output signal or decrease the duty of output signal for low speed channel6.*/
- uint32_t duty_start: 1; /*When reg_duty_num_hsch1 reg_duty_cycle_hsch1 and reg_duty_scale_hsch1 has been configured. these register won't take effect until set reg_duty_start_hsch1. this bit is automatically cleared by hardware.*/
- };
- uint32_t val;
- } conf1;
- union {
- struct {
- uint32_t duty_read: 25; /*This register represents the current duty of the output signal for low speed channel.*/
- uint32_t reserved25: 7;
- };
- uint32_t val;
- } duty_r;
- } low_speed_channel[8];
- struct{
- union {
- struct {
- uint32_t timer_lim: 5; /*This register controls the range of the counter in high speed timer. the counter range is [0 2**reg_hstimer0_lim] the max bit width for counter is 20.*/
- uint32_t div_num: 18; /*This register is used to configure parameter for divider in high speed timer the least significant eight bits represent the decimal part.*/
- uint32_t pause: 1; /*This bit is used to pause the counter in high speed timer*/
- uint32_t rst: 1; /*This bit is used to reset high speed timer the counter will be 0 after reset.*/
- uint32_t tick_sel: 1; /*This bit is used to choose apb_clk or ref_tick for high speed timer. 1'b1:apb_clk 0:ref_tick*/
- uint32_t reserved26: 6;
- };
- uint32_t val;
- } conf;
- union {
- struct {
- uint32_t timer_cnt: 20; /*software can read this register to get the current counter value in high speed timer*/
- uint32_t reserved20: 12;
- };
- uint32_t val;
- } value;
- } high_speed_timer[4];
- struct{
- union {
- struct {
- uint32_t timer_lim: 5; /*This register controls the range of the counter in low speed timer. the counter range is [0 2**reg_lstimer0_lim] the max bit width for counter is 20.*/
- uint32_t div_num: 18; /*This register is used to configure parameter for divider in low speed timer the least significant eight bits represent the decimal part.*/
- uint32_t pause: 1; /*This bit is used to pause the counter in low speed timer.*/
- uint32_t rst: 1; /*This bit is used to reset low speed timer the counter will be 0 after reset.*/
- uint32_t tick_sel: 1; /*This bit is used to choose slow_clk or ref_tick for low speed timer. 1'b1:slow_clk 0:ref_tick*/
- uint32_t param_update: 1; /*Set this bit to update reg_div_num_lstime0 and reg_lstimer0_lim.*/
- uint32_t reserved27: 5;
- };
- uint32_t val;
- } conf;
- union {
- struct {
- uint32_t timer_cnt: 20; /*software can read this register to get the current counter value in low speed timer.*/
- uint32_t reserved20: 12;
- };
- uint32_t val;
- } value;
- } low_speed_timer[4];
+ struct {
+ struct {
+ union {
+ struct {
+ uint32_t timer_sel: 2; /*There are four high speed timers the two bits are used to select one of them for high speed channel. 2'b00: seletc hstimer0. 2'b01: select hstimer1. 2'b10: select hstimer2. 2'b11: select hstimer3.*/
+ uint32_t sig_out_en: 1; /*This is the output enable control bit for high speed channel*/
+ uint32_t idle_lv: 1; /*This bit is used to control the output value when high speed channel is off.*/
+ uint32_t reserved4: 27;
+ uint32_t clk_en: 1; /*This bit is clock gating control signal. when software configure LED_PWM internal registers it controls the register clock.*/
+ };
+ uint32_t val;
+ } conf0;
+ union {
+ struct {
+ uint32_t hpoint: 20; /*The output value changes to high when htimerx(x=[0 3]) selected by high speed channel has reached reg_hpoint_hsch0[19:0]*/
+ uint32_t reserved20: 12;
+ };
+ uint32_t val;
+ } hpoint;
+ union {
+ struct {
+ uint32_t duty: 25; /*The register is used to control output duty. When hstimerx(x=[0 3]) chosen by high speed channel has reached reg_lpoint_hsch0 the output signal changes to low. reg_lpoint_hsch0=(reg_hpoint_hsch0[19:0]+reg_duty_hsch0[24:4]) (1) reg_lpoint_hsch0=(reg_hpoint_hsch0[19:0]+reg_duty_hsch0[24:4] +1) (2) The least four bits in this register represent the decimal part and determines when to choose (1) or (2)*/
+ uint32_t reserved25: 7;
+ };
+ uint32_t val;
+ } duty;
+ union {
+ struct {
+ uint32_t duty_scale:10; /*This register controls the increase or decrease step scale for high speed channel.*/
+ uint32_t duty_cycle:10; /*This register is used to increase or decrease the duty every reg_duty_cycle_hsch0 cycles for high speed channel.*/
+ uint32_t duty_num: 10; /*This register is used to control the number of increased or decreased times for high speed channel.*/
+ uint32_t duty_inc: 1; /*This register is used to increase the duty of output signal or decrease the duty of output signal for high speed channel.*/
+ uint32_t duty_start: 1; /*When reg_duty_num_hsch0 reg_duty_cycle_hsch0 and reg_duty_scale_hsch0 has been configured. these register won't take effect until set reg_duty_start_hsch0. this bit is automatically cleared by hardware.*/
+ };
+ uint32_t val;
+ } conf1;
+ union {
+ struct {
+ uint32_t duty_read: 25; /*This register represents the current duty of the output signal for high speed channel.*/
+ uint32_t reserved25: 7;
+ };
+ uint32_t val;
+ } duty_rd;
+ } channel[8];
+ } channel_group[2]; /*two channel groups : 0: high-speed channels; 1: low-speed channels*/
+ struct {
+ struct {
+ union {
+ struct {
+ uint32_t bit_num: 5; /*This register controls the range of the counter in high speed timer. the counter range is [0 2**reg_hstimer0_lim] the max bit width for counter is 20.*/
+ uint32_t div_num: 18; /*This register is used to configure parameter for divider in high speed timer the least significant eight bits represent the decimal part.*/
+ uint32_t pause: 1; /*This bit is used to pause the counter in high speed timer*/
+ uint32_t rst: 1; /*This bit is used to reset high speed timer the counter will be 0 after reset.*/
+ uint32_t tick_sel: 1; /*This bit is used to choose apb_clk or ref_tick for high speed timer. 1'b1:apb_clk 0:ref_tick*/
+ uint32_t low_speed_update: 1; /*This bit is only useful for low speed timer channels, reserved for high speed timers*/
+ uint32_t reserved26: 5;
+ };
+ uint32_t val;
+ } conf;
+ union {
+ struct {
+ uint32_t timer_cnt: 20; /*software can read this register to get the current counter value in high speed timer*/
+ uint32_t reserved20: 12;
+ };
+ uint32_t val;
+ } value;
+ } timer[4];
+ } timer_group[2]; /*two channel groups : 0: high-speed channels; 1: low-speed channels*/
union {
struct {
uint32_t hstimer0_ovf: 1; /*The interrupt raw bit for high speed channel0 counter overflow.*/