I'm trying to port some code form v4.4.2 to v5.5.1, in this case about using the new GPTimer (Timer Group Before). Here is a simple code to blink red led at a WROVER-KIT at 1Hz (change LED every 500ms).
Code: Select all
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <stddef.h>
#include "driver/gptimer.h"
#include "driver/gpio.h"
#include "esp_log.h"
#define LED_GPIO 0
#define PERIOD_MS 500
static const char *TAG = "example";
static bool IRAM_ATTR timer_gpt0_isr(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data)
{
static int io_state = 0;
io_state ^= 1;
gpio_set_level(LED_GPIO, io_state);
return (true);
}
void timer_gpt0_initialise (int timer_period_ms)
{
gptimer_handle_t gptimer = NULL;
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1000000, // 1MHz, 1 tick=1us //
};
ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer));
gptimer_event_callbacks_t cbs = { // ISR/callback del timer
.on_alarm = timer_gpt0_isr,
};
gptimer_alarm_config_t alarm_config = {
.reload_count = 0, // counter will reload with 0 on alarm event
.alarm_count = timer_period_ms, // period = 500mss @resolution 1MHz
.flags.auto_reload_on_alarm = true, // enable auto-reload
};
ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config));
ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, NULL));
ESP_LOGI(TAG, "Enable timer");
ESP_ERROR_CHECK(gptimer_enable(gptimer));
ESP_LOGI(TAG, "Start timer");
ESP_ERROR_CHECK(gptimer_start(gptimer));
}
void app_main(void){
gpio_set_direction(LED_GPIO, GPIO_MODE_OUTPUT);
timer_gpt0_initialise(PERIOD_MS*1000);
while(1) {
vTaskDelay(0.010* configTICK_RATE_HZ);
}
}
Code: Select all
.resolution_hz = 1000, // 1KHz, 1 tick=1ms //
I would like to now whether is a minimum resolution for GPTimers and why.
Thanks in advance.