Skip to content

Commit 6628f49

Browse files
authored
Use StateMachine.c interrupt setup and simplify SM program
1 parent a3c3e8a commit 6628f49

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
*/
2626

2727
#include "src/rp2_common/hardware_gpio/include/hardware/gpio.h"
28+
<<<<<<< HEAD
29+
=======
2830
#include "src/rp2_common/hardware_irq/include/hardware/irq.h"
31+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
2932

3033
#include <stdint.h>
3134

@@ -46,12 +49,16 @@ volatile uint16_t result = 0;
4649
volatile uint16_t buf_index = 0;
4750

4851
uint16_t pulsein_program[] = {
52+
<<<<<<< HEAD
53+
0x4001, // 1: in pins, 1
54+
=======
4955
0xe03f, // 0: set x, 31
5056
0x4001, // 1: in pins, 1
5157
0x0041, // 2: jmp x--, 2
5258
0x8060, // 3: push iffull block
5359
0xc020, // 4: irq wait 0
5460
0x0000, // 5: jmp 1
61+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
5562
};
5663

5764
void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
@@ -73,7 +80,11 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
7380

7481
bool ok = rp2pio_statemachine_construct(&state_machine,
7582
pulsein_program, sizeof(pulsein_program) / sizeof(pulsein_program[0]),
83+
<<<<<<< HEAD
84+
1000000,
85+
=======
7686
1000000 * 3,
87+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
7788
NULL, 0,
7889
NULL, 0,
7990
pin, 1,
@@ -91,21 +102,28 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
91102
self->state_machine.state_machine = state_machine.state_machine;
92103
self->state_machine.sm_config = state_machine.sm_config;
93104
self->state_machine.offset = state_machine.offset;
105+
<<<<<<< HEAD
106+
=======
94107
if ( self->state_machine.pio == pio0 ) {
95108
self->pio_interrupt = PIO0_IRQ_0;
96109
} else {
97110
self->pio_interrupt = PIO1_IRQ_0;
98111
}
112+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
99113
pio_sm_clear_fifos(self->state_machine.pio,self->state_machine.state_machine);
100114
last_level = self->idle_state;
101115
level_count = 0;
102116
result = 0;
103117
buf_index = 0;
104118

105119
pio_sm_set_in_pins(state_machine.pio,state_machine.state_machine,pin->number);
120+
<<<<<<< HEAD
121+
common_hal_rp2pio_statemachine_set_interrupt_handler(&state_machine,&common_hal_pulseio_pulsein_interrupt,NULL,PIO_IRQ0_INTE_SM0_RXNEMPTY_BITS);
122+
=======
106123
irq_set_exclusive_handler(self->pio_interrupt, common_hal_pulseio_pulsein_interrupt);
107124
hw_clear_bits(&state_machine.pio->inte0, 1u << state_machine.state_machine);
108125
hw_set_bits(&state_machine.pio->inte0, 1u << (state_machine.state_machine+8));
126+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
109127

110128
// exec a set pindirs to 0 for input
111129
pio_sm_exec(state_machine.pio,state_machine.state_machine,0xe080);
@@ -116,7 +134,10 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self,
116134
pio_sm_exec(self->state_machine.pio,self->state_machine.state_machine,0x20a0);
117135
}
118136
pio_sm_set_enabled(state_machine.pio, state_machine.state_machine, true);
137+
<<<<<<< HEAD
138+
=======
119139
irq_set_enabled(self->pio_interrupt, true);
140+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
120141
}
121142

122143
bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) {
@@ -127,7 +148,10 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) {
127148
if (common_hal_pulseio_pulsein_deinited(self)) {
128149
return;
129150
}
151+
<<<<<<< HEAD
152+
=======
130153
irq_set_enabled(self->pio_interrupt, false);
154+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
131155
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
132156
pio_sm_unclaim (self->state_machine.pio, self->state_machine.state_machine);
133157
m_free(self->buffer);
@@ -161,17 +185,23 @@ void common_hal_pulseio_pulsein_interrupt() {
161185
}
162186
}
163187
}
188+
<<<<<<< HEAD
189+
=======
164190
// clear interrupt
165191
irq_clear(self->pio_interrupt);
166192
hw_clear_bits(&self->state_machine.pio->inte0, 1u << self->state_machine.state_machine);
167193
self->state_machine.pio->irq = 1u << self->state_machine.state_machine;
194+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
168195
// check for a pulse thats too long (4000 us) or maxlen reached, and reset
169196
if (( level_count > 4000 ) || (buf_index >= self->maxlen)) {
170197
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, false);
171198
pio_sm_init(self->state_machine.pio, self->state_machine.state_machine, self->state_machine.offset, &self->state_machine.sm_config);
172199
pio_sm_restart(self->state_machine.pio,self->state_machine.state_machine);
173200
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
201+
<<<<<<< HEAD
202+
=======
174203
irq_set_enabled(self->pio_interrupt, true);
204+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
175205
}
176206
}
177207
void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
@@ -189,10 +219,17 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self,
189219
gpio_put(self->pin, !self->idle_state);
190220
common_hal_mcu_delay_us((uint32_t)trigger_duration);
191221
gpio_set_function(self->pin ,GPIO_FUNC_PIO0);
222+
<<<<<<< HEAD
223+
common_hal_mcu_delay_us(225);
224+
}
225+
226+
// Reconfigure the pin for PIO
227+
=======
192228
}
193229

194230
// Reconfigure the pin for PIO
195231
common_hal_mcu_delay_us(200);
232+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
196233
gpio_set_function(self->pin, GPIO_FUNC_PIO0);
197234
pio_sm_set_enabled(self->state_machine.pio, self->state_machine.state_machine, true);
198235
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ typedef struct {
4242
volatile uint16_t start;
4343
volatile uint16_t len;
4444
rp2pio_statemachine_obj_t state_machine;
45+
<<<<<<< HEAD
46+
=======
4547
uint16_t pio_interrupt;
48+
>>>>>>> a3c3e8a0fa5e06910747f1a95a12b899562a618d
4649
} pulseio_pulsein_obj_t;
4750

4851
void pulsein_reset(void);

0 commit comments

Comments
 (0)