Skip to content

Commit aba866f

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

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

cores/arduino/zephyrCommon.cpp

+44
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,47 @@ long random(long max) {
349349
}
350350

351351
#endif
352+
353+
#ifdef CONFIG_GPIO_GET_DIRECTION
354+
355+
unsigned long pulseIn(pin_size_t pinNumber, uint8_t state, unsigned long timeout) {
356+
struct k_timer timer;
357+
int64_t start, end, delta = 0;
358+
const struct gpio_dt_spec *spec = &arduino_pins[pinNumber];
359+
360+
k_timer_init(&timer, NULL, NULL);
361+
k_timer_start(&timer, K_MSEC(timeout), K_NO_WAIT);
362+
363+
if (!gpio_is_ready_dt(spec)) {
364+
goto cleanup;
365+
}
366+
367+
if (!gpio_pin_is_input_dt(spec)) {
368+
goto cleanup;
369+
}
370+
371+
while(gpio_pin_get_dt(spec) == state && k_timer_status_get(&timer) == 0);
372+
if (k_timer_status_get(&timer) > 0) {
373+
goto cleanup;
374+
}
375+
376+
while(gpio_pin_get_dt(spec) != state && k_timer_status_get(&timer) == 0);
377+
if (k_timer_status_get(&timer) > 0) {
378+
goto cleanup;
379+
}
380+
381+
start = k_uptime_ticks();
382+
while(gpio_pin_get_dt(spec) == state && k_timer_status_get(&timer) == 0);
383+
if (k_timer_status_get(&timer) > 0) {
384+
goto cleanup;
385+
}
386+
end = k_uptime_ticks();
387+
388+
delta = k_ticks_to_us_floor64(end - start);
389+
390+
cleanup:
391+
k_timer_stop(&timer);
392+
return (unsigned long)delta;
393+
}
394+
395+
#endif // CONFIG_GPIO_GET_DIRECTION

0 commit comments

Comments
 (0)