There are few things you have not told us yet.
- You did not mention the board you are using except saying "esp32". So I assume just that.
- You never really told us what you are trying to achieve, unless I missed it.
I have not used LEDC before so I tried some things just learning what can be done. Using your code in post #10 I commented out "attachInterrup" in setup() and attempted to first get a PWM signal on pin 18. I do not know whether my S3 is slower than a ESP32 but it messaged unable to assign "ledcSetup", so no output.
After some messing around I found I needed to lower the resolution to 7 bits and it worked generating a pwm at 4us (at duty cycle set to 32 it showed 25%). I then enable "attachInterrupt" and got a mess.
I changed your code to show 7 bits at 1us followed by a single 8th at 100 duty cycle (eliminating the delays). I did not use the full 128 to allow a small gap before the next 4us pulse.
However, I could not get a clean sequence at 4us. It seems the rest of the code in the interrupt routine is too slow to keep up. If I change the frequency to 150000 it does what I would expect. That is a cycle time of about 6.7us. My system will not go faster and still work correctly. I did find at 150000 I could up the resolution to 8 bits and it still works.
I do not think for this type of pulse train that LEDC is the correct route. It will be better to do it directly using a timer. The overhead added by LEDC is unnecessary.
My final version of your code is below.
#include <driver/ledc.h>
#define pwmPin 18
int pwmCounter = 0;
bool intSet = false;
void IRAM_ATTR pwmcount() {
intSet = true;
pwmCounter++;
if (pwmCounter == 7) {
ledcWrite(pwmPin, 200); //255);
// delayMicroseconds(2);
// ledcWrite(pwmPin, 32);
// ledcWrite(LEDC_CHANNEL,0 );
// delayMicroseconds(3);
//pwmCounter = 0;
}
if (pwmCounter > 7) {
ledcWrite(pwmPin, 64);
pwmCounter = 0;
}
}
void setup() {
Serial0.begin(115200); // Using Serial0 for the UART port
while ( !Serial0 ) {
delay(10);
}
// Configure LED PWM channel
// ledcSetup(LEDC_CHANNEL, LEDC_BASE_FREQ, LEDC_TIMER_BIT);
// bool success = ledcAttachChannel(pwmPin,250000,8,0);
bool success = ledcAttach(pwmPin,150000,8); //150000,7);
if ( !success ) {
Serial0.println("Could not attach LEDC");
}
// Attach the channel to the GPIO to be controlled
attachInterrupt(pwmPin, &pwmcount, RISING);
// Square wave generation
ledcWrite(pwmPin, 64); // 65 corresponds to 25% duty cycle (fully on)
Serial0.println("Setup done");
}
void loop() {
}
I just commented out what I did not use.
Here is a scope trace of the output. The cycle time measurement is between the cursors.
That was quite fun.