Skip to content

Commit

Permalink
Revert "Use driver/gptimer.h"
Browse files Browse the repository at this point in the history
This reverts commit a017eba.
  • Loading branch information
FozzTexx authored and dillera committed Sep 29, 2024
1 parent 1b3f533 commit 16c9d26
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
24 changes: 20 additions & 4 deletions lib/hardware/fnHardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,34 @@ void HardwareTimer::config()
// uint32_t divider; /*!< Counter clock divider */
// } timer_config_t;

#if defined(CONFIG_IDF_TARGET_ESP32S3)

fn_config.direction = GPTIMER_COUNT_UP,
fn_config.clk_src = GPTIMER_CLK_SRC_APB;
#if defined(CONFIG_IDF_TARGET_ESP32)
fn_config.resolution_hz = APB_CLK_FREQ / TIMER_DIVIDER;
#else
fn_config.resolution_hz = 10000000;
#endif

gptimer_new_timer(&fn_config, &gptimer);
gptimer_enable(gptimer);
gptimer_start(gptimer);

#else

fn_config.alarm_en = TIMER_ALARM_DIS;
fn_config.counter_en = TIMER_PAUSE;
fn_config.intr_type = TIMER_INTR_LEVEL;
fn_config.counter_dir = TIMER_COUNT_UP;
fn_config.auto_reload = TIMER_AUTORELOAD_DIS;
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
fn_config.clk_src = TIMER_SRC_CLK_APB;
#endif
fn_config.divider = TIMER_DIVIDER; // default clock source is APB

timer_init(TIMER_GROUP_1, TIMER_1, &fn_config);
timer_set_counter_value(TIMER_GROUP_1, TIMER_1, 0);
timer_start(TIMER_GROUP_1, TIMER_1);

#endif

}

HardwareTimer fnTimer; // global object for the hardware timer
25 changes: 24 additions & 1 deletion lib/hardware/fnHardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
#include "esp_idf_version.h"
#include "sdkconfig.h"

#if defined(CONFIG_IDF_TARGET_ESP32) || defined(CONFIG_IDF_TARGET_ESP32S3)
#if defined(CONFIG_IDF_TARGET_ESP32)
#include "driver/timer.h"
#elif defined(CONFIG_IDF_TARGET_ESP32S3)
#include "driver/gptimer.h"
#else
#error "neither esp32 or s3"
Expand Down Expand Up @@ -40,12 +42,19 @@ class HardwareTimer
} fn_timer;


#if defined(CONFIG_IDF_TARGET_ESP32S3)
gptimer_handle_t gptimer;
gptimer_config_t fn_config;
//gptimer_alarm_config_t alarm_config;
#else
timer_config_t fn_config;
#endif

public:
void config();

#if defined(CONFIG_IDF_TARGET_ESP32S3)

void reset() { gptimer_set_raw_count(gptimer, 0); };
void latch() {};
void read() {
Expand All @@ -54,6 +63,20 @@ gptimer_config_t fn_config;
fn_timer.t0 = count & 0xFFFFFFFF;
};

#else

#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 0, 0)
void reset() { TIMERG1.hw_timer[TIMER_1].loadlo.val = 0; TIMERG1.hw_timer[TIMER_1].load.val = 0; };
void latch() { TIMERG1.hw_timer[TIMER_1].update.val = 0; };
void read() { fn_timer.t0 = TIMERG1.hw_timer[TIMER_1].lo.val; };
#else
void reset() { TIMERG1.hw_timer[TIMER_1].load_low = 0; TIMERG1.hw_timer[TIMER_1].reload = 0; };
void latch() { TIMERG1.hw_timer[TIMER_1].update = 0; };
void read() { fn_timer.t0 = TIMERG1.hw_timer[TIMER_1].cnt_low; };
#endif

#endif

bool timeout() { return (fn_timer.t0 > fn_timer.tn); };
void wait() { do{latch(); read();} while (!timeout()); };
void alarm_set(int s) { fn_timer.tn = fn_timer.t0 + s * TIMER_100NS_FACTOR - TIMER_ADJUST; };
Expand Down

0 comments on commit 16c9d26

Please sign in to comment.