Can ESP32 alone can generate 40MHz square wave using LEDC library?

zennya
Posts: 4
Joined: Sat Sep 14, 2024 10:43 am

Can ESP32 alone can generate 40MHz square wave using LEDC library?

Postby zennya » Sat Sep 14, 2024 11:00 am

Hi,

I happen to surf around this forum and found interesting, but old (currently uncompilable codes under ESP-IDF 5) codes.

Fyi, https://esp32.com/viewtopic.php?t=1037#p14480 provides an example to generate 20MHz square wave at 50% duty on GPIO18 using LEDC library alone.

Being new to the esp32 and C, I tried to make a few changes and made an .ino file from the code:

Code: Select all

include "driver/ledc.h"

void setup() {

  ledc_timer_config_t ledc_timer = {
    .speed_mode = LEDC_HIGH_SPEED_MODE,
    .timer_num  = LEDC_TIMER_0,
    .bit_num    = 2,
    .freq_hz    = 20000000
};
 
ledc_channel_config_t ledc_channel = {
    .channel    = LEDC_CHANNEL_0,
    .gpio_num   = 18,
    .speed_mode = LEDC_HIGH_SPEED_MODE,
    .timer_sel  = LEDC_TIMER_0,
    .duty       = 2
};

void loop() {

{
    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
    while (1) {
        ;
    }
}
}
But it refuses to compile in esp32 dev board with the error that reads:

Code: Select all

/path/to/esp32-ledc.ino: In function 'void setup()':
esp32-ledc:10:1: error: designator order for field 'ledc_timer_config_t::<unnamed union>::bit_num' does not match declaration order in 'ledc_timer_config_t'
 };
 ^
esp32-ledc:18:1: error: designator order for field 'ledc_channel_config_t::gpio_num' does not match declaration order in 'ledc_channel_config_t'
 };
 ^
/path/to/esp32-ledc.ino: In function 'void loop()':
esp32-ledc:24:24: error: 'ledc_timer' was not declared in this scope
     ledc_timer_config(&ledc_timer);
                        ^~~~~~~~~~
/path/to/esp32-ledc.ino:24:24: note: suggested alternative: 'ledc_timer_t'
     ledc_timer_config(&ledc_timer);
                        ^~~~~~~~~~
                        ledc_timer_t
esp32-ledc:25:26: error: 'ledc_channel' was not declared in this scope
     ledc_channel_config(&ledc_channel);
                          ^~~~~~~~~~~~
/path/to/esp32-ledc.ino:25:26: note: suggested alternative: 'ledc_channel_t'
     ledc_channel_config(&ledc_channel);
                          ^~~~~~~~~~~~
                          ledc_channel_t
exit status 1
designator order for field 'ledc_timer_config_t::<unnamed union>::bit_num' does not match declaration order in 'ledc_timer_config_t'
Just wondering is 40Mhz with mere esp32 possible? Is ther any way to convert the C code above to .ino in v5+ of ESP-IDF?

Thanks in advance, and have a nice weekend.

MicroController
Posts: 1552
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Can ESP32 alone can generate 40MHz square wave using LEDC library?

Postby MicroController » Sun Sep 15, 2024 11:40 am

Try it like this:

Code: Select all

ledc_timer_config_t ledc_timer { };
    ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
    ledc_timer.timer_num  = LEDC_TIMER_0;
    ledc_timer.bit_num    = 2;
    ledc_timer.freq_hz    = 20000000;
And yes, the LEDC should be able to generate a 40MHz square wave, cf. https://docs.espressif.com/projects/esp ... esolutions

zennya
Posts: 4
Joined: Sat Sep 14, 2024 10:43 am

Re: Can ESP32 alone generate 40MHz square wave using LEDC library?

Postby zennya » Mon Sep 16, 2024 6:23 am

MicroController wrote:
Sun Sep 15, 2024 11:40 am
Try it like this:

Code: Select all

ledc_timer_config_t ledc_timer { };
    ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
    ledc_timer.timer_num  = LEDC_TIMER_0;
    ledc_timer.bit_num    = 2;
    ledc_timer.freq_hz    = 20000000;
And yes, the LEDC should be able to generate a 40MHz square wave, cf. https://docs.espressif.com/projects/esp ... esolutions

@MicroController:

Thanks for your input. Yet being new, I could not figure out how to get it built. I am sure I must be somewhere wrong. Any input appreciated.

The stdout with error-logs from esp-idf is posted at https://termbin.com/p4qi
The debug output from arduino-ide is in https://termbin.com/2xqy

To avoid long path of the project directory, I just replaced them with

