|
| 1 | +// Copyright 2022 Mitch Bradley |
| 2 | +// |
| 3 | +// Interface to the ESP32 alarm timer for step timing |
| 4 | + |
| 5 | +#ifdef __cplusplus |
| 6 | +extern "C" { |
| 7 | +#endif |
| 8 | + |
| 9 | +// #include "../src/Driver/StepTimer.h" |
| 10 | +#include "driver/timer.h" |
| 11 | +#include "hal/timer_hal.h" |
| 12 | + |
| 13 | +static const uint32_t fTimers = 80000000; // the frequency of ESP32 timers |
| 14 | + |
| 15 | +#define USE_LEGACY_TIMER |
| 16 | + |
| 17 | +#ifdef USE_LEGACY_TIMER |
| 18 | +# define TIMER_GROUP_NUM TIMER_GROUP_0, TIMER_0 |
| 19 | + |
| 20 | +void stepTimerStart() { |
| 21 | + // timer_set_counter_value(TIMER_GROUP_NUM, 0); |
| 22 | + // timer_group_enable_alarm_in_isr(TIMER_GROUP_NUM); |
| 23 | + timer_set_alarm(TIMER_GROUP_NUM, TIMER_ALARM_EN); |
| 24 | + timer_start(TIMER_GROUP_NUM); |
| 25 | +} |
| 26 | + |
| 27 | +void IRAM_ATTR stepTimerRestart() { |
| 28 | + // Resetting the counter value here si unnecessary because it |
| 29 | + // happens automatically via the autoreload hardware. |
| 30 | + // The timer framework requires autoreload. |
| 31 | + // If you set autoreload to false, the ISR wrapper will |
| 32 | + // disable the alarm after our ISR function returns, so |
| 33 | + // an attempt to enable the alarm here is ineffective. |
| 34 | + // timer_set_counter_value(TIMER_GROUP_NUM, 0); |
| 35 | +} |
| 36 | + |
| 37 | +uint32_t stepTimerGetTicks() { |
| 38 | + uint64_t val; |
| 39 | + timer_get_alarm_value(TIMER_GROUP_NUM, &val); |
| 40 | + return val; |
| 41 | +} |
| 42 | + |
| 43 | +void IRAM_ATTR stepTimerSetTicks(uint32_t ticks) { |
| 44 | + timer_group_set_alarm_value_in_isr(TIMER_GROUP_NUM, (uint64_t)ticks); |
| 45 | +} |
| 46 | + |
| 47 | +void IRAM_ATTR stepTimerStop() { |
| 48 | + timer_pause(TIMER_GROUP_NUM); |
| 49 | + // timer_set_auto_reload(TIMER_GROUP_NUM, TIMER_AUTORELOAD_DIS); |
| 50 | + timer_set_alarm(TIMER_GROUP_NUM, TIMER_ALARM_DIS); |
| 51 | +} |
| 52 | + |
| 53 | +void stepTimerInit(uint32_t frequency, bool (*fn)(void)) { |
| 54 | + timer_config_t config = { |
| 55 | + .alarm_en = TIMER_ALARM_DIS, |
| 56 | + .counter_en = TIMER_PAUSE, |
| 57 | + .counter_dir = TIMER_COUNT_UP, |
| 58 | + .auto_reload = TIMER_AUTORELOAD_EN, |
| 59 | + // .clk_src = TIMER_SRC_CLK_DEFAULT, |
| 60 | + .divider = fTimers / frequency, |
| 61 | + }; |
| 62 | + timer_init(TIMER_GROUP_NUM, &config); |
| 63 | + timer_set_counter_value(TIMER_GROUP_NUM, 0); |
| 64 | + timer_isr_callback_add(TIMER_GROUP_NUM, (timer_isr_t)fn, NULL, ESP_INTR_FLAG_IRAM); |
| 65 | + //timer_pause(TIMER_GROUP_NUM); |
| 66 | + // timer_start(TIMER_GROUP_NUM); |
| 67 | +} |
| 68 | +#else |
| 69 | +# include "driver/gptimer.h" |
| 70 | + |
| 71 | +static gptimer_handle_t gptimer = NULL; |
| 72 | +void stepTimerInit(uint32_t frequency, bool (*fn)(void)) { |
| 73 | + gptimer_config_t timer_config = { |
| 74 | + .clk_src = GPTIMER_CLK_SRC_DEFAULT, |
| 75 | + .direction = GPTIMER_COUNT_UP, |
| 76 | + .resolution_hz = frequency, |
| 77 | + }; |
| 78 | + gptimer_new_timer(&timer_config, &gptimer); |
| 79 | + |
| 80 | + gptimer_event_callbacks_t cbs = { |
| 81 | + .on_alarm = step_timer_cb, |
| 82 | + }; |
| 83 | + gptimer_register_event_callbacks(gptimer, &cbs, NULL); |
| 84 | + |
| 85 | + gptimer_alarm_config_t alarm_config = { |
| 86 | + .alarm_count = 1000000, // period = 1s |
| 87 | + }; |
| 88 | + gptimer_set_alarm_action(gptimer, &alarm_config); |
| 89 | + gptimer_pause(gptimer); |
| 90 | +} |
| 91 | + |
| 92 | +void IRAM_ATTR stepTimerStart() { |
| 93 | + gptimer_alarm_config_t alarm_config = { |
| 94 | + .reload_count = 0, |
| 95 | + .alarm_count = 1, // Trigger immediately for first pulse |
| 96 | + .flags.auto_reload_on_alarm = false, |
| 97 | + }; |
| 98 | + gptimer_set_alarm_action(gptimer, &alarm_config); |
| 99 | +} |
| 100 | + |
| 101 | +void IRAM_ATTR stepTimerSetTicks(uint32_t ticks) { |
| 102 | + gptimer_alarm_config_t alarm_config = { |
| 103 | + .reload_count = 0, |
| 104 | + .alarm_count = ticks, // Trigger immediately for first pulse |
| 105 | + .flags.auto_reload_on_alarm = true, |
| 106 | + }; |
| 107 | + gptimer_set_alarm_action(gptimer, &alarm_config); |
| 108 | +} |
| 109 | + |
| 110 | +void IRAM_ATTR stepTimerStop() { |
| 111 | + gptimer_stop(gptimer); |
| 112 | +} |
| 113 | +#endif |
| 114 | + |
| 115 | +#ifdef __cplusplus |
| 116 | +} |
| 117 | +#endif |
0 commit comments