Skip to content

Commit bd94398

Browse files
committed
zephyrCommon: Implement pulseIn
- A basic sync implementation - Max precision of millisec. The actual arduino API seems to want microsec precision. Signed-off-by: Ayush Singh <[email protected]>
1 parent 347a80d commit bd94398

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

cores/arduino/zephyrCommon.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,28 @@ void detachInterrupt(pin_size_t pinNumber)
328328
{
329329
setInterruptHandler(pinNumber, nullptr);
330330
}
331+
332+
unsigned long pulseIn(pin_size_t pinNumber, uint8_t state, unsigned long timeout) {
333+
const struct gpio_dt_spec *spec = &arduino_pins[pinNumber];
334+
int64_t start, end, delta;
335+
336+
if (!gpio_is_ready_dt(spec)) {
337+
return 0;
338+
}
339+
340+
if (!gpio_pin_is_input_dt(spec)) {
341+
return 0;
342+
}
343+
344+
345+
while(gpio_pin_get_dt(spec) == state);
346+
while(gpio_pin_get_dt(spec) != state);
347+
348+
start = k_uptime_ticks();
349+
while(gpio_pin_get_dt(spec) == state);
350+
end = k_uptime_ticks();
351+
352+
delta = k_ticks_to_us_floor64(end) - k_ticks_to_us_floor64(start);
353+
354+
return (unsigned long)delta;
355+
}

0 commit comments

Comments
 (0)