mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-30 22:53:59 +00:00 
			
		
		
		
	Support ISR based pulse counter on ESP32-C3 (#2983)
This commit is contained in:
		| @@ -8,7 +8,7 @@ static const char *const TAG = "pulse_counter"; | ||||
|  | ||||
| const char *const EDGE_MODE_TO_STRING[] = {"DISABLE", "INCREMENT", "DECREMENT"}; | ||||
|  | ||||
| #ifdef USE_ESP8266 | ||||
| #ifndef HAS_PCNT | ||||
| void IRAM_ATTR PulseCounterStorage::gpio_intr(PulseCounterStorage *arg) { | ||||
|   const uint32_t now = micros(); | ||||
|   const bool discard = now - arg->last_pulse < arg->filter_us; | ||||
| @@ -43,7 +43,7 @@ pulse_counter_t PulseCounterStorage::read_raw_value() { | ||||
| } | ||||
| #endif | ||||
|  | ||||
| #ifdef USE_ESP32 | ||||
| #ifdef HAS_PCNT | ||||
| bool PulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) { | ||||
|   static pcnt_unit_t next_pcnt_unit = PCNT_UNIT_0; | ||||
|   this->pin = pin; | ||||
| @@ -96,7 +96,7 @@ bool PulseCounterStorage::pulse_counter_setup(InternalGPIOPin *pin) { | ||||
|   } | ||||
|  | ||||
|   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); | ||||
|     error = pcnt_set_filter_value(this->pcnt_unit, filter_val); | ||||
|     if (error != ESP_OK) { | ||||
|   | ||||
| @@ -4,8 +4,9 @@ | ||||
| #include "esphome/core/hal.h" | ||||
| #include "esphome/components/sensor/sensor.h" | ||||
|  | ||||
| #ifdef USE_ESP32 | ||||
| #if defined(USE_ESP32) && !defined(USE_ESP32_VARIANT_ESP32C3) | ||||
| #include <driver/pcnt.h> | ||||
| #define HAS_PCNT | ||||
| #endif | ||||
|  | ||||
| namespace esphome { | ||||
| @@ -17,10 +18,9 @@ enum PulseCounterCountMode { | ||||
|   PULSE_COUNTER_DECREMENT, | ||||
| }; | ||||
|  | ||||
| #ifdef USE_ESP32 | ||||
| #ifdef HAS_PCNT | ||||
| using pulse_counter_t = int16_t; | ||||
| #endif | ||||
| #ifdef USE_ESP8266 | ||||
| #else | ||||
| using pulse_counter_t = int32_t; | ||||
| #endif | ||||
|  | ||||
| @@ -30,16 +30,15 @@ struct PulseCounterStorage { | ||||
|  | ||||
|   static void gpio_intr(PulseCounterStorage *arg); | ||||
|  | ||||
| #ifdef USE_ESP8266 | ||||
| #ifndef HAS_PCNT | ||||
|   volatile pulse_counter_t counter{0}; | ||||
|   volatile uint32_t last_pulse{0}; | ||||
| #endif | ||||
|  | ||||
|   InternalGPIOPin *pin; | ||||
| #ifdef USE_ESP32 | ||||
| #ifdef HAS_PCNT | ||||
|   pcnt_unit_t pcnt_unit; | ||||
| #endif | ||||
| #ifdef USE_ESP8266 | ||||
| #else | ||||
|   ISRInternalGPIOPin isr_pin; | ||||
| #endif | ||||
|   PulseCounterCountMode rising_edge_mode{PULSE_COUNTER_INCREMENT}; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user