Skip to content

Commit 0467451

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 0467451

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

cores/arduino/zephyrCommon.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,26 @@ 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, 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_get();
349+
while(gpio_pin_get_dt(spec) == state);
350+
delta = k_uptime_delta(&start);
351+
352+
return (unsigned long)(delta * USEC_PER_MSEC);
353+
}

0 commit comments

Comments
 (0)