Skip to content

Commit 0c58cc4

Browse files
authored
Moved pin wait to an initial exec
1 parent f0e3274 commit 0c58cc4

File tree

1 file changed

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

1 file changed

+17
-16
lines changed

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,12 @@ volatile uint16_t result = 0;
4646
volatile uint16_t buf_index = 0;
4747

4848
uint16_t pulsein_program[] = {
49-
0x2020, // 0: wait 0 pin, 0
50-
0xe03f, // 1: set x, 31
51-
0x4001, // 2: in pins, 1
52-
0x0042, // 3: jmp x--, 2
53-
0x8060, // 4: push iffull block
54-
0xc020, // 5: irq wait 0
55-
0x0001, // 6: jmp 1
49+
0xe03f, // 0: set x, 31
50+
0x4001, // 1: in pins, 1
51+
0x0041, // 2: jmp x--, 2
52+
0x8060, // 3: push iffull block
53+
0xc020, // 4: irq wait 0
54+
0x0000, // 5: jmp 1
5655
};
5756

5857
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
@@ -68,10 +67,6 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
6867
self->start = 0;
6968
self->len = 0;
7069
save_self = self;
71-
// change initial state machine wait if idle_state is false
72-
if (idle_state == false) {
73-
pulsein_program[0] = 0x20a0;
74-
}
7570

7671
// Set everything up.
7772
rp2pio_statemachine_obj_t state_machine;
@@ -91,6 +86,7 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
9186
false,
9287
true, 32, true, // RX auto-push every 32 bits
9388
false); // claim pins
89+
pio_sm_set_enabled(state_machine.pio,state_machine.state_machine, false);
9490
self->state_machine.pio = state_machine.pio;
9591
self->state_machine.state_machine = state_machine.state_machine;
9692
self->state_machine.sm_config = state_machine.sm_config;
@@ -107,15 +103,20 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
107103
buf_index = 0;
108104

109105
pio_sm_set_in_pins(state_machine.pio,state_machine.state_machine,pin->number);
110-
pio_sm_set_enabled(state_machine.pio,state_machine.state_machine, false);
111106
irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt);
112107
hw_clear_bits(&state_machine.pio->inte0, 1u << state_machine.state_machine);
113108
hw_set_bits(&state_machine.pio->inte0, 1u << (state_machine.state_machine+8));
114109

115110
// exec a set pindirs to 0 for input
116111
pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080);
117-
irq_set_enabled(self->pio_interrupt, true);
112+
//exec the appropriate wait for pin
113+
if (self->idle_state == true ) {
114+
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
115+
} else {
116+
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
117+
}
118118
pio_sm_set_enabled(state_machine.pio, state_machine.state_machine, true);
119+
irq_set_enabled(self->pio_interrupt, true);
119120
}
120121

121122
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
@@ -177,9 +178,9 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
177178
uint16_t trigger_duration) {
178179
// exec a wait for the selected pin to change state
179180
if (self->idle_state == true ) {
180-
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
181-
} else {
182181
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x2020);
182+
} else {
183+
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
183184
}
184185
// Send the trigger pulse.
185186
if (trigger_duration > 0) {
@@ -191,7 +192,7 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
191192
}
192193

193194
// Reconfigure the pin for PIO
194-
common_hal_mcu_delay_us(100);
195+
common_hal_mcu_delay_us(200);
195196
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
196197
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
197198
}

0 commit comments

Comments
 (0)