Commit 023ae75b authored by me-no-dev's avatar me-no-dev

Rework pulseIn to work on ESP32-C3

Fixes: https://github.com/espressif/arduino-esp32/issues/5488
parent c5a1f3ef
...@@ -17,13 +17,11 @@ ...@@ -17,13 +17,11 @@
//#include <limits.h> //#include <limits.h>
#include "wiring_private.h" #include "wiring_private.h"
#include "pins_arduino.h" #include "pins_arduino.h"
#include <hal/cpu_hal.h>
extern uint32_t xthal_get_ccount();
#define WAIT_FOR_PIN_STATE(state) \ #define WAIT_FOR_PIN_STATE(state) \
while (digitalRead(pin) != (state)) { \ while (digitalRead(pin) != (state)) { \
if (xthal_get_ccount() - start_cycle_count > timeout_cycles) { \ if (cpu_hal_get_cycle_count() - start_cycle_count > timeout_cycles) { \
return 0; \ return 0; \
} \ } \
} }
...@@ -36,12 +34,12 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout) ...@@ -36,12 +34,12 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
timeout = max_timeout_us; timeout = max_timeout_us;
} }
const uint32_t timeout_cycles = microsecondsToClockCycles(timeout); const uint32_t timeout_cycles = microsecondsToClockCycles(timeout);
const uint32_t start_cycle_count = xthal_get_ccount(); const uint32_t start_cycle_count = cpu_hal_get_cycle_count();
WAIT_FOR_PIN_STATE(!state); WAIT_FOR_PIN_STATE(!state);
WAIT_FOR_PIN_STATE(state); WAIT_FOR_PIN_STATE(state);
const uint32_t pulse_start_cycle_count = xthal_get_ccount(); const uint32_t pulse_start_cycle_count = cpu_hal_get_cycle_count();
WAIT_FOR_PIN_STATE(!state); WAIT_FOR_PIN_STATE(!state);
return clockCyclesToMicroseconds(xthal_get_ccount() - pulse_start_cycle_count); return clockCyclesToMicroseconds(cpu_hal_get_cycle_count() - pulse_start_cycle_count);
} }
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout) unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment