ESP_morris wrote: ↑Thu Feb 10, 2022 9:54 am
haha, I see your use case now. I think you'd better to try the MCPWM capture feature, it will capture a timestamp by hardware.
Sharp but incisive. Thank you.
True, MCPWM is a better solution than CPU-driven timer polling. Probably, the best. I've not considered them and somehow these modules were not mentioned in other forums on GPS-RTC sync until I explicitly googled them.
I have an issue though driving an MCPWM with a low-frequency signal (1 sec). I've borrowed code from a sonar example and here is the minimal code:
Code: Select all
#define GPS_PPS_PIN GPIO_NUM_26
static xQueueHandle cap_queue;
static uint32_t pulse_count = 0;
static bool gps_pps_isr_handler(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap_sig, const cap_event_data_t *edata,
void *arg) {
//calculate the interval in the ISR,
//so that the interval will be always correct even when cap_queue is not handled in time and overflow.
BaseType_t high_task_wakeup = pdFALSE;
xQueueSendFromISR(cap_queue, &pulse_count, &high_task_wakeup);
pulse_count++;
return high_task_wakeup == pdTRUE;
}
void app_main(void) {
// the queue where we read data
cap_queue = xQueueCreate(10, sizeof(uint32_t));
ESP_ERROR_CHECK(mcpwm_gpio_init(MCPWM_UNIT_0, MCPWM_CAP_0, GPS_PPS_PIN));
ESP_ERROR_CHECK(gpio_pulldown_en(GPS_PPS_PIN));
mcpwm_capture_config_t conf = {
.cap_edge = MCPWM_BOTH_EDGE,
.cap_prescale = 1,
.capture_cb = gps_pps_isr_handler,
.user_data = NULL
};
ESP_ERROR_CHECK(mcpwm_capture_enable_channel(MCPWM_UNIT_0, MCPWM_SELECT_CAP0, &conf));
// forever loop
while (true) {
uint32_t pulse_count;
xQueueReceive(cap_queue, &pulse_count, portMAX_DELAY);
ESP_LOGI(TAG, "Pulse count %u", pulse_count);
}
}
The gps_pps_isr_handler function is never called. Perhaps, this is somehow related to MCPWM frequency. There are mcpwm_init, mcpwm_group_set_resolution, and mcpwm_timer_set_resolution functions but I don't understand how should I initialize an MCPWM module in order to track a 1-second signal with at least 1-us resolution.
Will appreciate if someone explains it to me.
Thanks.
----
Update.
Never mind, I solved the issue.