Creating >10MHz clock using PWM and LEDC

JohnnyR
Posts: 4
Joined: Sun Dec 20, 2020 10:38 pm

Creating >10MHz clock using PWM and LEDC

Postby JohnnyR » Sun Dec 20, 2020 11:03 pm

Hi,

I'm hoping someone can help as this is driving me mad!

I'm trying to generate a square wave clock signal from the ESP32 GPIO0 with a frequency of 13MHz and a 50% duty cycle. From what I've read elsewhere, it seems that a clock up to 40MHz should be possible using LEDC with a timer duty resolution of 1 bit. The best I can get is 4MHz (see below) before other effects seem to happen (like inconsistent duty cycles and unexpected frequencies). For example, when I set the frequency to 13MHz, I get the attached waveform at 1.6/2.66MHz. Even below 4MHZ, I cannot seem to set precise frequencies with a 50% duty cycle.

Would anyone have any ideas? Could it be an issue with the LEDC_HIGH_SPEED_MODE perhaps?

Thanks,

Code: Select all

void init_lecd() {
  // set LEDC timer
  ledc_timer.speed_mode       = LEDC_HIGH_SPEED_MODE;
  ledc_timer.duty_resolution  = LEDC_TIMER_1_BIT;
  ledc_timer.timer_num        = LEDC_TIMER_0;           
  ledc_timer.freq_hz          = 4000000;                   

  // set LEDC channel
  ledc_channel.channel    = LEDC_CHANNEL_0;
  ledc_channel.duty       = 1;
  ledc_channel.gpio_num   = 0;
  ledc_channel.speed_mode = LEDC_HIGH_SPEED_MODE;
  ledc_channel.timer_sel  = LEDC_TIMER_0;

  // Set timer configuration
  ledc_timer_config(&ledc_timer);
  // Set channel configuration
  ledc_channel_config(&ledc_channel);

 //PIN_FUNC_SELECT(PIN_CTRL, CLK_OUT1_S);
//PIN_FUNC_SELECT(PERIPHS_IO_MUX_GPIO0_U, FUNC_GPIO0_CLK_OUT1);
}


Image

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Creating >10MHz clock using PWM and LEDC

Postby ESP_Sprite » Mon Dec 21, 2020 3:59 am

The base clock for LEDC is 80MHz. You'll see weirdness as there's no way you can divide 80MHz cleanly into 13MHz. With that being said, I'd still expect a frequency of 13.3MHz and a duty cycle of 48% if you set ledc_timer.freq_hz to 13000000. Is that what you get, or do you get something else?

JohnnyR
Posts: 4
Joined: Sun Dec 20, 2020 10:38 pm

Re: Creating >10MHz clock using PWM and LEDC

Postby JohnnyR » Mon Dec 21, 2020 11:11 pm

thanks for the reply. If I make the setting 13Mhz, I get the attached waveform:
temp1.jpg
temp1.jpg (100.63 KiB) Viewed 7481 times
Any ideas?

Thanks

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Creating >10MHz clock using PWM and LEDC

Postby ESP_Sprite » Tue Dec 22, 2020 3:58 am

Interesting... if I put a frequency of 13MHz there, I do get 13MHz but with a bit of jitter; there's probably a fractional divider involved. What ESP-IDF version are you using? Fwiw, I'm using master, and this bit of code:

Code: Select all

  periph_module_enable(PERIPH_LEDC_MODULE);                                                                                                                    
                                                                                                                                                               
  ledc_timer_config_t timer_conf={};                                                                                                                           
  timer_conf.duty_resolution = 1;                                                                                                                              
  timer_conf.freq_hz = 13000000;                                                                                                                               
  timer_conf.speed_mode = LEDC_HIGH_SPEED_MODE;                                                                                                                
  timer_conf.timer_num = LEDC_TIMER_0;                                                                                                                         
  esp_err_t err = ledc_timer_config(&timer_conf);                                                                                                              
                                                                                                                                                               
  ledc_channel_config_t ch_conf;                                                                                                                               
  ch_conf.gpio_num = 4;                                                                                                                                        
  ch_conf.speed_mode = LEDC_HIGH_SPEED_MODE;                                                                                                                   
  ch_conf.channel = LEDC_CHANNEL_0;                                                                                                                            
  ch_conf.intr_type = LEDC_INTR_DISABLE;                                                                                                                       
  ch_conf.timer_sel = LEDC_TIMER_0;                                                                                                                            
  ch_conf.duty = 1;                                                                                                                                            
  ch_conf.hpoint = 0;                                                                                                                                          
  err = ledc_channel_config(&ch_conf);  
[/code

JohnnyR
Posts: 4
Joined: Sun Dec 20, 2020 10:38 pm

Re: Creating >10MHz clock using PWM and LEDC

Postby JohnnyR » Tue Dec 22, 2020 11:58 pm

I'm actually using Arduino rather than IDF at the moment. It's got me thinking if perhaps that could be part of the problem?

ESP_Sprite
Posts: 9730
Joined: Thu Nov 26, 2015 4:08 am

Re: Creating >10MHz clock using PWM and LEDC

Postby ESP_Sprite » Wed Dec 23, 2020 1:48 am

Possibly... the ledc calls you use are part of ESP-IDF (you can use most ESP-IDF calls in an Arduino project) but Arduino may be on a lower version of ESP-IDF; could be that there's a bug in the ledc driver for that version.

Who is online

Users browsing this forum: No registered users and 15 guests