Skip to content

Fix pulsein pause and resume functions to handle HCSR04 #4859

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 4, 2021
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
28 changes: 16 additions & 12 deletions ports/raspberrypi/common-hal/pulseio/PulseIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,13 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
}

void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) {
pio_sm_restart(self->state_machine.pio, self->state_machine.state_machine);
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
last_level = self->idle_state;
level_count = 0;
result = 0;
buf_index = 0;
}

void common_hal_pulseio_pulsein_interrupt() {

pulseio_pulsein_obj_t *self = save_self;
Expand All @@ -134,13 +138,13 @@ void common_hal_pulseio_pulsein_interrupt() {
} else {
result = level_count;
last_level = level;
level_count = 1;
// Pulses that are londger than MAX_PULSE will return MAX_PULSE
level_count = 0;
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
if (result > MAX_PULSE) {
result = MAX_PULSE;
}
// ignore pulses that are too short
if (result <= MAX_PULSE && result > MIN_PULSE) {
// return pulses that are not too short
if (result > MIN_PULSE) {
self->buffer[buf_index] = (uint16_t)result;
if (self->len < self->maxlen) {
self->len++;
Expand All @@ -165,24 +169,24 @@ void common_hal_pulseio_pulsein_interrupt() {
}
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
uint16_t trigger_duration) {
// exec a wait for the selected pin to change state
if (self->idle_state == true) {
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
} else {
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
}
// Send the trigger pulse.
if (trigger_duration > 0) {
gpio_set_function(self->pin,GPIO_FUNC_SIO);
gpio_set_dir(self->pin,true);
gpio_put(self->pin, !self->idle_state);
common_hal_mcu_delay_us((uint32_t)trigger_duration);
gpio_set_function(self->pin,GPIO_FUNC_PIO0);
common_hal_mcu_delay_us(225);
common_hal_mcu_delay_us(125);
}

// Reconfigure the pin for PIO
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
// exec a wait for the selected pin to change state
if (self->idle_state == true) {
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
} else {
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
}
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
}

Expand Down