Skip to content

Commit bc014ce

Browse files
authored
Merge pull request #4859 from DavePutz/hcsr04_fix
Fix pulsein pause and resume functions to handle HCSR04
2 parents e1ebc37 + 57334c8 commit bc014ce

File tree

1 file changed

+16
-12
lines changed
  • ports/raspberrypi/common-hal/pulseio

1 file changed

+16
-12
lines changed

ports/raspberrypi/common-hal/pulseio/PulseIn.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,13 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
117117
}
118118

119119
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) {
120+
pio_sm_restart(self->state_machine.pio, self->state_machine.state_machine);
120121
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
122+
last_level = self->idle_state;
123+
level_count = 0;
124+
result = 0;
125+
buf_index = 0;
121126
}
122-
123127
void common_hal_pulseio_pulsein_interrupt() {
124128

125129
pulseio_pulsein_obj_t *self = save_self;
@@ -134,13 +138,13 @@ void common_hal_pulseio_pulsein_interrupt() {
134138
} else {
135139
result = level_count;
136140
last_level = level;
137-
level_count = 1;
138-
// Pulses that are londger than MAX_PULSE will return MAX_PULSE
141+
level_count = 0;
142+
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
139143
if (result > MAX_PULSE) {
140144
result = MAX_PULSE;
141145
}
142-
// ignore pulses that are too short
143-
if (result <= MAX_PULSE && result > MIN_PULSE) {
146+
// return pulses that are not too short
147+
if (result > MIN_PULSE) {
144148
self->buffer[buf_index] = (uint16_t)result;
145149
if (self->len < self->maxlen) {
146150
self->len++;
@@ -165,24 +169,24 @@ void common_hal_pulseio_pulsein_interrupt() {
165169
}
166170
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
167171
uint16_t trigger_duration) {
168-
// exec a wait for the selected pin to change state
169-
if (self->idle_state == true) {
170-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
171-
} else {
172-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
173-
}
174172
// Send the trigger pulse.
175173
if (trigger_duration > 0) {
176174
gpio_set_function(self->pin,GPIO_FUNC_SIO);
177175
gpio_set_dir(self->pin,true);
178176
gpio_put(self->pin, !self->idle_state);
179177
common_hal_mcu_delay_us((uint32_t)trigger_duration);
180178
gpio_set_function(self->pin,GPIO_FUNC_PIO0);
181-
common_hal_mcu_delay_us(225);
179+
common_hal_mcu_delay_us(125);
182180
}
183181

184182
// Reconfigure the pin for PIO
185183
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
184+
// exec a wait for the selected pin to change state
185+
if (self->idle_state == true) {
186+
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
187+
} else {
188+
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
189+
}
186190
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
187191
}
188192

0 commit comments

Comments
 (0)