Skip to content

Commit

Permalink
zephyrCommon: Implement pulseIn
Browse files Browse the repository at this point in the history
- A basic sync implementation

Signed-off-by: Ayush Singh <ayushdevel1325@gmail.com>
  • Loading branch information
Ayush1325 committed Jun 10, 2024
1 parent 347a80d commit 96b6e8a
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions cores/arduino/zephyrCommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,3 +328,28 @@ void detachInterrupt(pin_size_t pinNumber)
{
setInterruptHandler(pinNumber, nullptr);
}

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

if (!gpio_is_ready_dt(spec)) {
return 0;
}

if (!gpio_pin_is_input_dt(spec)) {
return 0;
}


while(gpio_pin_get_dt(spec) == state);
while(gpio_pin_get_dt(spec) != state);

start = k_uptime_ticks();
while(gpio_pin_get_dt(spec) == state);
end = k_uptime_ticks();

delta = k_ticks_to_us_floor64(end) - k_ticks_to_us_floor64(start);

return (unsigned long)delta;
}

0 comments on commit 96b6e8a

Please sign in to comment.