Skip to content

Commit 28ef76c

Browse files
committed
zephyrCommon: Implement pulseIn
- A basic sync implementation - Use ticks for better resolution Signed-off-by: Ayush Singh <[email protected]>
1 parent 347a80d commit 28ef76c

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

cores/arduino/zephyrCommon.cpp

+43
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,46 @@ 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+
struct k_timer timer;
334+
int64_t start, end, delta = 0;
335+
const struct gpio_dt_spec *spec = &arduino_pins[pinNumber];
336+
337+
k_timer_init(&timer, NULL, NULL);
338+
k_timer_start(&timer, K_MSEC(timeout), K_NO_WAIT);
339+
340+
if (!gpio_is_ready_dt(spec)) {
341+
goto cleanup;
342+
}
343+
344+
if (!gpio_pin_is_input_dt(spec)) {
345+
goto cleanup;
346+
}
347+
348+
while(gpio_pin_get_dt(spec) == state) {
349+
if (k_timer_status_get(&timer) > 0) {
350+
goto cleanup;
351+
}
352+
}
353+
354+
while(gpio_pin_get_dt(spec) != state) {
355+
if (k_timer_status_get(&timer) > 0) {
356+
goto cleanup;
357+
}
358+
}
359+
360+
start = k_uptime_ticks();
361+
while(gpio_pin_get_dt(spec) == state) {
362+
if (k_timer_status_get(&timer) > 0) {
363+
goto cleanup;
364+
}
365+
}
366+
end = k_uptime_ticks();
367+
368+
delta = k_ticks_to_us_floor64(end - start);
369+
370+
cleanup:
371+
k_timer_stop(&timer);
372+
return (unsigned long)delta;
373+
}

0 commit comments

Comments
 (0)