Skip to content

Commit 2b05182

Browse files
committed
rp2040 PulseIn improvements
1 parent 893fc66 commit 2b05182

File tree

2 files changed

+11
-46
lines changed

2 files changed

+11
-46
lines changed

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

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838

3939
#define NO_PIN 0xff
4040
#define MAX_PULSE 65535
41-
#define MIN_PULSE 10
41+
#define MIN_PULSE 0
4242

4343
static const uint16_t pulsein_program[] = {
4444
0x4001, // 1: in pins, 1
@@ -75,24 +75,11 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
7575
false, // Not user-interruptible.
7676
0, -1); // wrap settings
7777

78-
pio_sm_set_enabled(self->state_machine.pio,self->state_machine.state_machine, false);
79-
pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine);
80-
self->last_level = self->idle_state;
81-
self->level_count = 0;
82-
self->buf_index = 0;
78+
common_hal_pulseio_pulsein_pause(self);
8379

84-
pio_sm_set_in_pins(self->state_machine.pio,self->state_machine.state_machine,pin->number);
8580
common_hal_rp2pio_statemachine_set_interrupt_handler(&(self->state_machine),&common_hal_pulseio_pulsein_interrupt,self,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS);
8681

87-
// exec a set pindirs to 0 for input
88-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0xe080);
89-
// exec the appropriate wait for pin
90-
if (self->idle_state == true) {
91-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
92-
} else {
93-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
94-
}
95-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
82+
common_hal_pulseio_pulsein_resume(self, 0);
9683
}
9784

9885
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) {
@@ -113,9 +100,10 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
113100
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) {
114101
pio_sm_restart(self->state_machine.pio, self->state_machine.state_machine);
115102
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
103+
pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine);
116104
self->last_level = self->idle_state;
117105
self->level_count = 0;
118-
self->buf_index = 0;
106+
self->paused = true;
119107
}
120108
void common_hal_pulseio_pulsein_interrupt(void *self_in) {
121109
pulseio_pulsein_obj_t *self = self_in;
@@ -134,7 +122,7 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
134122
} else {
135123
uint32_t result = self->level_count;
136124
self->last_level = level;
137-
self->level_count = 0;
125+
self->level_count = 1;
138126
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
139127
if (result > MAX_PULSE) {
140128
result = MAX_PULSE;
@@ -148,53 +136,36 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
148136
} else {
149137
self->start = (self->start + 1) % self->maxlen;
150138
}
151-
if (self->buf_index < self->maxlen) {
152-
self->buf_index++;
153-
} else {
154-
self->start = 0;
155-
self->buf_index = 0;
156-
}
157139
}
158140
}
159141
}
160142
}
161-
162-
// check for a pulse thats too long (MAX_PULSE us) or maxlen reached, and reset
163-
if ((self->level_count > MAX_PULSE) || (self->buf_index >= self->maxlen)) {
164-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
165-
pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config);
166-
pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine);
167-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
168-
self->buf_index = 0;
169-
}
170143
}
171144
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
172145
uint16_t trigger_duration) {
146+
147+
common_hal_pulseio_pulsein_pause(self);
173148
// Send the trigger pulse.
174149
if (trigger_duration > 0) {
175150
gpio_set_function(self->pin,GPIO_FUNC_SIO);
176151
gpio_set_dir(self->pin,true);
177152
gpio_put(self->pin, !self->idle_state);
178153
common_hal_mcu_delay_us((uint32_t)trigger_duration);
179154
gpio_set_function(self->pin,GPIO_FUNC_PIO0);
180-
common_hal_mcu_delay_us(125);
181155
}
182156

183-
// Reconfigure the pin for PIO
184-
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
185157
// exec a wait for the selected pin to change state
186158
if (self->idle_state == true) {
187159
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
188160
} else {
189161
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
190162
}
191163
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
164+
self->paused = false;
192165
}
193166

194167
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) {
195-
self->start = 0;
196168
self->len = 0;
197-
self->buf_index = 0;
198169
}
199170

200171
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
@@ -204,12 +175,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
204175
uint16_t value = self->buffer[self->start];
205176
self->start = (self->start + 1) % self->maxlen;
206177
self->len--;
207-
// if we are empty reset buffer pointer and counters
208-
if (self->len == 0) {
209-
self->start = 0;
210-
self->buf_index = 0;
211-
self->level_count = 0;
212-
}
213178
return value;
214179
}
215180

@@ -222,7 +187,7 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
222187
}
223188

224189
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
225-
return true;
190+
return self->paused;
226191
}
227192

228193
uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t *self,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ typedef struct {
3737
mp_obj_base_t base;
3838
uint8_t pin;
3939
bool idle_state;
40+
bool paused;
4041
uint16_t maxlen;
4142
uint16_t *buffer;
4243
volatile bool last_level;
4344
volatile uint32_t level_count;
4445
volatile uint16_t len;
4546
volatile uint16_t start;
46-
volatile uint16_t buf_index;
4747
rp2pio_statemachine_obj_t state_machine;
4848
} pulseio_pulsein_obj_t;
4949

0 commit comments

Comments
 (0)