Code: Select all
void IRAM_ATTR ControlPilot::Pulse(void* arg){
TIMERG0.int_clr_timers.t0 = 1;
TIMERG0.hw_timer[0].config.alarm_en = 1;
short nextActionDelay = 0;
switch(nextAction){
case Action::PulseHigh:
digitalWrite(CP_PWM_PIN, LOW);
nextActionDelay = 30;
nextAction = Action::Sample;
break;
case Action::Sample:
digitalWrite(32, HIGH);
lastCpValue = analogRead(CP_READ_PIN);
digitalWrite(32, LOW);
nextActionDelay = highTime - 30;
nextAction = Action::PulseLow;
break;
case Action::PulseLow:
digitalWrite(CP_PWM_PIN, HIGH);
nextActionDelay = CP_PWM_FREQ - highTime;
nextAction = Action::PulseHigh;
break;
}
TIMERG0.hw_timer[0].alarm_low = nextActionDelay;
TIMERG0.hw_timer[0].alarm_high = 0x0;
}
Code: Select all
//Set up our timer. We use IDF here because arduino makes too many assumptions
timer_config_t tConfig = {
.alarm_en = true,
.counter_en = false,
.intr_type = TIMER_INTR_LEVEL,
.counter_dir = TIMER_COUNT_UP,
.auto_reload = true,
.divider = 80, //Prescaler of 80 gives us a resolution of 1uS
};
ESP_ERROR_CHECK(timer_init(TIMER_GROUP_0, TIMER_0, &tConfig));
ESP_ERROR_CHECK(timer_set_counter_value(TIMER_GROUP_0, TIMER_0, 0)); //Timer will reset to this value on alarm
Currently there is only 107uS:
Now you'll notice my prescaler is set to 80. This is because I was under the impression that the APB clock was running at 80MHz by default in the arduino framework. It worked just fine before IDF. Oddly enough, if I change the prescaler to 160, I get precisely half of the correct timing; that is to say the frequency of the square wave is 500Hz and the duration of the pulse is 100uS. Changing the frequency of the esp in menuconfig has no effect. I tried 160MHz and 80Mhz. Is there something I am missing? Something that is configured by default in arduino? Thanks.