Rework pulseIn to work on ESP32-C3

Fixes: https://github.com/espressif/arduino-esp32/issues/5488
This commit is contained in:
me-no-dev 2021-08-02 14:53:11 +03:00
parent c5a1f3efd7
commit 023ae75b97

View File

@ -17,13 +17,11 @@
//#include <limits.h>
#include "wiring_private.h"
#include "pins_arduino.h"
extern uint32_t xthal_get_ccount();
#include <hal/cpu_hal.h>
#define WAIT_FOR_PIN_STATE(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; \
} \
}
@ -36,12 +34,12 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout)
timeout = max_timeout_us;
}
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);
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);
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)