Skip to content

zephyrCommon: Implement pulseIn #101

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions cores/arduino/zephyrCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,47 @@ long random(long max) {
}

#endif

#ifdef CONFIG_GPIO_GET_DIRECTION

unsigned long pulseIn(pin_size_t pinNumber, uint8_t state, unsigned long timeout) {
struct k_timer timer;
int64_t start, end, delta = 0;
const struct gpio_dt_spec *spec = &arduino_pins[pinNumber];

k_timer_init(&timer, NULL, NULL);
k_timer_start(&timer, K_MSEC(timeout), K_NO_WAIT);

if (!gpio_is_ready_dt(spec)) {
goto cleanup;
}

if (!gpio_pin_is_input_dt(spec)) {
goto cleanup;
}

while(gpio_pin_get_dt(spec) == state && k_timer_status_get(&timer) == 0);
if (k_timer_status_get(&timer) > 0) {
goto cleanup;
}

while(gpio_pin_get_dt(spec) != state && k_timer_status_get(&timer) == 0);
if (k_timer_status_get(&timer) > 0) {
goto cleanup;
}

start = k_uptime_ticks();
while(gpio_pin_get_dt(spec) == state && k_timer_status_get(&timer) == 0);
if (k_timer_status_get(&timer) > 0) {
goto cleanup;
}
end = k_uptime_ticks();

delta = k_ticks_to_us_floor64(end - start);

cleanup:
k_timer_stop(&timer);
return (unsigned long)delta;
}

#endif // CONFIG_GPIO_GET_DIRECTION
Loading