Skip to content

Commit 9c20eb5

Browse files
authored
Merge pull request #6450 from mwisslead/rp2040_pulsein_common_hal_construct
Rp2040 pulsein improvements
2 parents ee3ccbc + 2b05182 commit 9c20eb5

File tree

2 files changed

+30
-71
lines changed

2 files changed

+30
-71
lines changed

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

Lines changed: 29 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@
3838

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

43-
uint16_t pulsein_program[] = {
43+
static const uint16_t pulsein_program[] = {
4444
0x4001, // 1: in pins, 1
4545
};
4646

@@ -57,48 +57,29 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t *self,
5757
self->start = 0;
5858
self->len = 0;
5959

60-
bool ok = rp2pio_statemachine_construct(&self->state_machine,
61-
pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]),
62-
1000000,
63-
NULL, 0,
64-
NULL, 0,
65-
pin, 1,
66-
0,0,
67-
NULL, 0,
68-
NULL, 0,
69-
1, 0,
70-
NULL, // jump pin
71-
1 << self->pin, false, true,
72-
false, 8, false, // TX, unused
73-
false,
74-
true, 32, true, // RX auto-push every 32 bits
75-
false, // claim pins
76-
false, // Not user-interruptible.
77-
false, // No sideset enable
78-
0, -1); // wrap settings
79-
80-
if (!ok) {
81-
mp_raise_RuntimeError(translate("All state machines in use"));
82-
}
83-
84-
pio_sm_set_enabled(self->state_machine.pio,self->state_machine.state_machine, false);
85-
pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine);
86-
self->last_level = self->idle_state;
87-
self->level_count = 0;
88-
self->buf_index = 0;
60+
common_hal_rp2pio_statemachine_construct(&self->state_machine,
61+
pulsein_program, MP_ARRAY_SIZE(pulsein_program),
62+
1000000, // frequency
63+
NULL, 0, // init, init_len
64+
NULL, 0, 0, 0, // first out pin, # out pins, initial_out_pin_state
65+
pin, 1, 0, 0, // first in pin, # in pins
66+
NULL, 0, 0, 0, // first set pin
67+
NULL, 0, 0, 0, // first sideset pin
68+
false, // No sideset enable
69+
NULL, PULL_NONE, // jump pin, jmp_pull
70+
0, // wait gpio pins
71+
true, // exclusive pin usage
72+
false, 8, false, // TX, setting we don't use
73+
false, // wait for TX stall
74+
true, 32, true, // RX auto pull every 32 bits. shift left to output msb first
75+
false, // Not user-interruptible.
76+
0, -1); // wrap settings
77+
78+
common_hal_pulseio_pulsein_pause(self);
8979

90-
pio_sm_set_in_pins(self->state_machine.pio,self->state_machine.state_machine,pin->number);
9180
common_hal_rp2pio_statemachine_set_interrupt_handler(&(self->state_machine),&common_hal_pulseio_pulsein_interrupt,self,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS);
9281

93-
// exec a set pindirs to 0 for input
94-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0xe080);
95-
// exec the appropriate wait for pin
96-
if (self->idle_state == true) {
97-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
98-
} else {
99-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
100-
}
101-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
82+
common_hal_pulseio_pulsein_resume(self, 0);
10283
}
10384

10485
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t *self) {
@@ -119,9 +100,10 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t *self) {
119100
void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t *self) {
120101
pio_sm_restart(self->state_machine.pio, self->state_machine.state_machine);
121102
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);
122104
self->last_level = self->idle_state;
123105
self->level_count = 0;
124-
self->buf_index = 0;
106+
self->paused = true;
125107
}
126108
void common_hal_pulseio_pulsein_interrupt(void *self_in) {
127109
pulseio_pulsein_obj_t *self = self_in;
@@ -140,7 +122,7 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
140122
} else {
141123
uint32_t result = self->level_count;
142124
self->last_level = level;
143-
self->level_count = 0;
125+
self->level_count = 1;
144126
// Pulses that are longer than MAX_PULSE will return MAX_PULSE
145127
if (result > MAX_PULSE) {
146128
result = MAX_PULSE;
@@ -154,53 +136,36 @@ void common_hal_pulseio_pulsein_interrupt(void *self_in) {
154136
} else {
155137
self->start = (self->start + 1) % self->maxlen;
156138
}
157-
if (self->buf_index < self->maxlen) {
158-
self->buf_index++;
159-
} else {
160-
self->start = 0;
161-
self->buf_index = 0;
162-
}
163139
}
164140
}
165141
}
166142
}
167-
168-
// check for a pulse thats too long (MAX_PULSE us) or maxlen reached, and reset
169-
if ((self->level_count > MAX_PULSE) || (self->buf_index >= self->maxlen)) {
170-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
171-
pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config);
172-
pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine);
173-
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
174-
self->buf_index = 0;
175-
}
176143
}
177144
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t *self,
178145
uint16_t trigger_duration) {
146+
147+
common_hal_pulseio_pulsein_pause(self);
179148
// Send the trigger pulse.
180149
if (trigger_duration > 0) {
181150
gpio_set_function(self->pin,GPIO_FUNC_SIO);
182151
gpio_set_dir(self->pin,true);
183152
gpio_put(self->pin, !self->idle_state);
184153
common_hal_mcu_delay_us((uint32_t)trigger_duration);
185154
gpio_set_function(self->pin,GPIO_FUNC_PIO0);
186-
common_hal_mcu_delay_us(125);
187155
}
188156

189-
// Reconfigure the pin for PIO
190-
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
191157
// exec a wait for the selected pin to change state
192158
if (self->idle_state == true) {
193159
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
194160
} else {
195161
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
196162
}
197163
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
164+
self->paused = false;
198165
}
199166

200167
void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t *self) {
201-
self->start = 0;
202168
self->len = 0;
203-
self->buf_index = 0;
204169
}
205170

206171
uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
@@ -210,12 +175,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t *self) {
210175
uint16_t value = self->buffer[self->start];
211176
self->start = (self->start + 1) % self->maxlen;
212177
self->len--;
213-
// if we are empty reset buffer pointer and counters
214-
if (self->len == 0) {
215-
self->start = 0;
216-
self->buf_index = 0;
217-
self->level_count = 0;
218-
}
219178
return value;
220179
}
221180

@@ -228,7 +187,7 @@ uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t *self) {
228187
}
229188

230189
bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t *self) {
231-
return true;
190+
return self->paused;
232191
}
233192

234193
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)