Skip to content
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

Add parameterized 'timeout' to 'pulseIn' function #2158

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions hal/inc/gpio_hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ PinMode HAL_Get_Pin_Mode(pin_t pin);
void HAL_GPIO_Write(pin_t pin, uint8_t value);
int32_t HAL_GPIO_Read(pin_t pin);
uint32_t HAL_Pulse_In(pin_t pin, uint16_t value);
uint32_t HAL_Pulse_In(pin_t pin, uint16_t value, unsigned long timeout);

#ifdef __cplusplus
}
Expand Down
5 changes: 5 additions & 0 deletions hal/src/gcc/gpio_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,8 @@ uint32_t HAL_Pulse_In(pin_t pin, uint16_t value)
return 0;
}


uint32_t HAL_Pulse_In(pin_t pin, uint16_t value, unsigned long timeout)
{
return 0;
}
60 changes: 60 additions & 0 deletions hal/src/nRF52840/gpio_hal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -344,3 +344,63 @@ uint32_t HAL_Pulse_In(pin_t pin, uint16_t value) {
}
#endif
}

/*
* @brief blocking call to measure a high or low pulse
* @returns uint32_t pulse width in microseconds up to a specified number of microseconds timeout,
* returns 0 on specified microsecond timeout error, or invalid pin.
*/
uint32_t HAL_Pulse_In(pin_t pin, uint16_t value, unsigned long timeout) {
#define FAST_READ(pin) ((reg->IN >> pin) & 1UL)

if (!is_valid_pin(pin)) {
return 0;
}

Hal_Pin_Info* PIN_MAP = HAL_Pin_Map();

#if HAL_PLATFORM_IO_EXTENSION && MODULE_FUNCTION != MOD_FUNC_BOOTLOADER
if (PIN_MAP[pin].type == HAL_PIN_TYPE_MCU) {
#endif
uint32_t nrf_pin = NRF_GPIO_PIN_MAP(PIN_MAP[pin].gpio_port, PIN_MAP[pin].gpio_pin);
NRF_GPIO_Type *reg = nrf_gpio_pin_port_decode(&nrf_pin);

volatile uint32_t timeout_start = SYSTEM_TICK_COUNTER;
volatile unsigned long timeoutTicks = 192UL*timeout; // total systems ticks to wait before timeout

/* If already on the value we want to measure, wait for the next one.
* Time out after specified microseconds so we don't block the background tasks
*/
// while (nrf_gpio_pin_read(gpio_pin_map) == value)
while (FAST_READ(nrf_pin) == value) {
if (SYSTEM_TICK_COUNTER - timeout_start > timeoutTicks) {
return 0;
}
}

/* Wait until the start of the pulse.
* Time out after specified microseconds so we don't block the background tasks
*/
while (FAST_READ(nrf_pin) != value) {
if (SYSTEM_TICK_COUNTER - timeout_start > timeoutTicks) {
return 0;
}
}

/* Wait until this value changes, this will be our elapsed pulse width.
* Time out after specified microseconds so we don't block the background tasks
*/
volatile uint32_t pulse_start = SYSTEM_TICK_COUNTER;
while (FAST_READ(nrf_pin) == value) {
if (SYSTEM_TICK_COUNTER - timeout_start > timeoutTicks) {
return 0;
}
}

return (SYSTEM_TICK_COUNTER - pulse_start) / SYSTEM_US_TICKS;
#if HAL_PLATFORM_IO_EXTENSION && MODULE_FUNCTION != MOD_FUNC_BOOTLOADER
} else {
return 0;
}
#endif
}
44 changes: 44 additions & 0 deletions hal/src/stm32f2xx/gpio_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,3 +325,47 @@ uint32_t HAL_Pulse_In(pin_t pin, uint16_t value)

return (SYSTEM_TICK_COUNTER - pulseStart)/SYSTEM_US_TICKS;
}

/*
* @brief blocking call to measure a high or low pulse
* @returns uint32_t pulse width in microseconds up to a specified number of microseconds timeout,
* returns 0 on specified microsecond timeout error, or invalid pin.
*/
uint32_t HAL_Pulse_In(pin_t pin, uint16_t value, unsigned long timeout)
{
Hal_Pin_Info* SOLO_PIN_MAP = HAL_Pin_Map();
#define pinReadFast(_pin) ((SOLO_PIN_MAP[_pin].gpio_peripheral->IDR & SOLO_PIN_MAP[_pin].gpio_pin) == 0 ? 0 : 1)

volatile uint32_t timeoutStart = SYSTEM_TICK_COUNTER;
volatile unsigned long timeoutTicks = 360UL*timeout; // total systems ticks to wait before timeout

/* If already on the value we want to measure, wait for the next one.
* Time out after specified microseconds so we don't block the background tasks
*/
while (pinReadFast(pin) == value) {
if (SYSTEM_TICK_COUNTER - timeoutStart > timeoutTicks) {
return 0;
}
}

/* Wait until the start of the pulse.
* Time out after specified microseconds so we don't block the background tasks
*/
while (pinReadFast(pin) != value) {
if (SYSTEM_TICK_COUNTER - timeoutStart > timeoutTicks) {
return 0;
}
}

/* Wait until this value changes, this will be our elapsed pulse width.
* Time out after specified microseconds so we don't block the background tasks
*/
volatile uint32_t pulseStart = SYSTEM_TICK_COUNTER;
while (pinReadFast(pin) == value) {
if (SYSTEM_TICK_COUNTER - timeoutStart > timeoutTicks) {
return 0;
}
}

return (SYSTEM_TICK_COUNTER - pulseStart)/SYSTEM_US_TICKS;
}
1 change: 1 addition & 0 deletions wiring/inc/spark_wiring.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ uint8_t shiftIn(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder);
void serialReadLine(Stream *serialObj, char *dst, int max_len, system_tick_t timeout);

uint32_t pulseIn(pin_t pin, uint16_t value);
uint32_t pulseIn(pin_t pin, uint16_t value, unsigned long timeout);

#ifdef __cplusplus
}
Expand Down