Skip to content

Commit 3baf23b

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 3baf23b

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

Diff for: cores/arduino/zephyrCommon.cpp

+47
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,50 @@ 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) {
372+
if (k_timer_status_get(&timer) > 0) {
373+
goto cleanup;
374+
}
375+
}
376+
377+
while(gpio_pin_get_dt(spec) != state) {
378+
if (k_timer_status_get(&timer) > 0) {
379+
goto cleanup;
380+
}
381+
}
382+
383+
start = k_uptime_ticks();
384+
while(gpio_pin_get_dt(spec) == state) {
385+
if (k_timer_status_get(&timer) > 0) {
386+
goto cleanup;
387+
}
388+
}
389+
end = k_uptime_ticks();
390+
391+
delta = k_ticks_to_us_floor64(end - start);
392+
393+
cleanup:
394+
k_timer_stop(&timer);
395+
return (unsigned long)delta;
396+
}
397+
398+
#endif

0 commit comments

Comments
 (0)