Code: Select all

/path/to/project
.

Meanwhile, I have tried with the code in https://esp32.com/viewtopic.php?t=4560#p57367 without success. I am trying to learn slowly by walking in the guidance of stalwarts and veterans here. So bear with me.

Thanking you in anticipation.

MicroController
Posts: 1552
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Can ESP32 alone can generate 40MHz square wave using LEDC library?

Postby MicroController » Mon Sep 16, 2024 7:20 am

Code: Select all

sketch_sep15a:2:3: error: 'ledc_timer_config_t' was not declared in this scope
indicates that your code may be lacking an

Code: Select all

#include "driver/ledc.h"
Not sure about Arduino vs. IDF (versions, dependencies,...) though, so it may be easier to use Arduino only:
https://espressif-docs.readthedocs-host ... /ledc.html

zennya
Posts: 4
Joined: Sat Sep 14, 2024 10:43 am

Re: Can ESP32 alone can generate 40MHz square wave using LEDC library?

Postby zennya » Mon Sep 16, 2024 9:24 am

MicroController wrote:
Mon Sep 16, 2024 7:20 am

Code: Select all

sketch_sep15a:2:3: error: 'ledc_timer_config_t' was not declared in this scope
indicates that your code may be lacking an

Code: Select all

#include "driver/ledc.h"
Not sure about Arduino vs. IDF (versions, dependencies,...) though, so it may be easier to use Arduino only:
https://espressif-docs.readthedocs-host ... /ledc.html

@MicroController: Thanks for prompt response. I shall follow suit to your advice to use Arduino only in that case.

However when I appended the header file

Code: Select all

 #include "driver/ledc.h"
, still it does not compile with errors that reads:

Code: Select all

/path/to/project/sketch_sep15a/sketch_sep15a.ino: In function 'void setup()':
sketch_sep15a:7:29: error: invalid conversion from 'int' to 'ledc_timer_bit_t' [-fpermissive]
     ledc_timer.bit_num    = 2;
                             ^
/path/to/project/sketch_sep15a/sketch_sep15a.ino: At global scope:
sketch_sep15a:18:1: error: designator order for field 'ledc_channel_config_t::gpio_num' does not match declaration order in 'ledc_channel_config_t'
 };
 ^
/path/to/project/sketch_sep15a/sketch_sep15a.ino: In function 'void loop()':
sketch_sep15a:22:24: error: 'ledc_timer' was not declared in this scope
     ledc_timer_config(&ledc_timer);
                        ^~~~~~~~~~
/path/to/project/sketch_sep15a/sketch_sep15a.ino:22:24: note: suggested alternative: 'ledc_timer_t'
     ledc_timer_config(&ledc_timer);
                        ^~~~~~~~~~
                        ledc_timer_t
exit status 1
invalid conversion from 'int' to 'ledc_timer_bit_t' [-fpermissive]
Fyi, the sketch that produced above error during compilation looks like the following:

Code: Select all

#include "driver/ledc.h"

void setup() {
  ledc_timer_config_t ledc_timer { };
    ledc_timer.speed_mode = LEDC_HIGH_SPEED_MODE;
    ledc_timer.timer_num  = LEDC_TIMER_0;
    ledc_timer.bit_num    = 2;
    ledc_timer.freq_hz    = 10000000;

}

ledc_channel_config_t ledc_channel = {
    .channel    = LEDC_CHANNEL_0,
    .gpio_num   = 18,
    .speed_mode = LEDC_HIGH_SPEED_MODE,
    .timer_sel  = LEDC_TIMER_0,
    .duty       = 2
};

void loop() {
  {
    ledc_timer_config(&ledc_timer);
    ledc_channel_config(&ledc_channel);
    while (1) {
        ;
    }
}
}

MicroController
Posts: 1552
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Can ESP32 alone can generate 40MHz square wave using LEDC library?

Postby MicroController » Mon Sep 16, 2024 9:44 am

Code: Select all

sketch_sep15a:7:29: error: invalid conversion from 'int' to 'ledc_timer_bit_t' [-fpermissive]
     ledc_timer.bit_num    = 2;
Instead of 2, use LEDC_TIMER_2_BIT.

Code: Select all

sketch_sep15a:18:1: error: designator order for field 'ledc_channel_config_t::gpio_num' does not match declaration order in 'ledc_channel_config_t'
Same issue as with the initialization of ledc_timer before. Try initializing each field individually.

Also, now setup() does nothing, and loop() cannot use &ledc_timer because ledc_timer is only visible in setup().

Who is online

Users browsing this forum: No registered users and 295 guests