Skip to content

Commit bc4ecbc

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 bc4ecbc

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
@@ -349,3 +349,46 @@ long random(long max) {
349349
}
350350

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

0 commit comments

Comments
 (0)