1
0
mirror of https://github.com/esphome/esphome.git synced 2025-01-19 04:20:56 +00:00

Support ISR based pulse counter on ESP32-C3 (#2983)

This commit is contained in:
Stefan Agner 2022-01-03 23:06:43 +01:00 committed by GitHub
parent dbc2812022
commit 15ce27992e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 11 deletions

View File

@ -8,7 +8,7 @@ static const char *const TAG = "pulse_counter";
const char *const EDGE_MODE_TO_STRING[] = {"DISABLE", "INCREMENT", "DECREMENT"}; const char *const EDGE_MODE_TO_STRING[] = {"DISABLE", "INCREMENT", "DECREMENT"};
#ifdef USE_ESP8266 #ifndef HAS_PCNT
void IRAM_ATTR PulseCounterStorage::gpio_intr(PulseCounterStorage *arg) { void IRAM_ATTR PulseCounterStorage::gpio_intr(PulseCounterStorage *arg) {
const uint32_t now = micros(); const uint32_t now = micros();
const bool discard = now - arg->last_pulse < arg->filter_us; const bool discard = now - arg->last_pulse < arg->filter_us;
@ -43,7 +43,7 @@ pulse_counter_t PulseCounterStorage::read_raw_value() {
} }
#endif #endif
#ifdef USE_ESP32 #ifdef HAS_PCNT
bool PulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) { bool PulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) {
static pcnt_unit_t next_pcnt_unit = PCNT_UNIT_0; static pcnt_unit_t next_pcnt_unit = PCNT_UNIT_0;
this->pin = pin; this->pin = pin;
@ -96,7 +96,7 @@ bool PulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) {
} }
if (this->filter_us != 0) { if (this->filter_us != 0) {
uint16_t filter_val = std::min(this->filter_us * 80u, 1023u); uint16_t filter_val = std::min(static_cast<unsigned int>(this->filter_us * 80u), 1023u);
ESP_LOGCONFIG(TAG, " Filter Value: %uus (val=%u)", this->filter_us, filter_val); ESP_LOGCONFIG(TAG, " Filter Value: %uus (val=%u)", this->filter_us, filter_val);
error = pcnt_set_filter_value(this->pcnt_unit, filter_val); error = pcnt_set_filter_value(this->pcnt_unit, filter_val);
if (error != ESP_OK) { if (error != ESP_OK) {

View File

@ -4,8 +4,9 @@
#include "esphome/core/hal.h" #include "esphome/core/hal.h"
#include "esphome/components/sensor/sensor.h" #include "esphome/components/sensor/sensor.h"
#ifdef USE_ESP32 #if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3)
#include <driver/pcnt.h> #include <driver/pcnt.h>
#define HAS_PCNT
#endif #endif
namespace esphome { namespace esphome {
@ -17,10 +18,9 @@ enum PulseCounterCountMode {
PULSE_COUNTER_DECREMENT, PULSE_COUNTER_DECREMENT,
}; };
#ifdef USE_ESP32 #ifdef HAS_PCNT
using pulse_counter_t = int16_t; using pulse_counter_t = int16_t;
#endif #else
#ifdef USE_ESP8266
using pulse_counter_t = int32_t; using pulse_counter_t = int32_t;
#endif #endif
@ -30,16 +30,15 @@ struct PulseCounterStorage {
static void gpio_intr(PulseCounterStorage *arg); static void gpio_intr(PulseCounterStorage *arg);
#ifdef USE_ESP8266 #ifndef HAS_PCNT
volatile pulse_counter_t counter{0}; volatile pulse_counter_t counter{0};
volatile uint32_t last_pulse{0}; volatile uint32_t last_pulse{0};
#endif #endif
InternalGPIOPin *pin; InternalGPIOPin *pin;
#ifdef USE_ESP32 #ifdef HAS_PCNT
pcnt_unit_t pcnt_unit; pcnt_unit_t pcnt_unit;
#endif #else
#ifdef USE_ESP8266
ISRInternalGPIOPin isr_pin; ISRInternalGPIOPin isr_pin;
#endif #endif
PulseCounterCountMode rising_edge_mode{PULSE_COUNTER_INCREMENT}; PulseCounterCountMode rising_edge_mode{PULSE_COUNTER_INCREMENT};